tvi: distinct keymap for insert mode and command prompt - neatvi - [fork] simple vi-type editor with UTF-8 support
 (HTM) git clone git://src.adamsgaard.dk/neatvi
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 6f05dff38aadda562cfbcf7532af2119a0e8d998
 (DIR) parent 685cdd8a70a01377e93fb1e49a6674200c92a5cd
 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
       Date:   Wed, 20 May 2015 15:58:12 +0430
       
       vi: distinct keymap for insert mode and command prompt
       
       Diffstat:
         M led.c                               |      35 +++++++++++++++----------------
         M vi.c                                |      19 +++++++++++--------
         M vi.h                                |       6 +++---
       
       3 files changed, 31 insertions(+), 29 deletions(-)
       ---
 (DIR) diff --git a/led.c b/led.c
       t@@ -6,22 +6,22 @@
        #include "kmap.h"
        
        static char **kmaps[] = {kmap_en, kmap_fa};
       -static char **led_kmap = kmap_en;
        
        static char **kmap_find(char *name)
        {
                int i;
                for (i = 0; i < LEN(kmaps); i++)
       -                if (kmaps[i][0] && !strcmp(name, kmaps[i][0]))
       +                if (name && kmaps[i][0] && !strcmp(name, kmaps[i][0]))
                                return kmaps[i];
                return kmap_en;
        }
        
       -static char *kmap_map(char **kmap, int c)
       +static char *kmap_map(char *kmap, int c)
        {
                static char cs[4];
       +        char **keymap = kmap_find(kmap);
                cs[0] = c;
       -        return kmap[c] ? kmap[c] : cs;
       +        return keymap[c] ? keymap[c] : cs;
        }
        
        /* map cursor horizontal position to terminal column number */
       t@@ -30,9 +30,9 @@ int led_pos(char *s, int pos)
                return dir_context(s) >= 0 ? pos : xcols - pos - 1;
        }
        
       -char *led_keymap(int c)
       +char *led_keymap(char *kmap, int c)
        {
       -        return c >= 0 ? kmap_map(led_kmap, c) : NULL;
       +        return c >= 0 ? kmap_map(kmap, c) : NULL;
        }
        
        static char *led_render(char *s0)
       t@@ -112,7 +112,7 @@ static int led_lastword(char *s)
                return r - s;
        }
        
       -static void led_printparts(char *ai, char *pref, char *main, char *post)
       +static void led_printparts(char *ai, char *pref, char *main, char *post, char *kmap)
        {
                struct sbuf *ln;
                int off, pos;
       t@@ -125,7 +125,7 @@ static void led_printparts(char *ai, char *pref, char *main, char *post)
                /* cursor position for inserting the next character */
                if (*pref || *main || *ai) {
                        int len = sbuf_len(ln);
       -                sbuf_str(ln, kmap_map(led_kmap, 'a'));
       +                sbuf_str(ln, kmap_map(kmap, 'a'));
                        sbuf_str(ln, post);
                        idir = ren_pos(sbuf_buf(ln), off) -
                                ren_pos(sbuf_buf(ln), off - 1) < 0 ? -1 : +1;
       t@@ -138,7 +138,7 @@ static void led_printparts(char *ai, char *pref, char *main, char *post)
                sbuf_free(ln);
        }
        
       -static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, char ***kmap)
       +static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, char **kmap)
        {
                struct sbuf *sb;
                int ai_len = strlen(ai);
       t@@ -149,14 +149,14 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
                if (!post)
                        post = "";
                while (1) {
       -                led_printparts(ai, pref, sbuf_buf(sb), post);
       +                led_printparts(ai, pref, sbuf_buf(sb), post, *kmap);
                        c = term_read(-1);
                        switch (c) {
                        case TK_CTL('f'):
       -                        *kmap = kmap_find(conf_kmapalt());
       +                        *kmap = conf_kmapalt();
                                continue;
                        case TK_CTL('e'):
       -                        *kmap = kmap_en;
       +                        *kmap = kmap_en[0];
                                continue;
                        case TK_CTL('h'):
                        case 127:
       t@@ -194,12 +194,11 @@ static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, ch
        }
        
        /* read an ex command */
       -char *led_prompt(char *pref, char *post)
       +char *led_prompt(char *pref, char *post, char **kmap)
        {
       -        char **kmap = kmap_en;
                char *s;
                int key;
       -        s = led_line(pref, post, "", 0, &key, &kmap);
       +        s = led_line(pref, post, "", 0, &key, kmap);
                if (key == '\n')
                        return s;
                free(s);
       t@@ -207,13 +206,13 @@ char *led_prompt(char *pref, char *post)
        }
        
        /* read visual command input */
       -char *led_input(char *pref, char *post, char *ai, int ai_max)
       +char *led_input(char *pref, char *post, char *ai, int ai_max, char **kmap)
        {
                struct sbuf *sb = sbuf_make();
                char *first_ai = NULL;
                int key;
                while (1) {
       -                char *ln = led_line(pref, post, ai, ai_max, &key, &led_kmap);
       +                char *ln = led_line(pref, post, ai, ai_max, &key, kmap);
                        if (pref)
                                first_ai = uc_dup(ai);
                        if (!pref)
       t@@ -221,7 +220,7 @@ char *led_input(char *pref, char *post, char *ai, int ai_max)
                        sbuf_str(sb, ln);
                        if (key == '\n')
                                sbuf_chr(sb, '\n');
       -                led_printparts(ai, pref ? pref : "", ln, key == '\n' ? "" : post);
       +                led_printparts(ai, pref ? pref : "", ln, key == '\n' ? "" : post, *kmap);
                        if (key == '\n')
                                term_chr('\n');
                        pref = NULL;
 (DIR) diff --git a/vi.c b/vi.c
       t@@ -19,6 +19,7 @@ static char vi_charlast[8];        /* the last character searched via f, t, F, or T */
        static int vi_charcmd;                /* the character finding command */
        static int vi_arg1, vi_arg2;        /* the first and second arguments */
        static int vi_ybuf;                /* current yank buffer */
       +static char *vi_kmap;                /* current insertion keymap */
        
        static void vi_drawmsg(void)
        {
       t@@ -56,14 +57,14 @@ static void vi_back(int c)
        static char *vi_char(void)
        {
                int key = vi_read();
       -        return TK_INT(key) ? NULL : led_keymap(key);
       +        return TK_INT(key) ? NULL : led_keymap(vi_kmap, key);
        }
        
       -static char *vi_prompt(char *msg)
       +static char *vi_prompt(char *msg, char **kmap)
        {
                term_pos(xrows, led_pos(msg, 0));
                term_kill();
       -        return led_prompt(msg, "");
       +        return led_prompt(msg, "", kmap);
        }
        
        char *ex_read(char *msg)
       t@@ -71,7 +72,7 @@ char *ex_read(char *msg)
                struct sbuf *sb;
                char c;
                if (xled) {
       -                char *s = led_prompt(msg, "");
       +                char *s = led_prompt(msg, "", &vi_kmap);
                        if (s)
                                term_chr('\n');
                        return s;
       t@@ -225,7 +226,7 @@ static int vi_search(int cmd, int cnt, int *row, int *col)
                char *off = "";
                if (cmd == '/' || cmd == '?') {
                        char sign[4] = {cmd};
       -                char *kw = vi_prompt(sign);
       +                char *kw = vi_prompt(sign, &vi_kmap);
                        if (!kw)
                                return 1;
                        vi_finddir = cmd == '/' ? +1 : -1;
       t@@ -685,7 +686,7 @@ static char *vi_input(char *pref, char *post, int *row, int *col)
                int last, off;
                if (xai)
                        pref += indentscopy(ai, pref, sizeof(ai));
       -        rep = led_input(pref, post, ai, xai ? sizeof(ai) - 1 : 0);
       +        rep = led_input(pref, post, ai, xai ? sizeof(ai) - 1 : 0, &vi_kmap);
                if (!rep)
                        return NULL;
                sb = sbuf_make();
       t@@ -741,7 +742,8 @@ static void vi_pipe(int r1, int r2)
        {
                char *text;
                char *rep;
       -        char *cmd = vi_prompt("!");
       +        char *kmap = NULL;
       +        char *cmd = vi_prompt("!", &kmap);
                if (!cmd)
                        return;
                if (r2 < r1)
       t@@ -1005,6 +1007,7 @@ static void vi(void)
        {
                int mark;
                char *ln;
       +        char *kmap = NULL;
                term_init();
                xtop = 0;
                xrow = 0;
       t@@ -1080,7 +1083,7 @@ static void vi(void)
                                        redraw = 1;
                                        break;
                                case ':':
       -                                ln = vi_prompt(":");
       +                                ln = vi_prompt(":", &kmap);
                                        if (ln && ln[0]) {
                                                ex_command(ln);
                                                redraw = 1;
 (DIR) diff --git a/vi.h b/vi.h
       t@@ -109,10 +109,10 @@ void term_commit(void);
        #define TK_ESC                (TK_CTL('['))
        
        /* line-oriented input and output */
       -char *led_prompt(char *pref, char *post);
       -char *led_input(char *pref, char *post, char *ai, int ai_max);
       +char *led_prompt(char *pref, char *post, char **kmap);
       +char *led_input(char *pref, char *post, char *ai, int ai_max, char **kmap);
        void led_print(char *msg, int row);
       -char *led_keymap(int c);
       +char *led_keymap(char *kmap, int c);
        int led_pos(char *s, int pos);
        
        /* ex commands */