aboutsummaryrefslogtreecommitdiffstats
path: root/src/keys.c
blob: 40c3188c794d5543f98db56f480165e5ac509552 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2017 calcurse Development Team <misc@calcurse.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <string.h>
#include <math.h>

#include "calcurse.h"

#define MAXKEYVAL   KEY_MAX	/* ncurses defines KEY_MAX as maximum key value */

struct keydef_s {
	const char *label;
	const char *binding;
	const char *sb_label;
};

static llist_t keys[NBKEYS];
static enum key actions[MAXKEYVAL];

struct key_ext {
	int ch;
	enum key action;
};

llist_t actions_ext;

#define gettext_noop(s) s
static struct keydef_s keydef[NBKEYS] = {
	{ "generic-cancel", "ESC", gettext_noop("Cancel") },
	{ "generic-select", "SPC", gettext_noop("Select") },
	{ "generic-credits", "@", gettext_noop("Credits") },
	{ "generic-help", "?", gettext_noop("Help") },
	{ "generic-quit", "q Q", gettext_noop("Quit") },
	{ "generic-save", "s S C-s", gettext_noop("Save") },
	{ "generic-reload", "R", gettext_noop("Reload") },
	{ "generic-copy", "c", gettext_noop("Copy") },
	{ "generic-paste", "p C-v", gettext_noop("Paste") },
	{ "generic-change-view", "TAB", gettext_noop("Chg Win") },
	{ "generic-import", "i I", gettext_noop("Import") },
	{ "generic-export", "x X", gettext_noop("Export") },
	{ "generic-goto", "g G", gettext_noop("Go to") },
	{ "generic-other-cmd", "o O", gettext_noop("OtherCmd") },
	{ "generic-config-menu", "C", gettext_noop("Config") },
	{ "generic-redraw", "C-r", gettext_noop("Redraw") },
	{ "generic-add-appt", "C-a", gettext_noop("Add Appt") },
	{ "generic-add-todo", "C-t", gettext_noop("Add Todo") },
	{ "generic-prev-day", "T C-h", gettext_noop("-1 Day") },
	{ "generic-next-day", "t C-l", gettext_noop("+1 Day") },
	{ "generic-prev-week", "W C-k", gettext_noop("-1 Week") },
	{ "generic-next-week", "w C-j", gettext_noop("+1 Week") },
	{ "generic-prev-month", "M", gettext_noop("-1 Month") },
	{ "generic-next-month", "m", gettext_noop("+1 Month") },
	{ "generic-prev-year", "Y", gettext_noop("-1 Year") },
	{ "generic-next-year", "y", gettext_noop("+1 Year") },
	{ "generic-scroll-down", "C-n", gettext_noop("Nxt View") },
	{ "generic-scroll-up", "C-p", gettext_noop("Prv View") },
	{ "generic-goto-today", "C-g", gettext_noop("Today") },
	{ "generic-command", ":", gettext_noop("Command") },

	{ "move-right", "l L RGT", gettext_noop("Right") },
	{ "move-left", "h H LFT", gettext_noop("Left") },
	{ "move-down", "j J DWN", gettext_noop("Down") },
	{ "move-up", "k K UP", gettext_noop("Up") },
	{ "start-of-week", "0", gettext_noop("beg Week") },
	{ "end-of-week", "$", gettext_noop("end Week") },
	{ "add-item", "a A", gettext_noop("Add Item") },
	{ "del-item", "d D", gettext_noop("Del Item") },
	{ "edit-item", "e E", gettext_noop("Edit Itm") },
	{ "view-item", "v V", gettext_noop("View") },
	{ "pipe-item", "|", gettext_noop("Pipe") },
	{ "flag-item", "!", gettext_noop("Flag Itm") },
	{ "repeat", "r", gettext_noop("Repeat") },
	{ "edit-note", "n N", gettext_noop("EditNote") },
	{ "view-note", ">", gettext_noop("ViewNote") },
	{ "raise-priority", "+", gettext_noop("Prio.+") },
	{ "lower-priority", "-", gettext_noop("Prio.-") }
};

