tex: ic and ai options - 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 84c242d464735708949dc486a36af9ef16853a70
 (DIR) parent 69f88a475e1d3633b0f944a1917ba7cc74da823a
 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
       Date:   Mon, 18 May 2015 23:04:59 +0430
       
       ex: ic and ai options
       
       Diffstat:
         M ex.c                                |      71 ++++++++++++++++++++++++++++++-
         M vi.c                                |      34 +++++++++++--------------------
         M vi.h                                |       2 ++
       
       3 files changed, 83 insertions(+), 24 deletions(-)
       ---
 (DIR) diff --git a/ex.c b/ex.c
       t@@ -9,6 +9,18 @@
        
        #define EXLEN                512
        
       +char xpath[PATHLEN];                /* current file */
       +char xpath_alt[PATHLEN];        /* alternate file */
       +int xquit;                        /* exit if set */
       +int xvis;                        /* visual mode */
       +int xai = 1;                        /* autoindent option */
       +int xic = 1;                        /* ignorecase option */
       +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 */
       +
        /* read ex command location */
        static char *ex_loc(char *s, char *loc)
        {
       t@@ -76,7 +88,7 @@ static int ex_search(char *pat)
                                e++;
                }
                re_kw[0] = sbuf_buf(kw);
       -        re = rset_make(1, re_kw, 0);
       +        re = rset_make(1, re_kw, xic ? RE_ICASE : 0);
                sbuf_free(kw);
                if (!re)
                        return i;
       t@@ -409,7 +421,7 @@ static void ec_substitute(char *ec)
                delim = (unsigned char) *s++;
                pat = readuntil(&s, delim);
                rep = readuntil(&s, delim);
       -        re = rset_make(1, &pat, 0);
       +        re = rset_make(1, &pat, xic ? RE_ICASE : 0);
                for (i = beg; i < end; i++) {
                        char *ln = lbuf_get(xb, i);
                        if (rset_find(re, ln, LEN(offs) / 2, offs, 0)) {
       t@@ -427,6 +439,60 @@ static void ec_substitute(char *ec)
                free(rep);
        }
        
       +static struct option {
       +        char *abbr;
       +        char *name;
       +        int *var;
       +} options[] = {
       +        {"ai", "autoindent", &xai},
       +        {"ic", "ignorecase", &xic},
       +};
       +
       +static char *cutword(char *s, char *d)
       +{
       +        while (isspace(*s))
       +                s++;
       +        while (*s && !isspace(*s))
       +                *d++ = *s++;
       +        while (isspace(*s))
       +                s++;
       +        *d = '\0';
       +        return s;
       +}
       +
       +static void ec_set(char *ec)
       +{
       +        char arg[EXLEN];
       +        char tok[EXLEN];
       +        char opt[EXLEN];
       +        char *s = arg;
       +        int val = 0;
       +        int i;
       +        ex_arg(ec, arg);
       +        if (*s) {
       +                s = cutword(s, tok);
       +                if (tok[0] == 'n' && tok[1] == 'o') {
       +                        strcpy(opt, tok + 2);
       +                        val = 0;
       +                } else {
       +                        char *r = strchr(tok, '=');
       +                        if (r) {
       +                                *r = '\0';
       +                                strcpy(opt, tok);
       +                                val = atoi(r + 1);
       +                        } else {
       +                                strcpy(opt, tok);
       +                                val = 1;
       +                        }
       +                }
       +                for (i = 0; i < LEN(options); i++) {
       +                        struct option *o = &options[i];
       +                        if (!strcmp(o->abbr, opt) || !strcmp(o->name, opt))
       +                                *o->var = val;
       +                }
       +        }
       +}
       +
        static struct excmd {
                char *abbr;
                char *name;
       t@@ -447,6 +513,7 @@ static struct excmd {
                {"wq", "wq", ec_write},
                {"u", "undo", ec_undo},
                {"r", "redo", ec_redo},
       +        {"se", "set", ec_set},
                {"s", "substitute", ec_substitute},
                {"ya", "yank", ec_yank},
                {"", "", ec_print},
 (DIR) diff --git a/vi.c b/vi.c
       t@@ -12,17 +12,7 @@
        #include <string.h>
        #include "vi.h"
        
       -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 */
       -int xled = 1;                        /* use the line editor */
       -int xdir = 'L';                        /* current direction context */
       -int xvis;                        /* visual mode */
       -int xquit;
       -int xautoindent = 1;
       +char vi_msg[512];                /* current message */
        static char vi_findlast[256];        /* the last searched keyword */
        static int vi_finddir;                /* the last search direction */
        static char vi_charlast[8];        /* the last character searched via f, t, F, or T */
       t@@ -32,8 +22,8 @@ static int vi_ybuf;                /* current yank buffer */
        
        static void vi_drawmsg(void)
        {
       -        led_print(xmsg, xrows);
       -        xmsg[0] = '\0';
       +        led_print(vi_msg, xrows);
       +        vi_msg[0] = '\0';
        }
        
        static void vi_draw(void)
       t@@ -99,7 +89,7 @@ char *ex_read(char *msg)
        void ex_show(char *msg)
        {
                if (xvis) {
       -                snprintf(xmsg, sizeof(xmsg), "%s", msg);
       +                snprintf(vi_msg, sizeof(vi_msg), "%s", msg);
                } else if (xled) {
                        led_print(msg, -1);
                        term_chr('\n');
       t@@ -202,7 +192,7 @@ static int lbuf_search(struct lbuf *lb, char *kw, int dir, int *r, int *c, int *
                int found = 0;
                int row = *r, col = *c;
                int i;
       -        struct rset *re = rset_make(1, &kw, 0);
       +        struct rset *re = rset_make(1, &kw, xic ? RE_ICASE : 0);
                if (!re)
                        return 1;
                for (i = row; !found && i >= 0 && i < lbuf_len(lb); i += dir) {
       t@@ -273,7 +263,7 @@ static int vi_search(int cmd, int cnt, int *row, int *col)
                        }
                }
                if (failed)
       -                snprintf(xmsg, sizeof(xmsg), "\"%s\" not found\n", vi_findlast);
       +                snprintf(vi_msg, sizeof(vi_msg), "\"%s\" not found\n", vi_findlast);
                return failed;
        }
        
       t@@ -693,9 +683,9 @@ static char *vi_input(char *pref, char *post, int *row, int *col)
                char *rep;
                struct sbuf *sb;
                int last, off;
       -        if (xautoindent)
       +        if (xai)
                        pref += indentscopy(ai, pref, sizeof(ai));
       -        rep = led_input(pref, post, ai, xautoindent ? sizeof(ai) - 1 : 0);
       +        rep = led_input(pref, post, ai, xai ? sizeof(ai) - 1 : 0);
                if (!rep)
                        return NULL;
                sb = sbuf_make();
       t@@ -705,7 +695,7 @@ static char *vi_input(char *pref, char *post, int *row, int *col)
                last = lastline(sbuf_buf(sb));
                off = uc_slen(sbuf_buf(sb) + last);
                if (last)
       -                while (xautoindent && (post[0] == ' ' || post[0] == '\t'))
       +                while (xai && (post[0] == ' ' || post[0] == '\t'))
                                post++;
                sbuf_str(sb, post);
                *row = linecount(sbuf_buf(sb)) - 1;
       t@@ -717,7 +707,7 @@ static char *vi_input(char *pref, char *post, int *row, int *col)
        static char *vi_indents(char *ln)
        {
                struct sbuf *sb = sbuf_make();
       -        while (xautoindent && ln && (*ln == ' ' || *ln == '\t'))
       +        while (xai && ln && (*ln == ' ' || *ln == '\t'))
                        sbuf_chr(sb, *ln++);
                return sbuf_done(sb);
        }
       t@@ -972,7 +962,7 @@ static int vi_scrollbackward(int cnt)
        static void vc_status(void)
        {
                int pos = ren_noeol(lbuf_get(xb, xrow), xcol);
       -        snprintf(xmsg, sizeof(xmsg), "\"%s\" line %d of %d, col %d\n",
       +        snprintf(vi_msg, sizeof(vi_msg), "\"%s\" line %d of %d, col %d\n",
                        xpath[0] ? xpath : "unnamed", xrow + 1, lbuf_len(xb),
                        ren_cursor(lbuf_get(xb, xrow), pos) + 1);
        }
       t@@ -1206,7 +1196,7 @@ static void vi(void)
                                                xrow - xrows / 2 : xrow - xrows + 1;
                        if (redraw || xtop != otop)
                                vi_draw();
       -                if (xmsg[0])
       +                if (vi_msg[0])
                                vi_drawmsg();
                        term_pos(xrow - xtop, led_pos(lbuf_get(xb, xrow),
                                        ren_cursor(lbuf_get(xb, xrow), xcol)));
 (DIR) diff --git a/vi.h b/vi.h
       t@@ -138,3 +138,5 @@ extern char xpath[];
        extern char xpath_alt[];
        extern int xquit;
        extern int xdir;
       +extern int xic;
       +extern int xai;