~ubuntu-branches/ubuntu/lucid/mutt/lucid-updates

1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
1
/*
2
 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
3
 * 
4
 *     This program is free software; you can redistribute it and/or modify
5
 *     it under the terms of the GNU General Public License as published by
6
 *     the Free Software Foundation; either version 2 of the License, or
7
 *     (at your option) any later version.
8
 * 
9
 *     This program is distributed in the hope that it will be useful,
10
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *     GNU General Public License for more details.
13
 * 
14
 *     You should have received a copy of the GNU General Public License
15
 *     along with this program; if not, write to the Free Software
16
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
 */ 
18
19
#if HAVE_CONFIG_H
20
# include "config.h"
21
#endif
22
23
#include "mutt.h"
24
#include "mutt_menu.h"
25
#include "mutt_curses.h"
26
#include "keymap.h"
27
#include "mapping.h"
28
#include "mutt_crypt.h"
29
30
#include <stdlib.h>
31
#include <string.h>
32
#include <ctype.h>
33
34
#include "functions.h"
35
36
struct mapping_t Menus[] = {
37
 { "alias",	MENU_ALIAS },
38
 { "attach",	MENU_ATTACH },
39
 { "browser",	MENU_FOLDER },
40
 { "compose",	MENU_COMPOSE },
41
 { "editor",	MENU_EDITOR },
42
 { "index",	MENU_MAIN },
43
 { "pager",	MENU_PAGER },
44
 { "postpone",	MENU_POST },
45
 { "pgp",	MENU_PGP },
46
 { "smime",	MENU_SMIME },
47
#ifdef HAVE_GPGME
48
 { "key_select_pgp",	MENU_KEY_SELECT_PGP },
49
 { "key_select_smime",	MENU_KEY_SELECT_SMIME },
50
#endif
51
52
#ifdef MIXMASTER
53
  { "mix", 	MENU_MIX },
54
#endif
55
  
56
57
 { "query",	MENU_QUERY },
58
 { "generic",	MENU_GENERIC },
59
 { NULL,	0 }
60
};
61
62
#define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
63
64
static struct mapping_t KeyNames[] = {
65
  { "<PageUp>",	KEY_PPAGE },
66
  { "<PageDown>",	KEY_NPAGE },
67
  { "<Up>",	KEY_UP },
68
  { "<Down>",	KEY_DOWN },
69
  { "<Right>",	KEY_RIGHT },
70
  { "<Left>",	KEY_LEFT },
71
  { "<Delete>",	KEY_DC },
72
  { "<BackSpace>",KEY_BACKSPACE },
73
  { "<Insert>",	KEY_IC },
74
  { "<Home>",	KEY_HOME },
75
  { "<End>",	KEY_END },
76
#ifdef KEY_ENTER
77
  { "<Enter>",	KEY_ENTER },
78
#endif
79
  { "<Return>",	M_ENTER_C },
80
  { "<Esc>",	'\033' },
81
  { "<Tab>",	'\t' },
82
  { "<Space>",	' ' },
83
#ifdef KEY_BTAB
84
  { "<BackTab>", KEY_BTAB },
85
#endif
86
#ifdef KEY_NEXT
87
  { "<Next>",    KEY_NEXT },
88
#endif  
89
  { NULL,	0 }
90
};
91
92
/* contains the last key the user pressed */
93
int LastKey;
94
95
struct keymap_t *Keymaps[MENU_MAX];
96
97
static struct keymap_t *allocKeys (int len, keycode_t *keys)
98
{
99
  struct keymap_t *p;
100
101
  p = safe_calloc (1, sizeof (struct keymap_t));
102
  p->len = len;
103
  p->keys = safe_malloc (len * sizeof (keycode_t));
104
  memcpy (p->keys, keys, len * sizeof (keycode_t));
105
  return (p);
106
}
107
108
static int parse_fkey(char *s)
109
{
110
  char *t;
111
  int n = 0;
112
113
  if(s[0] != '<' || ascii_tolower(s[1]) != 'f')
114
    return -1;
115
116
  for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
117
  {
118
    n *= 10;
119
    n += *t - '0';
120
  }
121
122
  if(*t != '>')
123
    return -1;
124
  else
125
    return n;
126
}
127
128
/*
129
 * This function parses the string <NNN> and uses the octal value as the key
130
 * to bind.
131
 */