static void dump_intro(FILE * fd)
{
	const char *intro =
	    _("#\n"
	      "# Calcurse keys configuration file\n#\n"
	      "# This file sets the keybindings used by Calcurse.\n"
	      "# Lines beginning with \"#\" are comments, and ignored by Calcurse.\n"
	      "# To assign a keybinding to an action, this file must contain a line\n"
	      "# with the following syntax:\n#\n"
	      "#        ACTION  KEY1  KEY2  ...  KEYn\n#\n"
	      "# Where ACTION is what will be performed when KEY1, KEY2, ..., or KEYn\n"
	      "# will be pressed.\n"
	      "#\n"
	      "# To define bindings which use the CONTROL key, prefix the key with "
	      "'C-'.\n"
	      "# The escape, space bar and horizontal Tab key can be specified using\n"
	      "# the 'ESC', 'SPC' and 'TAB' keyword, respectively.\n"
	      "# Arrow keys can also be specified with the UP, DWN, LFT, RGT keywords.\n"
	      "# Last, Home and End keys can be assigned using 'KEY_HOME' and 'KEY_END'\n"
	      "# keywords."
	      "\n#\n"
	      "# A description of what each ACTION keyword is used for is available\n"
	      "# from calcurse online configuration menu.\n");

	fprintf(fd, "%s\n", intro);
}

void keys_init(void)
{
	int i;

	for (i = 0; i < MAXKEYVAL; i++)
		actions[i] = KEY_UNDEF;
	LLIST_INIT(&actions_ext);
	for (i = 0; i < NBKEYS; i++)
		LLIST_INIT(&keys[i]);
}

static void key_free(char *s)
{
	mem_free(s);
}

void keys_free(void)
{
	int i;

	for (i = 0; i < NBKEYS; i++) {
		LLIST_FREE_INNER(&keys[i], key_free);
		LLIST_FREE(&keys[i]);
	}
}

void keys_dump_defaults(char *file)
{
	FILE *fd;
	int i;

	fd = fopen(file, "w");
	EXIT_IF(fd == NULL,
		_("FATAL ERROR: could not create default keys file."));

	dump_intro(fd);
	for (i = 0; i < NBKEYS; i++)
		fprintf(fd, "%s  %s\n", keydef[i].label,
			keydef[i].binding);
	file_close(fd, __FILE_POS__);
}

const char *keys_get_label(enum key key)
{
	EXIT_IF(key < 0
		|| key > NBKEYS,
		_("FATAL ERROR: key value out of bounds"));

	return keydef[key].label;
}

static int key_ext_hasch(struct key_ext *k, void *cbdata)
{
	return (k->ch == *((int *)cbdata));
}

enum key keys_get_action(int pressed)
{
	if (pressed < 0) {
		return -1;
	} else if (pressed > MAXKEYVAL) {
		llist_item_t *i = LLIST_FIND_FIRST(&actions_ext, &pressed,
						   key_ext_hasch);
		if (!i)
			return KEY_UNDEF;

		struct key_ext *k = LLIST_GET_DATA(i);
		return k->action;
	} else {
		return actions[pressed];
	}
}

int keys_wgetch(WINDOW *win)
{
	int ch, i;
	char buf[UTF8_MAXLEN];

	ch = wgetch(win);

	if (ch > 255) {
		if (ch == KEY_UP || ch == KEY_DOWN || ch == KEY_LEFT ||
		    ch == KEY_RIGHT || ch == KEY_HOME || ch == KEY_END) {
			return ch;
		}
		return -1;
	}

	if (UTF8_ISMULTI(ch) && !UTF8_ISCONT(ch)) {
		buf[0] = ch;
		for (i = 1; i < UTF8_LENGTH(ch); i++) {
			ch = wgetch(win);
			if (!UTF8_ISCONT(ch))
				return -1;
			buf[i] = ch;
		}
		return utf8_ord(buf);
	} else {
		return ch;
	}

}

