NeoMutt  2024-11-14-34-g5aaf0d
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 <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 "functions.h"
83#include "mutt_logging.h"
84#include "pattern_data.h"
85
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
101void pattern_d(const struct ExpandoNode *node, void *data,
102 MuttFormatFlags flags, struct Buffer *buf)
103{
104 const struct PatternEntry *entry = data;
105
106 const char *s = entry->desc;
107 buf_strcpy(buf, s);
108}
109
113void pattern_e(const struct ExpandoNode *node, void *data,
114 MuttFormatFlags flags, struct Buffer *buf)
115{
116 const struct PatternEntry *entry = data;
117
118 const char *s = entry->expr;
119 buf_strcpy(buf, s);
120}
121
125long pattern_n_num(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
126{
127 const struct PatternEntry *entry = data;
128
129 return entry->num;
130}
131
137static int pattern_make_entry(struct Menu *menu, int line, int max_cols, struct Buffer *buf)
138{
139 struct PatternData *pd = menu->mdata;
140
141 struct PatternEntry *entry = ARRAY_GET(&pd->entries, line);
142
143 const bool c_arrow_cursor = cs_subset_bool(menu->sub, "arrow_cursor");
144 if (c_arrow_cursor)
145 {
146 const char *const c_arrow_string = cs_subset_string(menu->sub, "arrow_string");
147 if (max_cols > 0)
148 max_cols -= (mutt_strwidth(c_arrow_string) + 1);
149 }
150
151 const struct Expando *c_pattern_format = cs_subset_expando(NeoMutt->sub, "pattern_format");
152 return expando_filter(c_pattern_format, PatternRenderData, entry,
153 MUTT_FORMAT_ARROWCURSOR, max_cols, buf);
154}
155
160static void create_pattern_entries(struct PatternEntryArray *pea)
161{
162 int num_entries = 0;
163 while (Flags[num_entries].tag != 0)
164 num_entries++;
165
166 /* Add three more hard-coded entries */
167 ARRAY_RESERVE(pea, num_entries + 3);
168
169 struct Buffer *buf = buf_pool_get();
170
171 struct PatternEntry entry = { 0 };
172 for (int i = 0; Flags[i].tag != '\0'; i++)
173 {
174 entry.num = i + 1;
175
176 buf_printf(buf, "~%c", Flags[i].tag);
177 entry.tag = buf_strdup(buf);
178
179 switch (Flags[i].eat_arg)
180 {
181 case EAT_REGEX:
182 /* L10N:
183 Pattern Completion Menu argument type: a regular expression
184 */
185 buf_add_printf(buf, " %s", _("EXPR"));
186 break;
187 case EAT_RANGE:
189 /* L10N:
190 Pattern Completion Menu argument type: a numeric range.
191 Used by ~m, ~n, ~X, ~z.
192 */
193 buf_add_printf(buf, " %s", _("RANGE"));
194 break;
195 case EAT_DATE:
196 /* L10N:
197 Pattern Completion Menu argument type: a date range
198 Used by ~d, ~r.
199 */
200 buf_add_printf(buf, " %s", _("DATERANGE"));
201 break;
202 case EAT_QUERY:
203 /* L10N:
204 Pattern Completion Menu argument type: a query
205 Used by ~I.
206 */
207 buf_add_printf(buf, " %s", _("QUERY"));
208 break;
209 default:
210 break;
211 }
212
213 entry.expr = buf_strdup(buf);
214 entry.desc = mutt_str_dup(_(Flags[i].desc));
215
216 ARRAY_ADD(pea, entry);
217 }
218
219 /* Add struct MuttThread patterns manually.
220 * Note we allocated 3 extra slots for these above. */
221
222 /* L10N:
223 Pattern Completion Menu argument type: a nested pattern.
224 Used by ~(), ~<(), ~>().
225 */
226 const char *patternstr = _("PATTERN");
227
228 entry.num = ARRAY_SIZE(pea) + 1;
229 entry.tag = mutt_str_dup("~()");
230 buf_printf(buf, "~(%s)", patternstr);
231 entry.expr = buf_strdup(buf);
232 // L10N: Pattern Completion Menu description for ~()
233 entry.desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
234 ARRAY_ADD(pea, entry);
235
236 entry.num = ARRAY_SIZE(pea) + 1;
237 entry.tag = mutt_str_dup("~<()");
238 buf_printf(buf, "~<(%s)", patternstr);
239 entry.expr = buf_strdup(buf);
240 // L10N: Pattern Completion Menu description for ~<()
241 entry.desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
242 ARRAY_ADD(pea, entry);
243
244 entry.num = ARRAY_SIZE(pea) + 1;
245 entry.tag = mutt_str_dup("~>()");
246 buf_printf(buf, "~>(%s)", patternstr);
247 entry.expr = buf_strdup(buf);
248 // L10N: Pattern Completion Menu description for ~>()
249 entry.desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
250 ARRAY_ADD(pea, entry);
251
252 buf_pool_release(&buf);
253}
254
261{
262 if (nc->event_type != NT_CONFIG)
263 return 0;
264 if (!nc->global_data || !nc->event_data)
265 return -1;
266
267 struct EventConfig *ev_c = nc->event_data;
268
269 if (!mutt_str_equal(ev_c->name, "pattern_format"))
270 return 0;
271
272 struct Menu *menu = nc->global_data;
274 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
275
276 return 0;
277}
278
287{
288 if (nc->event_type != NT_WINDOW)
289 return 0;
290 if (!nc->global_data || !nc->event_data)
291 return -1;
293 return 0;
294
295 struct MuttWindow *win_menu = nc->global_data;
296 struct EventWindow *ev_w = nc->event_data;
297 if (ev_w->win != win_menu)
298 return 0;
299
300 struct Menu *menu = win_menu->wdata;
301
304
305 mutt_debug(LL_DEBUG5, "window delete done\n");
306 return 0;
307}
308
317bool dlg_pattern(struct Buffer *buf)
318{
319 struct PatternData *pd = pattern_data_new();
320
323
324 struct Menu *menu = sdw.menu;
325 pd->menu = menu;
326 pd->buf = buf;
327
328 menu->mdata = pd;
331 menu->max = ARRAY_SIZE(&pd->entries);
332
333 // L10N: Pattern completion menu title
334 sbar_set_title(sdw.sbar, _("Patterns"));
335
336 // NT_COLOR is handled by the SimpleDialog
339
340 struct MuttWindow *old_focus = window_set_focus(menu->win);
341 // ---------------------------------------------------------------------------
342 // Event Loop
343 int op = OP_NULL;
344 do
345 {
346 menu_tagging_dispatcher(menu->win, op);
347 window_redraw(NULL);
348
350 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
351 if (op < 0)
352 continue;
353 if (op == OP_NULL)
354 {
356 continue;
357 }
359
360 int rc = pattern_function_dispatcher(sdw.dlg, op);
361 if (rc == FR_UNKNOWN)
362 rc = menu_function_dispatcher(menu->win, op);
363 if (rc == FR_UNKNOWN)
364 rc = global_function_dispatcher(NULL, op);
365 } while (!pd->done);
366 // ---------------------------------------------------------------------------
367
368 bool rc = pd->selection;
369
370 window_set_focus(old_focus);
372
373 return rc;
374}
375
381const struct ExpandoRenderData PatternRenderData[] = {
382 // clang-format off
386 { -1, -1, NULL, NULL },
387 // clang-format on
388};
#define ARRAY_RESERVE(head, num)
Reserve memory for the array.
Definition: array.h:189
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#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
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
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.
Definition: config_type.c:357
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:86
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition: dlg_pattern.c:89
static void create_pattern_entries(struct PatternEntryArray *pea)
Create the Pattern Entries.
Definition: dlg_pattern.c:160
@ ED_PATTERN
Pattern ED_PAT_ ExpandoDataPattern.
Definition: domain.h:50
int expando_filter(const struct Expando *exp, const struct ExpandoRenderData *rdata, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Render an Expando and run the result through a filter.
Definition: filter.c:138
Parse Expando string.
int km_dokey(enum MenuType mtype, GetChFlags flags)
Determine what a keypress should do.
Definition: get.c:464
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: get.c:294
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:81
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:125
void pattern_e(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Pattern: pattern expression - Implements ExpandoRenderData::get_string() -.
Definition: dlg_pattern.c:113
void pattern_d(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Pattern: pattern description - Implements ExpandoRenderData::get_string() -.
Definition: dlg_pattern.c:101
bool dlg_pattern(struct Buffer *buf)
Show menu to select a Pattern -.
Definition: dlg_pattern.c:317
#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:137
void pattern_data_free(struct Menu *menu, void **ptr)
Free Pattern Data - Implements Menu::mdata_free() -.
Definition: pattern_data.c:49
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_pattern.c:286
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pattern.c:260
Convenience wrapper for the gui headers.
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:168
struct SimpleDialogWindows 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
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:660
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:633
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:683
@ 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:228
@ 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:40
@ ED_PAT_EXPRESION
PatternEntry.expr.
Definition: private.h:41
@ ED_PAT_NUMBER
PatternEntry.num.
Definition: private.h:42
@ EAT_RANGE
Process a number (range)
Definition: private.h:55
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:56
@ EAT_DATE
Process a date (range)
Definition: private.h:54
@ EAT_QUERY
Process a query string.
Definition: private.h:57
@ EAT_REGEX
Process a regex.
Definition: private.h:53
struct PatternData * pattern_data_new(void)
Create new Pattern Data.
Definition: pattern_data.c:37
Private Pattern Data.
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:238
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:239
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:144
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:137
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
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: pattern_data.h:47
struct Menu * menu
Pattern Menu.
Definition: pattern_data.h:51
struct PatternEntryArray entries
Patterns for the Menu.
Definition: pattern_data.h:52
bool done
Should we close the Dialog?
Definition: pattern_data.h:48
struct Buffer * buf
Buffer for the results.
Definition: pattern_data.h:50
bool selection
Was a selection made?
Definition: pattern_data.h:49
A line in the Pattern Completion menu.
Definition: pattern_data.h:35
const char * desc
Description of pattern.
Definition: pattern_data.h:39
const char * tag
Copied to buffer if selected.
Definition: pattern_data.h:37
int num
Index number.
Definition: pattern_data.h:36
const char * expr
Displayed in the menu.
Definition: pattern_data.h:38
char tag
Character used to represent this operation, e.g. 'A' for '~A'.
Definition: private.h:65
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
@ MENU_DIALOG
Simple Dialog.
Definition: type.h:43
@ MENU_GENERIC
Generic selection list.
Definition: type.h:46