NeoMutt  2019-12-07-60-g0cfa53
Teaching an old dog new tricks
DOXYGEN
addrbook.c File Reference

Address book handling aliases. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "mutt/mutt.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "addrbook.h"
#include "alias.h"
#include "format_flags.h"
#include "globals.h"
#include "keymap.h"
#include "mutt_menu.h"
#include "muttlib.h"
#include "opcodes.h"
+ Include dependency graph for addrbook.c:

Go to the source code of this file.

Macros

#define RSORT(num)   ((C_SortAlias & SORT_REVERSE) ? -num : num)
 

Functions

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, unsigned long data, MuttFormatFlags flags)
 Format a string for the alias list - Implements format_t. More...
 
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::menu_make_entry() More...
 
static int alias_tag (struct Menu *menu, int sel, int act)
 Tag some aliases - Implements Menu::menu_tag() More...
 
static int alias_sort_alias (const void *a, const void *b)
 Compare two Aliases. More...
 
static int alias_sort_address (const void *a, const void *b)
 Compare two Addresses. More...
 
int mutt_dlg_alias_observer (struct NotifyCallback *nc)
 Listen for config changes affecting the Alias menu - Implements observer_t() More...
 
void mutt_alias_menu (char *buf, size_t buflen, struct AliasList *aliases)
 Display a menu of Aliases. More...
 

Variables

char * C_AliasFormat
 Config: printf-like format string for the alias menu. More...
 
short C_SortAlias
 Config: Sort method for the alias menu. More...
 
static const struct Mapping AliasHelp []
 

Detailed Description

Address book handling aliases.

Authors
  • Michael R. Elkins
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file addrbook.c.

Macro Definition Documentation

◆ RSORT

#define RSORT (   num)    ((C_SortAlias & SORT_REVERSE) ? -num : num)

Definition at line 53 of file addrbook.c.

Function Documentation

◆ alias_format_str()

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,
unsigned long  data,
MuttFormatFlags  flags 
)
static

Format a string for the alias list - Implements format_t.

Expando Description
%a Alias name
%f Flags - currently, a 'd' for an alias marked for deletion
%n Index number
%r Address which alias expands to
%t Character which indicates if the alias is tagged for inclusion

Definition at line 72 of file addrbook.c.

76 {
77  char fmt[128], addr[128];
78  struct Alias *alias = (struct Alias *) data;
79 
80  switch (op)
81  {
82  case 'a':
83  mutt_format_s(buf, buflen, prec, alias->name);
84  break;
85  case 'f':
86  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
87  snprintf(buf, buflen, fmt, alias->del ? "D" : " ");
88  break;
89  case 'n':
90  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
91  snprintf(buf, buflen, fmt, alias->num + 1);
92  break;
93  case 'r':
94  addr[0] = '\0';
95  mutt_addrlist_write(&alias->addr, addr, sizeof(addr), true);
96  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
97  snprintf(buf, buflen, fmt, addr);
98  break;
99  case 't':
100  buf[0] = alias->tagged ? '*' : ' ';
101  buf[1] = '\0';
102  break;
103  }
104 
105  return src;
106 }
A shortcut for an email address.
Definition: alias.h:37
char * name
Short name.
Definition: alias.h:39
short num
Index number in list.
Definition: alias.h:43
bool del
Is it deleted?
Definition: alias.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
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:40
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:1138
bool tagged
Is it tagged?
Definition: alias.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_make_entry()

static void alias_make_entry ( char *  buf,
size_t  buflen,
struct Menu menu,
int  line 
)
static

Format a menu item for the alias list - Implements Menu::menu_make_entry()

Definition at line 111 of file addrbook.c.

112 {
113  mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols,
115  (unsigned long) ((struct Alias **) menu->data)[line],
117 }
A shortcut for an email address.
Definition: alias.h:37
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t *callback, unsigned long data, MuttFormatFlags flags)
Expand expandos (x) in a string.
Definition: muttlib.c:877
#define NONULL(x)
Definition: string2.h:37
#define MUTT_FORMAT_ARROWCURSOR
Reserve space for arrow_cursor.
Definition: format_flags.h:35
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, unsigned long data, MuttFormatFlags flags)
Format a string for the alias list - Implements format_t.
Definition: addrbook.c:72
char * C_AliasFormat
Config: printf-like format string for the alias menu.
Definition: addrbook.c:50
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:56
const char * line
Definition: common.c:36
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:91
void * data
Extra data for the current menu.
Definition: mutt_menu.h:86
struct MuttWindow * win_index
Definition: mutt_menu.h:95
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_tag()

static int alias_tag ( struct Menu menu,
int  sel,
int  act 
)
static

Tag some aliases - Implements Menu::menu_tag()

Definition at line 122 of file addrbook.c.