void keys_wait_for_any_key(WINDOW *win)
{
	keys_wgetch(win);
}

enum key keys_get(WINDOW *win, int *count, int *reg)
{
	int ch = '0';

	if (count && reg) {
		*count = 0;
		*reg = 0;
		do {
			*count = *count * 10 + ch - '0';
			ch = keys_wgetch(win);
		}
		while ((ch == '0' && *count > 0)
		       || (ch >= '1' && ch <= '9'));

		if (*count == 0)
			*count = 1;

		if (ch == '"') {
			ch = keys_wgetch(win);
			if (ch >= '1' && ch <= '9') {
				*reg = ch - '1' + 1;
			} else if (ch >= 'a' && ch <= 'z') {
				*reg = ch - 'a' + 10;
			} else if (ch == '_') {
				*reg = REG_BLACK_HOLE;
			}
			ch = keys_wgetch(win);
		}
	} else {
		ch = keys_wgetch(win);
	}

	switch (ch) {
	case KEY_RESIZE:
		return KEY_RESIZE;
	default:
		return keys_get_action(ch);
	}
}

static void add_key_str(enum key action, int key)
{
	if (action > NBKEYS)
		return;

	LLIST_ADD(&keys[action], keys_int2str(key));
}

int keys_assign_binding(int key, enum key action)
{
	if (key < 0 || actions[key] != KEY_UNDEF) {
		return 1;
	} else if (key > MAXKEYVAL) {
		struct key_ext *k = mem_malloc(sizeof(struct key_ext));
		k->ch = key;
		k->action = action;
		LLIST_ADD(&actions_ext, k);
	} else {
		actions[key] = action;
	}

	add_key_str(action, key);

	return 0;
}

static void del_key_str(enum key action, int key)
{
	llist_item_t *i;
	char *oldstr = keys_int2str(key);;

	if (action > NBKEYS)
		return;

	LLIST_FOREACH(&keys[action], i) {
		if (strcmp(LLIST_GET_DATA(i), oldstr) == 0) {
			LLIST_REMOVE(&keys[action], i);
			goto cleanup;
		}
	}

cleanup:
	mem_free(oldstr);
}

void keys_remove_binding(int key, enum key action)
{
	if (key < 0)
	       return;

	if (key <= MAXKEYVAL) {
		actions[key] = KEY_UNDEF;
	} else {
		llist_item_t *i = LLIST_FIND_FIRST(&actions_ext, &key,
						   key_ext_hasch);
		if (i) {
			struct key_ext *k = LLIST_GET_DATA(i);
			LLIST_REMOVE(&actions_ext, i);
			mem_free(k);
		}
	}

	del_key_str(action, key);
}

int keys_str2int(const char *key)
{
	if (!key)
		return -1;

	if (strlen(key) == 1)
		return (int)key[0];

	if (key[0] == '^')
		return CTRL((int)key[1]);
	else if (starts_with(key, "C-"))
		return CTRL((int)key[strlen("C-")]);
	else if (starts_with(key, "U+"))
		return strtol(&key[2], NULL, 16);
	else if (!strcmp(key, "TAB"))
		return TAB;
	else if (!strcmp(key, "ESC"))
		return ESCAPE;
	else if (!strcmp(key, "SPC"))
		return SPACE;
	else if (!strcmp(key, "UP"))
		return KEY_UP;
	else if (!strcmp(key, "DWN"))
		return KEY_DOWN;
	else if (!strcmp(key, "LFT"))
		return KEY_LEFT;
	else if (!strcmp(key, "RGT"))
		return KEY_RIGHT;
	else if (!strcmp(key, "KEY_HOME"))
		return KEY_HOME;
	else if (!strcmp(key, "KEY_END"))
		return KEY_END;

	return -1;
}

