Thanks to visit codestin.com
Credit goes to code.neomutt.org

NeoMutt  2025-12-11-189-gceedb6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
preview.c
Go to the documentation of this file.
1
22
59
60#include "config.h"
61#include <stdbool.h>
62#include <stdio.h>
63#include "private.h"
64#include "mutt/lib.h"
65#include "email/lib.h"
66#include "core/lib.h"
67#include "gui/lib.h"
68#include "color/lib.h"
69#include "key/lib.h"
70
71// Maxixum body size in bytes to show in preview.
72const long MAX_PREVIEW_BODY_SIZE = 1024 * 1024 * 5;
73
85
99typedef int (*preview_function_t)(struct PreviewWindowData *wdata,
100 const struct KeyEvent *event);
101
110
114static void preview_wdata_free(struct MuttWindow *win, void **ptr)
115{
116 if (!ptr || !*ptr)
117 return;
118
119 FREE(ptr);
120}
121
127{
128 struct PreviewWindowData *wdata = mutt_mem_calloc(1, sizeof(struct PreviewWindowData));
129
130 return wdata;
131}
132
138static void draw_preview(struct MuttWindow *win, struct PreviewWindowData *wdata)
139{
140 struct Email *e = wdata->email;
141
142 // Reset preview window and status bar.
144 sbar_set_title(wdata->bar, _("-- Preview"));
145
146 // Check for valid content type and disposition
147 if ((e->body->disposition != DISP_INLINE) || (e->body->type != TYPE_TEXT))
148 {
149 mutt_error(_("Only inline attachments with content-type text/* can be previewed"));
150 return;
151 }
152
153 // Ensure file isn't to too large.
154 long file_size = mutt_file_get_size(e->body->filename);
155 if (file_size > MAX_PREVIEW_BODY_SIZE)
156 {
157 mutt_error(_("Email too large to preview"));
158 return;
159 }
160
161 FILE *fp = mutt_file_fopen(e->body->filename, "r");
162 if (!fp)
163 {
164 mutt_perror("%s", e->body->filename);
165 return;
166 }
167
168 wdata->more_content = false;
169
170 int content_lines = 0; // number of (wrapped) content lines
171 int row = 0; // window row to print
172 char *line = NULL;
173 size_t line_len = 0;
174 while ((line = mutt_file_read_line(line, &line_len, fp, NULL, MUTT_RL_NO_FLAGS)))
175 {
176 size_t pos = 0;
177 bool text_left = true;
178 while (text_left)
179 {
180 /* Text wrapping loop
181 *
182 * Note: We need to do the text wrapping also for text outside the visible
183 * area to ensure the scrolling works correctly.
184 */
185
186 content_lines++;
187
188 line = mutt_str_expand_tabs(line, &line_len, 8);
189
190 // Check how much of the string fits into the window width.
191 size_t width = 0;
192 size_t bytes = mutt_wstr_trunc(&line[pos], line_len - pos, win->state.cols, &width);
193
194 // If it doesn't fill the full width we're done wrapping.
195 if ((win->state.cols - width) > 0)
196 text_left = false;
197
198 // Only move the cursor and print if this line is currently visible.
199 if ((content_lines >= wdata->scroll_offset) && (row < win->state.rows))
200 {
201 int rc = mutt_window_move(win, row, 0);
202 if (rc == ERR)
203 mutt_warning(_("Failed to move cursor!"));
204
205 mutt_paddstr(win, win->state.cols, &line[pos]);
206
207 row++;
208 }
209
210 // Advance position in string.
211 pos += bytes;
212 }
213 }
214
215 FREE(&line);
216 mutt_file_fclose(&fp);
217
218 // Show the scroll percentage in the status bar
219 if ((content_lines != 0) && (content_lines > win->state.rows))
220 {
221 char title[256] = { 0 };
222 double percent = 100.0;
223 if ((wdata->scroll_offset + row) < content_lines)
224 percent = 100.0 / content_lines * (wdata->scroll_offset + row);
225
226 // TODO: having the percentage right-aligned would be nice
227 snprintf(title, sizeof(title), _("-- Preview (%.0f%%)"), percent);
228 sbar_set_title(wdata->bar, title);
229
230 if (content_lines > (wdata->scroll_offset + row))
231 wdata->more_content = true;
232 }
233}
234
239{
240 if (nc->event_type != NT_COLOR)
241 return 0;
242 if (!nc->global_data || !nc->event_data)
243 return -1;
244
245 struct EventColor *ev_c = nc->event_data;
246 struct MuttWindow *win = nc->global_data;
247
248 enum ColorId cid = ev_c->cid;
249
250 switch (cid)
251 {
252 case MT_COLOR_BOLD:
253 case MT_COLOR_NORMAL:
254 case MT_COLOR_STATUS:
255 case MT_COLOR_MAX: // Sent on `uncolor *`
256 mutt_debug(LL_DEBUG5, "color done, request WA_REPAINT\n");
257 win->actions |= WA_REPAINT;
258 break;
259
260 default:
261 break;
262 }
263 return 0;
264}
265
270{
271 if (nc->event_type != NT_EMAIL)
272 return 0;
273 if (!nc->global_data)
274 return -1;
275
276 struct MuttWindow *win = nc->global_data;
277
278 win->actions |= WA_RECALC;
279 mutt_debug(LL_DEBUG5, "email done, request WA_RECALC\n");
280 return 0;
281}
282
287{
288 if (nc->event_type != NT_WINDOW)
289 return 0;
290 if (!nc->global_data || !nc->event_data)
291 return -1;
292
293 struct MuttWindow *win = nc->global_data;
294 struct EventWindow *ev_w = nc->event_data;
295 if (ev_w->win != win)
296 return 0;
297
299 {
301 mutt_debug(LL_DEBUG5, "window state done, request WA_RECALC\n");
302 }
303 else if (nc->event_subtype == NT_WINDOW_DELETE)
304 {
305 struct PreviewWindowData *wdata = win->wdata;
306
310 mutt_debug(LL_DEBUG5, "window delete done\n");
311 }
312
313 return 0;
314}
315
319static int preview_repaint(struct MuttWindow *win)
320{
321 struct PreviewWindowData *wdata = win->wdata;
322 draw_preview(win, wdata);
323
324 mutt_debug(LL_DEBUG5, "repaint done\n");
325 return 0;
326}
327
331static int preview_recalc(struct MuttWindow *win)
332{
333 if (!win)
334 return -1;
335
337 mutt_debug(LL_DEBUG5, "recalc done, request WA_REPAINT\n");
338 return 0;
339}
340
369
373static int preview_page_up(struct PreviewWindowData *wdata, const struct KeyEvent *event)
374{
375 if (wdata->scroll_offset <= 0)
376 return FR_NO_ACTION;
377
378 wdata->scroll_offset -= MAX(wdata->win->state.rows - 1, 1);
379 draw_preview(wdata->win, wdata);
380
381 return FR_SUCCESS;
382}
383
387static int preview_page_down(struct PreviewWindowData *wdata, const struct KeyEvent *event)
388{
389 if (!wdata->more_content)
390 return FR_NO_ACTION;
391
392 wdata->scroll_offset += MAX(wdata->win->state.rows - 1, 1);
393 draw_preview(wdata->win, wdata);
394
395 return FR_SUCCESS;
396}
397
401static const struct PreviewFunction PreviewFunctions[] = {
402 // clang-format off
403 { OP_PREVIEW_PAGE_DOWN, preview_page_down },
404 { OP_PREVIEW_PAGE_UP, preview_page_up },
405 { 0, NULL },
406 // clang-format on
407};
408
412int preview_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
413{
414 if (!event || !win || !win->wdata)
415 return FR_UNKNOWN;
416
417 const int op = event->op;
418 int rc = FR_UNKNOWN;
419 for (size_t i = 0; PreviewFunctions[i].op != OP_NULL; i++)
420 {
421 const struct PreviewFunction *fn = &PreviewFunctions[i];
422 if (fn->op == op)
423 {
424 struct PreviewWindowData *wdata = win->wdata;
425 rc = fn->function(wdata, event);
426 break;
427 }
428 }
429
430 if (rc == FR_UNKNOWN) // Not our function
431 return rc;
432
433 const char *result = dispatcher_get_retval_name(rc);
434 mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
435
436 return rc;
437}
Color and attribute parsing.
void mutt_color_observer_remove(observer_t callback, void *global_data)
Remove an observer.
Definition notify.c:71
void mutt_color_observer_add(observer_t callback, void *global_data)
Add an observer.
Definition notify.c:61
ColorId
List of all coloured objects.
Definition color.h:36
@ MT_COLOR_MAX
Definition color.h:98
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition color.h:79
@ MT_COLOR_BOLD
Bold text.
Definition color.h:41
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:54
Compose Private Data.
Convenience wrapper for the core headers.
size_t mutt_wstr_trunc(const char *src, size_t maxlen, size_t maxwid, size_t *width)
Work out how to truncate a widechar string.
Definition curs_lib.c:384
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition curs_lib.c:342
char * mutt_str_expand_tabs(char *str, size_t *len, int tabwidth)
Convert tabs to spaces in a string.
Definition curs_lib.c:591
const char * dispatcher_get_retval_name(int rv)
Get the name of a return value.
Definition dispatcher.c:54
@ FR_SUCCESS
Valid function - successfully performed.
Definition dispatcher.h:40
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
@ FR_NO_ACTION
Valid function - no action performed.
Definition dispatcher.h:38
Structs that make up an email.
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition file.c:686
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1413
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition file.h:40
int preview_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a preview function - Implements function_dispatcher_t -.
Definition preview.c:412
#define mutt_warning(...)
Definition logging2.h:92
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
#define mutt_perror(...)
Definition logging2.h:95
static int preview_email_observer(struct NotifyCallback *nc)
Notification that the Email has changed - Implements observer_t -.
Definition preview.c:269
static int preview_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition preview.c:286
static int preview_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition preview.c:238
static int preview_page_down(struct PreviewWindowData *wdata, const struct KeyEvent *event)
Show the previous page of the message - Implements preview_function_t -.
Definition preview.c:387
static int preview_page_up(struct PreviewWindowData *wdata, const struct KeyEvent *event)
Show the previous page of the message - Implements preview_function_t -.
Definition preview.c:373
static int preview_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition preview.c:331
static int preview_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition preview.c:319
static void preview_wdata_free(struct MuttWindow *win, void **ptr)
Free the Preview Data - Implements MuttWindow::wdata_free() -.
Definition preview.c:114
Convenience wrapper for the gui headers.
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:76
#define FREE(x)
Definition memory.h:63
#define MAX(a, b)
Definition memory.h:37
@ TYPE_TEXT
Type: 'text/*'.
Definition mime.h:38
@ DISP_INLINE
Content is inline.
Definition mime.h:62
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition notify.c:230
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition notify.c:191
void mutt_window_clear(struct MuttWindow *win)
Clear a Window.
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
#define WA_RECALC
Recalculate the contents of the Window.
@ WT_CUSTOM
Window with a custom drawing function.
Definition mutt_window.h:95
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition mutt_window.h:39
@ NT_WINDOW_STATE
Window state has changed, e.g. WN_VISIBLE.
@ NT_WINDOW_DELETE
Window is about to be deleted.
#define WA_REPAINT
Redraw the contents of the Window.
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition mutt_window.h:53
@ MUTT_WIN_SIZE_MAXIMISE
Window wants as much space as possible.
Definition mutt_window.h:49
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:57
@ NT_COLOR
Colour has changed, NotifyColor, EventColor.
Definition notify_type.h:41
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition notify_type.h:44
@ NT_ALL
Register for all notifications.
Definition notify_type.h:35
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
struct MuttWindow * preview_window_new(struct Email *e, struct MuttWindow *bar)
Create the preview window.
Definition preview.c:346
static const struct PreviewFunction PreviewFunctions[]
All the functions that the preview window supports.
Definition preview.c:401
static void draw_preview(struct MuttWindow *win, struct PreviewWindowData *wdata)
Write the message preview to the compose window.
Definition preview.c:138
const long MAX_PREVIEW_BODY_SIZE
Definition preview.c:72
static struct PreviewWindowData * preview_wdata_new(void)
Create new Preview Data.
Definition preview.c:126
int(* preview_function_t)(struct PreviewWindowData *wdata, const struct KeyEvent *event)
Definition preview.c:99
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition sbar.c:227
#define NONULL(x)
Definition string2.h:44
unsigned int disposition
content-disposition, ContentDisposition
Definition body.h:42
unsigned int type
content-type primary type, ContentType
Definition body.h:40
char * filename
When sending a message, this is the file to which this structure refers.
Definition body.h:59
The envelope/body of an email.
Definition email.h:39
struct Body * body
List of MIME parts.
Definition email.h:69
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition email.h:73
An Event that happened to a Colour.
Definition notify2.h:55
enum ColorId cid
Colour ID that has changed.
Definition notify2.h:56
An Event that happened to a Window.
struct MuttWindow * win
Window that changed.
An event such as a keypress.
Definition get.h:50
int(* repaint)(struct MuttWindow *win)
struct WindowState state
Current state of the Window.
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
WindowActionFlags actions
Actions to be performed, e.g. WA_RECALC.
Data passed to a notification function.
Definition observer.h:34
void * event_data
Data from notify_send()
Definition observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition observer.h:37
void * global_data
Data from notify_observer_add()
Definition observer.h:39
A message preview function.
Definition preview.c:106
int op
Op code, e.g. OP_NEXT_PAGE.
Definition preview.c:107
preview_function_t function
Function to call.
Definition preview.c:108
Data to fill the Preview Window.
Definition preview.c:78
struct MuttWindow * win
Window holding the message preview.
Definition preview.c:81
struct MuttWindow * bar
Status bar above the preview window.
Definition preview.c:82
int scroll_offset
Scroll offset.
Definition preview.c:80
struct Email * email
Email being composed.
Definition preview.c:79
bool more_content
Is there more content to scroll down to?
Definition preview.c:83
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:61
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:62