tled: ^C should act like escape in input mode - 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 dff850c67acc8216c6df2a6fe8c4eaf081abb485
 (DIR) parent d5fdad85ce7861cb976204da853df15ad14b5a38
 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
       Date:   Wed, 13 May 2015 10:50:13 +0430
       
       led: ^C should act like escape in input mode
       
       Diffstat:
         M led.c                               |      20 +++++++++++---------
         M term.c                              |       2 +-
         M vi.c                                |      21 ++++++++++++---------
         M vi.h                                |       4 ++--
       
       4 files changed, 26 insertions(+), 21 deletions(-)
       ---
 (DIR) diff --git a/led.c b/led.c
       t@@ -5,6 +5,8 @@
        #include "vi.h"
        #include "kmap.h"
        
       +#define TK_STOP(c)                ((c) < 0 || (c) == TK_ESC || (c) == TK_CTL('c'))
       +
        static char **led_kmap = kmap_def;
        
        static char *keymap(char **kmap, int c)
       t@@ -126,33 +128,33 @@ static char *led_line(char *pref, char *post, int *key, char ***kmap)
                        led_printparts(pref, sbuf_buf(sb), post);
                        c = term_read(-1);
                        switch (c) {
       -                case TERMCTRL('f'):
       +                case TK_CTL('f'):
                                *kmap = kmap_farsi;
                                continue;
       -                case TERMCTRL('e'):
       +                case TK_CTL('e'):
                                *kmap = kmap_def;
                                continue;
       -                case TERMCTRL('h'):
       +                case TK_CTL('h'):
                        case 127:
                                if (sbuf_len(sb))
                                        sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
                                break;
       -                case TERMCTRL('u'):
       +                case TK_CTL('u'):
                                sbuf_cut(sb, 0);
                                break;
       -                case TERMCTRL('v'):
       +                case TK_CTL('v'):
                                sbuf_chr(sb, term_read(-1));
                                break;
       -                case TERMCTRL('w'):
       +                case TK_CTL('w'):
                                if (sbuf_len(sb))
                                        sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
                                break;
                        default:
       -                        if (c == '\n' || c == TERMESC || c < 0)
       +                        if (c == '\n' || TK_STOP(c))
                                        break;
                                sbuf_str(sb, keymap(*kmap, c));
                        }
       -                if (c == '\n' || c == TERMESC || c < 0)
       +                if (c == '\n' || TK_STOP(c))
                                break;
                }
                *key = c;
       t@@ -191,7 +193,7 @@ char *led_input(char *pref, char *post)
                        if (key != '\n')
                                break;
                }
       -        if (key == TERMESC)
       +        if (TK_STOP(key))
                        return sbuf_done(sb);
                sbuf_free(sb);
                return NULL;
 (DIR) diff --git a/term.c b/term.c
       t@@ -17,7 +17,7 @@ void term_init(void)
                struct termios newtermios;
                tcgetattr(0, &termios);
                newtermios = termios;
       -        newtermios.c_lflag &= ~ICANON;
       +        newtermios.c_lflag &= ~(ICANON | ISIG);
                newtermios.c_lflag &= ~ECHO;
                tcsetattr(0, TCSAFLUSH, &newtermios);
                if (getenv("LINES"))
 (DIR) diff --git a/vi.c b/vi.c
       t@@ -342,7 +342,7 @@ static int vi_motion(int *row, int *col, int pre1, int pre2)
                        *col = pre - 1;
                        break;
                case 127:
       -        case TERMCTRL('h'):
       +        case TK_CTL('h'):
                        for (i = 0; i < pre; i++)
                                if (lbuf_lnnext(xb, row, col, -1))
                                        break;
       t@@ -482,8 +482,11 @@ static int linecount(char *s)
        static char *vi_input(char *pref, char *post, int *row, int *col)
        {
                char *rep = led_input(pref, post);
       -        struct sbuf *sb = sbuf_make();
       +        struct sbuf *sb;
                int last, off;
       +        if (!rep)
       +                return NULL;
       +        sb = sbuf_make();
                sbuf_str(sb, pref);
                sbuf_str(sb, rep);
                last = lastline(sbuf_buf(sb));
       t@@ -717,33 +720,33 @@ static void vi(void)
                                        lbuf_undo(xb);
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('b'):
       +                        case TK_CTL('b'):
                                        if (vi_scrollbackward((pre1 ? pre1 : 1) * (xrows - 1)))
                                                break;
                                        lbuf_postindents(xb, &xrow, &xcol);
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('f'):
       +                        case TK_CTL('f'):
                                        if (vi_scrollforeward((pre1 ? pre1 : 1) * (xrows - 1)))
                                                break;
                                        lbuf_postindents(xb, &xrow, &xcol);
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('e'):
       +                        case TK_CTL('e'):
                                        if (vi_scrollforeward((pre1 ? pre1 : 1)))
                                                break;
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('y'):
       +                        case TK_CTL('y'):
                                        if (vi_scrollbackward((pre1 ? pre1 : 1)))
                                                break;
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('r'):
       +                        case TK_CTL('r'):
                                        lbuf_redo(xb);
                                        redraw = 1;
                                        break;
       -                        case TERMCTRL('g'):
       +                        case TK_CTL('g'):
                                        vi_status();
                                        break;
                                case ':':
       t@@ -813,7 +816,7 @@ static void vi(void)
                                        redraw = 1;
                                        break;
                                case 'X':
       -                                vi_back(TERMCTRL('h'));
       +                                vi_back(TK_CTL('h'));
                                        vc_motion('d', pre1);
                                        redraw = 1;
                                        break;
 (DIR) diff --git a/vi.h b/vi.h
       t@@ -99,8 +99,8 @@ int term_read(int timeout);
        void term_record(void);
        void term_commit(void);
        
       -#define TERMCTRL(x)        ((x) - 96)
       -#define TERMESC                27
       +#define TK_CTL(x)        ((x) - 96)
       +#define TK_ESC                27
        
        /* line-oriented input and output */
        char *led_prompt(char *pref, char *post);