NeoMutt  2025-12-11-980-ge38c27
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 "core/lib.h"
38#include "gui/lib.h"
39#include "lib.h"
40#include "color/lib.h"
41#include "index/lib.h"
42#include "pattern/lib.h"
43
53static const struct AttrColor *get_color(int index, unsigned char *s)
54{
55 const int type = *s;
56 struct RegexColorList *rcl = regex_colors_get_list(type);
57 struct Mailbox *m_cur = get_current_mailbox();
58 struct Email *e = mutt_get_virt_email(m_cur, index);
59 if (!rcl || !e)
60 {
61 return simple_color_get(type);
62 }
63
64 struct RegexColor *np = NULL;
65 const struct AttrColor *ac_merge = NULL;
66
68 ASSERT(mod_data);
69
70 struct Buffer *buf = buf_pool_get();
71 buf_strcpy(buf, (const char *) (s + 1));
72 char *marker = (char *) buf_find_char(buf, MUTT_SPECIAL_INDEX);
73 if (marker)
74 *marker = '\0';
75
76 // %Gx %{tags-transformed} - Transformed message tags
77 if (type == MT_COLOR_INDEX_TAG)
78 {
79 STAILQ_FOREACH(np, rcl, entries)
80 {
81 const char *transform = mutt_hash_find(mod_data->tag_transforms, np->pattern);
82 if (transform && strstr(buf_string(buf), transform))
83
84 {
85 ac_merge = merged_color_overlay(ac_merge, &np->attr_color);
86 }
87 }
88 goto done;
89 }
90
91 STAILQ_FOREACH(np, rcl, entries)
92 {
94 MUTT_MATCH_FULL_ADDRESS, m_cur, e, NULL))
95 {
96 ac_merge = merged_color_overlay(ac_merge, &np->attr_color);
97 }
98 }
99
100done:
101 buf_pool_release(&buf);
102 return ac_merge;
103}
104
114static void print_enriched_string(struct MuttWindow *win, int index,
115 const struct AttrColor *ac_def, struct AttrColor *ac_ind,
116 struct Buffer *buf, struct ConfigSubset *sub)
117{
118 wchar_t wc = 0;
119 size_t k;
120 size_t n = mutt_str_len(buf_string(buf));
121 unsigned char *s = (unsigned char *) buf->data;
122 mbstate_t mbstate = { 0 };
123
124 const bool c_ascii_chars = cs_subset_bool(sub, "ascii_chars");
125 while (*s)
126 {
127 if (*s < MUTT_TREE_MAX)
128 {
129 /* Combining tree fg color and another bg color requires having
130 * use_default_colors, because the other bg color may be undefined. */
131 mutt_curses_set_color(ac_ind);
132
133 while (*s && (*s < MUTT_TREE_MAX))
134 {
135 switch (*s)
136 {
138 if (c_ascii_chars)
139 mutt_window_addch(win, '`');
140#ifdef WACS_LLCORNER
141 else
142 add_wch(WACS_LLCORNER);
143#else
144 else if (CharsetIsUtf8)
145 mutt_window_addstr(win, "\342\224\224"); /* WACS_LLCORNER */
146 else
147 mutt_window_addch(win, ACS_LLCORNER);
148#endif
149 break;
151 if (c_ascii_chars)
152 mutt_window_addch(win, ',');
153#ifdef WACS_ULCORNER
154 else
155 add_wch(WACS_ULCORNER);
156#else
157 else if (CharsetIsUtf8)
158 mutt_window_addstr(win, "\342\224\214"); /* WACS_ULCORNER */
159 else
160 mutt_window_addch(win, ACS_ULCORNER);
161#endif
162 break;
163 case MUTT_TREE_LTEE:
164 if (c_ascii_chars)
165 mutt_window_addch(win, '|');
166#ifdef WACS_LTEE
167 else
168 add_wch(WACS_LTEE);
169#else
170 else if (CharsetIsUtf8)
171 mutt_window_addstr(win, "\342\224\234"); /* WACS_LTEE */
172 else
173 mutt_window_addch(win, ACS_LTEE);
174#endif
175 break;
176 case MUTT_TREE_HLINE:
177 if (c_ascii_chars)
178 mutt_window_addch(win, '-');
179#ifdef WACS_HLINE
180 else
181 add_wch(WACS_HLINE);
182#else
183 else if (CharsetIsUtf8)
184 mutt_window_addstr(win, "\342\224\200"); /* WACS_HLINE */
185 else
186 mutt_window_addch(win, ACS_HLINE);
187#endif
188 break;
189 case MUTT_TREE_VLINE:
190 if (c_ascii_chars)
191 mutt_window_addch(win, '|');
192#ifdef WACS_VLINE
193 else
194 add_wch(WACS_VLINE);
195#else
196 else if (CharsetIsUtf8)
197 mutt_window_addstr(win, "\342\224\202"); /* WACS_VLINE */
198 else
199 mutt_window_addch(win, ACS_VLINE);
200#endif
201 break;
202 case MUTT_TREE_TTEE:
203 if (c_ascii_chars)
204 mutt_window_addch(win, '-');
205#ifdef WACS_TTEE
206 else
207 add_wch(WACS_TTEE);
208#else
209 else if (CharsetIsUtf8)
210 mutt_window_addstr(win, "\342\224\254"); /* WACS_TTEE */
211 else
212 mutt_window_addch(win, ACS_TTEE);
213#endif
214 break;
215 case MUTT_TREE_BTEE:
216 if (c_ascii_chars)
217 mutt_window_addch(win, '-');
218#ifdef WACS_BTEE
219 else
220 add_wch(WACS_BTEE);
221#else
222 else if (CharsetIsUtf8)
223 mutt_window_addstr(win, "\342\224\264"); /* WACS_BTEE */
224 else
225 mutt_window_addch(win, ACS_BTEE);
226#endif
227 break;
228 case MUTT_TREE_SPACE:
229 mutt_window_addch(win, ' ');
230 break;
231 case MUTT_TREE_RARROW:
232 mutt_window_addch(win, '>');
233 break;
234 case MUTT_TREE_STAR:
235 mutt_window_addch(win, '*'); /* fake thread indicator */
236 break;
237 case MUTT_TREE_HIDDEN:
238 mutt_window_addch(win, '&');
239 break;
240 case MUTT_TREE_EQUALS:
241 mutt_window_addch(win, '=');
242 break;
244 mutt_window_addch(win, '?');
245 break;
246 }
247 s++;
248 n--;
249 }
250 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, ac_ind);
251 mutt_curses_set_color(ac_merge);
252 }
253 else if ((*s == MUTT_SPECIAL_INDEX) && (n >= 2))
254 {
255 s++;
256 if (*s == MT_COLOR_INDEX)
257 {
258 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, ac_ind);
259 mutt_curses_set_color(ac_merge);
260 }
261 else
262 {
263 const struct AttrColor *color = get_color(index, s);
264 const struct AttrColor *ac_merge = merged_color_overlay(ac_def, color);
265 ac_merge = merged_color_overlay(ac_merge, ac_ind);
266
267 mutt_curses_set_color(ac_merge);
268 }
269 s++;
270 n -= 2;
271 }
272 else if ((k = mbrtowc(&wc, (char *) s, n, &mbstate)) > 0)
273 {
274 mutt_window_addnstr(win, (char *) s, k);
275 s += k;
276 n -= k;
277 }
278 else
279 {
280 break;
281 }
282 }
283}
284
292static void menu_pad_string(struct Menu *menu, struct Buffer *buf)
293{
294 int max_cols = menu->win->state.cols;
295 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
296 if (c_arrow_cursor)
297 {
298 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
299 if (max_cols > 0)
300 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
301 }
302
303 int buf_cols = mutt_strwidth(buf_string(buf));
304 for (; buf_cols < max_cols; buf_cols++)
305 {
306 buf_addch(buf, ' ');
307 }
308}
309
314void menu_redraw_full(struct Menu *menu)
315{
317 mutt_window_clear(menu->win);
318
319 menu->page_len = menu->win->state.rows;
320
322}
323
328void menu_redraw_index(struct Menu *menu)
329{
330 struct Buffer *buf = buf_pool_get();
331 const struct AttrColor *ac = NULL;
332
333 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
334 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
335 const int arrow_width = mutt_strwidth(c_arrow_string);
336 struct AttrColor *ac_ind = menu->show_indicator ? simple_color_get(MT_COLOR_INDICATOR) : NULL;
337 for (int i = menu->top; i < (menu->top + menu->page_len); i++)
338 {
339 if (i < menu->max)
340 {
341 ac = menu->color(menu, i);
342
343 buf_reset(buf);
344 menu->make_entry(menu, i, menu->win->state.cols, buf);
345 menu_pad_string(menu, buf);
346
348 mutt_window_move(menu->win, i - menu->top, 0);
349
350 if (i == menu->current)
351 mutt_curses_set_color(ac_ind);
352
353 if (c_arrow_cursor)
354 {
355 if (i == menu->current)
356 {
357 mutt_window_addstr(menu->win, c_arrow_string);
359 mutt_window_addch(menu->win, ' ');
360 }
361 else
362 {
363 /* Print space chars to match the screen width of `$arrow_string` */
364 mutt_window_printf(menu->win, "%*s", arrow_width + 1, "");
365 }
366 }
367
368 if ((i == menu->current) && !c_arrow_cursor)
369 {
370 print_enriched_string(menu->win, i, ac, ac_ind, buf, menu->sub);
371 }
372 else
373 {
374 print_enriched_string(menu->win, i, ac, NULL, buf, menu->sub);
375 }
376 }
377 else
378 {
380 mutt_window_clearline(menu->win, i - menu->top);
381 }
382 }
384 menu->redraw = MENU_REDRAW_NONE;
385 buf_pool_release(&buf);
386}
387
392void menu_redraw_motion(struct Menu *menu)
393{
394 struct Buffer *buf = buf_pool_get();
395
396 /* Note: menu->color() for the index can end up retrieving a message
397 * over imap (if matching against ~h for instance). This can
398 * generate status messages. So we want to call it *before* we
399 * position the cursor for drawing. */
400 const struct AttrColor *old_color = menu->color(menu, menu->old_current);
401 mutt_window_move(menu->win, menu->old_current - menu->top, 0);
402 mutt_curses_set_color(old_color);
403
404 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
405 struct AttrColor *ac_ind = menu->show_indicator ? simple_color_get(MT_COLOR_INDICATOR) : NULL;
406 if (c_arrow_cursor)
407 {
408 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
409 const int arrow_width = mutt_strwidth(c_arrow_string);
410 /* clear the arrow */
411 /* Print space chars to match the screen width of `$arrow_string` */
412 mutt_window_printf(menu->win, "%*s", arrow_width + 1, "");
414
415 menu->make_entry(menu, menu->old_current, menu->win->state.cols, buf);
416 menu_pad_string(menu, buf);
417 mutt_window_move(menu->win, menu->old_current - menu->top, arrow_width + 1);
418 print_enriched_string(menu->win, menu->old_current, old_color, NULL, buf, menu->sub);
419
420 /* now draw it in the new location */
421 mutt_curses_set_color(ac_ind);
422 mutt_window_move(menu->win, menu->current - menu->top, 0);
423 mutt_window_addstr(menu->win, c_arrow_string);
424 }
425 else
426 {
428 /* erase the current indicator */
429 menu->make_entry(menu, menu->old_current, menu->win->state.cols, buf);
430 menu_pad_string(menu, buf);
431 print_enriched_string(menu->win, menu->old_current, old_color, NULL, buf, menu->sub);
432
433 /* now draw the new one to reflect the change */
434 const struct AttrColor *cur_color = menu->color(menu, menu->current);
435 cur_color = merged_color_overlay(cur_color, ac_ind);
436 buf_reset(buf);
437 menu->make_entry(menu, menu->current, menu->win->state.cols, buf);
438 menu_pad_string(menu, buf);
439 mutt_window_move(menu->win, menu->current - menu->top, 0);
440 mutt_curses_set_color(cur_color);
441 print_enriched_string(menu->win, menu->current, cur_color, ac_ind, buf, menu->sub);
442 }
444 buf_pool_release(&buf);
445}
446
451void menu_redraw_current(struct Menu *menu)
452{
453 struct Buffer *buf = buf_pool_get();
454 const struct AttrColor *ac = menu->color(menu, menu->current);
455
456 mutt_window_move(menu->win, menu->current - menu->top, 0);
457 menu->make_entry(menu, menu->current, menu->win->state.cols, buf);
458 menu_pad_string(menu, buf);
459
460 struct AttrColor *ac_ind = menu->show_indicator ? simple_color_get(MT_COLOR_INDICATOR) : NULL;
461 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
462 if (c_arrow_cursor)
463 {
464 mutt_curses_set_color(ac_ind);
465 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
466 mutt_window_addstr(menu->win, c_arrow_string);
468 mutt_window_addch(menu->win, ' ');
469 menu_pad_string(menu, buf);
470 print_enriched_string(menu->win, menu->current, ac, NULL, buf, menu->sub);
471 }
472 else
473 {
474 print_enriched_string(menu->win, menu->current, ac, ac_ind, buf, menu->sub);
475 }
477 buf_pool_release(&buf);
478}
479
486int menu_redraw(struct Menu *menu)
487{
488 /* See if all or part of the screen needs to be updated. */
489 if (menu->redraw & MENU_REDRAW_FULL)
490 menu_redraw_full(menu);
491
492 if (menu->redraw & MENU_REDRAW_INDEX)
493 menu_redraw_index(menu);
494 else if (menu->redraw & MENU_REDRAW_MOTION)
495 menu_redraw_motion(menu);
496 else if (menu->redraw == MENU_REDRAW_CURRENT)
498
499 return OP_NULL;
500}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:89
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:248
const char * buf_find_char(const struct Buffer *buf, const char c)
Return a pointer to a char found in the buffer.
Definition buffer.c:659
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:401
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:192
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition simple.c:98
@ MT_COLOR_INDICATOR
Selected item in list.
Definition color.h:49
@ MT_COLOR_INDEX_TAG
Index: tag field (G)
Definition color.h:95
@ MT_COLOR_NORMAL
Plain text.
Definition color.h:53
@ MT_COLOR_INDEX
Index: default colour.
Definition color.h:86
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.
Convenience wrapper for the core headers.
size_t mutt_strwidth(const char *s)
Measure a string's width in screen cells.
Definition curs_lib.c:449
void menu_redraw_current(struct Menu *menu)
Redraw the current menu.
Definition draw.c:451
static const struct AttrColor * get_color(int index, unsigned char *s)
Choose a colour for a line of the index.
Definition draw.c:53
void menu_redraw_index(struct Menu *menu)
Force the redraw of the index.
Definition draw.c:328
int menu_redraw(struct Menu *menu)
Redraw the parts of the screen that have been flagged to be redrawn.
Definition draw.c:486
void menu_redraw_full(struct Menu *menu)
Force the redraw of the Menu.
Definition draw.c:314
static void menu_pad_string(struct Menu *menu, struct Buffer *buf)
Pad a string with spaces for display in the Menu.
Definition draw.c:292
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:114
void menu_redraw_motion(struct Menu *menu)
Force the redraw of the list part of the menu.
Definition draw.c:392
Structs that make up an email.
Convenience wrapper for the gui headers.
@ MUTT_TREE_MAX
Definition thread.h:70
@ MUTT_TREE_LLCORNER
Lower left corner.
Definition thread.h:57
@ MUTT_TREE_RARROW
Right arrow.
Definition thread.h:63
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition thread.h:72
@ MUTT_TREE_ULCORNER
Upper left corner.
Definition thread.h:58
@ MUTT_TREE_EQUALS
Equals (for threads)
Definition thread.h:66
@ MUTT_TREE_HIDDEN
Ampersand character (for threads)
Definition thread.h:65
@ MUTT_TREE_STAR
Star character (for threads)
Definition thread.h:64
@ MUTT_TREE_LTEE
Left T-piece.
Definition thread.h:59
@ MUTT_TREE_VLINE
Vertical line.
Definition thread.h:61
@ MUTT_TREE_MISSING
Question mark.
Definition thread.h:69
@ MUTT_TREE_TTEE
Top T-piece.
Definition thread.h:67
@ MUTT_TREE_HLINE
Horizontal line.
Definition thread.h:60
@ MUTT_TREE_SPACE
Blank space.
Definition thread.h:62
@ MUTT_TREE_BTEE
Bottom T-piece.
Definition thread.h:68
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:365
GUI manage the main index (list of emails)
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition index.c:725
GUI present the user with a selectable list.
@ MENU_REDRAW_INDEX
Redraw the index.
Definition lib.h:61
@ MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:64
@ MENU_REDRAW_CURRENT
Redraw the current line of the menu.
Definition lib.h:63
@ MENU_REDRAW_NONE
No flags are set.
Definition lib.h:60
@ MENU_REDRAW_MOTION
Redraw after moving the menu list.
Definition lib.h:62
const struct AttrColor * merged_color_overlay(const struct AttrColor *base, const struct AttrColor *over)
Combine two colours.
Definition merged.c:110
@ MODULE_ID_EMAIL
ModuleEmail, Email code
Definition module_api.h:64
bool CharsetIsUtf8
Is the user's current character set utf-8?
Definition charset.c:67
Convenience wrapper for the library headers.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
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
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:378
void * neomutt_get_module_data(struct NeoMutt *n, enum ModuleId id)
Get the private data for a Module.
Definition neomutt.c:666
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:1151
Match patterns to emails.
@ MUTT_MATCH_FULL_ADDRESS
Match the full address.
Definition lib.h:117
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define SLIST_FIRST(head)
Definition queue.h:227
#define ASSERT(COND)
Definition signal2.h:59
A curses colour and its attributes.
Definition attr.h:65
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
Email private Module data.
Definition module_data.h:32
struct HashTable * tag_transforms
Hash Table: "inbox" -> "i" - Alternative tag names.
Definition module_data.h:42
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:81
Definition lib.h:86
struct MuttWindow * win
Window holding the Menu.
Definition lib.h:94
int current
Current entry.
Definition lib.h:87
const struct AttrColor *(* color)(struct Menu *menu, int line)
Definition lib.h:151
MenuRedrawFlags redraw
When to redraw the screen.
Definition lib.h:89
bool show_indicator
Show the Indicator colour.
Definition lib.h:93
int top
Entry that is the top of the current page.
Definition lib.h:98
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition lib.h:114
struct ConfigSubset * sub
Inherited config items.
Definition lib.h:95
int page_len
Number of entries per screen.
Definition lib.h:91
int old_current
For driver use only.
Definition lib.h:99
struct WindowState state
Current state of the Window.
Container for Accounts, Notifications.
Definition neomutt.h:41
A regular expression and a color to highlight a line.
Definition regex4.h:37
struct PatternList * color_pattern
Compiled pattern to speed up index color calculation.
Definition regex4.h:42
struct AttrColor attr_color
Colour and attributes to apply.
Definition regex4.h:38
char * pattern
Pattern to match.
Definition regex4.h:39
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