tex: alternate file - 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 6ec7a51894148a1051971d3b7628094f70cf2c9e
 (DIR) parent 24a8fe7457134dd4423c4700bf715077b5f26df4
 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
       Date:   Thu, 14 May 2015 17:47:47 +0430
       
       ex: alternate file
       
       Diffstat:
         M ex.c                                |      23 ++++++++++++++++++++---
         M vi.c                                |      25 ++++++++++++++++---------
         M vi.h                                |       7 +++++--
       
       3 files changed, 41 insertions(+), 14 deletions(-)
       ---
 (DIR) diff --git a/ex.c b/ex.c
       t@@ -191,15 +191,32 @@ static void ec_edit(char *ec)
                char arg[EXLEN];
                int fd;
                ex_arg(ec, arg);
       -        fd = open(arg, O_RDONLY);
       +        if (!strcmp(arg, "%") || !arg[0]) {
       +                strcpy(arg, xpath);
       +        } else if (!strcmp(arg, "#")) {
       +                char xpath_tmp[PATHLEN];
       +                int xrow_tmp = xrow;
       +                strcpy(xpath_tmp, xpath_alt);
       +                strcpy(xpath_alt, xpath);
       +                strcpy(xpath, xpath_tmp);
       +                xrow = xrow_alt;
       +                xrow_alt = xrow_tmp;
       +                xcol = 0;
       +                xtop = 0;
       +        } else {
       +                strcpy(xpath_alt, xpath);
       +                snprintf(xpath, PATHLEN, "%s", arg);
       +                xrow_alt = xrow;
       +                xrow = xvis ? 0 : 1 << 20;
       +        }
       +        fd = open(xpath, O_RDONLY);
                lbuf_rm(xb, 0, lbuf_len(xb));
                if (fd >= 0) {
                        lbuf_rd(xb, fd, 0);
                        close(fd);
                }
       -        xrow = MAX(0, lbuf_len(xb) - 1);
       +        xrow = MAX(0, MIN(xrow, lbuf_len(xb) - 1));
                lbuf_undofree(xb);
       -        snprintf(xpath, PATHLEN, "%s", arg);
        }
        
        static void ec_read(char *ec)
 (DIR) diff --git a/vi.c b/vi.c
       t@@ -12,11 +12,14 @@
        #include <string.h>
        #include "vi.h"
        
       -char xpath[PATHLEN];        /* current file */
       -struct lbuf *xb;        /* current buffer */
       -int xrow, xcol, xtop;        /* current row, column, and top row */
       -int xled = 1;                /* use the line editor */
       -int xdir = 'L';                /* current direction context */
       +char xpath[PATHLEN];                /* current file */
       +char xpath_alt[PATHLEN];        /* alternate file */
       +struct lbuf *xb;                /* current buffer */
       +int xrow, xcol, xtop;                /* current row, column, and top row */
       +int xrow_alt;                        /* alternate row, column, and top row */
       +int xled = 1;                        /* use the line editor */
       +int xdir = 'L';                        /* current direction context */
       +int xvis;                        /* visual mode */
        int xquit;
        static char vi_findlast[256];        /* the last searched keyword */
        static int vi_finddir;                /* the last search direction */
       t@@ -859,6 +862,10 @@ static void vi(void)
                                case TK_CTL('g'):
                                        vi_status();
                                        break;
       +                        case TK_CTL('^'):
       +                                ex_command("e #");
       +                                redraw = 1;
       +                                break;
                                case ':':
                                        term_pos(xrows, led_pos(":", 0));
                                        term_kill();
       t@@ -978,24 +985,24 @@ static void vi(void)
        
        int main(int argc, char *argv[])
        {
       -        int visual = 1;
                char ecmd[PATHLEN];
                int i;
                xb = lbuf_make();
       +        xvis = 1;
                for (i = 1; i < argc && argv[i][0] == '-'; i++) {
                        if (argv[i][1] == 's')
                                xled = 0;
                        if (argv[i][1] == 'e')
       -                        visual = 0;
       +                        xvis = 0;
                        if (argv[i][1] == 'v')
       -                        visual = 1;
       +                        xvis = 1;
                }
                dir_init();
                if (i < argc) {
                        snprintf(ecmd, PATHLEN, "e %s", argv[i]);
                        ex_command(ecmd);
                }
       -        if (visual)
       +        if (xvis)
                        vi();
                else
                        ex();
 (DIR) diff --git a/vi.h b/vi.h
       t@@ -104,8 +104,8 @@ int term_read(int timeout);
        void term_record(void);
        void term_commit(void);
        
       -#define TK_CTL(x)        ((x) - 96)
       -#define TK_ESC                27
       +#define TK_CTL(x)        ((x) & 037)
       +#define TK_ESC                (TK_CTL('['))
        
        /* line-oriented input and output */
        char *led_prompt(char *pref, char *post);
       t@@ -119,11 +119,14 @@ void ex(void);
        void ex_command(char *cmd);
        
        /* global variables */
       +extern int xvis;
        extern struct lbuf *xb;
        extern int xrow;
        extern int xcol;
        extern int xtop;
        extern int xled;
       +extern int xrow_alt;
        extern char xpath[];
       +extern char xpath_alt[];
        extern int xquit;
        extern int xdir;