char *keys_int2str(int key)
{
	char *res;

	switch (key) {
	case TAB:
		return strdup("TAB");
	case SPACE:
		return strdup("SPC");
	case ESCAPE:
		return strdup("ESC");
	case KEY_UP:
		return strdup("UP");
	case KEY_DOWN:
		return strdup("DWN");
	case KEY_LEFT:
		return strdup("LFT");
	case KEY_RIGHT:
		return strdup("RGT");
	case KEY_HOME:
		return strdup("KEY_HOME");
	case KEY_END:
		return strdup("KEY_END");
	}

	if (key >= 0x80) {
		asprintf(&res, "U+%04X", key);
		return res;
	} else {
		return strdup((char *)keyname(key));
	}
}

int keys_action_count_keys(enum key action)
{
	llist_item_t *i;
	int n = 0;

	LLIST_FOREACH(&keys[action], i)
	    n++;

	return n;
}

const char *keys_action_firstkey(enum key action)
{
	const char *s = LLIST_GET_DATA(LLIST_FIRST(&keys[action]));
	return (s != NULL) ? s : "XXX";
}

const char *keys_action_nkey(enum key action, int keynum)
{
	return LLIST_GET_DATA(LLIST_NTH(&keys[action], keynum));
}

char *keys_action_allkeys(enum key action)
{
	llist_item_t *i;
	static char keystr[BUFSIZ];
	const char *CHAR_SPACE = " ";

	if (!LLIST_FIRST(&keys[action]))
		return NULL;

	keystr[0] = '\0';
	LLIST_FOREACH(&keys[action], i) {
		const int MAXLEN = sizeof(keystr) - 1 - strlen(keystr);
		strncat(keystr, LLIST_GET_DATA(i), MAXLEN - 1);
		strncat(keystr, CHAR_SPACE, 1);
	}

	return keystr;
}

/* Need this to display keys properly inside status bar. */
static char *keys_format_label(char *key, int keylen)
{
	static char fmtkey[BUFSIZ];
	const int len = strlen(key);
	const char dot = '.';
	int i;

	if (keylen > BUFSIZ)
		return NULL;

	memset(fmtkey, 0, sizeof(fmtkey));
	if (len == 0) {
		strncpy(fmtkey, "?", sizeof(fmtkey));
	} else if (len <= keylen) {
		for (i = 0; i < keylen - len; i++)
			fmtkey[i] = ' ';
		strncat(fmtkey, key, keylen);
	} else {
		for (i = 0; i < keylen - 1; i++)
			fmtkey[i] = key[i];
		fmtkey[keylen - 1] = dot;
	}
	return fmtkey;
}

void
keys_display_bindings_bar(WINDOW * win, int *bindings, int count,
			  int page_base, int page_size)
{
	page_size = MIN(page_size, count - page_base);

	/* Padding between two key bindings. */
	const int padding =
	    (col * 2) / page_size - (KEYS_KEYLEN + KEYS_LABELEN + 1);
	/* Total length of a key binding (including padding). */
	const int cmd_len = KEYS_KEYLEN + KEYS_LABELEN + 1 + padding;

	int i;

	wins_erase_status_bar();
	for (i = 0; i < page_size && page_base + i < count; i++) {
		/* Location of key and label. */
		const int key_pos_x = (i / 2) * cmd_len;
		const int key_pos_y = i % 2;
		const int label_pos_x = key_pos_x + KEYS_KEYLEN + 1;
		const int label_pos_y = key_pos_y;

		char key[KEYS_KEYLEN + 1], *fmtkey;

		int binding_key;

		if (i < page_size - 1 || page_base + i == count - 1)
			binding_key = bindings[page_base + i];
		else
			binding_key = KEY_GENERIC_OTHER_CMD;

		const char *label;

		if (binding_key < NBKEYS) {
			strncpy(key, keys_action_firstkey(binding_key),
				KEYS_KEYLEN);
			key[KEYS_KEYLEN] = '\0';
			label = gettext(keydef[binding_key].sb_label);
		} else {
			switch (binding_key) {
			case KEY_CONFIGMENU_GENERAL:
				strcpy(key, "g");
				label = _("General");
				break;
			case KEY_CONFIGMENU_LAYOUT:
				strcpy(key, "l");
				label = _("Layout");
				break;
			case KEY_CONFIGMENU_SIDEBAR:
				strcpy(key, "s");
				label = _("Sidebar");
				break;
			case KEY_CONFIGMENU_COLOR:
				strcpy(key, "c");
				label = _("Color");
				break;
			case KEY_CONFIGMENU_NOTIFY:
				strcpy(key, "n");
				label = _("Notify");
				break;
			case KEY_CONFIGMENU_KEYS:
				strcpy(key, "k");
				label = _("Keys");
				break;
			default:
				strcpy(key, "?");
				label = _("Unknown");
				break;
			}
		}

		custom_apply_attr(win, ATTR_HIGHEST);
		fmtkey = keys_format_label(key, KEYS_KEYLEN);
		mvwaddstr(win, key_pos_y, key_pos_x, fmtkey);
		custom_remove_attr(win, ATTR_HIGHEST);
		mvwaddstr(win, label_pos_y, label_pos_x, label);
	}
	wnoutrefresh(win);
}

