NeoMutt  2021-10-22-8-g9cb437
Teaching an old dog new tricks
DOXYGEN
dlgpattern.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 "muttlib.h"
83 #include "opcodes.h"
84 
89 {
90  int num;
91  const char *tag;
92  const char *expr;
93  const char *desc;
94 };
95 
97 static const struct Mapping PatternHelp[] = {
98  // clang-format off
99  { N_("Exit"), OP_EXIT },
100  { N_("Select"), OP_GENERIC_SELECT_ENTRY },
101  { N_("Help"), OP_HELP },
102  { NULL, 0 },
103  // clang-format on
104 };
105 
115 static const char *pattern_format_str(char *buf, size_t buflen, size_t col, int cols,
116  char op, const char *src, const char *prec,
117  const char *if_str, const char *else_str,
118  intptr_t data, MuttFormatFlags flags)
119 {
120  struct PatternEntry *entry = (struct PatternEntry *) data;
121 
122  switch (op)
123  {
124  case 'd':
125  mutt_format_s(buf, buflen, prec, NONULL(entry->desc));
126  break;
127  case 'e':
128  mutt_format_s(buf, buflen, prec, NONULL(entry->expr));
129  break;
130  case 'n':
131  {
132  char tmp[32];
133  snprintf(tmp, sizeof(tmp), "%%%sd", prec);
134  snprintf(buf, buflen, tmp, entry->num);
135  break;
136  }
137  }
138 
139  return src;
140 }
141 
145 static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
146 {
147  struct PatternEntry *entry = &((struct PatternEntry *) menu->mdata)[num];
148 
149  const char *const c_pattern_format =
150  cs_subset_string(NeoMutt->sub, "pattern_format");
151  mutt_expando_format(buf, buflen, 0, menu->win->state.cols, NONULL(c_pattern_format),
152  pattern_format_str, (intptr_t) entry, MUTT_FORMAT_ARROWCURSOR);
153 }
154 
158 static void free_pattern_menu(struct Menu *menu, void **ptr)
159 {
160  struct PatternEntry *entries = *ptr;
161 
162  for (size_t i = 0; i < menu->max; i++)
163  {
164  FREE(&entries[i].tag);
165  FREE(&entries[i].expr);
166  FREE(&entries[i].desc);
167  }
168 
169  FREE(ptr);
170 }
171 
177 static struct Menu *create_pattern_menu(struct MuttWindow *dlg)
178 {
179  int num_entries = 0, i = 0;
180  struct Buffer *entrybuf = NULL;
181 
182  while (Flags[num_entries].tag)
183  num_entries++;
184  /* Add three more hard-coded entries */
185  num_entries += 3;
186  struct PatternEntry *entries =
187  mutt_mem_calloc(num_entries, sizeof(struct PatternEntry));
188 
189  struct Menu *menu = dlg->wdata;
191  menu->mdata = entries;
193  menu->max = num_entries;
194 
195  struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
196  // L10N: Pattern completion menu title
197  sbar_set_title(sbar, _("Patterns"));
198 
199  entrybuf = mutt_buffer_pool_get();
200  while (Flags[i].tag)
201  {
202  entries[i].num = i + 1;
203 
204  mutt_buffer_printf(entrybuf, "~%c", (char) Flags[i].tag);
205  entries[i].tag = mutt_str_dup(mutt_buffer_string(entrybuf));
206 
207  switch (Flags[i].eat_arg)
208  {
209  case EAT_REGEX:
210  /* L10N:
211  Pattern Completion Menu argument type: a regular expression
212  */
213  mutt_buffer_add_printf(entrybuf, " %s", _("EXPR"));
214  break;
215  case EAT_RANGE:
216  case EAT_MESSAGE_RANGE:
217  /* L10N:
218  Pattern Completion Menu argument type: a numeric range.
219  Used by ~m, ~n, ~X, ~z.
220  */
221  mutt_buffer_add_printf(entrybuf, " %s", _("RANGE"));
222  break;
223  case EAT_DATE:
224  /* L10N:
225  Pattern Completion Menu argument type: a date range
226  Used by ~d, ~r.
227  */
228  mutt_buffer_add_printf(entrybuf, " %s", _("DATERANGE"));
229  break;
230  case EAT_QUERY:
231  /* L10N:
232  Pattern Completion Menu argument type: a query
233  Used by ~I.
234  */
235  mutt_buffer_add_printf(entrybuf, " %s", _("QUERY"));
236  break;
237  default:
238  break;
239  }
240  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
241  entries[i].desc = mutt_str_dup(_(Flags[i].desc));
242 
243  i++;
244  }
245 
246  /* Add struct MuttThread patterns manually.
247  * Note we allocated 3 extra slots for these above. */
248 
249  /* L10N:
250  Pattern Completion Menu argument type: a nested pattern.
251  Used by ~(), ~<(), ~>().
252  */
253  const char *patternstr = _("PATTERN");
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(
261  _("messages in threads containing messages matching PATTERN"));
262  i++;
263 
264  entries[i].num = i + 1;
265  entries[i].tag = mutt_str_dup("~<()");
266  mutt_buffer_printf(entrybuf, "~<(%s)", patternstr);
267  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
268  // L10N: Pattern Completion Menu description for ~<()
269  entries[i].desc =
270  mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
271  i++;
272 
273  entries[i].num = i + 1;
274  entries[i].tag = mutt_str_dup("~>()");
275  mutt_buffer_printf(entrybuf, "~>(%s)", patternstr);
276  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
277  // L10N: Pattern Completion Menu description for ~>()
278  entries[i].desc =
279  mutt_str_dup(_("messages having an immediate child matching PATTERN"));
280 
281  mutt_buffer_pool_release(&entrybuf);
282 
283  return menu;
284 }
285 
292 {
293  if ((nc->event_type != NT_CONFIG) || !nc->global_data || !nc->event_data)
294  return -1;
295 
296  struct EventConfig *ev_c = nc->event_data;
297 
298  if (!mutt_str_equal(ev_c->name, "pattern_format"))
299  return 0;
300 
301  struct Menu *menu = nc->global_data;
303  mutt_debug(LL_DEBUG5, "config done, request WA_RECALC, MENU_REDRAW_FULL\n");
304 
305  return 0;
306 }
307 
316 {
317  if ((nc->event_type != NT_WINDOW) || !nc->global_data || !nc->event_data)
318  return -1;
319 
320  if (nc->event_subtype != NT_WINDOW_DELETE)
321  return 0;
322 
323  struct MuttWindow *win_menu = nc->global_data;
324  struct EventWindow *ev_w = nc->event_data;
325  if (ev_w->win != win_menu)
326  return 0;
327 
328  struct Menu *menu = win_menu->wdata;
329 
332 
333  mutt_debug(LL_DEBUG5, "window delete done\n");
334  return 0;
335 }
336 
343 bool dlg_select_pattern(char *buf, size_t buflen)
344 {
346  struct Menu *menu = create_pattern_menu(dlg);
347 
348  struct MuttWindow *win_menu = menu->win;
349 
350  // NT_COLOR is handled by the SimpleDialog
353 
354  bool rc = false;
355  bool done = false;
356  while (!done)
357  {
358  switch (menu_loop(menu))
359  {
360  case OP_GENERIC_SELECT_ENTRY:
361  {
362  const int index = menu_get_index(menu);
363  struct PatternEntry *entry = ((struct PatternEntry *) menu->mdata) + index;
364  mutt_str_copy(buf, entry->tag, buflen);
365  rc = true;
366  done = true;
367  break;
368  }
369 
370  case OP_EXIT:
371  done = true;
372  break;
373  }
374  }
375 
376  simple_dialog_free(&dlg);
377  return rc;
378 }
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
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:872
bool dlg_select_pattern(char *buf, size_t buflen)
Show menu to select a Pattern.
Definition: dlgpattern.c:343
static struct Menu * create_pattern_menu(struct MuttWindow *dlg)
Create the Pattern Completion menu.
Definition: dlgpattern.c:177
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition: dlgpattern.c:97
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
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:780
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: dlgpattern.c:115
#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: dlgpattern.c:145
static void free_pattern_menu(struct Menu *menu, void **ptr)
Free the Pattern Completion menu - Implements Menu::mdata_free() -.
Definition: dlgpattern.c:158
static int pattern_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlgpattern.c:315
static int pattern_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: dlgpattern.c:291
Convenience wrapper for the gui headers.
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:126
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:163
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
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:40
GUI present the user with a selectable list.
#define MENU_REDRAW_FULL
Redraw everything.
Definition: lib.h:55
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:632
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:608
int menu_loop(struct Menu *menu)
Menu event loop.
Definition: menu.c:283
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, observer_t callback, 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:370
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:904
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:749
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:520
@ 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:53
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
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:41
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:42
@ EAT_DATE
Process a date (range)
Definition: private.h:40
@ EAT_QUERY
Process a query string.
Definition: private.h:43
@ EAT_REGEX
Process a regex.
Definition: private.h:39
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:219
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
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:67
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:74
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:103
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:168
void * mdata
Private data.
Definition: lib.h:153
int max
Number of entries in the menu.
Definition: lib.h:69
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
A line in the Pattern Completion menu.
Definition: dlgpattern.c:89
const char * desc
Description of pattern.
Definition: dlgpattern.c:93
const char * tag
Copied to buffer if selected.
Definition: dlgpattern.c:91
int num
Index number.
Definition: dlgpattern.c:90
const char * expr
Displayed in the menu.
Definition: dlgpattern.c:92
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