12 char xpath
[PATHLEN
]; /* current file */
13 char xpath_alt
[PATHLEN
]; /* alternate file */
14 int xquit
; /* exit if set */
15 int xvis
; /* visual mode */
16 int xai
= 1; /* autoindent option */
17 int xic
= 1; /* ignorecase option */
18 struct lbuf
*xb
; /* current buffer */
19 int xrow
, xcol
, xtop
; /* current row, column, and top row */
20 int xrow_alt
; /* alternate row, column, and top row */
21 int xled
= 1; /* use the line editor */
22 int xdir
= +1; /* current direction context */
24 /* read ex command location */
25 static char *ex_loc(char *s
, char *loc
)
27 while (*s
== ':' || isspace((unsigned char) *s
))
29 while (*s
&& !isalpha((unsigned char) *s
) && *s
!= '=') {
32 if (*s
== '/' || *s
== '?') {
35 while (*s
&& *s
!= d
) {
36 if (*s
== '\\' && s
[1])
47 /* read ex command name */
48 static char *ex_cmd(char *s
, char *cmd
)
52 while (isspace((unsigned char) *s
))
54 while (isalpha((unsigned char) *s
) || *s
== '=' || *s
== '!')
55 if ((*cmd
++ = *s
++) == 'k' && cmd
== cmd0
+ 1)
61 /* read ex command argument */
62 static char *ex_arg(char *s
, char *arg
)
65 while (isspace((unsigned char) *s
))
67 while (*s
&& !isspace((unsigned char) *s
))
73 static int ex_search(char *pat
)
76 int dir
= *pat
== '/' ? 1 : -1;
86 sbuf_chr(kw
, (unsigned char) *e
);
87 if (*e
== '\\' && e
[1])
90 re_kw
[0] = sbuf_buf(kw
);
91 re
= rset_make(1, re_kw
, xic
? RE_ICASE
: 0);
95 while (i
>= 0 && i
< lbuf_len(xb
)) {
96 if (rset_find(re
, lbuf_get(xb
, i
), 0, NULL
, 0) >= 0)
104 static int ex_lineno(char *num
)
107 if (!num
[0] || num
[0] == '.')
112 n
= lbuf_len(xb
) - 1;
114 n
= xrow
- (num
[1] ? ex_lineno(num
+ 1) : 1);
116 n
= xrow
+ (num
[1] ? ex_lineno(num
+ 1) : 1);
118 n
= lbuf_markpos(xb
, num
[1]);
119 if (num
[0] == '/' && num
[1])
121 if (num
[0] == '?' && num
[1])
126 /* parse ex command location */
127 static int ex_region(char *loc
, int *beg
, int *end
)
130 if (!strcmp("%", loc
)) {
132 *end
= MAX(0, lbuf_len(xb
));
137 *end
= xrow
== lbuf_len(xb
) ? xrow
: xrow
+ 1;
142 *end
= ex_lineno(loc
) + 1;
143 *beg
= naddr
++ ? end0
- 1 : *end
- 1;
146 while (*loc
&& *loc
!= ';' && *loc
!= ',')
154 if (*beg
< 0 || *beg
>= lbuf_len(xb
))
156 if (*end
< *beg
|| *end
> lbuf_len(xb
))
161 static void ec_quit(char *ec
)
166 static void ec_edit(char *ec
)
172 if (!strcmp(arg
, "%") || !arg
[0]) {
174 } else if (!strcmp(arg
, "#")) {
175 char xpath_tmp
[PATHLEN
];
177 strcpy(xpath_tmp
, xpath_alt
);
178 strcpy(xpath_alt
, xpath
);
179 strcpy(xpath
, xpath_tmp
);
185 strcpy(xpath_alt
, xpath
);
186 snprintf(xpath
, PATHLEN
, "%s", arg
);
188 xrow
= xvis
? 0 : 1 << 20;
190 fd
= open(xpath
, O_RDONLY
);
191 lbuf_rm(xb
, 0, lbuf_len(xb
));
195 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
196 xpath
, lbuf_len(xb
));
199 xrow
= MAX(0, MIN(xrow
, lbuf_len(xb
) - 1));
203 static void ec_read(char *ec
)
205 char arg
[EXLEN
], loc
[EXLEN
];
210 int n
= lbuf_len(xb
);
213 path
= arg
[0] ? arg
: xpath
;
214 fd
= open(path
, O_RDONLY
);
215 if (fd
>= 0 && !ex_region(loc
, &beg
, &end
)) {
216 lbuf_rd(xb
, fd
, lbuf_len(xb
) ? end
: 0);
218 xrow
= end
+ lbuf_len(xb
) - n
;
219 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [r]\n",
220 path
, lbuf_len(xb
) - n
);
225 static void ec_write(char *ec
)
227 char cmd
[EXLEN
], arg
[EXLEN
], loc
[EXLEN
];
235 path
= arg
[0] ? arg
: xpath
;
236 if (ex_region(loc
, &beg
, &end
))
242 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
244 lbuf_wr(xb
, fd
, beg
, end
);
246 snprintf(msg
, sizeof(msg
), "\"%s\" %d lines [w]\n",
250 if (!strcmp("wq", cmd
))
254 static void ec_insert(char *ec
)
256 char arg
[EXLEN
], cmd
[EXLEN
], loc
[EXLEN
];
264 if (ex_region(loc
, &beg
, &end
) && (beg
!= 0 || end
!= 0))
268 lbuf_rm(xb
, beg
, end
);
272 while ((s
= ex_read(""))) {
273 if (!strcmp(".", s
)) {
282 if (end
> lbuf_len(xb
))
285 lbuf_put(xb
, end
, sbuf_buf(sb
));
286 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
290 static void ec_print(char *ec
)
292 char cmd
[EXLEN
], loc
[EXLEN
];
297 if (!cmd
[0] && !loc
[0]) {
298 if (xrow
>= lbuf_len(xb
) - 1)
302 if (!ex_region(loc
, &beg
, &end
)) {
303 for (i
= beg
; i
< end
; i
++)
304 ex_show(lbuf_get(xb
, i
));
309 static void ex_yank(int reg
, int beg
, int end
)
311 char *buf
= lbuf_cp(xb
, beg
, end
);
312 reg_put(reg
, buf
, 1);
316 static void ec_delete(char *ec
)
323 if (!ex_region(loc
, &beg
, &end
) && lbuf_len(xb
)) {
324 ex_yank(arg
[0], beg
, end
);
325 lbuf_rm(xb
, beg
, end
);
330 static void ec_yank(char *ec
)
337 if (!ex_region(loc
, &beg
, &end
) && lbuf_len(xb
))
338 ex_yank(arg
[0], beg
, end
);
341 static void ec_put(char *ec
)
348 int n
= lbuf_len(xb
);
351 buf
= reg_get(arg
[0], &lnmode
);
352 if (buf
&& !ex_region(loc
, &beg
, &end
)) {
353 lbuf_put(xb
, end
, buf
);
354 xrow
= MIN(lbuf_len(xb
) - 1, end
+ lbuf_len(xb
) - n
- 1);
358 static void ec_lnum(char *ec
)
364 if (ex_region(loc
, &beg
, &end
))
366 sprintf(msg
, "%d\n", end
);
370 static void ec_undo(char *ec
)
375 static void ec_redo(char *ec
)
380 static void ec_mark(char *ec
)
382 char loc
[EXLEN
], arg
[EXLEN
];
386 if (ex_region(loc
, &beg
, &end
))
388 lbuf_mark(xb
, arg
[0], end
- 1);
391 static char *readuntil(char **src
, int delim
)
393 struct sbuf
*sbuf
= sbuf_make();
395 /* reading the pattern */
396 while (*s
&& *s
!= delim
) {
397 if (s
[0] == '\\' && s
[1])
398 sbuf_chr(sbuf
, (unsigned char) *s
++);
399 sbuf_chr(sbuf
, (unsigned char) *s
++);
401 if (*s
) /* skipping the delimiter */
404 return sbuf_done(sbuf
);
407 static void ec_substitute(char *ec
)
409 char loc
[EXLEN
], arg
[EXLEN
];
419 if (ex_region(loc
, &beg
, &end
))
421 delim
= (unsigned char) *s
++;
422 pat
= readuntil(&s
, delim
);
423 rep
= readuntil(&s
, delim
);
424 re
= rset_make(1, &pat
, xic
? RE_ICASE
: 0);
425 for (i
= beg
; i
< end
; i
++) {
426 char *ln
= lbuf_get(xb
, i
);
427 if (rset_find(re
, ln
, LEN(offs
) / 2, offs
, 0)) {
428 struct sbuf
*r
= sbuf_make();
429 sbuf_mem(r
, ln
, offs
[0]);
431 sbuf_str(r
, ln
+ offs
[1]);
432 lbuf_put(xb
, i
, sbuf_buf(r
));
433 lbuf_rm(xb
, i
+ 1, i
+ 2);
442 static struct option
{
447 {"ai", "autoindent", &xai
},
448 {"ic", "ignorecase", &xic
},
449 {"td", "textdirection", &xdir
},
452 static char *cutword(char *s
, char *d
)
456 while (*s
&& !isspace(*s
))
464 static void ec_set(char *ec
)
475 if (tok
[0] == 'n' && tok
[1] == 'o') {
476 strcpy(opt
, tok
+ 2);
479 char *r
= strchr(tok
, '=');
489 for (i
= 0; i
< LEN(options
); i
++) {
490 struct option
*o
= &options
[i
];
491 if (!strcmp(o
->abbr
, opt
) || !strcmp(o
->name
, opt
))
497 static struct excmd
{
502 {"p", "print", ec_print
},
503 {"a", "append", ec_insert
},
504 {"i", "insert", ec_insert
},
505 {"d", "delete", ec_delete
},
506 {"c", "change", ec_insert
},
507 {"e", "edit", ec_edit
},
509 {"k", "mark", ec_mark
},
510 {"pu", "put", ec_put
},
511 {"q", "quit", ec_quit
},
512 {"r", "read", ec_read
},
513 {"w", "write", ec_write
},
514 {"wq", "wq", ec_write
},
515 {"u", "undo", ec_undo
},
516 {"r", "redo", ec_redo
},
517 {"se", "set", ec_set
},
518 {"s", "substitute", ec_substitute
},
519 {"ya", "yank", ec_yank
},
523 /* execute a single ex command */
524 void ex_command(char *ln
)
529 for (i
= 0; i
< LEN(excmds
); i
++) {
530 if (!strcmp(excmds
[i
].abbr
, cmd
) || !strcmp(excmds
[i
].name
, cmd
)) {
544 char *ln
= ex_read(":");