2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
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 $";
12 #include <sys/types.h>
22 static int getcmd
__P((SCR
*, EXF
*,
23 VICMDARG
*, VICMDARG
*, VICMDARG
*, int *));
25 getcount
__P((SCR
*, ARG_CHAR_T
, u_long
*));
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
*));
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)
42 * Main vi command loop.
51 u_int flags
, saved_mode
;
58 /* Paint the screen. */
59 if (sp
->s_refresh(sp
, ep
))
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
))
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
))
79 * Historical practice: if a dot command gets a new count,
80 * any motion component goes away, i.e. "d3w2." deletes a
83 if (F_ISSET(vp
, VC_ISDOT
) && comcount
)
86 /* Get any associated keyword. */
87 flags
= vp
->kp
->flags
;
88 if (LF_ISSET(V_KEYNUM
| V_KEYW
) &&
89 getkeyword(sp
, ep
, vp
, flags
))
92 /* If a non-relative movement, copy the future absolute mark. */
93 if (LF_ISSET(V_ABS
)) {
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
))
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
)) {
128 if (F_ISSET(vp
, VC_C1SET
)) {
129 tm
.lno
= sp
->lno
+ vp
->count
- 1;
135 /* Increment the command count. */
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.
145 saved_mode
= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
);
146 if ((vp
->kp
->func
)(sp
, ep
, vp
, &fm
, &tm
, &m
))
149 /* Make sure no function left the temporary space locked. */
150 if (F_ISSET(sp
->gp
, G_TMP_INUSE
)) {
152 "Error: vi: temporary buffer not released.");
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
))
163 /* Set the absolute mark. */
164 if (LF_ISSET(V_ABS
) && mark_set(sp
, ep
, ABSMARK1
, &abs
, 1))
167 /* Set the dot command structure. */
168 if (LF_ISSET(V_DOT
)) {
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,
192 switch (LF_ISSET(V_RCM
| V_RCM_SETFNB
|
193 V_RCM_SETLAST
| V_RCM_SETLFNB
| V_RCM_SETNNB
)) {
197 m
.cno
= sp
->s_relative(sp
, ep
, m
.lno
);
200 sp
->rcmflags
= RCM_LAST
;
203 if (fm
.lno
!= m
.lno
) {
204 if (nonblank(sp
, ep
, m
.lno
, &m
.cno
))
206 sp
->rcmflags
= RCM_FNB
;
213 if (nonblank(sp
, ep
, m
.lno
, &m
.cno
))
215 sp
->rcmflags
= RCM_FNB
;
221 /* Update the cursor. */
225 if (!MAPPED_KEYS_WAITING(sp
)) {
226 (void)msg_rpt(sp
, 1);
229 err
: term_map_flush(sp
, "Vi error");
232 /* Refresh the screen. */
233 if (sp
->s_refresh(sp
, ep
)) {
238 /* Set the new favorite position. */
239 if (LF_ISSET(V_RCM_SET
)) {
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)) \
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]
268 getcmd(sp
, ep
, dp
, vp
, ismotion
, comcountp
)
272 VICMDARG
*ismotion
; /* Previous key if getting motion component. */
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");
292 if (key
> MAXVIKEY
) {
293 msgq(sp
, M_BERR
, "%s isn't a vi command", charname(sp
, key
));
297 /* Pick up optional buffer. */
300 F_SET(vp
, VC_BUFFER
);
301 KEY(key
, TXT_MAPCOMMAND
);
305 * Pick up optional count, where a leading 0 is not a count,
308 if (isdigit(key
) && key
!= '0') {
309 if (getcount(sp
, key
, &vp
->count
))
313 KEY(key
, TXT_MAPCOMMAND
);
317 /* Pick up optional buffer. */
319 if (F_ISSET(vp
, VC_BUFFER
)) {
320 msgq(sp
, M_ERR
, "Only one buffer can be specified.");
324 F_SET(vp
, VC_BUFFER
);
325 KEY(key
, TXT_MAPCOMMAND
);
329 * Find the command. The only legal command with no underlying
332 kp
= vp
->kp
= &vikeys
[vp
->key
= key
];
333 if (kp
->func
== NULL
) {
336 "%s isn't a command", charname(sp
, key
));
340 /* If called for a motion command, stop now. */
344 /* A repeatable command must have been executed. */
345 if (!F_ISSET(dp
, VC_ISDOT
)) {
346 msgq(sp
, M_ERR
, "No command to repeat.");
350 /* Set new count/buffer, if any, and return. */
351 if (F_ISSET(vp
, VC_C1SET
)) {
353 dp
->count
= vp
->count
;
355 if (F_ISSET(vp
, VC_BUFFER
))
356 dp
->buffer
= vp
->buffer
;
363 /* Check for illegal count. */
364 if (F_ISSET(vp
, VC_C1SET
) && !LF_ISSET(V_CNT
))
367 /* Illegal motion command. */
368 if (ismotion
== NULL
) {
369 /* Illegal buffer. */
370 if (!LF_ISSET(V_OBUF
) && F_ISSET(vp
, VC_BUFFER
))
373 /* Required buffer. */
374 if (LF_ISSET(V_RBUF
))
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
);
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
))
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
);
409 /* Required character. */
410 if (LF_ISSET(V_CHAR
))
411 KEY(vp
->character
, 0);
419 * Get resulting motion mark.
422 getmotion(sp
, ep
, dm
, vp
, fm
, tm
)
433 /* If '.' command, use the dot motion, else get the motion command. */
434 if (F_ISSET(vp
, VC_ISDOT
)) {
436 F_SET(&motion
, VC_ISDOT
);
437 } else if (getcmd(sp
, ep
, NULL
, &motion
, vp
, ¬used
))
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.
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
) {
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
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
!= '!') {
487 /* Set the origin of the command. */
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
))
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
))
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.
526 * Historic vi changed the cursor as part of this, which made
527 * no sense. For example, "yj" would move the cursor but "yk"
530 if (tm
->lno
< fm
->lno
||
531 tm
->lno
== fm
->lno
&& tm
->cno
< fm
->cno
) {
535 #ifdef HISTORIC_MOVE_TO_START_OF_BLOCK
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
)) {
554 #define innum(c) (isdigit(c) || strchr("abcdefABCDEF", c))
558 * Get the "word" the cursor is on.
561 getkeyword(sp
, ep
, kp
, flags
)
567 register size_t beg
, end
;
572 if ((p
= file_gline(sp
, ep
, sp
->lno
, &len
)) == NULL
) {
573 if (file_lline(sp
, ep
, &lno
))
578 GETLINE_ERR(sp
, sp
->lno
);
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");
593 /* Find the beginning/end of the keyword. */
595 if (LF_ISSET(V_KEYW
)) {
598 if (!inword(p
[beg
])) {
608 if (!innum(p
[beg
])) {
609 if (beg
> 0 && p
[beg
- 1] == '0' &&
610 (p
[beg
] == 'X' || p
[beg
] == 'x'))
620 /* Skip possible leading sign. */
621 if (beg
!= 0 && p
[beg
] != '0' &&
622 (p
[beg
- 1] == '+' || p
[beg
- 1] == '-'))
626 if (LF_ISSET(V_KEYW
)) {
627 for (end
= sp
->cno
; ++end
< len
&& inword(p
[end
]););
630 for (end
= sp
->cno
; ++end
< len
;) {
631 if (p
[end
] == 'X' || p
[end
] == 'x') {
632 if (end
!= beg
+ 1 || p
[beg
] != '0')
640 /* Just a sign isn't a number. */
641 if (end
== beg
&& (p
[beg
] == '+' || p
[beg
] == '-'))
647 * Getting a keyword implies moving the cursor to its beginning.
650 if (beg
!= sp
->cno
) {
652 sp
->s_refresh(sp
, ep
);
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 */
671 * Return the next count.
674 getcount(sp
, fkey
, countp
)
685 /* Assume that overflow results in a smaller number. */
686 tc
= count
* 10 + ikey
.ch
- '0';
688 /* Toss to the next non-digit. */
690 if (getkey(sp
, &ikey
,
691 TXT_MAPCOMMAND
| TXT_MAPNODIGIT
))
693 } while (isdigit(ikey
.ch
));
694 msgq(sp
, M_ERR
, "Number larger than %lu", ULONG_MAX
);
698 if (getkey(sp
, &ikey
, TXT_MAPCOMMAND
| TXT_MAPNODIGIT
))
700 } while (isdigit(ikey
.ch
));
707 * Return the next key.
710 getkey(sp
, ikeyp
, map
)
715 switch (term_key(sp
, ikeyp
, map
)) {
719 F_SET(sp
, S_EXIT_FORCE
);
724 return (ikeyp
->value
== K_ESCAPE
);