add vi_send(), vi_translate() to list of exported functions
[nvi.git] / vi / vi.c
blobb10ee67adc0e1f9f7ba672518d7e92d8d6a32625
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: vi.c,v 10.65 1996/12/18 10:24:36 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:24:36 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "vi.h"
32 typedef enum {
33 GC_ERR, GC_ERR_NOFLUSH, GC_EVENT, GC_FATAL, GC_INTERRUPT, GC_OK
34 } gcret_t;
36 static VIKEYS const
37 *v_alias __P((SCR *, VICMD *, VIKEYS const *));
38 static gcret_t v_cmd __P((SCR *, VICMD *, VICMD *, VICMD *, int *, int *));
39 static int v_count __P((SCR *, VICMD *, ARG_CHAR_T, u_long *));
40 static void v_dtoh __P((SCR *));
41 static int v_init __P((SCR *));
42 static gcret_t v_key __P((SCR *, VICMD *, int, u_int32_t));
43 static int v_motion __P((SCR *, VICMD *, VICMD *, int *));
45 #if defined(DEBUG) && defined(COMLOG)
46 static void v_comlog __P((SCR *, VICMD *));
47 #endif
50 * Side-effect:
51 * The dot structure can be set by the underlying vi functions,
52 * see v_Put() and v_put().
54 #define DOT (&VIP(sp)->sdot)
55 #define DOTMOTION (&VIP(sp)->sdotmotion)
58 * vi --
59 * Main vi command loop.
61 * PUBLIC: int vi __P((SCR **));
63 int
64 vi(spp)
65 SCR **spp;
67 GS *gp;
68 MARK abs;
69 SCR *next, *sp;
70 VICMD cmd, *vp;
71 VI_PRIVATE *vip;
72 int comcount, mapped, rval;
74 /* Get the first screen. */
75 sp = *spp;
76 gp = sp->gp;
78 /* Initialize the command structure. */
79 vp = &cmd;
80 memset(vp, 0, sizeof(VICMD));
82 /* Reset strange attraction. */
83 F_SET(vp, VM_RCM_SET);
85 /* Initialize the vi screen. */
86 if (v_init(sp))
87 return (1);
89 /* Set the focus. */
90 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
92 for (vip = VIP(sp), rval = 0;;) {
93 /* Resolve messages. */
94 if (!MAPPED_KEYS_WAITING(sp) && vs_resolve(sp, NULL, 0))
95 goto ret;
98 * If not skipping a refresh, return to command mode and
99 * refresh the screen.
101 if (F_ISSET(vip, VIP_S_REFRESH))
102 F_CLR(vip, VIP_S_REFRESH);
103 else {
104 sp->showmode = SM_COMMAND;
105 if (vs_refresh(sp, 0))
106 goto ret;
109 /* Set the new favorite position. */
110 if (F_ISSET(vp, VM_RCM_SET | VM_RCM_SETFNB | VM_RCM_SETNNB)) {
111 F_CLR(vip, VIP_RCM_LAST);
112 (void)vs_column(sp, &sp->rcm);
116 * If not currently in a map, log the cursor position,
117 * and set a flag so that this command can become the
118 * DOT command.
120 if (MAPPED_KEYS_WAITING(sp))
121 mapped = 1;
122 else {
123 if (log_cursor(sp))
124 goto err;
125 mapped = 0;
129 * There may be an ex command waiting, and we returned here
130 * only because we exited a screen or file. In this case,
131 * we simply go back into the ex parser.
133 if (EXCMD_RUNNING(gp)) {
134 vp->kp = &vikeys[':'];
135 goto ex_continue;
138 /* Refresh the command structure. */
139 memset(vp, 0, sizeof(VICMD));
142 * We get a command, which may or may not have an associated
143 * motion. If it does, we get it too, calling its underlying
144 * function to get the resulting mark. We then call the
145 * command setting the cursor to the resulting mark.
147 * !!!
148 * Vi historically flushed mapped characters on error, but
149 * entering extra <escape> characters at the beginning of
150 * a map wasn't considered an error -- in fact, users would
151 * put leading <escape> characters in maps to clean up vi
152 * state before the map was interpreted. Beauty!
154 switch (v_cmd(sp, DOT, vp, NULL, &comcount, &mapped)) {
155 case GC_ERR:
156 goto err;
157 case GC_ERR_NOFLUSH:
158 goto gc_err_noflush;
159 case GC_FATAL:
160 goto ret;
161 case GC_INTERRUPT:
162 goto intr;
163 case GC_EVENT:
164 case GC_OK:
165 break;
168 /* Check for security setting. */
169 if (F_ISSET(vp->kp, V_SECURE) && O_ISSET(sp, O_SECURE)) {
170 ex_emsg(sp, KEY_NAME(sp, vp->key), EXM_SECURE);
171 goto err;
175 * Historical practice: if a dot command gets a new count,
176 * any motion component goes away, i.e. "d3w2." deletes a
177 * total of 5 words.
179 if (F_ISSET(vp, VC_ISDOT) && comcount)
180 DOTMOTION->count = 1;
182 /* Copy the key flags into the local structure. */
183 F_SET(vp, vp->kp->flags);
185 /* Prepare to set the previous context. */
186 if (F_ISSET(vp, V_ABS | V_ABS_C | V_ABS_L)) {
187 abs.lno = sp->lno;
188 abs.cno = sp->cno;
192 * Set the three cursor locations to the current cursor. The
193 * underlying routines don't bother if the cursor doesn't move.
194 * This also handles line commands (e.g. Y) defaulting to the
195 * current line.
197 vp->m_start.lno = vp->m_stop.lno = vp->m_final.lno = sp->lno;
198 vp->m_start.cno = vp->m_stop.cno = vp->m_final.cno = sp->cno;
201 * Do any required motion; v_motion sets the from MARK and the
202 * line mode flag, as well as the VM_RCM flags.
204 if (F_ISSET(vp, V_MOTION) &&
205 v_motion(sp, DOTMOTION, vp, &mapped)) {
206 if (INTERRUPTED(sp))
207 goto intr;
208 goto err;
212 * If a count is set and the command is line oriented, set the
213 * to MARK here relative to the cursor/from MARK. This is for
214 * commands that take both counts and motions, i.e. "4yy" and
215 * "y%". As there's no way the command can know which the user
216 * did, we have to do it here. (There are commands that are
217 * line oriented and that take counts ("#G", "#H"), for which
218 * this calculation is either completely meaningless or wrong.
219 * Each command must validate the value for itself.
221 if (F_ISSET(vp, VC_C1SET) && F_ISSET(vp, VM_LMODE))
222 vp->m_stop.lno += vp->count - 1;
224 /* Increment the command count. */
225 ++sp->ccnt;
227 #if defined(DEBUG) && defined(COMLOG)
228 v_comlog(sp, vp);
229 #endif
230 /* Call the function. */
231 ex_continue: if (vp->kp->func(sp, vp))
232 goto err;
233 #ifdef DEBUG
234 /* Make sure no function left the temporary space locked. */
235 if (F_ISSET(gp, G_TMP_INUSE)) {
236 F_CLR(gp, G_TMP_INUSE);
237 msgq(sp, M_ERR,
238 "232|vi: temporary buffer not released");
240 #endif
242 * If we're exiting this screen, move to the next one, or, if
243 * there aren't any more, return to the main editor loop. The
244 * ordering is careful, don't discard the contents of sp until
245 * the end.
247 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
248 if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
249 goto ret;
250 if (vs_discard(sp, &next))
251 goto ret;
252 if (next == NULL && vs_swap(sp, &next, NULL))
253 goto ret;
254 *spp = next;
255 if (screen_end(sp))
256 goto ret;
257 if (next == NULL)
258 break;
260 /* Switch screens, change focus. */
261 sp = next;
262 vip = VIP(sp);
263 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
265 /* Don't trust the cursor. */
266 F_SET(vip, VIP_CUR_INVALID);
268 continue;
272 * Set the dot command structure.
274 * !!!
275 * Historically, commands which used mapped keys did not
276 * set the dot command, with the exception of the text
277 * input commands.
279 if (F_ISSET(vp, V_DOT) && !mapped) {
280 *DOT = cmd;
281 F_SET(DOT, VC_ISDOT);
284 * If a count was supplied for both the command and
285 * its motion, the count was used only for the motion.
286 * Turn the count back on for the dot structure.
288 if (F_ISSET(vp, VC_C1RESET))
289 F_SET(DOT, VC_C1SET);
291 /* VM flags aren't retained. */
292 F_CLR(DOT, VM_COMMASK | VM_RCM_MASK);
296 * Some vi row movements are "attracted" to the last position
297 * set, i.e. the VM_RCM commands are moths to the VM_RCM_SET
298 * commands' candle. If the movement is to the EOL the vi
299 * command handles it. If it's to the beginning, we handle it
300 * here.
302 * Note, some commands (e.g. _, ^) don't set the VM_RCM_SETFNB
303 * flag, but do the work themselves. The reason is that they
304 * have to modify the column in case they're being used as a
305 * motion component. Other similar commands (e.g. +, -) don't
306 * have to modify the column because they are always line mode
307 * operations when used as motions, so the column number isn't
308 * of any interest.
310 * Does this totally violate the screen and editor layering?
311 * You betcha. As they say, if you think you understand it,
312 * you don't.
314 switch (F_ISSET(vp, VM_RCM_MASK)) {
315 case 0:
316 case VM_RCM_SET:
317 break;
318 case VM_RCM:
319 vp->m_final.cno = vs_rcm(sp,
320 vp->m_final.lno, F_ISSET(vip, VIP_RCM_LAST));
321 break;
322 case VM_RCM_SETLAST:
323 F_SET(vip, VIP_RCM_LAST);
324 break;
325 case VM_RCM_SETFNB:
326 vp->m_final.cno = 0;
327 /* FALLTHROUGH */
328 case VM_RCM_SETNNB:
329 if (nonblank(sp, vp->m_final.lno, &vp->m_final.cno))
330 goto err;
331 break;
332 default:
333 abort();
336 /* Update the cursor. */
337 sp->lno = vp->m_final.lno;
338 sp->cno = vp->m_final.cno;
341 * Set the absolute mark -- set even if a tags or similar
342 * command, since the tag may be moving to the same file.
344 if ((F_ISSET(vp, V_ABS) ||
345 F_ISSET(vp, V_ABS_L) && sp->lno != abs.lno ||
346 F_ISSET(vp, V_ABS_C) &&
347 (sp->lno != abs.lno || sp->cno != abs.cno)) &&
348 mark_set(sp, ABSMARK1, &abs, 1))
349 goto err;
351 if (0) {
352 err: if (v_event_flush(sp, CH_MAPPED))
353 msgq(sp, M_BERR,
354 "110|Vi command failed: mapped keys discarded");
358 * Check and clear interrupts. There's an obvious race, but
359 * it's not worth fixing.
361 gc_err_noflush: if (INTERRUPTED(sp)) {
362 intr: CLR_INTERRUPT(sp);
363 if (v_event_flush(sp, CH_MAPPED))
364 msgq(sp, M_ERR,
365 "231|Interrupted: mapped keys discarded");
366 else
367 msgq(sp, M_ERR, "236|Interrupted");
370 /* If the last command switched screens, update. */
371 if (F_ISSET(sp, SC_SSWITCH)) {
372 F_CLR(sp, SC_SSWITCH);
375 * If the current screen is still displayed, it will
376 * need a new status line.
378 F_SET(sp, SC_STATUS);
380 /* Switch screens, change focus. */
381 sp = sp->nextdisp;
382 vip = VIP(sp);
383 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
385 /* Don't trust the cursor. */
386 F_SET(vip, VIP_CUR_INVALID);
388 /* Refresh so we can display messages. */
389 if (vs_refresh(sp, 1))
390 return (1);
393 /* If the last command switched files, change focus. */
394 if (F_ISSET(sp, SC_FSWITCH)) {
395 F_CLR(sp, SC_FSWITCH);
396 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
399 /* If leaving vi, return to the main editor loop. */
400 if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_EX)) {
401 *spp = sp;
402 v_dtoh(sp);
403 break;
406 if (0)
407 ret: rval = 1;
408 return (rval);
411 #define KEY(key, ec_flags) { \
412 if ((gcret = v_key(sp, vp, 0, ec_flags)) != GC_OK) \
413 return (gcret); \
414 if (vp->ev.e_value == K_ESCAPE) \
415 goto esc; \
416 if (FL_ISSET(vp->ev.e_flags, CH_MAPPED)) \
417 *mappedp = 1; \
418 key = vp->ev.e_c; \
422 * The O_TILDEOP option makes the ~ command take a motion instead
423 * of a straight count. This is the replacement structure we use
424 * instead of the one currently in the VIKEYS table.
426 * XXX
427 * This should probably be deleted -- it's not all that useful, and
428 * we get help messages wrong.
430 VIKEYS const tmotion = {
431 v_mulcase, V_CNT|V_DOT|V_MOTION|VM_RCM_SET,
432 "[count]~[count]motion",
433 " ~ change case to motion"
437 * v_cmd --
438 * Get a vi command.
440 static gcret_t
441 v_cmd(sp, dp, vp, ismotion, comcountp, mappedp)
442 SCR *sp;
443 VICMD *dp, *vp;
444 VICMD *ismotion; /* Previous key if getting motion component. */
445 int *comcountp, *mappedp;
447 enum { COMMANDMODE, ISPARTIAL, NOTPARTIAL } cpart;
448 CHAR_T key;
449 VIKEYS const *kp;
450 gcret_t gcret;
451 u_int flags;
452 char *s;
455 * Get an event command or a key. Event commands are simple, and
456 * don't have any additional information.
458 cpart = ismotion == NULL ? COMMANDMODE : ISPARTIAL;
459 gcret = v_key(sp, vp, 1, EC_MAPCOMMAND);
460 if (gcret != GC_OK) {
461 if (gcret != GC_EVENT)
462 return (gcret);
463 if (v_event(sp, vp))
464 return (GC_ERR);
465 if (ismotion != NULL && !F_ISSET(vp->kp, V_MOVE))
466 v_event_err(sp, &vp->ev);
467 return (GC_EVENT);
471 * Keys are not simple. (Although vi's command structure less complex
472 * than ex (and don't think I'm not grateful!) The command syntax is:
474 * [count] [buffer] [count] key [[motion] | [buffer] [character]]
476 * and there are, of course, several special cases. The motion value
477 * is itself a vi command, with the syntax:
479 * [count] key [character]
481 * <escape> cancels partial commands, i.e. a command where at least
482 * one non-numeric character has been entered. Otherwise, it beeps
483 * the terminal.
485 * !!!
486 * POSIX 1003.2-1992 explicitly disallows cancelling commands where
487 * all that's been entered is a number, requiring that the terminal
488 * be alerted.
490 if (vp->ev.e_value == K_ESCAPE)
491 goto esc;
494 * Commands that are mapped are treated differently (e.g., they
495 * don't set the dot command. Pass that information back.
497 if (FL_ISSET(vp->ev.e_flags, CH_MAPPED))
498 *mappedp = 1;
499 key = vp->ev.e_c;
501 if (ismotion == NULL)
502 cpart = NOTPARTIAL;
504 /* Pick up an optional buffer. */
505 if (key == '"') {
506 cpart = ISPARTIAL;
507 if (ismotion != NULL) {
508 v_emsg(sp, NULL, VIM_COMBUF);
509 return (GC_ERR);
511 KEY(vp->buffer, 0);
512 F_SET(vp, VC_BUFFER);
514 KEY(key, EC_MAPCOMMAND);
518 * Pick up an optional count, where a leading 0 isn't a count, it's
519 * a command. When a count is specified, the dot command behaves
520 * differently, pass the information back.
522 if (isdigit(key) && key != '0') {
523 if (v_count(sp, vp, key, &vp->count))
524 return (GC_ERR);
526 F_SET(vp, VC_C1SET);
527 *comcountp = 1;
529 KEY(key, EC_MAPCOMMAND);
530 } else
531 *comcountp = 0;
533 /* Pick up optional buffer. */
534 if (key == '"') {
535 cpart = ISPARTIAL;
536 if (F_ISSET(vp, VC_BUFFER)) {
537 msgq(sp, M_ERR, "234|Only one buffer may be specified");
538 return (GC_ERR);
540 if (ismotion != NULL) {
541 v_emsg(sp, NULL, VIM_COMBUF);
542 return (GC_ERR);
544 KEY(vp->buffer, 0);
545 F_SET(vp, VC_BUFFER);
547 KEY(key, EC_MAPCOMMAND);
550 /* Check for an OOB command key. */
551 cpart = ISPARTIAL;
552 if (key > MAXVIKEY) {
553 v_emsg(sp, KEY_NAME(sp, key), VIM_NOCOM);
554 return (GC_ERR);
556 kp = &vikeys[vp->key = key];
559 * !!!
560 * Historically, D accepted and then ignored a count. Match it.
562 if (vp->key == 'D' && F_ISSET(vp, VC_C1SET)) {
563 *comcountp = 0;
564 vp->count = 0;
565 F_CLR(vp, VC_C1SET);
569 * There are several commands that we implement as aliases, both
570 * to match historic practice and to ensure consistency. Check
571 * for command aliases.
573 if (kp->func == NULL && (kp = v_alias(sp, vp, kp)) == NULL)
574 return (GC_ERR);
576 /* The tildeop option makes the ~ command take a motion. */
577 if (key == '~' && O_ISSET(sp, O_TILDEOP))
578 kp = &tmotion;
580 vp->kp = kp;
583 * Find the command. The only legal command with no underlying
584 * function is dot. It's historic practice that <escape> doesn't
585 * just erase the preceding number, it beeps the terminal as well.
586 * It's a common problem, so just beep the terminal unless verbose
587 * was set.
589 if (kp->func == NULL) {
590 if (key != '.') {
591 v_emsg(sp, KEY_NAME(sp, key),
592 vp->ev.e_value == K_ESCAPE ?
593 VIM_NOCOM_B : VIM_NOCOM);
594 return (GC_ERR);
597 /* If called for a motion command, stop now. */
598 if (dp == NULL)
599 goto usage;
602 * !!!
603 * If a '.' is immediately entered after an undo command, we
604 * replay the log instead of redoing the last command. This
605 * is necessary because 'u' can't set the dot command -- see
606 * vi/v_undo.c:v_undo for details.
608 if (VIP(sp)->u_ccnt == sp->ccnt) {
609 vp->kp = &vikeys['u'];
610 F_SET(vp, VC_ISDOT);
611 return (GC_OK);
614 /* Otherwise, a repeatable command must have been executed. */
615 if (!F_ISSET(dp, VC_ISDOT)) {
616 msgq(sp, M_ERR, "208|No command to repeat");
617 return (GC_ERR);
620 /* Set new count/buffer, if any, and return. */
621 if (F_ISSET(vp, VC_C1SET)) {
622 F_SET(dp, VC_C1SET);
623 dp->count = vp->count;
625 if (F_ISSET(vp, VC_BUFFER))
626 dp->buffer = vp->buffer;
628 *vp = *dp;
629 return (GC_OK);
632 /* Set the flags based on the command flags. */
633 flags = kp->flags;
635 /* Check for illegal count. */
636 if (F_ISSET(vp, VC_C1SET) && !LF_ISSET(V_CNT))
637 goto usage;
639 /* Illegal motion command. */
640 if (ismotion == NULL) {
641 /* Illegal buffer. */
642 if (!LF_ISSET(V_OBUF) && F_ISSET(vp, VC_BUFFER))
643 goto usage;
645 /* Required buffer. */
646 if (LF_ISSET(V_RBUF)) {
647 KEY(vp->buffer, 0);
648 F_SET(vp, VC_BUFFER);
653 * Special case: '[', ']' and 'Z' commands. Doesn't the fact that
654 * the *single* characters don't mean anything but the *doubled*
655 * characters do, just frost your shorts?
657 if (vp->key == '[' || vp->key == ']' || vp->key == 'Z') {
659 * Historically, half entered [[, ]] or Z commands weren't
660 * cancelled by <escape>, the terminal was beeped instead.
661 * POSIX.2-1992 probably didn't notice, and requires that
662 * they be cancelled instead of beeping. Seems fine to me.
664 * Don't set the EC_MAPCOMMAND flag, apparently ] is a popular
665 * vi meta-character, and we don't want the user to wait while
666 * we time out a possible mapping. This *appears* to match
667 * historic vi practice, but with mapping characters, You Just
668 * Never Know.
670 KEY(key, 0);
672 if (vp->key != key) {
673 usage: if (ismotion == NULL)
674 s = kp->usage;
675 else if (ismotion->key == '~' && O_ISSET(sp, O_TILDEOP))
676 s = tmotion.usage;
677 else
678 s = vikeys[ismotion->key].usage;
679 v_emsg(sp, s, VIM_USAGE);
680 return (GC_ERR);
683 /* Special case: 'z' command. */
684 if (vp->key == 'z') {
685 KEY(vp->character, 0);
686 if (isdigit(vp->character)) {
687 if (v_count(sp, vp, vp->character, &vp->count2))
688 return (GC_ERR);
689 F_SET(vp, VC_C2SET);
690 KEY(vp->character, 0);
695 * Commands that have motion components can be doubled to imply the
696 * current line.
698 if (ismotion != NULL && ismotion->key != key && !LF_ISSET(V_MOVE)) {
699 msgq(sp, M_ERR, "210|%s may not be used as a motion command",
700 KEY_NAME(sp, key));
701 return (GC_ERR);
704 /* Pick up required trailing character. */
705 if (LF_ISSET(V_CHAR))
706 KEY(vp->character, 0);
708 /* Get any associated cursor word. */
709 if (F_ISSET(kp, V_KEYW) && v_curword(sp))
710 return (GC_ERR);
712 return (GC_OK);
714 esc: switch (cpart) {
715 case COMMANDMODE:
716 msgq(sp, M_BERR, "211|Already in command mode");
717 return (GC_ERR_NOFLUSH);
718 case ISPARTIAL:
719 break;
720 case NOTPARTIAL:
721 (void)sp->gp->scr_bell(sp);
722 break;
724 return (GC_ERR);
728 * v_motion --
730 * Get resulting motion mark.
732 static int
733 v_motion(sp, dm, vp, mappedp)
734 SCR *sp;
735 VICMD *dm, *vp;
736 int *mappedp;
738 VICMD motion;
739 gcret_t gcret;
740 size_t len;
741 u_long cnt;
742 u_int flags;
743 int tilde_reset, notused;
746 * If '.' command, use the dot motion, else get the motion command.
747 * Clear any line motion flags, the subsequent motion isn't always
748 * the same, i.e. "/aaa" may or may not be a line motion.
750 if (F_ISSET(vp, VC_ISDOT)) {
751 motion = *dm;
752 F_SET(&motion, VC_ISDOT);
753 F_CLR(&motion, VM_COMMASK);
754 gcret = GC_OK;
755 } else {
756 memset(&motion, 0, sizeof(VICMD));
757 gcret = v_cmd(sp, NULL, &motion, vp, &notused, mappedp);
758 if (gcret != GC_OK && gcret != GC_EVENT)
759 return (1);
763 * A count may be provided both to the command and to the motion, in
764 * which case the count is multiplicative. For example, "3y4y" is the
765 * same as "12yy". This count is provided to the motion command and
766 * not to the regular function.
768 cnt = motion.count = F_ISSET(&motion, VC_C1SET) ? motion.count : 1;
769 if (F_ISSET(vp, VC_C1SET)) {
770 motion.count *= vp->count;
771 F_SET(&motion, VC_C1SET);
774 * Set flags to restore the original values of the command
775 * structure so dot commands can change the count values,
776 * e.g. "2dw" "3." deletes a total of five words.
778 F_CLR(vp, VC_C1SET);
779 F_SET(vp, VC_C1RESET);
783 * Some commands can be repeated to indicate the current line. In
784 * this case, or if the command is a "line command", set the flags
785 * appropriately. If not a doubled command, run the function to get
786 * the resulting mark.
788 if (gcret != GC_EVENT && vp->key == motion.key) {
789 F_SET(vp, VM_LDOUBLE | VM_LMODE);
791 /* Set the origin of the command. */
792 vp->m_start.lno = sp->lno;
793 vp->m_start.cno = 0;
796 * Set the end of the command.
798 * If the current line is missing, i.e. the file is empty,
799 * historic vi permitted a "cc" or "!!" command to insert
800 * text.
802 vp->m_stop.lno = sp->lno + motion.count - 1;
803 if (db_get(sp, vp->m_stop.lno, 0, NULL, &len)) {
804 if (vp->m_stop.lno != 1 ||
805 vp->key != 'c' && vp->key != '!') {
806 v_emsg(sp, NULL, VIM_EMPTY);
807 return (1);
809 vp->m_stop.cno = 0;
810 } else
811 vp->m_stop.cno = len ? len - 1 : 0;
812 } else {
814 * Motion commands change the underlying movement (*snarl*).
815 * For example, "l" is illegal at the end of a line, but "dl"
816 * is not. Set flags so the function knows the situation.
818 motion.rkp = vp->kp;
821 * XXX
822 * Use yank instead of creating a new motion command, it's a
823 * lot easier for now.
825 if (vp->kp == &tmotion) {
826 tilde_reset = 1;
827 vp->kp = &vikeys['y'];
828 } else
829 tilde_reset = 0;
832 * Copy the key flags into the local structure, except for the
833 * RCM flags -- the motion command will set the RCM flags in
834 * the vp structure if necessary. This means that the motion
835 * command is expected to determine where the cursor ends up!
836 * However, we save off the current RCM mask and restore it if
837 * it no RCM flags are set by the motion command, with a small
838 * modification.
840 * We replace the VM_RCM_SET flag with the VM_RCM flag. This
841 * is so that cursor movement doesn't set the relative position
842 * unless the motion command explicitly specified it. This
843 * appears to match historic practice, but I've never been able
844 * to develop a hard-and-fast rule.
846 flags = F_ISSET(vp, VM_RCM_MASK);
847 if (LF_ISSET(VM_RCM_SET)) {
848 LF_SET(VM_RCM);
849 LF_CLR(VM_RCM_SET);
851 F_CLR(vp, VM_RCM_MASK);
852 F_SET(&motion, motion.kp->flags & ~VM_RCM_MASK);
855 * Set the three cursor locations to the current cursor. This
856 * permits commands like 'j' and 'k', that are line oriented
857 * motions and have special cursor suck semantics when they are
858 * used as standalone commands, to ignore column positioning.
860 motion.m_final.lno =
861 motion.m_stop.lno = motion.m_start.lno = sp->lno;
862 motion.m_final.cno =
863 motion.m_stop.cno = motion.m_start.cno = sp->cno;
865 /* Run the function. */
866 if ((motion.kp->func)(sp, &motion))
867 return (1);
870 * If the current line is missing, i.e. the file is empty,
871 * historic vi allowed "c<motion>" or "!<motion>" to insert
872 * text. Otherwise fail -- most motion commands will have
873 * already failed, but some, e.g. G, succeed in empty files.
875 if (!db_exist(sp, vp->m_stop.lno)) {
876 if (vp->m_stop.lno != 1 ||
877 vp->key != 'c' && vp->key != '!') {
878 v_emsg(sp, NULL, VIM_EMPTY);
879 return (1);
881 vp->m_stop.cno = 0;
885 * XXX
886 * See above.
888 if (tilde_reset)
889 vp->kp = &tmotion;
892 * Copy cut buffer, line mode and cursor position information
893 * from the motion command structure, i.e. anything that the
894 * motion command can set for us. The commands can flag the
895 * movement as a line motion (see v_sentence) as well as set
896 * the VM_RCM_* flags explicitly.
898 F_SET(vp, F_ISSET(&motion, VM_COMMASK | VM_RCM_MASK));
901 * If the motion command set no relative motion flags, use
902 * the (slightly) modified previous values.
904 if (!F_ISSET(vp, VM_RCM_MASK))
905 F_SET(vp, flags);
908 * Commands can change behaviors based on the motion command
909 * used, for example, the ! command repeated the last bang
910 * command if N or n was used as the motion.
912 vp->rkp = motion.kp;
915 * Motion commands can reset all of the cursor information.
916 * If the motion is in the reverse direction, switch the
917 * from and to MARK's so that it's in a forward direction.
918 * Motions are from the from MARK to the to MARK (inclusive).
920 if (motion.m_start.lno > motion.m_stop.lno ||
921 motion.m_start.lno == motion.m_stop.lno &&
922 motion.m_start.cno > motion.m_stop.cno) {
923 vp->m_start = motion.m_stop;
924 vp->m_stop = motion.m_start;
925 } else {
926 vp->m_start = motion.m_start;
927 vp->m_stop = motion.m_stop;
929 vp->m_final = motion.m_final;
933 * If the command sets dot, save the motion structure. The motion
934 * count was changed above and needs to be reset, that's why this
935 * is done here, and not in the calling routine.
937 if (F_ISSET(vp->kp, V_DOT)) {
938 *dm = motion;
939 dm->count = cnt;
941 return (0);
945 * v_init --
946 * Initialize the vi screen.
948 static int
949 v_init(sp)
950 SCR *sp;
952 GS *gp;
953 VI_PRIVATE *vip;
955 gp = sp->gp;
956 vip = VIP(sp);
958 /* Switch into vi. */
959 if (gp->scr_screen(sp, SC_VI))
960 return (1);
961 (void)gp->scr_attr(sp, SA_ALTERNATE, 1);
963 F_CLR(sp, SC_EX | SC_SCR_EX);
964 F_SET(sp, SC_VI);
967 * Initialize screen values.
969 * Small windows: see vs_refresh(), section 6a.
971 * Setup:
972 * t_minrows is the minimum rows to display
973 * t_maxrows is the maximum rows to display (rows - 1)
974 * t_rows is the rows currently being displayed
976 sp->rows = vip->srows = O_VAL(sp, O_LINES);
977 sp->cols = O_VAL(sp, O_COLUMNS);
978 sp->t_rows = sp->t_minrows = O_VAL(sp, O_WINDOW);
979 if (sp->rows != 1) {
980 if (sp->t_rows > sp->rows - 1) {
981 sp->t_minrows = sp->t_rows = sp->rows - 1;
982 msgq(sp, M_INFO,
983 "214|Windows option value is too large, max is %u",
984 sp->t_rows);
986 sp->t_maxrows = sp->rows - 1;
987 } else
988 sp->t_maxrows = 1;
989 sp->roff = sp->coff = 0;
991 /* Create a screen map. */
992 CALLOC_RET(sp, HMAP, SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
993 TMAP = HMAP + (sp->t_rows - 1);
994 HMAP->lno = sp->lno;
995 HMAP->coff = 0;
996 HMAP->soff = 1;
999 * Fill the screen map from scratch -- try and center the line. That
1000 * way if we're starting with a file we've seen before, we'll put the
1001 * line in the middle, otherwise, it won't work and we'll end up with
1002 * the line at the top.
1004 F_SET(sp, SC_SCR_REFORMAT | SC_SCR_CENTER);
1006 /* Invalidate the cursor. */
1007 F_SET(vip, VIP_CUR_INVALID);
1009 /* Paint the screen image from scratch. */
1010 F_SET(vip, VIP_N_EX_PAINT);
1012 return (0);
1016 * v_dtoh --
1017 * Move all but the current screen to the hidden queue.
1019 static void
1020 v_dtoh(sp)
1021 SCR *sp;
1023 GS *gp;
1024 SCR *tsp;
1025 int hidden;
1027 /* Move all screens to the hidden queue, tossing screen maps. */
1028 for (hidden = 0, gp = sp->gp;
1029 (tsp = gp->dq.cqh_first) != (void *)&gp->dq; ++hidden) {
1030 if (_HMAP(tsp) != NULL) {
1031 free(_HMAP(tsp));
1032 _HMAP(tsp) = NULL;
1034 CIRCLEQ_REMOVE(&gp->dq, tsp, q);
1035 CIRCLEQ_INSERT_TAIL(&gp->hq, tsp, q);
1038 /* Move current screen back to the display queue. */
1039 CIRCLEQ_REMOVE(&gp->hq, sp, q);
1040 CIRCLEQ_INSERT_TAIL(&gp->dq, sp, q);
1042 if (hidden > 1)
1043 msgq(sp, M_INFO,
1044 "319|%d screens backgrounded; use :display to list them",
1045 hidden - 1);
1049 * v_curword --
1050 * Get the word (or non-word) the cursor is on.
1052 * PUBLIC: int v_curword __P((SCR *));
1055 v_curword(sp)
1056 SCR *sp;
1058 VI_PRIVATE *vip;
1059 size_t beg, end, len;
1060 int moved, state;
1061 char *p;
1063 if (db_get(sp, sp->lno, DBG_FATAL, &p, &len))
1064 return (1);
1067 * !!!
1068 * Historically, tag commands skipped over any leading whitespace
1069 * characters. Make this true in general when using cursor words.
1070 * If movement, getting a cursor word implies moving the cursor to
1071 * its beginning. Refresh now.
1073 * !!!
1074 * Find the beginning/end of the keyword. Keywords are currently
1075 * used for cursor-word searching and for tags. Historical vi
1076 * only used the word in a tag search from the cursor to the end
1077 * of the word, i.e. if the cursor was on the 'b' in " abc ", the
1078 * tag was "bc". For consistency, we make cursor word searches
1079 * follow the same rule.
1081 for (moved = 0,
1082 beg = sp->cno; beg < len && isspace(p[beg]); moved = 1, ++beg);
1083 if (beg >= len) {
1084 msgq(sp, M_BERR, "212|Cursor not in a word");
1085 return (1);
1087 if (moved) {
1088 sp->cno = beg;
1089 (void)vs_refresh(sp, 0);
1092 /* Find the end of the word. */
1093 for (state = inword(p[beg]),
1094 end = beg; ++end < len && state == inword(p[end]););
1096 vip = VIP(sp);
1097 len = (end - beg);
1098 BINC_RET(sp, vip->keyw, vip->klen, len);
1099 memmove(vip->keyw, p + beg, len);
1100 vip->keyw[len] = '\0'; /* XXX */
1101 return (0);
1105 * v_alias --
1106 * Check for a command alias.
1108 static VIKEYS const *
1109 v_alias(sp, vp, kp)
1110 SCR *sp;
1111 VICMD *vp;
1112 VIKEYS const *kp;
1114 CHAR_T push;
1116 switch (vp->key) {
1117 case 'C': /* C -> c$ */
1118 push = '$';
1119 vp->key = 'c';
1120 break;
1121 case 'D': /* D -> d$ */
1122 push = '$';
1123 vp->key = 'd';
1124 break;
1125 case 'S': /* S -> c_ */
1126 push = '_';
1127 vp->key = 'c';
1128 break;
1129 case 'Y': /* Y -> y_ */
1130 push = '_';
1131 vp->key = 'y';
1132 break;
1133 default:
1134 return (kp);
1136 return (v_event_push(sp,
1137 NULL, &push, 1, CH_NOMAP | CH_QUOTED) ? NULL : &vikeys[vp->key]);
1141 * v_count --
1142 * Return the next count.
1144 static int
1145 v_count(sp, vp, fkey, countp)
1146 SCR *sp;
1147 VICMD *vp;
1148 ARG_CHAR_T fkey;
1149 u_long *countp;
1151 u_long count, tc;
1153 vp->ev.e_c = fkey;
1154 count = tc = 0;
1155 do {
1157 * XXX
1158 * Assume that overflow results in a smaller number.
1160 tc = count * 10 + vp->ev.e_c - '0';
1161 if (count > tc) {
1162 /* Toss to the next non-digit. */
1163 do {
1164 if (v_key(sp, vp, 0,
1165 EC_MAPCOMMAND | EC_MAPNODIGIT) != GC_OK)
1166 return (1);
1167 } while (isdigit(vp->ev.e_c));
1168 msgq(sp, M_ERR,
1169 "235|Number larger than %lu", ULONG_MAX);
1170 return (1);
1172 count = tc;
1173 if (v_key(sp, vp, 0, EC_MAPCOMMAND | EC_MAPNODIGIT) != GC_OK)
1174 return (1);
1175 } while (isdigit(vp->ev.e_c));
1176 *countp = count;
1177 return (0);
1181 * v_key --
1182 * Return the next event.
1184 static gcret_t
1185 v_key(sp, vp, events_ok, ec_flags)
1186 SCR *sp;
1187 VICMD *vp;
1188 int events_ok;
1189 u_int32_t ec_flags;
1191 EVENT *evp;
1192 u_int32_t quote;
1194 for (evp = &vp->ev, quote = 0;;) {
1195 if (v_event_get(sp, evp, 0, ec_flags | quote))
1196 return (GC_FATAL);
1197 quote = 0;
1199 switch (evp->e_event) {
1200 case E_CHARACTER:
1202 * !!!
1203 * Historically, ^V was ignored in the command stream,
1204 * although it had a useful side-effect of interrupting
1205 * mappings. Adding a quoting bit to the call probably
1206 * extends historic practice, but it feels right.
1208 if (evp->e_value == K_VLNEXT) {
1209 quote = EC_QUOTED;
1210 break;
1212 return (GC_OK);
1213 case E_ERR:
1214 case E_EOF:
1215 return (GC_FATAL);
1216 case E_INTERRUPT:
1218 * !!!
1219 * Historically, vi beeped on command level interrupts.
1221 * Historically, vi exited to ex mode if no file was
1222 * named on the command line, and two interrupts were
1223 * generated in a row. (I figured you might want to
1224 * know that, just in case there's a quiz later.)
1226 (void)sp->gp->scr_bell(sp);
1227 return (GC_INTERRUPT);
1228 case E_REPAINT:
1229 if (v_erepaint(sp, evp))
1230 return (GC_FATAL);
1231 break;
1232 case E_WRESIZE:
1234 * !!!
1235 * We don't do anything here, just return an error.
1236 * The vi loop will return because of this, and then
1237 * the main loop will realize that we had to restart
1238 * the world and will call the vi loop again.
1240 return (GC_ERR);
1241 case E_IPCOMMAND:
1242 if (events_ok)
1243 return (GC_EVENT);
1244 /* FALLTHROUGH */
1245 default:
1246 v_event_err(sp, evp);
1247 return (GC_ERR);
1250 /* NOTREACHED */
1253 #if defined(DEBUG) && defined(COMLOG)
1255 * v_comlog --
1256 * Log the contents of the command structure.
1258 static void
1259 v_comlog(sp, vp)
1260 SCR *sp;
1261 VICMD *vp;
1263 vtrace(sp, "vcmd: %c", vp->key);
1264 if (F_ISSET(vp, VC_BUFFER))
1265 vtrace(sp, " buffer: %c", vp->buffer);
1266 if (F_ISSET(vp, VC_C1SET))
1267 vtrace(sp, " c1: %lu", vp->count);
1268 if (F_ISSET(vp, VC_C2SET))
1269 vtrace(sp, " c2: %lu", vp->count2);
1270 vtrace(sp, " flags: 0x%x\n", vp->flags);
1272 #endif