NeoMutt  2022-04-29-247-gc6aae8
Teaching an old dog new tricks
DOXYGEN
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 "menu/lib.h"
81#include "format_flags.h"
82#include "functions.h"
83#include "keymap.h"
84#include "mutt_logging.h"
85#include "muttlib.h"
86#include "opcodes.h"
87
89static const struct Mapping PatternHelp[] = {
90 // clang-format off
91 { N_("Exit"), OP_EXIT },
92 { N_("Select"), OP_GENERIC_SELECT_ENTRY },
93 { N_("Help"), OP_HELP },
94 { NULL, 0 },
95 // clang-format on
96};
97
107static const char *pattern_format_str(char *buf, size_t buflen, size_t col, int cols,
108 char op, const char *src, const char *prec,
109 const char *if_str, const char *else_str,
110 intptr_t data, MuttFormatFlags flags)
111{
112 struct PatternEntry *entry = (struct PatternEntry *) data;
113
114 switch (op)
115 {
116 case 'd':
117 mutt_format_s(buf, buflen, prec, NONULL(entry->desc));
118 break;
119 case 'e':
120 mutt_format_s(buf, buflen, prec, NONULL(entry->expr));
121 break;
122 case 'n':
123 {
124 char tmp[32] = { 0 };
125 snprintf(tmp, sizeof(tmp), "%%%sd", prec);
126 snprintf(buf, buflen, tmp, entry->num);
127 break;
128 }
129 }
130
131 return src;
132}
133
139static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
140{
141 struct PatternEntry *entry = &((struct PatternEntry *) menu->mdata)[num];
142
143 const char *const c_pattern_format = cs_subset_string(NeoMutt->sub, "pattern_format");
144 mutt_expando_format(buf, buflen, 0, menu->win->state.cols, NONULL(c_pattern_format),
146}
147
151static void free_pattern_menu(struct Menu *menu, void **ptr)
152{
153 struct PatternEntry *entries = *ptr;
154
155 for (size_t i = 0; i < menu->max; i++)
156 {
157 FREE(&entries[i].tag);
158 FREE(&entries[i].expr);
159 FREE(&entries[i].desc);
160 }
161
162 FREE(ptr);
163}
164
170static struct Menu *create_pattern_menu(struct MuttWindow *dlg)
171{
172 int num_entries = 0, i = 0;
173 struct Buffer *entrybuf = NULL;
174
175 while (Flags[num_entries].tag)
176 num_entries++;
177 /* Add three more hard-coded entries */
178 num_entries += 3;
179 struct PatternEntry *entries = mutt_mem_calloc(num_entries, sizeof(struct PatternEntry));
180
181 struct Menu *menu = dlg->wdata;
183 menu->mdata = entries;
185 menu->max = num_entries;
186
187 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
188 // L10N: Pattern completion menu title
189 sbar_set_title(sbar, _("Patterns"));
190
191 entrybuf = mutt_buffer_pool_get();
192 while (Flags[i].tag)
193 {
194 entries[i].num = i + 1;
195
196 mutt_buffer_printf(entrybuf, "~%c", (char) Flags[i].tag);
197 entries[i].tag = mutt_str_dup(mutt_buffer_string(entrybuf));
198
199 switch (Flags[i].eat_arg)
200 {
201 case EAT_REGEX:
202 /* L10N:
203 Pattern Completion Menu argument type: a regular expression
204 */
205 mutt_buffer_add_printf(entrybuf, " %s", _("EXPR"));
206 break;
207 case EAT_RANGE:
209 /* L10N:
210 Pattern Completion Menu argument type: a numeric range.
211 Used by ~m, ~n, ~X, ~z.
212 */
213 mutt_buffer_add_printf(entrybuf, " %s", _("RANGE"));
214 break;
215 case EAT_DATE:
216 /* L10N:
217 Pattern Completion Menu argument type: a date range
218 Used by ~d, ~r.
219 */
220 mutt_buffer_add_printf(entrybuf, " %s", _("DATERANGE"));
221 break;
222 case EAT_QUERY:
223 /* L10N:
224 Pattern Completion Menu argument type: a query
225 Used by ~I.
226 */
227 mutt_buffer_add_printf(entrybuf, " %s", _("QUERY"));
228 break;
229 default:
230 break;
231 }
232 entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
233 entries[i].desc = mutt_str_dup(_(Flags[i].desc));
234
235 i++;
236 }
237
238 /* Add struct MuttThread patterns manually.
239 * Note we allocated 3 extra slots for these above. */
240
241 /* L10N:
242 Pattern Completion Menu argument type: a nested pattern.
243 Used by ~(), ~<(), ~>().
244 */
245 const char *patternstr = _("PATTERN");
246
247 entries[i].num = i + 1;
248 entries[i].tag = mutt_str_dup("~()");
249 mutt_buffer_printf(entrybuf, "~(%s)", patternstr);
250 entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
251 // L10N: Pattern Completion Menu description for ~()
252 entries[i].desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
253 i++;
254
255 entries[i].num = i + 1;
256 entries[i].tag = mutt_str_dup("~<()");
257 mutt_buffer_printf(entrybuf, "~<(%s)", patternstr);
258 entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
259 // L10N: Pattern Completion Menu description for ~<()
260 entries[i].desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
261 i++;
262
263 entries[i].num = i + 1;
264 entries[i].tag = mutt_str_dup("~>()");
265 mutt_buffer_printf(entrybuf, "~>(%s)", patternstr);
266 entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
267 // L10N: Pattern Completion Menu description for ~>()
268 entries[i].desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
269
270 mutt_buffer_pool_release(&entrybuf);
271
272 return menu;
273}
274
281{
282 if (nc->event_type != NT_CONFIG)
283 return 0;
284 if (!nc->global_data || !nc->event_data)
285 return -1;
286
287 struct EventConfig *ev_c = nc->event_data;
288
289 if (!mutt_str_equal(ev_c->name, "pattern_format"))
290 return 0;
291
292 struct Menu *menu = nc->global_data;
294 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
295
296 return 0;
297}
298
307{
308 if (nc->event_type != NT_WINDOW)
309 return 0;
310 if (!nc->global_data || !nc->event_data)
311 return -1;
313 return 0;
314
315 struct MuttWindow *win_menu = nc->global_data;
316 struct EventWindow *ev_w = nc->event_data;
317 if (ev_w->win != win_menu)
318 return 0;
319
320 struct Menu *menu = win_menu->wdata;
321
324
325 mutt_debug(LL_DEBUG5, "window delete done\n");
326 return 0;
327}
328
335bool dlg_select_pattern(char *buf, size_t buflen)
336{
338 struct Menu *menu = create_pattern_menu(dlg);
339
340 struct PatternData pd = { false, false, buf, buflen, menu };
341 dlg->wdata = &pd;
342
343 // NT_COLOR is handled by the SimpleDialog
346
347 // ---------------------------------------------------------------------------
348 // Event Loop
349 int op = OP_NULL;
350 do
351 {
353 window_redraw(NULL);
354
355 struct KeyEvent event = km_dokey_event(MENU_GENERIC);
356 if (event.ch == 'q')
357 op = OP_EXIT;
358 else
359 op = event.op;
360
361 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
362 if (op < 0)
363 continue;
364 if (op == OP_NULL)
365 {
367 continue;
368 }
370
371 int rc = pattern_function_dispatcher(dlg, op);
372
373 if (rc == FR_UNKNOWN)
374 rc = menu_function_dispatcher(menu->win, op);
375 if (rc == FR_UNKNOWN)
376 rc = global_function_dispatcher(NULL, op);
377 } while (!pd.done);
378 // ---------------------------------------------------------------------------
379
380 simple_dialog_free(&dlg);
381 return pd.selection;
382}
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:211
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: curs_lib.c:793
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
bool dlg_select_pattern(char *buf, size_t buflen)
Show menu to select a Pattern.
Definition: dlg_pattern.c:335
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition: dlg_pattern.c:89
static struct Menu * create_pattern_menu(struct MuttWindow *dlg)
Create the Pattern Completion menu.
Definition: dlg_pattern.c:170
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 menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int pattern_function_dispatcher(struct MuttWindow *win, int op)
Perform a Pattern function - Implements function_dispatcher_t -.
Definition: functions.c:78
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:320
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:107
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:777
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
Create a line for the Pattern Completion menu - Implements Menu::make_entry() -.
Definition: dlg_pattern.c:139
static void free_pattern_menu(struct Menu *menu, void **ptr)
Free the Pattern Completion menu - Implements Menu::mdata_free() -.
Definition: dlg_pattern.c:151
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pattern.c:306
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pattern.c:280
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
struct KeyEvent km_dokey_event(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:637
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1061
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
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:43
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:178
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:228
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:189
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
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:604
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:522
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ WT_DLG_PATTERN
Pattern Dialog, create_pattern_menu()
Definition: mutt_window.h:87
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:206
Some miscellaneous functions.
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ 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:46
All user-callable functions.
const struct PatternFlags Flags[]
Lookup table for all patterns.
Definition: flags.c:40
@ EAT_RANGE
Process a number (range)
Definition: private.h:51
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:52
@ EAT_DATE
Process a date (range)
Definition: private.h:50
@ EAT_QUERY
Process a query string.
Definition: private.h:53
@ EAT_REGEX
Process a regex.
Definition: private.h:49
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
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
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.
Definition: mutt_window.h:216
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
An event such as a keypress.
Definition: keymap.h:65
int op
Function opcode, e.g. OP_HELP.
Definition: keymap.h:67
int ch
Raw key pressed.
Definition: keymap.h:66
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:69
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:76
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:152
void * mdata
Private data.
Definition: lib.h:137
int max
Number of entries in the menu.
Definition: lib.h:71
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:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
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:34
const char * desc
Description of pattern.
Definition: private.h:38
const char * tag
Copied to buffer if selected.
Definition: private.h:36
int num
Index number.
Definition: private.h:35
const char * expr
Displayed in the menu.
Definition: private.h:37
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
@ MENU_GENERIC
Generic selection list.
Definition: type.h:45