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 xtd
= +1; /* current text direction */
20 int xshape
= 1; /* perform letter shaping */
21 int xorder
= 1; /* change the order of characters */
22 int xkmap
= 0; /* the current keymap */
23 int xkmap_alt
= 1; /* the alternate keymap */
24 static char xkwd
[EXLEN
]; /* the last searched keyword */
25 static char xrep
[EXLEN
]; /* the last replacement */
26 static int xkwddir
; /* the last search direction */
27 static int xgdep
; /* global command recursion depth */
34 long mtime
; /* modification time */
37 static int bufs_find(char *path
)
40 for (i
= 0; i
< LEN(bufs
); i
++)
41 if (bufs
[i
].lb
&& !strcmp(bufs
[i
].path
, path
))
46 static void bufs_free(int idx
)
50 lbuf_free(bufs
[idx
].lb
);
51 memset(&bufs
[idx
], 0, sizeof(bufs
[idx
]));
55 static long mtime(char *path
)
63 static int bufs_open(char *path
)
66 for (i
= 0; i
< LEN(bufs
) - 1; i
++)
70 bufs
[i
].path
= uc_dup(path
);
71 bufs
[i
].lb
= lbuf_make();
76 strcpy(bufs
[i
].ft
, syn_filetype(path
));
80 static void bufs_swap(int i
, int j
)
85 memcpy(&tmp
, &bufs
[i
], sizeof(tmp
));
86 memcpy(&bufs
[i
], &bufs
[j
], sizeof(tmp
));
87 memcpy(&bufs
[j
], &tmp
, sizeof(tmp
));
90 static void bufs_switch(int idx
)
105 struct lbuf
*ex_lbuf(void)
110 char *ex_filetype(void)
112 return xhl
? bufs
[0].ft
: "";
115 /* read ex command location */
116 static char *ex_loc(char *s
, char *loc
)
118 while (*s
== ':' || isspace((unsigned char) *s
))
120 while (*s
&& !isalpha((unsigned char) *s
) && *s
!= '=' && *s
!= '!') {
123 if (*s
== '/' || *s
== '?') {
126 while (*s
&& *s
!= d
) {
127 if (*s
== '\\' && s
[1])
139 /* read ex command name */
140 static char *ex_cmd(char *s
, char *cmd
)
144 while (isspace((unsigned char) *s
))
146 while (isalpha((unsigned char) *s
))
147 if ((*cmd
++ = *s
++) == 'k' && cmd
== cmd0
+ 1)
149 if (*s
== '!' || *s
== '=')
155 /* read ex command argument */
156 static char *ex_arg(char *s
, char *arg
)
159 while (isspace((unsigned char) *s
))
161 while (*s
&& !isspace((unsigned char) *s
)) {
162 if (*s
== '\\' && s
[1])
170 static char *ex_argeol(char *ec
)
173 char *s
= ex_cmd(ec
, arg
);
174 while (isspace((unsigned char) *s
))
179 /* the previous search keyword */
180 int ex_kwd(char **kwd
, int *dir
)
189 /* set the previous search keyword */
190 void ex_kwdset(char *kwd
, int dir
)
193 snprintf(xkwd
, sizeof(xkwd
), "%s", kwd
);
194 reg_put('/', kwd
, 0);
199 static int ex_search(char **pat
)
211 sbuf_chr(kw
, (unsigned char) *e
);
212 if (*e
== '\\' && e
[1])
216 ex_kwdset(sbuf_buf(kw
), **pat
== '/' ? 1 : -1);
218 *pat
= *e
? e
+ 1 : e
;
219 if (ex_kwd(&pats
[0], &dir
))
221 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
225 while (row
>= 0 && row
< lbuf_len(xb
)) {
226 if (rset_find(re
, lbuf_get(xb
, row
), 0, NULL
, 0) >= 0)
231 return row
>= 0 && row
< lbuf_len(xb
) ? row
: -1;
234 static int ex_lineno(char **num
)
237 switch ((unsigned char) **num
) {
242 n
= lbuf_len(xb
) - 1;
246 if (lbuf_jump(xb
, (unsigned char) *++(*num
), &n
, NULL
))
255 if (isdigit((unsigned char) **num
)) {
257 while (isdigit((unsigned char) **num
))
261 while (**num
== '-' || **num
== '+') {
263 while (isdigit((unsigned char) **num
))
269 /* parse ex command location */
270 static int ex_region(char *loc
, int *beg
, int *end
)
273 if (!strcmp("%", loc
)) {
275 *end
= MAX(0, lbuf_len(xb
));
280 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
285 *end
= ex_lineno(&loc
) + 1;
286 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
289 while (*loc
&& *loc
!= ';' && *loc
!= ',')
297 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
299 if (*end
< *beg
|| *end
> lbuf_len(xb
))
304 static int ec_write(char *ec
);
306 static int ex_modifiedbuffer(char *msg
)
308 if (!lbuf_modified(xb
))
310 if (xaw
&& ex_path()[0])
311 return ec_write("w");
317 static int ec_quit(char *ec
)
321 if (!strchr(cmd
, '!'))
322 if (ex_modifiedbuffer("buffer modified\n"))
328 static int ex_expand(char *d
, char *s
)
331 int c
= (unsigned char) *s
++;
333 if (!bufs
[0].path
|| !bufs
[0].path
[0]) {
334 ex_show("\"%\" is unset\n");
337 strcpy(d
, bufs
[0].path
);
342 if (!bufs
[1].path
|| !bufs
[1].path
[0]) {
343 ex_show("\"#\" is unset\n");
346 strcpy(d
, bufs
[1].path
);
350 if (c
== '\\' && (*s
== '%' || *s
== '#'))
358 static int ec_edit(char *ec
)
361 char arg
[EXLEN
], cmd
[EXLEN
];
366 if (!strchr(cmd
, '!'))
367 if (xb
&& ex_modifiedbuffer("buffer modified\n"))
369 if (ex_expand(path
, arg
))
374 if (arg
[0] && bufs_find(path
) >= 0) {
375 bufs_switch(bufs_find(path
));
378 if (path
[0] || !bufs
[0].path
)
379 bufs_switch(bufs_open(path
));
380 fd
= open(ex_path(), O_RDONLY
);
382 int rd
= lbuf_rd(xb
, fd
, 0, lbuf_len(xb
));
384 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
385 ex_path(), lbuf_len(xb
));
387 ex_show("read failed\n");
391 lbuf_saved(xb
, path
[0] != '\0');
392 bufs
[0].mtime
= mtime(ex_path());
393 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
395 xtop
= MAX(0, MIN(xtop
, lbuf_len(xb
) - 1));
399 static int ec_read(char *ec
)
401 char arg
[EXLEN
], loc
[EXLEN
];
406 int n
= lbuf_len(xb
);
409 path
= arg
[0] ? arg
: ex_path();
410 if (ex_region(loc
, &beg
, &end
))
413 int pos
= MIN(xrow
+ 1, lbuf_len(xb
));
414 if (ex_expand(arg
, ex_argeol(ec
)))
416 obuf
= cmd_pipe(arg
+ 1, NULL
, 0, 1);
418 lbuf_edit(xb
, obuf
, pos
, pos
);
421 int fd
= open(path
, O_RDONLY
);
422 int pos
= lbuf_len(xb
) ? end
: 0;
424 ex_show("read failed\n");
427 if (lbuf_rd(xb
, fd
, pos
, pos
)) {
428 ex_show("read failed\n");
434 xrow
= end
+ lbuf_len(xb
) - n
- 1;
435 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
436 path
, lbuf_len(xb
) - n
);
441 static int ec_write(char *ec
)
443 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
451 path
= arg
[0] ? arg
: ex_path();
452 if (cmd
[0] == 'x' && !lbuf_modified(xb
))
454 if (ex_region(loc
, &beg
, &end
))
461 if (ex_expand(arg
, ex_argeol(ec
)))
463 ibuf
= lbuf_cp(xb
, beg
, end
);
465 cmd_pipe(arg
+ 1, ibuf
, 1, 0);
469 if (!strchr(cmd
, '!') && bufs
[0].path
&&
470 !strcmp(bufs
[0].path
, path
) &&
471 mtime(bufs
[0].path
) > bufs
[0].mtime
) {
472 ex_show("write failed: file changed\n");
475 if (!strchr(cmd
, '!') && arg
[0] && mtime(arg
) >= 0) {
476 ex_show("write failed: file exists\n");
479 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
481 ex_show("write failed: cannot create file\n");
484 if (lbuf_wr(xb
, fd
, beg
, end
)) {
485 ex_show("write failed\n");
491 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
496 bufs
[0].path
= uc_dup(path
);
498 if (!strcmp(ex_path(), path
))
500 if (!strcmp(ex_path(), path
))
501 bufs
[0].mtime
= mtime(path
);
502 if (cmd
[0] == 'x' || (cmd
[0] == 'w' && cmd
[1] == 'q'))
507 static int ec_insert(char *ec
)
509 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
517 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
520 while ((s
= ex_read(""))) {
521 if (!strcmp(".", s
)) {
530 if (beg
+ 1 <= lbuf_len(xb
))
535 lbuf_edit(xb
, sbuf_buf(sb
), beg
, end
);
536 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
541 static int ec_print(char *ec
)
543 char cmd
[EXLEN
], loc
[EXLEN
];
548 if (!cmd
[0] && !loc
[0]) {
549 if (xrow
>= lbuf_len(xb
) - 1)
553 if (ex_region(loc
, &beg
, &end
))
555 for (i
= beg
; i
< end
; i
++)
556 ex_print(lbuf_get(xb
, i
));
562 static int ec_null(char *ec
)
569 if (ex_region(loc
, &beg
, &end
))
571 xrow
= MAX(beg
, end
- 1);
576 static void ex_yank(int reg
, int beg
, int end
)
578 char *buf
= lbuf_cp(xb
, beg
, end
);
579 reg_put(reg
, buf
, 1);
583 static int ec_delete(char *ec
)
590 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
592 ex_yank(arg
[0], beg
, end
);
593 lbuf_edit(xb
, NULL
, beg
, end
);
598 static int ec_yank(char *ec
)
605 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
607 ex_yank(arg
[0], beg
, end
);
611 static int ec_put(char *ec
)
618 int n
= lbuf_len(xb
);
621 buf
= reg_get(arg
[0], &lnmode
);
622 if (!buf
|| ex_region(loc
, &beg
, &end
))
624 lbuf_edit(xb
, buf
, end
, end
);
625 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
629 static int ec_lnum(char *ec
)
635 if (ex_region(loc
, &beg
, &end
))
637 sprintf(msg
, "%d\n", end
);
642 static int ec_undo(char *ec
)
644 return lbuf_undo(xb
);
647 static int ec_redo(char *ec
)
649 return lbuf_redo(xb
);
652 static int ec_mark(char *ec
)
654 char loc
[EXLEN
], arg
[EXLEN
];
658 if (ex_region(loc
, &beg
, &end
))
660 lbuf_mark(xb
, arg
[0], end
- 1, 0);
664 static void replace(struct sbuf
*dst
, char *rep
, char *ln
, int *offs
)
667 if (rep
[0] == '\\' && rep
[1]) {
668 if (rep
[1] >= '0' && rep
[1] <= '9') {
669 int grp
= (rep
[1] - '0') * 2;
670 int len
= offs
[grp
+ 1] - offs
[grp
];
671 sbuf_mem(dst
, ln
+ offs
[grp
], len
);
673 sbuf_chr(dst
, (unsigned char) rep
[1]);
677 sbuf_chr(dst
, (unsigned char) rep
[0]);
683 static int ec_substitute(char *ec
)
690 char *pat
= NULL
, *rep
= NULL
;
694 if (ex_region(loc
, &beg
, &end
))
705 rep
= uc_dup(pat
? "" : xrep
);
706 snprintf(xrep
, sizeof(xrep
), "%s", rep
);
708 if (ex_kwd(&pats
[0], NULL
))
710 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
715 for (i
= beg
; i
< end
; i
++) {
716 char *ln
= lbuf_get(xb
, i
);
717 struct sbuf
*r
= sbuf_make();
718 while (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) >= 0) {
719 sbuf_mem(r
, ln
, offs
[0]);
720 replace(r
, rep
, ln
, offs
);
726 lbuf_edit(xb
, sbuf_buf(r
), i
, i
+ 1);
734 static int ec_exec(char *ec
)
737 ex_modifiedbuffer(NULL
);
738 if (ex_expand(arg
, ex_argeol(ec
)))
746 static int ec_make(char *ec
)
750 ex_modifiedbuffer(NULL
);
751 if (ex_expand(arg
, ex_argeol(ec
)))
753 sprintf(make
, "make %s", arg
);
760 static int ec_ft(char *ec
)
765 snprintf(bufs
[0].ft
, sizeof(bufs
[0].ft
), arg
);
767 ex_print(ex_filetype());
771 static int ec_cmap(char *ec
)
778 xkmap_alt
= conf_kmapfind(arg
);
780 ex_print(conf_kmap(xkmap
)[0]);
781 if (arg
[0] && !strchr(cmd
, '!'))
786 static int ex_exec(char *ln
);
788 static int ec_glob(char *ec
)
790 char loc
[EXLEN
], cmd
[EXLEN
];
799 if (!loc
[0] && !xgdep
)
801 if (ex_region(loc
, &beg
, &end
))
803 not = strchr(cmd
, '!') || cmd
[0] == 'v';
809 if (ex_kwd(&pats
[0], NULL
))
811 if (!(re
= rset_make(1, pats
, xic
? RE_ICASE
: 0)))
814 for (i
= beg
+ 1; i
< end
; i
++)
815 lbuf_globset(xb
, i
, xgdep
);
817 while (i
< lbuf_len(xb
)) {
818 char *ln
= lbuf_get(xb
, i
);
819 if ((rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) < 0) == not) {
825 while (i
< lbuf_len(xb
) && !lbuf_globget(xb
, i
, xgdep
))
828 for (i
= 0; i
< lbuf_len(xb
); i
++)
829 lbuf_globget(xb
, i
, xgdep
);
835 static struct option
{
840 {"ai", "autoindent", &xai
},
841 {"aw", "autowrite", &xaw
},
842 {"ic", "ignorecase", &xic
},
843 {"td", "textdirection", &xtd
},
844 {"shape", "shape", &xshape
},
845 {"order", "xorder", &xorder
},
846 {"hl", "highlight", &xhl
},
849 static char *cutword(char *s
, char *d
)
853 while (*s
&& !isspace(*s
))
861 static int ec_set(char *ec
)
872 if (tok
[0] == 'n' && tok
[1] == 'o') {
873 strcpy(opt
, tok
+ 2);
876 char *r
= strchr(tok
, '=');
886 for (i
= 0; i
< LEN(options
); i
++) {
887 struct option
*o
= &options
[i
];
888 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
)) {
893 ex_show("unknown option");
899 static struct excmd
{
904 {"p", "print", ec_print
},
905 {"a", "append", ec_insert
},
906 {"i", "insert", ec_insert
},
907 {"d", "delete", ec_delete
},
908 {"c", "change", ec_insert
},
909 {"e", "edit", ec_edit
},
910 {"e!", "edit!", ec_edit
},
911 {"g", "global", ec_glob
},
912 {"g!", "global!", ec_glob
},
914 {"k", "mark", ec_mark
},
915 {"pu", "put", ec_put
},
916 {"q", "quit", ec_quit
},
917 {"q!", "quit!", ec_quit
},
918 {"r", "read", ec_read
},
919 {"v", "vglobal", ec_glob
},
920 {"w", "write", ec_write
},
921 {"w!", "write!", ec_write
},
922 {"wq", "wq", ec_write
},
923 {"wq!", "wq!", ec_write
},
924 {"u", "undo", ec_undo
},
925 {"r", "redo", ec_redo
},
926 {"se", "set", ec_set
},
927 {"s", "substitute", ec_substitute
},
928 {"x", "xit", ec_write
},
929 {"x!", "xit!", ec_write
},
930 {"ya", "yank", ec_yank
},
932 {"make", "make", ec_make
},
933 {"ft", "filetype", ec_ft
},
934 {"cm", "cmap", ec_cmap
},
935 {"cm!", "cmap!", ec_cmap
},
939 /* read an ex command and its arguments from src into dst */
940 static void ex_line(int (*ec
)(char *s
), char *dst
, char **src
)
942 if (!ec
|| ec
!= ec_glob
) {
943 while (**src
&& **src
!= '|' && **src
!= '\n')
948 } else { /* the rest of the line for :g */
950 *src
= strchr(*src
, '\0');
954 /* execute a single ex command */
955 static int ex_exec(char *ln
)
963 for (i
= 0; i
< LEN(excmds
); i
++) {
964 if (!strcmp(excmds
[i
].abbr
, cmd
) ||
965 !strcmp(excmds
[i
].name
, cmd
)) {
966 ex_line(excmds
[i
].ec
, ec
, &ln
);
967 ret
= excmds
[i
].ec(ec
);
971 if (!xvis
&& !cmd
[0])
973 if (i
== LEN(excmds
))
974 ex_line(NULL
, ec
, &ln
);
979 /* execute a single ex command */
980 void ex_command(char *ln
)
991 char *ln
= ex_read(":");
998 int ex_init(char **files
)
1002 char *r
= files
[0] ? files
[0] : "";
1005 while (*r
&& s
+ 2 < cmd
+ sizeof(cmd
)) {
1013 if (getenv("EXINIT"))
1014 ex_command(getenv("EXINIT"));
1021 for (i
= 0; i
< LEN(bufs
); i
++)