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.36 1993/11/22 17:28:45 bostic Exp $ (Berkeley) $Date: 1993/11/22 17:28:45 $";
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
*, CHAR_T
*, 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 (!TERM_MORE(sp
->gp
->key
) && 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 (!TERM_MORE(sp
->gp
->key
)) {
226 (void)msg_rpt(sp
, 1);
229 err
: TERM_FLUSH(sp
->gp
->key
);
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, &key, map)) \
256 * The command structure for vi is less complex than ex (and don't think
257 * I'm not grateful!) The command syntax is:
259 * [count] [buffer] [count] key [[motion] | [buffer] [character]]
261 * and there are several special cases. The motion value is itself a vi
262 * command, with the syntax:
264 * [count] key [character]
267 getcmd(sp
, ep
, dp
, vp
, ismotion
, comcountp
)
271 VICMDARG
*ismotion
; /* Previous key if getting motion component. */
278 /* Refresh the command structure. */
279 memset(&vp
->vp_startzero
, 0,
280 (char *)&vp
->vp_endzero
- (char *)&vp
->vp_startzero
);
282 /* An escape bells the user if in command mode. */
283 if (getkey(sp
, &key
, TXT_MAPCOMMAND
)) {
284 if (sp
->special
[key
] == K_ESCAPE
&& ismotion
== NULL
)
285 msgq(sp
, M_BERR
, "Already in command mode");
289 if (key
> MAXVIKEY
) {
290 msgq(sp
, M_BERR
, "%s isn't a vi command", charname(sp
, key
));
294 /* Pick up optional buffer. */
297 F_SET(vp
, VC_BUFFER
);
298 KEY(key
, TXT_MAPCOMMAND
);
302 * Pick up optional count, where a leading 0 is not a count,
305 if (isdigit(key
) && key
!= '0') {
306 if (getcount(sp
, key
, &vp
->count
))
310 KEY(key
, TXT_MAPCOMMAND
);
314 /* Pick up optional buffer. */
316 if (F_ISSET(vp
, VC_BUFFER
)) {
317 msgq(sp
, M_ERR
, "Only one buffer can be specified.");
321 F_SET(vp
, VC_BUFFER
);
322 KEY(key
, TXT_MAPCOMMAND
);
326 * Find the command. The only legal command with no underlying
329 kp
= vp
->kp
= &vikeys
[vp
->key
= key
];
330 if (kp
->func
== NULL
) {
333 "%s isn't a command", charname(sp
, key
));
337 /* If called for a motion command, stop now. */
341 /* A repeatable command must have been executed. */
342 if (!F_ISSET(dp
, VC_ISDOT
)) {
343 msgq(sp
, M_ERR
, "No command to repeat.");
347 /* Set new count/buffer, if any, and return. */
348 if (F_ISSET(vp
, VC_C1SET
)) {
350 dp
->count
= vp
->count
;
352 if (F_ISSET(vp
, VC_BUFFER
))
353 dp
->buffer
= vp
->buffer
;
360 /* Check for illegal count. */
361 if (F_ISSET(vp
, VC_C1SET
) && !LF_ISSET(V_CNT
))
364 /* Illegal motion command. */
365 if (ismotion
== NULL
) {
366 /* Illegal buffer. */
367 if (!LF_ISSET(V_OBUF
) && F_ISSET(vp
, VC_BUFFER
))
370 /* Required buffer. */
371 if (LF_ISSET(V_RBUF
))
375 * Special case: '[', ']' and 'Z' commands. Doesn't the
376 * fact that the *single* characters don't mean anything
377 * but the *doubled* characters do just frost your shorts?
379 if (vp
->key
== '[' || vp
->key
== ']' || vp
->key
== 'Z') {
380 KEY(key
, TXT_MAPCOMMAND
);
384 /* Special case: 'z' command. */
385 if (vp
->key
== 'z') {
386 KEY(vp
->character
, 0);
387 if (isdigit(vp
->character
)) {
388 if (getcount(sp
, key
, &vp
->count2
))
391 KEY(vp
->character
, 0);
397 * Commands that have motion components can be doubled to
398 * imply the current line.
400 else if (ismotion
->key
!= key
&& !LF_ISSET(V_MOVE
)) {
401 usage
: msgq(sp
, M_ERR
, "Usage: %s", ismotion
!= NULL
?
402 vikeys
[ismotion
->key
].usage
: kp
->usage
);
406 /* Required character. */
407 if (LF_ISSET(V_CHAR
))
408 KEY(vp
->character
, 0);
416 * Get resulting motion mark.
419 getmotion(sp
, ep
, dm
, vp
, fm
, tm
)
430 /* If '.' command, use the dot motion, else get the motion command. */
431 if (F_ISSET(vp
, VC_ISDOT
)) {
433 F_SET(&motion
, VC_ISDOT
);
434 } else if (getcmd(sp
, ep
, NULL
, &motion
, vp
, ¬used
))
438 * A count may be provided both to the command and to the motion, in
439 * which case the count is multiplicative. For example, "3y4y" is the
440 * same as "12yy". This count is provided to the motion command and
441 * not to the regular function.
443 cnt
= motion
.count
= F_ISSET(&motion
, VC_C1SET
) ? motion
.count
: 1;
444 if (F_ISSET(vp
, VC_C1SET
)) {
445 motion
.count
*= vp
->count
;
446 F_SET(&motion
, VC_C1SET
);
449 * Set flags to restore the original values of the command
450 * structure so dot commands can change the count values,
451 * e.g. "2dw" "3." deletes a total of five words.
454 F_SET(vp
, VC_C1RESET
);
458 * Some commands can be repeated to indicate the current line. In
459 * this case, or if the command is a "line command", set the flags
460 * appropriately. If not a doubled command, run the function to get
461 * the resulting mark.
463 if (vp
->key
== motion
.key
) {
467 * Set the end of the command; the column is after the line.
469 * If the current line is missing, i.e. the file is empty,
470 * historic vi permitted a "cc" or "!!" command to insert
473 tm
->lno
= sp
->lno
+ motion
.count
- 1;
474 if (file_gline(sp
, ep
, tm
->lno
, &tm
->cno
) == NULL
) {
475 if (tm
->lno
!= 1 || vp
->key
!= 'c' && vp
->key
!= '!') {
484 /* Set the origin of the command. */
489 * Motion commands change the underlying movement (*snarl*).
490 * For example, "l" is illegal at the end of a line, but "dl"
491 * is not. Set flags so the function knows the situation.
493 F_SET(&motion
, vp
->kp
->flags
& VC_COMMASK
);
496 * Everything starts at the current position. This permits
497 * commands like 'j' and 'k', that are line oriented motions
498 * and have special cursor suck semantics when they are used
499 * as standalone commands, to ignore column positioning.
501 fm
->lno
= tm
->lno
= sp
->lno
;
502 fm
->cno
= tm
->cno
= sp
->cno
;
503 if ((motion
.kp
->func
)(sp
, ep
, &motion
, fm
, NULL
, tm
))
507 * If the underlying motion was a line motion, set the flag
508 * in the command structure. Underlying commands can also
509 * flag the movement as a line motion (see v_sentence).
511 if (F_ISSET(motion
.kp
, V_LMODE
) || F_ISSET(&motion
, VC_LMODE
))
515 * If the motion is in the reverse direction, switch the from
516 * and to MARK's so that it's always in a forward direction.
517 * Because the motion is always from the from MARK to, but not
518 * including, the to MARK, the function may have modified the
519 * from MARK, so that it gets the one-past-the-place semantics
520 * we use; see v_match() for an example.
523 * Historic vi changed the cursor as part of this, which made
524 * no sense. For example, "yj" would move the cursor but "yk"
527 if (tm
->lno
< fm
->lno
||
528 tm
->lno
== fm
->lno
&& tm
->cno
< fm
->cno
) {
532 #ifdef HISTORIC_MOVE_TO_START_OF_BLOCK
540 * If the command sets dot, save the motion structure. The
541 * motion count was changed above and needs to be reset, that's
542 * why this is done here, and not in the calling routine.
544 if (F_ISSET(vp
->kp
, V_DOT
)) {
551 #define innum(c) (isdigit(c) || strchr("abcdefABCDEF", c))
555 * Get the "word" the cursor is on.
558 getkeyword(sp
, ep
, kp
, flags
)
564 register size_t beg
, end
;
569 if ((p
= file_gline(sp
, ep
, sp
->lno
, &len
)) == NULL
) {
570 if (file_lline(sp
, ep
, &lno
))
575 GETLINE_ERR(sp
, sp
->lno
);
580 /* May not be a keyword at all. */
581 if (p
== NULL
|| len
== 0 ||
582 LF_ISSET(V_KEYW
) && !inword(p
[beg
]) ||
583 LF_ISSET(V_KEYNUM
) && !innum(p
[beg
]) &&
584 p
[beg
] != '-' && p
[beg
] != '+') {
585 noword
: msgq(sp
, M_BERR
, "Cursor not in a %s",
586 LF_ISSET(V_KEYW
) ? "word" : "number");
590 /* Find the beginning/end of the keyword. */
592 if (LF_ISSET(V_KEYW
)) {
595 if (!inword(p
[beg
])) {
605 if (!innum(p
[beg
])) {
606 if (beg
> 0 && p
[beg
- 1] == '0' &&
607 (p
[beg
] == 'X' || p
[beg
] == 'x'))
617 /* Skip possible leading sign. */
618 if (beg
!= 0 && p
[beg
] != '0' &&
619 (p
[beg
- 1] == '+' || p
[beg
- 1] == '-'))
623 if (LF_ISSET(V_KEYW
)) {
624 for (end
= sp
->cno
; ++end
< len
&& inword(p
[end
]););
627 for (end
= sp
->cno
; ++end
< len
;) {
628 if (p
[end
] == 'X' || p
[end
] == 'x') {
629 if (end
!= beg
+ 1 || p
[beg
] != '0')
637 /* Just a sign isn't a number. */
638 if (end
== beg
&& (p
[beg
] == '+' || p
[beg
] == '-'))
644 * Getting a keyword implies moving the cursor to its beginning.
647 if (beg
!= sp
->cno
) {
649 sp
->s_refresh(sp
, ep
);
654 * 8-bit clean problem. Numeric keywords are handled using strtol(3)
655 * and friends. This would have to be fixed in v_increment and here
656 * to not depend on a trailing NULL.
658 len
= (end
- beg
) + 2; /* XXX */
659 kp
->klen
= (end
- beg
) + 1;
660 BINC(sp
, kp
->keyword
, kp
->kbuflen
, len
);
661 memmove(kp
->keyword
, p
+ beg
, kp
->klen
);
662 kp
->keyword
[kp
->klen
] = '\0'; /* XXX */
668 * Return the next count.
671 getcount(sp
, fkey
, countp
)
682 /* Assume that overflow results in a smaller number. */
683 tc
= count
* 10 + key
- '0';
685 /* Toss to the next non-digit. */
688 TXT_MAPCOMMAND
| TXT_MAPNODIGIT
))
690 } while (isdigit(key
));
691 msgq(sp
, M_ERR
, "Number larger than %lu", ULONG_MAX
);
695 if (getkey(sp
, &key
, TXT_MAPCOMMAND
| TXT_MAPNODIGIT
))
697 } while (isdigit(key
));
704 * Return the next key.
707 getkey(sp
, keyp
, map
)
714 switch (term_key(sp
, keyp
, map
)) {
718 F_SET(sp
, S_EXIT_FORCE
);
723 return (sp
->special
[*keyp
] == K_ESCAPE
);