/*
 * Display information about the given key.
 * (could not add the keys descriptions to keydef variable, because of i18n).
 */
void keys_popup_info(enum key key)
{
	char *info[NBKEYS];
	WINDOW *infowin;

	info[KEY_GENERIC_CANCEL] = _("Cancel the ongoing action.");
	info[KEY_GENERIC_SELECT] = _("Select the highlighted item.");
	info[KEY_GENERIC_CREDITS] =
	    _("Print general information about calcurse's authors, license, etc.");
	info[KEY_GENERIC_HELP] =
	    _("Display hints whenever some help screens are available.");
	info[KEY_GENERIC_QUIT] =
	    _("Exit from the current menu, or quit calcurse.");
	info[KEY_GENERIC_SAVE] = _("Save calcurse data.");
	info[KEY_GENERIC_RELOAD] = _("Reload appointments and todo items.");
	info[KEY_GENERIC_COPY] =
	    _("Copy the item that is currently selected.");
	info[KEY_GENERIC_PASTE] =
	    _("Paste an item at the current position.");
	info[KEY_GENERIC_CHANGE_VIEW] =
	    _("Select next panel in calcurse main screen.");
	info[KEY_GENERIC_IMPORT] = _("Import data from an external file.");
	info[KEY_GENERIC_EXPORT] = _("Export data to a new file format.");
	info[KEY_GENERIC_GOTO] = _("Select the day to go to.");
	info[KEY_GENERIC_OTHER_CMD] =
	    _("Show next possible actions inside status bar.");
	info[KEY_GENERIC_CONFIG_MENU] = _("Enter the configuration menu.");
	info[KEY_GENERIC_REDRAW] = _("Redraw calcurse's screen.");
	info[KEY_GENERIC_ADD_APPT] =
	    _("Add an appointment, whichever panel is currently selected.");
	info[KEY_GENERIC_ADD_TODO] =
	    _("Add a todo item, whichever panel is currently selected.");
	info[KEY_GENERIC_PREV_DAY] =
	    _("Move to previous day in calendar, whichever panel is currently "
	     "selected.");
	info[KEY_GENERIC_NEXT_DAY] =
	    _("Move to next day in calendar, whichever panel is currently selected.");
	info[KEY_GENERIC_PREV_WEEK] =
	    _("Move to previous week in calendar, whichever panel is currently "
	     "selected");
	info[KEY_GENERIC_NEXT_WEEK] =
	    _("Move to next week in calendar, whichever panel is currently selected.");
	info[KEY_GENERIC_PREV_MONTH] =
	    _("Move to previous month in calendar, whichever panel is currently "
	     "selected");
	info[KEY_GENERIC_NEXT_MONTH] =
	    _("Move to next month in calendar, whichever panel is currently "
	     "selected.");
	info[KEY_GENERIC_PREV_YEAR] =
	    _("Move to previous year in calendar, whichever panel is currently "
	     "selected");
	info[KEY_GENERIC_NEXT_YEAR] =
	    _("Move to next year in calendar, whichever panel is currently selected.");
	info[KEY_GENERIC_SCROLL_DOWN] =
	    _("Scroll window down (e.g. when displaying text inside a popup window).");
	info[KEY_GENERIC_SCROLL_UP] =
	    _("Scroll window up (e.g. when displaying text inside a popup window).");
	info[KEY_GENERIC_GOTO_TODAY] =
	    _("Go to today, whichever panel is selected.");
	info[KEY_GENERIC_CMD] = _("Enter command mode.");
	info[KEY_MOVE_RIGHT] = _("Move to the right.");
	info[KEY_MOVE_LEFT] = _("Move to the left.");
	info[KEY_MOVE_DOWN] = _("Move down.");
	info[KEY_MOVE_UP] = _("Move up.");
	info[KEY_START_OF_WEEK] =
	    _("Select the first day of the current week when inside the calendar "
	     "panel.");
	info[KEY_END_OF_WEEK] =
	    _("Select the last day of the current week when inside the calendar "
	     "panel.");
	info[KEY_ADD_ITEM] =
	    _("Add an item to the currently selected panel.");
	info[KEY_DEL_ITEM] = _("Delete the currently selected item.");
	info[KEY_EDIT_ITEM] = _("Edit the currently seleted item.");
	info[KEY_VIEW_ITEM] =
	    _("Display the currently selected item inside a popup window.");
	info[KEY_FLAG_ITEM] =
	    _("Flag the currently selected item as important.");
	info[KEY_REPEAT_ITEM] = _("Repeat an item");
	info[KEY_PIPE_ITEM] =
	    _("Pipe the currently selected item to an external program.");
	info[KEY_EDIT_NOTE] =
	    _("Attach (or edit if one exists) a note to the currently selected item");
	info[KEY_VIEW_NOTE] =
	    _("View the note attached to the currently selected item.");
	info[KEY_RAISE_PRIORITY] =
	    _("Raise a task priority inside the todo panel.");
	info[KEY_LOWER_PRIORITY] =
	    _("Lower a task priority inside the todo panel.");

	if (key > NBKEYS)
		return;

#define WINROW 10
#define WINCOL (col - 4)
	infowin =
	    popup(WINROW, WINCOL, (row - WINROW) / 2, (col - WINCOL) / 2,
		  keydef[key].label, info[key], 1);
	keys_get(infowin, NULL, NULL);
	delwin(infowin);
#undef WINROW
#undef WINCOL
}

