Commit 2cdaaacb authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

Melting the binding and unbinding code into a single function.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4746 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 49 additions and 93 deletions
+49 -93
2014-04-08 Benno Schulenberg <bensberg@justemail.net>
* src/rcfile.c (parse_binding): Melt the binding and unbinding code,
which are very similar, into a single function.
2014-04-07 Benno Schulenberg <bensberg@justemail.net> 2014-04-07 Benno Schulenberg <bensberg@justemail.net>
* src/{proto.h,global.c,text.c}: Keep a pointer to the Uncut item in * src/{proto.h,global.c,text.c}: Keep a pointer to the Uncut item in
the functions list, to be able to change its description to Unjustify the functions list, to be able to change its description to Unjustify
......
...@@ -462,14 +462,18 @@ int check_bad_binding(sc *s) ...@@ -462,14 +462,18 @@ int check_bad_binding(sc *s)
return 0; return 0;
} }
void parse_keybinding(char *ptr) void parse_binding(char *ptr, bool dobind)
{ {
char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL; char *keyptr = NULL, *keycopy = NULL, *funcptr = NULL, *menuptr = NULL;
sc *s, *newsc; sc *s, *newsc = NULL;
int i, menu; int i, menu;
assert(ptr != NULL); assert(ptr != NULL);
#ifdef DEBUG
fprintf(stderr, "Starting the rebinding code...\n");
#endif
if (*ptr == '\0') { if (*ptr == '\0') {
rcfile_error(N_("Missing key name")); rcfile_error(N_("Missing key name"));
return; return;
...@@ -487,14 +491,15 @@ void parse_keybinding(char *ptr) ...@@ -487,14 +491,15 @@ void parse_keybinding(char *ptr)
return; return;
} }
if (dobind) {
funcptr = ptr; funcptr = ptr;
ptr = parse_next_word(ptr); ptr = parse_next_word(ptr);
if (!strcmp(funcptr, "")) { if (!strcmp(funcptr, "")) {
rcfile_error( rcfile_error(N_("Must specify function to bind key to"));
N_("Must specify function to bind key to"));
return; return;
} }
}
menuptr = ptr; menuptr = ptr;
ptr = parse_next_word(ptr); ptr = parse_next_word(ptr);
...@@ -507,12 +512,14 @@ void parse_keybinding(char *ptr) ...@@ -507,12 +512,14 @@ void parse_keybinding(char *ptr)
} }
menu = strtomenu(menuptr); menu = strtomenu(menuptr);
if (dobind) {
newsc = strtosc(menu, funcptr); newsc = strtosc(menu, funcptr);
if (newsc == NULL) { if (newsc == NULL) {
rcfile_error( rcfile_error(N_("Could not map name \"%s\" to a function"), funcptr);
N_("Could not map name \"%s\" to a function"), funcptr);
return; return;
} }
}
if (menu < 1) { if (menu < 1) {
rcfile_error( rcfile_error(
...@@ -521,10 +528,14 @@ void parse_keybinding(char *ptr) ...@@ -521,10 +528,14 @@ void parse_keybinding(char *ptr)
} }
#ifdef DEBUG #ifdef DEBUG
if (dobind)
fprintf(stderr, "newsc now address %d, func assigned = %d, menu = %x\n", fprintf(stderr, "newsc now address %d, func assigned = %d, menu = %x\n",
&newsc, newsc->scfunc, menu); &newsc, newsc->scfunc, menu);
else
fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu);
#endif #endif
if (dobind) {
newsc->keystr = keycopy; newsc->keystr = keycopy;
newsc->menu = menu; newsc->menu = menu;
newsc->type = strtokeytype(newsc->keystr); newsc->type = strtokeytype(newsc->keystr);
...@@ -535,85 +546,26 @@ void parse_keybinding(char *ptr) ...@@ -535,85 +546,26 @@ void parse_keybinding(char *ptr)
#endif #endif
if (check_bad_binding(newsc)) { if (check_bad_binding(newsc)) {
rcfile_error( rcfile_error(N_("Sorry, keystr \"%s\" is an illegal binding"), newsc->keystr);
N_("Sorry, keystr \"%s\" is an illegal binding"), newsc->keystr);
return; return;
} }
/* Now let's have some fun. Try and delete the other entries
we found for the same menu, then make this the new beginning. */
for (s = sclist; s != NULL; s = s->next) {
if (((s->menu & newsc->menu)) && s->seq == newsc->seq) {
#ifdef DEBUG
fprintf(stderr, "replacing entry in menu %x\n", s->menu);
#endif
s->menu &= ~newsc->menu;
}
} }
newsc->next = sclist;
sclist = newsc;
}
/* Let the user unbind a sequence from a given (or all) menus. */
void parse_unbinding(char *ptr)
{
char *keyptr = NULL, *keycopy = NULL, *menuptr = NULL;
sc *s;
int i, menu;
assert(ptr != NULL);
if (*ptr == '\0') {
rcfile_error(N_("Missing key name"));
return;
}
keyptr = ptr;
ptr = parse_next_word(ptr);
keycopy = mallocstrcpy(NULL, keyptr);
for (i = 0; i < strlen(keycopy); i++)
keycopy[i] = toupper(keycopy[i]);
#ifdef DEBUG /* Now find and delete any existing same shortcut in the menu(s). */
fprintf(stderr, "Starting the unbinding code...\n");
#endif
if (keycopy[0] != 'M' && keycopy[0] != '^' && keycopy[0] != 'F' && keycopy[0] != 'K') {
rcfile_error(
N_("Keybindings must begin with \"^\", \"M\", or \"F\""));
return;
}
menuptr = ptr;
ptr = parse_next_word(ptr);
if (!strcmp(menuptr, "")) {
rcfile_error(
/* TRANSLATORS: do not translate the word "all". */
N_("Must specify menu to bind key to (or \"all\")"));
return;
}
menu = strtomenu(menuptr);
if (menu < 1) {
rcfile_error(
N_("Could not map name \"%s\" to a menu"), menuptr);
return;
}
#ifdef DEBUG
fprintf(stderr, "unbinding \"%s\" from menu %x\n", keycopy, menu);
#endif
/* Now find the appropriate entries in the menu to delete. */
for (s = sclist; s != NULL; s = s->next) { for (s = sclist; s != NULL; s = s->next) {
if (((s->menu & menu)) && !strcmp(s->keystr,keycopy)) { if (((s->menu & menu)) && !strcmp(s->keystr, keycopy)) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "deleting entry from menu %x\n", s->menu); fprintf(stderr, "deleting entry from menu %x\n", s->menu);
#endif #endif
s->menu &= ~menu; s->menu &= ~menu;
} }
} }
if (dobind) {
/* Add the new shortcut at the start of the list. */
newsc->next = sclist;
sclist = newsc;
}
} }
...@@ -1118,9 +1070,9 @@ void parse_rcfile(FILE *rcstream ...@@ -1118,9 +1070,9 @@ void parse_rcfile(FILE *rcstream
parse_linter(ptr); parse_linter(ptr);
#endif /* !DISABLE_COLOR */ #endif /* !DISABLE_COLOR */
else if (strcasecmp(keyword, "bind") == 0) else if (strcasecmp(keyword, "bind") == 0)
parse_keybinding(ptr); parse_binding(ptr, TRUE);
else if (strcasecmp(keyword, "unbind") == 0) else if (strcasecmp(keyword, "unbind") == 0)
parse_unbinding(ptr); parse_binding(ptr, FALSE);
else else
rcfile_error(N_("Command \"%s\" not understood"), keyword); rcfile_error(N_("Command \"%s\" not understood"), keyword);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment