'z' doesn't change the window size per se, only the displayed text
[nvi.git] / vi / vi.c
bloba487d158a10c084a4c12c42e689f1e47fb278db7
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.39 1993/12/02 14:44:08 bostic Exp $ (Berkeley) $Date: 1993/12/02 14:44:08 $";
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 return (v_end(sp));
62 /* Command initialization. */
63 memset(&cmd, 0, sizeof(VICMDARG));
65 for (eval = 0, vp = &cmd;;) {
66 if (!MAPPED_KEYS_WAITING(sp) && log_cursor(sp, ep))
67 goto err;
70 * We get a command, which may or may not have an associated
71 * motion. If it does, we get it too, calling its underlying
72 * function to get the resulting mark. We then call the
73 * command setting the cursor to the resulting mark.
75 if (getcmd(sp, ep, DOT, vp, NULL, &comcount))
76 goto err;
79 * Historical practice: if a dot command gets a new count,
80 * any motion component goes away, i.e. "d3w2." deletes a
81 * total of 5 words.
83 if (F_ISSET(vp, VC_ISDOT) && comcount)
84 DOTMOTION->count = 1;
86 /* Get any associated keyword. */
87 flags = vp->kp->flags;
88 if (LF_ISSET(V_KEYNUM | V_KEYW) &&
89 getkeyword(sp, ep, vp, flags))
90 goto err;
92 /* If a non-relative movement, copy the future absolute mark. */
93 if (LF_ISSET(V_ABS)) {
94 abs.lno = sp->lno;
95 abs.cno = sp->cno;
99 * Do any required motion; getmotion sets the from MARK
100 * and the line mode flag.
102 if (LF_ISSET(V_MOTION)) {
103 if (getmotion(sp, ep, DOTMOTION, vp, &fm, &tm))
104 goto err;
105 } else {
107 * Set everything to the current cursor position.
108 * Line commands (ex: Y) default to the current line.
110 tm.lno = fm.lno = sp->lno;
111 tm.cno = fm.cno = sp->cno;
114 * Set line mode flag, for example, "yy".
116 * If a count is set, we set the to MARK here relative
117 * to the cursor/from MARK. This is done for commands
118 * that take both counts and motions, i.e. "4yy" and
119 * "y%" -- there's no way the command can known which
120 * the user did, so we have to do it here. There are
121 * other commands that are line mode commands and take
122 * counts ("#G", "#H") and for which this calculation
123 * is either meaningless or wrong. Each command must
124 * do its own validity checking of the value.
126 if (F_ISSET(vp->kp, V_LMODE)) {
127 F_SET(vp, VC_LMODE);
128 if (F_ISSET(vp, VC_C1SET)) {
129 tm.lno = sp->lno + vp->count - 1;
130 tm.cno = sp->cno;
135 /* Increment the command count. */
136 ++sp->ccnt;
139 * Call the function. Set the return cursor to the current
140 * cursor position first -- the underlying routines don't
141 * bother to do the work if it doesn't move.
143 m.lno = sp->lno;
144 m.cno = sp->cno;
145 saved_mode = F_ISSET(sp, S_SCREENS | S_MAJOR_CHANGE);
146 if ((vp->kp->func)(sp, ep, vp, &fm, &tm, &m))
147 goto err;
148 #ifdef DEBUG
149 /* Make sure no function left the temporary space locked. */
150 if (F_ISSET(sp->gp, G_TMP_INUSE)) {
151 msgq(sp, M_ERR,
152 "Error: vi: temporary buffer not released.");
153 return (1);
155 #endif
157 * If that command took us out of vi or changed the screen,
158 * then exit the loop without further action.
160 if (saved_mode != F_ISSET(sp, S_SCREENS | S_MAJOR_CHANGE))
161 break;
163 /* Set the absolute mark. */
164 if (LF_ISSET(V_ABS) && mark_set(sp, ep, ABSMARK1, &abs, 1))
165 goto err;
167 /* Set the dot command structure. */
168 if (LF_ISSET(V_DOT)) {
169 *DOT = cmd;
170 F_SET(DOT, VC_ISDOT);
172 * If a count was supplied for both the command and
173 * its motion, the count was used only for the motion.
174 * Turn the count back on for the dot structure.
176 if (F_ISSET(vp, VC_C1RESET))
177 F_SET(DOT, VC_C1SET);
181 * Some vi row movements are "attracted" to the last position
182 * set, i.e. the V_RCM commands are moths to the V_RCM_SET
183 * commands' candle. It's broken into two parts. Here we deal
184 * with the command flags. In sp->relative(), we deal with the
185 * screen flags. If the movement is to the EOL the vi command
186 * handles it. If it's to the beginning, we handle it here.
188 * Does this totally violate the screen and editor layering?
189 * You betcha. As they say, if you think you understand it,
190 * you don't.
192 switch (LF_ISSET(V_RCM | V_RCM_SETFNB |
193 V_RCM_SETLAST | V_RCM_SETLFNB | V_RCM_SETNNB)) {
194 case 0:
195 break;
196 case V_RCM:
197 m.cno = sp->s_relative(sp, ep, m.lno);
198 break;
199 case V_RCM_SETLAST:
200 sp->rcmflags = RCM_LAST;
201 break;
202 case V_RCM_SETLFNB:
203 if (fm.lno != m.lno) {
204 if (nonblank(sp, ep, m.lno, &m.cno))
205 goto err;
206 sp->rcmflags = RCM_FNB;
208 break;
209 case V_RCM_SETFNB:
210 m.cno = 0;
211 /* FALLTHROUGH */
212 case V_RCM_SETNNB:
213 if (nonblank(sp, ep, m.lno, &m.cno))
214 goto err;
215 sp->rcmflags = RCM_FNB;
216 break;
217 default:
218 abort();
221 /* Update the cursor. */
222 sp->lno = m.lno;
223 sp->cno = m.cno;
225 if (!MAPPED_KEYS_WAITING(sp)) {
226 (void)msg_rpt(sp, 1);
228 if (0)
229 err: term_map_flush(sp, "Vi error");
232 /* Refresh the screen. */
233 if (sp->s_refresh(sp, ep)) {
234 eval = 1;
235 break;
238 /* Set the new favorite position. */
239 if (LF_ISSET(V_RCM_SET)) {
240 sp->rcmflags = 0;
241 (void)sp->s_column(sp, ep, &sp->rcm);
245 return (v_end(sp) || eval);
248 #define KEY(key, map) { \
249 if (getkey(sp, &ikey, map)) \
250 return (1); \
251 key = ikey.ch; \
255 * getcmd --
257 * The command structure for vi is less complex than ex (and don't think
258 * I'm not grateful!) The command syntax is:
260 * [count] [buffer] [count] key [[motion] | [buffer] [character]]
262 * and there are several special cases. The motion value is itself a vi
263 * command, with the syntax:
265 * [count] key [character]
267 static int
268 getcmd(sp, ep, dp, vp, ismotion, comcountp)
269 SCR *sp;
270 EXF *ep;
271 VICMDARG *dp, *vp;
272 VICMDARG *ismotion; /* Previous key if getting motion component. */
273 int *comcountp;
275 VIKEYS const *kp;
276 u_int flags;
277 CH ikey;
278 CHAR_T key;
280 /* Refresh the command structure. */
281 memset(&vp->vp_startzero, 0,
282 (char *)&vp->vp_endzero - (char *)&vp->vp_startzero);
284 /* An escape bells the user if in command mode. */
285 if (getkey(sp, &ikey, TXT_MAPCOMMAND)) {
286 if (ikey.value == K_ESCAPE && ismotion == NULL)
287 msgq(sp, M_BERR, "Already in command mode");
288 return (1);
291 key = ikey.ch;
292 if (key > MAXVIKEY) {
293 msgq(sp, M_BERR, "%s isn't a vi command", charname(sp, key));
294 return (1);
297 /* Pick up optional buffer. */
298 if (key == '"') {
299 KEY(vp->buffer, 0);
300 F_SET(vp, VC_BUFFER);
301 KEY(key, TXT_MAPCOMMAND);
305 * Pick up optional count, where a leading 0 is not a count,
306 * it's a command.
308 if (isdigit(key) && key != '0') {
309 if (getcount(sp, key, &vp->count))
310 return (1);
311 F_SET(vp, VC_C1SET);
312 *comcountp = 1;
313 KEY(key, TXT_MAPCOMMAND);
314 } else
315 *comcountp = 0;
317 /* Pick up optional buffer. */
318 if (key == '"') {
319 if (F_ISSET(vp, VC_BUFFER)) {
320 msgq(sp, M_ERR, "Only one buffer can be specified.");
321 return (1);
323 KEY(vp->buffer, 0);
324 F_SET(vp, VC_BUFFER);
325 KEY(key, TXT_MAPCOMMAND);
329 * Find the command. The only legal command with no underlying
330 * function is dot.
332 kp = vp->kp = &vikeys[vp->key = key];
333 if (kp->func == NULL) {
334 if (key != '.') {
335 msgq(sp, M_ERR,
336 "%s isn't a command", charname(sp, key));
337 return (1);
340 /* If called for a motion command, stop now. */
341 if (dp == NULL)
342 goto usage;
344 /* A repeatable command must have been executed. */
345 if (!F_ISSET(dp, VC_ISDOT)) {
346 msgq(sp, M_ERR, "No command to repeat.");
347 return (1);
350 /* Set new count/buffer, if any, and return. */
351 if (F_ISSET(vp, VC_C1SET)) {
352 F_SET(dp, VC_C1SET);
353 dp->count = vp->count;
355 if (F_ISSET(vp, VC_BUFFER))
356 dp->buffer = vp->buffer;
357 *vp = *dp;
358 return (0);
361 flags = kp->flags;
363 /* Check for illegal count. */
364 if (F_ISSET(vp, VC_C1SET) && !LF_ISSET(V_CNT))
365 goto usage;
367 /* Illegal motion command. */
368 if (ismotion == NULL) {
369 /* Illegal buffer. */
370 if (!LF_ISSET(V_OBUF) && F_ISSET(vp, VC_BUFFER))
371 goto usage;
373 /* Required buffer. */
374 if (LF_ISSET(V_RBUF))
375 KEY(vp->buffer, 0);
378 * Special case: '[', ']' and 'Z' commands. Doesn't the
379 * fact that the *single* characters don't mean anything
380 * but the *doubled* characters do just frost your shorts?
382 if (vp->key == '[' || vp->key == ']' || vp->key == 'Z') {
383 KEY(key, TXT_MAPCOMMAND);
384 if (vp->key != key)
385 goto usage;
387 /* Special case: 'z' command. */
388 if (vp->key == 'z') {
389 KEY(vp->character, 0);
390 if (isdigit(vp->character)) {
391 if (getcount(sp, vp->character, &vp->count2))
392 return (1);
393 F_SET(vp, VC_C2SET);
394 KEY(vp->character, 0);
400 * Commands that have motion components can be doubled to
401 * imply the current line.
403 else if (ismotion->key != key && !LF_ISSET(V_MOVE)) {
404 usage: msgq(sp, M_ERR, "Usage: %s", ismotion != NULL ?
405 vikeys[ismotion->key].usage : kp->usage);
406 return (1);
409 /* Required character. */
410 if (LF_ISSET(V_CHAR))
411 KEY(vp->character, 0);
413 return (0);
417 * getmotion --
419 * Get resulting motion mark.
421 static int
422 getmotion(sp, ep, dm, vp, fm, tm)
423 SCR *sp;
424 EXF *ep;
425 VICMDARG *dm, *vp;
426 MARK *fm, *tm;
428 MARK m;
429 VICMDARG motion;
430 u_long cnt;
431 int notused;
433 /* If '.' command, use the dot motion, else get the motion command. */
434 if (F_ISSET(vp, VC_ISDOT)) {
435 motion = *dm;
436 F_SET(&motion, VC_ISDOT);
437 } else if (getcmd(sp, ep, NULL, &motion, vp, &notused))
438 return (1);
441 * A count may be provided both to the command and to the motion, in
442 * which case the count is multiplicative. For example, "3y4y" is the
443 * same as "12yy". This count is provided to the motion command and
444 * not to the regular function.
446 cnt = motion.count = F_ISSET(&motion, VC_C1SET) ? motion.count : 1;
447 if (F_ISSET(vp, VC_C1SET)) {
448 motion.count *= vp->count;
449 F_SET(&motion, VC_C1SET);
452 * Set flags to restore the original values of the command
453 * structure so dot commands can change the count values,
454 * e.g. "2dw" "3." deletes a total of five words.
456 F_CLR(vp, VC_C1SET);
457 F_SET(vp, VC_C1RESET);
461 * Some commands can be repeated to indicate the current line. In
462 * this case, or if the command is a "line command", set the flags
463 * appropriately. If not a doubled command, run the function to get
464 * the resulting mark.
466 if (vp->key == motion.key) {
467 F_SET(vp, VC_LMODE);
470 * Set the end of the command; the column is after the line.
472 * If the current line is missing, i.e. the file is empty,
473 * historic vi permitted a "cc" or "!!" command to insert
474 * text.
476 tm->lno = sp->lno + motion.count - 1;
477 if (file_gline(sp, ep, tm->lno, &tm->cno) == NULL) {
478 if (tm->lno != 1 || vp->key != 'c' && vp->key != '!') {
479 m.lno = sp->lno;
480 m.cno = sp->cno;
481 v_eof(sp, ep, &m);
482 return (1);
484 tm->cno = 0;
487 /* Set the origin of the command. */
488 fm->lno = sp->lno;
489 fm->cno = 0;
490 } else {
492 * Motion commands change the underlying movement (*snarl*).
493 * For example, "l" is illegal at the end of a line, but "dl"
494 * is not. Set flags so the function knows the situation.
496 F_SET(&motion, vp->kp->flags & VC_COMMASK);
499 * Everything starts at the current position. This permits
500 * commands like 'j' and 'k', that are line oriented motions
501 * and have special cursor suck semantics when they are used
502 * as standalone commands, to ignore column positioning.
504 fm->lno = tm->lno = sp->lno;
505 fm->cno = tm->cno = sp->cno;
506 if ((motion.kp->func)(sp, ep, &motion, fm, NULL, tm))
507 return (1);
510 * If the underlying motion was a line motion, set the flag
511 * in the command structure. Underlying commands can also
512 * flag the movement as a line motion (see v_sentence).
514 if (F_ISSET(motion.kp, V_LMODE) || F_ISSET(&motion, VC_LMODE))
515 F_SET(vp, VC_LMODE);
518 * If the motion is in the reverse direction, switch the from
519 * and to MARK's so that it's always in a forward direction.
520 * Because the motion is always from the from MARK to, but not
521 * including, the to MARK, the function may have modified the
522 * from MARK, so that it gets the one-past-the-place semantics
523 * we use; see v_match() for an example.
525 * !!!
526 * Historic vi changed the cursor as part of this, which made
527 * no sense. For example, "yj" would move the cursor but "yk"
528 * would not.
530 if (tm->lno < fm->lno ||
531 tm->lno == fm->lno && tm->cno < fm->cno) {
532 m = *fm;
533 *fm = *tm;
534 *tm = m;
535 #ifdef HISTORIC_MOVE_TO_START_OF_BLOCK
536 sp->lno = fm->lno;
537 sp->cno = fm->cno;
538 #endif
543 * If the command sets dot, save the motion structure. The
544 * motion count was changed above and needs to be reset, that's
545 * why this is done here, and not in the calling routine.
547 if (F_ISSET(vp->kp, V_DOT)) {
548 *dm = motion;
549 dm->count = cnt;
551 return (0);
554 #define innum(c) (isdigit(c) || strchr("abcdefABCDEF", c))
557 * getkeyword --
558 * Get the "word" the cursor is on.
560 static int
561 getkeyword(sp, ep, kp, flags)
562 SCR *sp;
563 EXF *ep;
564 VICMDARG *kp;
565 u_int flags;
567 register size_t beg, end;
568 recno_t lno;
569 size_t len;
570 char *p;
572 if ((p = file_gline(sp, ep, sp->lno, &len)) == NULL) {
573 if (file_lline(sp, ep, &lno))
574 return (1);
575 if (lno == 0)
576 v_eof(sp, ep, NULL);
577 else
578 GETLINE_ERR(sp, sp->lno);
579 return (1);
581 beg = sp->cno;
583 /* May not be a keyword at all. */
584 if (p == NULL || len == 0 ||
585 LF_ISSET(V_KEYW) && !inword(p[beg]) ||
586 LF_ISSET(V_KEYNUM) && !innum(p[beg]) &&
587 p[beg] != '-' && p[beg] != '+') {
588 noword: msgq(sp, M_BERR, "Cursor not in a %s",
589 LF_ISSET(V_KEYW) ? "word" : "number");
590 return (1);
593 /* Find the beginning/end of the keyword. */
594 if (beg != 0)
595 if (LF_ISSET(V_KEYW)) {
596 for (;;) {
597 --beg;
598 if (!inword(p[beg])) {
599 ++beg;
600 break;
602 if (beg == 0)
603 break;
605 } else {
606 for (;;) {
607 --beg;
608 if (!innum(p[beg])) {
609 if (beg > 0 && p[beg - 1] == '0' &&
610 (p[beg] == 'X' || p[beg] == 'x'))
611 --beg;
612 else
613 ++beg;
614 break;
616 if (beg == 0)
617 break;
620 /* Skip possible leading sign. */
621 if (beg != 0 && p[beg] != '0' &&
622 (p[beg - 1] == '+' || p[beg - 1] == '-'))
623 --beg;
626 if (LF_ISSET(V_KEYW)) {
627 for (end = sp->cno; ++end < len && inword(p[end]););
628 --end;
629 } else {
630 for (end = sp->cno; ++end < len;) {
631 if (p[end] == 'X' || p[end] == 'x') {
632 if (end != beg + 1 || p[beg] != '0')
633 break;
634 continue;
636 if (!innum(p[end]))
637 break;
640 /* Just a sign isn't a number. */
641 if (end == beg && (p[beg] == '+' || p[beg] == '-'))
642 goto noword;
643 --end;
647 * Getting a keyword implies moving the cursor to its beginning.
648 * Refresh now.
650 if (beg != sp->cno) {
651 sp->cno = beg;
652 sp->s_refresh(sp, ep);
656 * XXX
657 * 8-bit clean problem. Numeric keywords are handled using strtol(3)
658 * and friends. This would have to be fixed in v_increment and here
659 * to not depend on a trailing NULL.
661 len = (end - beg) + 2; /* XXX */
662 kp->klen = (end - beg) + 1;
663 BINC(sp, kp->keyword, kp->kbuflen, len);
664 memmove(kp->keyword, p + beg, kp->klen);
665 kp->keyword[kp->klen] = '\0'; /* XXX */
666 return (0);
670 * getcount --
671 * Return the next count.
673 static inline int
674 getcount(sp, fkey, countp)
675 SCR *sp;
676 ARG_CHAR_T fkey;
677 u_long *countp;
679 u_long count, tc;
680 CH ikey;
682 ikey.ch = fkey;
683 count = tc = 0;
684 do {
685 /* Assume that overflow results in a smaller number. */
686 tc = count * 10 + ikey.ch - '0';
687 if (count > tc) {
688 /* Toss to the next non-digit. */
689 do {
690 if (getkey(sp, &ikey,
691 TXT_MAPCOMMAND | TXT_MAPNODIGIT))
692 return (1);
693 } while (isdigit(ikey.ch));
694 msgq(sp, M_ERR, "Number larger than %lu", ULONG_MAX);
695 return (1);
697 count = tc;
698 if (getkey(sp, &ikey, TXT_MAPCOMMAND | TXT_MAPNODIGIT))
699 return (1);
700 } while (isdigit(ikey.ch));
701 *countp = count;
702 return (0);
706 * getkey --
707 * Return the next key.
709 static inline int
710 getkey(sp, ikeyp, map)
711 SCR *sp;
712 CH *ikeyp;
713 u_int map;
715 switch (term_key(sp, ikeyp, map)) {
716 case INP_OK:
717 break;
718 case INP_EOF:
719 F_SET(sp, S_EXIT_FORCE);
720 /* FALLTHROUGH */
721 case INP_ERR:
722 return (1);
724 return (ikeyp->value == K_ESCAPE);