From 76563c9b90ab01658779a1118e3a1b6f90bc370e Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Thu, 17 Jul 2014 21:08:55 +0200 Subject: Rework key binding context switching Store key binding contexts using another data structure to optimize space usage and execution time. Signed-off-by: Lukas Fleischer --- src/calcurse.h | 11 +----- src/custom.c | 70 ++++++++++------------------------- src/keys.c | 71 ++++++++++++++++++++++++++++++----- src/notify.c | 8 ++-- src/wins.c | 114 +++++++++++++++++++++++---------------------------------- 5 files changed, 132 insertions(+), 142 deletions(-) diff --git a/src/calcurse.h b/src/calcurse.h index 6d592f7..d6dfda2 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -466,12 +466,6 @@ enum key { KEY_UNDEF }; -/* To describe a key binding. */ -struct binding { - char *label; - enum key action; -}; - #define FLAG_CAL (1 << CAL) #define FLAG_APP (1 << APP) #define FLAG_TOD (1 << TOD) @@ -803,8 +797,7 @@ int keys_action_count_keys(enum key); const char *keys_action_firstkey(enum key); const char *keys_action_nkey(enum key, int); char *keys_action_allkeys(enum key); -void keys_display_bindings_bar(WINDOW *, struct binding *[], int, int, - int, struct binding *); +void keys_display_bindings_bar(WINDOW *, int *, int, int, int); void keys_popup_info(enum key); void keys_save_bindings(FILE *); int keys_check_missing_bindings(void); @@ -1126,7 +1119,7 @@ void wins_reset(void); void wins_prepare_external(void); void wins_unprepare_external(void); void wins_launch_external(const char *[]); -void wins_set_bindings(struct binding **, int); +void wins_set_bindings(int *, int); void wins_update_bindings(void); void wins_status_bar(void); void wins_erase_status_bar(void); diff --git a/src/custom.c b/src/custom.c index 9e61705..607d026 100644 --- a/src/custom.c +++ b/src/custom.c @@ -124,21 +124,14 @@ void custom_config_bar(void) static void layout_selection_bar(void) { - struct binding quit = { _("Exit"), KEY_GENERIC_QUIT }; - struct binding select = { _("Select"), KEY_GENERIC_SELECT }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding left = { _("Left"), KEY_MOVE_LEFT }; - struct binding right = { _("Right"), KEY_MOVE_RIGHT }; - struct binding help = { _("Help"), KEY_GENERIC_HELP }; - - struct binding *bindings[] = { - &quit, &select, &up, &down, &left, &right, &help + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_GENERIC_SELECT, KEY_MOVE_UP, + KEY_MOVE_DOWN, KEY_MOVE_LEFT, KEY_MOVE_RIGHT, KEY_GENERIC_HELP }; int bindings_size = ARRAY_SIZE(bindings); keys_display_bindings_bar(win[STA].p, bindings, bindings_size, 0, - bindings_size, NULL); + bindings_size); } #define NBLAYOUTS 8 @@ -269,19 +262,13 @@ void custom_layout_config(void) /* Sidebar configuration screen. */ void custom_sidebar_config(void) { - struct binding quit = { _("Exit"), KEY_GENERIC_QUIT }; - struct binding inc = { _("Width +"), KEY_MOVE_UP }; - struct binding dec = { _("Width -"), KEY_MOVE_DOWN }; - struct binding help = { _("Help"), KEY_GENERIC_HELP }; - struct binding *bindings[] = { - &inc, &dec, &help, &quit + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_MOVE_UP, KEY_MOVE_DOWN, KEY_GENERIC_HELP }; - int ch, bindings_size; - - bindings_size = ARRAY_SIZE(bindings); + int ch, bindings_size = ARRAY_SIZE(bindings); keys_display_bindings_bar(win[STA].p, bindings, bindings_size, 0, - bindings_size, NULL); + bindings_size); wins_doupdate(); while ((ch = @@ -308,7 +295,7 @@ void custom_sidebar_config(void) wins_update_panels(FLAG_ALL); keys_display_bindings_bar(win[STA].p, bindings, bindings_size, 0, - bindings_size, NULL); + bindings_size); wins_doupdate(); } } @@ -351,21 +338,14 @@ void custom_confwin_init(struct window *confwin, const char *label) static void color_selection_bar(void) { - struct binding quit = { _("Exit"), KEY_GENERIC_QUIT }; - struct binding select = { _("Select"), KEY_GENERIC_SELECT }; - struct binding nocolor = { _("No color"), KEY_GENERIC_CANCEL }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding left = { _("Left"), KEY_MOVE_LEFT }; - struct binding right = { _("Right"), KEY_MOVE_RIGHT }; - - struct binding *bindings[] = { - &quit, &nocolor, &up, &down, &left, &right, &select + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_GENERIC_SELECT, KEY_GENERIC_CANCEL, + KEY_MOVE_UP, KEY_MOVE_DOWN, KEY_MOVE_LEFT, KEY_GENERIC_SELECT }; int bindings_size = ARRAY_SIZE(bindings); keys_display_bindings_bar(win[STA].p, bindings, bindings_size, 0, - bindings_size, NULL); + bindings_size); } /* @@ -771,11 +751,9 @@ static void general_option_edit(int i) /* General configuration. */ void custom_general_config(void) { - struct binding quit = { _("Quit"), KEY_GENERIC_QUIT }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding edit = { _("Edit Itm"), KEY_EDIT_ITEM }; - struct binding *bindings[] = { &quit, &up, &down, &edit }; + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_MOVE_UP, KEY_MOVE_DOWN, KEY_EDIT_ITEM + }; struct listbox lb; int ch; @@ -895,22 +873,14 @@ print_keys_bindings(WINDOW * win, int selected_row, int selected_elm, static void custom_keys_config_bar(void) { - struct binding quit = { _("Exit"), KEY_GENERIC_QUIT }; - struct binding info = { _("Key info"), KEY_GENERIC_HELP }; - struct binding add = { _("Add key"), KEY_ADD_ITEM }; - struct binding del = { _("Del key"), KEY_DEL_ITEM }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding left = { _("Prev Key"), KEY_MOVE_LEFT }; - struct binding right = { _("Next Key"), KEY_MOVE_RIGHT }; - - struct binding *bindings[] = { - &quit, &info, &add, &del, &up, &down, &left, &right + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_GENERIC_HELP, KEY_ADD_ITEM, KEY_DEL_ITEM, + KEY_MOVE_UP, KEY_MOVE_DOWN, KEY_MOVE_LEFT, KEY_MOVE_RIGHT }; int bindings_size = ARRAY_SIZE(bindings); keys_display_bindings_bar(win[STA].p, bindings, bindings_size, 0, - bindings_size, NULL); + bindings_size); } void custom_keys_config(void) diff --git a/src/keys.c b/src/keys.c index d05b837..0895472 100644 --- a/src/keys.c +++ b/src/keys.c @@ -100,6 +100,57 @@ static struct keydef_s keydef[NBKEYS] = { {"lower-priority", "-"}, }; +#define gettext_noop(s) s +static const char *binding_labels[] = { + gettext_noop("Cancel"), + gettext_noop("Select"), + gettext_noop("Credits"), + gettext_noop("Help"), + gettext_noop("Quit"), + gettext_noop("Save"), + gettext_noop("Reload"), + gettext_noop("Copy"), + gettext_noop("Paste"), + gettext_noop("Chg Win"), + gettext_noop("Import"), + gettext_noop("Export"), + gettext_noop("Go to"), + gettext_noop("OtherCmd"), + gettext_noop("Config"), + gettext_noop("Redraw"), + gettext_noop("Add Appt"), + gettext_noop("Add Todo"), + gettext_noop("-1 Day"), + gettext_noop("+1 Day"), + gettext_noop("-1 Week"), + gettext_noop("+1 Week"), + gettext_noop("-1 Month"), + gettext_noop("+1 Month"), + gettext_noop("-1 Year"), + gettext_noop("+1 Year"), + gettext_noop("Nxt View"), + gettext_noop("Prv View"), + gettext_noop("Today"), + gettext_noop("Command"), + gettext_noop("Right"), + gettext_noop("Left"), + gettext_noop("Down"), + gettext_noop("Up"), + gettext_noop("beg Week"), + gettext_noop("end Week"), + gettext_noop("Add Item"), + gettext_noop("Del Item"), + gettext_noop("Edit Itm"), + gettext_noop("View"), + gettext_noop("Pipe"), + gettext_noop("Flag Itm"), + gettext_noop("Repeat"), + gettext_noop("EditNote"), + gettext_noop("ViewNote"), + gettext_noop("Prio.+"), + gettext_noop("Prio.-") +}; + static void dump_intro(FILE * fd) { const char *intro = @@ -411,9 +462,8 @@ static char *keys_format_label(char *key, int keylen) } void -keys_display_bindings_bar(WINDOW * win, struct binding *bindings[], - int count, int page_base, int page_size, - struct binding *more) +keys_display_bindings_bar(WINDOW * win, int *bindings, int count, + int page_base, int page_size) { /* Padding between two key bindings. */ const int padding = @@ -431,16 +481,16 @@ keys_display_bindings_bar(WINDOW * win, struct binding *bindings[], const int label_pos_x = key_pos_x + KEYS_KEYLEN + 1; const int label_pos_y = key_pos_y; - struct binding *binding; char key[KEYS_KEYLEN + 1], *fmtkey; - if (!more || i < page_size - 1 - || page_base + i == count - 1) - binding = bindings[page_base + i]; + int binding_key; + + if (i < page_size - 1 || page_base + i == count - 1) + binding_key = bindings[page_base + i]; else - binding = more; + binding_key = KEY_GENERIC_OTHER_CMD; - strncpy(key, keys_action_firstkey(binding->action), + strncpy(key, keys_action_firstkey(binding_key), KEYS_KEYLEN); key[KEYS_KEYLEN] = '\0'; fmtkey = keys_format_label(key, KEYS_KEYLEN); @@ -448,7 +498,8 @@ keys_display_bindings_bar(WINDOW * win, struct binding *bindings[], custom_apply_attr(win, ATTR_HIGHEST); mvwaddstr(win, key_pos_y, key_pos_x, fmtkey); custom_remove_attr(win, ATTR_HIGHEST); - mvwaddstr(win, label_pos_y, label_pos_x, binding->label); + mvwaddstr(win, label_pos_y, label_pos_x, + gettext(binding_labels[binding_key])); } wnoutrefresh(win); } diff --git a/src/notify.c b/src/notify.c index a83de0c..b8ba244 100644 --- a/src/notify.c +++ b/src/notify.c @@ -775,11 +775,9 @@ static void config_option_edit(int i) /* Notify-bar configuration. */ void notify_config_bar(void) { - struct binding quit = { _("Quit"), KEY_GENERIC_QUIT }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding edit = { _("Edit Itm"), KEY_EDIT_ITEM }; - struct binding *bindings[] = { &quit, &up, &down, &edit }; + static int bindings[] = { + KEY_GENERIC_QUIT, KEY_MOVE_UP, KEY_MOVE_DOWN, KEY_EDIT_ITEM + }; struct listbox lb; int ch; diff --git a/src/wins.c b/src/wins.c index 5f8d877..66e11af 100644 --- a/src/wins.c +++ b/src/wins.c @@ -612,12 +612,12 @@ void wins_launch_external(const char *arg[]) wins_unprepare_external(); } -static struct binding **bindings; +static int *bindings; static int bindings_size; static unsigned status_page; /* Sets the current set of key bindings to display in the status bar. */ -void wins_set_bindings(struct binding **new_bindings, int size) +void wins_set_bindings(int *new_bindings, int size) { bindings = new_bindings; bindings_size = size; @@ -631,76 +631,55 @@ void wins_set_bindings(struct binding **new_bindings, int size) */ void wins_update_bindings(void) { - struct binding help = { _("Help"), KEY_GENERIC_HELP }; - struct binding quit = { _("Quit"), KEY_GENERIC_QUIT }; - struct binding save = { _("Save"), KEY_GENERIC_SAVE }; - struct binding reload = { _("Reload"), KEY_GENERIC_RELOAD }; - struct binding copy = { _("Copy"), KEY_GENERIC_COPY }; - struct binding paste = { _("Paste"), KEY_GENERIC_PASTE }; - struct binding chgvu = { _("Chg Win"), KEY_GENERIC_CHANGE_VIEW }; - struct binding import = { _("Import"), KEY_GENERIC_IMPORT }; - struct binding export = { _("Export"), KEY_GENERIC_EXPORT }; - struct binding togo = { _("Go to"), KEY_GENERIC_GOTO }; - struct binding conf = { _("Config"), KEY_GENERIC_CONFIG_MENU }; - struct binding draw = { _("Redraw"), KEY_GENERIC_REDRAW }; - struct binding appt = { _("Add Appt"), KEY_GENERIC_ADD_APPT }; - struct binding todo = { _("Add Todo"), KEY_GENERIC_ADD_TODO }; - struct binding gpday = { _("-1 Day"), KEY_GENERIC_PREV_DAY }; - struct binding gnday = { _("+1 Day"), KEY_GENERIC_NEXT_DAY }; - struct binding gpweek = { _("-1 Week"), KEY_GENERIC_PREV_WEEK }; - struct binding gnweek = { _("+1 Week"), KEY_GENERIC_NEXT_WEEK }; - struct binding gpmonth = { _("-1 Month"), KEY_GENERIC_PREV_MONTH }; - struct binding gnmonth = { _("+1 Month"), KEY_GENERIC_NEXT_MONTH }; - struct binding gpyear = { _("-1 Year"), KEY_GENERIC_PREV_YEAR }; - struct binding gnyear = { _("+1 Year"), KEY_GENERIC_NEXT_YEAR }; - struct binding today = { _("Today"), KEY_GENERIC_GOTO_TODAY }; - struct binding nview = { _("Nxt View"), KEY_GENERIC_SCROLL_DOWN }; - struct binding pview = { _("Prv View"), KEY_GENERIC_SCROLL_UP }; - struct binding cmd = { _("Command"), KEY_GENERIC_CMD }; - struct binding up = { _("Up"), KEY_MOVE_UP }; - struct binding down = { _("Down"), KEY_MOVE_DOWN }; - struct binding left = { _("Left"), KEY_MOVE_LEFT }; - struct binding right = { _("Right"), KEY_MOVE_RIGHT }; - struct binding weekb = { _("beg Week"), KEY_START_OF_WEEK }; - struct binding weeke = { _("end Week"), KEY_END_OF_WEEK }; - struct binding add = { _("Add Item"), KEY_ADD_ITEM }; - struct binding del = { _("Del Item"), KEY_DEL_ITEM }; - struct binding edit = { _("Edit Itm"), KEY_EDIT_ITEM }; - struct binding view = { _("View"), KEY_VIEW_ITEM }; - struct binding pipe = { _("Pipe"), KEY_PIPE_ITEM }; - struct binding flag = { _("Flag Itm"), KEY_FLAG_ITEM }; - struct binding rept = { _("Repeat"), KEY_REPEAT_ITEM }; - struct binding enote = { _("EditNote"), KEY_EDIT_NOTE }; - struct binding vnote = { _("ViewNote"), KEY_VIEW_NOTE }; - struct binding rprio = { _("Prio.+"), KEY_RAISE_PRIORITY }; - struct binding lprio = { _("Prio.-"), KEY_LOWER_PRIORITY }; - - struct binding *bindings_cal[] = { - &help, &quit, &save, &reload, &chgvu, &nview, &pview, &up, - &down, &left, &right, &togo, &import, &export, &weekb, &weeke, - &appt, &todo, &gpday, &gnday, &gpweek, &gnweek, &gpmonth, - &gnmonth, &gpyear, &gnyear, &draw, &today, &conf, &cmd + static int bindings_cal[] = { + KEY_GENERIC_HELP, KEY_GENERIC_QUIT, KEY_GENERIC_SAVE, + KEY_GENERIC_RELOAD, KEY_GENERIC_CHANGE_VIEW, + KEY_GENERIC_SCROLL_DOWN, KEY_GENERIC_SCROLL_UP, KEY_MOVE_UP, + KEY_MOVE_DOWN, KEY_MOVE_LEFT, KEY_MOVE_RIGHT, KEY_GENERIC_GOTO, + KEY_GENERIC_IMPORT, KEY_GENERIC_EXPORT, KEY_START_OF_WEEK, + KEY_END_OF_WEEK, KEY_GENERIC_ADD_APPT, KEY_GENERIC_ADD_TODO, + KEY_GENERIC_PREV_DAY, KEY_GENERIC_NEXT_DAY, + KEY_GENERIC_PREV_WEEK, KEY_GENERIC_NEXT_WEEK, + KEY_GENERIC_PREV_MONTH, KEY_GENERIC_NEXT_MONTH, + KEY_GENERIC_PREV_YEAR, KEY_GENERIC_NEXT_YEAR, + KEY_GENERIC_REDRAW, KEY_GENERIC_GOTO_TODAY, + KEY_GENERIC_CONFIG_MENU, KEY_GENERIC_CMD }; - struct binding *bindings_apoint[] = { - &help, &quit, &save, &reload, &chgvu, &import, &export, &add, - &del, &edit, &view, &pipe, &draw, &rept, &flag, &enote, &vnote, - &up, &down, &gpday, &gnday, &gpweek, &gnweek, &gpmonth, - &gnmonth, &gpyear, &gnyear, &togo, &today, &conf, &appt, &todo, - ©, &paste, &cmd + static int bindings_apoint[] = { + KEY_GENERIC_HELP, KEY_GENERIC_QUIT, KEY_GENERIC_SAVE, + KEY_GENERIC_RELOAD, KEY_GENERIC_CHANGE_VIEW, + KEY_GENERIC_IMPORT, KEY_GENERIC_EXPORT, KEY_ADD_ITEM, + KEY_DEL_ITEM, KEY_EDIT_ITEM, KEY_VIEW_ITEM, KEY_PIPE_ITEM, + KEY_GENERIC_REDRAW, KEY_REPEAT_ITEM, KEY_FLAG_ITEM, + KEY_EDIT_NOTE, KEY_VIEW_NOTE, KEY_MOVE_UP, KEY_MOVE_DOWN, + KEY_GENERIC_PREV_DAY, KEY_GENERIC_NEXT_DAY, + KEY_GENERIC_PREV_WEEK, KEY_GENERIC_NEXT_WEEK, + KEY_GENERIC_PREV_MONTH, KEY_GENERIC_NEXT_MONTH, + KEY_GENERIC_PREV_YEAR, KEY_GENERIC_NEXT_YEAR, KEY_GENERIC_GOTO, + KEY_GENERIC_GOTO_TODAY, KEY_GENERIC_CONFIG_MENU, + KEY_GENERIC_ADD_APPT, KEY_GENERIC_ADD_TODO, KEY_GENERIC_COPY, + KEY_GENERIC_PASTE, KEY_GENERIC_CMD }; - struct binding *bindings_todo[] = { - &help, &quit, &save, &reload, &chgvu, &import, &export, &add, - &del, &edit, &view, &pipe, &flag, &rprio, &lprio, &enote, - &vnote, &up, &down, &gpday, &gnday, &gpweek, &gnweek, &gpmonth, - &gnmonth, &gpyear, &gnyear, &togo, &today, &conf, &appt, &todo, - &draw, &cmd + static int bindings_todo[] = { + KEY_GENERIC_HELP, KEY_GENERIC_QUIT, KEY_GENERIC_SAVE, + KEY_GENERIC_RELOAD, KEY_GENERIC_CHANGE_VIEW, + KEY_GENERIC_IMPORT, KEY_GENERIC_EXPORT, KEY_ADD_ITEM, + KEY_DEL_ITEM, KEY_EDIT_ITEM, KEY_VIEW_ITEM, KEY_PIPE_ITEM, + KEY_FLAG_ITEM, KEY_RAISE_PRIORITY, KEY_LOWER_PRIORITY, + KEY_EDIT_NOTE, KEY_VIEW_NOTE, KEY_MOVE_UP, KEY_MOVE_DOWN, + KEY_GENERIC_PREV_DAY, KEY_GENERIC_NEXT_DAY, + KEY_GENERIC_PREV_WEEK, KEY_GENERIC_NEXT_WEEK, + KEY_GENERIC_PREV_MONTH, KEY_GENERIC_NEXT_MONTH, + KEY_GENERIC_PREV_YEAR, KEY_GENERIC_NEXT_YEAR, KEY_GENERIC_GOTO, + KEY_GENERIC_GOTO_TODAY, KEY_GENERIC_CONFIG_MENU, + KEY_GENERIC_ADD_APPT, KEY_GENERIC_ADD_TODO, KEY_GENERIC_REDRAW, + KEY_GENERIC_CMD }; enum win active_panel = wins_slctd(); - switch (active_panel) { case CAL: wins_set_bindings(bindings_cal, ARRAY_SIZE(bindings_cal)); @@ -721,12 +700,11 @@ void wins_update_bindings(void) /* Draws the status bar. */ void wins_status_bar(void) { - struct binding othr = { _("OtherCmd"), KEY_GENERIC_OTHER_CMD }; + int page_base = (KEYS_CMDS_PER_LINE * 2 - 1) * (status_page - 1); + int page_size = KEYS_CMDS_PER_LINE * 2; keys_display_bindings_bar(win[STA].p, bindings, bindings_size, - (KEYS_CMDS_PER_LINE * 2 - - 1) * (status_page - 1), - KEYS_CMDS_PER_LINE * 2, &othr); + page_base, page_size); } /* Erase status bar. */ -- cgit v1.2.3-54-g00ecf