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 int ex_search(char *pat
)
149 int dir
= *pat
== '/' ? 1 : -1;
159 sbuf_chr(kw
, (unsigned char) *e
);
160 if (*e
== '\\' && e
[1])
163 re_kw
[0] = sbuf_buf(kw
);
164 re
= rset_make(1, re_kw
, xic
? RE_ICASE
: 0);
168 while (i
>= 0 && i
< lbuf_len(xb
)) {
169 if (rset_find(re
, lbuf_get(xb
, i
), 0, NULL
, 0) >= 0)
177 static int ex_lineno(char *num
)
180 if (!num
[0] || num
[0] == '.')
185 n
= lbuf_len(xb
) - 1;
187 n
= xrow
- (num
[1] ? ex_lineno(num
+ 1) : 1);
189 n
= xrow
+ (num
[1] ? ex_lineno(num
+ 1) : 1);
191 lbuf_jump(xb
, num
[1], &n
, NULL
);
192 if (num
[0] == '/' && num
[1])
194 if (num
[0] == '?' && num
[1])
199 /* parse ex command location */
200 static int ex_region(char *loc
, int *beg
, int *end
)
203 if (!strcmp("%", loc
)) {
205 *end
= MAX(0, lbuf_len(xb
));
210 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
215 *end
= ex_lineno(loc
) + 1;
216 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
219 while (*loc
&& *loc
!= ';' && *loc
!= ',')
227 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
229 if (*end
< *beg
|| *end
> lbuf_len(xb
))
234 static int ec_write(char *ec
);
236 static int ex_modifiedbuffer(char *msg
)
238 if (!lbuf_modified(xb
))
240 if (xaw
&& ex_path()[0])
241 return ec_write("w");
247 static int ec_quit(char *ec
)
251 if (!strchr(cmd
, '!'))
252 if (ex_modifiedbuffer("buffer modified\n"))
258 static int ex_expand(char *d
, char *s
)
261 int c
= (unsigned char) *s
++;
263 if (!bufs
[0].path
|| !bufs
[0].path
[0]) {
264 ex_show("\"%\" is unset\n");
267 strcpy(d
, bufs
[0].path
);
272 if (!bufs
[1].path
|| !bufs
[1].path
[0]) {
273 ex_show("\"#\" is unset\n");
276 strcpy(d
, bufs
[1].path
);
280 if (c
== '\\' && (*s
== '%' || *s
== '#'))
288 static int ec_edit(char *ec
)
291 char arg
[EXLEN
], cmd
[EXLEN
];
296 if (!strchr(cmd
, '!'))
297 if (xb
&& ex_modifiedbuffer("buffer modified\n"))
299 if (ex_expand(path
, arg
))
302 if (arg
[0] && bufs_find(path
) >= 0) {
303 bufs_switch(bufs_find(path
));
306 if (path
[0] || !bufs
[0].path
)
307 bufs_switch(bufs_open(path
));
308 fd
= open(ex_path(), O_RDONLY
);
310 lbuf_rm(xb
, 0, lbuf_len(xb
));
313 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
314 ex_path(), lbuf_len(xb
));
317 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
319 lbuf_saved(xb
, path
[0] != '\0');
323 static int ec_read(char *ec
)
325 char arg
[EXLEN
], loc
[EXLEN
];
330 int n
= lbuf_len(xb
);
333 path
= arg
[0] ? arg
: ex_path();
334 fd
= open(path
, O_RDONLY
);
336 ex_show("read failed\n");
339 if (ex_region(loc
, &beg
, &end
))
341 lbuf_rd(xb
, fd
, lbuf_len(xb
) ? end
: 0);
343 xrow
= end
+ lbuf_len(xb
) - n
;
344 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
345 path
, lbuf_len(xb
) - n
);
350 static int ec_write(char *ec
)
352 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
360 path
= arg
[0] ? arg
: ex_path();
361 if (ex_region(loc
, &beg
, &end
))
367 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
369 ex_show("write failed\n");
372 lbuf_wr(xb
, fd
, beg
, end
);
374 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
379 bufs
[0].path
= uc_dup(path
);
381 if (!strcmp(ex_path(), path
))
383 if (!strcmp("wq", cmd
))
388 static int ec_insert(char *ec
)
390 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
398 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
402 lbuf_rm(xb
, beg
, end
);
406 while ((s
= ex_read(""))) {
407 if (!strcmp(".", s
)) {
416 if (end
> lbuf_len(xb
))
419 lbuf_put(xb
, end
, sbuf_buf(sb
));
420 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
425 static int ec_print(char *ec
)
427 char cmd
[EXLEN
], loc
[EXLEN
];
432 if (!cmd
[0] && !loc
[0]) {
433 if (xrow
>= lbuf_len(xb
) - 1)
437 if (ex_region(loc
, &beg
, &end
))
439 for (i
= beg
; i
< end
; i
++)
440 ex_show(lbuf_get(xb
, i
));
445 static void ex_yank(int reg
, int beg
, int end
)
447 char *buf
= lbuf_cp(xb
, beg
, end
);
448 reg_put(reg
, buf
, 1);
452 static int ec_delete(char *ec
)
459 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
461 ex_yank(arg
[0], beg
, end
);
462 lbuf_rm(xb
, beg
, end
);
467 static int ec_yank(char *ec
)
474 if (ex_region(loc
, &beg
, &end
) || !lbuf_len(xb
))
476 ex_yank(arg
[0], beg
, end
);
480 static int ec_put(char *ec
)
487 int n
= lbuf_len(xb
);
490 buf
= reg_get(arg
[0], &lnmode
);
491 if (!buf
|| ex_region(loc
, &beg
, &end
))
493 lbuf_put(xb
, end
, buf
);
494 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
498 static int ec_lnum(char *ec
)
504 if (ex_region(loc
, &beg
, &end
))
506 sprintf(msg
, "%d\n", end
);
511 static int ec_undo(char *ec
)
513 return lbuf_undo(xb
);
516 static int ec_redo(char *ec
)
518 return lbuf_redo(xb
);
521 static int ec_mark(char *ec
)
523 char loc
[EXLEN
], arg
[EXLEN
];
527 if (ex_region(loc
, &beg
, &end
))
529 lbuf_mark(xb
, arg
[0], end
- 1, 0);
533 static char *readuntil(char **src
, int delim
)
535 struct sbuf
*sbuf
= sbuf_make();
537 /* reading the pattern */
538 while (*s
&& *s
!= delim
) {
539 if (s
[0] == '\\' && s
[1])
540 sbuf_chr(sbuf
, (unsigned char) *s
++);
541 sbuf_chr(sbuf
, (unsigned char) *s
++);
543 if (*s
) /* skipping the delimiter */
546 return sbuf_done(sbuf
);
549 static int ec_substitute(char *ec
)
551 char loc
[EXLEN
], arg
[EXLEN
];
561 if (ex_region(loc
, &beg
, &end
))
563 delim
= (unsigned char) *s
++;
564 pat
= readuntil(&s
, delim
);
565 rep
= readuntil(&s
, delim
);
566 re
= rset_make(1, &pat
, xic
? RE_ICASE
: 0);
569 for (i
= beg
; i
< end
; i
++) {
570 char *ln
= lbuf_get(xb
, i
);
571 struct sbuf
*r
= sbuf_make();
572 while (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0) >= 0) {
573 sbuf_mem(r
, ln
, offs
[0]);
580 lbuf_rm(xb
, i
, i
+ 1);
581 lbuf_put(xb
, i
, sbuf_buf(r
));
590 static int ec_exec(char *ec
)
594 ex_modifiedbuffer(NULL
);
595 if (ex_expand(arg
, ex_cmd(ec
, cmd
)))
597 return cmd_exec(arg
);
600 static int ec_make(char *ec
)
605 ex_modifiedbuffer(NULL
);
606 if (ex_expand(arg
, ex_cmd(ec
, cmd
)))
608 sprintf(make
, "make %s", arg
);
609 return cmd_exec(make
);
612 static struct option
{
617 {"ai", "autoindent", &xai
},
618 {"aw", "autowrite", &xaw
},
619 {"ic", "ignorecase", &xic
},
620 {"td", "textdirection", &xdir
},
621 {"shape", "shape", &xshape
},
622 {"order", "xorder", &xorder
},
625 static char *cutword(char *s
, char *d
)
629 while (*s
&& !isspace(*s
))
637 static int ec_set(char *ec
)
648 if (tok
[0] == 'n' && tok
[1] == 'o') {
649 strcpy(opt
, tok
+ 2);
652 char *r
= strchr(tok
, '=');
662 for (i
= 0; i
< LEN(options
); i
++) {
663 struct option
*o
= &options
[i
];
664 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
)) {
669 ex_show("unknown option");
675 static struct excmd
{
680 {"p", "print", ec_print
},
681 {"a", "append", ec_insert
},
682 {"i", "insert", ec_insert
},
683 {"d", "delete", ec_delete
},
684 {"c", "change", ec_insert
},
685 {"e", "edit", ec_edit
},
686 {"e!", "edit!", ec_edit
},
688 {"k", "mark", ec_mark
},
689 {"pu", "put", ec_put
},
690 {"q", "quit", ec_quit
},
691 {"q!", "quit!", ec_quit
},
692 {"r", "read", ec_read
},
693 {"w", "write", ec_write
},
694 {"wq", "wq", ec_write
},
695 {"u", "undo", ec_undo
},
696 {"r", "redo", ec_redo
},
697 {"se", "set", ec_set
},
698 {"s", "substitute", ec_substitute
},
699 {"ya", "yank", ec_yank
},
701 {"make", "make", ec_make
},
705 /* execute a single ex command */
706 void ex_command(char *ln
)
711 for (i
= 0; i
< LEN(excmds
); i
++) {
712 if (!strcmp(excmds
[i
].abbr
, cmd
) || !strcmp(excmds
[i
].name
, cmd
)) {
726 char *ln
= ex_read(":");
735 void ex_init(char **files
)
738 snprintf(cmd
, sizeof(cmd
), "e %s", files
[0] ? files
[0] : "");
745 for (i
= 0; i
< LEN(bufs
); i
++)