NeoMutt  2018-07-16 +952-a2da0a
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 "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "alias.h"
#include "curs_lib.h"
#include "format_flags.h"
#include "globals.h"
#include "keymap.h"
#include "menu.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "opcodes.h"
#include "sort.h"
+ Include dependency graph for addrbook.c:

Go to the source code of this file.

Macros

#define RSORT(x)   ((SortAlias & SORT_REVERSE) ? -x : x)
 

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, enum FormatFlag 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...
 
void mutt_alias_menu (char *buf, size_t buflen, struct AliasList *aliases)
 Display a menu of Aliases. More...
 

Variables

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

Detailed Description

Address book handling aliases.

Authors
  • Michael R. Elkins

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

#define RSORT (   x)    ((SortAlias & SORT_REVERSE) ? -x : x)

Definition at line 52 of file addrbook.c.

Function Documentation

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,
enum FormatFlag  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 71 of file addrbook.c.

75 {
76  char fmt[SHORT_STRING], addr[SHORT_STRING];
77  struct Alias *alias = (struct Alias *) data;
78 
79  switch (op)
80  {
81  case 'a':
82  mutt_format_s(buf, buflen, prec, alias->name);
83  break;
84  case 'f':
85  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
86  snprintf(buf, buflen, fmt, alias->del ? "D" : " ");
87  break;
88  case 'n':
89  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
90  snprintf(buf, buflen, fmt, alias->num + 1);
91  break;
92  case 'r':
93  addr[0] = '\0';
94  mutt_addr_write(addr, sizeof(addr), alias->addr, true);
95  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
96  snprintf(buf, buflen, fmt, addr);
97  break;
98  case 't':
99  buf[0] = alias->tagged ? '*' : ' ';
100  buf[1] = '\0';
101  break;
102  }
103 
104  return src;
105 }
A shortcut for an email address.
Definition: alias.h:36
char * name
Definition: alias.h:38
#define SHORT_STRING
Definition: string2.h:34
short num
Definition: alias.h:42
size_t mutt_addr_write(char *buf, size_t buflen, struct Address *addr, bool display)
Write an Address to a buffer.
Definition: address.c:1146
bool del
Definition: alias.h:41
void mutt_format_s(char *buf, size_t buflen, const char *prec, const char *s)
Format a simple string.
Definition: curs_lib.c:1061
struct Address * addr
Definition: alias.h:39
bool tagged
Definition: alias.h:40

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 110 of file addrbook.c.

111 {
113  (unsigned long) ((struct Alias **) menu->data)[line],
115 }
A shortcut for an email address.
Definition: alias.h:36
#define NONULL(x)
Definition: string2.h:39
const char * line
Definition: common.c:35
struct MuttWindow * MuttIndexWindow
Index Window.
Definition: mutt_window.c:39
reserve space for arrow_cursor
Definition: format_flags.h:38
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, enum FormatFlag flags)
Format a string for the alias list - Implements format_t.
Definition: addrbook.c:71
void * data
extra data for the current menu
Definition: menu.h:60
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t *callback, unsigned long data, enum FormatFlag flags)
Expand expandos (x) in a string.
Definition: muttlib.c:816
char * AliasFormat
Config: printf-like format string for the alias menu.
Definition: addrbook.c:49

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Tag some aliases - Implements Menu::menu_tag()

Definition at line 120 of file addrbook.c.

121 {
122  struct Alias *cur = ((struct Alias **) menu->data)[sel];
123  bool ot = cur->tagged;
124 
125  cur->tagged = ((act >= 0) ? act : !cur->tagged);
126 
127  return cur->tagged - ot;
128 }
A shortcut for an email address.
Definition: alias.h:36
void * data
extra data for the current menu
Definition: menu.h:60
bool tagged
Definition: alias.h:40

+ Here is the caller graph for this function:

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 138 of file addrbook.c.

139 {
140  struct Alias *pa = *(struct Alias **) a;
141  struct Alias *pb = *(struct Alias **) b;
142  int r = mutt_str_strcasecmp(pa->name, pb->name);
143 
144  return RSORT(r);
145 }
A shortcut for an email address.
Definition: alias.h:36
char * name
Definition: alias.h:38
#define RSORT(x)
Definition: addrbook.c:52
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:625

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 155 of file addrbook.c.

156 {
157  struct Address *pa = (*(struct Alias **) a)->addr;
158  struct Address *pb = (*(struct Alias **) b)->addr;
159  int r;
160 
161  if (pa == pb)
162  r = 0;
163  else if (!pa)
164  r = -1;
165  else if (!pb)
166  r = 1;
167  else if (pa->personal)
168  {
169  if (pb->personal)
170  r = mutt_str_strcasecmp(pa->personal, pb->personal);
171  else
172  r = 1;
173  }
174  else if (pb->personal)
175  r = -1;
176  else
177  r = mutt_str_strcasecmp(pa->mailbox, pb->mailbox);
178  return RSORT(r);
179 }
A shortcut for an email address.
Definition: alias.h:36
An email address.
Definition: address.h:32
char * mailbox
mailbox and host address
Definition: address.h:35
#define RSORT(x)
Definition: addrbook.c:52
char * personal
real name of address
Definition: address.h:34
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:625

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 187 of file addrbook.c.

