3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: v_txt.c,v 8.71 1993/12/22 17:24:42 bostic Exp $ (Berkeley) $Date: 1993/12/22 17:24:42 $";
12 #include <sys/types.h>
25 static int txt_abbrev
__P((SCR
*, TEXT
*, ARG_CHAR_T
, int, int *, int *));
26 static void txt_ai_resolve
__P((SCR
*, TEXT
*));
27 static TEXT
*txt_backup
__P((SCR
*, EXF
*, TEXTH
*, TEXT
*, u_int
));
28 static void txt_err
__P((SCR
*, EXF
*, TEXTH
*));
29 static int txt_hex
__P((SCR
*, TEXT
*, int *, ARG_CHAR_T
));
30 static int txt_indent
__P((SCR
*, TEXT
*));
31 static int txt_margin
__P((SCR
*, TEXT
*, int *, ARG_CHAR_T
));
32 static int txt_outdent
__P((SCR
*, TEXT
*));
33 static void txt_showmatch
__P((SCR
*, EXF
*));
34 static int txt_resolve
__P((SCR
*, EXF
*, TEXTH
*));
36 /* Cursor character (space is hard to track on the screen). */
37 #if defined(DEBUG) && 0
42 /* Local version of BINC. */
43 #define TBINC(sp, lp, llen, nlen) { \
44 if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \
50 * Read in text from the user.
53 * Historic vi always used:
55 * ^D: autoindent deletion
56 * ^H: last character deletion
57 * ^W: last word deletion
58 * ^V: quote the next character
60 * regardless of the user's choices for these characters. The user's erase
61 * and kill characters worked in addition to these characters. Ex was not
62 * completely consistent with this, as it did map the scroll command to the
63 * user's EOF character.
65 * This implementation does not use fixed characters, but uses whatever the
66 * user specified as described by the termios structure. I'm getting away
67 * with something here, but I think I'm unlikely to get caught.
70 * Historic vi did a special screen optimization for tab characters. For
71 * the keystrokes "iabcd<esc>0C<tab>", the tab would overwrite the rest of
72 * the string when it was displayed. Because this implementation redisplays
73 * the entire line on each keystroke, the "bcd" gets pushed to the right as
74 * we ignore that the user has "promised" to change the rest of the characters.
75 * Users have noticed, but this isn't worth fixing, and, the way that the
76 * historic vi did it results in an even worse bug. Given the keystrokes
77 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
78 * on the second <esc> key.
81 v_ntext(sp
, ep
, tiqh
, tm
, lp
, len
, rp
, prompt
, ai_line
, flags
)
85 MARK
*tm
; /* To MARK. */
86 const char *lp
; /* Input line. */
87 const size_t len
; /* Input line length. */
88 MARK
*rp
; /* Return MARK. */
89 int prompt
; /* Prompt to display. */
90 recno_t ai_line
; /* Line number to use for autoindent count. */
91 u_int flags
; /* TXT_ flags. */
93 /* State of abbreviation checks. */
94 enum { A_NOTSET
, A_SPACE
, A_NOTSPACE
} abb
;
95 /* State of the "[^0]^D" sequences. */
96 enum { C_NOTSET
, C_CARATSET
, C_NOCHANGE
, C_ZEROSET
} carat_st
;
97 /* State of the hex input character. */
98 enum { H_NOTSET
, H_NEXTCHAR
, H_INHEX
} hex
;
99 /* State of quotation. */
100 enum { Q_NOTSET
, Q_NEXTCHAR
, Q_THISCHAR
} quoted
;
101 CH ikey
; /* Input character structure. */
102 CHAR_T ch
; /* Input character. */
103 GS
*gp
; /* Global pointer. */
104 TEXT
*tp
, *ntp
, ait
; /* Input and autoindent text structures. */
105 size_t rcol
; /* 0-N: insert offset in the replay buffer. */
106 size_t col
; /* Current column. */
107 u_long margin
; /* Wrapmargin value. */
108 u_int iflags
; /* Input flags. */
109 int ab_cnt
, ab_turnoff
; /* Abbreviation count, if turned off. */
110 int eval
; /* Routine return value. */
111 int replay
; /* If replaying a set of input. */
112 int showmatch
; /* Showmatch set on this character. */
113 int testnr
; /* Test first character for nul replay. */
118 * Set the input flag, so tabs get displayed correctly
119 * and everyone knows that the text buffer is in use.
123 /* Local initialization. */
128 * Get one TEXT structure with some initial buffer space, reusing
129 * the last one if it's big enough. (All TEXT bookkeeping fields
130 * default to 0 -- text_init() handles this.) If changing a line,
131 * copy it into the TEXT buffer.
133 if (tiqh
->cqh_first
!= (void *)tiqh
) {
134 tp
= tiqh
->cqh_first
;
135 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< len
+ 32) {
139 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
142 memmove(tp
->lb
, lp
, len
);
146 newtp
: if ((tp
= text_init(sp
, lp
, len
, len
+ 32)) == NULL
)
148 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
151 /* Set the starting line number. */
155 * Set the insert and overwrite counts. If overwriting characters,
156 * do insertion afterward. If not overwriting characters, assume
157 * doing insertion. If change is to a mark, emphasize it with an
161 if (LF_ISSET(TXT_OVERWRITE
)) {
162 tp
->owrite
= tm
->cno
- sp
->cno
;
163 tp
->insert
= len
- tm
->cno
;
165 tp
->insert
= len
- sp
->cno
;
167 if (LF_ISSET(TXT_EMARK
))
168 tp
->lb
[tm
->cno
- 1] = END_CH
;
172 * Many of the special cases in this routine are to handle autoindent
173 * support. Somebody decided that it would be a good idea if "^^D"
174 * and "0^D" deleted all of the autoindented characters. In an editor
175 * that takes single character input from the user, this wasn't a very
176 * good idea. Note also that "^^D" resets the next lines' autoindent,
179 * We assume that autoindent only happens on empty lines, so insert
180 * and overwrite will be zero. If doing autoindent, figure out how
181 * much indentation we need and fill it in. Update input column and
182 * screen cursor as necessary.
184 if (LF_ISSET(TXT_AUTOINDENT
) && ai_line
!= OOBLNO
) {
185 if (txt_auto(sp
, ep
, ai_line
, NULL
, 0, tp
))
190 * The cc and S commands have a special feature -- leading
191 * <blank> characters are handled as autoindent characters.
194 if (LF_ISSET(TXT_AICHARS
)) {
198 tp
->offset
= sp
->cno
;
201 /* If getting a command buffer from the user, there may be a prompt. */
202 if (LF_ISSET(TXT_PROMPT
)) {
203 tp
->lb
[sp
->cno
++] = prompt
;
209 * If appending after the end-of-line, add a space into the buffer
210 * and move the cursor right. This space is inserted, i.e. pushed
211 * along, and then deleted when the line is resolved. Assumes that
212 * the cursor is already positioned at the end of the line. This
213 * avoids the nastiness of having the cursor reside on a magical
214 * column, i.e. a column that doesn't really exist. The only down
215 * side is that we may wrap lines or scroll the screen before it's
216 * strictly necessary. Not a big deal.
218 if (LF_ISSET(TXT_APPENDEOL
)) {
219 tp
->lb
[sp
->cno
] = CURSOR_CH
;
225 * Historic practice is that the wrapmargin value was a distance
226 * from the RIGHT-HAND column, not the left. It's more useful to
227 * us as a distance from the left-hand column.
230 * Replay commands are not affected by wrapmargin values. What
231 * I found surprising was that people actually depend on it, as
232 * in this gem of a macro which centers lines:
234 * map #c $mq81a ^V^[81^V|D`qld0:s/ / /g^V^M$p
237 * Setting margin causes a significant performance hit. Normally
238 * we don't update the screen if there are keys waiting, but we
239 * have to if margin is set, otherwise the screen routines don't
240 * know where the cursor is.
242 if (LF_ISSET(TXT_REPLAY
) || !LF_ISSET(TXT_WRAPMARGIN
))
244 else if ((margin
= O_VAL(sp
, O_WRAPMARGIN
)) != 0)
245 margin
= sp
->cols
- margin
;
247 /* Initialize abbreviations checks. */
248 if (F_ISSET(gp
, G_ABBREV
) && LF_ISSET(TXT_MAPINPUT
)) {
250 ab_cnt
= ab_turnoff
= 0;
255 * Set up the dot command. Dot commands are done by saving the
256 * actual characters and replaying the input. We have to push
257 * the characters onto the key stack and then handle them normally,
258 * otherwise things like wrapmargin will fail.
261 * It would be nice if we could swallow backspaces and such, but
262 * it's not all that easy to do. Another possibility would be to
263 * recognize full line insertions, which could be performed quickly,
268 if (replay
= LF_ISSET(TXT_REPLAY
)) {
271 * Historically, it wasn't an error to replay non-existent
272 * input. This test is necessary, we get here by the user
273 * doing an input command followed by a nul.
276 * Historically, vi did not remap or reabbreviate replayed
277 * input. It did, however, beep at you if you changed an
278 * abbreviation and then replayed the input. We're not that
281 if (VIP(sp
)->rep
== NULL
)
283 if (term_push(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_cnt
, 0, CH_NOMAP
))
291 iflags
= LF_ISSET(TXT_MAPCOMMAND
| TXT_MAPINPUT
);
292 for (gp
, showmatch
= 0,
293 carat_st
= C_NOTSET
, hex
= H_NOTSET
, quoted
= Q_NOTSET
;;) {
295 * Reset the line and update the screen. (The txt_showmatch()
296 * code refreshes the screen for us.) Don't refresh unless
297 * we're about to wait on a character or we need to know where
298 * the cursor really is.
300 if (showmatch
|| margin
|| !KEYS_WAITING(sp
)) {
301 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
305 txt_showmatch(sp
, ep
);
306 } else if (sp
->s_refresh(sp
, ep
))
310 /* Get the next character. */
311 next_ch
: if (term_key(sp
, &ikey
, iflags
) != INP_OK
)
315 /* Abbreviation check. See comment in txt_abbrev(). */
316 #define MAX_ABBREVIATION_EXPANSION 256
317 if (ikey
.flags
& CH_ABBREVIATED
) {
318 if (++ab_cnt
> MAX_ABBREVIATION_EXPANSION
) {
320 "Abbreviation exceeded maximum number of characters");
329 * Historic feature. If the first character of the input is
330 * a nul, replay the previous input. This isn't documented
331 * anywhere, and is a great test of vi clones.
333 if (ch
== '\0' && testnr
) {
340 * Check to see if the character fits into the input (and
341 * replay, if necessary) buffers. It isn't necessary to
342 * have tp->len bytes, since it doesn't consider overwrite
343 * characters, but not worth fixing.
345 if (LF_ISSET(TXT_RECORD
)) {
346 TBINC(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_len
, rcol
+ 1);
347 VIP(sp
)->rep
[rcol
++] = ch
;
349 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
352 * If the character was quoted, replace the last character
353 * (the literal mark) with the new character. If quoted
354 * by someone else, simply insert the character.
357 * Extension -- if the quoted character is HEX_CH, enter hex
358 * mode. If the user enters "<HEX_CH>[isxdigit()]*" we will
359 * try to use the value as a character. Anything else resets
362 if (ikey
.flags
& CH_QUOTED
)
364 if (quoted
== Q_THISCHAR
) {
374 switch (ikey
.value
) {
376 case K_NL
: /* New line. */
377 #define LINE_RESOLVE { \
379 * Handle abbreviations. If there was one, \
380 * discard the replay characters. \
382 if (abb == A_NOTSPACE && !replay) { \
383 if (txt_abbrev(sp, tp, ch, \
384 LF_ISSET(TXT_INFOLINE), &tmp, \
388 if (LF_ISSET(TXT_RECORD)) \
393 if (abb != A_NOTSET) \
395 /* Handle hex numbers. */ \
396 if (hex == H_INHEX) { \
397 if (txt_hex(sp, tp, &tmp, ch)) \
405 * The 'R' command returns any overwriteable \
406 * characters in the first line to the original \
409 if (LF_ISSET(TXT_REPLACE) && tp->owrite && \
410 tp == tiqh->cqh_first) { \
411 memmove(tp->lb + sp->cno, \
412 lp + sp->cno, tp->owrite); \
413 tp->insert += tp->owrite; \
416 /* Delete any appended cursor. */ \
417 if (LF_ISSET(TXT_APPENDEOL)) { \
424 /* CR returns from the vi command line. */
425 if (LF_ISSET(TXT_CR
)) {
427 * If a script window and not the colon
428 * line, push a <cr> so it gets executed.
430 if (F_ISSET(sp
, S_SCRIPT
) &&
431 !LF_ISSET(TXT_INFOLINE
))
433 "\r", 1, 0, CH_NOMAP
);
438 * Historic practice was to delete any <blank>
439 * characters following the inserted newline.
440 * This affects the 'R', 'c', and 's' commands.
442 for (p
= tp
->lb
+ sp
->cno
+ tp
->owrite
;
443 tp
->insert
&& isblank(*p
);
444 ++p
, ++tp
->owrite
, --tp
->insert
);
447 * Move any remaining insert characters into
448 * a new TEXT structure.
450 if ((ntp
= text_init(sp
,
451 tp
->lb
+ sp
->cno
+ tp
->owrite
,
452 tp
->insert
, tp
->insert
+ 32)) == NULL
)
454 CIRCLEQ_INSERT_TAIL(tiqh
, ntp
, q
);
456 /* Set bookkeeping for the new line. */
457 ntp
->lno
= tp
->lno
+ 1;
458 ntp
->insert
= tp
->insert
;
461 * Note if the user inserted any characters on this
462 * line. Done before calling txt_ai_resolve() because
463 * it changes the value of sp->cno without making the
464 * corresponding changes to tp->ai.
466 tmp
= sp
->cno
<= tp
->ai
;
469 * Resolve autoindented characters for the old line.
470 * Reset the autoindent line value. 0^D keeps the ai
471 * line from changing, ^D changes the level, even if
472 * there are no characters in the old line. Note,
473 * if using the current tp structure, use the cursor
474 * as the length, the user may have erased autoindent
477 if (LF_ISSET(TXT_AUTOINDENT
)) {
478 txt_ai_resolve(sp
, tp
);
480 if (carat_st
== C_NOCHANGE
) {
482 OOBLNO
, &ait
, ait
.ai
, ntp
))
484 FREE_SPACE(sp
, ait
.lb
, ait
.lb_len
);
487 OOBLNO
, tp
, sp
->cno
, ntp
))
493 * If the user hasn't entered any characters, delete
494 * any autoindent characters.
497 * Historic vi didn't get the insert test right, if
498 * there were characters after the cursor, entering
499 * a <cr> left the autoindent characters on the line.
504 /* Reset bookkeeping for the old line. */
506 tp
->ai
= tp
->insert
= tp
->owrite
= 0;
508 /* New cursor position. */
511 /* New lines are TXT_APPENDEOL if nothing to insert. */
512 if (ntp
->insert
== 0) {
513 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
514 LF_SET(TXT_APPENDEOL
);
515 ntp
->lb
[sp
->cno
] = CURSOR_CH
;
520 /* Update the old line. */
521 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
524 /* Swap old and new TEXT's. */
527 /* Reset the cursor. */
530 /* Update the new line. */
531 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_INSERT
))
534 /* Set the renumber bit. */
535 F_SET(sp
, S_RENUMBER
);
537 /* Refresh if nothing waiting. */
538 if ((margin
|| !KEYS_WAITING(sp
)) &&
539 sp
->s_refresh(sp
, ep
))
542 case K_ESCAPE
: /* Escape. */
543 if (!LF_ISSET(TXT_ESCAPE
))
549 * If there aren't any trailing characters in the line
550 * and the user hasn't entered any characters, delete
551 * the autoindent characters.
553 if (!tp
->insert
&& sp
->cno
<= tp
->ai
) {
554 tp
->len
= tp
->owrite
= 0;
556 } else if (LF_ISSET(TXT_AUTOINDENT
))
557 txt_ai_resolve(sp
, tp
);
559 /* If there are insert characters, copy them down. */
560 k_escape
: if (tp
->insert
&& tp
->owrite
)
561 memmove(tp
->lb
+ sp
->cno
,
562 tp
->lb
+ sp
->cno
+ tp
->owrite
, tp
->insert
);
563 tp
->len
-= tp
->owrite
;
566 * Delete any lines that were inserted into the text
567 * structure and then erased.
569 while (tp
->q
.cqe_next
!= (void *)tiqh
) {
570 ntp
= tp
->q
.cqe_next
;
571 CIRCLEQ_REMOVE(tiqh
, ntp
, q
);
576 * If not resolving the lines into the file, end
580 * This is wrong, should pass back a length.
582 if (LF_ISSET(TXT_RESOLVE
)) {
583 if (txt_resolve(sp
, ep
, tiqh
))
586 * Clear input flag -- input buffer no longer
591 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
592 tp
->lb
[tp
->len
] = '\0';
596 * Set the return cursor position to rest on the last
597 * inserted character.
601 rp
->cno
= sp
->cno
? sp
->cno
- 1 : 0;
602 if (sp
->s_change(sp
, ep
, rp
->lno
, LINE_RESET
))
606 case K_CARAT
: /* Delete autoindent chars. */
607 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
608 carat_st
= C_CARATSET
;
610 case K_ZERO
: /* Delete autoindent chars. */
611 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
612 carat_st
= C_ZEROSET
;
614 case K_VEOF
: /* Delete autoindent char. */
616 * If in the first column or no characters to erase,
617 * ignore the ^D (this matches historic practice). If
618 * not doing autoindent or already inserted non-ai
619 * characters, it's a literal. The latter test is done
620 * in the switch, as the CARAT forms are N + 1, not N.
622 if (!LF_ISSET(TXT_AUTOINDENT
))
624 if (sp
->cno
== 0 || tp
->ai
== 0)
627 case C_CARATSET
: /* ^^D */
628 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
631 /* Save the ai string for later. */
634 TBINC(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
635 memmove(ait
.lb
, tp
->lb
, tp
->ai
);
636 ait
.ai
= ait
.len
= tp
->ai
;
638 carat_st
= C_NOCHANGE
;
640 case C_ZEROSET
: /* 0^D */
641 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
644 leftmargin
: tp
->lb
[sp
->cno
- 1] = ' ';
645 tp
->owrite
+= sp
->cno
- tp
->offset
;
647 sp
->cno
= tp
->offset
;
649 case C_NOTSET
: /* ^D */
650 if (sp
->cno
> tp
->ai
+ tp
->offset
)
652 (void)txt_outdent(sp
, tp
);
658 case K_VERASE
: /* Erase the last character. */
660 * If can erase over the prompt, return. Len is 0
661 * if backspaced over the prompt, 1 if only CR entered.
663 if (LF_ISSET(TXT_BS
) && sp
->cno
<= tp
->offset
) {
669 * If at the beginning of the line, try and drop back
670 * to a previously inserted line.
673 if ((ntp
= txt_backup(sp
,
674 ep
, tiqh
, tp
, flags
)) == NULL
)
680 /* If nothing to erase, bell the user. */
681 if (sp
->cno
<= tp
->offset
) {
683 "No more characters to erase.");
687 /* Drop back one character. */
691 * Increment overwrite, decrement ai if deleted.
694 * Historic vi did not permit users to use erase
695 * characters to delete autoindent characters.
698 if (sp
->cno
< tp
->ai
)
701 case K_VWERASE
: /* Skip back one word. */
703 * If at the beginning of the line, try and drop back
704 * to a previously inserted line.
707 if ((ntp
= txt_backup(sp
,
708 ep
, tiqh
, tp
, flags
)) == NULL
)
714 * If at offset, nothing to erase so bell the user.
716 if (sp
->cno
<= tp
->offset
) {
718 "No more characters to erase.");
723 * First werase goes back to any autoindent
724 * and second werase goes back to the offset.
727 * Historic vi did not permit users to use erase
728 * characters to delete autoindent characters.
730 if (tp
->ai
&& sp
->cno
> tp
->ai
)
737 /* Skip over trailing space characters. */
738 while (sp
->cno
> max
&& isblank(tp
->lb
[sp
->cno
- 1])) {
745 * There are three types of word erase found on UNIX
746 * systems. They can be identified by how the string
747 * /a/b/c is treated -- as 1, 3, or 6 words. Historic
748 * vi had two classes of characters, and strings were
749 * delimited by them and <blank>'s, so, 6 words. The
750 * historic tty interface used <blank>'s to delimit
751 * strings, so, 1 word. The algorithm offered in the
752 * 4.4BSD tty interface (as stty altwerase) treats it
753 * as 3 words -- there are two classes of characters,
754 * and strings are delimited by them and <blank>'s.
755 * The difference is that the type of the first erased
756 * character erased is ignored, which is exactly right
757 * when erasing pathname components. Here, the options
758 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD
759 * tty interface and the historic tty driver behavior,
760 * respectively, and the default is the same as the
761 * historic vi behavior.
763 if (LF_ISSET(TXT_TTYWERASE
))
764 while (sp
->cno
> max
) {
767 if (isblank(tp
->lb
[sp
->cno
- 1]))
771 if (LF_ISSET(TXT_ALTWERASE
)) {
774 if (isblank(tp
->lb
[sp
->cno
- 1]))
778 tmp
= inword(tp
->lb
[sp
->cno
- 1]);
779 while (sp
->cno
> max
) {
782 if (tmp
!= inword(tp
->lb
[sp
->cno
- 1])
783 || isblank(tp
->lb
[sp
->cno
- 1]))
788 case K_VKILL
: /* Restart this line. */
790 * If at the beginning of the line, try and drop back
791 * to a previously inserted line.
794 if ((ntp
= txt_backup(sp
,
795 ep
, tiqh
, tp
, flags
)) == NULL
)
800 /* If at offset, nothing to erase so bell the user. */
801 if (sp
->cno
<= tp
->offset
) {
803 "No more characters to erase.");
808 * First kill goes back to any autoindent
809 * and second kill goes back to the offset.
812 * Historic vi did not permit users to use erase
813 * characters to delete autoindent characters.
815 if (tp
->ai
&& sp
->cno
> tp
->ai
)
821 tp
->owrite
+= sp
->cno
- max
;
824 case K_CNTRLT
: /* Add autoindent char. */
825 if (!LF_ISSET(TXT_CNTRLT
))
827 if (txt_indent(sp
, tp
))
831 (void)sp
->s_suspend(sp
);
833 #ifdef HISTORIC_PRACTICE_IS_TO_INSERT_NOT_REPAINT
835 F_SET(sp
, S_REFRESH
);
840 showmatch
= LF_ISSET(TXT_SHOWMATCH
);
842 case K_VLNEXT
: /* Quote the next character. */
843 /* If in hex mode, see if we've entered a hex value. */
844 if (hex
== H_INHEX
) {
845 if (txt_hex(sp
, tp
, &tmp
, ch
))
855 default: /* Insert the character. */
857 * If entering a space character after a word, check
858 * for abbreviations. If there was one, discard the
861 if (isblank(ch
) && abb
== A_NOTSPACE
&& !replay
) {
862 if (txt_abbrev(sp
, tp
, ch
,
863 LF_ISSET(TXT_INFOLINE
), &tmp
, &ab_turnoff
))
866 if (LF_ISSET(TXT_RECORD
))
871 /* If in hex mode, see if we've entered a hex value. */
872 if (hex
== H_INHEX
&& !isxdigit(ch
)) {
873 if (txt_hex(sp
, tp
, &tmp
, ch
))
880 /* Check to see if we've crossed the margin. */
882 if (sp
->s_column(sp
, ep
, &col
))
885 if (txt_margin(sp
, tp
, &tmp
, ch
))
892 abb
= isblank(ch
) ? A_SPACE
: A_NOTSPACE
;
894 if (tp
->owrite
) /* Overwrite a character. */
896 else if (tp
->insert
) { /* Insert a character. */
899 tp
->lb
[sp
->cno
+ 1] = tp
->lb
[sp
->cno
];
901 memmove(tp
->lb
+ sp
->cno
+ 1,
902 tp
->lb
+ sp
->cno
, tp
->insert
);
905 tp
->lb
[sp
->cno
++] = ch
;
908 * If we've reached the end of the buffer, then we
909 * need to switch into insert mode. This happens
910 * when there's a change to a mark and the user puts
911 * in more characters than the length of the motion.
913 ebuf_chk
: if (sp
->cno
>= tp
->len
) {
914 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
915 LF_SET(TXT_APPENDEOL
);
916 tp
->lb
[sp
->cno
] = CURSOR_CH
;
921 if (hex
== H_NEXTCHAR
)
923 if (quoted
== Q_NEXTCHAR
)
927 #if defined(DEBUG) && 1
928 if (sp
->cno
+ tp
->insert
+ tp
->owrite
!= tp
->len
)
930 "len %u != cno: %u ai: %u insert %u overwrite %u",
931 tp
->len
, sp
->cno
, tp
->ai
, tp
->insert
, tp
->owrite
);
932 tp
->len
= sp
->cno
+ tp
->insert
+ tp
->owrite
;
936 /* Clear input flag. */
937 ret
: F_CLR(sp
, S_INPUT
);
939 if (LF_ISSET(TXT_RECORD
))
940 VIP(sp
)->rep_cnt
= rcol
;
945 txt_err(sp
, ep
, tiqh
);
951 * Handle abbreviations.
954 txt_abbrev(sp
, tp
, pushc
, isinfoline
, didsubp
, turnoffp
)
958 int isinfoline
, *didsubp
, *turnoffp
;
965 /* Find the beginning of this "word". */
966 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
972 if (off
== tp
->ai
|| off
== tp
->offset
)
978 * Historic vi exploded abbreviations on the command line. This has
979 * obvious problems in that unabbreviating the string can be extremely
980 * tricky, particularly if the string has, say, an embedded escape
981 * character. Personally, I think it's a stunningly bad idea. Other
982 * examples of problems this caused in historic vi are:
987 * results in "bar" abbreviated to "baz", which wasn't what the user
988 * had in mind at all. Also, the commands:
993 * resulted in an error message that "foo" wasn't mapped. However,
994 * people sadly neglected to first ask my opinion before they wrote
995 * macros that depend on it.
997 * We make this work as follows. When checking for an abbreviation on
998 * the command line, if we get a string which is <blank> terminated and
999 * which starts at the beginning of the line, we check to see it is the
1000 * abbreviate or unabbreviate commands. If it is, turn abbreviations
1001 * off and return as if no abbreviation was found. Note also, minor
1002 * trickiness, so that if the user erases the line and starts another
1003 * command, we turn abbreviations back on.
1005 * This makes the layering look like a Nachos Supreme.
1009 if (off
== tp
->ai
|| off
== tp
->offset
)
1010 if (ex_is_abbrev(p
, len
)) {
1019 /* Check for any abbreviations. */
1020 if ((qp
= seq_find(sp
, NULL
, p
, len
, SEQ_ABBREV
, NULL
)) == NULL
)
1024 * Push the abbreviation onto the tty stack. Historically, characters
1025 * resulting from an abbreviation expansion were themselves subject to
1026 * map expansions, O_SHOWMATCH matching etc. This means the expanded
1027 * characters will be re-tested for abbreviations. It's difficult to
1028 * know what historic practice in this case was, since abbreviations
1029 * were applied to :colon command lines, so entering abbreviations that
1030 * looped was tricky, although possible. In addition, obvious loops
1031 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
1032 * silently only implement and/or display the last abbreviation.)
1034 * This implementation doesn't recover well from such abbreviations.
1035 * The main input loop counts abbreviated characters, and, when it
1036 * reaches a limit, discards any abbreviated characters on the queue.
1037 * It's difficult to back up to the original position, as the replay
1038 * queue would have to be adjusted, and the line state when an initial
1039 * abbreviated character was received would have to be saved.
1042 if (term_push(sp
, &ch
, 1, 0, CH_ABBREVIATED
))
1044 if (term_push(sp
, qp
->output
, qp
->olen
, 0, CH_ABBREVIATED
))
1048 * Move the cursor to the start of the abbreviation,
1049 * adjust the length.
1054 /* Copy any insert characters back. */
1056 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
1057 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
, tp
->insert
);
1060 * We return the length of the abbreviated characters. This is so
1061 * the calling routine can replace the replay characters with the
1062 * abbreviation. This means that subsequent '.' commands will produce
1063 * the same text, regardless of intervening :[un]abbreviate commands.
1064 * This is historic practice.
1070 /* Offset to next column of stop size. */
1071 #define STOP_OFF(c, stop) (stop - (c) % stop)
1075 * When a line is resolved by <esc> or <cr>, review autoindent
1079 txt_ai_resolve(sp
, tp
)
1085 size_t cno
, len
, new, old
, scno
, spaces
, tab_after_sp
, tabs
;
1089 * If the line is empty, has an offset, or no autoindent
1090 * characters, we're done.
1092 if (!tp
->len
|| tp
->offset
|| !tp
->ai
)
1096 * The autoindent characters plus any leading <blank> characters
1097 * in the line are resolved into the minimum number of characters.
1098 * Historic practice.
1100 ts
= O_VAL(sp
, O_TABSTOP
);
1102 /* Figure out the last <blank> screen column. */
1103 for (p
= tp
->lb
, scno
= 0, len
= tp
->len
,
1104 spaces
= tab_after_sp
= 0; len
-- && isblank(*p
); ++p
)
1108 scno
+= STOP_OFF(scno
, ts
);
1115 * If there are no spaces, or no tabs after spaces and less than
1116 * ts spaces, it's already minimal.
1118 if (!spaces
|| !tab_after_sp
&& spaces
< ts
)
1121 /* Count up spaces/tabs needed to get to the target. */
1122 for (cno
= 0, tabs
= 0; cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1123 cno
+= STOP_OFF(cno
, ts
);
1124 spaces
= scno
- cno
;
1127 * Figure out how many characters we're dropping -- if we're not
1128 * dropping any, it's already minimal, we're done.
1131 new = spaces
+ tabs
;
1135 /* Shift the rest of the characters down, adjust the counts. */
1137 memmove(p
- del
, p
, tp
->len
- old
);
1141 /* Fill in space/tab characters. */
1142 for (p
= tp
->lb
; tabs
--;)
1150 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1151 * retrieve the line.
1154 txt_auto(sp
, ep
, lno
, aitp
, len
, tp
)
1165 if ((p
= t
= file_gline(sp
, ep
, lno
, &len
)) == NULL
)
1169 for (nlen
= 0; len
; ++p
) {
1172 /* If last character is a space, it counts. */
1179 /* No indentation. */
1186 /* Make sure the buffer's big enough. */
1187 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ nlen
);
1189 /* Copy the indentation into the new buffer. */
1190 memmove(tp
->lb
+ nlen
, tp
->lb
, tp
->len
);
1191 memmove(tp
->lb
, t
, nlen
);
1194 /* Return the additional length. */
1201 * Back up to the previously edited line.
1204 txt_backup(sp
, ep
, tiqh
, tp
, flags
)
1214 if (tp
->q
.cqe_prev
== (void *)tiqh
) {
1215 msgq(sp
, M_BERR
, "Already at the beginning of the insert");
1219 /* Update the old line on the screen. */
1220 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_DELETE
))
1223 /* Get a handle on the previous TEXT structure. */
1224 ntp
= tp
->q
.cqe_prev
;
1226 /* Make sure that we can get enough space. */
1227 if (LF_ISSET(TXT_APPENDEOL
) && ntp
->len
+ 1 > ntp
->lb_len
&&
1228 binc(sp
, &ntp
->lb
, &ntp
->lb_len
, ntp
->len
+ 1))
1232 * Release current TEXT; now committed to the swap, nothing
1235 CIRCLEQ_REMOVE(tiqh
, tp
, q
);
1241 /* Set bookkeeping information. */
1243 if (LF_ISSET(TXT_APPENDEOL
)) {
1244 tp
->lb
[col
] = CURSOR_CH
;
1255 * Handle an error during input processing.
1258 txt_err(sp
, ep
, tiqh
)
1267 * The problem with input processing is that the cursor is at an
1268 * indeterminate position since some input may have been lost due
1269 * to a malloc error. So, try to go back to the place from which
1270 * the cursor started, knowing that it may no longer be available.
1272 * We depend on at least one line number being set in the text
1275 for (lno
= tiqh
->cqh_first
->lno
;
1276 file_gline(sp
, ep
, lno
, &len
) == NULL
&& lno
> 0; --lno
);
1278 sp
->lno
= lno
== 0 ? 1 : lno
;
1281 /* Redraw the screen, just in case. */
1282 F_SET(sp
, S_REDRAW
);
1287 * Let the user insert any character value they want.
1290 * This is an extension. The pattern "^Vx[0-9a-fA-F]*" is a way
1291 * for the user to specify a character value which their keyboard
1292 * may not be able to enter.
1295 txt_hex(sp
, tp
, was_hex
, pushc
)
1307 * Null-terminate the string. Since nul isn't a legal hex value,
1308 * this should be okay, and lets us use a local routine, which
1309 * presumably understands the character set, to convert the value.
1311 savec
= tp
->lb
[sp
->cno
];
1312 tp
->lb
[sp
->cno
] = 0;
1314 /* Find the previous HEX_CH. */
1315 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1321 /* If not on this line, there's nothing to do. */
1322 if (off
== tp
->ai
|| off
== tp
->offset
)
1326 /* If no length, then it wasn't a hex value. */
1330 /* Get the value. */
1331 value
= strtol(wp
, NULL
, 16);
1332 if (value
== LONG_MIN
|| value
== LONG_MAX
|| value
> MAX_CHAR_T
) {
1333 nothex
: tp
->lb
[sp
->cno
] = savec
;
1339 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1342 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1345 tp
->lb
[sp
->cno
] = savec
;
1347 /* Move the cursor to the start of the hex value, adjust the length. */
1351 /* Copy any insert characters back. */
1353 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
1354 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
+ 1, tp
->insert
);
1361 * Txt_indent and txt_outdent are truly strange. ^T and ^D do movements
1362 * to the next or previous shiftwidth value, i.e. for a 1-based numbering,
1363 * with shiftwidth=3, ^T moves a cursor on the 7th, 8th or 9th column to
1364 * the 10th column, and ^D moves it back.
1367 * The ^T and ^D characters in historical vi only had special meaning when
1368 * they were the first characters typed after entering text input mode.
1369 * Since normal erase characters couldn't erase autoindent (in this case
1370 * ^T) characters, this meant that inserting text into previously existing
1371 * text was quite strange, ^T only worked if it was the first keystroke,
1372 * and then it could only be erased by using ^D. This implementation treats
1373 * ^T specially anywhere it occurs in the input, and permits the standard
1374 * erase characters to erase characters inserted using it.
1377 * Technically, txt_indent, txt_outdent should part of the screen interface,
1378 * as they require knowledge of the size of a space character on the screen.
1379 * (Not the size of tabs, because tabs are logically composed of spaces.)
1380 * They're left in the text code because they're complicated, not to mention
1381 * the gruesome awareness that if spaces aren't a single column on the screen
1382 * for any language, we're into some serious, ah, for lack of a better word,
1388 * Handle ^T indents.
1396 size_t cno
, off
, scno
, spaces
, tabs
;
1398 ts
= O_VAL(sp
, O_TABSTOP
);
1399 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1401 /* Get the current screen column. */
1402 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1403 if (tp
->lb
[off
] == '\t')
1404 scno
+= STOP_OFF(scno
, ts
);
1408 /* Count up spaces/tabs needed to get to the target. */
1409 for (cno
= scno
, scno
+= STOP_OFF(scno
, sw
), tabs
= 0;
1410 cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1411 cno
+= STOP_OFF(cno
, ts
);
1412 spaces
= scno
- cno
;
1414 /* Put space/tab characters in place of any overwrite characters. */
1415 for (; tp
->owrite
&& tabs
; --tp
->owrite
, --tabs
, ++tp
->ai
)
1416 tp
->lb
[sp
->cno
++] = '\t';
1417 for (; tp
->owrite
&& spaces
; --tp
->owrite
, --spaces
, ++tp
->ai
)
1418 tp
->lb
[sp
->cno
++] = ' ';
1420 if (!tabs
&& !spaces
)
1423 /* Make sure there's enough room. */
1424 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
+ tabs
);
1426 /* Move the insert characters out of the way. */
1428 memmove(tp
->lb
+ sp
->cno
+ spaces
+ tabs
,
1429 tp
->lb
+ sp
->cno
, tp
->insert
);
1431 /* Add new space/tab characters. */
1432 for (; tabs
--; ++tp
->len
, ++tp
->ai
)
1433 tp
->lb
[sp
->cno
++] = '\t';
1434 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1435 tp
->lb
[sp
->cno
++] = ' ';
1441 * Handle ^D outdents.
1450 size_t cno
, off
, scno
, spaces
;
1452 ts
= O_VAL(sp
, O_TABSTOP
);
1453 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1455 /* Get the current screen column. */
1456 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1457 if (tp
->lb
[off
] == '\t')
1458 scno
+= STOP_OFF(scno
, ts
);
1462 /* Get the previous shiftwidth column. */
1463 for (cno
= scno
; --scno
% sw
!= 0;);
1465 /* Decrement characters until less than or equal to that slot. */
1466 for (; cno
> scno
; --sp
->cno
, --tp
->ai
, ++tp
->owrite
)
1467 if (tp
->lb
[--off
] == '\t')
1468 cno
-= STOP_OFF(cno
, ts
);
1472 /* Spaces needed to get to the target. */
1473 spaces
= scno
- cno
;
1475 /* Maybe just a delete. */
1479 /* Make sure there's enough room. */
1480 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
);
1482 /* Use up any overwrite characters. */
1483 for (; tp
->owrite
&& spaces
; --spaces
, ++tp
->ai
, --tp
->owrite
)
1484 tp
->lb
[sp
->cno
++] = ' ';
1486 /* Maybe that was enough. */
1490 /* Move the insert characters out of the way. */
1492 memmove(tp
->lb
+ sp
->cno
+ spaces
,
1493 tp
->lb
+ sp
->cno
, tp
->insert
);
1495 /* Add new space characters. */
1496 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1497 tp
->lb
[sp
->cno
++] = ' ';
1503 * Resolve the input text chain into the file.
1506 txt_resolve(sp
, ep
, tiqh
)
1514 /* The first line replaces a current line. */
1515 tp
= tiqh
->cqh_first
;
1516 if (file_sline(sp
, ep
, tp
->lno
, tp
->lb
, tp
->len
))
1519 /* All subsequent lines are appended into the file. */
1520 for (lno
= tp
->lno
; (tp
= tp
->q
.cqe_next
) != (void *)&sp
->tiq
; ++lno
)
1521 if (file_aline(sp
, ep
, 0, lno
, tp
->lb
, tp
->len
))
1528 * Show a character match.
1531 * Historic vi tried to display matches even in the :colon command line.
1535 txt_showmatch(sp
, ep
)
1539 struct timeval second
;
1543 int cnt
, endc
, startc
;
1546 * Do a refresh first, in case the v_ntext() code hasn't done
1547 * one in awhile, so the user can see what we're complaining
1550 if (sp
->s_refresh(sp
, ep
))
1553 * We don't display the match if it's not on the screen. Find
1554 * out what the first character on the screen is.
1556 if (sp
->s_position(sp
, ep
, &m
, 0, P_TOP
))
1559 /* Initialize the getc() interface. */
1560 cs
.cs_lno
= sp
->lno
;
1561 cs
.cs_cno
= sp
->cno
- 1;
1562 if (cs_init(sp
, ep
, &cs
))
1564 startc
= (endc
= cs
.cs_ch
) == ')' ? '(' : '{';
1566 /* Search for the match. */
1568 if (cs_prev(sp
, ep
, &cs
))
1570 if (cs
.cs_lno
< m
.lno
||
1571 cs
.cs_lno
== m
.lno
&& cs
.cs_cno
< m
.cno
)
1573 if (cs
.cs_flags
!= 0) {
1574 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
) {
1575 (void)sp
->s_bell(sp
);
1580 if (cs
.cs_ch
== endc
)
1582 else if (cs
.cs_ch
== startc
&& --cnt
== 0)
1586 /* Move to the match. */
1589 sp
->lno
= cs
.cs_lno
;
1590 sp
->cno
= cs
.cs_cno
;
1591 (void)sp
->s_refresh(sp
, ep
);
1594 * Sleep(3) is eight system calls. Do it fast -- besides,
1595 * I don't want to wait an entire second.
1598 second
.tv_sec
= O_VAL(sp
, O_MATCHTIME
) / 10;
1599 second
.tv_usec
= (O_VAL(sp
, O_MATCHTIME
) % 10) * 100000L;
1600 (void)select(0, &zero
, &zero
, &zero
, &second
);
1602 /* Return to the current location. */
1605 (void)sp
->s_refresh(sp
, ep
);
1610 * Handle margin wrap.
1613 * Historic vi belled the user each time a character was entered after
1614 * crossing the margin until a space was entered which could be used to
1615 * break the line. I don't, it tends to wake the cats.
1618 txt_margin(sp
, tp
, didbreak
, pushc
)
1625 size_t len
, off
, tlen
;
1628 /* Find the closest previous blank. */
1629 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1635 /* If it's the beginning of the line, there's nothing to do. */
1636 if (off
== tp
->ai
|| off
== tp
->offset
) {
1643 * Historic practice is to delete any trailing whitespace
1644 * from the previous line.
1646 for (tlen
= len
;; --p
, --off
) {
1650 if (off
== tp
->ai
|| off
== tp
->offset
)
1655 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1657 if (len
&& term_push(sp
, wp
, len
, 0, CH_NOMAP
| CH_QUOTED
))
1660 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
))