wordsmithing, print out the full path for local .exrc files that
[nvi.git] / vi / vi.c
blobcad7971085ec4fc53ed74dae2407994c2ffcb035
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: vi.c,v 8.42 1993/12/16 12:14:30 bostic Exp $ (Berkeley) $Date: 1993/12/16 12:14:30 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "vi.h"
20 #include "vcmd.h"
22 static int getcmd __P((SCR *, EXF *,
23 VICMDARG *, VICMDARG *, VICMDARG *, int *));
24 static inline int
25 getcount __P((SCR *, ARG_CHAR_T, u_long *));
26 static inline int
27 getkey __P((SCR *, CH *, u_int));
28 static int getkeyword __P((SCR *, EXF *, VICMDARG *, u_int));
29 static int getmotion __P((SCR *, EXF *,
30 VICMDARG *, VICMDARG *, MARK *, MARK *));
33 * Side-effect:
34 * The dot structure can be set by the underlying vi functions,
35 * see v_Put() and v_put().
37 #define DOT (&VIP(sp)->sdot)
38 #define DOTMOTION (&VIP(sp)->sdotmotion)
41 * vi --
42 * Main vi command loop.
44 int
45 vi(sp, ep)
46 SCR *sp;
47 EXF *ep;
49 MARK abs, fm, tm, m;
50 VICMDARG cmd, *vp;
51 u_int flags, saved_mode;
52 int comcount, eval;
54 /* Start vi. */
55 if (v_init(sp, ep))
56 return (1);
58 /* Paint the screen. */
59 if (sp->s_refresh(sp, ep)) {
60 (void)v_end(sp);
61 return (1);
64 /* Command initialization. */
65 memset(&cmd, 0, sizeof(VICMDARG));
67 for (eval = 0, vp = &cmd;;) {
68 if (!MAPPED_KEYS_WAITING(sp) && log_cursor(sp, ep))
69 goto err;
72 * We get a command, which may or may not have an associated
73 * motion. If it does, we get it too, calling its underlying
74 * function to get the resulting mark. We then call the
75 * command setting the cursor to the resulting mark.
77 if (getcmd(sp, ep, DOT, vp, NULL, &comcount))
78 goto err;
81 * Historical practice: if a dot command gets a new count,
82 * any motion component goes away, i.e. "d3w2." deletes a
83 * total of 5 words.
85 if (F_ISSET(vp, VC_ISDOT) && comcount)
86 DOTMOTION->count = 1;
88 /* Get any associated keyword. */
89 flags = vp->kp->flags;
90 if (LF_ISSET(V_KEYNUM | V_KEYW) &&
91 getkeyword(sp, ep, vp, flags))
92 goto err;
94 /* If a non-relative movement, copy the future absolute mark. */
95 if (LF_ISSET(V_ABS)) {
96 abs.lno = sp->lno;
97 abs.cno = sp->cno;
101 * Do any required motion; getmotion sets the from MARK
102 * and the line mode flag.
104 if (LF_ISSET(V_MOTION)) {
105 if (getmotion(sp, ep, DOTMOTION, vp, &fm, &tm))
106 goto err;
107 } else {
109 * Set everything to the current cursor position.
110 * Line commands (ex: Y) default to the current line.
112 tm.lno = fm.lno = sp->lno;
113 tm.cno = fm.cno = sp->cno;
116 * Set line mode flag, for example, "yy".
118 * If a count is set, we set the to MARK here relative
119 * to the cursor/from MARK. This is done for commands
120 * that take both counts and motions, i.e. "4yy" and
121 * "y%" -- there's no way the command can known which
122 * the user did, so we have to do it here. There are
123 * other commands that are line mode commands and take
124 * counts ("#G", "#H") and for which this calculation
125 * is either meaningless or wrong. Each command must
126 * do its own validity checking of the value.
128 if (F_ISSET(vp->kp, V_LMODE)) {
129 F_SET(vp, VC_LMODE);
130 if (F_ISSET(vp, VC_C1SET)) {
131 tm.lno = sp->lno + vp->count - 1;
132 tm.cno = sp->cno;
137 /* Increment the command count. */
138 ++sp->ccnt;
141 * Call the function. Set the return cursor to the current
142 * cursor position first -- the underlying routines don't
143 * bother to do the work if it doesn't move.
145 m.lno = sp->lno;
146 m.cno = sp->cno;
147 saved_mode = F_ISSET(sp, S_SCREENS | S_MAJOR_CHANGE);
148 if ((vp->kp->func)(sp, ep, vp, &fm, &tm, &m))
149 goto err;
150 #ifdef DEBUG
151 /* Make sure no function left the temporary space locked. */
152 if (F_ISSET(sp->gp, G_TMP_INUSE)) {
153 msgq(sp, M_ERR,
154 "Error: vi: temporary buffer not released.");
155 return (1);
157 #endif
159 * If that command took us out of vi or changed the screen,
160 * then exit the loop without further action.
162 if (saved_mode != F_ISSET(sp, S_SCREENS | S_MAJOR_CHANGE))
163 break;
165 /* Set the absolute mark. */
166 if (LF_ISSET(V_ABS) && mark_set(sp, ep, ABSMARK1, &abs, 1))
167 goto err;
169 /* Set the dot command structure. */
170 if (LF_ISSET(V_DOT)) {
171 *DOT = cmd;
172 F_SET(DOT, VC_ISDOT);
174 * If a count was supplied for both the command and
175 * its motion, the count was used only for the motion.
176 * Turn the count back on for the dot structure.
178 if (F_ISSET(vp, VC_C1RESET))
179 F_SET(DOT, VC_C1SET);
183 * Some vi row movements are "attracted" to the last position
184 * set, i.e. the V_RCM commands are moths to the V_RCM_SET
185 * commands' candle. It's broken into two parts. Here we deal
186 * with the command flags. In sp->relative(), we deal with the
187 * screen flags. If the movement is to the EOL the vi command
188 * handles it. If it's to the beginning, we handle it here.
190 * Note, some commands (e.g. _, ^) don't set the V_RCM_SETFNB
191 * flag, but do the work themselves. The reason is that they
192 * have to modify the column in case they're being used as a
193 * motion component. Other similar commands (e.g. +, -) don't
194 * have to modify the column because they are always line mode
195 * operations when used as motions, so the column number isn't
196 * of any interest.
198 * Does this totally violate the screen and editor layering?
199 * You betcha. As they say, if you think you understand it,
200 * you don't.
202 switch (LF_ISSET(V_RCM | V_RCM_SETFNB |
203 V_RCM_SETLAST | V_RCM_SETLFNB | V_RCM_SETNNB)) {
204 case 0:
205 break;
206 case V_RCM:
207 m.cno = sp->s_relative(sp, ep, m.lno);
208 break;
209 case V_RCM_SETLAST:
210 sp->rcmflags = RCM_LAST;
211 break;
212 case V_RCM_SETLFNB:
213 if (fm.lno != m.lno) {
214 if (nonblank(sp, ep, m.lno, &m.cno))
215 goto err;
216 sp->rcmflags = RCM_FNB;
218 break;
219 case V_RCM_SETFNB:
220 m.cno = 0;
221 /* FALLTHROUGH */
222 case V_RCM_SETNNB:
223 if (nonblank(sp, ep, m.lno, &m.cno))
224 goto err;
225 sp->rcmflags = RCM_FNB;
226 break;
227 default:
228 abort();
231 /* Update the cursor. */
232 sp->lno = m.lno;
233 sp->cno = m.cno;
235 if (!MAPPED_KEYS_WAITING(sp)) {
236 (void)msg_rpt(sp, 1);
238 if (0)
239 err: term_map_flush(sp, "Vi error");
242 /* Refresh the screen. */
243 if (sp->s_refresh(sp, ep)) {
244 eval = 1;
245 break;
248 /* Set the new favorite position. */
249 if (LF_ISSET(V_RCM_SET)) {
250 sp->rcmflags = 0;
251 (void)sp->s_column(sp, ep, &sp->rcm);
255 return (v_end(sp) || eval);
258 #define KEY(key, map) { \
259 if (getkey(sp, &ikey, map)) \
260 return (1); \
261 key = ikey.ch; \
265 * getcmd --
267 * The command structure for vi is less complex than ex (and don't think
268 * I'm not grateful!) The command syntax is:
270 * [count] [buffer] [count] key [[motion] | [buffer] [character]]
272 * and there are several special cases. The motion value is itself a vi
273 * command, with the syntax:
275 * [count] key [character]
277 static int
278 getcmd(sp, ep, dp, vp, ismotion, comcountp)
279 SCR *sp;
280 EXF *ep;
281 VICMDARG *dp, *vp;
282 VICMDARG *ismotion; /* Previous key if getting motion component. */
283 int *comcountp;
285 VIKEYS const *kp;
286 u_int flags;
287 CH ikey;
288 CHAR_T key;
290 /* Refresh the command structure. */
291 memset(&vp->vp_startzero, 0,
292 (char *)&vp->vp_endzero - (char *)&vp->vp_startzero);
294 /* An escape bells the user if in command mode. */
295 if (getkey(sp, &ikey, TXT_MAPCOMMAND)) {
296 if (ikey.value == K_ESCAPE && ismotion == NULL)
297 msgq(sp, M_BERR, "Already in command mode");
298 return (1);
301 key = ikey.ch;
302 if (key > MAXVIKEY) {
303 msgq(sp, M_BERR, "%s isn't a vi command", charname(sp, key));
304 return (1);
307 /* Pick up optional buffer. */
308 if (key == '"') {
309 KEY(vp->buffer, 0);
310 F_SET(vp, VC_BUFFER);
311 KEY(key, TXT_MAPCOMMAND);
315 * Pick up optional count, where a leading 0 is not a count,
316 * it's a command.
318 if (isdigit(key) && key != '0') {
319 if (getcount(sp, key, &vp->count))
320 return (1);
321 F_SET(vp, VC_C1SET);
322 *comcountp = 1;
323 KEY(key, TXT_MAPCOMMAND);
324 } else
325 *comcountp = 0;
327 /* Pick up optional buffer. */
328 if (key == '"') {
329 if (F_ISSET(vp, VC_BUFFER)) {
330 msgq(sp, M_ERR, "Only one buffer can be specified.");
331 return (1);
333 KEY(vp->buffer, 0);
334 F_SET(vp, VC_BUFFER);
335 KEY(key, TXT_MAPCOMMAND);
339 * Find the command. The only legal command with no underlying
340 * function is dot.
342 kp = vp->kp = &vikeys[vp->key = key];
343 if (kp->func == NULL) {
344 if (key != '.') {
345 msgq(sp, M_ERR,
346 "%s isn't a command", charname(sp, key));
347 return (1);
350 /* If called for a motion command, stop now. */
351 if (dp == NULL)
352 goto usage;
354 /* A repeatable command must have been executed. */
355 if (!F_ISSET(dp, VC_ISDOT)) {
356 msgq(sp, M_ERR, "No command to repeat.");
357 return (1);
360 /* Set new count/buffer, if any, and return. */
361 if (F_ISSET(vp, VC_C1SET)) {
362 F_SET(dp, VC_C1SET);
363 dp->count = vp->count;
365 if (F_ISSET(vp, VC_BUFFER))
366 dp->buffer = vp->buffer;
367 *vp = *dp;
368 return (0);
371 flags = kp->flags;
373 /* Check for illegal count. */
374 if (F_ISSET(vp, VC_C1SET) && !LF_ISSET(V_CNT))
375 goto usage;
377 /* Illegal motion command. */
378 if (ismotion == NULL) {
379 /* Illegal buffer. */
380 if (!LF_ISSET(V_OBUF) && F_ISSET(vp, VC_BUFFER))
381 goto usage;
383 /* Required buffer. */
384 if (LF_ISSET(V_RBUF))
385 KEY(vp->buffer, 0);
388 * Special case: '[', ']' and 'Z' commands. Doesn't the
389 * fact that the *single* characters don't mean anything
390 * but the *doubled* characters do just frost your shorts?
392 if (vp->key == '[' || vp->key == ']' || vp->key == 'Z') {
393 KEY(key, TXT_MAPCOMMAND);
394 if (vp->key != key)
395 goto usage;
397 /* Special case: 'z' command. */
398 if (vp->key == 'z') {
399 KEY(vp->character, 0);
400 if (isdigit(vp->character)) {
401 if (getcount(sp, vp->character, &vp->count2))
402 return (1);
403 F_SET(vp, VC_C2SET);
404 KEY(vp->character, 0);
410 * Commands that have motion components can be doubled to
411 * imply the current line.
413 else if (ismotion->key != key && !LF_ISSET(V_MOVE)) {
414 usage: msgq(sp, M_ERR, "Usage: %s", ismotion != NULL ?
415 vikeys[ismotion->key].usage : kp->usage);
416 return (1);
419 /* Required character. */
420 if (LF_ISSET(V_CHAR))
421 KEY(vp->character, 0);
423 return (0);
427 * getmotion --
429 * Get resulting motion mark.
431 static int
432 getmotion(sp, ep, dm, vp, fm, tm)
433 SCR *sp;
434 EXF *ep;
435 VICMDARG *dm, *vp;
436 MARK *fm, *tm;
438 MARK m;
439 VICMDARG motion;
440 u_long cnt;
441 int notused;
443 /* If '.' command, use the dot motion, else get the motion command. */
444 if (F_ISSET(vp, VC_ISDOT)) {
445 motion = *dm;
446 F_SET(&motion, VC_ISDOT);
447 } else if (getcmd(sp, ep, NULL, &motion, vp, &notused))
448 return (1);
451 * A count may be provided both to the command and to the motion, in
452 * which case the count is multiplicative. For example, "3y4y" is the
453 * same as "12yy". This count is provided to the motion command and
454 * not to the regular function.
456 cnt = motion.count = F_ISSET(&motion, VC_C1SET) ? motion.count : 1;
457 if (F_ISSET(vp, VC_C1SET)) {
458 motion.count *= vp->count;
459 F_SET(&motion, VC_C1SET);
462 * Set flags to restore the original values of the command
463 * structure so dot commands can change the count values,
464 * e.g. "2dw" "3." deletes a total of five words.
466 F_CLR(vp, VC_C1SET);
467 F_SET(vp, VC_C1RESET);
471 * Some commands can be repeated to indicate the current line. In
472 * this case, or if the command is a "line command", set the flags
473 * appropriately. If not a doubled command, run the function to get
474 * the resulting mark.
476 if (vp->key == motion.key) {
477 F_SET(vp, VC_LMODE);
480 * Set the end of the command; the column is after the line.
482 * If the current line is missing, i.e. the file is empty,
483 * historic vi permitted a "cc" or "!!" command to insert
484 * text.
486 tm->lno = sp->lno + motion.count - 1;
487 if (file_gline(sp, ep, tm->lno, &tm->cno) == NULL) {
488 if (tm->lno != 1 || vp->key != 'c' && vp->key != '!') {
489 m.lno = sp->lno;
490 m.cno = sp->cno;
491 v_eof(sp, ep, &m);
492 return (1);
494 tm->cno = 0;
497 /* Set the origin of the command. */
498 fm->lno = sp->lno;
499 fm->cno = 0;
500 } else {
502 * Motion commands change the underlying movement (*snarl*).
503 * For example, "l" is illegal at the end of a line, but "dl"
504 * is not. Set flags so the function knows the situation.
506 F_SET(&motion, vp->kp->flags & VC_COMMASK);
509 * Everything starts at the current position. This permits
510 * commands like 'j' and 'k', that are line oriented motions
511 * and have special cursor suck semantics when they are used
512 * as standalone commands, to ignore column positioning.
514 fm->lno = tm->lno = sp->lno;
515 fm->cno = tm->cno = sp->cno;
516 if ((motion.kp->func)(sp, ep, &motion, fm, NULL, tm))
517 return (1);
520 * If the underlying motion was a line motion, set the flag
521 * in the command structure. Underlying commands can also
522 * flag the movement as a line motion (see v_sentence).
524 if (F_ISSET(motion.kp, V_LMODE) || F_ISSET(&motion, VC_LMODE))
525 F_SET(vp, VC_LMODE);
528 * If the motion is in the reverse direction, switch the from
529 * and to MARK's so that it's always in a forward direction.
530 * Because the motion is always from the from MARK to, but not
531 * including, the to MARK, the function may have modified the
532 * from MARK, so that it gets the one-past-the-place semantics
533 * we use; see v_match() for an example.
535 * !!!
536 * Historic vi changed the cursor as part of this, which made
537 * no sense. For example, "yj" would move the cursor but "yk"
538 * would not.
540 if (tm->lno < fm->lno ||
541 tm->lno == fm->lno && tm->cno < fm->cno) {
542 m = *fm;
543 *fm = *tm;
544 *tm = m;
545 #ifdef HISTORIC_MOVE_TO_START_OF_BLOCK
546 sp->lno = fm->lno;
547 sp->cno = fm->cno;
548 #endif
553 * If the command sets dot, save the motion structure. The
554 * motion count was changed above and needs to be reset, that's
555 * why this is done here, and not in the calling routine.
557 if (F_ISSET(vp->kp, V_DOT)) {
558 *dm = motion;
559 dm->count = cnt;
561 return (0);
564 #define innum(c) (isdigit(c) || strchr("abcdefABCDEF", c))
567 * getkeyword --
568 * Get the "word" the cursor is on.
570 static int
571 getkeyword(sp, ep, kp, flags)
572 SCR *sp;
573 EXF *ep;
574 VICMDARG *kp;
575 u_int flags;
577 register size_t beg, end;
578 recno_t lno;
579 size_t len;
580 char *p;
582 if ((p = file_gline(sp, ep, sp->lno, &len)) == NULL) {
583 if (file_lline(sp, ep, &lno))
584 return (1);
585 if (lno == 0)
586 v_eof(sp, ep, NULL);
587 else
588 GETLINE_ERR(sp, sp->lno);
589 return (1);
591 beg = sp->cno;
593 /* May not be a keyword at all. */
594 if (p == NULL || len == 0 ||
595 LF_ISSET(V_KEYW) && !inword(p[beg]) ||
596 LF_ISSET(V_KEYNUM) && !innum(p[beg]) &&
597 p[beg] != '-' && p[beg] != '+') {
598 noword: msgq(sp, M_BERR, "Cursor not in a %s",
599 LF_ISSET(V_KEYW) ? "word" : "number");
600 return (1);
603 /* Find the beginning/end of the keyword. */
604 if (beg != 0)
605 if (LF_ISSET(V_KEYW)) {
606 for (;;) {
607 --beg;
608 if (!inword(p[beg])) {
609 ++beg;
610 break;
612 if (beg == 0)
613 break;
615 } else {
616 for (;;) {
617 --beg;
618 if (!innum(p[beg])) {
619 if (beg > 0 && p[beg - 1] == '0' &&
620 (p[beg] == 'X' || p[beg] == 'x'))
621 --beg;
622 else
623 ++beg;
624 break;
626 if (beg == 0)
627 break;
630 /* Skip possible leading sign. */
631 if (beg != 0 && p[beg] != '0' &&
632 (p[beg - 1] == '+' || p[beg - 1] == '-'))
633 --beg;
636 if (LF_ISSET(V_KEYW)) {
637 for (end = sp->cno; ++end < len && inword(p[end]););
638 --end;
639 } else {
640 for (end = sp->cno; ++end < len;) {
641 if (p[end] == 'X' || p[end] == 'x') {
642 if (end != beg + 1 || p[beg] != '0')
643 break;
644 continue;
646 if (!innum(p[end]))
647 break;
650 /* Just a sign isn't a number. */
651 if (end == beg && (p[beg] == '+' || p[beg] == '-'))
652 goto noword;
653 --end;
657 * Getting a keyword implies moving the cursor to its beginning.
658 * Refresh now.
660 if (beg != sp->cno) {
661 sp->cno = beg;
662 sp->s_refresh(sp, ep);
666 * XXX
667 * 8-bit clean problem. Numeric keywords are handled using strtol(3)
668 * and friends. This would have to be fixed in v_increment and here
669 * to not depend on a trailing NULL.
671 len = (end - beg) + 2; /* XXX */
672 kp->klen = (end - beg) + 1;
673 BINC_RET(sp, kp->keyword, kp->kbuflen, len);
674 memmove(kp->keyword, p + beg, kp->klen);
675 kp->keyword[kp->klen] = '\0'; /* XXX */
676 return (0);
680 * getcount --
681 * Return the next count.
683 static inline int
684 getcount(sp, fkey, countp)
685 SCR *sp;
686 ARG_CHAR_T fkey;
687 u_long *countp;
689 u_long count, tc;
690 CH ikey;
692 ikey.ch = fkey;
693 count = tc = 0;
694 do {
695 /* Assume that overflow results in a smaller number. */
696 tc = count * 10 + ikey.ch - '0';
697 if (count > tc) {
698 /* Toss to the next non-digit. */
699 do {
700 if (getkey(sp, &ikey,
701 TXT_MAPCOMMAND | TXT_MAPNODIGIT))
702 return (1);
703 } while (isdigit(ikey.ch));
704 msgq(sp, M_ERR, "Number larger than %lu", ULONG_MAX);
705 return (1);
707 count = tc;
708 if (getkey(sp, &ikey, TXT_MAPCOMMAND | TXT_MAPNODIGIT))
709 return (1);
710 } while (isdigit(ikey.ch));
711 *countp = count;
712 return (0);
716 * getkey --
717 * Return the next key.
719 static inline int
720 getkey(sp, ikeyp, map)
721 SCR *sp;
722 CH *ikeyp;
723 u_int map;
725 switch (term_key(sp, ikeyp, map)) {
726 case INP_OK:
727 break;
728 case INP_EOF:
729 F_SET(sp, S_EXIT_FORCE);
730 /* FALLTHROUGH */
731 case INP_ERR:
732 return (1);
734 return (ikeyp->value == K_ESCAPE);