132
static int parse_keycode (const char *s)
133
{
134
  if (isdigit ((unsigned char) s[1]) &&
135
      isdigit ((unsigned char) s[2]) &&
136
      isdigit ((unsigned char) s[3]) &&
137
      s[4] == '>')
138
  {
139
    return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
140
  }
141
  return -1;
142
}
143
144
static int parsekeys (char *str, keycode_t *d, int max)
145
{
146
  int n, len = max;
147
  char buff[SHORT_STRING];
148
  char c;
149
  char *s, *t;
150
151
  strfcpy(buff, str, sizeof(buff));
152
  s = buff;
153
  
154
  while (*s && len)
155
  {
156
    *d = '\0';
157
    if(*s == '<' && (t = strchr(s, '>')))
158
    {
159
      t++; c = *t; *t = '\0';
160
      
161
      if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
162
      {
163
	s = t;
164
	*d = n;
165
      }
166
      else if ((n = parse_fkey(s)) > 0)
167
      {
168
	s = t;
169
	*d = KEY_F (n);
170
      }
171
      else if ((n = parse_keycode(s)) > 0)
172
      {
173
	s = t;
174
	*d = n;
175
      }
176
      
177
      *t = c;
178
    }
179
180
    if(!*d)
181
    {
182
      *d = (unsigned char)*s;
183
      s++;
184
    }
185
    d++;
186
    len--;
187
  }
188
189
  return (max - len);
190
}
191
192
/* insert a key sequence into the specified map.  the map is sorted by ASCII
193
 * value (lowest to highest)
194
 */
