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.62 1993/11/30 09:28:57 bostic Exp $ (Berkeley) $Date: 1993/11/30 09:28:57 $";
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 v_ntext(sp
, ep
, tiqh
, tm
, lp
, len
, rp
, prompt
, ai_line
, flags
)
74 MARK
*tm
; /* To MARK. */
75 const char *lp
; /* Input line. */
76 const size_t len
; /* Input line length. */
77 MARK
*rp
; /* Return MARK. */
78 int prompt
; /* Prompt to display. */
79 recno_t ai_line
; /* Line number to use for autoindent count. */
80 u_int flags
; /* TXT_ flags. */
82 /* State of abbreviation checks. */
83 enum { A_NOTSET
, A_SPACE
, A_NOTSPACE
} abb
;
84 /* State of the "[^0]^D" sequences. */
85 enum { C_NOTSET
, C_CARATSET
, C_NOCHANGE
, C_ZEROSET
} carat_st
;
86 /* State of the hex input character. */
87 enum { H_NOTSET
, H_NEXTCHAR
, H_INHEX
} hex
;
88 /* State of quotation. */
89 enum { Q_NOTSET
, Q_NEXTCHAR
, Q_THISCHAR
} quoted
;
90 CH ikey
; /* Input character structure. */
91 CHAR_T ch
; /* Input character. */
92 GS
*gp
; /* Global pointer. */
93 TEXT
*tp
, *ntp
, ait
; /* Input and autoindent text structures. */
94 size_t rcol
; /* 0-N: insert offset in the replay buffer. */
95 size_t col
; /* Current column. */
96 u_long margin
; /* Wrapmargin value. */
97 u_int iflags
; /* Input flags. */
98 int ab_cnt
; /* Abbreviation count. */
99 int eval
; /* Routine return value. */
100 int replay
; /* If replaying a set of input. */
101 int showmatch
; /* Showmatch set on this character. */
102 int testnr
; /* Test first character for nul replay. */
107 * Set the input flag, so tabs get displayed correctly
108 * and everyone knows that the text buffer is in use.
112 /* Set return value. */
116 * Get one TEXT structure with some initial buffer space, reusing
117 * the last one if it's big enough. (All TEXT bookkeeping fields
118 * default to 0 -- text_init() handles this.) If changing a line,
119 * copy it into the TEXT buffer.
121 if (tiqh
->cqh_first
!= (void *)tiqh
) {
122 tp
= tiqh
->cqh_first
;
123 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< len
+ 32) {
127 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
130 memmove(tp
->lb
, lp
, len
);
134 newtp
: if ((tp
= text_init(sp
, lp
, len
, len
+ 32)) == NULL
)
136 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
139 /* Set the starting line number. */
143 * Set the insert and overwrite counts. If overwriting characters,
144 * do insertion afterward. If not overwriting characters, assume
145 * doing insertion. If change is to a mark, emphasize it with an
149 if (LF_ISSET(TXT_OVERWRITE
)) {
150 tp
->owrite
= tm
->cno
- sp
->cno
;
151 tp
->insert
= len
- tm
->cno
;
153 tp
->insert
= len
- sp
->cno
;
155 if (LF_ISSET(TXT_EMARK
))
156 tp
->lb
[tm
->cno
- 1] = END_CH
;
160 * Many of the special cases in this routine are to handle autoindent
161 * support. Somebody decided that it would be a good idea if "^^D"
162 * and "0^D" deleted all of the autoindented characters. In an editor
163 * that takes single character input from the user, this wasn't a very
164 * good idea. Note also that "^^D" resets the next lines' autoindent,
167 * We assume that autoindent only happens on empty lines, so insert
168 * and overwrite will be zero. If doing autoindent, figure out how
169 * much indentation we need and fill it in. Update input column and
170 * screen cursor as necessary.
172 if (LF_ISSET(TXT_AUTOINDENT
) && ai_line
!= OOBLNO
) {
173 if (txt_auto(sp
, ep
, ai_line
, NULL
, 0, tp
))
178 * The cc and S commands have a special feature -- leading
179 * <blank> characters are handled as autoindent characters.
182 if (LF_ISSET(TXT_AICHARS
)) {
186 tp
->offset
= sp
->cno
;
189 /* If getting a command buffer from the user, there may be a prompt. */
190 if (LF_ISSET(TXT_PROMPT
)) {
191 tp
->lb
[sp
->cno
++] = prompt
;
197 * If appending after the end-of-line, add a space into the buffer
198 * and move the cursor right. This space is inserted, i.e. pushed
199 * along, and then deleted when the line is resolved. Assumes that
200 * the cursor is already positioned at the end of the line. This
201 * avoids the nastiness of having the cursor reside on a magical
202 * column, i.e. a column that doesn't really exist. The only down
203 * side is that we may wrap lines or scroll the screen before it's
204 * strictly necessary. Not a big deal.
206 if (LF_ISSET(TXT_APPENDEOL
)) {
207 tp
->lb
[sp
->cno
] = CURSOR_CH
;
213 * Historic practice is that the wrapmargin value was a distance
214 * from the RIGHT-HAND column, not the left. It's more useful to
215 * us as a distance from the left-hand column.
218 * Setting margin causes a significant performance hit. Normally
219 * we don't update the screen if there are keys waiting, but we
220 * have to if margin is set, otherwise the screen routines don't
221 * know where the cursor is.
223 if (!LF_ISSET(TXT_WRAPMARGIN
))
225 else if ((margin
= O_VAL(sp
, O_WRAPMARGIN
)) != 0)
226 margin
= sp
->cols
- margin
;
228 /* Initialize abbreviations checks. */
229 if (F_ISSET(sp
, S_ABBREV
) && LF_ISSET(TXT_MAPINPUT
)) {
236 * Set up the dot command. Dot commands are done by saving the
237 * actual characters and replaying the input. We have to push
238 * the characters onto the key stack and then handle them normally,
239 * otherwise things like wrapmargin will fail.
242 * It would be nice if we could swallow backspaces and such, but
243 * it's not all that easy to do. Another possibility would be to
244 * recognize full line insertions, which could be performed quickly,
249 if (replay
= LF_ISSET(TXT_REPLAY
)) {
252 * Historically, it wasn't an error to replay non-existent
253 * input. This test is necessary, we get here by the user
254 * doing an input command followed by a nul.
257 * Historically, vi did not remap or reabbreviate replayed
258 * input. It did, however, beep at you if you changed an
259 * abbreviation and then replayed the input. We're not that
262 if (VIP(sp
)->rep
== NULL
)
264 if (term_push(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_cnt
, 0, CH_NOMAP
))
272 iflags
= LF_ISSET(TXT_MAPCOMMAND
| TXT_MAPINPUT
);
273 for (gp
= sp
->gp
, showmatch
= 0,
274 carat_st
= C_NOTSET
, hex
= H_NOTSET
, quoted
= Q_NOTSET
;;) {
276 * Reset the line and update the screen. (The txt_showmatch()
277 * code refreshes the screen for us.) Don't refresh unless
278 * we're about to wait on a character or we need to know where
279 * the cursor really is.
281 if (showmatch
|| margin
|| !KEYS_WAITING(sp
)) {
282 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
286 txt_showmatch(sp
, ep
);
287 } else if (sp
->s_refresh(sp
, ep
))
291 /* Get the next character. */
292 next_ch
: if (term_key(sp
, &ikey
, iflags
) != INP_OK
)
296 /* Abbreviation check. See comment in txt_abbrev(). */
297 #define MAX_ABBREVIATION_EXPANSION 256
298 if (ikey
.flags
& CH_ABBREVIATED
) {
299 if (++ab_cnt
> MAX_ABBREVIATION_EXPANSION
) {
301 "Abbreviation exceeded maximum number of characters");
310 * Historic feature. If the first character of the input is
311 * a nul, replay the previous input. This isn't documented
312 * anywhere, and is a great test of vi clones.
314 if (ch
== '\0' && testnr
) {
321 * Check to see if the character fits into the input (and
322 * replay, if necessary) buffers. It isn't necessary to
323 * have tp->len bytes, since it doesn't consider overwrite
324 * characters, but not worth fixing.
326 if (LF_ISSET(TXT_RECORD
)) {
327 TBINC(sp
, VIP(sp
)->rep
, VIP(sp
)->rep_len
, rcol
+ 1);
328 VIP(sp
)->rep
[rcol
++] = ch
;
330 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
333 * If the character was quoted, replace the last character
334 * (the literal mark) with the new character. If quoted
335 * by someone else, simply insert the character.
338 * Extension -- if the quoted character is HEX_CH, enter hex
339 * mode. If the user enters "<HEX_CH>[isxdigit()]*" we will
340 * try to use the value as a character. Anything else resets
343 if (ikey
.flags
& CH_QUOTED
)
345 if (quoted
== Q_THISCHAR
) {
355 switch (ikey
.value
) {
357 case K_NL
: /* New line. */
358 #define LINE_RESOLVE { \
360 * Handle abbreviations. If there was one, \
361 * discard the replay characters. \
363 if (abb == A_NOTSPACE && !replay) { \
364 if (txt_abbrev(sp, tp, &tmp, ch)) \
367 if (LF_ISSET(TXT_RECORD)) \
372 if (abb != A_NOTSET) \
374 /* Handle hex numbers. */ \
375 if (hex == H_INHEX) { \
376 if (txt_hex(sp, tp, &tmp, ch)) \
384 * The 'R' command returns any overwriteable \
385 * characters in the first line to the original \
388 if (LF_ISSET(TXT_REPLACE) && tp->owrite && \
389 tp == tiqh->cqh_first) { \
390 memmove(tp->lb + sp->cno, \
391 lp + sp->cno, tp->owrite); \
392 tp->insert += tp->owrite; \
395 /* Delete any appended cursor. */ \
396 if (LF_ISSET(TXT_APPENDEOL)) { \
403 /* CR returns from the vi command line. */
404 if (LF_ISSET(TXT_CR
)) {
405 if (F_ISSET(sp
, S_SCRIPT
))
407 "\r", 1, 0, CH_NOMAP
);
412 * Historic practice was to delete any <blank>
413 * characters following the inserted newline.
414 * This affects the 'R', 'c', and 's' commands.
416 for (p
= tp
->lb
+ sp
->cno
+ tp
->owrite
;
417 tp
->insert
&& isblank(*p
);
418 ++p
, ++tp
->owrite
, --tp
->insert
);
421 * Move any remaining insert characters into
422 * a new TEXT structure.
424 if ((ntp
= text_init(sp
,
425 tp
->lb
+ sp
->cno
+ tp
->owrite
,
426 tp
->insert
, tp
->insert
+ 32)) == NULL
)
428 CIRCLEQ_INSERT_TAIL(tiqh
, ntp
, q
);
430 /* Set bookkeeping for the new line. */
431 ntp
->lno
= tp
->lno
+ 1;
432 ntp
->insert
= tp
->insert
;
435 * Note if the user inserted any characters on this
436 * line. Done before calling txt_ai_resolve() because
437 * it changes the value of sp->cno without making the
438 * corresponding changes to tp->ai.
440 tmp
= sp
->cno
<= tp
->ai
;
443 * Resolve autoindented characters for the old line.
444 * Reset the autoindent line value. 0^D keeps the ai
445 * line from changing, ^D changes the level, even if
446 * there are no characters in the old line. Note,
447 * if using the current tp structure, use the cursor
448 * as the length, the user may have erased autoindent
451 if (LF_ISSET(TXT_AUTOINDENT
)) {
452 txt_ai_resolve(sp
, tp
);
454 if (carat_st
== C_NOCHANGE
) {
456 OOBLNO
, &ait
, ait
.ai
, ntp
))
458 FREE_SPACE(sp
, ait
.lb
, ait
.lb_len
);
461 OOBLNO
, tp
, sp
->cno
, ntp
))
467 * If the user hasn't entered any characters, delete
468 * any autoindent characters.
471 * Historic vi didn't get the insert test right, if
472 * there were characters after the cursor, entering
473 * a <cr> left the autoindent characters on the line.
478 /* Reset bookkeeping for the old line. */
480 tp
->ai
= tp
->insert
= tp
->owrite
= 0;
482 /* New cursor position. */
485 /* New lines are TXT_APPENDEOL if nothing to insert. */
486 if (ntp
->insert
== 0) {
487 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
488 LF_SET(TXT_APPENDEOL
);
489 ntp
->lb
[sp
->cno
] = CURSOR_CH
;
494 /* Update the old line. */
495 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_RESET
))
498 /* Swap old and new TEXT's. */
501 /* Reset the cursor. */
504 /* Update the new line. */
505 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_INSERT
))
508 /* Set the renumber bit. */
509 F_SET(sp
, S_RENUMBER
);
511 /* Refresh if nothing waiting. */
512 if ((margin
|| !KEYS_WAITING(sp
)) &&
513 sp
->s_refresh(sp
, ep
))
516 case K_ESCAPE
: /* Escape. */
517 if (!LF_ISSET(TXT_ESCAPE
))
523 * If there aren't any trailing characters in the line
524 * and the user hasn't entered any characters, delete
525 * the autoindent characters.
527 if (!tp
->insert
&& sp
->cno
<= tp
->ai
) {
528 tp
->len
= tp
->owrite
= 0;
530 } else if (LF_ISSET(TXT_AUTOINDENT
))
531 txt_ai_resolve(sp
, tp
);
533 /* If there are insert characters, copy them down. */
534 k_escape
: if (tp
->insert
&& tp
->owrite
)
535 memmove(tp
->lb
+ sp
->cno
,
536 tp
->lb
+ sp
->cno
+ tp
->owrite
, tp
->insert
);
537 tp
->len
-= tp
->owrite
;
540 * Delete any lines that were inserted into the text
541 * structure and then erased.
543 while (tp
->q
.cqe_next
!= (void *)tiqh
) {
544 ntp
= tp
->q
.cqe_next
;
545 CIRCLEQ_REMOVE(tiqh
, ntp
, q
);
550 * If not resolving the lines into the file, end
554 * This is wrong, should pass back a length.
556 if (LF_ISSET(TXT_RESOLVE
)) {
557 if (txt_resolve(sp
, ep
, tiqh
))
560 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
561 tp
->lb
[tp
->len
] = '\0';
565 * Set the return cursor position to rest on the last
566 * inserted character.
570 rp
->cno
= sp
->cno
? sp
->cno
- 1 : 0;
571 if (sp
->s_change(sp
, ep
, rp
->lno
, LINE_RESET
))
575 case K_CARAT
: /* Delete autoindent chars. */
576 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
577 carat_st
= C_CARATSET
;
579 case K_ZERO
: /* Delete autoindent chars. */
580 if (LF_ISSET(TXT_AUTOINDENT
) && sp
->cno
<= tp
->ai
)
581 carat_st
= C_ZEROSET
;
583 case K_VEOF
: /* Delete autoindent char. */
585 * If not doing autoindent, in the first column, no
586 * characters to erase, or already inserted non-ai
587 * characters, it's a literal. The last test is done
588 * in the switch, as the CARAT forms are N + 1, not N.
590 if (!LF_ISSET(TXT_AUTOINDENT
) ||
591 sp
->cno
== 0 || tp
->ai
== 0)
594 case C_CARATSET
: /* ^^D */
595 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
598 /* Save the ai string for later. */
601 TBINC(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
602 memmove(ait
.lb
, tp
->lb
, tp
->ai
);
603 ait
.ai
= ait
.len
= tp
->ai
;
605 carat_st
= C_NOCHANGE
;
607 case C_ZEROSET
: /* 0^D */
608 if (sp
->cno
> tp
->ai
+ tp
->offset
+ 1)
611 leftmargin
: tp
->lb
[sp
->cno
- 1] = ' ';
612 tp
->owrite
+= sp
->cno
- tp
->offset
;
614 sp
->cno
= tp
->offset
;
616 case C_NOTSET
: /* ^D */
617 if (sp
->cno
> tp
->ai
+ tp
->offset
)
619 (void)txt_outdent(sp
, tp
);
625 case K_VERASE
: /* Erase the last character. */
627 * If can erase over the prompt, return. Len is 0
628 * if backspaced over the prompt, 1 if only CR entered.
630 if (LF_ISSET(TXT_BS
) && sp
->cno
<= tp
->offset
) {
636 * If at the beginning of the line, try and drop back
637 * to a previously inserted line.
640 if ((ntp
= txt_backup(sp
,
641 ep
, tiqh
, tp
, flags
)) == NULL
)
647 /* If nothing to erase, bell the user. */
648 if (sp
->cno
<= tp
->offset
) {
650 "No more characters to erase.");
654 /* Drop back one character. */
658 * Increment overwrite, decrement ai if deleted.
661 * Historic vi did not permit users to use erase
662 * characters to delete autoindent characters.
665 if (sp
->cno
< tp
->ai
)
668 case K_VWERASE
: /* Skip back one word. */
670 * If at the beginning of the line, try and drop back
671 * to a previously inserted line.
674 if ((ntp
= txt_backup(sp
,
675 ep
, tiqh
, tp
, flags
)) == NULL
)
681 * If at offset, nothing to erase so bell the user.
683 if (sp
->cno
<= tp
->offset
) {
685 "No more characters to erase.");
690 * First werase goes back to any autoindent
691 * and second werase goes back to the offset.
694 * Historic vi did not permit users to use erase
695 * characters to delete autoindent characters.
697 if (tp
->ai
&& sp
->cno
> tp
->ai
)
704 /* Skip over trailing space characters. */
705 while (sp
->cno
> max
&& isblank(tp
->lb
[sp
->cno
- 1])) {
712 * There are three types of word erase found on UNIX
713 * systems. They can be identified by how the string
714 * /a/b/c is treated -- as 1, 3, or 6 words. Historic
715 * vi had two classes of characters, and strings were
716 * delimited by them and <blank>'s, so, 6 words. The
717 * historic tty interface used <blank>'s to delimit
718 * strings, so, 1 word. The algorithm offered in the
719 * 4.4BSD tty interface (as stty altwerase) treats it
720 * as 3 words -- there are two classes of characters,
721 * and strings are delimited by them and <blank>'s.
722 * The difference is that the type of the first erased
723 * character erased is ignored, which is exactly right
724 * when erasing pathname components. Here, the options
725 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD
726 * tty interface and the historic tty driver behavior,
727 * respectively, and the default is the same as the
728 * historic vi behavior.
730 if (LF_ISSET(TXT_TTYWERASE
))
731 while (sp
->cno
> max
) {
734 if (isblank(tp
->lb
[sp
->cno
- 1]))
738 if (LF_ISSET(TXT_ALTWERASE
)) {
741 if (isblank(tp
->lb
[sp
->cno
- 1]))
745 tmp
= inword(tp
->lb
[sp
->cno
- 1]);
746 while (sp
->cno
> max
) {
749 if (tmp
!= inword(tp
->lb
[sp
->cno
- 1])
750 || isblank(tp
->lb
[sp
->cno
- 1]))
755 case K_VKILL
: /* Restart this line. */
757 * If at the beginning of the line, try and drop back
758 * to a previously inserted line.
761 if ((ntp
= txt_backup(sp
,
762 ep
, tiqh
, tp
, flags
)) == NULL
)
767 /* If at offset, nothing to erase so bell the user. */
768 if (sp
->cno
<= tp
->offset
) {
770 "No more characters to erase.");
775 * First kill goes back to any autoindent
776 * and second kill goes back to the offset.
779 * Historic vi did not permit users to use erase
780 * characters to delete autoindent characters.
782 if (tp
->ai
&& sp
->cno
> tp
->ai
)
788 tp
->owrite
+= sp
->cno
- max
;
791 case K_CNTRLT
: /* Add autoindent char. */
792 if (!LF_ISSET(TXT_CNTRLT
))
794 if (txt_indent(sp
, tp
))
798 (void)sp
->s_suspend(sp
);
800 #ifdef HISTORIC_PRACTICE_IS_TO_INSERT_NOT_REPAINT
802 F_SET(sp
, S_REFRESH
);
807 showmatch
= LF_ISSET(TXT_SHOWMATCH
);
809 case K_VLNEXT
: /* Quote the next character. */
810 /* If in hex mode, see if we've entered a hex value. */
811 if (hex
== H_INHEX
) {
812 if (txt_hex(sp
, tp
, &tmp
, ch
))
822 default: /* Insert the character. */
824 * If entering a space character after a word, check
825 * for abbreviations. If there was one, discard the
828 if (isblank(ch
) && abb
== A_NOTSPACE
&& !replay
) {
829 if (txt_abbrev(sp
, tp
, &tmp
, ch
))
832 if (LF_ISSET(TXT_RECORD
))
837 /* If in hex mode, see if we've entered a hex value. */
838 if (hex
== H_INHEX
&& !isxdigit(ch
)) {
839 if (txt_hex(sp
, tp
, &tmp
, ch
))
846 /* Check to see if we've crossed the margin. */
848 if (sp
->s_column(sp
, ep
, &col
))
851 if (txt_margin(sp
, tp
, &tmp
, ch
))
858 abb
= isblank(ch
) ? A_SPACE
: A_NOTSPACE
;
860 if (tp
->owrite
) /* Overwrite a character. */
862 else if (tp
->insert
) { /* Insert a character. */
865 tp
->lb
[sp
->cno
+ 1] = tp
->lb
[sp
->cno
];
867 memmove(tp
->lb
+ sp
->cno
+ 1,
868 tp
->lb
+ sp
->cno
, tp
->insert
);
871 tp
->lb
[sp
->cno
++] = ch
;
874 * If we've reached the end of the buffer, then we
875 * need to switch into insert mode. This happens
876 * when there's a change to a mark and the user puts
877 * in more characters than the length of the motion.
879 ebuf_chk
: if (sp
->cno
>= tp
->len
) {
880 TBINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
881 LF_SET(TXT_APPENDEOL
);
882 tp
->lb
[sp
->cno
] = CURSOR_CH
;
887 if (hex
== H_NEXTCHAR
)
889 if (quoted
== Q_NEXTCHAR
)
893 #if defined(DEBUG) && 1
894 if (sp
->cno
+ tp
->insert
+ tp
->owrite
!= tp
->len
)
896 "len %u != cno: %u ai: %u insert %u overwrite %u",
897 tp
->len
, sp
->cno
, tp
->ai
, tp
->insert
, tp
->owrite
);
898 tp
->len
= sp
->cno
+ tp
->insert
+ tp
->owrite
;
902 /* Clear input flag. */
903 ret
: F_CLR(sp
, S_INPUT
);
905 if (LF_ISSET(TXT_RECORD
))
906 VIP(sp
)->rep_cnt
= rcol
;
911 txt_err(sp
, ep
, tiqh
);
917 * Handle abbreviations.
920 txt_abbrev(sp
, tp
, didsubp
, pushc
)
931 /* Find the beginning of this "word". */
932 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
938 if (off
== tp
->ai
|| off
== tp
->offset
)
942 /* Check for any abbreviations. */
943 if ((qp
= seq_find(sp
, NULL
, p
, len
, SEQ_ABBREV
, NULL
)) == NULL
) {
949 * Push the abbreviation onto the tty stack. Historically, characters
950 * resulting from an abbreviation expansion were themselves subject to
951 * map expansions, O_SHOWMATCH matching etc. This means the expanded
952 * characters will be re-tested for abbreviations. It's difficult to
953 * know what historic practice in this case was, since abbreviations
954 * were applied to :colon command lines, so entering abbreviations that
955 * looped was tricky, although possible. In addition, obvious loops
956 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
957 * silently only implement and/or display the last abbreviation.)
959 * This implementation doesn't recover well from such abbreviations.
960 * The main input loop counts abbreviated characters, and, when it
961 * reaches a limit, discards any abbreviated characters on the queue.
962 * It's difficult to back up to the original position, as the replay
963 * queue would have to be adjusted, and the line state when an initial
964 * abbreviated character was received would have to be saved.
967 if (term_push(sp
, &ch
, 1, 0, CH_ABBREVIATED
))
969 if (term_push(sp
, qp
->output
, qp
->olen
, 0, CH_ABBREVIATED
))
973 * Move the cursor to the start of the abbreviation,
979 /* Copy any insert characters back. */
981 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
982 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
, tp
->insert
);
985 * We return the length of the abbreviated characters. This is so
986 * the calling routine can replace the replay characters with the
987 * abbreviation. This means that subsequent '.' commands will produce
988 * the same text, regardless of intervening :[un]abbreviate commands.
989 * This is historic practice.
995 /* Offset to next column of stop size. */
996 #define STOP_OFF(c, stop) (stop - (c) % stop)
1000 * When a line is resolved by <esc> or <cr>, review autoindent
1004 txt_ai_resolve(sp
, tp
)
1010 size_t cno
, len
, new, old
, scno
, spaces
, tab_after_sp
, tabs
;
1014 * If the line is empty, has an offset, or no autoindent
1015 * characters, we're done.
1017 if (!tp
->len
|| tp
->offset
|| !tp
->ai
)
1021 * The autoindent characters plus any leading <blank> characters
1022 * in the line are resolved into the minimum number of characters.
1023 * Historic practice.
1025 ts
= O_VAL(sp
, O_TABSTOP
);
1027 /* Figure out the last <blank> screen column. */
1028 for (p
= tp
->lb
, scno
= 0, len
= tp
->len
,
1029 spaces
= tab_after_sp
= 0; len
-- && isblank(*p
); ++p
)
1033 scno
+= STOP_OFF(scno
, ts
);
1040 * If there are no spaces, or no tabs after spaces and less than
1041 * ts spaces, it's already minimal.
1043 if (!spaces
|| !tab_after_sp
&& spaces
< ts
)
1046 /* Count up spaces/tabs needed to get to the target. */
1047 for (cno
= 0, tabs
= 0; cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1048 cno
+= STOP_OFF(cno
, ts
);
1049 spaces
= scno
- cno
;
1052 * Figure out how many characters we're dropping -- if we're not
1053 * dropping any, it's already minimal, we're done.
1056 new = spaces
+ tabs
;
1060 /* Shift the rest of the characters down, adjust the counts. */
1062 memmove(p
- del
, p
, tp
->len
- old
);
1066 /* Fill in space/tab characters. */
1067 for (p
= tp
->lb
; tabs
--;)
1075 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1076 * retrieve the line.
1079 txt_auto(sp
, ep
, lno
, aitp
, len
, tp
)
1090 if ((p
= t
= file_gline(sp
, ep
, lno
, &len
)) == NULL
)
1094 for (nlen
= 0; len
; ++p
) {
1097 /* If last character is a space, it counts. */
1104 /* No indentation. */
1111 /* Make sure the buffer's big enough. */
1112 BINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ nlen
);
1114 /* Copy the indentation into the new buffer. */
1115 memmove(tp
->lb
+ nlen
, tp
->lb
, tp
->len
);
1116 memmove(tp
->lb
, t
, nlen
);
1119 /* Return the additional length. */
1126 * Back up to the previously edited line.
1129 txt_backup(sp
, ep
, tiqh
, tp
, flags
)
1139 if (tp
->q
.cqe_prev
== (void *)tiqh
) {
1140 msgq(sp
, M_BERR
, "Already at the beginning of the insert");
1144 /* Update the old line on the screen. */
1145 if (sp
->s_change(sp
, ep
, tp
->lno
, LINE_DELETE
))
1148 /* Get a handle on the previous TEXT structure. */
1149 ntp
= tp
->q
.cqe_prev
;
1151 /* Make sure that we can get enough space. */
1152 if (LF_ISSET(TXT_APPENDEOL
) && ntp
->len
+ 1 > ntp
->lb_len
&&
1153 binc(sp
, &ntp
->lb
, &ntp
->lb_len
, ntp
->len
+ 1))
1157 * Release current TEXT; now committed to the swap, nothing
1160 CIRCLEQ_REMOVE(tiqh
, tp
, q
);
1166 /* Set bookkeeping information. */
1168 if (LF_ISSET(TXT_APPENDEOL
)) {
1169 tp
->lb
[col
] = CURSOR_CH
;
1180 * Handle an error during input processing.
1183 txt_err(sp
, ep
, tiqh
)
1192 * The problem with input processing is that the cursor is at an
1193 * indeterminate position since some input may have been lost due
1194 * to a malloc error. So, try to go back to the place from which
1195 * the cursor started, knowing that it may no longer be available.
1197 * We depend on at least one line number being set in the text
1200 for (lno
= tiqh
->cqh_first
->lno
;
1201 file_gline(sp
, ep
, lno
, &len
) == NULL
&& lno
> 0; --lno
);
1203 sp
->lno
= lno
== 0 ? 1 : lno
;
1206 /* Redraw the screen, just in case. */
1207 F_SET(sp
, S_REDRAW
);
1212 * Let the user insert any character value they want.
1215 * This is an extension. The pattern "^Vx[0-9a-fA-F]*" is a way
1216 * for the user to specify a character value which their keyboard
1217 * may not be able to enter.
1220 txt_hex(sp
, tp
, was_hex
, pushc
)
1232 * Null-terminate the string. Since nul isn't a legal hex value,
1233 * this should be okay, and lets us use a local routine, which
1234 * presumably understands the character set, to convert the value.
1236 savec
= tp
->lb
[sp
->cno
];
1237 tp
->lb
[sp
->cno
] = 0;
1239 /* Find the previous HEX_CH. */
1240 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1246 /* If not on this line, there's nothing to do. */
1247 if (off
== tp
->ai
|| off
== tp
->offset
)
1251 /* If no length, then it wasn't a hex value. */
1255 /* Get the value. */
1256 value
= strtol(wp
, NULL
, 16);
1257 if (value
== LONG_MIN
|| value
== LONG_MAX
|| value
> MAX_CHAR_T
) {
1258 nothex
: tp
->lb
[sp
->cno
] = savec
;
1264 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1267 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1270 tp
->lb
[sp
->cno
] = savec
;
1272 /* Move the cursor to the start of the hex value, adjust the length. */
1276 /* Copy any insert characters back. */
1278 memmove(tp
->lb
+ sp
->cno
+ tp
->owrite
,
1279 tp
->lb
+ sp
->cno
+ tp
->owrite
+ len
+ 1, tp
->insert
);
1286 * Txt_indent and txt_outdent are truly strange. ^T and ^D do movements
1287 * to the next or previous shiftwidth value, i.e. for a 1-based numbering,
1288 * with shiftwidth=3, ^T moves a cursor on the 7th, 8th or 9th column to
1289 * the 10th column, and ^D moves it back.
1292 * The ^T and ^D characters in historical vi only had special meaning when
1293 * they were the first characters typed after entering text input mode.
1294 * Since normal erase characters couldn't erase autoindent (in this case
1295 * ^T) characters, this meant that inserting text into previously existing
1296 * text was quite strange, ^T only worked if it was the first keystroke,
1297 * and then it could only be erased by using ^D. This implementation treats
1298 * ^T specially anywhere it occurs in the input, and permits the standard
1299 * erase characters to erase characters inserted using it.
1302 * Technically, txt_indent, txt_outdent should part of the screen interface,
1303 * as they require knowledge of the size of a space character on the screen.
1304 * (Not the size of tabs, because tabs are logically composed of spaces.)
1305 * They're left in the text code because they're complicated, not to mention
1306 * the gruesome awareness that if spaces aren't a single column on the screen
1307 * for any language, we're into some serious, ah, for lack of a better word,
1313 * Handle ^T indents.
1321 size_t cno
, off
, scno
, spaces
, tabs
;
1323 ts
= O_VAL(sp
, O_TABSTOP
);
1324 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1326 /* Get the current screen column. */
1327 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1328 if (tp
->lb
[off
] == '\t')
1329 scno
+= STOP_OFF(scno
, ts
);
1333 /* Count up spaces/tabs needed to get to the target. */
1334 for (cno
= scno
, scno
+= STOP_OFF(scno
, sw
), tabs
= 0;
1335 cno
+ STOP_OFF(cno
, ts
) <= scno
; ++tabs
)
1336 cno
+= STOP_OFF(cno
, ts
);
1337 spaces
= scno
- cno
;
1339 /* Put space/tab characters in place of any overwrite characters. */
1340 for (; tp
->owrite
&& tabs
; --tp
->owrite
, --tabs
, ++tp
->ai
)
1341 tp
->lb
[sp
->cno
++] = '\t';
1342 for (; tp
->owrite
&& spaces
; --tp
->owrite
, --spaces
, ++tp
->ai
)
1343 tp
->lb
[sp
->cno
++] = ' ';
1345 if (!tabs
&& !spaces
)
1348 /* Make sure there's enough room. */
1349 BINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
+ tabs
);
1351 /* Move the insert characters out of the way. */
1353 memmove(tp
->lb
+ sp
->cno
+ spaces
+ tabs
,
1354 tp
->lb
+ sp
->cno
, tp
->insert
);
1356 /* Add new space/tab characters. */
1357 for (; tabs
--; ++tp
->len
, ++tp
->ai
)
1358 tp
->lb
[sp
->cno
++] = '\t';
1359 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1360 tp
->lb
[sp
->cno
++] = ' ';
1366 * Handle ^D outdents.
1375 size_t cno
, off
, scno
, spaces
;
1377 ts
= O_VAL(sp
, O_TABSTOP
);
1378 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1380 /* Get the current screen column. */
1381 for (off
= scno
= 0; off
< sp
->cno
; ++off
)
1382 if (tp
->lb
[off
] == '\t')
1383 scno
+= STOP_OFF(scno
, ts
);
1387 /* Get the previous shiftwidth column. */
1388 for (cno
= scno
; --scno
% sw
!= 0;);
1390 /* Decrement characters until less than or equal to that slot. */
1391 for (; cno
> scno
; --sp
->cno
, --tp
->ai
, ++tp
->owrite
)
1392 if (tp
->lb
[--off
] == '\t')
1393 cno
-= STOP_OFF(cno
, ts
);
1397 /* Spaces needed to get to the target. */
1398 spaces
= scno
- cno
;
1400 /* Maybe just a delete. */
1404 /* Make sure there's enough room. */
1405 BINC(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ spaces
);
1407 /* Use up any overwrite characters. */
1408 for (; tp
->owrite
&& spaces
; --spaces
, ++tp
->ai
, --tp
->owrite
)
1409 tp
->lb
[sp
->cno
++] = ' ';
1411 /* Maybe that was enough. */
1415 /* Move the insert characters out of the way. */
1417 memmove(tp
->lb
+ sp
->cno
+ spaces
,
1418 tp
->lb
+ sp
->cno
, tp
->insert
);
1420 /* Add new space characters. */
1421 for (; spaces
--; ++tp
->len
, ++tp
->ai
)
1422 tp
->lb
[sp
->cno
++] = ' ';
1428 * Resolve the input text chain into the file.
1431 txt_resolve(sp
, ep
, tiqh
)
1439 /* The first line replaces a current line. */
1440 tp
= tiqh
->cqh_first
;
1441 if (file_sline(sp
, ep
, tp
->lno
, tp
->lb
, tp
->len
))
1444 /* All subsequent lines are appended into the file. */
1445 for (lno
= tp
->lno
; (tp
= tp
->q
.cqe_next
) != (void *)&sp
->tiq
; ++lno
)
1446 if (file_aline(sp
, ep
, 0, lno
, tp
->lb
, tp
->len
))
1453 * Show a character match.
1456 * Historic vi tried to display matches even in the :colon command line.
1460 txt_showmatch(sp
, ep
)
1464 struct timeval second
;
1468 int cnt
, endc
, startc
;
1471 * Do a refresh first, in case the v_ntext() code hasn't done
1472 * one in awhile, so the user can see what we're complaining
1475 if (sp
->s_refresh(sp
, ep
))
1478 * We don't display the match if it's not on the screen. Find
1479 * out what the first character on the screen is.
1481 if (sp
->s_position(sp
, ep
, &m
, 0, P_TOP
))
1484 /* Initialize the getc() interface. */
1485 cs
.cs_lno
= sp
->lno
;
1486 cs
.cs_cno
= sp
->cno
- 1;
1487 if (cs_init(sp
, ep
, &cs
))
1489 startc
= (endc
= cs
.cs_ch
) == ')' ? '(' : '{';
1491 /* Search for the match. */
1493 if (cs_prev(sp
, ep
, &cs
))
1495 if (cs
.cs_lno
< m
.lno
||
1496 cs
.cs_lno
== m
.lno
&& cs
.cs_cno
< m
.cno
)
1498 if (cs
.cs_flags
!= 0) {
1499 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
) {
1500 (void)sp
->s_bell(sp
);
1505 if (cs
.cs_ch
== endc
)
1507 else if (cs
.cs_ch
== startc
&& --cnt
== 0)
1511 /* Move to the match. */
1514 sp
->lno
= cs
.cs_lno
;
1515 sp
->cno
= cs
.cs_cno
;
1516 (void)sp
->s_refresh(sp
, ep
);
1519 * Sleep(3) is eight system calls. Do it fast -- besides,
1520 * I don't want to wait an entire second.
1523 second
.tv_sec
= O_VAL(sp
, O_MATCHTIME
) / 10;
1524 second
.tv_usec
= (O_VAL(sp
, O_MATCHTIME
) % 10) * 100000L;
1525 (void)select(0, &zero
, &zero
, &zero
, &second
);
1527 /* Return to the current location. */
1530 (void)sp
->s_refresh(sp
, ep
);
1535 * Handle margin wrap.
1538 * Historic vi belled the user each time a character was entered after
1539 * crossing the margin until a space was entered which could be used to
1540 * break the line. I don't, it tends to wake the cats.
1543 txt_margin(sp
, tp
, didbreak
, pushc
)
1550 size_t len
, off
, tlen
;
1553 /* Find the closest previous blank. */
1554 for (off
= sp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1560 /* If it's the beginning of the line, there's nothing to do. */
1561 if (off
== tp
->ai
|| off
== tp
->offset
) {
1568 * Historic practice is to delete any trailing whitespace
1569 * from the previous line.
1571 for (tlen
= len
;; --p
, --off
) {
1575 if (off
== tp
->ai
|| off
== tp
->offset
)
1580 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
| CH_QUOTED
))
1582 if (len
&& term_push(sp
, wp
, len
, 0, CH_NOMAP
| CH_QUOTED
))
1585 if (term_push(sp
, &ch
, 1, 0, CH_NOMAP
))