123 {
124  struct Alias *cur = ((struct Alias **) menu->data)[sel];
125  bool ot = cur->tagged;
126 
127  cur->tagged = ((act >= 0) ? act : !cur->tagged);
128 
129  return cur->tagged - ot;
130 }
A shortcut for an email address.
Definition: alias.h:37
void * data
Extra data for the current menu.
Definition: mutt_menu.h:86
bool tagged
Is it tagged?
Definition: alias.h:41
+ Here is the caller graph for this function:

◆ alias_sort_alias()

static int alias_sort_alias ( const void *  a,
const void *  b 
)
static

Compare two Aliases.

Parameters
aFirst Alias to compare
bSecond Alias to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 140 of file addrbook.c.

141 {
142  const struct Alias *pa = *(struct Alias const *const *) a;
143  const struct Alias *pb = *(struct Alias const *const *) b;
144  int r = mutt_str_strcasecmp(pa->name, pb->name);
145 
146  return RSORT(r);
147 }
A shortcut for an email address.
Definition: alias.h:37
char * name
Short name.
Definition: alias.h:39
#define RSORT(num)
Definition: addrbook.c:53
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_sort_address()

static int alias_sort_address ( const void *  a,
const void *  b 
)
static

Compare two Addresses.

Parameters
aFirst Address to compare
bSecond Address to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 157 of file addrbook.c.

158 {
159  const struct AddressList *pal = &(*(struct Alias const *const *) a)->addr;
160  const struct AddressList *pbl = &(*(struct Alias const *const *) b)->addr;
161  int r;
162 
163  if (pal == pbl)
164  r = 0;
165  else if (!pal)
166  r = -1;
167  else if (!pbl)
168  r = 1;
169  else
170  {
171  const struct Address *pa = TAILQ_FIRST(pal);
172  const struct Address *pb = TAILQ_FIRST(pbl);
173  if (pa->personal)
174  {
175  if (pb->personal)
176  r = mutt_str_strcasecmp(pa->personal, pb->personal);
177  else
178  r = 1;
179  }
180  else if (pb->personal)
181  r = -1;
182  else
183  r = mutt_str_strcasecmp(pa->mailbox, pb->mailbox);
184  }
185  return RSORT(r);
186 }
A shortcut for an email address.
Definition: alias.h:37
#define TAILQ_FIRST(head)
Definition: queue.h:716
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
#define RSORT(num)
Definition: addrbook.c:53
char * personal
Real name of address.
Definition: address.h:36
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_dlg_alias_observer()

int mutt_dlg_alias_observer ( struct NotifyCallback nc)

Listen for config changes affecting the Alias menu - Implements observer_t()

Definition at line 191 of file addrbook.c.

192 {
193  if (!nc->event_data || !nc->global_data)
194  return -1;
195  if (nc->event_type != NT_CONFIG)
196  return 0;
197 
198  struct EventConfig *ec = nc->event_data;
199  struct MuttWindow *dlg = nc->global_data;
200 
201  if (mutt_str_strcmp(ec->name, "status_on_top") != 0)
202  return 0;
203 
204  struct MuttWindow *win_first = TAILQ_FIRST(&dlg->children);
205 
206  if ((C_StatusOnTop && (win_first->type == WT_INDEX)) ||
207  (!C_StatusOnTop && (win_first->type != WT_INDEX)))
208  {
209  // Swap the Index and the IndexBar Windows
210  TAILQ_REMOVE(&dlg->children, win_first, entries);
211  TAILQ_INSERT_TAIL(&dlg->children, win_first, entries);
212  }
213 
214  mutt_window_reflow(dlg);
215  return 0;
216 }
WHERE bool C_StatusOnTop
Config: Display the status bar at the top.
Definition: globals.h:255
#define TAILQ_FIRST(head)
Definition: queue.h:716
A config-change event.
Definition: subset.h:68
A division of the screen.
Definition: mutt_window.h:86
An Index Window containing a selection list.
Definition: mutt_window.h:72
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:834
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:802
void * event_data
Data from notify_send()
Definition: observer.h:44
Config has changed.
Definition: notify_type.h:34
void mutt_window_reflow(struct MuttWindow *win)
Resize a Window and its children.
Definition: mutt_window.c:346
struct MuttWindowList children
Children Windows.
Definition: mutt_window.h:99
enum WindowType type
Window type, e.g. WT_SIDEBAR.
Definition: mutt_window.h:101
const char * name
Name of config item that changed.
Definition: subset.h:71
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_alias_menu()

void mutt_alias_menu ( char *  buf,
size_t  buflen,
struct AliasList *  aliases 
)

Display a menu of Aliases.

Parameters
bufBuffer for expanded aliases
buflenLength of buffer
aliasesAlias List

Definition at line 224 of file addrbook.c.

