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;
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);

View File

@ -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;

View File

@ -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

View File

@ -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;

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. */
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);