NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dlg_pattern.c
Go to the documentation of this file.
1
69#include "config.h"
70#include <stddef.h>
71#include <stdbool.h>
72#include <stdint.h>
73#include <stdio.h>
74#include "private.h"
75#include "mutt/lib.h"
76#include "config/lib.h"
77#include "core/lib.h"
78#include "gui/lib.h"
79#include "lib.h"
80#include "key/lib.h"
81#include "menu/lib.h"
82#include "format_flags.h"
83#include "functions.h"
84#include "mutt_logging.h"
85#include "muttlib.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
106static const char *pattern_format_str(char *buf, size_t buflen, size_t col, int cols,
107 char op, const char *src, const char *prec,
108 const char *if_str, const char *else_str,
109 intptr_t data, MuttFormatFlags flags)
110{
111 struct PatternEntry *entry = (struct PatternEntry *) data;
112
113 switch (op)
114 {
115 case 'd':
116 mutt_format_s(buf, buflen, prec, NONULL(entry->desc));
117 break;
118 case 'e':
119 mutt_format_s(buf, buflen, prec, NONULL(entry->expr));
120 break;
121 case 'n':
122 {
123 char tmp[32] = { 0 };
124 snprintf(tmp, sizeof(tmp), "%%%sd", prec);
125 snprintf(buf, buflen, tmp, entry->num);
126 break;
127 }
128 }
129
130 return src;
131}
132
138static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
139{
140 struct PatternEntry *entry = &((struct PatternEntry *) menu->mdata)[num];
141
142 const char *const c_pattern_format = cs_subset_string(NeoMutt->sub, "pattern_format");
143 mutt_expando_format(buf, buflen, 0, menu->win->state.cols, NONULL(c_pattern_format),
145}
146
150static void free_pattern_menu(struct Menu *menu, void **ptr)
151{
152 struct PatternEntry *entries = *ptr;
153
154 for (size_t i = 0; i < menu->max; i++)
155 {
156 FREE(&entries[i].tag);
157 FREE(&entries[i].expr);
158 FREE(&entries[i].desc);
159 }
160
161 FREE(ptr);
162}
163
169static struct Menu *create_pattern_menu(struct MuttWindow *dlg)
170{
171 int num_entries = 0, i = 0;
172 struct Buffer *entrybuf = NULL;
173
174 while (Flags[num_entries].tag)
175 num_entries++;
176 /* Add three more hard-coded entries */
177 num_entries += 3;
178 struct PatternEntry *entries = mutt_mem_calloc(num_entries, sizeof(struct PatternEntry));
179
180 struct Menu *menu = dlg->wdata;
182 menu->mdata = entries;
184 menu->max = num_entries;
185
186 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
187 // L10N: Pattern completion menu title
188 sbar_set_title(sbar, _("Patterns"));
189
190 entrybuf = buf_pool_get();
191 while (Flags[i].tag)
192 {
193 entries[i].num = i + 1;
194
195 buf_printf(entrybuf, "~%c", (char) Flags[i].tag);
196 entries[i].tag = mutt_str_dup(buf_string(entrybuf));
197
198 switch (Flags[i].eat_arg)
199 {
200 case EAT_REGEX:
201 /* L10N:
202 Pattern Completion Menu argument type: a regular expression
203 */
204 buf_add_printf(entrybuf, " %s", _("EXPR"));
205 break;
206 case EAT_RANGE:
208 /* L10N:
209 Pattern Completion Menu argument type: a numeric range.
210 Used by ~m, ~n, ~X, ~z.
211 */
212 buf_add_printf(entrybuf, " %s", _("RANGE"));
213 break;
214 case EAT_DATE:
215 /* L10N:
216 Pattern Completion Menu argument type: a date range
217 Used by ~d, ~r.
218 */
219 buf_add_printf(entrybuf, " %s", _("DATERANGE"));
220 break;
221 case EAT_QUERY:
222 /* L10N:
223 Pattern Completion Menu argument type: a query
224 Used by ~I.
225 */
226 buf_add_printf(entrybuf, " %s", _("QUERY"));
227 break;
228 default:
229 break;
230 }
231 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
232 entries[i].desc = mutt_str_dup(_(Flags[i].desc));
233
234 i++;
235 }
236
237 /* Add struct MuttThread patterns manually.
238 * Note we allocated 3 extra slots for these above. */
239
240 /* L10N:
241 Pattern Completion Menu argument type: a nested pattern.
242 Used by ~(), ~<(), ~>().
243 */
244 const char *patternstr = _("PATTERN");
245
246 entries[i].num = i + 1;
247 entries[i].tag = mutt_str_dup("~()");
248 buf_printf(entrybuf, "~(%s)", patternstr);
249 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
250 // L10N: Pattern Completion Menu description for ~()
251 entries[i].desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
252 i++;
253
254 entries[i].num = i + 1;
255 entries[i].tag = mutt_str_dup("~<()");
256 buf_printf(entrybuf, "~<(%s)", patternstr);
257 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
258 // L10N: Pattern Completion Menu description for ~<()
259 entries[i].desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
260 i++;
261
262 entries[i].num = i + 1;
263 entries[i].tag = mutt_str_dup("~>()");
264 buf_printf(entrybuf, "~>(%s)", patternstr);
265 entries[i].expr = mutt_str_dup(buf_string(entrybuf));
266 // L10N: Pattern Completion Menu description for ~>()
267 entries[i].desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
268
269 buf_pool_release(&entrybuf);
270
271 return menu;
272}
273
280{
281 if (nc->event_type != NT_CONFIG)
282 return 0;
283 if (!nc->global_data || !nc->event_data)
284 return -1;
285
286 struct EventConfig *ev_c = nc->event_data;
287
288 if (!mutt_str_equal(ev_c->name, "pattern_format"))
289 return 0;
290
291 struct Menu *menu = nc->global_data;
293 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
294
295 return 0;
296}
297
306{
307 if (nc->event_type != NT_WINDOW)
308 return 0;
309 if (!nc->global_data || !nc->event_data)
310 return -1;
312 return 0;
313
314 struct MuttWindow *win_menu = nc->global_data;
315 struct EventWindow *ev_w = nc->event_data;
316 if (ev_w->win != win_menu)
317 return 0;
318
319 struct Menu *menu = win_menu->wdata;
320
323
324 mutt_debug(LL_DEBUG5, "window delete done\n");
325 return 0;
326}
327
337bool dlg_pattern(char *buf, size_t buflen)
338{
340 struct Menu *menu = create_pattern_menu(dlg);
341
342 struct PatternData pd = { false, false, buf, buflen, menu };
343 dlg->wdata = &pd;
344
345 // NT_COLOR is handled by the SimpleDialog
348
349 struct MuttWindow *old_focus = window_set_focus(menu->win);
350 // ---------------------------------------------------------------------------
351 // Event Loop
352 int op = OP_NULL;
353 do
354 {
355 menu_tagging_dispatcher(menu->win, op);
356 window_redraw(NULL);
357
359 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
360 if (op < 0)
361 continue;
362 if (op == OP_NULL)
363 {
365 continue;
366 }
368
369 int rc = pattern_function_dispatcher(dlg, op);
370 if (rc == FR_UNKNOWN)
371 rc = menu_function_dispatcher(menu->win, op);
372 if (rc == FR_UNKNOWN)
373 rc = global_function_dispatcher(NULL, op);
374 } while (!pd.done);
375 // ---------------------------------------------------------------------------
376
377 window_set_focus(old_focus);
378 simple_dialog_free(&dlg);
379 return pd.selection;
380}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:216
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
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:169
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: format.c:215
Flags to control mutt_expando_format()
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:475
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:305
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:169
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:317
static const char * pattern_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Format a string for the pattern completion menu - Implements format_t -.
Definition: dlg_pattern.c:106
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:739
bool dlg_pattern(char *buf, size_t buflen)
Show menu to select a Pattern -.
Definition: dlg_pattern.c:337
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
Create a Pattern for the Menu - Implements Menu::make_entry() -.
Definition: dlg_pattern.c:138
static void free_pattern_menu(struct Menu *menu, void **ptr)
Free the Pattern Completion menu - Implements Menu::mdata_free() -.
Definition: dlg_pattern.c:150
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pattern.c:305
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pattern.c:279
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:52
@ 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:60
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:180
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:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
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
Some miscellaneous functions.
@ 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
@ EAT_RANGE
Process a number (range)
Definition: private.h:53
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:54
@ EAT_DATE
Process a date (range)
Definition: private.h:52
@ EAT_QUERY
Process a query string.
Definition: private.h:55
@ EAT_REGEX
Process a regex.
Definition: private.h:51
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
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.
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:34
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
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:96
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:151
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:72
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
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:36
const char * desc
Description of pattern.
Definition: private.h:40
const char * tag
Copied to buffer if selected.
Definition: private.h:38
int num
Index number.
Definition: private.h:37
const char * expr
Displayed in the menu.
Definition: private.h:39
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_DIALOG
Simple Dialog.
Definition: type.h:43
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46