NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
dlgalias.c
Go to the documentation of this file.
1 
31 #include "config.h"
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "mutt/lib.h"
38 #include "address/lib.h"
39 #include "config/lib.h"
40 #include "core/lib.h"
41 #include "gui/lib.h"
42 #include "mutt.h"
43 #include "lib.h"
44 #include "pattern/lib.h"
45 #include "alias.h"
46 #include "format_flags.h"
47 #include "gui.h"
48 #include "keymap.h"
49 #include "mutt_globals.h"
50 #include "mutt_menu.h"
51 #include "muttlib.h"
52 #include "opcodes.h"
53 
55 static const struct Mapping AliasHelp[] = {
56  // clang-format off
57  { N_("Exit"), OP_EXIT },
58  { N_("Del"), OP_DELETE },
59  { N_("Undel"), OP_UNDELETE },
60  { N_("Sort"), OP_SORT },
61  { N_("Rev-Sort"), OP_SORT_REVERSE },
62  { N_("Select"), OP_GENERIC_SELECT_ENTRY },
63  { N_("Help"), OP_HELP },
64  { NULL, 0 },
65  // clang-format on
66 };
67 
80 static const char *alias_format_str(char *buf, size_t buflen, size_t col, int cols,
81  char op, const char *src, const char *prec,
82  const char *if_str, const char *else_str,
83  intptr_t data, MuttFormatFlags flags)
84 {
85  char fmt[128], addr[1024];
86  struct AliasView *av = (struct AliasView *) data;
87  struct Alias *alias = av->alias;
88 
89  switch (op)
90  {
91  case 'a':
92  mutt_format_s(buf, buflen, prec, alias->name);
93  break;
94  case 'c':
95  mutt_format_s(buf, buflen, prec, alias->comment);
96  break;
97  case 'f':
98  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
99  snprintf(buf, buflen, fmt, av->is_deleted ? "D" : " ");
100  break;
101  case 'n':
102  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
103  snprintf(buf, buflen, fmt, av->num + 1);
104  break;
105  case 'r':
106  addr[0] = '\0';
107  mutt_addrlist_write(&alias->addr, addr, sizeof(addr), true);
108  mutt_format_s(buf, buflen, prec, addr);
109  break;
110  case 't':
111  buf[0] = av->is_tagged ? '*' : ' ';
112  buf[1] = '\0';
113  break;
114  }
115 
116  return src;
117 }
118 
122 static void alias_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
123 {
124  const struct AliasMenuData *mdata = (struct AliasMenuData *) menu->mdata;
125  const struct AliasViewArray *ava = &((struct AliasMenuData *) menu->mdata)->ava;
126  const struct AliasView *av = ARRAY_GET(ava, line);
127 
128  const char *alias_format = cs_subset_string(mdata->sub, "alias_format");
129 
130  mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols, NONULL(alias_format),
132 }
133 
137 static int alias_tag(struct Menu *menu, int sel, int act)
138 {
139  const struct AliasViewArray *ava = &((struct AliasMenuData *) menu->mdata)->ava;
140  struct AliasView *av = ARRAY_GET(ava, sel);
141 
142  bool ot = av->is_tagged;
143 
144  av->is_tagged = ((act >= 0) ? act : !av->is_tagged);
145 
146  return av->is_tagged - ot;
147 }
148 
152 static int alias_alias_observer(struct NotifyCallback *nc)
153 {
154  if (!nc->event_data || !nc->global_data)
155  return -1;
156  if (nc->event_type != NT_ALIAS)
157  return 0;
158 
159  struct EventAlias *ea = nc->event_data;
160  struct Menu *menu = nc->global_data;
161  struct AliasMenuData *mdata = menu->mdata;
162  struct Alias *alias = ea->alias;
163 
164  if (nc->event_subtype == NT_ALIAS_NEW)
165  {
166  alias_array_alias_add(&mdata->ava, alias);
167 
168  if (alias_array_count_visible(&mdata->ava) != ARRAY_SIZE(&mdata->ava))
169  mutt_pattern_alias_func(MUTT_LIMIT, NULL, _("Aliases"), mdata, menu);
170  }
171  else if (nc->event_subtype == NT_ALIAS_DELETED)
172  {
173  alias_array_alias_delete(&mdata->ava, alias);
174 
175  int vcount = alias_array_count_visible(&mdata->ava);
176  if ((menu->current > (vcount - 1)) && (menu->current > 0))
177  menu->current--;
178  }
179 
180  alias_array_sort(&mdata->ava, mdata->sub);
181 
182  menu->max = alias_array_count_visible(&mdata->ava);
183  menu->redraw = REDRAW_FULL;
184 
185  return 0;
186 }
187 
194 static void dlg_select_alias(char *buf, size_t buflen, struct AliasMenuData *mdata)
195 {
196  if (ARRAY_EMPTY(&mdata->ava))
197  {
198  mutt_warning(_("You have no aliases"));
199  return;
200  }
201 
202  int t = -1;
203  bool done = false;
204 
205  struct Menu *menu = mutt_menu_new(MENU_ALIAS);
207  dlg->help_data = AliasHelp;
208  dlg->help_menu = MENU_ALIAS;
209 
211  menu->custom_search = true;
212  menu->tag = alias_tag;
213  menu->max = alias_array_count_visible(&mdata->ava);
214  menu->mdata = mdata;
215  menu->title = menu_create_alias_title(_("Aliases"), mdata->str);
216 
220 
222 
223  alias_array_sort(&mdata->ava, mdata->sub);
224 
225  struct AliasView *avp = NULL;
226  ARRAY_FOREACH(avp, &mdata->ava)
227  {
228  avp->num = ARRAY_FOREACH_IDX;
229  }
230 
231  while (!done)
232  {
233  int op = mutt_menu_loop(menu);
234  switch (op)
235  {
236  case OP_DELETE:
237  case OP_UNDELETE:
238  if (menu->tagprefix)
239  {
240  ARRAY_FOREACH(avp, &mdata->ava)
241  {
242  if (avp->is_tagged)
243  avp->is_deleted = (op == OP_DELETE);
244  }
245  menu->redraw |= REDRAW_INDEX;
246  }
247  else
248  {
249  ARRAY_GET(&mdata->ava, menu->current)->is_deleted = (op == OP_DELETE);
250  menu->redraw |= REDRAW_CURRENT;
251  if (C_Resolve && (menu->current < menu->max - 1))
252  {
253  menu->current++;
254  menu->redraw |= REDRAW_INDEX;
255  }
256  }
257  break;
258  case OP_SORT:
259  case OP_SORT_REVERSE:
260  {
261  int sort = cs_subset_sort(mdata->sub, "sort_alias");
262  bool resort = true;
263  bool reverse = (op == OP_SORT_REVERSE);
264 
265  switch (mutt_multi_choice(
266  reverse ?
267  /* L10N: The highlighted letters must match the "Sort" options */
268  _("Rev-Sort (a)lias, a(d)dress or (u)nsorted?") :
269  /* L10N: The highlighted letters must match the "Rev-Sort" options */
270  _("Sort (a)lias, a(d)dress or (u)nsorted?"),
271  /* L10N: These must match the highlighted letters from "Sort" and "Rev-Sort" */
272  _("adu")))
273  {
274  case -1: /* abort */
275  resort = false;
276  break;
277 
278  case 1: /* (a)lias */
279  sort = SORT_ALIAS;
280  break;
281 
282  case 2: /* a(d)dress */
283  sort = SORT_ADDRESS;
284  break;
285 
286  case 3: /* (u)nsorted */
287  sort = SORT_ORDER;
288  break;
289  }
290 
291  if (resort)
292  {
293  sort |= reverse ? SORT_REVERSE : 0;
294 
295  cs_subset_str_native_set(mdata->sub, "sort_alias", sort, NULL);
296  menu->redraw = REDRAW_FULL;
297  }
298 
299  break;
300  }
301  case OP_SEARCH_REVERSE:
302  case OP_SEARCH_NEXT:
303  case OP_SEARCH_OPPOSITE:
304  case OP_SEARCH:
305  menu->current = mutt_search_alias_command(menu, menu->current, op);
306  if (menu->current == -1)
307  menu->current = menu->oldcurrent;
308  else
309  menu->redraw |= REDRAW_MOTION;
310  break;
311 
312  case OP_MAIN_LIMIT:
313  {
314  int result = mutt_pattern_alias_func(MUTT_LIMIT, _("Limit to messages matching: "),
315  _("Aliases"), mdata, menu);
316  if (result == 0)
317  {
318  alias_array_sort(&mdata->ava, mdata->sub);
319  menu->redraw = REDRAW_FULL;
320  }
321 
322  break;
323  }
324 
325  case OP_GENERIC_SELECT_ENTRY:
326  t = menu->current;
327  if (t >= ARRAY_SIZE(&mdata->ava))
328  t = -1;
329  done = true;
330  break;
331  case OP_EXIT:
332  done = true;
333  break;
334  }
335  }
336 
337  ARRAY_FOREACH(avp, &mdata->ava)
338  {
339  if (avp->is_tagged)
340  {
341  mutt_addrlist_write(&avp->alias->addr, buf, buflen, true);
342  t = -1;
343  }
344  }
345 
346  if (t != -1)
347  {
348  mutt_addrlist_write(&ARRAY_GET(&mdata->ava, t)->alias->addr, buf, buflen, true);
349  }
350 
354 
355  mutt_menu_pop_current(menu);
356 
357  FREE(&menu->title);
358  mutt_menu_free(&menu);
359 
361 }
362 
375 int alias_complete(char *buf, size_t buflen, struct ConfigSubset *sub)
376 {
377  struct Alias *np = NULL;
378  char bestname[8192] = { 0 };
379 
380  struct AliasMenuData mdata = { NULL, NULL, ARRAY_HEAD_INITIALIZER, sub };
381  mdata.str = mutt_str_dup(buf);
382 
383  if (buf[0] != '\0')
384  {
385  TAILQ_FOREACH(np, &Aliases, entries)
386  {
387  if (np->name && mutt_strn_equal(np->name, buf, strlen(buf)))
388  {
389  if (bestname[0] == '\0') /* init */
390  {
391  mutt_str_copy(bestname, np->name,
392  MIN(mutt_str_len(np->name) + 1, sizeof(bestname)));
393  }
394  else
395  {
396  int i;
397  for (i = 0; np->name[i] && (np->name[i] == bestname[i]); i++)
398  ; // do nothing
399 
400  bestname[i] = '\0';
401  }
402  }
403  }
404 
405  if (bestname[0] != '\0')
406  {
407  /* fake the pattern for menu title */
408  char *mtitle = NULL;
409  mutt_str_asprintf(&mtitle, "~f ^%s", buf);
410  FREE(&mdata.str);
411  mdata.str = mtitle;
412 
413  if (!mutt_str_equal(bestname, buf))
414  {
415  /* we are adding something to the completion */
416  mutt_str_copy(buf, bestname, mutt_str_len(bestname) + 1);
417  FREE(&mdata.str);
418  return 1;
419  }
420 
421  /* build alias list and show it */
422  TAILQ_FOREACH(np, &Aliases, entries)
423  {
424  int aasize = alias_array_alias_add(&mdata.ava, np);
425 
426  struct AliasView *av = ARRAY_GET(&mdata.ava, aasize - 1);
427 
428  if (np->name && !mutt_strn_equal(np->name, buf, strlen(buf)))
429  {
430  av->is_visible = false;
431  }
432  }
433  }
434  }
435 
436  if (ARRAY_EMPTY(&mdata.ava))
437  {
438  TAILQ_FOREACH(np, &Aliases, entries)
439  {
440  alias_array_alias_add(&mdata.ava, np);
441  }
442 
443  mutt_pattern_alias_func(MUTT_LIMIT, NULL, _("Aliases"), &mdata, NULL);
444  }
445 
446  alias_array_sort(&mdata.ava, mdata.sub);
447 
448  bestname[0] = '\0';
449  dlg_select_alias(bestname, sizeof(bestname), &mdata);
450  if (bestname[0] != '\0')
451  mutt_str_copy(buf, bestname, buflen);
452 
453  struct AliasView *avp = NULL;
454  ARRAY_FOREACH(avp, &mdata.ava)
455  {
456  if (!avp->is_deleted)
457  continue;
458 
459  TAILQ_REMOVE(&Aliases, avp->alias, entries);
460  alias_free(&avp->alias);
461  }
462 
463  ARRAY_FREE(&mdata.ava);
464  FREE(&mdata.str);
465 
466  return 0;
467 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
Convenience wrapper for the gui headers.
A shortcut for an email address or addresses.
Definition: alias.h:33
#define mutt_warning(...)
Definition: logging.h:82
char * name
Short name.
Definition: alias.h:35
#define REDRAW_FULL
Redraw everything.
Definition: mutt_menu.h:45
#define IP
Definition: set.h:54
Manage keymappings.
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
#define NONULL(x)
Definition: string2.h:37
static void alias_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
Format a menu item for the alias list - Implements Menu::make_entry()
Definition: dlgalias.c:122
static void dlg_select_alias(char *buf, size_t buflen, struct AliasMenuData *mdata)
Display a menu of Aliases.
Definition: dlgalias.c:194
#define MIN(a, b)
Definition: memory.h:31
GUI selectable list of items.
Definition: mutt_menu.h:52
Data passed to a notification function.
Definition: observer.h:39
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:134
char * menu_create_alias_title(char *menu_name, char *limit)
Create a title string for the Menu.
Definition: gui.c:84
AliasMenuData - AliasView array wrapper with Pattern information.
Definition: gui.h:53
int oldcurrent
For driver use only.
Definition: mutt_menu.h:76
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:198
Messages in limited view.
Definition: mutt.h:105
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:206
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:105
int alias_array_count_visible(struct AliasViewArray *ava)
Count number of visible Aliases.
Definition: array.c:91
Alias has changed, NotifyAlias, EventAlias.
Definition: notify_type.h:44
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define _(a)
Definition: message.h:28
Sort by email address.
Definition: sort2.h:56
An alias-change event.
Definition: alias.h:59
A division of the screen.
Definition: mutt_window.h:115
GUI data wrapping an Alias.
Definition: gui.h:36
int alias_array_alias_add(struct AliasViewArray *ava, struct Alias *alias)
Add an Alias to the AliasViewArray.
Definition: array.c:45
int alias_color_observer(struct NotifyCallback *nc)
Listen for color configuration changes and refreshes the menu.
Definition: gui.c:66
Flags to control mutt_expando_format()
All user-callable functions.
Container for Accounts, Notifications.
Definition: neomutt.h:36
Representation of a single alias to an email address.
struct MuttWindow * dialog_create_simple_index(struct Menu *menu, enum WindowType type)
Create a simple index Dialog.
Definition: dialog.c:165
Convenience wrapper for the config headers.
void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
Sort and reindex an AliasViewArray.
Definition: sort.c:158
Email Address Handling.
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
Some miscellaneous functions.
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:292
Many unsorted constants and some structs.
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:50
const char * title
Title of this menu.
Definition: mutt_menu.h:54
Convenience wrapper for the core headers.
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:54
char * str
String representing the limit being used.
Definition: gui.h:55
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:169
#define REDRAW_MOTION
Redraw after moving the menu list.
Definition: mutt_menu.h:41
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
int alias_config_observer(struct NotifyCallback *nc)
Listen for sort_alias configuration changes and reorders menu items accordingly.
Definition: gui.c:44
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:834
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:219
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:57
int mutt_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question.
Definition: curs_lib.c:933
bool is_visible
Is visible?
Definition: gui.h:44
Colour has changed, ColorId, EventColor.
Definition: notify_type.h:35
struct AliasList Aliases
List of all the user&#39;s email aliases.
Definition: alias.c:55
A set of inherited config items.
Definition: subset.h:46
struct Alias * alias
Alias.
Definition: gui.h:45
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:120
void * mdata
Extra data for the current menu.
Definition: mutt_menu.h:55
static const struct Mapping AliasHelp[]
Help Bar for the Alias dialog (address book)
Definition: dlgalias.c:55
A new Alias has been created.
Definition: alias.h:51
bool tagprefix
Definition: mutt_menu.h:61
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:70
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:135
Match patterns to emails.
void dialog_destroy_simple_index(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: dialog.c:209
struct Alias * alias
Definition: alias.h:61
GUI present the user with a selectable list.
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:241
int alias_complete(char *buf, size_t buflen, struct ConfigSubset *sub)
alias completion routine
Definition: dlgalias.c:375
Sort by email alias.
Definition: sort2.h:55
char * comment
Free-form comment string.
Definition: alias.h:37
static const char * alias_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 alias list - Implements format_t.
Definition: dlgalias.c:80
static int alias_alias_observer(struct NotifyCallback *nc)
Listen for data changes affecting the Alias menu - Implements observer_t.
Definition: dlgalias.c:152
int(* tag)(struct Menu *menu, int sel, int act)
Tag some menu items.
Definition: mutt_menu.h:107
bool custom_search
The menu implements its own non-Menusearch()-compatible search, trickle OP_SEARCH*.
Definition: mutt_menu.h:79
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
void alias_free(struct Alias **ptr)
Free an Alias.
Definition: alias.c:619
Select an email address by its alias.
Definition: keymap.h:74
int max
Number of entries in the menu.
Definition: mutt_menu.h:57
void * event_data
Data from notify_send()
Definition: observer.h:44
bool mutt_strn_equal(const char *a, const char *b, size_t l)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:593
MuttRedrawFlags redraw
When to redraw the screen.
Definition: mutt_menu.h:58
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:37
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:631
An Alias is about to be deleted.
Definition: alias.h:53
bool is_tagged
Is it tagged?
Definition: gui.h:42
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: curs_lib.c:1244
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
#define REDRAW_INDEX
Redraw the index.
Definition: mutt_menu.h:40
struct ConfigSubset * sub
Config items.
Definition: gui.h:58
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:772
int alias_array_alias_delete(struct AliasViewArray *ava, struct Alias *alias)
Delete an Alias from the AliasViewArray.
Definition: array.c:69
#define FREE(x)
Definition: memory.h:40
Mapping between user-readable string and a constant.
Definition: mapping.h:31
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:207
struct AliasViewArray ava
Array of AliasView.
Definition: gui.h:57
Hundreds of global variables to back the user variables.
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1095
static int alias_tag(struct Menu *menu, int sel, int act)
Tag some aliases - Implements Menu::tag()
Definition: dlgalias.c:137
int current
Current entry.
Definition: mutt_menu.h:56
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
struct MuttWindow * win_index
Definition: mutt_menu.h:63
Convenience wrapper for the library headers.
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:81
int mutt_pattern_alias_func(int op, char *prompt, char *title, struct AliasMenuData *mdata, struct Menu *menu)
Perform some Pattern matching for Alias.
Definition: pattern.c:236
int num
Index number in list.
Definition: gui.h:38
#define N_(a)
Definition: message.h:32
int mutt_search_alias_command(struct Menu *menu, int cur, int op)
Perform a search.
Definition: pattern.c:631
bool is_deleted
Is it deleted?
Definition: gui.h:43
Shared code for the Alias and Query Dialogs.
void(* make_entry)(char *buf, size_t buflen, struct Menu *menu, int line)
Format a item for a menu.
Definition: mutt_menu.h:88
size_t mutt_addrlist_write(const struct AddressList *al, char *buf, size_t buflen, bool display)
Write an Address to a buffer.
Definition: address.c:1150
#define REDRAW_CURRENT
Redraw the current line of the menu.
Definition: mutt_menu.h:43
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:83
WHERE bool C_Resolve
Config: Move to the next email whenever a command modifies an email.
Definition: mutt_globals.h:155
Alias Dialog, dlg_select_alias()
Definition: mutt_window.h:74