From e295070193ac202e5f41e699590be3f74a49a002 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sat, 23 Jul 2016 14:42:40 +0200 Subject: [PATCH] shortcuts: group the setting of key string and keycode into one function And instead of using two key types, just use a bool to indicate whether a keystroke involves Meta. --- src/global.c | 16 +++++++--------- src/nano.h | 8 ++------ src/proto.h | 2 +- src/rcfile.c | 7 +++---- src/winio.c | 6 +++--- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/global.c b/src/global.c index b95afa87..ab7e689d 100644 --- a/src/global.c +++ b/src/global.c @@ -339,8 +339,7 @@ void add_to_sclist(int menus, const char *scstring, void (*func)(void), int togg s->toggle = toggle; if (toggle) s->ordinal = ++counter; - s->keystr = (char *) scstring; - assign_keyinfo(s); + assign_keyinfo(s, scstring); #ifdef DEBUG fprintf(stderr, "Setting sequence to %d for shortcut \"%s\" in menus %x\n", s->seq, scstring, s->menus); @@ -381,7 +380,7 @@ int sc_seq_or(void (*func)(void), int defaultval) const sc *s = first_sc_for(currmenu, func); if (s) { - meta_key = (s->type == META); + meta_key = s->meta; return s->seq; } /* else */ @@ -399,18 +398,17 @@ functionptrtype func_from_key(int *kbinput) return NULL; } -/* Assign the info to the shortcut struct. - * Assumes keystr is already assigned, naturally. */ -void assign_keyinfo(sc *s) +/* Set the string and its corresponding keycode for the given shortcut s. */ +void assign_keyinfo(sc *s, const char *keystring) { - s->type = DIRECT; + s->keystr = (char *)keystring; + s->meta = (keystring[0] == 'M'); if (s->keystr[0] == '^') { assert(strlen(s->keystr) > 1); s->seq = s->keystr[1] - 64; - } else if (s->keystr[0] == 'M') { + } else if (s->meta) { assert(strlen(s->keystr) > 2); - s->type = META; s->seq = tolower((int) s->keystr[2]); } else if (s->keystr[0] == 'F') { assert(strlen(s->keystr) > 1); diff --git a/src/nano.h b/src/nano.h index b43c3214..da5b278d 100644 --- a/src/nano.h +++ b/src/nano.h @@ -187,10 +187,6 @@ typedef enum { CENTERING, FLOWING, STATIONARY } update_type; -typedef enum { - DIRECT, META -} key_type; - typedef enum { ADD, DEL, BACK, CUT, CUT_EOF, REPLACE, #ifndef DISABLE_WRAPPING @@ -441,8 +437,8 @@ typedef struct rcoption { typedef struct sc { char *keystr; /* The shortcut key for a function, ASCII version. */ - key_type type; - /* What kind of command key it is, for convenience later. */ + bool meta; + /* Whether this is a Meta keystroke. */ int seq; /* The actual sequence to check once the type is determined. */ int menus; diff --git a/src/proto.h b/src/proto.h index 1da24708..796163c2 100644 --- a/src/proto.h +++ b/src/proto.h @@ -362,7 +362,7 @@ size_t length_of_list(int menu); const sc *first_sc_for(int menu, void (*func)(void)); int sc_seq_or(void (*func)(void), int defaultval); functionptrtype func_from_key(int *kbinput); -void assign_keyinfo(sc *s); +void assign_keyinfo(sc *s, const char *keystring); void print_sclist(void); void shortcut_init(void); #ifndef DISABLE_COLOR diff --git a/src/rcfile.c b/src/rcfile.c index 722a9adf..441ffcac 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -470,12 +470,11 @@ void parse_binding(char *ptr, bool dobind) goto free_copy; } - newsc->keystr = keycopy; newsc->menus = menu; - assign_keyinfo(newsc); + assign_keyinfo(newsc, keycopy); - /* Do not allow rebinding the equivalent of the Escape key. */ - if (newsc->type == META && newsc->seq == 91) { + /* Do not allow rebinding a frequent escape-sequence starter: Esc [. */ + if (newsc->meta && newsc->seq == 91) { rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr); free(newsc); goto free_copy; diff --git a/src/winio.c b/src/winio.c index 0cfe2724..b8f034ae 100644 --- a/src/winio.c +++ b/src/winio.c @@ -1558,7 +1558,7 @@ int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts) /* And put the corresponding key into the keyboard buffer. */ if (f != NULL) { const sc *s = first_sc_for(currmenu, f->scfunc); - unget_kbinput(s->seq, s->type == META); + unget_kbinput(s->seq, s->meta); } return 1; } else @@ -1616,8 +1616,8 @@ const sc *get_shortcut(int *kbinput) #endif for (s = sclist; s != NULL; s = s->next) { - if ((s->menus & currmenu) && *kbinput == s->seq - && meta_key == (s->type == META)) { + if ((s->menus & currmenu) && *kbinput == s->seq && + meta_key == s->meta) { #ifdef DEBUG fprintf (stderr, "matched seq '%s' (menu is %x from %x)\n", s->keystr, currmenu, s->menus);