195
void km_bind (char *s, int menu, int op, char *macro, char *descr)
196
{
197
  struct keymap_t *map, *tmp, *last = NULL, *next;
198
  keycode_t buf[MAX_SEQ];
199
  int len, pos = 0, lastpos = 0;
200
201
  len = parsekeys (s, buf, MAX_SEQ);
202
203
  map = allocKeys (len, buf);
204
  map->op = op;
205
  map->macro = safe_strdup (macro);
206
  map->descr = safe_strdup (descr);
207
208
  tmp = Keymaps[menu];
209
210
  while (tmp)
211
  {
212
    if (pos >= len || pos >= tmp->len)
213
    {
214
      /* map and tmp match, but have different lengths, so overwrite */
215
      do
216
      {
217
	len = tmp->eq;
218
	next = tmp->next;
219
	FREE (&tmp->macro);
220
	FREE (&tmp->keys);
221
	FREE (&tmp->descr);
222
	FREE (&tmp);
223
	tmp = next;
224
      }
225
      while (tmp && len >= pos);
226
      map->eq = len;
227
      break;
228
    }
229
    else if (buf[pos] == tmp->keys[pos])
230
      pos++;
231
    else if (buf[pos] < tmp->keys[pos])
232
    {
233
      /* found location to insert between last and tmp */
234
      map->eq = pos;
235
      break;
236
    }
237
    else /* buf[pos] > tmp->keys[pos] */
238
    {
239
      last = tmp;
240
      lastpos = pos;
241
      if (pos > tmp->eq)
242
	pos = tmp->eq;
243
      tmp = tmp->next;
244
    }
245
  }
246
247
  map->next = tmp;
248
  if (last)
249
  {
250
    last->next = map;
251
    last->eq = lastpos;
252
  }
253
  else
254
    Keymaps[menu] = map;
255
}
256
257
void km_bindkey (char *s, int menu, int op)
258
{
259
  km_bind (s, menu, op, NULL, NULL);
260
}
261
262
static int get_op (struct binding_t *bindings, const char *start, size_t len)
263
{
264
  int i;
265
266
  for (i = 0; bindings[i].name; i++)
267
  {
268
    if (!ascii_strncasecmp (start, bindings[i].name, len) &&   
269
	mutt_strlen (bindings[i].name) == len)
270
      return bindings[i].op;
271
  }
272
273
  return OP_NULL;
274
}
275
276
static char *get_func (struct binding_t *bindings, int op)
277
{
278
  int i;
279
280
  for (i = 0; bindings[i].name; i++)
281
  {
282
    if (bindings[i].op == op)
283
      return bindings[i].name;
284
  }
285
286
  return NULL;
287
}
288
289
static void push_string (char *s)
290
{
291
  char *pp, *p = s + mutt_strlen (s) - 1;
292
  size_t l;
293
  int i, op = OP_NULL;
294
295
  while (p >= s)
296
  {
297
    /* if we see something like "<PageUp>", look to see if it is a real
298
       function name and return the corresponding value */
299
    if (*p == '>')
300
    {
301
      for (pp = p - 1; pp >= s && *pp != '<'; pp--)
302
	;
303
      if (pp >= s)
304
      {
305
	if ((i = parse_fkey (pp)) > 0)
306
	{
307
	  mutt_ungetch (KEY_F (i), 0);
308
	  p = pp - 1;
309
	  continue;
310
	}
311
312
	l = p - pp + 1;
313
	for (i = 0; KeyNames[i].name; i++)
314
	{
315
	  if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
316
	    break;
317
	}
318
	if (KeyNames[i].name)
319
	{
320
	  /* found a match */
321
	  mutt_ungetch (KeyNames[i].value, 0);
322
	  p = pp - 1;
323
	  continue;
324
	}
325
326
	/* See if it is a valid command
327
	 * skip the '<' and the '>' when comparing */
328
	for (i = 0; Menus[i].name; i++)
329
	{
330
	  struct binding_t *binding = km_get_table (Menus[i].value);
331
	  if (binding)
332
	  {
333
	    op = get_op (binding, pp + 1, l - 2);
334
	    if (op != OP_NULL)
335
	      break;
336
	  }
337
	}
338
339
	if (op != OP_NULL)
340
	{
341
	  mutt_ungetch (0, op);
342
	  p = pp - 1;
343
	  continue;
344
	}
345
      }
346
    }
347
    mutt_ungetch ((unsigned char)*p--, 0);	/* independent 8 bits chars */
348
  }
349
}
350
351
static int retry_generic (int menu, keycode_t *keys, int keyslen, int lastkey)
352
{
353
  if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER)
354
  {
355
    if (lastkey)
356
      mutt_ungetch (lastkey, 0);
357
    for (; keyslen; keyslen--)
358
      mutt_ungetch (keys[keyslen - 1], 0);
359
    return (km_dokey (MENU_GENERIC));
360
  }
361
  if (menu != MENU_EDITOR)
362
  {
363
    /* probably a good idea to flush input here so we can abort macros */
364
    mutt_flushinp ();
365
  }
366
  return OP_NULL;
367
}
368
369
/* return values:
370
 *	>0		function to execute
371
 *	OP_NULL		no function bound to key sequence
372
 *	-1		error occured while reading input
373
 */
