12 int xrow
, xoff
, xtop
; /* current row, column, and top row */
13 int xquit
; /* exit if set */
14 int xvis
; /* visual mode */
15 int xai
= 1; /* autoindent option */
16 int xic
= 1; /* ignorecase option */
17 int xaw
; /* autowrite 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 */
30 static int bufs_find(char *path
)
33 for (i
= 0; i
< LEN(bufs
); i
++)
34 if (bufs
[i
].lb
&& !strcmp(bufs
[i
].path
, path
))
39 static void bufs_free(int idx
)
43 lbuf_free(bufs
[idx
].lb
);
44 memset(&bufs
[idx
], 0, sizeof(bufs
[idx
]));
48 static int bufs_open(char *path
)
51 for (i
= 0; i
< LEN(bufs
) - 1; i
++)
55 bufs
[i
].path
= uc_dup(path
);
56 bufs
[i
].lb
= lbuf_make();
58 strcpy(bufs
[i
].ft
, syn_filetype(path
));
62 static void bufs_swap(int i
, int j
)
67 memcpy(&tmp
, &bufs
[i
], sizeof(tmp
));
68 memcpy(&bufs
[i
], &bufs
[j
], sizeof(tmp
));
69 memcpy(&bufs
[j
], &tmp
, sizeof(tmp
));
72 static void bufs_switch(int idx
)
85 struct lbuf
*ex_lbuf(void)
90 char *ex_filetype(void)
95 /* read ex command location */
96 static char *ex_loc(char *s
, char *loc
)
98 while (*s
== ':' || isspace((unsigned char) *s
))
100 while (*s
&& !isalpha((unsigned char) *s
) && *s
!= '=' && *s
!= '!') {
103 if (*s
== '/' || *s
== '?') {
106 while (*s
&& *s
!= d
) {
107 if (*s
== '\\' && s
[1])
118 /* read ex command name */
119 static char *ex_cmd(char *s
, char *cmd
)
123 while (isspace((unsigned char) *s
))
125 while (isalpha((unsigned char) *s
))
126 if ((*cmd
++ = *s
++) == 'k' && cmd
== cmd0
+ 1)
128 if (*s
== '!' || *s
== '=')
134 /* read ex command argument */
135 static char *ex_arg(char *s
, char *arg
)
138 while (isspace((unsigned char) *s
))
140 while (*s
&& !isspace((unsigned char) *s
))
146 static char *ex_argeol(char *ec
)
149 char *s
= ex_cmd(ec
, arg
);
150 while (isspace((unsigned char) *s
))
155 static int ex_search(char *pat
)
158 int dir
= *pat
== '/' ? 1 : -1;
168 sbuf_chr(kw
, (unsigned char) *e
);
169 if (*e
== '\\' && e
[1])
172 re_kw
[0] = sbuf_buf(kw
);
173 re
= rset_make(1, re_kw
, xic
? RE_ICASE
: 0);
177 while (i
>= 0 && i
< lbuf_len(xb
)) {
178 if (rset_find(re
, lbuf_get(xb
, i
), 0, NULL
, 0) >= 0)
186 static int ex_lineno(char *num
)
189 if (!num
[0] || num
[0] == '.')
194 n
= lbuf_len(xb
) - 1;
196 n
= xrow
- (num
[1] ? ex_lineno(num
+ 1) : 1);
198 n
= xrow
+ (num
[1] ? ex_lineno(num
+ 1) : 1);
200 lbuf_jump(xb
, num
[1], &n
, NULL
);
201 if (num
[0] == '/' && num
[1])
203 if (num
[0] == '?' && num
[1])
208 /* parse ex command location */
209 static int ex_region(char *loc
, int *beg
, int *end
)
212 if (!strcmp("%", loc
)) {
214 *end
= MAX(0, lbuf_len(xb
));
219 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
224 *end
= ex_lineno(loc
) + 1;
225 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
228 while (*loc
&& *loc
!= ';' && *loc
!= ',')
236 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
238 if (*end
< *beg
|| *end
> lbuf_len(xb
))
243 static int ec_write(char *ec
);
245 static int ex_modifiedbuffer(char *msg
)
247 if (!lbuf_modified(xb
))
249 if (xaw
&& ex_path()[0])
250 return ec_write("w");
256 static int ec_quit(char *ec
)
260 if (!strchr(cmd
, '!'))
261 if (ex_modifiedbuffer("buffer modified\n"))
267 static int ex_expand(char *d
, char *s
)
270 int c
= (unsigned char) *s
++;
272 if (!bufs
[0].path
|| !bufs
[0].path
[0]) {
273 ex_show("\"%\" is unset\n");
276 strcpy(d
, bufs
[0].path
);
281 if (!bufs
[1].path
|| !bufs
[1].path
[0]) {
282 ex_show("\"#\" is unset\n");
285 strcpy(d
, bufs
[1].path
);
289 if (c
== '\\' && (*s
== '%' || *s
== '#'))
297 static int ec_edit(char *ec
)
300 char arg
[EXLEN
], cmd
[EXLEN
];
305 if (!strchr(cmd
, '!'))
306 if (xb
&& ex_modifiedbuffer("buffer modified\n"))
308 if (ex_expand(path
, arg
))
311 if (arg
[0] && bufs_find(path
) >= 0) {
312 bufs_switch(bufs_find(path
));
315 if (path
[0] || !bufs
[0].path
)
316 bufs_switch(bufs_open(path
));
317 fd
= open(ex_path(), O_RDONLY
);
319 lbuf_rm(xb
, 0, lbuf_len(xb
));
322 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
323 ex_path(), lbuf_len(xb
));
326 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
327 lbuf_saved(xb
, path
[0] != '\0');
331 static int ec_read(char *ec
)
333 char arg
[EXLEN
], loc
[EXLEN
];
338 int n
= lbuf_len(xb
);
341 path
= arg
[0] ? arg
: ex_path();
342 if (ex_region(loc
, &beg
, &end
))
345 if (ex_expand(arg
, ex_argeol(ec
)))
347 obuf
= cmd_pipe(arg
+ 1, NULL
, 0, 1);
349 lbuf_put(xb
, MIN(xrow
+ 1, lbuf_len(xb
)), obuf
);
352 int fd
= open(path
, O_RDONLY
);
354 ex_show("read failed\n");
357 lbuf_rd(xb
, fd
, lbuf_len(xb
) ? end
: 0);
360 xrow
= end
+ lbuf_len(xb
) - n
- 1;
361 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
362 path
, lbuf_len(xb
) - n
);
367 static int ec_write(char *ec
)
369 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
377 path
= arg
[0] ? arg
: ex_path();
378 if (cmd
[0] == 'x' && !lbuf_modified(xb
))
380 if (ex_region(loc
, &beg
, &end
))
387 if (ex_expand(arg
, ex_argeol(ec
)))
389 ibuf
= lbuf_cp(xb
, beg
, end
);
391 cmd_pipe(arg
+ 1, ibuf
, 1, 0);
394 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
396 ex_show("write failed\n");
399 lbuf_wr(xb
, fd
, beg
, end
);
402 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
407 bufs
[0].path
= uc_dup(path
);
409 if (!strcmp(ex_path(), path
))
411 if (cmd
[0] == 'x' || (cmd
[0] == 'w' && cmd
[1] == 'q'))
416 static int ec_insert(char *ec
)
418 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
426 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
430 lbuf_rm(xb
, beg
, end
);
434 while ((s
= ex_read(""))) {
435 if (!strcmp(".", s
)) {
444 if (end
> lbuf_len(xb
))
447 lbuf_put(xb
, end
, sbuf_buf(sb
));
448 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
453 static int ec_print(char *ec
)
455 char cmd
[EXLEN
], loc
[EXLEN
];
460 if (!cmd
[0] && !loc
[0]) {
461 if (xrow
>= lbuf_len(xb
) - 1)
465 if (ex_region(loc
, &beg
, &end
))
467 for (i
= beg
; i
< end
; i
++)
468 ex_print(lbuf_get(xb
, i
));
473 static void ex_yank(int reg
, int beg
, int end
)
475 char *buf
= lbuf_cp(xb
, beg
, end
);
476 reg_put(reg
, buf
, 1);
480 static int ec_delete(char *ec
)
487 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
489 ex_yank(arg
[0], beg
, end
);
490 lbuf_rm(xb
, beg
, end
);
495 static int ec_yank(char *ec
)
502 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
504 ex_yank(arg
[0], beg
, end
);
508 static int ec_put(char *ec
)
515 int n
= lbuf_len(xb
);
518 buf
= reg_get(arg
[0], &lnmode
);
519 if (!buf
|| ex_region(loc
, &beg
, &end
))
521 lbuf_put(xb
, end
, buf
);
522 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
526 static int ec_lnum(char *ec
)
532 if (ex_region(loc
, &beg
, &end
))
534 sprintf(msg
, "%d\n", end
);
539 static int ec_undo(char *ec
)
541 return lbuf_undo(xb
);
544 static int ec_redo(char *ec
)
546 return lbuf_redo(xb
);
549 static int ec_mark(char *ec
)
551 char loc
[EXLEN
], arg
[EXLEN
];
555 if (ex_region(loc
, &beg
, &end
))
557 lbuf_mark(xb
, arg
[0], end
- 1, 0);
561 static char *readuntil(char **src
, int delim
)
563 struct sbuf
*sbuf
= sbuf_make();
565 /* reading the pattern */
566 while (*s
&& *s
!= delim
) {
567 if (s
[0] == '\\' && s
[1])
568 sbuf_chr(sbuf
, (unsigned char) *s
++);
569 sbuf_chr(sbuf
, (unsigned char) *s
++);
571 if (*s
) /* skipping the delimiter */
574 return sbuf_done(sbuf
);
577 static int ec_substitute(char *ec
)
588 if (ex_region(loc
, &beg
, &end
))
591 delim
= (unsigned char) *s
++;
592 pat
= readuntil(&s
, delim
);
593 rep
= readuntil(&s
, delim
);
594 re
= rset_make(1, &pat
, xic
? RE_ICASE
: 0);
597 for (i
= beg
; i
< end
; i
++) {
598 char *ln
= lbuf_get(xb
, i
);
599 struct sbuf
*r
= sbuf_make();
600 while (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) >= 0) {
601 sbuf_mem(r
, ln
, offs
[0]);
608 lbuf_rm(xb
, i
, i
+ 1);
609 lbuf_put(xb
, i
, sbuf_buf(r
));
618 static int ec_exec(char *ec
)
621 ex_modifiedbuffer(NULL
);
622 if (ex_expand(arg
, ex_argeol(ec
)))
630 static int ec_make(char *ec
)
634 ex_modifiedbuffer(NULL
);
635 if (ex_expand(arg
, ex_argeol(ec
)))
637 sprintf(make
, "make %s", arg
);
644 static struct option
{
649 {"ai", "autoindent", &xai
},
650 {"aw", "autowrite", &xaw
},
651 {"ic", "ignorecase", &xic
},
652 {"td", "textdirection", &xdir
},
653 {"shape", "shape", &xshape
},
654 {"order", "xorder", &xorder
},
657 static char *cutword(char *s
, char *d
)
661 while (*s
&& !isspace(*s
))
669 static int ec_set(char *ec
)
680 if (tok
[0] == 'n' && tok
[1] == 'o') {
681 strcpy(opt
, tok
+ 2);
684 char *r
= strchr(tok
, '=');
694 for (i
= 0; i
< LEN(options
); i
++) {
695 struct option
*o
= &options
[i
];
696 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
)) {
701 ex_show("unknown option");
707 static struct excmd
{
712 {"p", "print", ec_print
},
713 {"a", "append", ec_insert
},
714 {"i", "insert", ec_insert
},
715 {"d", "delete", ec_delete
},
716 {"c", "change", ec_insert
},
717 {"e", "edit", ec_edit
},
718 {"e!", "edit!", ec_edit
},
720 {"k", "mark", ec_mark
},
721 {"pu", "put", ec_put
},
722 {"q", "quit", ec_quit
},
723 {"q!", "quit!", ec_quit
},
724 {"r", "read", ec_read
},
725 {"w", "write", ec_write
},
726 {"wq", "wq", ec_write
},
727 {"u", "undo", ec_undo
},
728 {"r", "redo", ec_redo
},
729 {"se", "set", ec_set
},
730 {"s", "substitute", ec_substitute
},
731 {"x", "xit", ec_write
},
732 {"x!", "xit!", ec_write
},
733 {"ya", "yank", ec_yank
},
735 {"make", "make", ec_make
},
739 /* execute a single ex command */
740 void ex_command(char *ln
)
745 for (i
= 0; i
< LEN(excmds
); i
++) {
746 if (!strcmp(excmds
[i
].abbr
, cmd
) || !strcmp(excmds
[i
].name
, cmd
)) {
758 char *ln
= ex_read(":");
765 void ex_init(char **files
)
768 snprintf(cmd
, sizeof(cmd
), "e %s", files
[0] ? files
[0] : "");
775 for (i
= 0; i
< LEN(bufs
); i
++)