NeoMutt  2025-09-05-55-g97fc89
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
draw.c
Go to the documentation of this file.
1
23
29
30#include "config.h"
31#include <stdbool.h>
32#include <string.h>
33#include <wchar.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "email/lib.h"
37#include "gui/lib.h"
38#include "lib.h"
39#include "color/lib.h"
40#include "index/lib.h"
41#include "pattern/lib.h"
42#include "mutt_thread.h"
43#include "mview.h"
44
54static const struct AttrColor *get_color(int index, unsigned char *s)
55{
56 const int type = *s;
57 struct RegexColorList *rcl = regex_colors_get_list(type);
58 struct Mailbox *m_cur = get_current_mailbox();
59 struct Email *e = mutt_get_virt_email(m_cur, index);
60 if (!rcl || !e)
61 {
62 return simple_color_get(type);
63 }
64
65 struct RegexColor *np = NULL;
66
67 if (type == MT_COLOR_INDEX_TAG)
68 {
69 const struct AttrColor *ac_merge = NULL;
70 STAILQ_FOREACH(np, rcl, entries)
71 {
72 if (mutt_strn_equal((const char *) (s + 1), np->pattern, strlen(np->pattern)))
73 {
74 ac_merge = merged_color_overlay(ac_merge, &np->attr_color);
75 continue;
76 }
77 const char *transform = mutt_hash_find(TagTransforms, np->pattern);
78 if (transform && mutt_strn_equal((const char *) (s + 1), transform, strlen(transform)))
79 {
80 ac_merge = merged_color_overlay(ac_merge, &np->attr_color);
81 }
82 }
83 return ac_merge;
84 }
85
86 const struct AttrColor *ac_merge = NULL;
87 STAILQ_FOREACH(np, rcl, entries)
88 {
90 MUTT_MATCH_FULL_ADDRESS, m_cur, e, NULL))
91 {
92 ac_merge = merged_color_overlay(ac_merge, &np->attr_color);
93 }
94 }
95
96 return ac_merge;
97}
98
108static void print_enriched_string(struct MuttWindow *win, int index,
109 const struct AttrColor *ac_def, struct AttrColor *ac_ind,
110 struct Buffer *buf, struct ConfigSubset *sub)
111{
112 wchar_t wc = 0;
113 size_t k;
114 size_t n = mutt_str_len(buf_string(buf));
115 unsigned char *s = (unsigned char *) buf->data;
116 mbstate_t mbstate = { 0 };
117
118 const bool c_ascii_chars = cs_subset_bool(sub, "ascii_chars");
119 while (*s)
120 {
121 if (*s < MUTT_TREE_MAX)
122 {
123 /* Combining tree fg color and another bg color requires having
124 * use_default_colors, because the other bg color may be undefined. */
125 mutt_curses_set_color(ac_ind);
126
127 while (*s && (*s < MUTT_TREE_MAX))
128 {
129 switch (*s)
130 {
132 if (c_ascii_chars)
133 mutt_window_addch(win, '`');
134#ifdef WACS_LLCORNER
135 else
136 add_wch(WACS_LLCORNER);
137#else
138 else if (CharsetIsUtf8)
139 mutt_window_addstr(win, "\342\224\224"); /* WACS_LLCORNER */
140 else
141 mutt_window_addch(win, ACS_LLCORNER);
142#endif
143 break;
145 if (c_ascii_chars)
146 mutt_window_addch(win, ',');
147#ifdef WACS_ULCORNER
148 else
149 add_wch(WACS_ULCORNER);
150#else
151 else if (CharsetIsUtf8)
152 mutt_window_addstr(win, "\342\224\214"); /* WACS_ULCORNER */
153 else
154 mutt_window_addch(win, ACS_ULCORNER);
155#endif
156 break;
157 case MUTT_TREE_LTEE:
158 if (c_ascii_chars)
159 mutt_window_addch(win, '|');
160#ifdef WACS_LTEE
161 else
162 add_wch(WACS_LTEE);
163#else
164 else if (CharsetIsUtf8)
165 mutt_window_addstr(win, "\342\224\234"); /* WACS_LTEE */
166 else
167 mutt_window_addch(win, ACS_LTEE);
168#endif
169 break;
170 case MUTT_TREE_HLINE:
171 if (c_ascii_chars)
172 mutt_window_addch(win, '-');
173#ifdef WACS_HLINE
174 else
175 add_wch(WACS_HLINE);
176#else
177 else if (CharsetIsUtf8)
178 mutt_window_addstr(win, "\342\224\200"); /* WACS_HLINE */
179 else
180 mutt_window_addch(win, ACS_HLINE);
181#endif
182 break;
183 case MUTT_TREE_VLINE:
184 if (c_ascii_chars)
185 mutt_window_addch(win, '|');
186#ifdef WACS_VLINE
187 else
188 add_wch(WACS_VLINE);
189#else
190 else if (CharsetIsUtf8)
191 mutt_window_addstr(win, "\342\224\202"); /* WACS_VLINE */
192 else
193 mutt_window_addch(win, ACS_VLINE);
194#endif
195 break;
196 case MUTT_TREE_TTEE:
197 if (c_ascii_chars)
198 mutt_window_addch(win, '-');
199#ifdef WACS_TTEE
200 else
201 add_wch(WACS_TTEE);
202#else
203 else if (CharsetIsUtf8)
204 mutt_window_addstr(win, "\342\224\254"); /* WACS_TTEE */
205 else
206 mutt_window_addch(win, ACS_TTEE);
207#endif
208 break;
209 case MUTT_TREE_BTEE:
210 if (c_ascii_chars)
211 mutt_window_addch(win, '-');
212#ifdef WACS_BTEE
213 else
214 add_wch(WACS_BTEE);
215#else
216 else if (CharsetIsUtf8)
217 mutt_window_addstr(win, "\342\224\264"); /* WACS_BTEE */
218 else
219 mutt_window_addch(win, ACS_BTEE);
220#endif
221 break;
222 case MUTT_TREE_SPACE:
223 mutt_window_addch(win, ' ');
224 break;
225 case MUTT_TREE_RARROW:
226 mutt_window_addch(win, '>');
227 break;
228 case MUTT_TREE_STAR:
229 mutt_window_addch(win, '*'); /* fake thread indicator */
230 break;
231 case MUTT_TREE_HIDDEN:
232 mutt_window_addch(win, '&');
233 break;
234 case MUTT_TREE_EQUALS:
235 mutt_window_addch(win, '=');
236 break;
238 mutt_window_addch(win, '?');
239 break;
240 }
241 s++;
242 n--;
243 }
244 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, ac_ind);
245 mutt_curses_set_color(ac_merge);
246 }
247 else if (*s == MUTT_SPECIAL_INDEX)
248 {
249 s++;
250 if (*s == MT_COLOR_INDEX)
251 {
252 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, ac_ind);
253 mutt_curses_set_color(ac_merge);
254 }
255 else
256 {
257 const struct AttrColor *color = get_color(index, s);
258 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, color);
259 ac_merge = merged_color_overlay(ac_merge, ac_ind);
260
261 mutt_curses_set_color(ac_merge);
262 }
263 s++;
264 n -= 2;
265 }
266 else if ((k = mbrtowc(&wc, (char *) s, n, &mbstate)) > 0)
267 {
268 mutt_window_addnstr(win, (char *) s, k);
269 s += k;
270 n -= k;
271 }
272 else
273 {
274 break;
275 }
276 }
277}
278
286static void menu_pad_string(struct Menu *menu, struct Buffer *buf)
287{
288 int max_cols = menu->win->state.cols;
289 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
290 if (c_arrow_cursor)
291 {
292 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
293 if (max_cols > 0)
294 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
295 }
296
297 int buf_cols = mutt_strwidth(buf_string(buf));
298 for (; buf_cols < max_cols; buf_cols++)
299 {
300 buf_addch(buf, ' ');
301 }
302}
303
308void menu_redraw_full(struct Menu *menu)
309{
311 mutt_window_clear(menu->win);
312
313 menu->page_len = menu->win->state.rows;
314
316}
317
322void menu_redraw_index(struct Menu *menu)
323{
324 struct Buffer *buf = buf_pool_get();
325 const struct AttrColor *ac = NULL;
326
327 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
328 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
329 const int arrow_width = mutt_strwidth(c_arrow_string);
331 for (int i = menu->top; i < (menu->top + menu->page_len); i++)
332 {
333 if (i < menu->max)
334 {
335 ac = menu->color(menu, i);
336
337 buf_reset(buf);
338 menu->make_entry(menu, i, menu->win->state.cols, buf);
339 menu_pad_string(menu, buf);
340
342 mutt_window_move(menu->win, i - menu->top, 0);
343
344 if (i == menu->current)
345 mutt_curses_set_color(ac_ind);
346
347 if (c_arrow_cursor)
348 {
349 if (i == menu->current)
350 {
351 mutt_window_addstr(menu->win, c_arrow_string);
353 mutt_window_addch(menu->win, ' ');
354 }
355 else
356 {
357 /* Print space chars to match the screen width of `$arrow_string` */
358 mutt_window_printf(menu->win, "%*s", arrow_width + 1, "");
359 }
360 }
361
362 if ((i == menu->current) && !c_arrow_cursor)
363 {
364 print_enriched_string(menu->win, i, ac, ac_ind, buf, menu->sub);
365 }
366 else
367 {
368 print_enriched_string(menu->win, i, ac, NULL, buf, menu->sub);
369 }
370 }
371 else
372 {
374 mutt_window_clearline(menu->win, i - menu->top);
375 }
376 }
379 buf_pool_release(&buf);
380}
381
386void menu_redraw_motion(struct Menu *menu)
387{
388 struct Buffer *buf = buf_pool_get();
389
390 /* Note: menu->color() for the index can end up retrieving a message
391 * over imap (if matching against ~h for instance). This can
392 * generate status messages. So we want to call it *before* we
393 * position the cursor for drawing. */
394 const struct AttrColor *old_color = menu->color(menu, menu->old_current);
395 mutt_window_move(menu->win, menu->old_current - menu->top, 0);
396 mutt_curses_set_color(old_color);
397
398 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
400 if (c_arrow_cursor)
401 {
402 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
403 const int arrow_width = mutt_strwidth(c_arrow_string);
404 /* clear the arrow */
405 /* Print space chars to match the screen width of `$arrow_string` */
406 mutt_window_printf(menu->win, "%*s", arrow_width + 1, "");
408
409 menu->make_entry(menu, menu->old_current, menu->win->state.cols, buf);
410 menu_pad_string(menu, buf);
411 mutt_window_move(menu->win, menu->old_current - menu->top, arrow_width + 1);
412 print_enriched_string(menu->win, menu->old_current, old_color, NULL, buf, menu->sub);
413
414 /* now draw it in the new location */
415 mutt_curses_set_color(ac_ind);
416 mutt_window_move(menu->win, menu->current - menu->top, 0);
417 mutt_window_addstr(menu->win, c_arrow_string);
418 }
419 else
420 {
422 /* erase the current indicator */
423 menu->make_entry(menu, menu->old_current, menu->win->state.cols, buf);
424 menu_pad_string(menu, buf);
425 print_enriched_string(menu->win, menu->old_current, old_color, NULL, buf, menu->sub);
426
427 /* now draw the new one to reflect the change */
428 const struct AttrColor *cur_color = menu->color(menu, menu->current);
429 cur_color = merged_color_overlay(cur_color, ac_ind);
430 buf_reset(buf);
431 menu->make_entry(menu, menu->current, menu->win->state.cols, buf);
432 menu_pad_string(menu, buf);
433 mutt_window_move(menu->win, menu->current - menu->top, 0);
434 mutt_curses_set_color(cur_color);
435 print_enriched_string(menu->win, menu->current, cur_color, ac_ind, buf, menu->sub);
436 }
438 buf_pool_release(&buf);
439}
440
445void menu_redraw_current(struct Menu *menu)
446{
447 struct Buffer *buf = buf_pool_get();
448 const struct AttrColor *ac = menu->color(menu, menu->current);
449
450 mutt_window_move(menu->win, menu->current - menu->top, 0);
451 menu->make_entry(menu, menu->current, menu->win->state.cols, buf);
452 menu_pad_string(menu, buf);
453
455 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
456 if (c_arrow_cursor)
457 {
458 mutt_curses_set_color(ac_ind);
459 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
460 mutt_window_addstr(menu->win, c_arrow_string);
462 mutt_window_addch(menu->win, ' ');
463 menu_pad_string(menu, buf);
464 print_enriched_string(menu->win, menu->current, ac, NULL, buf, menu->sub);
465 }
466 else
467 {
468 print_enriched_string(menu->win, menu->current, ac, ac_ind, buf, menu->sub);
469 }
471 buf_pool_release(&buf);
472}
473
480int menu_redraw(struct Menu *menu)
481{
482 /* See if all or part of the screen needs to be updated. */
483 if (menu->redraw & MENU_REDRAW_FULL)
484 menu_redraw_full(menu);
485
486 if (menu->redraw & MENU_REDRAW_INDEX)
487 menu_redraw_index(menu);
488 else if (menu->redraw & MENU_REDRAW_MOTION)
489 menu_redraw_motion(menu);
490 else if (menu->redraw == MENU_REDRAW_CURRENT)
492
493 return OP_NULL;
494}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
Color and attribute parsing.
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a Colour ID.
Definition regex.c:205
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition simple.c:95
@ MT_COLOR_INDICATOR
Selected item in list.
Definition color.h:50
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition color.h:96
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:54
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:87
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition curs_lib.c:445
void menu_redraw_current(struct Menu *menu)
Redraw the current menu.
Definition draw.c:445
static const struct AttrColor * get_color(int index, unsigned char *s)
Choose a colour for a line of the index.
Definition draw.c:54
void menu_redraw_index(struct Menu *menu)
Force the redraw of the index.
Definition draw.c:322
int menu_redraw(struct Menu *menu)
Redraw the parts of the screen that have been flagged to be redrawn.
Definition draw.c:480
void menu_redraw_full(struct Menu *menu)
Force the redraw of the Menu.
Definition draw.c:308
static void menu_pad_string(struct Menu *menu, struct Buffer *buf)
Pad a string with spaces for display in the Menu.
Definition draw.c:286
static void print_enriched_string(struct MuttWindow *win, int index, const struct AttrColor *ac_def, struct AttrColor *ac_ind, struct Buffer *buf, struct ConfigSubset *sub)
Display a string with embedded colours and graphics.
Definition draw.c:108
void menu_redraw_motion(struct Menu *menu)
Force the redraw of the list part of the menu.
Definition draw.c:386
Structs that make up an email.
bool mutt_pattern_exec(struct Pattern *pat, PatternExecFlags flags, struct Mailbox *m, struct Email *e, struct PatternCache *cache)
Match a pattern against an email header.
Definition exec.c:1148
Convenience wrapper for the gui headers.
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition hash.c:362
GUI manage the main index (list of emails)
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition index.c:721
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:59
#define MENU_REDRAW_INDEX
Redraw the index.
Definition lib.h:56
#define MENU_REDRAW_NO_FLAGS
No flags are set.
Definition lib.h:55
#define MENU_REDRAW_CURRENT
Redraw the current line of the menu.
Definition lib.h:58
#define MENU_REDRAW_MOTION
Redraw after moving the menu list.
Definition lib.h:57
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition merged.c:107
bool CharsetIsUtf8
Is the user's current character set utf-8?
Definition charset.c:66
Convenience wrapper for the library headers.
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:427
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:498
const struct AttrColor * mutt_curses_set_color_by_id(enum ColorId cid)
Set the colour and attributes by the Colour ID.
Definition mutt_curses.c:79
void mutt_curses_set_color(const struct AttrColor *ac)
Set the colour and attributes for text.
Definition mutt_curses.c:38
Create/manipulate threading in emails.
@ MUTT_TREE_MAX
Definition mutt_thread.h:70
@ MUTT_TREE_LLCORNER
Lower left corner.
Definition mutt_thread.h:57
@ MUTT_TREE_RARROW
Right arrow.
Definition mutt_thread.h:63
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition mutt_thread.h:72
@ MUTT_TREE_ULCORNER
Upper left corner.
Definition mutt_thread.h:58
@ MUTT_TREE_EQUALS
Equals (for threads)
Definition mutt_thread.h:66
@ MUTT_TREE_HIDDEN
Ampersand character (for threads)
Definition mutt_thread.h:65
@ MUTT_TREE_STAR
Star character (for threads)
Definition mutt_thread.h:64
@ MUTT_TREE_LTEE
Left T-piece.
Definition mutt_thread.h:59
@ MUTT_TREE_VLINE
Vertical line.
Definition mutt_thread.h:61
@ MUTT_TREE_MISSING
Question mark.
Definition mutt_thread.h:69
@ MUTT_TREE_TTEE
Top T-piece.
Definition mutt_thread.h:67
@ MUTT_TREE_HLINE
Horizontal line.
Definition mutt_thread.h:60
@ MUTT_TREE_SPACE
Blank space.
Definition mutt_thread.h:62
@ MUTT_TREE_BTEE
Bottom T-piece.
Definition mutt_thread.h:68
void mutt_window_clear(struct MuttWindow *win)
Clear a Window.
int mutt_window_printf(struct MuttWindow *win, const char *fmt,...)
Write a formatted string to a Window.
int mutt_window_move(struct MuttWindow *win, int row, int col)
Move the cursor in a Window.
void mutt_window_clearline(struct MuttWindow *win, int row)
Clear a row of a Window.
int mutt_window_addstr(struct MuttWindow *win, const char *str)
Write a string to a Window.
int mutt_window_addnstr(struct MuttWindow *win, const char *str, int num)
Write a partial string to a Window.
int mutt_window_addch(struct MuttWindow *win, int ch)
Write one character to a Window.
struct Email * mutt_get_virt_email(struct Mailbox *m, int vnum)
Get a virtual Email.
Definition mview.c:417
View of a Mailbox.
Match patterns to emails.
#define MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:106
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define SLIST_FIRST(head)
Definition queue.h:227
A curses colour and its attributes.
Definition attr.h:66
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
A set of inherited config items.
Definition subset.h:46
The envelope/body of an email.
Definition email.h:39
int index
The absolute (unsorted) message number.
Definition email.h:110
A mailbox.
Definition mailbox.h:79
Definition lib.h:79
struct MuttWindow * win
Window holding the Menu.
Definition lib.h:86
int current
Current entry.
Definition lib.h:80
const struct AttrColor *(* color)(struct Menu *menu, int line)
Definition lib.h:143
MenuRedrawFlags redraw
When to redraw the screen.
Definition lib.h:82
int top
Entry that is the top of the current page.
Definition lib.h:90
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition lib.h:106
struct ConfigSubset * sub
Inherited config items.
Definition lib.h:87
int page_len
Number of entries per screen.
Definition lib.h:84
int old_current
For driver use only.
Definition lib.h:91
struct WindowState state
Current state of the Window.
A regular expression and a color to highlight a line.
Definition regex4.h:36
struct PatternList * color_pattern
Compiled pattern to speed up index color calculation.
Definition regex4.h:41
struct AttrColor attr_color
Colour and attributes to apply.
Definition regex4.h:37
char * pattern
Pattern to match.
Definition regex4.h:38
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
struct HashTable * TagTransforms
Hash Table: "inbox" -> "i" - Alternative tag names.
Definition tags.c:41