NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_pattern.c
Go to the documentation of this file.
1
70#include "config.h"
71#include <stddef.h>
72#include <stdbool.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 "functions.h"
83#include "mutt_logging.h"
84
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
100void pattern_d(const struct ExpandoNode *node, void *data,
101 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
102{
103 const struct PatternEntry *entry = data;
104
105 const char *s = entry->desc;
106 buf_strcpy(buf, s);
107}
108
112void pattern_e(const struct ExpandoNode *node, void *data,
113 MuttFormatFlags flags, int max_cols, struct Buffer *buf)
114{
115 const struct PatternEntry *entry = data;
116
117 const char *s = entry->expr;
118 buf_strcpy(buf, s);
119}
120
124long pattern_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
125{
126 const struct PatternEntry *entry = data;
127
128 return entry->num;
129}
130
136static int pattern_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
137{
138 struct PatternEntry *entry = &((struct PatternEntry *) menu->mdata)[line];
139
140 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
141 if (c_arrow_cursor)
142 {
143 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
144 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
145 }
146
147 const struct Expando *c_pattern_format = cs_subset_expando(NeoMutt->sub, "pattern_format");
148 return expando_render(c_pattern_format, PatternRenderData, entry,
149 MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
150}
151
155static void free_pattern_menu(struct Menu *menu, void **ptr)
156{
157 struct PatternEntry *entries = *ptr;
158
159 for (size_t i = 0; i < menu->max; i++)
160 {
161 FREE(&entries[i].tag);
162 FREE(&entries[i].expr);
163 FREE(&entries[i].desc);
164 }
165
166 FREE(ptr);
167}
168
174static struct Menu *create_pattern_menu(struct MuttWindow *dlg)
175{
176 int num_entries = 0, i = 0;
177 struct Buffer *entrybuf = NULL;
178
179 while (Flags[num_entries].tag)
180 num_entries++;
181 /* Add three more hard-coded entries */
182 num_entries += 3;
183 struct PatternEntry *entries = mutt_mem_calloc(num_entries, sizeof(struct PatternEntry));
184
185 struct Menu *menu = dlg->wdata;
187 menu->mdata = entries;
189 menu->max = num_entries;
190
191 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
192 // L10N: Pattern completion menu title
193 sbar_set_title(sbar, _("Patterns"));
194
195 entrybuf = buf_pool_get();
196 while (Flags[i].tag)
197 {
198 entries[i].num = i + 1;
199
200 buf_printf(entrybuf, "~%c", (char) Flags[i].tag);
201 entries[i].tag = mutt_str_dup(buf_string(entrybuf));
202
203 switch (Flags[i].eat_arg)
204 {
205 case EAT_REGEX:
206 /* L10N:
207 Pattern Completion Menu argument type: a regular expression
208 */
209 buf_add_printf(entrybuf, " %s", _("EXPR"));
210 break;
211 case EAT_RANGE:
213 /* L10N:
214 Pattern Completion Menu argument type: a numeric range.
215 Used by ~m, ~n, ~X, ~z.
216 */
217 buf_add_printf(entrybuf, " %s", _("RANGE"));
218 break;
219 case EAT_DATE:
220 /* L10N:
221 Pattern Completion Menu argument type: a date range
222 Used by ~d, ~r.
223 */
224 buf_add_printf(entrybuf, " %s", _("DATERANGE"));
225 break;
226 case EAT_QUERY:
227 /* L10N:
228 Pattern Completion Menu argument type: a query
229 Used by ~I.
230 */
231 buf_add_printf(entrybuf, " %s", _("QUERY"));
232 break;
233 default:
234 break;
235 }
236 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
237 entries[i].desc = mutt_str_dup(_(Flags[i].desc));
238
239 i++;
240 }
241
242 /* Add struct MuttThread patterns manually.
243 * Note we allocated 3 extra slots for these above. */
244
245 /* L10N:
246 Pattern Completion Menu argument type: a nested pattern.
247 Used by ~(), ~<(), ~>().
248 */
249 const char *patternstr = _("PATTERN");
250
251 entries[i].num = i + 1;
252 entries[i].tag = mutt_str_dup("~()");
253 buf_printf(entrybuf, "~(%s)", patternstr);
254 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
255 // L10N: Pattern Completion Menu description for ~()
256 entries[i].desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
257 i++;
258
259 entries[i].num = i + 1;
260 entries[i].tag = mutt_str_dup("~<()");
261 buf_printf(entrybuf, "~<(%s)", patternstr);
262 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
263 // L10N: Pattern Completion Menu description for ~<()
264 entries[i].desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
265 i++;
266
267 entries[i].num = i + 1;
268 entries[i].tag = mutt_str_dup("~>()");
269 buf_printf(entrybuf, "~>(%s)", patternstr);
270 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
271 // L10N: Pattern Completion Menu description for ~>()
272 entries[i].desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
273
274 buf_pool_release(&entrybuf);
275
276 return menu;
277}
278
285{
286 if (nc->event_type != NT_CONFIG)
287 return 0;
288 if (!nc->global_data || !nc->event_data)
289 return -1;
290
291 struct EventConfig *ev_c = nc->event_data;
292
293 if (!mutt_str_equal(ev_c->name, "pattern_format"))
294 return 0;
295
296 struct Menu *menu = nc->global_data;
298 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
299
300 return 0;
301}
302
311{
312 if (nc->event_type != NT_WINDOW)
313 return 0;
314 if (!nc->global_data || !nc->event_data)
315 return -1;
317 return 0;
318
319 struct MuttWindow *win_menu = nc->global_data;
320 struct EventWindow *ev_w = nc->event_data;
321 if (ev_w->win != win_menu)
322 return 0;
323
324 struct Menu *menu = win_menu->wdata;
325
328
329 mutt_debug(LL_DEBUG5, "window delete done\n");
330 return 0;
331}
332
342bool dlg_pattern(char *buf, size_t buflen)
343{
345 struct Menu *menu = create_pattern_menu(dlg);
346
347 struct PatternData pd = { false, false, buf, buflen, menu };
348 dlg->wdata = &pd;
349
350 // NT_COLOR is handled by the SimpleDialog
353
354 struct MuttWindow *old_focus = window_set_focus(menu->win);
355 // ---------------------------------------------------------------------------
356 // Event Loop
357 int op = OP_NULL;
358 do
359 {
360 menu_tagging_dispatcher(menu->win, op);
361 window_redraw(NULL);
362
364 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
365 if (op < 0)
366 continue;
367 if (op == OP_NULL)
368 {
370 continue;
371 }
373
374 int rc = pattern_function_dispatcher(dlg, op);
375 if (rc == FR_UNKNOWN)
376 rc = menu_function_dispatcher(menu->win, op);
377 if (rc == FR_UNKNOWN)
378 rc = global_function_dispatcher(NULL, op);
379 } while (!pd.done);
380 // ---------------------------------------------------------------------------
381
382 window_set_focus(old_focus);
383 simple_dialog_free(&dlg);
384 return pd.selection;
385}
386
392const struct ExpandoRenderData PatternRenderData[] = {
393 // clang-format off
397 { -1, -1, NULL, NULL },
398 // clang-format on
399};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:394
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:358
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:443
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
const struct ExpandoRenderData PatternRenderData[]
Callbacks for Pattern Expandos.
Definition: dlg_pattern.c:85
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition: dlg_pattern.c:88
static struct Menu * create_pattern_menu(struct MuttWindow *dlg)
Create the Pattern Completion menu.
Definition: dlg_pattern.c:174
@ ED_PATTERN
Pattern ED_PAT_ ExpandoDataPattern.
Definition: domain.h:51
Parse Expando string.
int expando_render(const struct Expando *exp, const struct ExpandoRenderData *rdata, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando + data into a string.
Definition: expando.c:110
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:463
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:293
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:230
int pattern_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pattern function - Implements function_dispatcher_t -.
Definition: functions.c:77
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:172
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:318
long pattern_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Pattern: Index number - Implements ExpandoRenderData::get_number() -.
Definition: dlg_pattern.c:124
void pattern_e(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Pattern: pattern expression - Implements ExpandoRenderData::get_string() -.
Definition: dlg_pattern.c:112
void pattern_d(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Pattern: pattern description - Implements ExpandoRenderData::get_string() -.
Definition: dlg_pattern.c:100
bool dlg_pattern(char *buf, size_t buflen)
Show menu to select a Pattern -.
Definition: dlg_pattern.c:342
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
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() -.
Definition: dlg_pattern.c:136
static void free_pattern_menu(struct Menu *menu, void **ptr)
Free the Pattern Completion menu - Implements Menu::mdata_free() -.
Definition: dlg_pattern.c:155
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pattern.c:310
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pattern.c:284
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:168
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:132
Manage keymappings.
#define GETCH_NO_FLAGS
No flags are set.
Definition: lib.h:51
@ LL_DEBUG5
Log at debug level 5.
Definition: logging2.h:47
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:59
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:184
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:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:634
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:684
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:533
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ WT_DLG_PATTERN
Pattern Dialog, dlg_pattern()
Definition: mutt_window.h:87
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:229
@ 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 PatternFlags Flags[]
Lookup table for all patterns.
Definition: flags.c:40
@ ED_PAT_DESCRIPTION
PatternEntry.desc.
Definition: private.h:51
@ ED_PAT_EXPRESION
PatternEntry.expr.
Definition: private.h:52
@ ED_PAT_NUMBER
PatternEntry.num.
Definition: private.h:53
@ EAT_RANGE
Process a number (range)
Definition: private.h:66
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:67
@ EAT_DATE
Process a date (range)
Definition: private.h:65
@ EAT_QUERY
Process a query string.
Definition: private.h:68
@ EAT_REGEX
Process a regex.
Definition: private.h:64
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: render.h:37
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:227
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
String manipulation buffer.
Definition: buffer.h:36
struct Notify * notify
Notifications: NotifyConfig, EventConfig.
Definition: subset.h:52
A config-change event.
Definition: subset.h:71
const char * name
Name of config item that changed.
Definition: subset.h:73
An Event that happened to a Window.
Definition: mutt_window.h:239
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:240
Basic Expando Node.
Definition: node.h:67
Parsed Expando trees.
Definition: expando.h:41
Mapping between user-readable string and a constant.
Definition: mapping.h:33
Definition: lib.h:79
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:86
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:161
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
void * mdata
Private data.
Definition: lib.h:147
int max
Number of entries in the menu.
Definition: lib.h:81
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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.
Definition: functions.h:35
char * buf
Buffer for the results.
Definition: functions.h:38
struct Menu * menu
Pattern Menu.
Definition: functions.h:40
bool done
Should we close the Dialog?
Definition: functions.h:36
size_t buflen
Length of the results buffer.
Definition: functions.h:39
bool selection
Was a selection made?
Definition: functions.h:37
A line in the Pattern Completion menu.
Definition: private.h:37
const char * desc
Description of pattern.
Definition: private.h:41
const char * tag
Copied to buffer if selected.
Definition: private.h:39
int num
Index number.
Definition: private.h:38
const char * expr
Displayed in the menu.
Definition: private.h:40
@ MENU_DIALOG
Simple Dialog.
Definition: type.h:43
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46