NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
dlgpattern.c
Go to the documentation of this file.
1 
30 #include "config.h"
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include "private.h"
36 #include "mutt/lib.h"
37 #include "config/lib.h"
38 #include "core/lib.h"
39 #include "gui/lib.h"
40 #include "mutt.h"
41 #include "lib.h"
42 #include "format_flags.h"
43 #include "keymap.h"
44 #include "mutt_globals.h"
45 #include "mutt_menu.h"
46 #include "muttlib.h"
47 #include "opcodes.h"
48 
53 {
54  int num;
55  const char *tag;
56  const char *expr;
57  const char *descr;
58 };
59 
61 static const struct Mapping PatternHelp[] = {
62  // clang-format off
63  { N_("Exit"), OP_EXIT },
64  { N_("Select"), OP_GENERIC_SELECT_ENTRY },
65  { N_("Help"), OP_HELP },
66  { NULL, 0 },
67  // clang-format on
68 };
69 
79 static const char *pattern_format_str(char *buf, size_t buflen, size_t col, int cols,
80  char op, const char *src, const char *prec,
81  const char *if_str, const char *else_str,
82  intptr_t data, MuttFormatFlags flags)
83 {
84  struct PatternEntry *entry = (struct PatternEntry *) data;
85 
86  switch (op)
87  {
88  case 'd':
89  mutt_format_s(buf, buflen, prec, NONULL(entry->descr));
90  break;
91  case 'e':
92  mutt_format_s(buf, buflen, prec, NONULL(entry->expr));
93  break;
94  case 'n':
95  {
96  char tmp[32];
97  snprintf(tmp, sizeof(tmp), "%%%sd", prec);
98  snprintf(buf, buflen, tmp, entry->num);
99  break;
100  }
101  }
102 
103  return src;
104 }
105 
109 static void make_pattern_entry(struct Menu *menu, char *buf, size_t buflen, int num)
110 {
111  struct PatternEntry *entry = &((struct PatternEntry *) menu->mdata)[num];
112 
113  mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols,
115  (intptr_t) entry, MUTT_FORMAT_ARROWCURSOR);
116 }
117 
122 static struct Menu *create_pattern_menu(void)
123 {
124  struct PatternEntry *entries = NULL;
125  int num_entries = 0, i = 0;
126  struct Buffer *entrybuf = NULL;
127 
128  while (Flags[num_entries].tag)
129  num_entries++;
130  /* Add three more hard-coded entries */
131  num_entries += 3;
132 
133  struct Menu *menu = mutt_menu_new(MENU_GENERIC);
134 
136 
137  // L10N: Pattern completion menu title
138  menu->title = _("Patterns");
139  menu->mdata = entries = mutt_mem_calloc(num_entries, sizeof(struct PatternEntry));
140  menu->max = num_entries;
141 
142  entrybuf = mutt_buffer_pool_get();
143  while (Flags[i].tag)
144  {
145  entries[i].num = i + 1;
146 
147  mutt_buffer_printf(entrybuf, "~%c", (char) Flags[i].tag);
148  entries[i].tag = mutt_str_dup(mutt_buffer_string(entrybuf));
149 
150  switch (Flags[i].eat_arg)
151  {
152  case EAT_REGEX:
153  /* L10N:
154  Pattern Completion Menu argument type: a regular expression
155  */
156  mutt_buffer_add_printf(entrybuf, " %s", _("EXPR"));
157  break;
158  case EAT_RANGE:
159  case EAT_MESSAGE_RANGE:
160  /* L10N:
161  Pattern Completion Menu argument type: a numeric range.
162  Used by ~m, ~n, ~X, ~z.
163  */
164  mutt_buffer_add_printf(entrybuf, " %s", _("RANGE"));
165  break;
166  case EAT_DATE:
167  /* L10N:
168  Pattern Completion Menu argument type: a date range
169  Used by ~d, ~r.
170  */
171  mutt_buffer_add_printf(entrybuf, " %s", _("DATERANGE"));
172  break;
173  case EAT_QUERY:
174  /* L10N:
175  Pattern Completion Menu argument type: a query
176  Used by ~I.
177  */
178  mutt_buffer_add_printf(entrybuf, " %s", _("QUERY"));
179  break;
180  default:
181  break;
182  }
183  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
184  entries[i].descr = mutt_str_dup(_(Flags[i].desc));
185 
186  i++;
187  }
188 
189  /* Add struct MuttThread patterns manually.
190  * Note we allocated 3 extra slots for these above. */
191 
192  /* L10N:
193  Pattern Completion Menu argument type: a nested pattern.
194  Used by ~(), ~<(), ~>().
195  */
196  const char *patternstr = _("PATTERN");
197 
198  entries[i].num = i + 1;
199  entries[i].tag = mutt_str_dup("~()");
200  mutt_buffer_printf(entrybuf, "~(%s)", patternstr);
201  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
202  // L10N: Pattern Completion Menu description for ~()
203  entries[i].descr = mutt_str_dup(
204  _("messages in threads containing messages matching PATTERN"));
205  i++;
206 
207  entries[i].num = i + 1;
208  entries[i].tag = mutt_str_dup("~<()");
209  mutt_buffer_printf(entrybuf, "~<(%s)", patternstr);
210  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
211  // L10N: Pattern Completion Menu description for ~<()
212  entries[i].descr =
213  mutt_str_dup(_("messages whose immediate parent matches PATTERN"));
214  i++;
215 
216  entries[i].num = i + 1;
217  entries[i].tag = mutt_str_dup("~>()");
218  mutt_buffer_printf(entrybuf, "~>(%s)", patternstr);
219  entries[i].expr = mutt_str_dup(mutt_buffer_string(entrybuf));
220  // L10N: Pattern Completion Menu description for ~>()
221  entries[i].descr =
222  mutt_str_dup(_("messages having an immediate child matching PATTERN"));
223 
225 
226  mutt_buffer_pool_release(&entrybuf);
227 
228  return menu;
229 }
230 
235 static void free_pattern_menu(struct Menu **ptr)
236 {
237  if (!ptr || !*ptr)
238  return;
239 
240  struct Menu *menu = *ptr;
241  mutt_menu_pop_current(menu);
242 
243  struct PatternEntry *entries = (struct PatternEntry *) menu->mdata;
244  while (menu->max)
245  {
246  menu->max--;
247  FREE(&entries[menu->max].tag);
248  FREE(&entries[menu->max].expr);
249  FREE(&entries[menu->max].descr);
250  }
251  FREE(&menu->mdata);
252 
253  mutt_menu_free(ptr);
254 }
255 
262 bool dlg_select_pattern(char *buf, size_t buflen)
263 {
264  struct Menu *menu = create_pattern_menu();
266  dlg->help_data = PatternHelp;
267  dlg->help_menu = MENU_GENERIC;
268 
269  bool rc = false;
270  bool done = false;
271  while (!done)
272  {
273  switch (mutt_menu_loop(menu))
274  {
275  case OP_GENERIC_SELECT_ENTRY:
276  {
277  struct PatternEntry *entry = (struct PatternEntry *) menu->mdata + menu->current;
278  mutt_str_copy(buf, entry->tag, buflen);
279  rc = true;
280  done = true;
281  break;
282  }
283 
284  case OP_EXIT:
285  done = true;
286  break;
287  }
288  }
289 
290  free_pattern_menu(&menu);
292  return rc;
293 }
PatternEntry::expr
const char * expr
Displayed in the menu.
Definition: dlgpattern.c:56
MENU_GENERIC
@ MENU_GENERIC
Generic selection list.
Definition: keymap.h:79
create_pattern_menu
static struct Menu * create_pattern_menu(void)
Create the Pattern Completion menu.
Definition: dlgpattern.c:122
dialog_create_simple_index
struct MuttWindow * dialog_create_simple_index(struct Menu *menu, enum WindowType type)
Create a simple index Dialog.
Definition: dialog.c:165
EAT_DATE
@ EAT_DATE
Process a date (range)
Definition: private.h:43
mutt_mem_calloc
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
_
#define _(a)
Definition: message.h:28
NONULL
#define NONULL(x)
Definition: string2.h:37
Buffer
String manipulation buffer.
Definition: buffer.h:33
PatternHelp
static const struct Mapping PatternHelp[]
Help Bar for the Pattern selection dialog.
Definition: dlgpattern.c:61
format_flags.h
MUTT_FORMAT_ARROWCURSOR
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
mutt_buffer_add_printf
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
MuttWindow
A division of the screen.
Definition: mutt_window.h:115
mutt_str_dup
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
mutt_globals.h
FREE
#define FREE(x)
Definition: memory.h:40
PatternEntry::tag
const char * tag
Copied to buffer if selected.
Definition: dlgpattern.c:55
mutt_menu_new
struct Menu * mutt_menu_new(enum MenuType type)
Create a new Menu.
Definition: menu.c:956
MuttWindow::help_menu
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:134
private.h
Menu::make_entry
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Format a item for a menu.
Definition: mutt_menu.h:88
mutt_buffer_pool_release
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
EAT_RANGE
@ EAT_RANGE
Process a number (range)
Definition: private.h:44
Mapping
Mapping between user-readable string and a constant.
Definition: mapping.h:31
lib.h
mutt_menu_push_current
void mutt_menu_push_current(struct Menu *menu)
Add a new Menu to the stack.
Definition: menu.c:1015
mutt_buffer_pool_get
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
keymap.h
lib.h
muttlib.h
mutt_menu.h
lib.h
PatternEntry::descr
const char * descr
Description of pattern.
Definition: dlgpattern.c:57
EAT_REGEX
@ EAT_REGEX
Process a regex.
Definition: private.h:42
Menu::tag
int(* tag)(struct Menu *menu, int sel, int act)
Tag some menu items.
Definition: mutt_menu.h:107
Menu::title
const char * title
Title of this menu.
Definition: mutt_menu.h:54
PatternEntry::num
int num
Index number.
Definition: dlgpattern.c:54
mutt_buffer_string
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
MuttWindow::help_data
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:135
mutt_format_s
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: curs_lib.c:1248
WT_DLG_PATTERN
@ WT_DLG_PATTERN
Pattern Dialog, create_pattern_menu()
Definition: mutt_window.h:84
mutt_menu_loop
int mutt_menu_loop(struct Menu *menu)
Menu event loop.
Definition: menu.c:1309
MuttWindow::state
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:120
mutt_menu_pop_current
void mutt_menu_pop_current(struct Menu *menu)
Remove a Menu from the stack.
Definition: menu.c:1027
pattern_format_str
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:79
Flags
const struct PatternFlags Flags[]
Lookup table for all patterns.
Definition: flags.c:41
free_pattern_menu
static void free_pattern_menu(struct Menu **ptr)
Free the Pattern Completion menu.
Definition: dlgpattern.c:235
Menu::mdata
void * mdata
Extra data for the current menu.
Definition: mutt_menu.h:55
mutt_expando_format
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:774
Menu::max
int max
Number of entries in the menu.
Definition: mutt_menu.h:57
PatternEntry
A line in the Pattern Completion menu.
Definition: dlgpattern.c:52
mutt.h
EAT_MESSAGE_RANGE
@ EAT_MESSAGE_RANGE
Process a message number (range)
Definition: private.h:45
EAT_QUERY
@ EAT_QUERY
Process a query string.
Definition: private.h:46
C_PatternFormat
char * C_PatternFormat
Config: printf-like format string for the pattern completion menu.
Definition: config.c:37
WindowState::cols
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:57
dialog_destroy_simple_index
void dialog_destroy_simple_index(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: dialog.c:209
Menu::current
int current
Current entry.
Definition: mutt_menu.h:56
opcodes.h
dlg_select_pattern
bool dlg_select_pattern(char *buf, size_t buflen)
Show menu to select a Pattern.
Definition: dlgpattern.c:262
lib.h
make_pattern_entry
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:109
mutt_buffer_printf
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Menu::win_index
struct MuttWindow * win_index
Definition: mutt_menu.h:63
Menu
GUI selectable list of items.
Definition: mutt_menu.h:52
N_
#define N_(a)
Definition: message.h:32
MuttFormatFlags
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
mutt_menu_free
void mutt_menu_free(struct Menu **ptr)
Destroy a menu.
Definition: menu.c:972
mutt_str_copy
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:716