NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_pattern.c
Go to the documentation of this file.
1
24
69
70#include "config.h"
71#include <stdbool.h>
72#include <stddef.h>
73#include "private.h"
74#include "mutt/lib.h"
75#include "config/lib.h"
76#include "core/lib.h"
77#include "gui/lib.h"
78#include "lib.h"
79#include "expando/lib.h"
80#include "key/lib.h"
81#include "menu/lib.h"
82#include "expando.h"
83#include "functions.h"
84#include "mutt_logging.h"
85#include "pattern_data.h"
86
88static const struct Mapping PatternHelp[] = {
89 // clang-format off
90 { N_("Exit"), OP_EXIT },
91 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
92 { N_("Help"), OP_HELP },
93 { NULL, 0 },
94 // clang-format on
95};
96
102static int pattern_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
103{
104 struct PatternData *pd = menu->mdata;
105
106 struct PatternEntry *entry = ARRAY_GET(&pd->entries, line);
107
108 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
109 if (c_arrow_cursor)
110 {
111 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
112 if (max_cols > 0)
113 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
114 }
115
116 const struct Expando *c_pattern_format = cs_subset_expando(NeoMutt->sub, "pattern_format");
117 return expando_filter(c_pattern_format, PatternRenderCallbacks, entry,
118 MUTT_FORMAT_ARROWCURSOR, max_cols, NeoMutt->env, buf);
119}
120
125static void create_pattern_entries(struct PatternEntryArray *pea)
126{
127 int num_entries = 0;
128 while (Flags[num_entries].tag != 0)
129 num_entries++;
130
131 /* Add three more hard-coded entries for thread patterns */
132 ARRAY_RESERVE(pea, num_entries + 3);
133
134 struct Buffer *buf = buf_pool_get();
135
136 struct PatternEntry entry = { 0 };
137 for (int i = 0; Flags[i].tag != '\0'; i++)
138 {
139 entry.num = i + 1;
140
141 buf_printf(buf, "%c%c", Flags[i].prefix, Flags[i].tag);
142 entry.tag = buf_strdup(buf);
143
144 switch (Flags[i].eat_arg)
145 {
146 case EAT_DATE:
147 /* L10N:
148 Pattern Completion Menu argument type: a date range
149 Used by ~d, ~r.
150 */
151 buf_add_printf(buf, " %s", _("DATERANGE"));
152 break;
153 case EAT_GROUP:
154 /* L10N:
155 Pattern Completion Menu argument type: an address group
156 Used by %c, %C, %e, %f, %L.
157 */
158 buf_add_printf(buf, " %s", _("GROUP"));
159 break;
161 /* L10N:
162 Pattern Completion Menu argument type: a message range.
163 Used by ~m.
164 */
165 buf_add_printf(buf, " %s", _("MSGRANGE"));
166 break;
167 case EAT_QUERY:
168 /* L10N:
169 Pattern Completion Menu argument type: a query
170 Used by ~I.
171 */
172 buf_add_printf(buf, " %s", _("QUERY"));
173 break;
174 case EAT_RANGE:
175 /* L10N:
176 Pattern Completion Menu argument type: a numeric range.
177 Used by ~n, ~X, ~z.
178 */
179 buf_add_printf(buf, " %s", _("RANGE"));
180 break;
181 case EAT_REGEX:
182 /* L10N:
183 Pattern Completion Menu argument type: a regular expression
184 */
185 buf_add_printf(buf, " %s", _("REGEX"));
186 break;
187 case EAT_STRING:
188 /* L10N:
189 Pattern Completion Menu argument type: a string (for IMAP patterns)
190 Used by =b, =B, =h, =/.
191 */
192 buf_add_printf(buf, " %s", _("STRING"));
193 break;
194 default:
195 break;
196 }
197
198 entry.expr = buf_strdup(buf);
199 entry.desc = mutt_str_dup(_(Flags[i].desc));
200
201 ARRAY_ADD(pea, entry);
202 }
203
204 /* Add struct MuttThread patterns manually.
205 * Note we allocated 3 extra slots for these above. */
206
207 /* L10N:
208 Pattern Completion Menu argument type: a nested pattern.
209 Used by ~(), ~<(), ~>().
210 */
211 const char *patternstr = _("PATTERN");
212
213 entry.num = ARRAY_SIZE(pea) + 1;
214 entry.tag = mutt_str_dup("~()");
215 buf_printf(buf, "~(%s)", patternstr);
216 entry.expr = buf_strdup(buf);
217 // L10N: Pattern Completion Menu description for ~()
218 entry.desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
219 ARRAY_ADD(pea, entry);
220
221 entry.num = ARRAY_SIZE(pea) + 1;
222 entry.tag = mutt_str_dup("~<()");
223 buf_printf(buf, "~<(%s)", patternstr);
224 entry.expr = buf_strdup(buf);
225 // L10N: Pattern Completion Menu description for ~<()
226 entry.desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
227 ARRAY_ADD(pea, entry);
228
229 entry.num = ARRAY_SIZE(pea) + 1;
230 entry.tag = mutt_str_dup("~>()");
231 buf_printf(buf, "~>(%s)", patternstr);
232 entry.expr = buf_strdup(buf);
233 // L10N: Pattern Completion Menu description for ~>()
234 entry.desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
235 ARRAY_ADD(pea, entry);
236
237 buf_pool_release(&buf);
238}
239
246{
247 if (nc->event_type != NT_CONFIG)
248 return 0;
249 if (!nc->global_data || !nc->event_data)
250 return -1;
251
252 struct EventConfig *ev_c = nc->event_data;
253
254 if (!mutt_str_equal(ev_c->name, "pattern_format"))
255 return 0;
256
257 struct Menu *menu = nc->global_data;
259 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
260
261 return 0;
262}
263
272{
273 if (nc->event_type != NT_WINDOW)
274 return 0;
275 if (!nc->global_data || !nc->event_data)
276 return -1;
278 return 0;
279
280 struct MuttWindow *win_menu = nc->global_data;
281 struct EventWindow *ev_w = nc->event_data;
282 if (ev_w->win != win_menu)
283 return 0;
284
285 struct Menu *menu = win_menu->wdata;
286
289
290 mutt_debug(LL_DEBUG5, "window delete done\n");
291 return 0;
292}
293
302bool dlg_pattern(struct Buffer *buf)
303{
304 struct PatternData *pd = pattern_data_new();
305
308
309 struct Menu *menu = sdw.menu;
310 pd->menu = menu;
311 pd->buf = buf;
312
313 menu->mdata = pd;
316 menu->max = ARRAY_SIZE(&pd->entries);
317
318 // L10N: Pattern completion menu title
319 sbar_set_title(sdw.sbar, _("Patterns"));
320
321 // NT_COLOR is handled by the SimpleDialog
324
325 struct MuttWindow *old_focus = window_set_focus(menu->win);
326 // ---------------------------------------------------------------------------
327 // Event Loop
328 int op = OP_NULL;
329 struct KeyEvent event = { 0, OP_NULL };
330 do
331 {
332 menu_tagging_dispatcher(menu->win, &event);
333 window_redraw(NULL);
334
336 op = event.op;
337 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
338 if (op < 0)
339 continue;
340 if (op == OP_NULL)
341 {
343 continue;
344 }
346
347 int rc = pattern_function_dispatcher(sdw.dlg, &event);
348 if (rc == FR_UNKNOWN)
349 rc = menu_function_dispatcher(menu->win, &event);
350 if (rc == FR_UNKNOWN)
351 rc = global_function_dispatcher(menu->win, &event);
352 } while (!pd->done);
353 // ---------------------------------------------------------------------------
354
355 bool rc = pd->selection;
356
357 window_set_focus(old_focus);
359
360 return rc;
361}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_RESERVE(head, n)
Reserve memory for the array.
Definition array.h:191
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
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
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
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:444
@ FR_UNKNOWN
Unknown function.
Definition dispatcher.h:34
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition dlg_pattern.c:88
static void create_pattern_entries(struct PatternEntryArray *pea)
Create the Pattern Entries.
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, void *data, MuttFormatFlags flags, int max_cols, char **env_list, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition filter.c:139
Parse Expando string.
struct KeyEvent km_dokey(const struct MenuDefinition *md, GetChFlags flags)
Determine what a keypress should do.
Definition get.c:434
void km_error_key(const struct MenuDefinition *md)
Handle an unbound key sequence.
Definition get.c:285
#define GETCH_NO_FLAGS
No flags are set.
Definition get.h:34
int pattern_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Pattern function - Implements function_dispatcher_t -.
Definition functions.c:83
int menu_tagging_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition tagging.c:230
int global_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Global function - Implements function_dispatcher_t -.
Definition global.c:182
int menu_function_dispatcher(struct MuttWindow *win, const struct KeyEvent *event)
Perform a Menu function - Implements function_dispatcher_t -.
Definition functions.c:320
bool dlg_pattern(struct Buffer *buf)
Show menu to select a Pattern -.
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
static int pattern_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Create a Pattern for the Menu - Implements Menu::make_entry() -.
void pattern_data_free(struct Menu *menu, void **ptr)
Free Pattern Data - Implements Menu::mdata_free() -.
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
struct MenuDefinition * MdDialog
Dialog Menu Definition.
Definition functions.c:53
struct MenuDefinition * MdGeneric
Generic Menu Definition.
Definition functions.c:50
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition simple.c:169
struct SimpleDialogWindows simple_dialog_new(const struct MenuDefinition *md, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition simple.c:132
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition logging2.h:49
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition lib.h:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition menu.c:188
Convenience wrapper for the library headers.
#define N_(a)
Definition message.h:32
#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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
@ WT_DLG_PATTERN
Pattern Dialog, dlg_pattern()
Definition mutt_window.h:87
@ NT_WINDOW_DELETE
Window is about to be deleted.
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition notify_type.h:57
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition notify_type.h:43
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition opcodes.c:48
const struct ExpandoRenderCallback PatternRenderCallbacks[]
Callbacks for Pattern Expandos.
Definition expando.c:75
Pattern Expando definitions.
const struct PatternFlags Flags[]
Lookup table for all patterns.
Definition flags.c:47
Pattern functions.
Match patterns to emails.
Shared constants/structs that are private to libpattern.
@ EAT_STRING
Process a plain string.
Definition private.h:59
@ EAT_RANGE
Process a number (range)
Definition private.h:57
@ EAT_GROUP
Process a group name.
Definition private.h:54
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition private.h:55
@ EAT_DATE
Process a date (range)
Definition private.h:53
@ EAT_QUERY
Process a query string.
Definition private.h:56
@ EAT_REGEX
Process a regex.
Definition private.h:58
struct PatternData * pattern_data_new(void)
Create new Pattern Data.
Private Pattern Data.
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 MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition render.h:37
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition sbar.c:227
String manipulation buffer.
Definition buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition subset.h:51
A config-change event.
Definition subset.h:70
const char * name
Name of config item that changed.
Definition subset.h:72
An Event that happened to a Window.
struct MuttWindow * win
Window that changed.
Parsed Expando trees.
Definition expando.h:41
An event such as a keypress.
Definition get.h:50
int op
Function opcode, e.g. OP_HELP.
Definition get.h:52
Mapping between user-readable string and a constant.
Definition mapping.h:33
Definition lib.h:80
struct MuttWindow * win
Window holding the Menu.
Definition lib.h:88
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition lib.h:163
int(* make_entry)(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
Definition lib.h:108
struct ConfigSubset * sub
Inherited config items.
Definition lib.h:89
void * mdata
Private data.
Definition lib.h:149
int max
Number of entries in the menu.
Definition lib.h:82
void * wdata
Private data.
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Container for Accounts, Notifications.
Definition neomutt.h:41
char ** env
Private copy of the environment variables.
Definition neomutt.h:58
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
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
Data to pass to the Pattern Functions.
struct Menu * menu
Pattern Menu.
struct PatternEntryArray entries
Patterns for the Menu.
bool done
Should we close the Dialog?
struct Buffer * buf
Buffer for the results.
bool selection
Was a selection made?
A line in the Pattern Completion menu.
const char * desc
Description of pattern.
const char * tag
Copied to buffer if selected.
int num
Index number.
const char * expr
Displayed in the menu.
Tuple for the results of simple_dialog_new()
Definition simple.h:35
struct MuttWindow * sbar
Simple Bar.
Definition simple.h:37
struct Menu * menu
Menu.
Definition simple.h:38
struct MuttWindow * dlg
Main Dialog Window.
Definition simple.h:36