From fe8a55b640256711e81b94f9488ec7bdd8d1e956 Mon Sep 17 00:00:00 2001 From: Clint Adams Date: Sat, 16 Dec 2000 16:25:41 +0000 Subject: 13285: use vi-up and -down in vicmd mode --- Src/Zle/zle_keymap.c | 96 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 40 deletions(-) (limited to 'Src/Zle/zle_keymap.c') diff --git a/Src/Zle/zle_keymap.c b/Src/Zle/zle_keymap.c index 7de96bd03..09601eb6d 100644 --- a/Src/Zle/zle_keymap.c +++ b/Src/Zle/zle_keymap.c @@ -98,17 +98,20 @@ struct bindstate { /* currently selected keymap, and its name */ /**/ -Keymap curkeymap; +Keymap curkeymap, localkeymap; /**/ char *curkeymapname; /* the hash table of keymap names */ -static HashTable keymapnamtab; +/**/ +mod_export HashTable keymapnamtab; /* key sequence reading data */ -static char *keybuf; +/**/ +char *keybuf; + static int keybuflen, keybufsz = 20; /* last command executed with execute-named-command */ @@ -128,6 +131,7 @@ createkeymapnamtab(void) keymapnamtab->hash = hasher; keymapnamtab->emptytable = emptyhashtable; keymapnamtab->filltable = NULL; + keymapnamtab->cmpnodes = strcmp; keymapnamtab->addnode = addhashnode; keymapnamtab->getnode = gethashnode2; keymapnamtab->getnode2 = gethashnode2; @@ -170,6 +174,7 @@ newkeytab(char *kmname) ht->hash = hasher; ht->emptytable = emptyhashtable; ht->filltable = NULL; + ht->cmpnodes = strcmp; ht->addnode = addhashnode; ht->getnode = gethashnode2; ht->getnode2 = gethashnode2; @@ -212,7 +217,7 @@ freekeynode(HashNode hn) static HashTable copyto; /**/ -static Keymap +mod_export Keymap newkeymap(Keymap tocopy, char *kmname) { Keymap km = zcalloc(sizeof(*km)); @@ -246,7 +251,7 @@ scancopykeys(HashNode hn, int flags) } /**/ -static void +void deletekeymap(Keymap km) { int i; @@ -318,21 +323,21 @@ openkeymap(char *name) } /**/ -static int -unlinkkeymap(char *name) +mod_export int +unlinkkeymap(char *name, int ignm) { KeymapName n = (KeymapName) keymapnamtab->getnode(keymapnamtab, name); if(!n) return 2; - if(n->flags & KMN_IMMORTAL) + if(!ignm && (n->flags & KMN_IMMORTAL)) return 1; keymapnamtab->freenode(keymapnamtab->removenode(keymapnamtab, name)); return 0; } /**/ -static int -linkkeymap(Keymap km, char *name) +mod_export int +linkkeymap(Keymap km, char *name, int imm) { KeymapName n = (KeymapName) keymapnamtab->getnode(keymapnamtab, name); if(n) { @@ -343,9 +348,12 @@ linkkeymap(Keymap km, char *name) if(!--n->keymap->rc) deletekeymap(n->keymap); n->keymap = km; - } else - keymapnamtab->addnode(keymapnamtab, ztrdup(name), - makekeymapnamnode(km)); + } else { + n = makekeymapnamnode(km); + if (imm) + n->flags |= KMN_IMMORTAL; + keymapnamtab->addnode(keymapnamtab, ztrdup(name), n); + } km->rc++; return 0; } @@ -375,6 +383,15 @@ selectkeymap(char *name, int fb) return 0; } +/* Select a local key map. */ + +/**/ +mod_export void +selectlocalmap(Keymap m) +{ + localkeymap = m; +} + /* Reopen the currently selected keymap, in case it got deleted. This * * should be called after doing anything that might have run an * * arbitrary user-specified command. */ @@ -397,7 +414,7 @@ reselectkeymap(void) * back onto the input. */ /**/ -int +mod_export int bindkey(Keymap km, char *seq, Thingy bind, char *str) { Key k; @@ -598,10 +615,10 @@ bin_bindkey(char *name, char **argv, char *ops, int func) int n; /* select operation and ensure no clashing arguments */ - for(op = opns; op->o && !ops[op->o]; op++) ; + for(op = opns; op->o && !ops[STOUC(op->o)]; op++) ; if(op->o) for(opp = op; (++opp)->o; ) - if(ops[opp->o]) { + if(ops[STOUC(opp->o)]) { zwarnnam(name, "incompatible operation selection options", NULL, 0); return 1; @@ -638,7 +655,7 @@ bin_bindkey(char *name, char **argv, char *ops, int func) return 1; } if(ops['e'] || ops['v']) - linkkeymap(km, "main"); + linkkeymap(km, "main", 0); } else { kmname = NULL; km = NULL; @@ -711,7 +728,7 @@ bin_bindkey_del(char *name, char *kmname, Keymap km, char **argv, char *ops, cha int ret = 0; do { - int r = unlinkkeymap(*argv); + int r = unlinkkeymap(*argv, 0); if(r == 1) zwarnnam(name, "keymap name `%s' is protected", *argv, 0); else if(r == 2) @@ -731,7 +748,7 @@ bin_bindkey_link(char *name, char *kmname, Keymap km, char **argv, char *ops, ch if(!km) { zwarnnam(name, "no such keymap `%s'", argv[0], 0); return 1; - } else if(linkkeymap(km, argv[1])) { + } else if(linkkeymap(km, argv[1], 0)) { zwarnnam(name, "keymap name `%s' is protected", argv[1], 0); return 1; } @@ -758,7 +775,7 @@ bin_bindkey_new(char *name, char *kmname, Keymap km, char **argv, char *ops, cha } } else km = NULL; - linkkeymap(newkeymap(km, argv[0]), argv[0]); + linkkeymap(newkeymap(km, argv[0]), argv[0], 0); return 0; } @@ -983,8 +1000,6 @@ init_keymaps(void) lastnamed = refthingy(t_undefinedkey); } -#ifdef MODULE - /* cleanup entry point (for unloading the zle module) */ /**/ @@ -996,8 +1011,6 @@ cleanup_keymaps(void) zfree(keybuf, keybufsz); } -#endif /* MODULE */ - /* Create the default keymaps. For efficiency reasons, this function * * assigns directly to the km->first array. It knows that there are no * * prefix bindings in the way, and that it is using a simple keymap. */ @@ -1054,12 +1067,12 @@ default_bindings(void) * Both standard and keypad modes are supported. */ /* vi command mode: arrow keys */ - bindkey(amap, "\33[A", refthingy(t_uplineorhistory), NULL); - bindkey(amap, "\33[B", refthingy(t_downlineorhistory), NULL); + bindkey(amap, "\33[A", refthingy(t_viuplineorhistory), NULL); + bindkey(amap, "\33[B", refthingy(t_vidownlineorhistory), NULL); bindkey(amap, "\33[C", refthingy(t_viforwardchar), NULL); bindkey(amap, "\33[D", refthingy(t_vibackwardchar), NULL); - bindkey(amap, "\33OA", refthingy(t_uplineorhistory), NULL); - bindkey(amap, "\33OB", refthingy(t_downlineorhistory), NULL); + bindkey(amap, "\33OA", refthingy(t_viuplineorhistory), NULL); + bindkey(amap, "\33OB", refthingy(t_vidownlineorhistory), NULL); bindkey(amap, "\33OC", refthingy(t_viforwardchar), NULL); bindkey(amap, "\33OD", refthingy(t_vibackwardchar), NULL); @@ -1104,20 +1117,18 @@ default_bindings(void) * will be linked to the "emacs" keymap, except that if VISUAL * * or EDITOR contain the string "vi" then it will be linked to * * the "viins" keymap. */ - linkkeymap(vmap, "viins"); - linkkeymap(emap, "emacs"); - linkkeymap(amap, "vicmd"); - linkkeymap(smap, ".safe"); + linkkeymap(vmap, "viins", 0); + linkkeymap(emap, "emacs", 0); + linkkeymap(amap, "vicmd", 0); + linkkeymap(smap, ".safe", 1); if (((ed = zgetenv("VISUAL")) && strstr(ed, "vi")) || ((ed = zgetenv("EDITOR")) && strstr(ed, "vi"))) - linkkeymap(vmap, "main"); + linkkeymap(vmap, "main", 0); else - linkkeymap(emap, "main"); + linkkeymap(emap, "main", 0); /* the .safe map cannot be modified or deleted */ smap->flags |= KM_IMMUTABLE; - ((KeymapName) keymapnamtab->getnode(keymapnamtab, ".safe"))->flags - |= KMN_IMMORTAL; } /*************************/ @@ -1138,7 +1149,12 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp) keybuf[0] = 0; while((c = getkeybuf(!!lastlen)) != EOF) { char *s; - Thingy f = keybind(km, keybuf, &s); + Thingy f; + int loc = 1; + + if (!localkeymap || + (f = keybind(localkeymap, keybuf, &s)) == t_undefinedkey) + loc = 0, f = keybind(km, keybuf, &s); if(f != t_undefinedkey) { lastlen = keybuflen; @@ -1146,7 +1162,7 @@ getkeymapcmd(Keymap km, Thingy *funcp, char **strp) str = s; lastc = c; } - if(!keyisprefix(km, keybuf)) + if(!keyisprefix((loc ? localkeymap : km), keybuf)) break; } if(!lastlen && keybuflen) @@ -1188,7 +1204,7 @@ getkeybuf(int w) * Must be executed at most once after each getkeymapcmd(). */ /**/ -void +mod_export void ungetkeycmd(void) { ungetkeys(keybuf, keybuflen); @@ -1197,7 +1213,7 @@ ungetkeycmd(void) /* read a command from the current keymap, with widgets */ /**/ -Thingy +mod_export Thingy getkeycmd(void) { Thingy func; -- cgit 1.4.1