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.
master
Benno Schulenberg 2016-07-23 14:42:40 +02:00
parent 91951ab22a
commit e295070193
5 changed files with 16 additions and 23 deletions

View File

@ -339,8 +339,7 @@ void add_to_sclist(int menus, const char *scstring, void (*func)(void), int togg
s->toggle = toggle; s->toggle = toggle;
if (toggle) if (toggle)
s->ordinal = ++counter; s->ordinal = ++counter;
s->keystr = (char *) scstring; assign_keyinfo(s, scstring);
assign_keyinfo(s);
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Setting sequence to %d for shortcut \"%s\" in menus %x\n", s->seq, scstring, s->menus); 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); const sc *s = first_sc_for(currmenu, func);
if (s) { if (s) {
meta_key = (s->type == META); meta_key = s->meta;
return s->seq; return s->seq;
} }
/* else */ /* else */
@ -399,18 +398,17 @@ functionptrtype func_from_key(int *kbinput)
return NULL; return NULL;
} }
/* Assign the info to the shortcut struct. /* Set the string and its corresponding keycode for the given shortcut s. */
* Assumes keystr is already assigned, naturally. */ void assign_keyinfo(sc *s, const char *keystring)
void assign_keyinfo(sc *s)
{ {
s->type = DIRECT; s->keystr = (char *)keystring;
s->meta = (keystring[0] == 'M');
if (s->keystr[0] == '^') { if (s->keystr[0] == '^') {
assert(strlen(s->keystr) > 1); assert(strlen(s->keystr) > 1);
s->seq = s->keystr[1] - 64; s->seq = s->keystr[1] - 64;
} else if (s->keystr[0] == 'M') { } else if (s->meta) {
assert(strlen(s->keystr) > 2); assert(strlen(s->keystr) > 2);
s->type = META;
s->seq = tolower((int) s->keystr[2]); s->seq = tolower((int) s->keystr[2]);
} else if (s->keystr[0] == 'F') { } else if (s->keystr[0] == 'F') {
assert(strlen(s->keystr) > 1); assert(strlen(s->keystr) > 1);

View File

@ -187,10 +187,6 @@ typedef enum {
CENTERING, FLOWING, STATIONARY CENTERING, FLOWING, STATIONARY
} update_type; } update_type;
typedef enum {
DIRECT, META
} key_type;
typedef enum { typedef enum {
ADD, DEL, BACK, CUT, CUT_EOF, REPLACE, ADD, DEL, BACK, CUT, CUT_EOF, REPLACE,
#ifndef DISABLE_WRAPPING #ifndef DISABLE_WRAPPING
@ -441,8 +437,8 @@ typedef struct rcoption {
typedef struct sc { typedef struct sc {
char *keystr; char *keystr;
/* The shortcut key for a function, ASCII version. */ /* The shortcut key for a function, ASCII version. */
key_type type; bool meta;
/* What kind of command key it is, for convenience later. */ /* Whether this is a Meta keystroke. */
int seq; int seq;
/* The actual sequence to check once the type is determined. */ /* The actual sequence to check once the type is determined. */
int menus; int menus;

View File

@ -362,7 +362,7 @@ size_t length_of_list(int menu);
const sc *first_sc_for(int menu, void (*func)(void)); const sc *first_sc_for(int menu, void (*func)(void));
int sc_seq_or(void (*func)(void), int defaultval); int sc_seq_or(void (*func)(void), int defaultval);
functionptrtype func_from_key(int *kbinput); functionptrtype func_from_key(int *kbinput);
void assign_keyinfo(sc *s); void assign_keyinfo(sc *s, const char *keystring);
void print_sclist(void); void print_sclist(void);
void shortcut_init(void); void shortcut_init(void);
#ifndef DISABLE_COLOR #ifndef DISABLE_COLOR

View File

@ -470,12 +470,11 @@ void parse_binding(char *ptr, bool dobind)
goto free_copy; goto free_copy;
} }
newsc->keystr = keycopy;
newsc->menus = menu; newsc->menus = menu;
assign_keyinfo(newsc); assign_keyinfo(newsc, keycopy);
/* Do not allow rebinding the equivalent of the Escape key. */ /* Do not allow rebinding a frequent escape-sequence starter: Esc [. */
if (newsc->type == META && newsc->seq == 91) { if (newsc->meta && newsc->seq == 91) {
rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr); rcfile_error(N_("Sorry, keystroke \"%s\" may not be rebound"), newsc->keystr);
free(newsc); free(newsc);
goto free_copy; goto free_copy;

View File

@ -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. */ /* And put the corresponding key into the keyboard buffer. */
if (f != NULL) { if (f != NULL) {
const sc *s = first_sc_for(currmenu, f->scfunc); const sc *s = first_sc_for(currmenu, f->scfunc);
unget_kbinput(s->seq, s->type == META); unget_kbinput(s->seq, s->meta);
} }
return 1; return 1;
} else } else
@ -1616,8 +1616,8 @@ const sc *get_shortcut(int *kbinput)
#endif #endif
for (s = sclist; s != NULL; s = s->next) { for (s = sclist; s != NULL; s = s->next) {
if ((s->menus & currmenu) && *kbinput == s->seq if ((s->menus & currmenu) && *kbinput == s->seq &&
&& meta_key == (s->type == META)) { meta_key == s->meta) {
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "matched seq '%s' (menu is %x from %x)\n", fprintf (stderr, "matched seq '%s' (menu is %x from %x)\n",
s->keystr, currmenu, s->menus); s->keystr, currmenu, s->menus);