188 {
189  struct Alias *a = NULL, *last = NULL;
190  struct Menu *menu = NULL;
191  struct Alias **AliasTable = NULL;
192  int t = -1;
193  int i;
194  bool done = false;
195  char helpstr[LONG_STRING];
196 
197  int omax;
198 
199  if (TAILQ_EMPTY(aliases))
200  {
201  mutt_error(_("You have no aliases"));
202  return;
203  }
204 
205  menu = mutt_menu_new(MENU_ALIAS);
207  menu->menu_tag = alias_tag;
208  menu->title = _("Aliases");
209  menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_ALIAS, AliasHelp);
211 
212 new_aliases:
213  omax = menu->max;
214 
215  /* count the number of aliases */
216  TAILQ_FOREACH_FROM(a, aliases, entries)
217  {
218  a->del = false;
219  a->tagged = false;
220  menu->max++;
221  }
222 
223  mutt_mem_realloc(&AliasTable, menu->max * sizeof(struct Alias *));
224  menu->data = AliasTable;
225  if (!AliasTable)
226  return;
227 
228  if (last)
229  a = TAILQ_NEXT(last, entries);
230 
231  i = omax;
232  TAILQ_FOREACH_FROM(a, aliases, entries)
233  {
234  AliasTable[i] = a;
235  i++;
236  }
237 
238  if ((SortAlias & SORT_MASK) != SORT_ORDER)
239  {
240  qsort(AliasTable, menu->max, sizeof(struct Alias *),
242  }
243 
244  for (i = 0; i < menu->max; i++)
245  AliasTable[i]->num = i;
246 
247  last = TAILQ_LAST(aliases, AliasList);
248 
249  while (!done)
250  {
251  int op;
252  if (TAILQ_NEXT(last, entries))
253  {
254  menu->redraw |= REDRAW_FULL;
255  a = TAILQ_NEXT(last, entries);
256  goto new_aliases;
257  }
258  switch ((op = mutt_menu_loop(menu)))
259  {
260  case OP_DELETE:
261  case OP_UNDELETE:
262  if (menu->tagprefix)
263  {
264  for (i = 0; i < menu->max; i++)
265  if (AliasTable[i]->tagged)
266  AliasTable[i]->del = (op == OP_DELETE);
267  menu->redraw |= REDRAW_INDEX;
268  }
269  else
270  {
271  AliasTable[menu->current]->del = (op == OP_DELETE);
272  menu->redraw |= REDRAW_CURRENT;
273  if (Resolve && menu->current < menu->max - 1)
274  {
275  menu->current++;
276  menu->redraw |= REDRAW_INDEX;
277  }
278  }
279  break;
280  case OP_GENERIC_SELECT_ENTRY:
281  t = menu->current;
282  done = true;
283  break;
284  case OP_EXIT:
285  done = true;
286  break;
287  }
288  }
289 
290  for (i = 0; i < menu->max; i++)
291  {
292  if (AliasTable[i]->tagged)
293  {
294  mutt_addr_write(buf, buflen, AliasTable[i]->addr, true);
295  t = -1;
296  }
297  }
298 
299  if (t != -1)
300  {
301  mutt_addr_write(buf, buflen, AliasTable[t]->addr, true);
302  }
303 
304  mutt_menu_pop_current(menu);
305  mutt_menu_destroy(&menu);
306  FREE(&AliasTable);
307 }
A shortcut for an email address.
Definition: alias.h:36
int redraw
when to redraw the screen
Definition: menu.h:63
GUI selectable list of items.
Definition: menu.h:56
static int alias_tag(struct Menu *menu, int sel, int act)
Tag some aliases - Implements Menu::menu_tag()
Definition: addrbook.c:120
#define TAILQ_LAST(head, headname)
Definition: queue.h:813
short num
Definition: alias.h:42
#define _(a)
Definition: message.h:28
short SortAlias
Config: Sort method for the alias menu.
Definition: addrbook.c:50
int menu
menu definition for keymap entries.
Definition: menu.h:64
#define LONG_STRING
Definition: string2.h:36
Select an email address by its alias.
Definition: keymap.h:67
void(* menu_make_entry)(char *buf, size_t buflen, struct Menu *menu, int line)
Format a item for a menu.
Definition: menu.h:96
char * mutt_compile_help(char *buf, size_t buflen, int menu, const struct Mapping *items)
Create the text for the help menu.
Definition: help.c:115
size_t mutt_addr_write(char *buf, size_t buflen, struct Address *addr, bool display)
Write an Address to a buffer.
Definition: address.c:1146
bool del
Definition: alias.h:41
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:110
WHERE bool Resolve
Config: Move to the next email whenever a command modifies an email.
Definition: globals.h:248
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:124
bool tagprefix
Definition: menu.h:67
static int alias_sort_address(const void *a, const void *b)
Compare two Addresses.
Definition: addrbook.c:155
#define TAILQ_FOREACH_FROM(var, head, field)
Definition: queue.h:724
static const struct Mapping AliasHelp[]
Definition: addrbook.c:54
int max
the number of entries in the menu
Definition: menu.h:62
int(* menu_tag)(struct Menu *menu, int sel, int act)
Tag some menu items.
Definition: menu.h:113
char * title
the title of this menu
Definition: menu.h:58
#define mutt_error(...)
Definition: logging.h:88
#define FREE(x)
Definition: memory.h:46
void * data
extra data for the current menu
Definition: menu.h:60
struct Address * addr
Definition: alias.h:39
#define TAILQ_NEXT(elm, field)
Definition: queue.h:816
#define TAILQ_EMPTY(head)
Definition: queue.h:715
#define SORT_ADDRESS
Sort by email address.
Definition: sort.h:56
int current
current entry
Definition: menu.h:61
static int alias_sort_alias(const void *a, const void *b)
Compare two Aliases.
Definition: addrbook.c:138
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:77
char * help
quickref for the current menu
Definition: menu.h:59
bool tagged
Definition: alias.h:40
#define SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort.h:50

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

char* AliasFormat

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

Definition at line 49 of file addrbook.c.

short SortAlias

Config: Sort method for the alias menu.

Definition at line 50 of file addrbook.c.

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 54 of file addrbook.c.