NeoMutt  2025-12-11-911-gd8d604
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
preview.c File Reference

Compose message preview. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "color/lib.h"
#include "key/lib.h"
+ Include dependency graph for preview.c:

Go to the source code of this file.

Data Structures

struct  PreviewWindowData
 Data to fill the Preview Window. More...
 
struct  PreviewFunction
 A message preview function. More...
 

Typedefs

typedef int(* preview_function_t) (struct PreviewWindowData *wdata, const struct KeyEvent *event)
 

Functions

static void preview_wdata_free (struct MuttWindow *win, void **ptr)
 Free the Preview Data - Implements MuttWindow::wdata_free() -.
 
static struct PreviewWindowDatapreview_wdata_new (void)
 Create new Preview Data.
 
static void draw_preview (struct MuttWindow *win, struct PreviewWindowData *wdata)
 Write the message preview to the compose window.
 
static int preview_color_observer (struct NotifyCallback *nc)
 Notification that a Color has changed - Implements observer_t -.
 
static int preview_email_observer (struct NotifyCallback *nc)
 Notification that the Email has changed - Implements observer_t -.
 
static int preview_window_observer (struct NotifyCallback *nc)
 Notification that a Window has changed - Implements observer_t -.
 
static int preview_repaint (struct MuttWindow *win)
 Repaint the Window - Implements MuttWindow::repaint() -.
 
static int preview_recalc (struct MuttWindow *win)
 Recalculate the Window data - Implements MuttWindow::recalc() -.
 
struct MuttWindowpreview_window_new (struct Email *e, struct MuttWindow *bar)
 Create the preview window.
 
static int preview_page_up (struct PreviewWindowData *wdata, const struct KeyEvent *event)
 Show the previous page of the message - Implements preview_function_t -.
 
static int preview_page_down (struct PreviewWindowData *wdata, const struct KeyEvent *event)
 Show the next page of the message - Implements preview_function_t -.
 
int preview_function_dispatcher (struct MuttWindow *win, const struct KeyEvent *event)
 Perform a preview function - Implements function_dispatcher_t -.
 

Variables

const long MAX_PREVIEW_BODY_SIZE = 1024 * 1024 * 5
 
static const struct PreviewFunction PreviewFunctions []
 All the functions that the preview window supports.
 

Detailed Description

Compose message preview.

Authors
  • Dennis Schön

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file preview.c.

Typedef Documentation

◆ preview_function_t

typedef int(* preview_function_t) (struct PreviewWindowData *wdata, const struct KeyEvent *event)

Definition at line 100 of file preview.c.

Function Documentation

◆ preview_wdata_new()

static struct PreviewWindowData * preview_wdata_new ( void )
static

Create new Preview Data.

Return values
ptrNew Preview Data

Definition at line 127 of file preview.c.

128{
129 struct PreviewWindowData *wdata = mutt_mem_calloc(1, sizeof(struct PreviewWindowData));
130
131 return wdata;
132}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:76
Data to fill the Preview Window.
Definition preview.c:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ draw_preview()

static void draw_preview ( struct MuttWindow * win,
struct PreviewWindowData * wdata )
static

Write the message preview to the compose window.

Parameters
winWindow to draw on
wdataPreview Window data

Definition at line 139 of file preview.c.