225 {
226  struct Alias *a = NULL, *last = NULL;
227  struct Menu *menu = NULL;
228  struct Alias **alias_table = NULL;
229  int t = -1;
230  int i;
231  bool done = false;
232  char helpstr[1024];
233 
234  int omax;
235 
236  if (TAILQ_EMPTY(aliases))
237  {
238  mutt_error(_("You have no aliases"));
239  return;
240  }
241 
242  struct MuttWindow *dlg =
245  dlg->type = WT_DIALOG;
246  struct MuttWindow *index =
249  index->type = WT_INDEX;
250  struct MuttWindow *ibar = mutt_window_new(
252  ibar->type = WT_INDEX_BAR;
253 
254  if (C_StatusOnTop)
255  {
256  mutt_window_add_child(dlg, ibar);
257  mutt_window_add_child(dlg, index);
258  }
259  else
260  {
261  mutt_window_add_child(dlg, index);
262  mutt_window_add_child(dlg, ibar);
263  }
264 
266  dialog_push(dlg);
267 
268  menu = mutt_menu_new(MENU_ALIAS);
269 
270  menu->pagelen = index->state.rows;
271  menu->win_index = index;
272  menu->win_ibar = ibar;
273 
275  menu->menu_tag = alias_tag;
276  menu->title = _("Aliases");
277  menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_ALIAS, AliasHelp);
279 
280 new_aliases:
281  omax = menu->max;
282 
283  /* count the number of aliases */
284  TAILQ_FOREACH_FROM(a, aliases, entries)
285  {
286  a->del = false;
287  a->tagged = false;
288  menu->max++;
289  }
290 
291  mutt_mem_realloc(&alias_table, menu->max * sizeof(struct Alias *));
292  menu->data = alias_table;
293  if (!alias_table)
294  goto mam_done;
295 
296  if (last)
297  a = TAILQ_NEXT(last, entries);
298 
299  i = omax;
300  TAILQ_FOREACH_FROM(a, aliases, entries)
301  {
302  alias_table[i] = a;
303  i++;
304  }
305 
306  if ((C_SortAlias & SORT_MASK) != SORT_ORDER)
307  {
308  qsort(alias_table, menu->max, sizeof(struct Alias *),
310  }
311 
312  for (i = 0; i < menu->max; i++)
313  alias_table[i]->num = i;
314 
315  last = TAILQ_LAST(aliases, AliasList);
316 
317  while (!done)
318  {
319  int op;
320  if (TAILQ_NEXT(last, entries))
321  {
322  menu->redraw |= REDRAW_FULL;
323  a = TAILQ_NEXT(last, entries);
324  goto new_aliases;
325  }
326  switch ((op = mutt_menu_loop(menu)))
327  {
328  case OP_DELETE:
329  case OP_UNDELETE:
330  if (menu->tagprefix)
331  {
332  for (i = 0; i < menu->max; i++)
333  if (alias_table[i]->tagged)
334  alias_table[i]->del = (op == OP_DELETE);
335  menu->redraw |= REDRAW_INDEX;
336  }
337  else
338  {
339  alias_table[menu->current]->del = (op == OP_DELETE);
340  menu->redraw |= REDRAW_CURRENT;
341  if (C_Resolve && (menu->current < menu->max - 1))
342  {
343  menu->current++;
344  menu->redraw |= REDRAW_INDEX;
345  }
346  }
347  break;
348  case OP_GENERIC_SELECT_ENTRY:
349  t = menu->current;
350  done = true;
351  break;
352  case OP_EXIT:
353  done = true;
354  break;
355  }
356  }
357 
358  for (i = 0; i < menu->max; i++)
359  {
360  if (alias_table[i]->tagged)
361  {
362  mutt_addrlist_write(&alias_table[i]->addr, buf, buflen, true);
363  t = -1;
364  }
365  }
366 
367  if (t != -1)
368  {
369  mutt_addrlist_write(&alias_table[t]->addr, buf, buflen, true);
370  }
371 
372  FREE(&alias_table);
373 
374 mam_done:
375  mutt_menu_pop_current(menu);
376  mutt_menu_free(&menu);
377  dialog_pop();
379  mutt_window_free(&dlg);
380 }
A shortcut for an email address.
Definition: alias.h:37
WHERE bool C_StatusOnTop
Config: Display the status bar at the top.
Definition: globals.h:255
#define REDRAW_FULL
Redraw everything.
Definition: mutt_menu.h:47
GUI selectable list of items.
Definition: mutt_menu.h:82
static int alias_tag(struct Menu *menu, int sel, int act)
Tag some aliases - Implements Menu::menu_tag()
Definition: addrbook.c:122
#define TAILQ_LAST(head, headname)
Definition: queue.h:812
Dialog (nested Windows) displayed to the user.
Definition: mutt_window.h:68
Window uses all available vertical space.
Definition: mutt_window.h:33
void dialog_pop(void)
Hide a Window from the user.
Definition: mutt_window.c:623
void dialog_push(struct MuttWindow *dlg)
Display a Window to the user.
Definition: mutt_window.c:603
#define _(a)
Definition: message.h:28
Sort by email address.
Definition: sort2.h:61
A division of the screen.
Definition: mutt_window.h:86
Container for Accounts, Notifications.
Definition: neomutt.h:35
void(* menu_make_entry)(char *buf, size_t buflen, struct Menu *menu, int line)
Format a item for a menu.
Definition: mutt_menu.h:120
bool notify_observer_add(struct Notify *notify, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:154
An Index Window containing a selection list.
Definition: mutt_window.h:72
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:75
bool del
Is it deleted?
Definition: alias.h:42
struct MuttWindow * win_ibar
Definition: mutt_menu.h:96
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:55
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::menu_make_entry()
Definition: addrbook.c:111
Window has a fixed size.
Definition: mutt_window.h:42
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:91
struct MuttWindow * mutt_window_new(enum MuttWindowOrientation orient, enum MuttWindowSize size, int rows, int cols)
Create a new Window.
Definition: mutt_window.c:57
bool notify_observer_remove(struct Notify *notify, observer_t callback, void *global_data)
Remove an observer from an object.
Definition: notify.c:186
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:47
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:55
bool tagprefix
Definition: mutt_menu.h:93
static int alias_sort_address(const void *a, const void *b)
Compare two Addresses.
Definition: addrbook.c:157
WHERE bool C_Resolve
Config: Move to the next email whenever a command modifies an email.
Definition: globals.h:246
#define TAILQ_FOREACH_FROM(var, head, field)
Definition: queue.h:723
int mutt_dlg_alias_observer(struct NotifyCallback *nc)
Listen for config changes affecting the Alias menu - Implements observer_t()
Definition: addrbook.c:191
static const struct Mapping AliasHelp[]
Definition: addrbook.c:55
short C_SortAlias
Config: Sort method for the alias menu.
Definition: addrbook.c:51
int pagelen
Number of entries per screen.
Definition: mutt_menu.h:92
struct Notify * notify
Notifications handler.
Definition: neomutt.h:37
Select an email address by its alias.
Definition: keymap.h:71
int max
Number of entries in the menu.
Definition: mutt_menu.h:88
void mutt_window_add_child(struct MuttWindow *parent, struct MuttWindow *child)
Add a child to Window.
Definition: mutt_window.c:471
int(* menu_tag)(struct Menu *menu, int sel, int act)
Tag some menu items.
Definition: mutt_menu.h:137
MuttRedrawFlags redraw
When to redraw the screen.
Definition: mutt_menu.h:89
char * title
Title of this menu.
Definition: mutt_menu.h:84
#define REDRAW_INDEX
Redraw the index.
Definition: mutt_menu.h:42
#define mutt_error(...)
Definition: logging.h:84
#define FREE(x)
Definition: memory.h:40
void * data
Extra data for the current menu.
Definition: mutt_menu.h:86
#define TAILQ_NEXT(elm, field)
Definition: queue.h:825
#define TAILQ_EMPTY(head)
Definition: queue.h:714
int current
Current entry.
Definition: mutt_menu.h:87
static int alias_sort_alias(const void *a, const void *b)
Compare two Aliases.
Definition: addrbook.c:140
struct MuttWindow * win_index
Definition: mutt_menu.h:95
char * mutt_compile_help(char *buf, size_t buflen, enum MenuType menu, const struct Mapping *items)
Create the text for the help menu.
Definition: help.c:115
Window wants as much space as possible.
Definition: mutt_window.h:43
Index Bar containing status info about the Index.
Definition: mutt_window.h:73
enum WindowType type
Window type, e.g. WT_SIDEBAR.
Definition: mutt_window.h:101
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:1138
char * help
Quickref for the current menu.
Definition: mutt_menu.h:85
#define REDRAW_CURRENT
Redraw the current line of the menu.
Definition: mutt_menu.h:45
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:85
bool tagged
Is it tagged?
Definition: alias.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_AliasFormat

char* C_AliasFormat

Config: printf-like format string for the alias menu.

Definition at line 50 of file addrbook.c.

◆ C_SortAlias

short C_SortAlias

Config: Sort method for the alias menu.

Definition at line 51 of file addrbook.c.

◆ AliasHelp

const struct Mapping AliasHelp[]
static
Initial value:
= {
{ N_("Exit"), OP_EXIT }, { N_("Del"), OP_DELETE },
{ N_("Undel"), OP_UNDELETE }, { N_("Select"), OP_GENERIC_SELECT_ENTRY },
{ N_("Help"), OP_HELP }, { NULL, 0 },
}
#define N_(a)
Definition: message.h:32

Definition at line 55 of file addrbook.c.