10 int xrow
, xoff
, xtop
; /* current row, column, and top row */
11 int xquit
; /* exit if set */
12 int xvis
; /* visual mode */
13 int xai
= 1; /* autoindent option */
14 int xic
= 1; /* ignorecase option */
15 int xaw
; /* autowrite option */
16 int xled
= 1; /* use the line editor */
17 int xdir
= +1; /* current direction context */
18 int xshape
= 1; /* perform letter shaping */
19 int xorder
= 1; /* change the order of characters */
20 char xfindkwd
[EXLEN
]; /* the last searched keyword */
21 int xfinddir
= +1; /* the last search direction */
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])
119 /* read ex command name */
120 static char *ex_cmd(char *s
, char *cmd
)
124 while (isspace((unsigned char) *s
))
126 while (isalpha((unsigned char) *s
))
127 if ((*cmd
++ = *s
++) == 'k' && cmd
== cmd0
+ 1)
129 if (*s
== '!' || *s
== '=')
135 /* read ex command argument */
136 static char *ex_arg(char *s
, char *arg
)
139 while (isspace((unsigned char) *s
))
141 while (*s
&& !isspace((unsigned char) *s
))
147 static char *ex_argeol(char *ec
)
150 char *s
= ex_cmd(ec
, arg
);
151 while (isspace((unsigned char) *s
))
156 static int ex_search(char *pat
)
159 int dir
= *pat
== '/' ? 1 : -1;
169 sbuf_chr(kw
, (unsigned char) *e
);
170 if (*e
== '\\' && e
[1])
174 snprintf(xfindkwd
, sizeof(xfindkwd
), "%s", sbuf_buf(kw
));
180 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
184 while (row
>= 0 && row
< lbuf_len(xb
)) {
185 if (rset_find(re
, lbuf_get(xb
, row
), 0, NULL
, 0) >= 0)
190 return row
< 0 || row
>= lbuf_len(xb
) ? xrow
: row
;
193 static int ex_lineno(char *num
)
196 if (!num
[0] || num
[0] == '.')
201 n
= lbuf_len(xb
) - 1;
203 n
= xrow
- (num
[1] ? ex_lineno(num
+ 1) : 1);
205 n
= xrow
+ (num
[1] ? ex_lineno(num
+ 1) : 1);
207 lbuf_jump(xb
, num
[1], &n
, NULL
);
208 if (num
[0] == '/' && num
[1])
210 if (num
[0] == '?' && num
[1])
215 /* parse ex command location */
216 static int ex_region(char *loc
, int *beg
, int *end
)
219 if (!strcmp("%", loc
)) {
221 *end
= MAX(0, lbuf_len(xb
));
226 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
231 *end
= ex_lineno(loc
) + 1;
232 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
235 while (*loc
&& *loc
!= ';' && *loc
!= ',')
243 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
245 if (*end
< *beg
|| *end
> lbuf_len(xb
))
250 static int ec_write(char *ec
);
252 static int ex_modifiedbuffer(char *msg
)
254 if (!lbuf_modified(xb
))
256 if (xaw
&& ex_path()[0])
257 return ec_write("w");
263 static int ec_quit(char *ec
)
267 if (!strchr(cmd
, '!'))
268 if (ex_modifiedbuffer("buffer modified\n"))
274 static int ex_expand(char *d
, char *s
)
277 int c
= (unsigned char) *s
++;
279 if (!bufs
[0].path
|| !bufs
[0].path
[0]) {
280 ex_show("\"%\" is unset\n");
283 strcpy(d
, bufs
[0].path
);
288 if (!bufs
[1].path
|| !bufs
[1].path
[0]) {
289 ex_show("\"#\" is unset\n");
292 strcpy(d
, bufs
[1].path
);
296 if (c
== '\\' && (*s
== '%' || *s
== '#'))
304 static int ec_edit(char *ec
)
307 char arg
[EXLEN
], cmd
[EXLEN
];
312 if (!strchr(cmd
, '!'))
313 if (xb
&& ex_modifiedbuffer("buffer modified\n"))
315 if (ex_expand(path
, arg
))
318 if (arg
[0] && bufs_find(path
) >= 0) {
319 bufs_switch(bufs_find(path
));
322 if (path
[0] || !bufs
[0].path
)
323 bufs_switch(bufs_open(path
));
324 fd
= open(ex_path(), O_RDONLY
);
326 lbuf_rm(xb
, 0, lbuf_len(xb
));
329 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
330 ex_path(), lbuf_len(xb
));
333 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
334 lbuf_saved(xb
, path
[0] != '\0');
338 static int ec_read(char *ec
)
340 char arg
[EXLEN
], loc
[EXLEN
];
345 int n
= lbuf_len(xb
);
348 path
= arg
[0] ? arg
: ex_path();
349 if (ex_region(loc
, &beg
, &end
))
352 if (ex_expand(arg
, ex_argeol(ec
)))
354 obuf
= cmd_pipe(arg
+ 1, NULL
, 0, 1);
356 lbuf_put(xb
, MIN(xrow
+ 1, lbuf_len(xb
)), obuf
);
359 int fd
= open(path
, O_RDONLY
);
361 ex_show("read failed\n");
364 lbuf_rd(xb
, fd
, lbuf_len(xb
) ? end
: 0);
367 xrow
= end
+ lbuf_len(xb
) - n
- 1;
368 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
369 path
, lbuf_len(xb
) - n
);
374 static int ec_write(char *ec
)
376 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
384 path
= arg
[0] ? arg
: ex_path();
385 if (cmd
[0] == 'x' && !lbuf_modified(xb
))
387 if (ex_region(loc
, &beg
, &end
))
394 if (ex_expand(arg
, ex_argeol(ec
)))
396 ibuf
= lbuf_cp(xb
, beg
, end
);
398 cmd_pipe(arg
+ 1, ibuf
, 1, 0);
401 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
403 ex_show("write failed\n");
406 lbuf_wr(xb
, fd
, beg
, end
);
409 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
414 bufs
[0].path
= uc_dup(path
);
416 if (!strcmp(ex_path(), path
))
418 if (cmd
[0] == 'x' || (cmd
[0] == 'w' && cmd
[1] == 'q'))
423 static int ec_insert(char *ec
)
425 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
433 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
437 lbuf_rm(xb
, beg
, end
);
441 while ((s
= ex_read(""))) {
442 if (!strcmp(".", s
)) {
451 if (end
> lbuf_len(xb
))
454 lbuf_put(xb
, end
, sbuf_buf(sb
));
455 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
460 static int ec_print(char *ec
)
462 char cmd
[EXLEN
], loc
[EXLEN
];
467 if (!cmd
[0] && !loc
[0]) {
468 if (xrow
>= lbuf_len(xb
) - 1)
472 if (ex_region(loc
, &beg
, &end
))
474 for (i
= beg
; i
< end
; i
++)
475 ex_print(lbuf_get(xb
, i
));
481 static int ec_null(char *ec
)
488 if (ex_region(loc
, &beg
, &end
))
490 xrow
= MAX(beg
, end
- 1);
495 static void ex_yank(int reg
, int beg
, int end
)
497 char *buf
= lbuf_cp(xb
, beg
, end
);
498 reg_put(reg
, buf
, 1);
502 static int ec_delete(char *ec
)
509 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
511 ex_yank(arg
[0], beg
, end
);
512 lbuf_rm(xb
, beg
, end
);
517 static int ec_yank(char *ec
)
524 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
526 ex_yank(arg
[0], beg
, end
);
530 static int ec_put(char *ec
)
537 int n
= lbuf_len(xb
);
540 buf
= reg_get(arg
[0], &lnmode
);
541 if (!buf
|| ex_region(loc
, &beg
, &end
))
543 lbuf_put(xb
, end
, buf
);
544 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
548 static int ec_lnum(char *ec
)
554 if (ex_region(loc
, &beg
, &end
))
556 sprintf(msg
, "%d\n", end
);
561 static int ec_undo(char *ec
)
563 return lbuf_undo(xb
);
566 static int ec_redo(char *ec
)
568 return lbuf_redo(xb
);
571 static int ec_mark(char *ec
)
573 char loc
[EXLEN
], arg
[EXLEN
];
577 if (ex_region(loc
, &beg
, &end
))
579 lbuf_mark(xb
, arg
[0], end
- 1, 0);
583 static char *readuntil(char **src
, int delim
)
585 struct sbuf
*sbuf
= sbuf_make();
587 /* reading the pattern */
588 while (*s
&& *s
!= delim
) {
589 if (s
[0] == '\\' && s
[1])
590 sbuf_chr(sbuf
, (unsigned char) *s
++);
591 sbuf_chr(sbuf
, (unsigned char) *s
++);
593 if (*s
) /* skipping the delimiter */
596 return sbuf_done(sbuf
);
599 static int ec_substitute(char *ec
)
611 if (ex_region(loc
, &beg
, &end
))
614 delim
= (unsigned char) *s
++;
615 pat
= readuntil(&s
, delim
);
616 rep
= readuntil(&s
, delim
);
618 snprintf(xfindkwd
, sizeof(xfindkwd
), "%s", pat
);
621 re
= rset_make(1, pats
, xic
? RE_ICASE
: 0);
626 for (i
= beg
; i
< end
; i
++) {
627 char *ln
= lbuf_get(xb
, i
);
628 struct sbuf
*r
= sbuf_make();
629 while (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) >= 0) {
630 sbuf_mem(r
, ln
, offs
[0]);
637 lbuf_rm(xb
, i
, i
+ 1);
638 lbuf_put(xb
, i
, sbuf_buf(r
));
646 static int ec_exec(char *ec
)
649 ex_modifiedbuffer(NULL
);
650 if (ex_expand(arg
, ex_argeol(ec
)))
658 static int ec_make(char *ec
)
662 ex_modifiedbuffer(NULL
);
663 if (ex_expand(arg
, ex_argeol(ec
)))
665 sprintf(make
, "make %s", arg
);
672 static struct option
{
677 {"ai", "autoindent", &xai
},
678 {"aw", "autowrite", &xaw
},
679 {"ic", "ignorecase", &xic
},
680 {"td", "textdirection", &xdir
},
681 {"shape", "shape", &xshape
},
682 {"order", "xorder", &xorder
},
685 static char *cutword(char *s
, char *d
)
689 while (*s
&& !isspace(*s
))
697 static int ec_set(char *ec
)
708 if (tok
[0] == 'n' && tok
[1] == 'o') {
709 strcpy(opt
, tok
+ 2);
712 char *r
= strchr(tok
, '=');
722 for (i
= 0; i
< LEN(options
); i
++) {
723 struct option
*o
= &options
[i
];
724 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
)) {
729 ex_show("unknown option");
735 static struct excmd
{
740 {"p", "print", ec_print
},
741 {"a", "append", ec_insert
},
742 {"i", "insert", ec_insert
},
743 {"d", "delete", ec_delete
},
744 {"c", "change", ec_insert
},
745 {"e", "edit", ec_edit
},
746 {"e!", "edit!", ec_edit
},
748 {"k", "mark", ec_mark
},
749 {"pu", "put", ec_put
},
750 {"q", "quit", ec_quit
},
751 {"q!", "quit!", ec_quit
},
752 {"r", "read", ec_read
},
753 {"w", "write", ec_write
},
754 {"wq", "wq", ec_write
},
755 {"u", "undo", ec_undo
},
756 {"r", "redo", ec_redo
},
757 {"se", "set", ec_set
},
758 {"s", "substitute", ec_substitute
},
759 {"x", "xit", ec_write
},
760 {"x!", "xit!", ec_write
},
761 {"ya", "yank", ec_yank
},
763 {"make", "make", ec_make
},
767 /* execute a single ex command */
768 void ex_command(char *ln
)
773 for (i
= 0; i
< LEN(excmds
); i
++) {
774 if (!strcmp(excmds
[i
].abbr
, cmd
) || !strcmp(excmds
[i
].name
, cmd
)) {
779 if (!xvis
&& !cmd
[0])
788 char *ln
= ex_read(":");
795 void ex_init(char **files
)
798 snprintf(cmd
, sizeof(cmd
), "e %s", files
[0] ? files
[0] : "");
805 for (i
= 0; i
< LEN(bufs
); i
++)