1 /* $NetBSD: vi.c,v 1.2 2008/12/05 22:51:43 christos Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: vi.c,v 10.73 2002/04/11 19:49:30 skimo Exp (Berkeley) Date: 2002/04/11 19:49:30";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
31 #include "../common/common.h"
35 GC_ERR
, GC_ERR_NOFLUSH
, GC_EVENT
, GC_FATAL
, GC_INTERRUPT
, GC_OK
39 *v_alias
__P((SCR
*, VICMD
*, VIKEYS
const *));
40 static gcret_t v_cmd
__P((SCR
*, VICMD
*, VICMD
*, VICMD
*, int *, int *));
41 static int v_count
__P((SCR
*, VICMD
*, ARG_CHAR_T
, u_long
*));
42 static void v_dtoh
__P((SCR
*));
43 static int v_init
__P((SCR
*));
44 static gcret_t v_key
__P((SCR
*, VICMD
*, int, u_int32_t
));
45 static int v_motion
__P((SCR
*, VICMD
*, VICMD
*, int *));
47 #if defined(DEBUG) && defined(COMLOG)
48 static void v_comlog
__P((SCR
*, VICMD
*));
53 * The dot structure can be set by the underlying vi functions,
54 * see v_Put() and v_put().
56 #define DOT (&VIP(sp)->sdot)
57 #define DOTMOTION (&VIP(sp)->sdotmotion)
61 * Main vi command loop.
63 * PUBLIC: int vi __P((SCR **));
74 int comcount
, mapped
, rval
;
76 /* Get the first screen. */
81 /* Initialize the command structure. */
83 memset(vp
, 0, sizeof(VICMD
));
85 /* Reset strange attraction. */
86 F_SET(vp
, VM_RCM_SET
);
88 /* Initialize the vi screen. */
93 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
95 for (vip
= VIP(sp
), rval
= 0;;) {
96 /* Resolve messages. */
97 if (!MAPPED_KEYS_WAITING(sp
) && vs_resolve(sp
, NULL
, 0))
101 * If not skipping a refresh, return to command mode and
102 * refresh the screen.
104 if (F_ISSET(vip
, VIP_S_REFRESH
))
105 F_CLR(vip
, VIP_S_REFRESH
);
107 sp
->showmode
= SM_COMMAND
;
108 if (vs_refresh(sp
, 0))
112 /* Set the new favorite position. */
113 if (F_ISSET(vp
, VM_RCM_SET
| VM_RCM_SETFNB
| VM_RCM_SETNNB
)) {
114 F_CLR(vip
, VIP_RCM_LAST
);
115 (void)vs_column(sp
, &sp
->rcm
);
119 * If not currently in a map, log the cursor position,
120 * and set a flag so that this command can become the
123 if (MAPPED_KEYS_WAITING(sp
))
132 * There may be an ex command waiting, and we returned here
133 * only because we exited a screen or file. In this case,
134 * we simply go back into the ex parser.
136 if (EXCMD_RUNNING(wp
)) {
137 vp
->kp
= &vikeys
[':'];
141 /* Refresh the command structure. */
142 memset(vp
, 0, sizeof(VICMD
));
145 * We get a command, which may or may not have an associated
146 * motion. If it does, we get it too, calling its underlying
147 * function to get the resulting mark. We then call the
148 * command setting the cursor to the resulting mark.
151 * Vi historically flushed mapped characters on error, but
152 * entering extra <escape> characters at the beginning of
153 * a map wasn't considered an error -- in fact, users would
154 * put leading <escape> characters in maps to clean up vi
155 * state before the map was interpreted. Beauty!
157 switch (v_cmd(sp
, DOT
, vp
, NULL
, &comcount
, &mapped
)) {
171 /* Check for security setting. */
172 if (F_ISSET(vp
->kp
, V_SECURE
) && O_ISSET(sp
, O_SECURE
)) {
173 ex_emsg(sp
, (const char *)KEY_NAME(sp
, vp
->key
),
179 * Historical practice: if a dot command gets a new count,
180 * any motion component goes away, i.e. "d3w2." deletes a
183 if (F_ISSET(vp
, VC_ISDOT
) && comcount
)
184 DOTMOTION
->count
= 1;
186 /* Copy the key flags into the local structure. */
187 F_SET(vp
, vp
->kp
->flags
);
189 /* Prepare to set the previous context. */
190 if (F_ISSET(vp
, V_ABS
| V_ABS_C
| V_ABS_L
)) {
196 * Set the three cursor locations to the current cursor. The
197 * underlying routines don't bother if the cursor doesn't move.
198 * This also handles line commands (e.g. Y) defaulting to the
201 vp
->m_start
.lno
= vp
->m_stop
.lno
= vp
->m_final
.lno
= sp
->lno
;
202 vp
->m_start
.cno
= vp
->m_stop
.cno
= vp
->m_final
.cno
= sp
->cno
;
205 * Do any required motion; v_motion sets the from MARK and the
206 * line mode flag, as well as the VM_RCM flags.
208 if (F_ISSET(vp
, V_MOTION
) &&
209 v_motion(sp
, DOTMOTION
, vp
, &mapped
)) {
216 * If a count is set and the command is line oriented, set the
217 * to MARK here relative to the cursor/from MARK. This is for
218 * commands that take both counts and motions, i.e. "4yy" and
219 * "y%". As there's no way the command can know which the user
220 * did, we have to do it here. (There are commands that are
221 * line oriented and that take counts ("#G", "#H"), for which
222 * this calculation is either completely meaningless or wrong.
223 * Each command must validate the value for itself.
225 if (F_ISSET(vp
, VC_C1SET
) && F_ISSET(vp
, VM_LMODE
))
226 vp
->m_stop
.lno
+= vp
->count
- 1;
228 /* Increment the command count. */
231 #if defined(DEBUG) && defined(COMLOG)
234 /* Call the function. */
235 ex_continue
: if (vp
->kp
->func(sp
, vp
))
238 /* Make sure no function left the temporary space locked. */
239 if (F_ISSET(wp
, W_TMP_INUSE
)) {
240 F_CLR(wp
, W_TMP_INUSE
);
242 "232|vi: temporary buffer not released");
246 * If we're exiting this screen, move to the next one, or, if
247 * there aren't any more, return to the main editor loop. The
248 * ordering is careful, don't discard the contents of sp until
251 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
252 if (file_end(sp
, NULL
, F_ISSET(sp
, SC_EXIT_FORCE
)))
254 if (vs_discard(sp
, &next
))
256 if (next
== NULL
&& vs_swap(sp
, &next
, NULL
))
264 /* Switch screens, change focus. */
267 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
269 /* Don't trust the cursor. */
270 F_SET(vip
, VIP_CUR_INVALID
);
276 * Set the dot command structure.
279 * Historically, commands which used mapped keys did not
280 * set the dot command, with the exception of the text
283 if (F_ISSET(vp
, V_DOT
) && !mapped
) {
285 F_SET(DOT
, VC_ISDOT
);
288 * If a count was supplied for both the command and
289 * its motion, the count was used only for the motion.
290 * Turn the count back on for the dot structure.
292 if (F_ISSET(vp
, VC_C1RESET
))
293 F_SET(DOT
, VC_C1SET
);
295 /* VM flags aren't retained. */
296 F_CLR(DOT
, VM_COMMASK
| VM_RCM_MASK
);
300 * Some vi row movements are "attracted" to the last position
301 * set, i.e. the VM_RCM commands are moths to the VM_RCM_SET
302 * commands' candle. If the movement is to the EOL the vi
303 * command handles it. If it's to the beginning, we handle it
306 * Note, some commands (e.g. _, ^) don't set the VM_RCM_SETFNB
307 * flag, but do the work themselves. The reason is that they
308 * have to modify the column in case they're being used as a
309 * motion component. Other similar commands (e.g. +, -) don't
310 * have to modify the column because they are always line mode
311 * operations when used as motions, so the column number isn't
314 * Does this totally violate the screen and editor layering?
315 * You betcha. As they say, if you think you understand it,
318 switch (F_ISSET(vp
, VM_RCM_MASK
)) {
323 vp
->m_final
.cno
= vs_rcm(sp
,
324 vp
->m_final
.lno
, F_ISSET(vip
, VIP_RCM_LAST
));
327 F_SET(vip
, VIP_RCM_LAST
);
333 if (nonblank(sp
, vp
->m_final
.lno
, &vp
->m_final
.cno
))
340 /* Update the cursor. */
341 sp
->lno
= vp
->m_final
.lno
;
342 sp
->cno
= vp
->m_final
.cno
;
345 * Set the absolute mark -- set even if a tags or similar
346 * command, since the tag may be moving to the same file.
348 if ((F_ISSET(vp
, V_ABS
) ||
349 (F_ISSET(vp
, V_ABS_L
) && sp
->lno
!= abst
.lno
) ||
350 (F_ISSET(vp
, V_ABS_C
) &&
351 (sp
->lno
!= abst
.lno
|| sp
->cno
!= abst
.cno
))) &&
352 mark_set(sp
, ABSMARK1
, &abst
, 1))
356 err
: if (v_event_flush(sp
, CH_MAPPED
))
358 "110|Vi command failed: mapped keys discarded");
362 * Check and clear interrupts. There's an obvious race, but
363 * it's not worth fixing.
365 gc_err_noflush
: if (INTERRUPTED(sp
)) {
366 intr
: CLR_INTERRUPT(sp
);
367 if (v_event_flush(sp
, CH_MAPPED
))
369 "231|Interrupted: mapped keys discarded");
371 msgq(sp
, M_ERR
, "236|Interrupted");
374 /* If the last command switched screens, update. */
375 if (F_ISSET(sp
, SC_SSWITCH
)) {
376 F_CLR(sp
, SC_SSWITCH
);
379 * If the current screen is still displayed, it will
380 * need a new status line.
382 F_SET(sp
, SC_STATUS
);
384 /* Switch screens, change focus. */
387 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
389 /* Don't trust the cursor. */
390 F_SET(vip
, VIP_CUR_INVALID
);
392 /* Refresh so we can display messages. */
393 if (vs_refresh(sp
, 1))
397 /* If the last command switched files, change focus. */
398 if (F_ISSET(sp
, SC_FSWITCH
)) {
399 F_CLR(sp
, SC_FSWITCH
);
400 (void)sp
->gp
->scr_rename(sp
, sp
->frp
->name
, 1);
403 /* If leaving vi, return to the main editor loop. */
404 if (F_ISSET(gp
, G_SRESTART
) || F_ISSET(sp
, SC_EX
)) {
407 gp
->scr_discard(sp
, NULL
);
416 #define KEY(key, ec_flags) { \
417 if ((gcret = v_key(sp, vp, 0, ec_flags)) != GC_OK) \
419 if (vp->ev.e_value == K_ESCAPE) \
421 if (FL_ISSET(vp->ev.e_flags, CH_MAPPED)) \
427 * The O_TILDEOP option makes the ~ command take a motion instead
428 * of a straight count. This is the replacement structure we use
429 * instead of the one currently in the VIKEYS table.
432 * This should probably be deleted -- it's not all that useful, and
433 * we get help messages wrong.
435 VIKEYS
const tmotion
= {
436 v_mulcase
, V_CNT
|V_DOT
|V_MOTION
|VM_RCM_SET
,
437 "[count]~[count]motion",
438 " ~ change case to motion"
446 v_cmd(SCR
*sp
, VICMD
*dp
, VICMD
*vp
, VICMD
*ismotion
, int *comcountp
, int *mappedp
)
449 /* Previous key if getting motion component. */
452 enum { COMMANDMODE
, ISPARTIAL
, NOTPARTIAL
} cpart
;
460 * Get an event command or a key. Event commands are simple, and
461 * don't have any additional information.
463 cpart
= ismotion
== NULL
? COMMANDMODE
: ISPARTIAL
;
464 gcret
= v_key(sp
, vp
, 1, EC_MAPCOMMAND
);
465 if (gcret
!= GC_OK
) {
466 if (gcret
!= GC_EVENT
)
470 if (ismotion
!= NULL
&& !F_ISSET(vp
->kp
, V_MOVE
))
471 v_event_err(sp
, &vp
->ev
);
476 * Keys are not simple. (Although vi's command structure less complex
477 * than ex (and don't think I'm not grateful!) The command syntax is:
479 * [count] [buffer] [count] key [[motion] | [buffer] [character]]
481 * and there are, of course, several special cases. The motion value
482 * is itself a vi command, with the syntax:
484 * [count] key [character]
486 * <escape> cancels partial commands, i.e. a command where at least
487 * one non-numeric character has been entered. Otherwise, it beeps
491 * POSIX 1003.2-1992 explicitly disallows cancelling commands where
492 * all that's been entered is a number, requiring that the terminal
495 if (vp
->ev
.e_value
== K_ESCAPE
)
499 * Commands that are mapped are treated differently (e.g., they
500 * don't set the dot command. Pass that information back.
502 if (FL_ISSET(vp
->ev
.e_flags
, CH_MAPPED
))
506 if (ismotion
== NULL
)
509 /* Pick up an optional buffer. */
512 if (ismotion
!= NULL
) {
513 v_emsg(sp
, NULL
, VIM_COMBUF
);
517 F_SET(vp
, VC_BUFFER
);
519 KEY(key
, EC_MAPCOMMAND
);
523 * Pick up an optional count, where a leading 0 isn't a count, it's
524 * a command. When a count is specified, the dot command behaves
525 * differently, pass the information back.
527 if (ISDIGIT(key
) && key
!= '0') {
528 if (v_count(sp
, vp
, key
, &vp
->count
))
534 KEY(key
, EC_MAPCOMMAND
);
538 /* Pick up optional buffer. */
541 if (F_ISSET(vp
, VC_BUFFER
)) {
542 msgq(sp
, M_ERR
, "234|Only one buffer may be specified");
545 if (ismotion
!= NULL
) {
546 v_emsg(sp
, NULL
, VIM_COMBUF
);
550 F_SET(vp
, VC_BUFFER
);
552 KEY(key
, EC_MAPCOMMAND
);
555 /* Check for an OOB command key. */
557 if (key
> MAXVIKEY
) {
558 v_emsg(sp
, (const char *)KEY_NAME(sp
, key
), VIM_NOCOM
);
561 kp
= &vikeys
[vp
->key
= key
];
565 * Historically, D accepted and then ignored a count. Match it.
567 if (vp
->key
== 'D' && F_ISSET(vp
, VC_C1SET
)) {
574 * There are several commands that we implement as aliases, both
575 * to match historic practice and to ensure consistency. Check
576 * for command aliases.
578 if (kp
->func
== NULL
&& (kp
= v_alias(sp
, vp
, kp
)) == NULL
)
581 /* The tildeop option makes the ~ command take a motion. */
582 if (key
== '~' && O_ISSET(sp
, O_TILDEOP
))
588 * Find the command. The only legal command with no underlying
589 * function is dot. It's historic practice that <escape> doesn't
590 * just erase the preceding number, it beeps the terminal as well.
591 * It's a common problem, so just beep the terminal unless verbose
594 if (kp
->func
== NULL
) {
596 v_emsg(sp
, (const char *)KEY_NAME(sp
, key
),
597 vp
->ev
.e_value
== K_ESCAPE
?
598 VIM_NOCOM_B
: VIM_NOCOM
);
602 /* If called for a motion command, stop now. */
608 * If a '.' is immediately entered after an undo command, we
609 * replay the log instead of redoing the last command. This
610 * is necessary because 'u' can't set the dot command -- see
611 * vi/v_undo.c:v_undo for details.
613 if (VIP(sp
)->u_ccnt
== sp
->ccnt
) {
614 vp
->kp
= &vikeys
['u'];
619 /* Otherwise, a repeatable command must have been executed. */
620 if (!F_ISSET(dp
, VC_ISDOT
)) {
621 msgq(sp
, M_ERR
, "208|No command to repeat");
625 /* Set new count/buffer, if any, and return. */
626 if (F_ISSET(vp
, VC_C1SET
)) {
628 dp
->count
= vp
->count
;
630 if (F_ISSET(vp
, VC_BUFFER
))
631 dp
->buffer
= vp
->buffer
;
637 /* Set the flags based on the command flags. */
640 /* Check for illegal count. */
641 if (F_ISSET(vp
, VC_C1SET
) && !LF_ISSET(V_CNT
))
644 /* Illegal motion command. */
645 if (ismotion
== NULL
) {
646 /* Illegal buffer. */
647 if (!LF_ISSET(V_OBUF
) && F_ISSET(vp
, VC_BUFFER
))
650 /* Required buffer. */
651 if (LF_ISSET(V_RBUF
)) {
653 F_SET(vp
, VC_BUFFER
);
658 * Special case: '[', ']' and 'Z' commands. Doesn't the fact that
659 * the *single* characters don't mean anything but the *doubled*
660 * characters do, just frost your shorts?
662 if (vp
->key
== '[' || vp
->key
== ']' || vp
->key
== 'Z') {
664 * Historically, half entered [[, ]] or Z commands weren't
665 * cancelled by <escape>, the terminal was beeped instead.
666 * POSIX.2-1992 probably didn't notice, and requires that
667 * they be cancelled instead of beeping. Seems fine to me.
669 * Don't set the EC_MAPCOMMAND flag, apparently ] is a popular
670 * vi meta-character, and we don't want the user to wait while
671 * we time out a possible mapping. This *appears* to match
672 * historic vi practice, but with mapping characters, You Just
677 if (vp
->key
!= key
) {
678 usage
: if (ismotion
== NULL
)
680 else if (ismotion
->key
== '~' && O_ISSET(sp
, O_TILDEOP
))
683 s
= vikeys
[ismotion
->key
].usage
;
684 v_emsg(sp
, s
, VIM_USAGE
);
688 /* Special case: 'z' command. */
689 if (vp
->key
== 'z') {
690 KEY(vp
->character
, 0);
691 if (ISDIGIT(vp
->character
)) {
692 if (v_count(sp
, vp
, vp
->character
, &vp
->count2
))
695 KEY(vp
->character
, 0);
700 * Commands that have motion components can be doubled to imply the
703 if (ismotion
!= NULL
&& ismotion
->key
!= key
&& !LF_ISSET(V_MOVE
)) {
704 msgq(sp
, M_ERR
, "210|%s may not be used as a motion command",
709 /* Pick up required trailing character. */
710 if (LF_ISSET(V_CHAR
))
711 KEY(vp
->character
, 0);
713 /* Get any associated cursor word. */
714 if (F_ISSET(kp
, V_KEYW
) && v_curword(sp
))
719 esc
: switch (cpart
) {
721 msgq(sp
, M_BERR
, "211|Already in command mode");
722 return (GC_ERR_NOFLUSH
);
726 (void)sp
->gp
->scr_bell(sp
);
735 * Get resulting motion mark.
738 v_motion(SCR
*sp
, VICMD
*dm
, VICMD
*vp
, int *mappedp
)
745 int tilde_reset
, notused
;
748 * If '.' command, use the dot motion, else get the motion command.
749 * Clear any line motion flags, the subsequent motion isn't always
750 * the same, i.e. "/aaa" may or may not be a line motion.
752 if (F_ISSET(vp
, VC_ISDOT
)) {
754 F_SET(&motion
, VC_ISDOT
);
755 F_CLR(&motion
, VM_COMMASK
);
758 memset(&motion
, 0, sizeof(VICMD
));
759 gcret
= v_cmd(sp
, NULL
, &motion
, vp
, ¬used
, mappedp
);
760 if (gcret
!= GC_OK
&& gcret
!= GC_EVENT
)
765 * A count may be provided both to the command and to the motion, in
766 * which case the count is multiplicative. For example, "3y4y" is the
767 * same as "12yy". This count is provided to the motion command and
768 * not to the regular function.
770 cnt
= motion
.count
= F_ISSET(&motion
, VC_C1SET
) ? motion
.count
: 1;
771 if (F_ISSET(vp
, VC_C1SET
)) {
772 motion
.count
*= vp
->count
;
773 F_SET(&motion
, VC_C1SET
);
776 * Set flags to restore the original values of the command
777 * structure so dot commands can change the count values,
778 * e.g. "2dw" "3." deletes a total of five words.
781 F_SET(vp
, VC_C1RESET
);
785 * Some commands can be repeated to indicate the current line. In
786 * this case, or if the command is a "line command", set the flags
787 * appropriately. If not a doubled command, run the function to get
788 * the resulting mark.
790 if (gcret
!= GC_EVENT
&& vp
->key
== motion
.key
) {
791 F_SET(vp
, VM_LDOUBLE
| VM_LMODE
);
793 /* Set the origin of the command. */
794 vp
->m_start
.lno
= sp
->lno
;
798 * Set the end of the command.
800 * If the current line is missing, i.e. the file is empty,
801 * historic vi permitted a "cc" or "!!" command to insert
804 vp
->m_stop
.lno
= sp
->lno
+ motion
.count
- 1;
805 if (db_get(sp
, vp
->m_stop
.lno
, 0, NULL
, &len
)) {
806 if (vp
->m_stop
.lno
!= 1 ||
807 (vp
->key
!= 'c' && vp
->key
!= '!')) {
808 v_emsg(sp
, NULL
, VIM_EMPTY
);
813 vp
->m_stop
.cno
= len
? len
- 1 : 0;
816 * Motion commands change the underlying movement (*snarl*).
817 * For example, "l" is illegal at the end of a line, but "dl"
818 * is not. Set flags so the function knows the situation.
824 * Use yank instead of creating a new motion command, it's a
825 * lot easier for now.
827 if (vp
->kp
== &tmotion
) {
829 vp
->kp
= &vikeys
['y'];
834 * Copy the key flags into the local structure, except for the
835 * RCM flags -- the motion command will set the RCM flags in
836 * the vp structure if necessary. This means that the motion
837 * command is expected to determine where the cursor ends up!
838 * However, we save off the current RCM mask and restore it if
839 * it no RCM flags are set by the motion command, with a small
842 * We replace the VM_RCM_SET flag with the VM_RCM flag. This
843 * is so that cursor movement doesn't set the relative position
844 * unless the motion command explicitly specified it. This
845 * appears to match historic practice, but I've never been able
846 * to develop a hard-and-fast rule.
848 flags
= F_ISSET(vp
, VM_RCM_MASK
);
849 if (LF_ISSET(VM_RCM_SET
)) {
853 F_CLR(vp
, VM_RCM_MASK
);
854 F_SET(&motion
, motion
.kp
->flags
& ~VM_RCM_MASK
);
857 * Set the three cursor locations to the current cursor. This
858 * permits commands like 'j' and 'k', that are line oriented
859 * motions and have special cursor suck semantics when they are
860 * used as standalone commands, to ignore column positioning.
863 motion
.m_stop
.lno
= motion
.m_start
.lno
= sp
->lno
;
865 motion
.m_stop
.cno
= motion
.m_start
.cno
= sp
->cno
;
867 /* Run the function. */
868 if ((motion
.kp
->func
)(sp
, &motion
))
872 * If the current line is missing, i.e. the file is empty,
873 * historic vi allowed "c<motion>" or "!<motion>" to insert
874 * text. Otherwise fail -- most motion commands will have
875 * already failed, but some, e.g. G, succeed in empty files.
877 if (!db_exist(sp
, vp
->m_stop
.lno
)) {
878 if (vp
->m_stop
.lno
!= 1 ||
879 (vp
->key
!= 'c' && vp
->key
!= '!')) {
880 v_emsg(sp
, NULL
, VIM_EMPTY
);
894 * Copy cut buffer, line mode and cursor position information
895 * from the motion command structure, i.e. anything that the
896 * motion command can set for us. The commands can flag the
897 * movement as a line motion (see v_sentence) as well as set
898 * the VM_RCM_* flags explicitly.
900 F_SET(vp
, F_ISSET(&motion
, VM_COMMASK
| VM_RCM_MASK
));
903 * If the motion command set no relative motion flags, use
904 * the (slightly) modified previous values.
906 if (!F_ISSET(vp
, VM_RCM_MASK
))
910 * Commands can change behaviors based on the motion command
911 * used, for example, the ! command repeated the last bang
912 * command if N or n was used as the motion.
917 * Motion commands can reset all of the cursor information.
918 * If the motion is in the reverse direction, switch the
919 * from and to MARK's so that it's in a forward direction.
920 * Motions are from the from MARK to the to MARK (inclusive).
922 if (motion
.m_start
.lno
> motion
.m_stop
.lno
||
923 (motion
.m_start
.lno
== motion
.m_stop
.lno
&&
924 motion
.m_start
.cno
> motion
.m_stop
.cno
)) {
925 vp
->m_start
= motion
.m_stop
;
926 vp
->m_stop
= motion
.m_start
;
928 vp
->m_start
= motion
.m_start
;
929 vp
->m_stop
= motion
.m_stop
;
931 vp
->m_final
= motion
.m_final
;
935 * If the command sets dot, save the motion structure. The motion
936 * count was changed above and needs to be reset, that's why this
937 * is done here, and not in the calling routine.
939 if (F_ISSET(vp
->kp
, V_DOT
)) {
948 * Initialize the vi screen.
959 /* Switch into vi. */
960 if (gp
->scr_screen(sp
, SC_VI
))
962 (void)gp
->scr_attr(sp
, SA_ALTERNATE
, 1);
964 F_CLR(sp
, SC_EX
| SC_SCR_EX
);
968 * Initialize screen values.
970 * Small windows: see vs_refresh(), section 6a.
973 * t_minrows is the minimum rows to display
974 * t_maxrows is the maximum rows to display (rows - 1)
975 * t_rows is the rows currently being displayed
977 sp
->rows
= vip
->srows
= O_VAL(sp
, O_LINES
);
978 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
979 sp
->t_rows
= sp
->t_minrows
= O_VAL(sp
, O_WINDOW
);
981 if (sp
->t_rows
> sp
->rows
- 1) {
982 sp
->t_minrows
= sp
->t_rows
= sp
->rows
- 1;
984 "214|Windows option value is too large, max is %zu",
987 sp
->t_maxrows
= sp
->rows
- 1;
990 sp
->roff
= sp
->coff
= 0;
992 /* Create a screen map. */
993 CALLOC_RET(sp
, HMAP
, SMAP
*, SIZE_HMAP(sp
), sizeof(SMAP
));
994 TMAP
= HMAP
+ (sp
->t_rows
- 1);
1000 * Fill the screen map from scratch -- try and center the line. That
1001 * way if we're starting with a file we've seen before, we'll put the
1002 * line in the middle, otherwise, it won't work and we'll end up with
1003 * the line at the top.
1005 F_CLR(sp
, SC_SCR_TOP
);
1006 F_SET(sp
, SC_SCR_REFORMAT
| SC_SCR_CENTER
);
1008 /* Invalidate the cursor. */
1009 F_SET(vip
, VIP_CUR_INVALID
);
1011 /* Paint the screen image from scratch. */
1012 F_SET(vip
, VIP_N_EX_PAINT
);
1019 * Move all but the current screen to the hidden queue.
1029 /* Move all screens to the hidden queue, tossing screen maps. */
1030 for (hidden
= 0, gp
= sp
->gp
, wp
= sp
->wp
;
1031 (tsp
= wp
->scrq
.cqh_first
) != (void *)&wp
->scrq
; ++hidden
) {
1032 if (_HMAP(tsp
) != NULL
) {
1036 CIRCLEQ_REMOVE(&wp
->scrq
, tsp
, q
);
1037 CIRCLEQ_INSERT_TAIL(&gp
->hq
, tsp
, q
);
1038 /* XXXX Change if hidden screens per window */
1040 gp
->scr_discard(tsp
, NULL
);
1043 /* Move current screen back to the display queue. */
1044 CIRCLEQ_REMOVE(&gp
->hq
, sp
, q
);
1045 CIRCLEQ_INSERT_TAIL(&wp
->scrq
, sp
, q
);
1050 "319|%d screens backgrounded; use :display to list them",
1056 * Get the word (or non-word) the cursor is on.
1058 * PUBLIC: int v_curword __P((SCR *));
1064 size_t beg
, end
, len
;
1068 if (db_get(sp
, sp
->lno
, DBG_FATAL
, &p
, &len
))
1073 * Historically, tag commands skipped over any leading whitespace
1074 * characters. Make this true in general when using cursor words.
1075 * If movement, getting a cursor word implies moving the cursor to
1076 * its beginning. Refresh now.
1079 * Find the beginning/end of the keyword. Keywords are currently
1080 * used for cursor-word searching and for tags. Historical vi
1081 * only used the word in a tag search from the cursor to the end
1082 * of the word, i.e. if the cursor was on the 'b' in " abc ", the
1083 * tag was "bc". For consistency, we make cursor word searches
1084 * follow the same rule.
1087 beg
= sp
->cno
; beg
< len
&& isspace(p
[beg
]); moved
= 1, ++beg
);
1089 msgq(sp
, M_BERR
, "212|Cursor not in a word");
1094 (void)vs_refresh(sp
, 0);
1097 /* Find the end of the word. */
1098 for (state
= inword(p
[beg
]),
1099 end
= beg
; ++end
< len
&& state
== inword(p
[end
]););
1103 BINC_RETW(sp
, vip
->keyw
, vip
->klen
, len
+1);
1104 MEMMOVEW(vip
->keyw
, p
+ beg
, len
);
1105 vip
->keyw
[len
] = '\0'; /* XXX */
1111 * Check for a command alias.
1113 static VIKEYS
const *
1114 v_alias(SCR
*sp
, VICMD
*vp
, const VIKEYS
*kp
)
1119 case 'C': /* C -> c$ */
1123 case 'D': /* D -> d$ */
1127 case 'S': /* S -> c_ */
1131 case 'Y': /* Y -> y_ */
1138 return (v_event_push(sp
,
1139 NULL
, &push
, 1, CH_NOMAP
| CH_QUOTED
) ? NULL
: &vikeys
[vp
->key
]);
1144 * Return the next count.
1147 v_count(SCR
*sp
, VICMD
*vp
, ARG_CHAR_T fkey
, u_long
*countp
)
1156 * Assume that overflow results in a smaller number.
1158 tc
= count
* 10 + vp
->ev
.e_c
- '0';
1160 /* Toss to the next non-digit. */
1162 if (v_key(sp
, vp
, 0,
1163 EC_MAPCOMMAND
| EC_MAPNODIGIT
) != GC_OK
)
1165 } while (ISDIGIT(vp
->ev
.e_c
));
1167 "235|Number larger than %lu", ULONG_MAX
);
1171 if (v_key(sp
, vp
, 0, EC_MAPCOMMAND
| EC_MAPNODIGIT
) != GC_OK
)
1173 } while (ISDIGIT(vp
->ev
.e_c
));
1180 * Return the next event.
1183 v_key(SCR
*sp
, VICMD
*vp
, int events_ok
, u_int32_t ec_flags
)
1188 for (evp
= &vp
->ev
, quote
= 0;;) {
1189 if (v_event_get(sp
, evp
, 0, ec_flags
| quote
))
1193 switch (evp
->e_event
) {
1197 * Historically, ^V was ignored in the command stream,
1198 * although it had a useful side-effect of interrupting
1199 * mappings. Adding a quoting bit to the call probably
1200 * extends historic practice, but it feels right.
1202 if (evp
->e_value
== K_VLNEXT
) {
1213 * Historically, vi beeped on command level interrupts.
1215 * Historically, vi exited to ex mode if no file was
1216 * named on the command line, and two interrupts were
1217 * generated in a row. (I figured you might want to
1218 * know that, just in case there's a quiz later.)
1220 (void)sp
->gp
->scr_bell(sp
);
1221 return (GC_INTERRUPT
);
1223 if (v_erepaint(sp
, evp
))
1229 * We don't do anything here, just return an error.
1230 * The vi loop will return because of this, and then
1231 * the main loop will realize that we had to restart
1232 * the world and will call the vi loop again.
1240 v_event_err(sp
, evp
);
1247 #if defined(DEBUG) && defined(COMLOG)
1250 * Log the contents of the command structure.
1257 vtrace(sp
, "vcmd: %c", vp
->key
);
1258 if (F_ISSET(vp
, VC_BUFFER
))
1259 vtrace(sp
, " buffer: %c", vp
->buffer
);
1260 if (F_ISSET(vp
, VC_C1SET
))
1261 vtrace(sp
, " c1: %lu", vp
->count
);
1262 if (F_ISSET(vp
, VC_C2SET
))
1263 vtrace(sp
, " c2: %lu", vp
->count2
);
1264 vtrace(sp
, " flags: 0x%x\n", vp
->flags
);