10 int xrow
, xoff
, xtop
; /* current row, column, and top row */
11 int xleft
; /* the first visible column */
12 int xquit
; /* exit if set */
13 int xvis
; /* visual mode */
14 int xai
= 1; /* autoindent option */
15 int xic
= 1; /* ignorecase option */
16 int xaw
; /* autowrite option */
17 int xhl
= 1; /* syntax highlight option */
18 int xled
= 1; /* use the line editor */
19 int xdir
= +1; /* current direction context */
20 int xshape
= 1; /* perform letter shaping */
21 int xorder
= 1; /* change the order of characters */
22 static char xkwd
[EXLEN
]; /* the last searched keyword */
23 static char xrep
[EXLEN
]; /* the last replacement */
24 static int xkwddir
; /* the last search direction */
25 static char *xkmap
= "en"; /* the current keymap */
26 static char xkmap2
[8] = "fa"; /* the alternate keymap */
33 long mtime
; /* modification time */
36 static int bufs_find(char *path
)
39 for (i
= 0; i
< LEN(bufs
); i
++)
40 if (bufs
[i
].lb
&& !strcmp(bufs
[i
].path
, path
))
45 static void bufs_free(int idx
)
49 lbuf_free(bufs
[idx
].lb
);
50 memset(&bufs
[idx
], 0, sizeof(bufs
[idx
]));
54 static long mtime(char *path
)
62 static int bufs_open(char *path
)
65 for (i
= 0; i
< LEN(bufs
) - 1; i
++)
69 bufs
[i
].path
= uc_dup(path
);
70 bufs
[i
].lb
= lbuf_make();
75 strcpy(bufs
[i
].ft
, syn_filetype(path
));
79 static void bufs_swap(int i
, int j
)
84 memcpy(&tmp
, &bufs
[i
], sizeof(tmp
));
85 memcpy(&bufs
[i
], &bufs
[j
], sizeof(tmp
));
86 memcpy(&bufs
[j
], &tmp
, sizeof(tmp
));
89 static void bufs_switch(int idx
)
104 struct lbuf
*ex_lbuf(void)
109 char *ex_filetype(void)
111 return xhl
? bufs
[0].ft
: "";
119 char *ex_kmapalt(void)
124 /* read ex command location */
125 static char *ex_loc(char *s
, char *loc
)
127 while (*s
== ':' || isspace((unsigned char) *s
))
129 while (*s
&& !isalpha((unsigned char) *s
) && *s
!= '=' && *s
!= '!') {
132 if (*s
== '/' || *s
== '?') {
135 while (*s
&& *s
!= d
) {
136 if (*s
== '\\' && s
[1])
148 /* read ex command name */
149 static char *ex_cmd(char *s
, char *cmd
)
153 while (isspace((unsigned char) *s
))
155 while (isalpha((unsigned char) *s
))
156 if ((*cmd
++ = *s
++) == 'k' && cmd
== cmd0
+ 1)
158 if (*s
== '!' || *s
== '=')
164 /* read ex command argument */
165 static char *ex_arg(char *s
, char *arg
)
168 while (isspace((unsigned char) *s
))
170 while (*s
&& !isspace((unsigned char) *s
)) {
171 if (*s
== '\\' && s
[1])
179 static char *ex_argeol(char *ec
)
182 char *s
= ex_cmd(ec
, arg
);
183 while (isspace((unsigned char) *s
))
188 /* the previous search keyword */
189 int ex_kwd(char **kwd
, int *dir
)
198 /* set the previous search keyword */
199 void ex_kwdset(char *kwd
, int dir
)
202 snprintf(xkwd
, sizeof(xkwd
), "%s", kwd
);
203 reg_put('/', kwd
, 0);
208 static int ex_search(char **pat
)
220 sbuf_chr(kw
, (unsigned char) *e
);
221 if (*e
== '\\' && e
[1])
225 ex_kwdset(sbuf_buf(kw
), **pat
== '/' ? 1 : -1);
227 *pat
= *e
? e
+ 1 : e
;
228 if (ex_kwd(&pats
[0], &dir
))
230 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
234 while (row
>= 0 && row
< lbuf_len(xb
)) {
235 if (rset_find(re
, lbuf_get(xb
, row
), 0, NULL
, 0) >= 0)
240 return row
>= 0 && row
< lbuf_len(xb
) ? row
: -1;
243 static int ex_lineno(char **num
)
246 switch ((unsigned char) **num
) {
251 n
= lbuf_len(xb
) - 1;
255 if (lbuf_jump(xb
, (unsigned char) *++(*num
), &n
, NULL
))
264 if (isdigit((unsigned char) **num
)) {
266 while (isdigit((unsigned char) **num
))
270 while (**num
== '-' || **num
== '+') {
272 while (isdigit((unsigned char) **num
))
278 /* parse ex command location */
279 static int ex_region(char *loc
, int *beg
, int *end
)
282 if (!strcmp("%", loc
)) {
284 *end
= MAX(0, lbuf_len(xb
));
289 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
294 *end
= ex_lineno(&loc
) + 1;
295 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
298 while (*loc
&& *loc
!= ';' && *loc
!= ',')
306 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
308 if (*end
< *beg
|| *end
> lbuf_len(xb
))
313 static int ec_write(char *ec
);
315 static int ex_modifiedbuffer(char *msg
)
317 if (!lbuf_modified(xb
))
319 if (xaw
&& ex_path()[0])
320 return ec_write("w");
326 static int ec_quit(char *ec
)
330 if (!strchr(cmd
, '!'))
331 if (ex_modifiedbuffer("buffer modified\n"))
337 static int ex_expand(char *d
, char *s
)
340 int c
= (unsigned char) *s
++;
342 if (!bufs
[0].path
|| !bufs
[0].path
[0]) {
343 ex_show("\"%\" is unset\n");
346 strcpy(d
, bufs
[0].path
);
351 if (!bufs
[1].path
|| !bufs
[1].path
[0]) {
352 ex_show("\"#\" is unset\n");
355 strcpy(d
, bufs
[1].path
);
359 if (c
== '\\' && (*s
== '%' || *s
== '#'))
367 static int ec_edit(char *ec
)
370 char arg
[EXLEN
], cmd
[EXLEN
];
375 if (!strchr(cmd
, '!'))
376 if (xb
&& ex_modifiedbuffer("buffer modified\n"))
378 if (ex_expand(path
, arg
))
383 if (arg
[0] && bufs_find(path
) >= 0) {
384 bufs_switch(bufs_find(path
));
387 if (path
[0] || !bufs
[0].path
)
388 bufs_switch(bufs_open(path
));
389 fd
= open(ex_path(), O_RDONLY
);
391 int rd
= lbuf_rd(xb
, fd
, 0, lbuf_len(xb
));
393 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
394 ex_path(), lbuf_len(xb
));
396 ex_show("read failed\n");
400 lbuf_saved(xb
, path
[0] != '\0');
401 bufs
[0].mtime
= mtime(ex_path());
402 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
404 xtop
= MAX(0, MIN(xtop
, lbuf_len(xb
) - 1));
408 static int ec_read(char *ec
)
410 char arg
[EXLEN
], loc
[EXLEN
];
415 int n
= lbuf_len(xb
);
418 path
= arg
[0] ? arg
: ex_path();
419 if (ex_region(loc
, &beg
, &end
))
422 int pos
= MIN(xrow
+ 1, lbuf_len(xb
));
423 if (ex_expand(arg
, ex_argeol(ec
)))
425 obuf
= cmd_pipe(arg
+ 1, NULL
, 0, 1);
427 lbuf_edit(xb
, obuf
, pos
, pos
);
430 int fd
= open(path
, O_RDONLY
);
431 int pos
= lbuf_len(xb
) ? end
: 0;
433 ex_show("read failed\n");
436 if (lbuf_rd(xb
, fd
, pos
, pos
)) {
437 ex_show("read failed\n");
443 xrow
= end
+ lbuf_len(xb
) - n
- 1;
444 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
445 path
, lbuf_len(xb
) - n
);
450 static int ec_write(char *ec
)
452 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
460 path
= arg
[0] ? arg
: ex_path();
461 if (cmd
[0] == 'x' && !lbuf_modified(xb
))
463 if (ex_region(loc
, &beg
, &end
))
470 if (ex_expand(arg
, ex_argeol(ec
)))
472 ibuf
= lbuf_cp(xb
, beg
, end
);
474 cmd_pipe(arg
+ 1, ibuf
, 1, 0);
478 if (!strchr(cmd
, '!') && bufs
[0].path
&&
479 !strcmp(bufs
[0].path
, path
) &&
480 mtime(bufs
[0].path
) > bufs
[0].mtime
) {
481 ex_show("write failed: file changed\n");
484 if (!strchr(cmd
, '!') && arg
[0] && mtime(arg
) >= 0) {
485 ex_show("write failed: file exists\n");
488 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
490 ex_show("write failed: cannot create file\n");
493 if (lbuf_wr(xb
, fd
, beg
, end
)) {
494 ex_show("write failed\n");
500 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
505 bufs
[0].path
= uc_dup(path
);
507 if (!strcmp(ex_path(), path
))
509 if (!strcmp(ex_path(), path
))
510 bufs
[0].mtime
= mtime(path
);
511 if (cmd
[0] == 'x' || (cmd
[0] == 'w' && cmd
[1] == 'q'))
516 static int ec_insert(char *ec
)
518 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
526 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
529 while ((s
= ex_read(""))) {
530 if (!strcmp(".", s
)) {
539 if (beg
+ 1 <= lbuf_len(xb
))
544 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
545 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
550 static int ec_print(char *ec
)
552 char cmd
[EXLEN
], loc
[EXLEN
];
557 if (!cmd
[0] && !loc
[0]) {
558 if (xrow
>= lbuf_len(xb
) - 1)
562 if (ex_region(loc
, &beg
, &end
))
564 for (i
= beg
; i
< end
; i
++)
565 ex_print(lbuf_get(xb
, i
));
571 static int ec_null(char *ec
)
578 if (ex_region(loc
, &beg
, &end
))
580 xrow
= MAX(beg
, end
- 1);
585 static void ex_yank(int reg
, int beg
, int end
)
587 char *buf
= lbuf_cp(xb
, beg
, end
);
588 reg_put(reg
, buf
, 1);
592 static int ec_delete(char *ec
)
599 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
601 ex_yank(arg
[0], beg
, end
);
602 lbuf_edit(xb
, NULL
, beg
, end
);
607 static int ec_yank(char *ec
)
614 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
616 ex_yank(arg
[0], beg
, end
);
620 static int ec_put(char *ec
)
627 int n
= lbuf_len(xb
);
630 buf
= reg_get(arg
[0], &lnmode
);
631 if (!buf
|| ex_region(loc
, &beg
, &end
))
633 lbuf_edit(xb
, buf
, end
, end
);
634 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
638 static int ec_lnum(char *ec
)
644 if (ex_region(loc
, &beg
, &end
))
646 sprintf(msg
, "%d\n", end
);
651 static int ec_undo(char *ec
)
653 return lbuf_undo(xb
);
656 static int ec_redo(char *ec
)
658 return lbuf_redo(xb
);
661 static int ec_mark(char *ec
)
663 char loc
[EXLEN
], arg
[EXLEN
];
667 if (ex_region(loc
, &beg
, &end
))
669 lbuf_mark(xb
, arg
[0], end
- 1, 0);
673 static void replace(struct sbuf
*dst
, char *rep
, char *ln
, int *offs
)
676 if (rep
[0] == '\\' && rep
[1]) {
677 if (rep
[1] >= '0' && rep
[1] <= '9') {
678 int grp
= (rep
[1] - '0') * 2;
679 int len
= offs
[grp
+ 1] - offs
[grp
];
680 sbuf_mem(dst
, ln
+ offs
[grp
], len
);
682 sbuf_chr(dst
, (unsigned char) rep
[1]);
686 sbuf_chr(dst
, (unsigned char) rep
[0]);
692 static int ec_substitute(char *ec
)
699 char *pat
= NULL
, *rep
= NULL
;
703 if (ex_region(loc
, &beg
, &end
))
714 rep
= uc_dup(pat
? "" : xrep
);
715 snprintf(xrep
, sizeof(xrep
), "%s", rep
);
717 if (ex_kwd(&pats
[0], NULL
))
719 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
724 for (i
= beg
; i
< end
; i
++) {
725 char *ln
= lbuf_get(xb
, i
);
726 struct sbuf
*r
= sbuf_make();
727 while (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) >= 0) {
728 sbuf_mem(r
, ln
, offs
[0]);
729 replace(r
, rep
, ln
, offs
);
735 lbuf_edit(xb
, sbuf_buf(r
), i
, i
+ 1);
743 static int ec_exec(char *ec
)
746 ex_modifiedbuffer(NULL
);
747 if (ex_expand(arg
, ex_argeol(ec
)))
755 static int ec_make(char *ec
)
759 ex_modifiedbuffer(NULL
);
760 if (ex_expand(arg
, ex_argeol(ec
)))
762 sprintf(make
, "make %s", arg
);
769 static int ec_ft(char *ec
)
774 snprintf(bufs
[0].ft
, sizeof(bufs
[0].ft
), arg
);
776 ex_print(ex_filetype());
780 static int ec_cmap(char *ec
)
787 snprintf(xkmap2
, sizeof(xkmap2
), arg
);
790 if (arg
[0] && !strchr(cmd
, '!'))
795 static int ex_exec(char *ln
);
797 static int ec_glob(char *ec
)
799 char loc
[EXLEN
], cmd
[EXLEN
];
808 if (ex_region(loc
, &beg
, &end
))
810 not = strchr(cmd
, '!') || cmd
[0] == 'v';
816 if (ex_kwd(&pats
[0], NULL
))
818 if (!(re
= rset_make(1, pats
, xic
? RE_ICASE
: 0)))
820 for (i
= beg
+ 1; i
< end
; i
++)
823 while (i
< lbuf_len(xb
)) {
824 char *ln
= lbuf_get(xb
, i
);
825 if ((rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) < 0) == not) {
831 while (i
< lbuf_len(xb
) && !lbuf_glob(xb
, i
, 0))
834 for (i
= 0; i
< lbuf_len(xb
); i
++)
840 static struct option
{
845 {"ai", "autoindent", &xai
},
846 {"aw", "autowrite", &xaw
},
847 {"ic", "ignorecase", &xic
},
848 {"td", "textdirection", &xdir
},
849 {"shape", "shape", &xshape
},
850 {"order", "xorder", &xorder
},
851 {"hl", "highlight", &xhl
},
854 static char *cutword(char *s
, char *d
)
858 while (*s
&& !isspace(*s
))
866 static int ec_set(char *ec
)
877 if (tok
[0] == 'n' && tok
[1] == 'o') {
878 strcpy(opt
, tok
+ 2);
881 char *r
= strchr(tok
, '=');
891 for (i
= 0; i
< LEN(options
); i
++) {
892 struct option
*o
= &options
[i
];
893 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
)) {
898 ex_show("unknown option");
904 static struct excmd
{
909 {"p", "print", ec_print
},
910 {"a", "append", ec_insert
},
911 {"i", "insert", ec_insert
},
912 {"d", "delete", ec_delete
},
913 {"c", "change", ec_insert
},
914 {"e", "edit", ec_edit
},
915 {"e!", "edit!", ec_edit
},
916 {"g", "global", ec_glob
},
917 {"g!", "global!", ec_glob
},
919 {"k", "mark", ec_mark
},
920 {"pu", "put", ec_put
},
921 {"q", "quit", ec_quit
},
922 {"q!", "quit!", ec_quit
},
923 {"r", "read", ec_read
},
924 {"v", "vglobal", ec_glob
},
925 {"w", "write", ec_write
},
926 {"w!", "write!", ec_write
},
927 {"wq", "wq", ec_write
},
928 {"wq!", "wq!", ec_write
},
929 {"u", "undo", ec_undo
},
930 {"r", "redo", ec_redo
},
931 {"se", "set", ec_set
},
932 {"s", "substitute", ec_substitute
},
933 {"x", "xit", ec_write
},
934 {"x!", "xit!", ec_write
},
935 {"ya", "yank", ec_yank
},
937 {"make", "make", ec_make
},
938 {"ft", "filetype", ec_ft
},
939 {"cm", "cmap", ec_cmap
},
940 {"cm!", "cmap!", ec_cmap
},
944 /* read an ex command and its arguments from src into dst */
945 static void ex_line(int (*ec
)(char *s
), char *dst
, char **src
)
947 if (!ec
|| ec
!= ec_glob
) {
948 while (**src
&& **src
!= '|' && **src
!= '\n')
953 } else { /* the rest of the line for :g */
955 *src
= strchr(*src
, '\0');
959 /* execute a single ex command */
960 static int ex_exec(char *ln
)
968 for (i
= 0; i
< LEN(excmds
); i
++) {
969 if (!strcmp(excmds
[i
].abbr
, cmd
) ||
970 !strcmp(excmds
[i
].name
, cmd
)) {
971 ex_line(excmds
[i
].ec
, ec
, &ln
);
972 ret
= excmds
[i
].ec(ec
);
976 if (!xvis
&& !cmd
[0])
978 if (i
== LEN(excmds
))
979 ex_line(NULL
, ec
, &ln
);
984 /* execute a single ex command */
985 void ex_command(char *ln
)
996 char *ln
= ex_read(":");
1003 int ex_init(char **files
)
1007 char *r
= files
[0] ? files
[0] : "";
1010 while (*r
&& s
+ 2 < cmd
+ sizeof(cmd
)) {
1018 if (getenv("EXINIT"))
1019 ex_command(getenv("EXINIT"));
1026 for (i
= 0; i
< LEN(bufs
); i
++)