void keys_save_bindings(FILE * fd)
{
	int i;
	char *action;

	EXIT_IF(fd == NULL, _("FATAL ERROR: null file pointer."));
	dump_intro(fd);
	for (i = 0; i < NBKEYS; i++) {
		action = keys_action_allkeys(i);
		if (action)
			fprintf(fd, "%s  %s\n", keydef[i].label, action);
	}
}

int keys_check_missing_bindings(void)
{
	int i;

	for (i = 0; i < NBKEYS; i++) {
		if (!LLIST_FIRST(&keys[i]))
			return 1;
	}
	return 0;
}

void keys_fill_missing(void)
{
	int i;

	for (i = 0; i < NBKEYS; i++) {
		if (!LLIST_FIRST(&keys[i])) {
			char *p, tmpbuf[BUFSIZ];

			strncpy(tmpbuf, keydef[i].binding, BUFSIZ);
			p = tmpbuf;
			for (;;) {
				char key_ch[BUFSIZ];

				while (*p == ' ')
					p++;
				if (sscanf(p, "%s", key_ch) == 1) {
					int ch, used;

					ch = keys_str2int(key_ch);
					used = keys_assign_binding(ch, i);
					if (used)
						WARN_MSG(_("When adding default key for \"%s\", "
							  "\"%s\" was already assigned!"),
							 keydef[i].label,
							 key_ch);
					p += strlen(key_ch) + 1;
				} else {
					break;
				}
			}
		}
	}
}