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.64 1993/12/16 14:19:27 bostic Exp $ (Berkeley) $Date: 1993/12/16 14:19:27 $";
12 #include <sys/types.h>
25 static int txt_abbrev
__P((SCR
*, TEXT
*, int *, ARG_CHAR_T
));
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
; /* Abbreviation count. */
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 /* Set return value. */
127 * Get one TEXT structure with some initial buffer space, reusing
128 * the last one if it's big enough. (All TEXT bookkeeping fields
129 * default to 0 -- text_init() handles this.) If changing a line,
130 * copy it into the TEXT buffer.
132 if (tiqh
->cqh_first
!= (void *)tiqh
) {
133 tp
= tiqh
->cqh_first
;
134 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< len
+ 32) {
138 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
141 memmove(tp
->lb
, lp
, len
);
145 newtp
: if ((tp
= text_init(sp
, lp
, len
, len
+ 32)) == NULL
)
147 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
150 /* Set the starting line number. */
154 * Set the insert and overwrite counts. If overwriting characters,
155 * do insertion afterward. If not overwriting characters, assume
156 * doing insertion. If change is to a mark, emphasize it with an
160 if (LF_ISSET(TXT_OVERWRITE
)) {
161 tp
->owrite
= tm
->cno
- sp
->cno
;
162 tp
->insert
= len
- tm
->cno
;
164 tp
->insert
= len
- sp
->cno
;
166 if (LF_ISSET(TXT_EMARK
))
167 tp
->lb
[tm
->cno
- 1] = END_CH
;
171 * Many of the special cases in this routine are to handle autoindent
172 * support. Somebody decided that it would be a good idea if "^^D"
173 * and "0^D" deleted all of the autoindented characters. In an editor
174 * that takes single character input from the user, this wasn't a very
175 * good idea. Note also that "^^D" resets the next lines' autoindent,
178 * We assume that autoindent only happens on empty lines, so insert
179 * and overwrite will be zero. If doing autoindent, figure out how
180 * much indentation we need and fill it in. Update input column and
181 * screen cursor as necessary.
183 if (LF_ISSET(TXT_AUTOINDENT
) && ai_line
!= OOBLNO
) {
184 if (txt_auto(sp
, ep
, ai_line
, NULL
, 0, tp
))
189 * The cc and S commands have a special feature -- leading
190 * <blank> characters are handled as autoindent characters.
193 if (LF_ISSET(TXT_AICHARS
)) {
197 tp
->offset
= sp
->cno
;
200 /* If getting a command buffer from the user, there may be a prompt. */
201 if (LF_ISSET(TXT_PROMPT
)) {
202 tp
->lb
[sp
->cno
++] = prompt
;
208 * If appending after the end-of-line, add a space into the buffer
209 * and move the cursor right. This space is inserted, i.e. pushed
210 * along, and then deleted when the line is resolved. Assumes that
211 * the cursor is already positioned at the end of the line. This
212 * avoids the nastiness of having the cursor reside on a magical
213 * column, i.e. a column that doesn't really exist. The only down
214 * side is that we may wrap lines or scroll the screen before it's
215 * strictly necessary. Not a big deal.
217 if (LF_ISSET(TXT_APPENDEOL
)) {
218 tp
->lb
[sp
->cno
] = CURSOR_CH
;
224 * Historic practice is that the wrapmargin value was a distance
225 * from the RIGHT-HAND column, not the left. It's more useful to
226 * us as a distance from the left-hand column.
229 * Setting margin causes a significant performance hit. Normally
230 * we don't update the screen if there are keys waiting, but we
231 * have to if margin is set, otherwise the screen routines don't
232 * know where the cursor is.
234 if (!LF_ISSET(TXT_WRAPMARGIN
))
236 else if ((margin
= O_VAL(sp
, O_WRAPMARGIN
)) != 0)
237 margin
= sp
->cols
- margin
;
239 /* Initialize abbreviations checks. */
240 if (F_ISSET(sp
, S_ABBREV
) && LF_ISSET(TXT_MAPINPUT
)) {
247 * Set up the dot command. Dot commands are done by saving the
248 * actual characters and replaying the input. We have to push
249 * the characters onto the key stack and then handle them normally,
250 * otherwise things like wrapmargin will fail.
253 * It would be nice if we could swallow backspaces and such, but
254 * it's not all that easy to do. Another possibility would be to
255 * recognize full line insertions, which could be performed quickly,
260 if (replay
= LF_ISSET(TXT_REPLAY
)) {
263 * Historically, it wasn't an error to replay non-existent
264 * input. This test is necessary, we get here by the user
265 * doing an input command followed by a nul.
268 * Historically, vi did not remap or reabbreviate replayed
269 * input. It did, however, beep at you if you changed an
270 * abbreviation and then replayed the input. We're not that
273 if (VIP(sp
)->rep
== NULL
)
275 if (term_push(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_cnt
, 0, CH_NOMAP
))
283 iflags
= LF_ISSET(TXT_MAPCOMMAND
| TXT_MAPINPUT
);
284 for (gp
= sp
->gp
, showmatch
= 0,
285 carat_st
= C_NOTSET
, hex
= H_NOTSET
, quoted
= Q_NOTSET
;;) {
287 * Reset the line and update the screen. (The txt_showmatch()
288 * code refreshes the screen for us.) Don't refresh unless
289 * we're about to wait on a character or we need to know where
290 * the cursor really is.
292 if (showmatch
|| margin
|| !KEYS_WAITING(sp
)) {
293 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
297 txt_showmatch(sp
, ep
);
298 } else if (sp
->s_refresh(sp
, ep
))
302 /* Get the next character. */
303 next_ch
: if (term_key(sp
, &ikey
, iflags
) != INP_OK
)
307 /* Abbreviation check. See comment in txt_abbrev(). */
308 #define MAX_ABBREVIATION_EXPANSION 256
309 if (ikey
.flags
& CH_ABBREVIATED
) {
310 if (++ab_cnt
> MAX_ABBREVIATION_EXPANSION
) {
312 "Abbreviation exceeded maximum number of characters");
321 * Historic feature. If the first character of the input is
322 * a nul, replay the previous input. This isn't documented
323 * anywhere, and is a great test of vi clones.
325 if (ch
== '\0' && testnr
) {
332 * Check to see if the character fits into the input (and
333 * replay, if necessary) buffers. It isn't necessary to
334 * have tp->len bytes, since it doesn't consider overwrite
335 * characters, but not worth fixing.
337 if (LF_ISSET(TXT_RECORD
)) {
338 TBINC(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_len
, rcol
+ 1);
339 VIP(sp
)->rep
[rcol
++] = ch
;
341 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
344 * If the character was quoted, replace the last character
345 * (the literal mark) with the new character. If quoted
346 * by someone else, simply insert the character.
349 * Extension -- if the quoted character is HEX_CH, enter hex
350 * mode. If the user enters "<HEX_CH>[isxdigit()]*" we will
351 * try to use the value as a character. Anything else resets
354 if (ikey
.flags
& CH_QUOTED
)
356 if (quoted
== Q_THISCHAR
) {
366 switch (ikey
.value
) {
368 case K_NL
: /* New line. */
369 #define LINE_RESOLVE { \
371 * Handle abbreviations. If there was one, \
372 * discard the replay characters. \
374 if (abb == A_NOTSPACE && !replay) { \
375 if (txt_abbrev(sp, tp, &tmp, ch)) \
378 if (LF_ISSET(TXT_RECORD)) \
383 if (abb != A_NOTSET) \
385 /* Handle hex numbers. */ \
386 if (hex == H_INHEX) { \
387 if (txt_hex(sp, tp, &tmp, ch)) \
395 * The 'R' command returns any overwriteable \
396 * characters in the first line to the original \
399 if (LF_ISSET(TXT_REPLACE) && tp->owrite && \
400 tp == tiqh->cqh_first) { \
401 memmove(tp->lb + sp->cno, \
402 lp + sp->cno, tp->owrite); \
403 tp->insert += tp->owrite; \
406 /* Delete any appended cursor. */ \
407 if (LF_ISSET(TXT_APPENDEOL)) { \
414 /* CR returns from the vi command line. */
415 if (LF_ISSET(TXT_CR
)) {
416 if (F_ISSET(sp
, S_SCRIPT
))
418 "\r", 1, 0, CH_NOMAP
);
423 * Historic practice was to delete any <blank>
424 * characters following the inserted newline.
425 * This affects the 'R', 'c', and 's' commands.
427 for (p
= tp
->lb
+ sp
->cno
+ tp
->owrite
;
428 tp
->insert
&& isblank(*p
);
429 ++p
, ++tp
->owrite
, --tp
->insert
);
432 * Move any remaining insert characters into
433 * a new TEXT structure.
435 if ((ntp
= text_init(sp
,
436 tp
->lb
+ sp
->cno
+ tp
->owrite
,
437 tp
->insert
, tp
->insert
+ 32)) == NULL
)
439 CIRCLEQ_INSERT_TAIL(tiqh
, ntp
, q
);
441 /* Set bookkeeping for the new line. */
442 ntp
->lno
= tp
->lno
+ 1;
443 ntp
->insert
= tp
->insert
;
446 * Note if the user inserted any characters on this
447 * line. Done before calling txt_ai_resolve() because
448 * it changes the value of sp->cno without making the
449 * corresponding changes to tp->ai.
451 tmp
= sp
->cno
<= tp
->ai
;
454 * Resolve autoindented characters for the old line.
455 * Reset the autoindent line value. 0^D keeps the ai
456 * line from changing, ^D changes the level, even if
457 * there are no characters in the old line. Note,
458 * if using the current tp structure, use the cursor
459 * as the length, the user may have erased autoindent
462 if (LF_ISSET(TXT_AUTOINDENT
)) {
463 txt_ai_resolve(sp
, tp
);
465 if (carat_st
== C_NOCHANGE
) {
467 OOBLNO
, &ait
, ait
.ai
, ntp
))
469 FREE_SPACE(sp
, ait
.lb
, ait
.lb_len
);
472 OOBLNO
, tp
, sp
->cno
, ntp
))
478 * If the user hasn't entered any characters, delete
479 * any autoindent characters.
482 * Historic vi didn't get the insert test right, if
483 * there were characters after the cursor, entering
484 * a <cr> left the autoindent characters on the line.
489 /* Reset bookkeeping for the old line. */
491 tp
->ai
= tp
->insert
= tp
->owrite
= 0;
493 /* New cursor position. */
496 /* New lines are TXT_APPENDEOL if nothing to insert. */
497 if (ntp
->insert
== 0) {
498 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
499 LF_SET(TXT_APPENDEOL
);
500 ntp
->lb
[sp
->cno
] = CURSOR_CH
;
505 /* Update the old line. */
506 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
509 /* Swap old and new TEXT's. */
512 /* Reset the cursor. */
515 /* Update the new line. */
516 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_INSERT
))
519 /* Set the renumber bit. */
520 F_SET(sp
, S_RENUMBER
);
522 /* Refresh if nothing waiting. */
523 if ((margin
|| !KEYS_WAITING(sp
)) &&
524 sp
->s_refresh(sp
, ep
))
527 case K_ESCAPE
: /* Escape. */
528 if (!LF_ISSET(TXT_ESCAPE
))
534 * If there aren't any trailing characters in the line
535 * and the user hasn't entered any characters, delete
536 * the autoindent characters.
538 if (!tp
->insert
&& sp
->cno
<= tp
->ai
) {
539 tp
->len
= tp
->owrite
= 0;
541 } else if (LF_ISSET(TXT_AUTOINDENT
))
542 txt_ai_resolve(sp
, tp
);
544 /* If there are insert characters, copy them down. */
545 k_escape
: if (tp
->insert
&& tp
->owrite
)
546 memmove(tp
->lb
+ sp
->cno
,
547 tp
->lb
+ sp
->cno
+ tp
->owrite
, tp
->insert
);
548 tp
->len
-= tp
->owrite
;
551 * Delete any lines that were inserted into the text
552 * structure and then erased.
554 while (tp
->q
.cqe_next
!= (void *)tiqh
) {
555 ntp
= tp
->q
.cqe_next
;
556 CIRCLEQ_REMOVE(tiqh
, ntp
, q
);
561 * If not resolving the lines into the file, end
565 * This is wrong, should pass back a length.
567 if (LF_ISSET(TXT_RESOLVE
)) {
568 if (txt_resolve(sp
, ep
, tiqh
))
571 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
572 tp
->lb
[tp
->len
] = '\0';
576 * Set the return cursor position to rest on the last
577 * inserted character.
581 rp
->cno
= sp
->cno
? sp
->cno
- 1 : 0;
582 if (sp
->s_change(sp
, ep
, rp
->lno
, LINE_RESET
))
586 case K_CARAT
: /* Delete autoindent chars. */
587 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
588 carat_st
= C_CARATSET
;
590 case K_ZERO
: /* Delete autoindent chars. */
591 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
592 carat_st
= C_ZEROSET
;
594 case K_VEOF
: /* Delete autoindent char. */
596 * If not doing autoindent, in the first column, no
597 * characters to erase, or already inserted non-ai
598 * characters, it's a literal. The last test is done
599 * in the switch, as the CARAT forms are N + 1, not N.
601 if (!LF_ISSET(TXT_AUTOINDENT
) ||
602 sp
->cno
== 0 || tp
->ai
== 0)
605 case C_CARATSET
: /* ^^D */
606 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
609 /* Save the ai string for later. */
612 TBINC(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
613 memmove(ait
.lb
, tp
->lb
, tp
->ai
);
614 ait
.ai
= ait
.len
= tp
->ai
;
616 carat_st
= C_NOCHANGE
;
618 case C_ZEROSET
: /* 0^D */
619 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
622 leftmargin
: tp
->lb
[sp
->cno
- 1] = ' ';
623 tp
->owrite
+= sp
->cno
- tp
->offset
;
625 sp
->cno
= tp
->offset
;
627 case C_NOTSET
: /* ^D */
628 if (sp
->cno
> tp
->ai
+ tp
->offset
)
630 (void)txt_outdent(sp
, tp
);
636 case K_VERASE
: /* Erase the last character. */
638 * If can erase over the prompt, return. Len is 0
639 * if backspaced over the prompt, 1 if only CR entered.
641 if (LF_ISSET(TXT_BS
) && sp
->cno
<= tp
->offset
) {
647 * If at the beginning of the line, try and drop back
648 * to a previously inserted line.
651 if ((ntp
= txt_backup(sp
,
652 ep
, tiqh
, tp
, flags
)) == NULL
)
658 /* If nothing to erase, bell the user. */
659 if (sp
->cno
<= tp
->offset
) {
661 "No more characters to erase.");
665 /* Drop back one character. */
669 * Increment overwrite, decrement ai if deleted.
672 * Historic vi did not permit users to use erase
673 * characters to delete autoindent characters.
676 if (sp
->cno
< tp
->ai
)
679 case K_VWERASE
: /* Skip back one word. */
681 * If at the beginning of the line, try and drop back
682 * to a previously inserted line.
685 if ((ntp
= txt_backup(sp
,
686 ep
, tiqh
, tp
, flags
)) == NULL
)
692 * If at offset, nothing to erase so bell the user.
694 if (sp
->cno
<= tp
->offset
) {
696 "No more characters to erase.");
701 * First werase goes back to any autoindent
702 * and second werase goes back to the offset.
705 * Historic vi did not permit users to use erase
706 * characters to delete autoindent characters.
708 if (tp
->ai
&& sp
->cno
> tp
->ai
)
715 /* Skip over trailing space characters. */
716 while (sp
->cno
> max
&& isblank(tp
->lb
[sp
->cno
- 1])) {
723 * There are three types of word erase found on UNIX
724 * systems. They can be identified by how the string
725 * /a/b/c is treated -- as 1, 3, or 6 words. Historic
726 * vi had two classes of characters, and strings were
727 * delimited by them and <blank>'s, so, 6 words. The
728 * historic tty interface used <blank>'s to delimit
729 * strings, so, 1 word. The algorithm offered in the
730 * 4.4BSD tty interface (as stty altwerase) treats it
731 * as 3 words -- there are two classes of characters,
732 * and strings are delimited by them and <blank>'s.
733 * The difference is that the type of the first erased
734 * character erased is ignored, which is exactly right
735 * when erasing pathname components. Here, the options
736 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD
737 * tty interface and the historic tty driver behavior,
738 * respectively, and the default is the same as the
739 * historic vi behavior.
741 if (LF_ISSET(TXT_TTYWERASE
))
742 while (sp
->cno
> max
) {
745 if (isblank(tp
->lb
[sp
->cno
- 1]))
749 if (LF_ISSET(TXT_ALTWERASE
)) {
752 if (isblank(tp
->lb
[sp
->cno
- 1]))
756 tmp
= inword(tp
->lb
[sp
->cno
- 1]);
757 while (sp
->cno
> max
) {
760 if (tmp
!= inword(tp
->lb
[sp
->cno
- 1])
761 || isblank(tp
->lb
[sp
->cno
- 1]))
766 case K_VKILL
: /* Restart this line. */
768 * If at the beginning of the line, try and drop back
769 * to a previously inserted line.
772 if ((ntp
= txt_backup(sp
,
773 ep
, tiqh
, tp
, flags
)) == NULL
)
778 /* If at offset, nothing to erase so bell the user. */
779 if (sp
->cno
<= tp
->offset
) {
781 "No more characters to erase.");
786 * First kill goes back to any autoindent
787 * and second kill goes back to the offset.
790 * Historic vi did not permit users to use erase
791 * characters to delete autoindent characters.
793 if (tp
->ai
&& sp
->cno
> tp
->ai
)
799 tp
->owrite
+= sp
->cno
- max
;
802 case K_CNTRLT
: /* Add autoindent char. */
803 if (!LF_ISSET(TXT_CNTRLT
))
805 if (txt_indent(sp
, tp
))
809 (void)sp
->s_suspend(sp
);
811 #ifdef HISTORIC_PRACTICE_IS_TO_INSERT_NOT_REPAINT
813 F_SET(sp
, S_REFRESH
);
818 showmatch
= LF_ISSET(TXT_SHOWMATCH
);
820 case K_VLNEXT
: /* Quote the next character. */
821 /* If in hex mode, see if we've entered a hex value. */
822 if (hex
== H_INHEX
) {
823 if (txt_hex(sp
, tp
, &tmp
, ch
))
833 default: /* Insert the character. */
835 * If entering a space character after a word, check
836 * for abbreviations. If there was one, discard the
839 if (isblank(ch
) && abb
== A_NOTSPACE
&& !replay
) {
840 if (txt_abbrev(sp
, tp
, &tmp
, ch
))
843 if (LF_ISSET(TXT_RECORD
))
848 /* If in hex mode, see if we've entered a hex value. */
849 if (hex
== H_INHEX
&& !isxdigit(ch
)) {
850 if (txt_hex(sp
, tp
, &tmp
, ch
))
857 /* Check to see if we've crossed the margin. */
859 if (sp
->s_column(sp
, ep
, &col
))
862 if (txt_margin(sp
, tp
, &tmp
, ch
))
869 abb
= isblank(ch
) ? A_SPACE
: A_NOTSPACE
;
871 if (tp
->owrite
) /* Overwrite a character. */
873 else if (tp
->insert
) { /* Insert a character. */
876 tp
->lb
[sp
->cno
+ 1] = tp
->lb
[sp
->cno
];
878 memmove(tp
->lb
+ sp
->cno
+ 1,
879 tp
->lb
+ sp
->cno
, tp
->insert
);
882 tp
->lb
[sp
->cno
++] = ch
;
885 * If we've reached the end of the buffer, then we
886 * need to switch into insert mode. This happens
887 * when there's a change to a mark and the user puts
888 * in more characters than the length of the motion.
890 ebuf_chk
: if (sp
->cno
>= tp
->len
) {
891 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
892 LF_SET(TXT_APPENDEOL
);
893 tp
->lb
[sp
->cno
] = CURSOR_CH
;
898 if (hex
== H_NEXTCHAR
)
900 if (quoted
== Q_NEXTCHAR
)
904 #if defined(DEBUG) && 1
905 if (sp
->cno
+ tp
->insert
+ tp
->owrite
!= tp
->len
)
907 "len %u != cno: %u ai: %u insert %u overwrite %u",
908 tp
->len
, sp
->cno
, tp
->ai
, tp
->insert
, tp
->owrite
);
909 tp
->len
= sp
->cno
+ tp
->insert
+ tp
->owrite
;
913 /* Clear input flag. */
914 ret
: F_CLR(sp
, S_INPUT
);
916 if (LF_ISSET(TXT_RECORD
))
917 VIP(sp
)->rep_cnt
= rcol
;
922 txt_err(sp
, ep
, tiqh
);
928 * Handle abbreviations.
931 txt_abbrev(sp
, tp
, didsubp
, pushc
)
942 /* Find the beginning of this "word". */
943 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
949 if (off
== tp
->ai
|| off
== tp
->offset
)
953 /* Check for any abbreviations. */
954 if ((qp
= seq_find(sp
, NULL
, p
, len
, SEQ_ABBREV
, NULL
)) == NULL
) {
960 * Push the abbreviation onto the tty stack. Historically, characters
961 * resulting from an abbreviation expansion were themselves subject to
962 * map expansions, O_SHOWMATCH matching etc. This means the expanded
963 * characters will be re-tested for abbreviations. It's difficult to
964 * know what historic practice in this case was, since abbreviations
965 * were applied to :colon command lines, so entering abbreviations that
966 * looped was tricky, although possible. In addition, obvious loops
967 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
968 * silently only implement and/or display the last abbreviation.)
970 * This implementation doesn't recover well from such abbreviations.
971 * The main input loop counts abbreviated characters, and, when it
972 * reaches a limit, discards any abbreviated characters on the queue.
973 * It's difficult to back up to the original position, as the replay
974 * queue would have to be adjusted, and the line state when an initial
975 * abbreviated character was received would have to be saved.
978 if (term_push(sp
, &ch
, 1, 0, CH_ABBREVIATED
))
980 if (term_push(sp
, qp
->output
, qp
->olen
, 0, CH_ABBREVIATED
))
984 * Move the cursor to the start of the abbreviation,
990 /* Copy any insert characters back. */
992 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
993 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
, tp
->insert
);
996 * We return the length of the abbreviated characters. This is so
997 * the calling routine can replace the replay characters with the
998 * abbreviation. This means that subsequent '.' commands will produce
999 * the same text, regardless of intervening :[un]abbreviate commands.
1000 * This is historic practice.
1006 /* Offset to next column of stop size. */
1007 #define STOP_OFF(c, stop) (stop - (c) % stop)
1011 * When a line is resolved by <esc> or <cr>, review autoindent
1015 txt_ai_resolve(sp
, tp
)
1021 size_t cno
, len
, new, old
, scno
, spaces
, tab_after_sp
, tabs
;
1025 * If the line is empty, has an offset, or no autoindent
1026 * characters, we're done.
1028 if (!tp
->len
|| tp
->offset
|| !tp
->ai
)
1032 * The autoindent characters plus any leading <blank> characters
1033 * in the line are resolved into the minimum number of characters.
1034 * Historic practice.
1036 ts
= O_VAL(sp
, O_TABSTOP
);
1038 /* Figure out the last <blank> screen column. */
1039 for (p
= tp
->lb
, scno
= 0, len
= tp
->len
,
1040 spaces
= tab_after_sp
= 0; len
-- && isblank(*p
); ++p
)
1044 scno
+= STOP_OFF(scno
, ts
);
1051 * If there are no spaces, or no tabs after spaces and less than
1052 * ts spaces, it's already minimal.
1054 if (!spaces
|| !tab_after_sp
&& spaces
< ts
)
1057 /* Count up spaces/tabs needed to get to the target. */
1058 for (cno
= 0, tabs
= 0; cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1059 cno
+= STOP_OFF(cno
, ts
);
1060 spaces
= scno
- cno
;
1063 * Figure out how many characters we're dropping -- if we're not
1064 * dropping any, it's already minimal, we're done.
1067 new = spaces
+ tabs
;
1071 /* Shift the rest of the characters down, adjust the counts. */
1073 memmove(p
- del
, p
, tp
->len
- old
);
1077 /* Fill in space/tab characters. */
1078 for (p
= tp
->lb
; tabs
--;)
1086 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1087 * retrieve the line.
1090 txt_auto(sp
, ep
, lno
, aitp
, len
, tp
)
1101 if ((p
= t
= file_gline(sp
, ep
, lno
, &len
)) == NULL
)
1105 for (nlen
= 0; len
; ++p
) {
1108 /* If last character is a space, it counts. */
1115 /* No indentation. */
1122 /* Make sure the buffer's big enough. */
1123 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ nlen
);
1125 /* Copy the indentation into the new buffer. */
1126 memmove(tp
->lb
+ nlen
, tp
->lb
, tp
->len
);
1127 memmove(tp
->lb
, t
, nlen
);
1130 /* Return the additional length. */
1137 * Back up to the previously edited line.
1140 txt_backup(sp
, ep
, tiqh
, tp
, flags
)
1150 if (tp
->q
.cqe_prev
== (void *)tiqh
) {
1151 msgq(sp
, M_BERR
, "Already at the beginning of the insert");
1155 /* Update the old line on the screen. */
1156 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_DELETE
))
1159 /* Get a handle on the previous TEXT structure. */
1160 ntp
= tp
->q
.cqe_prev
;
1162 /* Make sure that we can get enough space. */
1163 if (LF_ISSET(TXT_APPENDEOL
) && ntp
->len
+ 1 > ntp
->lb_len
&&
1164 binc(sp
, &ntp
->lb
, &ntp
->lb_len
, ntp
->len
+ 1))
1168 * Release current TEXT; now committed to the swap, nothing
1171 CIRCLEQ_REMOVE(tiqh
, tp
, q
);
1177 /* Set bookkeeping information. */
1179 if (LF_ISSET(TXT_APPENDEOL
)) {
1180 tp
->lb
[col
] = CURSOR_CH
;
1191 * Handle an error during input processing.
1194 txt_err(sp
, ep
, tiqh
)
1203 * The problem with input processing is that the cursor is at an
1204 * indeterminate position since some input may have been lost due
1205 * to a malloc error. So, try to go back to the place from which
1206 * the cursor started, knowing that it may no longer be available.
1208 * We depend on at least one line number being set in the text
1211 for (lno
= tiqh
->cqh_first
->lno
;
1212 file_gline(sp
, ep
, lno
, &len
) == NULL
&& lno
> 0; --lno
);
1214 sp
->lno
= lno
== 0 ? 1 : lno
;
1217 /* Redraw the screen, just in case. */
1218 F_SET(sp
, S_REDRAW
);
1223 * Let the user insert any character value they want.
1226 * This is an extension. The pattern "^Vx[0-9a-fA-F]*" is a way
1227 * for the user to specify a character value which their keyboard
1228 * may not be able to enter.
1231 txt_hex(sp
, tp
, was_hex
, pushc
)
1243 * Null-terminate the string. Since nul isn't a legal hex value,
1244 * this should be okay, and lets us use a local routine, which
1245 * presumably understands the character set, to convert the value.
1247 savec
= tp
->lb
[sp
->cno
];
1248 tp
->lb
[sp
->cno
] = 0;
1250 /* Find the previous HEX_CH. */
1251 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1257 /* If not on this line, there's nothing to do. */
1258 if (off
== tp
->ai
|| off
== tp
->offset
)
1262 /* If no length, then it wasn't a hex value. */
1266 /* Get the value. */
1267 value
= strtol(wp
, NULL
, 16);
1268 if (value
== LONG_MIN
|| value
== LONG_MAX
|| value
> MAX_CHAR_T
) {
1269 nothex
: tp
->lb
[sp
->cno
] = savec
;
1275 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1278 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1281 tp
->lb
[sp
->cno
] = savec
;
1283 /* Move the cursor to the start of the hex value, adjust the length. */
1287 /* Copy any insert characters back. */
1289 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
1290 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
+ 1, tp
->insert
);
1297 * Txt_indent and txt_outdent are truly strange. ^T and ^D do movements
1298 * to the next or previous shiftwidth value, i.e. for a 1-based numbering,
1299 * with shiftwidth=3, ^T moves a cursor on the 7th, 8th or 9th column to
1300 * the 10th column, and ^D moves it back.
1303 * The ^T and ^D characters in historical vi only had special meaning when
1304 * they were the first characters typed after entering text input mode.
1305 * Since normal erase characters couldn't erase autoindent (in this case
1306 * ^T) characters, this meant that inserting text into previously existing
1307 * text was quite strange, ^T only worked if it was the first keystroke,
1308 * and then it could only be erased by using ^D. This implementation treats
1309 * ^T specially anywhere it occurs in the input, and permits the standard
1310 * erase characters to erase characters inserted using it.
1313 * Technically, txt_indent, txt_outdent should part of the screen interface,
1314 * as they require knowledge of the size of a space character on the screen.
1315 * (Not the size of tabs, because tabs are logically composed of spaces.)
1316 * They're left in the text code because they're complicated, not to mention
1317 * the gruesome awareness that if spaces aren't a single column on the screen
1318 * for any language, we're into some serious, ah, for lack of a better word,
1324 * Handle ^T indents.
1332 size_t cno
, off
, scno
, spaces
, tabs
;
1334 ts
= O_VAL(sp
, O_TABSTOP
);
1335 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1337 /* Get the current screen column. */
1338 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1339 if (tp
->lb
[off
] == '\t')
1340 scno
+= STOP_OFF(scno
, ts
);
1344 /* Count up spaces/tabs needed to get to the target. */
1345 for (cno
= scno
, scno
+= STOP_OFF(scno
, sw
), tabs
= 0;
1346 cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1347 cno
+= STOP_OFF(cno
, ts
);
1348 spaces
= scno
- cno
;
1350 /* Put space/tab characters in place of any overwrite characters. */
1351 for (; tp
->owrite
&& tabs
; --tp
->owrite
, --tabs
, ++tp
->ai
)
1352 tp
->lb
[sp
->cno
++] = '\t';
1353 for (; tp
->owrite
&& spaces
; --tp
->owrite
, --spaces
, ++tp
->ai
)
1354 tp
->lb
[sp
->cno
++] = ' ';
1356 if (!tabs
&& !spaces
)
1359 /* Make sure there's enough room. */
1360 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
+ tabs
);
1362 /* Move the insert characters out of the way. */
1364 memmove(tp
->lb
+ sp
->cno
+ spaces
+ tabs
,
1365 tp
->lb
+ sp
->cno
, tp
->insert
);
1367 /* Add new space/tab characters. */
1368 for (; tabs
--; ++tp
->len
, ++tp
->ai
)
1369 tp
->lb
[sp
->cno
++] = '\t';
1370 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1371 tp
->lb
[sp
->cno
++] = ' ';
1377 * Handle ^D outdents.
1386 size_t cno
, off
, scno
, spaces
;
1388 ts
= O_VAL(sp
, O_TABSTOP
);
1389 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1391 /* Get the current screen column. */
1392 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1393 if (tp
->lb
[off
] == '\t')
1394 scno
+= STOP_OFF(scno
, ts
);
1398 /* Get the previous shiftwidth column. */
1399 for (cno
= scno
; --scno
% sw
!= 0;);
1401 /* Decrement characters until less than or equal to that slot. */
1402 for (; cno
> scno
; --sp
->cno
, --tp
->ai
, ++tp
->owrite
)
1403 if (tp
->lb
[--off
] == '\t')
1404 cno
-= STOP_OFF(cno
, ts
);
1408 /* Spaces needed to get to the target. */
1409 spaces
= scno
- cno
;
1411 /* Maybe just a delete. */
1415 /* Make sure there's enough room. */
1416 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
);
1418 /* Use up any overwrite characters. */
1419 for (; tp
->owrite
&& spaces
; --spaces
, ++tp
->ai
, --tp
->owrite
)
1420 tp
->lb
[sp
->cno
++] = ' ';
1422 /* Maybe that was enough. */
1426 /* Move the insert characters out of the way. */
1428 memmove(tp
->lb
+ sp
->cno
+ spaces
,
1429 tp
->lb
+ sp
->cno
, tp
->insert
);
1431 /* Add new space characters. */
1432 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1433 tp
->lb
[sp
->cno
++] = ' ';
1439 * Resolve the input text chain into the file.
1442 txt_resolve(sp
, ep
, tiqh
)
1450 /* The first line replaces a current line. */
1451 tp
= tiqh
->cqh_first
;
1452 if (file_sline(sp
, ep
, tp
->lno
, tp
->lb
, tp
->len
))
1455 /* All subsequent lines are appended into the file. */
1456 for (lno
= tp
->lno
; (tp
= tp
->q
.cqe_next
) != (void *)&sp
->tiq
; ++lno
)
1457 if (file_aline(sp
, ep
, 0, lno
, tp
->lb
, tp
->len
))
1464 * Show a character match.
1467 * Historic vi tried to display matches even in the :colon command line.
1471 txt_showmatch(sp
, ep
)
1475 struct timeval second
;
1479 int cnt
, endc
, startc
;
1482 * Do a refresh first, in case the v_ntext() code hasn't done
1483 * one in awhile, so the user can see what we're complaining
1486 if (sp
->s_refresh(sp
, ep
))
1489 * We don't display the match if it's not on the screen. Find
1490 * out what the first character on the screen is.
1492 if (sp
->s_position(sp
, ep
, &m
, 0, P_TOP
))
1495 /* Initialize the getc() interface. */
1496 cs
.cs_lno
= sp
->lno
;
1497 cs
.cs_cno
= sp
->cno
- 1;
1498 if (cs_init(sp
, ep
, &cs
))
1500 startc
= (endc
= cs
.cs_ch
) == ')' ? '(' : '{';
1502 /* Search for the match. */
1504 if (cs_prev(sp
, ep
, &cs
))
1506 if (cs
.cs_lno
< m
.lno
||
1507 cs
.cs_lno
== m
.lno
&& cs
.cs_cno
< m
.cno
)
1509 if (cs
.cs_flags
!= 0) {
1510 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
) {
1511 (void)sp
->s_bell(sp
);
1516 if (cs
.cs_ch
== endc
)
1518 else if (cs
.cs_ch
== startc
&& --cnt
== 0)
1522 /* Move to the match. */
1525 sp
->lno
= cs
.cs_lno
;
1526 sp
->cno
= cs
.cs_cno
;
1527 (void)sp
->s_refresh(sp
, ep
);
1530 * Sleep(3) is eight system calls. Do it fast -- besides,
1531 * I don't want to wait an entire second.
1534 second
.tv_sec
= O_VAL(sp
, O_MATCHTIME
) / 10;
1535 second
.tv_usec
= (O_VAL(sp
, O_MATCHTIME
) % 10) * 100000L;
1536 (void)select(0, &zero
, &zero
, &zero
, &second
);
1538 /* Return to the current location. */
1541 (void)sp
->s_refresh(sp
, ep
);
1546 * Handle margin wrap.
1549 * Historic vi belled the user each time a character was entered after
1550 * crossing the margin until a space was entered which could be used to
1551 * break the line. I don't, it tends to wake the cats.
1554 txt_margin(sp
, tp
, didbreak
, pushc
)
1561 size_t len
, off
, tlen
;
1564 /* Find the closest previous blank. */
1565 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1571 /* If it's the beginning of the line, there's nothing to do. */
1572 if (off
== tp
->ai
|| off
== tp
->offset
) {
1579 * Historic practice is to delete any trailing whitespace
1580 * from the previous line.
1582 for (tlen
= len
;; --p
, --off
) {
1586 if (off
== tp
->ai
|| off
== tp
->offset
)
1591 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1593 if (len
&& term_push(sp
, wp
, len
, 0, CH_NOMAP
| CH_QUOTED
))
1596 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
))