140{
141 struct Email *e = wdata->email;
142
143 // Reset preview window and status bar.
145 sbar_set_title(wdata->bar, _("-- Preview"));
146
147 // Check for valid content type and disposition
148 if ((e->body->disposition != DISP_INLINE) || (e->body->type != TYPE_TEXT))
149 {
150 mutt_error(_("Only inline attachments with content-type text/* can be previewed"));
151 return;
152 }
153
154 // Ensure file isn't to too large.
155 long file_size = mutt_file_get_size(e->body->filename);
156 if (file_size > MAX_PREVIEW_BODY_SIZE)
157 {
158 mutt_error(_("Email too large to preview"));
159 return;
160 }
161
162 FILE *fp = mutt_file_fopen(e->body->filename, "r");
163 if (!fp)
164 {
165 mutt_perror("%s", e->body->filename);
166 return;
167 }
168
169 wdata->more_content = false;
170
171 int content_lines = 0; // number of (wrapped) content lines
172 int row = 0; // window row to print
173 char *line = NULL;
174 size_t line_len = 0;
175 while ((line = mutt_file_read_line(line, &line_len, fp, NULL, MUTT_RL_NONE)))
176 {
177 size_t pos = 0;
178 bool text_left = true;
179 while (text_left)
180 {
181 /* Text wrapping loop
182 *
183 * Note: We need to do the text wrapping also for text outside the visible
184 * area to ensure the scrolling works correctly.
185 */
186
187 content_lines++;
188
189 mutt_str_expand_tabs(&line, &line_len, 8);
190
191 // Check how much of the string fits into the window width.
192 size_t width = 0;
193 size_t bytes = mutt_wstr_trunc(&line[pos], line_len - pos, win->state.cols, &width);
194
195 // If it doesn't fill the full width we're done wrapping.
196 if ((win->state.cols - width) > 0)
197 text_left = false;
198
199 // Only move the cursor and print if this line is currently visible.
200 if ((content_lines >= wdata->scroll_offset) && (row < win->state.rows))
201 {
202 int rc = mutt_window_move(win, row, 0);
203 if (rc == ERR)
204 mutt_warning(_("Failed to move cursor!"));
205
206 mutt_paddstr(win, win->state.cols, &line[pos]);
207
208 row++;
209 }
210
211 // Advance position in string.
212 pos += bytes;
213 }
214 }
215
216 FREE(&line);
217 mutt_file_fclose(&fp);
218
219 // Store content_lines for boundary checking
220 wdata->content_lines = content_lines;
221
222 // Clamp scroll_offset to prevent empty pages
223 // Maximum valid offset is when we can still show at least one line of content
224 if (content_lines > 0)
225 {
226 int max_offset = MAX(0, content_lines - win->state.rows);
227 if (wdata->scroll_offset > max_offset)
228 wdata->scroll_offset = max_offset;
229 }
230
231 // Show the scroll percentage in the status bar
232 if ((content_lines != 0) && (content_lines > win->state.rows))
233 {
234 char title[256] = { 0 };
235 double percent = 100.0;
236 if ((wdata->scroll_offset + row) < content_lines)
237 percent = 100.0 / content_lines * (wdata->scroll_offset + row);
238
239 // TODO: having the percentage right-aligned would be nice
240 snprintf(title, sizeof(title), _("-- Preview (%.0f%%)"), percent);
241 sbar_set_title(wdata->bar, title);
242
243 if (content_lines > (wdata->scroll_offset + row))
244 wdata->more_content = true;
245 }
246}
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:386
void mutt_paddstr(struct MuttWindow *win, int n, const char *s)
Display a string on screen, padded if necessary.
Definition curs_lib.c:344
void mutt_str_expand_tabs(char **str, size_t *len, int tabwidth)
Convert tabs to spaces in a string.
Definition curs_lib.c:597
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:678
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition file.c:1414
#define mutt_file_fclose(FP)
Definition file.h:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
@ MUTT_RL_NONE
No flags are set.
Definition file.h:43
#define mutt_warning(...)
Definition logging2.h:92
#define mutt_error(...)
Definition logging2.h:94
#define mutt_perror(...)
Definition logging2.h:95
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MAX(a, b)
Return the maximum of two values.
Definition memory.h:38
@ TYPE_TEXT
Type: 'text/*'.
Definition mime.h:38
@ DISP_INLINE
Content is inline.
Definition mime.h:62
#define _(a)
Definition message.h:28
void mutt_window_clear(struct MuttWindow *win)
Clear a Window.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
const long MAX_PREVIEW_BODY_SIZE
Definition preview.c:72
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition sbar.c:227
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 WindowState state
Current state of the Window.
struct MuttWindow * bar
Status bar above the preview window.
Definition preview.c:82
int content_lines
Total number of wrapped content lines.
Definition preview.c:84
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:60
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition mutt_window.h:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ preview_window_new()

struct MuttWindow * preview_window_new ( struct Email * e,
struct MuttWindow * bar )

Create the preview window.

Parameters
eEmail
barPreview Bar

Definition at line 359 of file preview.c.

360{
364
368
369 struct PreviewWindowData *wdata = preview_wdata_new();
370 wdata->email = e;
371 wdata->scroll_offset = 0;
372 wdata->win = win;
373 wdata->bar = bar;
374
375 win->wdata = wdata;
379
380 return win;
381}
void mutt_color_observer_add(observer_t callback, void *global_data)
Add an observer.
Definition notify.c:62
static int preview_email_observer(struct NotifyCallback *nc)
Notification that the Email has changed - Implements observer_t -.
Definition preview.c:282
static int preview_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition preview.c:299
static int preview_color_observer(struct NotifyCallback *nc)
Notification that a Color has changed - Implements observer_t -.
Definition preview.c:251
static int preview_recalc(struct MuttWindow *win)
Recalculate the Window data - Implements MuttWindow::recalc() -.
Definition preview.c:344
static int preview_repaint(struct MuttWindow *win)
Repaint the Window - Implements MuttWindow::repaint() -.
Definition preview.c:332
static void preview_wdata_free(struct MuttWindow *win, void **ptr)
Free the Preview Data - Implements MuttWindow::wdata_free() -.
Definition preview.c:115
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
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new 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:38
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition mutt_window.h:52
@ MUTT_WIN_SIZE_MAXIMISE
Window wants as much space as possible.
Definition mutt_window.h:48
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:58
@ NT_ALL
Register for all notifications.
Definition notify_type.h:35
static struct PreviewWindowData * preview_wdata_new(void)
Create new Preview Data.
Definition preview.c:127
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition email.h:73
int(* repaint)(struct MuttWindow *win)
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
int(* recalc)(struct MuttWindow *win)
void(* wdata_free)(struct MuttWindow *win, void **ptr)
struct MuttWindow * win
Window holding the message preview.
Definition preview.c:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MAX_PREVIEW_BODY_SIZE

const long MAX_PREVIEW_BODY_SIZE = 1024 * 1024 * 5

Definition at line 72 of file preview.c.

◆ PreviewFunctions

const struct PreviewFunction PreviewFunctions[]
static
Initial value:
= {
{ OP_PREVIEW_PAGE_DOWN, preview_page_down },
{ OP_PREVIEW_PAGE_UP, preview_page_up },
{ 0, NULL },
}
static int preview_page_down(struct PreviewWindowData *wdata, const struct KeyEvent *event)
Show the next page of the message - Implements preview_function_t -.
Definition preview.c:408
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:386

All the functions that the preview window supports.

Definition at line 428 of file preview.c.

428 {
429 // clang-format off
430 { OP_PREVIEW_PAGE_DOWN, preview_page_down },
431 { OP_PREVIEW_PAGE_UP, preview_page_up },
432 { 0, NULL },
433 // clang-format on
434};