NeoMutt  2024-12-12-14-g7b49f7
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 "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, 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 */
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", Flags[i].tag);
142 entry.tag = buf_strdup(buf);
143
144 switch (Flags[i].eat_arg)
145 {
146 case EAT_REGEX:
147 /* L10N:
148 Pattern Completion Menu argument type: a regular expression
149 */
150 buf_add_printf(buf, " %s", _("EXPR"));
151 break;
152 case EAT_RANGE:
154 /* L10N:
155 Pattern Completion Menu argument type: a numeric range.
156 Used by ~m, ~n, ~X, ~z.
157 */
158 buf_add_printf(buf, " %s", _("RANGE"));
159 break;
160 case EAT_DATE:
161 /* L10N:
162 Pattern Completion Menu argument type: a date range
163 Used by ~d, ~r.
164 */
165 buf_add_printf(buf, " %s", _("DATERANGE"));
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 default:
175 break;
176 }
177
178 entry.expr = buf_strdup(buf);
179 entry.desc = mutt_str_dup(_(Flags[i].desc));
180
181 ARRAY_ADD(pea, entry);
182 }
183
184 /* Add struct MuttThread patterns manually.
185 * Note we allocated 3 extra slots for these above. */
186
187 /* L10N:
188 Pattern Completion Menu argument type: a nested pattern.
189 Used by ~(), ~<(), ~>().
190 */
191 const char *patternstr = _("PATTERN");
192
193 entry.num = ARRAY_SIZE(pea) + 1;
194 entry.tag = mutt_str_dup("~()");
195 buf_printf(buf, "~(%s)", patternstr);
196 entry.expr = buf_strdup(buf);
197 // L10N: Pattern Completion Menu description for ~()
198 entry.desc = mutt_str_dup(_("messages in threads containing messages matching PATTERN"));
199 ARRAY_ADD(pea, entry);
200
201 entry.num = ARRAY_SIZE(pea) + 1;
202 entry.tag = mutt_str_dup("~<()");
203 buf_printf(buf, "~<(%s)", patternstr);
204 entry.expr = buf_strdup(buf);
205 // L10N: Pattern Completion Menu description for ~<()
206 entry.desc = mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
207 ARRAY_ADD(pea, entry);
208
209 entry.num = ARRAY_SIZE(pea) + 1;
210 entry.tag = mutt_str_dup("~>()");
211 buf_printf(buf, "~>(%s)", patternstr);
212 entry.expr = buf_strdup(buf);
213 // L10N: Pattern Completion Menu description for ~>()
214 entry.desc = mutt_str_dup(_("messages having an immediate child matching PATTERN"));
215 ARRAY_ADD(pea, entry);
216
217 buf_pool_release(&buf);
218}
219
226{
227 if (nc->event_type != NT_CONFIG)
228 return 0;
229 if (!nc->global_data || !nc->event_data)
230 return -1;
231
232 struct EventConfig *ev_c = nc->event_data;
233
234 if (!mutt_str_equal(ev_c->name, "pattern_format"))
235 return 0;
236
237 struct Menu *menu = nc->global_data;
239 mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
240
241 return 0;
242}
243
252{
253 if (nc->event_type != NT_WINDOW)
254 return 0;
255 if (!nc->global_data || !nc->event_data)
256 return -1;
258 return 0;
259
260 struct MuttWindow *win_menu = nc->global_data;
261 struct EventWindow *ev_w = nc->event_data;
262 if (ev_w->win != win_menu)
263 return 0;
264
265 struct Menu *menu = win_menu->wdata;
266
269
270 mutt_debug(LL_DEBUG5, "window delete done\n");
271 return 0;
272}
273
282bool dlg_pattern(struct Buffer *buf)
283{
284 struct PatternData *pd = pattern_data_new();
285
288
289 struct Menu *menu = sdw.menu;
290 pd->menu = menu;
291 pd->buf = buf;
292
293 menu->mdata = pd;
296 menu->max = ARRAY_SIZE(&pd->entries);
297
298 // L10N: Pattern completion menu title
299 sbar_set_title(sdw.sbar, _("Patterns"));
300
301 // NT_COLOR is handled by the SimpleDialog
304
305 struct MuttWindow *old_focus = window_set_focus(menu->win);
306 // ---------------------------------------------------------------------------
307 // Event Loop
308 int op = OP_NULL;
309 do
310 {
311 menu_tagging_dispatcher(menu->win, op);
312 window_redraw(NULL);
313
315 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
316 if (op < 0)
317 continue;
318 if (op == OP_NULL)
319 {
321 continue;
322 }
324
325 int rc = pattern_function_dispatcher(sdw.dlg, op);
326 if (rc == FR_UNKNOWN)
327 rc = menu_function_dispatcher(menu->win, op);
328 if (rc == FR_UNKNOWN)
329 rc = global_function_dispatcher(NULL, op);
330 } while (!pd->done);
331 // ---------------------------------------------------------------------------
332
333 bool rc = pd->selection;
334
335 window_set_focus(old_focus);
337
338 return rc;
339}
#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
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
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.
Definition: dlg_pattern.c:125
int expando_filter(const struct Expando *exp, const struct ExpandoRenderCallback *erc, 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
bool dlg_pattern(struct Buffer *buf)
Show menu to select a Pattern -.
Definition: dlg_pattern.c:282
#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:102
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:251
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlg_pattern.c:225
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:634
struct MuttWindow * window_set_focus(struct MuttWindow *win)
Set the Window focus.
Definition: mutt_window.c:684
@ WT_DLG_PATTERN
Pattern Dialog, dlg_pattern()
Definition: mutt_window.h:88
@ 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 ExpandoRenderCallback PatternRenderCallbacks[]
Callbacks for Pattern Expandos.
Definition: expando.c:75
const struct PatternFlags Flags[]
Lookup table for all patterns.
Definition: flags.c:40
@ 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:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
#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
Sidebar Expando definitions.
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
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: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