374
int km_dokey (int menu)
375
{
376
  event_t tmp;
377
  struct keymap_t *map = Keymaps[menu];
378
  int pos = 0;
379
  int n = 0;
380
  int i;
381
382
  if (!map)
383
    return (retry_generic (menu, NULL, 0, 0));
384
385
  FOREVER
386
  {
387
    /* ncurses doesn't return on resized screen when timeout is set to zero */
388
    if (menu != MENU_EDITOR)
389
      timeout ((Timeout > 0 ? Timeout : 60) * 1000);
390
391
    tmp = mutt_getch();
392
393
    if (menu != MENU_EDITOR)
394
      timeout (-1); /* restore blocking operation */
395
396
    LastKey = tmp.ch;
397
    if (LastKey == -1)
398
      return -1;
399
400
    /* do we have an op already? */
401
    if (tmp.op)
402
    {
403
      char *func = NULL;
404
      struct binding_t *bindings;
405
406
      /* is this a valid op for this menu? */
407
      if ((bindings = km_get_table (menu)) &&
408
	  (func = get_func (bindings, tmp.op)))
409
	return tmp.op;
410
411
      if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
412
	return tmp.op;
413
414
      if (menu != MENU_EDITOR && menu != MENU_PAGER)
415
      {
416
	/* check generic menu */
417
	bindings = OpGeneric; 
418
	if ((func = get_func (bindings, tmp.op)))
419
	  return tmp.op;
420
      }
421
422
      /* Sigh. Valid function but not in this context.
423
       * Find the literal string and push it back */
424
      for (i = 0; Menus[i].name; i++)
425
      {
426
	bindings = km_get_table (Menus[i].value);
427
	if (bindings)
428
	{
429
	  func = get_func (bindings, tmp.op);
430
	  if (func)
431
	  {
432
	    /* careful not to feed the <..> as one token. otherwise 
433
	    * push_string() will push the bogus op right back! */
434
	    mutt_ungetch ('>', 0);
435
	    push_string (func);
436
	    mutt_ungetch ('<', 0);
437
	    break;
438
	  }
439
	}
440
      }
441
      /* continue to chew */
442
      if (func)
443
	continue;
444
    }
445
446
    /* Nope. Business as usual */
447
    while (LastKey > map->keys[pos])
448
    {
449
      if (pos > map->eq || !map->next)
450
	return (retry_generic (menu, map->keys, pos, LastKey));
451
      map = map->next;
452
    }
453
454
    if (LastKey != map->keys[pos])
455
      return (retry_generic (menu, map->keys, pos, LastKey));
456
457
    if (++pos == map->len)
458
    {
459
460
      if (map->op != OP_MACRO)
461
	return map->op;
462
463
      if (n++ == 10)
464
      {
465
	mutt_flushinp ();
466
	mutt_error _("Macro loop detected.");
467
	return -1;
468
      }
469
470
      push_string (map->macro);
471
      map = Keymaps[menu];
472
      pos = 0;
473
    }
474
  }
475
476
  /* not reached */
477
}
478
479
static void create_bindings (struct binding_t *map, int menu)
480
{
481
  int i;
482
483
  for (i = 0 ; map[i].name ; i++)
484
    if (map[i].seq)
485
      km_bindkey (map[i].seq, menu, map[i].op);
486
}
487
488
char *km_keyname (int c)
489
{
490
  static char buf[10];
491
  char *p;
492
493
  if ((p = mutt_getnamebyvalue (c, KeyNames)))
494
    return p;
495
496
  if (c < 256 && c > -128 && iscntrl ((unsigned char) c))
497
  {
498
    if (c < 0)
499
      c += 256;
500
501
    if (c < 128)
502
    {
503
      buf[0] = '^';
504
      buf[1] = (c + '@') & 0x7f;
505
      buf[2] = 0;
506
    }
507
    else
508
      snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
509
  }
510
  else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */
511
    sprintf (buf, "<F%d>", c - KEY_F0);
512
  else if (IsPrint (c))
513
    snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
514
  else
515
    snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
516
  return (buf);
517
}
518
519
int km_expand_key (char *s, size_t len, struct keymap_t *map)
520
{
521
  size_t l;
522
  int p = 0;
523
524
  if (!map)
525
    return (0);
526
527
  FOREVER
528
  {
529
    strfcpy (s, km_keyname (map->keys[p]), len);
530
    len -= (l = mutt_strlen (s));
531
532
    if (++p >= map->len || !len)
533
      return (1);
534
535
    s += l;
536
  }
537
538
  /* not reached */
539
}
540
541
struct keymap_t *km_find_func (int menu, int func)
542
{
543
  struct keymap_t *map = Keymaps[menu];
544
545
  for (; map; map = map->next)
546
    if (map->op == func)
547
      break;
548
  return (map);
549
}
550
551
void km_init (void)
552
{
553
  memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
554
555
  create_bindings (OpAttach, MENU_ATTACH);
556
  create_bindings (OpBrowser, MENU_FOLDER);
557
  create_bindings (OpCompose, MENU_COMPOSE);
558
  create_bindings (OpMain, MENU_MAIN);
559
  create_bindings (OpPager, MENU_PAGER);
560
  create_bindings (OpPost, MENU_POST);
561
  create_bindings (OpQuery, MENU_QUERY);
562
  create_bindings (OpAlias, MENU_ALIAS);
563
564
565
  if ((WithCrypto & APPLICATION_PGP))
566
    create_bindings (OpPgp, MENU_PGP);
567
568
  if ((WithCrypto & APPLICATION_SMIME))
569
    create_bindings (OpSmime, MENU_SMIME);
570
571
#ifdef CRYPT_BACKEND_GPGME
572
  create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
573
  create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
574
#endif
575
576
#ifdef MIXMASTER
577
  create_bindings (OpMix, MENU_MIX);
578
  
579
  km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
580
  km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
581
  km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
582
#endif
583
  
584
  /* bindings for the line editor */
585
  create_bindings (OpEditor, MENU_EDITOR);
586
  
587
  km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
588
  km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
589
  km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
590
  km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
591
  km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
592
  km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
593
  km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
594
  km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
595
  km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
596
  
597
  /* generic menu keymap */
598
  create_bindings (OpGeneric, MENU_GENERIC);
599
  
600
  km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
601
  km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
602
  km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
603
  km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
604
  km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
605
  km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
606
  km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
607
  km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
608
  km_bindkey ("1", MENU_GENERIC, OP_JUMP);
609
  km_bindkey ("2", MENU_GENERIC, OP_JUMP);
610
  km_bindkey ("3", MENU_GENERIC, OP_JUMP);
611
  km_bindkey ("4", MENU_GENERIC, OP_JUMP);
612
  km_bindkey ("5", MENU_GENERIC, OP_JUMP);
613
  km_bindkey ("6", MENU_GENERIC, OP_JUMP);
614
  km_bindkey ("7", MENU_GENERIC, OP_JUMP);
615
  km_bindkey ("8", MENU_GENERIC, OP_JUMP);
616
  km_bindkey ("9", MENU_GENERIC, OP_JUMP);
617
618
  km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
619
620
  /* Miscellaneous extra bindings */
621
  
622
  km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
623
  km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
624
  km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
625
  km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
626
  km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
627
  km_bindkey ("x", MENU_MAIN, OP_EXIT);
628
629
  km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
630
631
  km_bindkey ("x", MENU_PAGER, OP_EXIT);
632
  km_bindkey ("i", MENU_PAGER, OP_EXIT);
633
  km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
634
  km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
635
  km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
636
  km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
637
  km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
638
  km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
639
  km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
640
  km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
641
  km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
642
  km_bindkey ("1", MENU_PAGER, OP_JUMP);
643
  km_bindkey ("2", MENU_PAGER, OP_JUMP);
644
  km_bindkey ("3", MENU_PAGER, OP_JUMP);
645
  km_bindkey ("4", MENU_PAGER, OP_JUMP);
646
  km_bindkey ("5", MENU_PAGER, OP_JUMP);
647
  km_bindkey ("6", MENU_PAGER, OP_JUMP);
648
  km_bindkey ("7", MENU_PAGER, OP_JUMP);
649
  km_bindkey ("8", MENU_PAGER, OP_JUMP);
650
  km_bindkey ("9", MENU_PAGER, OP_JUMP);
651
652
  km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
653
  
654
  km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
655
  km_bindkey ("<enter>",  MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
656
  km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
657
658
  km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
659
  km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
660
661
  /* edit-to (default "t") hides generic tag-entry in Compose menu
662
     This will bind tag-entry to  "T" in the Compose menu */
663
  km_bindkey ("T", MENU_COMPOSE, OP_TAG);
664
}
665
666
void km_error_key (int menu)
667
{
668
  char buf[SHORT_STRING];
669
  struct keymap_t *key;
670
671
  if(!(key = km_find_func(menu, OP_HELP)))
672
    key = km_find_func(MENU_GENERIC, OP_HELP);
673
  
674
  if(!(km_expand_key(buf, sizeof(buf), key)))
675
  {
676
    mutt_error _("Key is not bound.");
677
    return;
678
  }
679
680
  /* make sure the key is really the help key in this menu */
681
  push_string (buf);
682
  if (km_dokey (menu) != OP_HELP)
683
  {
684
    mutt_error _("Key is not bound.");
685
    return;
686
  }
687
688
  mutt_error (_("Key is not bound.  Press '%s' for help."), buf);
689
  return;
690
}
691
692
int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
693
{
694
  int r = 0;
695
696
  mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
697
  if (MoreArgs (s))
698
  {
699
    strfcpy (err->data, _("push: too many arguments"), err->dsize);
700
    r = -1;
701
  }
702
  else
703
    push_string (buf->data);
704
  return (r);
705
}
706
707
/* expects to see: <menu-string>,<menu-string>,... <key-string> */
708
static char *parse_keymap (int *menu, BUFFER *s, int maxmenus, int *nummenus, BUFFER *err)
709
{
710
  BUFFER buf;
711
  int i=0;
712
  char *p, *q;
713
714
  memset (&buf, 0, sizeof (buf));
715
716
  /* menu name */
717
  mutt_extract_token (&buf, s, 0);
718
  p = buf.data;
719
  if (MoreArgs (s))
720
  {
721
    while (i < maxmenus)
722
    {
723
      q = strchr(p,',');
724
      if (q)
725
        *q = '\0';
726
727
      if ((menu[i] = mutt_check_menu (p)) == -1)
728
      {
729
         snprintf (err->data, err->dsize, _("%s: no such menu"), p);
730
         goto error;
731
      }
732
      ++i;
733
      if (q)
734
        p = q+1;
735
      else
736
        break;
737
    }
738
    *nummenus=i;
739
    /* key sequence */
740
    mutt_extract_token (&buf, s, 0);
741
742
    if (!*buf.data)
743
    {
744
      strfcpy (err->data, _("null key sequence"), err->dsize);
745
    }
746
    else if (MoreArgs (s))
747
      return (buf.data);
748
  }
749
  else
750
  {
751
    strfcpy (err->data, _("too few arguments"), err->dsize);
752
  }
753
error:
754
  FREE (&buf.data);
755
  return (NULL);
756
}
757
758
static int
759
try_bind (char *key, int menu, char *func, struct binding_t *bindings)
760
{
761
  int i;
762
  
763
  for (i = 0; bindings[i].name; i++)
764
    if (mutt_strcmp (func, bindings[i].name) == 0)
765
    {
766
      km_bindkey (key, menu, bindings[i].op);
767
      return (0);
768
    }
769
  return (-1);
770
}
771
772
struct binding_t *km_get_table (int menu)
773
{
774
  switch (menu)
775
  {
776
    case MENU_MAIN:
777
      return OpMain;
778
    case MENU_GENERIC:
779
      return OpGeneric;
780
    case MENU_COMPOSE:
781
      return OpCompose;
782
    case MENU_PAGER:
783
      return OpPager;
784
    case MENU_POST:
785
      return OpPost;
786
    case MENU_FOLDER:
787
      return OpBrowser;
788
    case MENU_ALIAS:
789
      return OpAlias;
790
    case MENU_ATTACH:
791
      return OpAttach;
792
    case MENU_EDITOR:
793
      return OpEditor;
794
    case MENU_QUERY:
795
      return OpQuery;
796
797
    case MENU_PGP:
798
      return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;
799
800
#ifdef CRYPT_BACKEND_GPGME
801
    case MENU_KEY_SELECT_PGP:
802
      return OpPgp;
803
    case MENU_KEY_SELECT_SMIME:
804
      return OpSmime;
805
#endif
806
807
#ifdef MIXMASTER
808
    case MENU_MIX:
809
      return OpMix;
810
#endif
811
812
  }
813
  return NULL;
814
}
815
816
/* bind menu-name '<key_sequence>' function-name */
817
int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
818
{
819
  struct binding_t *bindings = NULL;
820
  char *key;
821
  int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = 0, nummenus, i;
822
823
  if ((key = parse_keymap (menu, s, sizeof (menu)/sizeof (menu[0]),
824
			   &nummenus, err)) == NULL)
825
    return (-1);
826
827
  /* function to execute */
828
  mutt_extract_token (buf, s, 0);
829
  if (MoreArgs (s))
830
  {
831
    strfcpy (err->data, _("bind: too many arguments"), err->dsize);
832
    r = -1;
833
  }
834
  else if (ascii_strcasecmp ("noop", buf->data) == 0)
835
  {
836
    for (i = 0; i < nummenus; ++i)
837
    {
838
      km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
839
    }
840
  }
841
  else
842
  {
843
    for (i = 0; i < nummenus; ++i)
844
    {
845
      /* First check the "generic" list of commands */
846
      if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
847
      menu[i] == MENU_GENERIC ||
848
	  try_bind (key, menu[i], buf->data, OpGeneric) != 0)
849
      {
850
        /* Now check the menu-specific list of commands (if they exist) */
851
        bindings = km_get_table (menu[i]);
852
        if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0)
853
        {
854
          snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data);
855
          r = -1;
856
        }
857
      }
858
    }
