tex: show a message after :e, :r, :w, or failed searches - 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 4135d145fd1d5329c1078e7c59693ab146d0ad39
 (DIR) parent ec086911d042a074ad149289f4200c3248446096
 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
       Date:   Sat, 16 May 2015 12:42:54 +0430
       
       ex: show a message after :e, :r, :w, or failed searches
       
       Diffstat:
         M ex.c                                |      51 +++++++++----------------------
         M vi.c                                |      57 ++++++++++++++++++++++++++-----
         M vi.h                                |       6 ++++--
       
       3 files changed, 68 insertions(+), 46 deletions(-)
       ---
 (DIR) diff --git a/ex.c b/ex.c
       t@@ -9,41 +9,6 @@
        
        #define EXLEN                512
        
       -/* read an input line; ex's input function */
       -static char *ex_read(char *msg)
       -{
       -        struct sbuf *sb;
       -        char c;
       -        if (xled) {
       -                char *s = led_prompt(msg, "");
       -                if (s)
       -                        printf("\n");
       -                return s;
       -        }
       -        sb = sbuf_make();
       -        while ((c = getchar()) != EOF) {
       -                if (c == '\n')
       -                        break;
       -                sbuf_chr(sb, c);
       -        }
       -        if (c == EOF) {
       -                sbuf_free(sb);
       -                return NULL;
       -        }
       -        return sbuf_done(sb);
       -}
       -
       -/* print an output line; ex's output function */
       -static void ex_show(char *msg)
       -{
       -        if (xled) {
       -                led_print(msg, -1);
       -                term_chr('\n');
       -        } else {
       -                printf("%s", msg);
       -        }
       -}
       -
        /* read ex command location */
        static char *ex_loc(char *s, char *loc)
        {
       t@@ -188,6 +153,7 @@ static void ec_quit(char *ec)
        
        static void ec_edit(char *ec)
        {
       +        char msg[128];
                char arg[EXLEN];
                int fd;
                ex_arg(ec, arg);
       t@@ -214,6 +180,9 @@ static void ec_edit(char *ec)
                if (fd >= 0) {
                        lbuf_rd(xb, fd, 0);
                        close(fd);
       +                snprintf(msg, sizeof(msg), "\"%s\" %d lines [r]\n",
       +                                xpath, lbuf_len(xb));
       +                ex_show(msg);
                }
                xrow = MAX(0, MIN(xrow, lbuf_len(xb) - 1));
                lbuf_undofree(xb);
       t@@ -222,22 +191,29 @@ static void ec_edit(char *ec)
        static void ec_read(char *ec)
        {
                char arg[EXLEN], loc[EXLEN];
       +        char msg[128];
       +        char *path;
                int fd;
                int beg, end;
                int n = lbuf_len(xb);
                ex_arg(ec, arg);
                ex_loc(ec, loc);
       -        fd = open(arg[0] ? arg : xpath, O_RDONLY);
       +        path = arg[0] ? arg : xpath;
       +        fd = open(path, O_RDONLY);
                if (fd >= 0 && !ex_region(loc, &beg, &end)) {
                        lbuf_rd(xb, fd, lbuf_len(xb) ? end : 0);
                        close(fd);
                        xrow = end + lbuf_len(xb) - n;
       +                snprintf(msg, sizeof(msg), "\"%s\" %d lines [r]\n",
       +                                path, lbuf_len(xb) - n);
       +                ex_show(msg);
                }
        }
        
        static void ec_write(char *ec)
        {
                char cmd[EXLEN], arg[EXLEN], loc[EXLEN];
       +        char msg[128];
                char *path;
                int beg, end;
                int fd;
       t@@ -255,6 +231,9 @@ static void ec_write(char *ec)
                if (fd >= 0) {
                        lbuf_wr(xb, fd, beg, end);
                        close(fd);
       +                snprintf(msg, sizeof(msg), "\"%s\" %d lines [w]\n",
       +                                path, end - beg);
       +                ex_show(msg);
                }
                if (!strcmp("wq", cmd))
                        ec_quit("wq");
 (DIR) diff --git a/vi.c b/vi.c
       t@@ -14,6 +14,7 @@
        
        char xpath[PATHLEN];                /* current file */
        char xpath_alt[PATHLEN];        /* alternate file */
       +char xmsg[512];                        /* current message */
        struct lbuf *xb;                /* current buffer */
        int xrow, xcol, xtop;                /* current row, column, and top row */
        int xrow_alt;                        /* alternate row, column, and top row */
       t@@ -29,6 +30,12 @@ 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 void vi_drawmsg(void)
       +{
       +        led_print(xmsg, xrows);
       +        xmsg[0] = '\0';
       +}
       +
        static void vi_draw(void)
        {
                int i;
       t@@ -37,7 +44,7 @@ static void vi_draw(void)
                        char *s = lbuf_get(xb, i);
                        led_print(s ? s : "~", i - xtop);
                }
       -        led_print("", xrows);
       +        vi_drawmsg();
                term_pos(xrow, led_pos(lbuf_get(xb, i), xcol));
                term_commit();
        }
       t@@ -69,6 +76,38 @@ static char *vi_prompt(char *msg)
                return led_prompt(msg, "");
        }
        
       +char *ex_read(char *msg)
       +{
       +        struct sbuf *sb;
       +        char c;
       +        if (xled) {
       +                char *s = led_prompt(msg, "");
       +                if (s)
       +                        term_chr('\n');
       +                return s;
       +        }
       +        sb = sbuf_make();
       +        while ((c = getchar()) != EOF && c != '\n')
       +                sbuf_chr(sb, c);
       +        if (c == EOF) {
       +                sbuf_free(sb);
       +                return NULL;
       +        }
       +        return sbuf_done(sb);
       +}
       +
       +void ex_show(char *msg)
       +{
       +        if (xvis) {
       +                snprintf(xmsg, sizeof(xmsg), "%s", msg);
       +        } else if (xled) {
       +                led_print(msg, -1);
       +                term_chr('\n');
       +        } else {
       +                printf("%s", msg);
       +        }
       +}
       +
        static int vi_yankbuf(void)
        {
                int c = vi_read();
       t@@ -233,6 +272,8 @@ static int vi_search(int cmd, int cnt, int *row, int *col)
                                        *row += atoi(off);
                        }
                }
       +        if (failed)
       +                snprintf(xmsg, sizeof(xmsg), "\"%s\" not found\n", vi_findlast);
                return failed;
        }
        
       t@@ -920,12 +961,10 @@ static int vi_scrollbackward(int cnt)
                return 0;
        }
        
       -static void vi_status(void)
       +static void vc_status(void)
        {
       -        char stat[128];
       -        sprintf(stat, "[%s] %d lines, %d,%d\n",
       -                xpath[0] ? xpath : "unnamed", lbuf_len(xb), xrow + 1, xcol + 1);
       -        led_print(stat, xrows);
       +        snprintf(xmsg, sizeof(xmsg), "\"%s\" line %d of %d, col %d\n",
       +                xpath[0] ? xpath : "unnamed", xrow + 1, lbuf_len(xb), xcol + 1);
        }
        
        static int vc_replace(void)
       t@@ -1029,7 +1068,7 @@ static void vi(void)
                                        redraw = 1;
                                        break;
                                case TK_CTL('g'):
       -                                vi_status();
       +                                vc_status();
                                        break;
                                case TK_CTL('^'):
                                        ex_command("e #");
       t@@ -1116,7 +1155,7 @@ static void vi(void)
                                        break;
                                case 'D':
                                        vi_back('$');
       -                                if (vc_motion('d'))
       +                                if (!vc_motion('d'))
                                                redraw = 1;
                                        break;
                                case 'r':
       t@@ -1150,6 +1189,8 @@ static void vi(void)
                        }
                        if (redraw)
                                vi_draw();
       +                if (xmsg[0])
       +                        vi_drawmsg();
                        term_pos(xrow - xtop, led_pos(lbuf_get(xb, xrow),
                                        ren_cursor(lbuf_get(xb, xrow), xcol)));
                        lbuf_undomark(xb);
 (DIR) diff --git a/vi.h b/vi.h
       t@@ -1,7 +1,5 @@
        /* neatvi main header */
        
       -#define PATHLEN                512
       -
        /* helper macros */
        #define LEN(a)                (sizeof(a) / sizeof((a)[0]))
        #define MIN(a, b)        ((a) < (b) ? (a) : (b))
       t@@ -118,11 +116,15 @@ int led_pos(char *s, int pos);
        /* ex commands */
        void ex(void);
        void ex_command(char *cmd);
       +char *ex_read(char *msg);
       +void ex_show(char *msg);
        
        /* process management */
        char *cmd_pipe(char *cmd, char *s);
        
        /* global variables */
       +#define PATHLEN                512
       +
        extern int xvis;
        extern struct lbuf *xb;
        extern int xrow;