859
  }
860
  FREE (&key);
861
  return (r);
862
}
863
864
/* macro <menu> <key> <macro> <description> */
865
int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
866
{
867
  int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = -1, nummenus, i;
868
  char *seq = NULL;
869
  char *key;
870
871
  if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus, err)) == NULL)
872
    return (-1);
873
874
  mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
875
  /* make sure the macro sequence is not an empty string */
876
  if (!*buf->data)
877
  {
878
    strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
879
  }
880
  else
881
  {
882
    if (MoreArgs (s))
883
    {
884
      seq = safe_strdup (buf->data);
885
      mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
886
887
      if (MoreArgs (s))
888
      {
889
	strfcpy (err->data, _("macro: too many arguments"), err->dsize);
890
      }
891
      else
892
      {
893
        for (i = 0; i < nummenus; ++i)
894
        {
895
          km_bind (key, menu[i], OP_MACRO, seq, buf->data);
896
          r = 0;
897
        }
898
      }
899
900
      FREE (&seq);
901
    }
902
    else
903
    {
904
      for (i = 0; i < nummenus; ++i)
905
      {
906
        km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
907
        r = 0;
908
      }
909
    }
910
  }
911
  FREE (&key);
912
  return (r);
913
}
914
915
/* exec function-name */
916
int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
917
{
918
  int ops[128]; 
919
  int nops = 0;
920
  struct binding_t *bindings = NULL;
921
  char *function;
922
923
  if (!MoreArgs (s))
924
  {
925
    strfcpy (err->data, _("exec: no arguments"), err->dsize);
926
    return (-1);
927
  }
928
929
  do
930
  {
931
    mutt_extract_token (buf, s, 0);
932
    function = buf->data;
933
934
    if ((bindings = km_get_table (CurrentMenu)) == NULL 
935
	&& CurrentMenu != MENU_PAGER)
936
      bindings = OpGeneric;
937
938
    ops[nops] = get_op (bindings, function, mutt_strlen(function));
939
    if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
940
      ops[nops] = get_op (OpGeneric, function, mutt_strlen(function));
941
942
    if (ops[nops] == OP_NULL)
943
    {
944
      mutt_flushinp ();
945
      mutt_error (_("%s: no such function"), function);
946
      return (-1);
947
    }
948
    nops++;
949
  }
950
  while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0]));
951
952
  while(nops)
953
    mutt_ungetch(0, ops[--nops]);
954
955
  return 0;
956
}
957
958
/*
959
 * prompts the user to enter a keystroke, and displays the octal value back
960
 * to the user.
961
 */
962
void mutt_what_key (void)
963
{
964
  int ch;
965
966
  mvprintw(LINES-1,0, _("Enter keys (^G to abort): "));
967
  do {
968
    ch = getch();
969
    if (ch != ERR && ch != ctrl ('G'))
970
    {
971
      mutt_message(_("Char = %s, Octal = %o, Decimal = %d"),
972
	       km_keyname(ch), ch, ch);
973
    }
974
  }
975
  while (ch != ERR && ch != ctrl ('G'));
976
977
  mutt_flushinp();
978
}