2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: v_txt.c,v 10.108 2003/07/18 21:27:42 skimo Exp $ (Berkeley) $Date: 2003/07/18 21:27:42 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.h>
30 #include "../common/common.h"
33 static int txt_abbrev
__P((SCR
*, TEXT
*, CHAR_T
*, int, int *, int *));
34 static void txt_ai_resolve
__P((SCR
*, TEXT
*, int *));
35 static TEXT
*txt_backup
__P((SCR
*, TEXTH
*, TEXT
*, u_int32_t
*));
36 static int txt_dent
__P((SCR
*, TEXT
*, int));
37 static int txt_emark
__P((SCR
*, TEXT
*, size_t));
38 static void txt_err
__P((SCR
*, TEXTH
*));
39 static int txt_fc
__P((SCR
*, TEXT
*, int *));
40 static int txt_fc_col
__P((SCR
*, int, ARGS
**));
41 static int txt_hex
__P((SCR
*, TEXT
*));
42 static int txt_insch
__P((SCR
*, TEXT
*, CHAR_T
*, u_int
));
43 static int txt_isrch
__P((SCR
*, VICMD
*, TEXT
*, u_int8_t
*));
44 static int txt_map_end
__P((SCR
*));
45 static int txt_map_init
__P((SCR
*));
46 static int txt_margin
__P((SCR
*, TEXT
*, TEXT
*, int *, u_int32_t
));
47 static void txt_nomorech
__P((SCR
*));
48 static void txt_Rresolve
__P((SCR
*, TEXTH
*, TEXT
*, const size_t));
49 static int txt_resolve
__P((SCR
*, TEXTH
*, u_int32_t
));
50 static int txt_showmatch
__P((SCR
*, TEXT
*));
51 static void txt_unmap
__P((SCR
*, TEXT
*, u_int32_t
*));
53 /* Cursor character (space is hard to track on the screen). */
54 #if defined(DEBUG) && 0
61 * Fill a buffer from the terminal for vi.
63 * PUBLIC: int v_tcmd __P((SCR *, VICMD *, ARG_CHAR_T, u_int));
66 v_tcmd(SCR
*sp
, VICMD
*vp
, ARG_CHAR_T prompt
, u_int flags
)
68 /* Normally, we end up where we started. */
69 vp
->m_final
.lno
= sp
->lno
;
70 vp
->m_final
.cno
= sp
->cno
;
72 /* Initialize the map. */
76 /* Move to the last line. */
77 sp
->lno
= TMAP
[0].lno
;
80 /* Don't update the modeline for now. */
81 F_SET(sp
, SC_TINPUT_INFO
);
83 /* Set the input flags. */
84 LF_SET(TXT_APPENDEOL
|
85 TXT_CR
| TXT_ESCAPE
| TXT_INFOLINE
| TXT_MAPINPUT
);
86 if (O_ISSET(sp
, O_ALTWERASE
))
87 LF_SET(TXT_ALTWERASE
);
88 if (O_ISSET(sp
, O_TTYWERASE
))
89 LF_SET(TXT_TTYWERASE
);
91 /* Do the input thing. */
92 if (v_txt(sp
, vp
, NULL
, NULL
, 0, prompt
, 0, 1, flags
))
95 /* Reenable the modeline updates. */
96 F_CLR(sp
, SC_TINPUT_INFO
);
98 /* Clean up the map. */
103 F_SET(sp
, SC_SCR_REDRAW
); /* XXX */
105 /* Set the cursor to the resulting position. */
106 sp
->lno
= vp
->m_final
.lno
;
107 sp
->cno
= vp
->m_final
.cno
;
114 * Initialize the screen map for colon command-line input.
117 txt_map_init(SCR
*sp
)
123 if (!IS_ONELINE(sp
)) {
125 * Fake like the user is doing input on the last line of the
126 * screen. This makes all of the scrolling work correctly,
127 * and allows us the use of the vi text editing routines, not
128 * to mention practically infinite length ex commands.
130 * Save the current location.
132 vip
->sv_tm_lno
= TMAP
->lno
;
133 vip
->sv_tm_soff
= TMAP
->soff
;
134 vip
->sv_tm_coff
= TMAP
->coff
;
135 vip
->sv_t_maxrows
= sp
->t_maxrows
;
136 vip
->sv_t_minrows
= sp
->t_minrows
;
137 vip
->sv_t_rows
= sp
->t_rows
;
140 * If it's a small screen, TMAP may be small for the screen.
141 * Fix it, filling in fake lines as we go.
145 HMAP
+ (sp
->t_maxrows
- 1); TMAP
< esmp
; ++TMAP
) {
146 TMAP
[1].lno
= TMAP
[0].lno
+ 1;
147 TMAP
[1].coff
= HMAP
->coff
;
151 /* Build the fake entry. */
152 TMAP
[1].lno
= TMAP
[0].lno
+ 1;
155 SMAP_FLUSH(&TMAP
[1]);
158 /* Reset the screen information. */
159 sp
->t_rows
= sp
->t_minrows
= ++sp
->t_maxrows
;
166 * Reset the screen map for colon command-line input.
175 if (!IS_ONELINE(sp
)) {
176 /* Restore the screen information. */
177 sp
->t_rows
= vip
->sv_t_rows
;
178 sp
->t_minrows
= vip
->sv_t_minrows
;
179 sp
->t_maxrows
= vip
->sv_t_maxrows
;
182 * If it's a small screen, TMAP may be wrong. Clear any
183 * lines that might have been overwritten.
186 for (cnt
= sp
->t_rows
; cnt
<= sp
->t_maxrows
; ++cnt
) {
187 (void)sp
->gp
->scr_move(sp
, cnt
, 0);
188 (void)sp
->gp
->scr_clrtoeol(sp
);
190 TMAP
= HMAP
+ (sp
->t_rows
- 1);
195 * The map may be wrong if the user entered more than one
196 * (logical) line. Fix it. If the user entered a whole
197 * screen, this will be slow, but we probably don't care.
199 if (!O_ISSET(sp
, O_LEFTRIGHT
))
200 while (vip
->sv_tm_lno
!= TMAP
->lno
||
201 vip
->sv_tm_soff
!= TMAP
->soff
)
207 * Invalidate the cursor and the line size cache, the line never
208 * really existed. This fixes bugs where the user searches for
209 * the last line on the screen + 1 and the refresh routine thinks
210 * that's where we just were.
213 F_SET(vip
, VIP_CUR_INVALID
);
219 * If doing input mapping on the colon command line, may need to unmap
220 * based on the command.
223 FL_ISSET(ec_flags, EC_MAPINPUT) && LF_ISSET(TXT_INFOLINE)
226 * Internally, we maintain tp->lno and tp->cno, externally, everyone uses
227 * sp->lno and sp->cno. Make them consistent as necessary.
229 #define UPDATE_POSITION(sp, tp) { \
230 (sp)->lno = (tp)->lno; \
231 (sp)->cno = (tp)->cno; \
238 * PUBLIC: int v_txt __P((SCR *, VICMD *, MARK *,
239 * PUBLIC: const CHAR_T *, size_t, ARG_CHAR_T, db_recno_t, u_long, u_int32_t));
242 v_txt(SCR
*sp
, VICMD
*vp
, MARK
*tm
, const CHAR_T
*lp
, size_t len
, ARG_CHAR_T prompt
, db_recno_t ai_line
, u_long rcount
, u_int32_t flags
)
247 /* Input line length. */
248 /* Prompt to display. */
249 /* Line number to use for autoindent count. */
253 EVENT ev
, *evp
; /* Current event. */
254 EVENT fc
; /* File name completion event. */
256 TEXT
*ntp
, *tp
; /* Input text structures. */
257 TEXT ait
; /* Autoindent text structure. */
258 TEXT wmt
; /* Wrapmargin text structure. */
261 abb_t abb
; /* State of abbreviation checks. */
262 carat_t carat
; /* State of the "[^0]^D" sequences. */
263 quote_t quote
; /* State of quotation. */
264 size_t owrite
, insert
; /* Temporary copies of TEXT fields. */
265 size_t margin
; /* Wrapmargin value. */
266 size_t rcol
; /* 0-N: insert offset in the replay buffer. */
267 size_t tcol
; /* Temporary column. */
268 u_int32_t ec_flags
; /* Input mapping flags. */
269 #define IS_RESTART 0x01 /* Reset the incremental search. */
270 #define IS_RUNNING 0x02 /* Incremental search turned on. */
272 int abcnt
, ab_turnoff
; /* Abbreviation character count, switch. */
273 int filec_redraw
; /* Redraw after the file completion routine. */
274 int hexcnt
; /* Hex character count. */
275 int showmatch
; /* Showmatch set on this character. */
276 int wm_set
, wm_skip
; /* Wrapmargin happened, blank skip flags. */
285 * Set the input flag, so tabs get displayed correctly
286 * and everyone knows that the text buffer is in use.
288 F_SET(sp
, SC_TINPUT
);
291 * Get one TEXT structure with some initial buffer space, reusing
292 * the last one if it's big enough. (All TEXT bookkeeping fields
293 * default to 0 -- text_init() handles this.) If changing a line,
294 * copy it into the TEXT buffer.
297 if (tiqh
->cqh_first
!= (void *)tiqh
) {
298 tp
= tiqh
->cqh_first
;
299 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< len
+ 32) {
303 tp
->ai
= tp
->insert
= tp
->offset
= tp
->owrite
= 0;
306 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, len
);
307 MEMMOVEW(tp
->lb
, lp
, len
);
311 newtp
: if ((tp
= text_init(sp
, lp
, len
, len
+ 32)) == NULL
)
313 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
316 /* Set default termination condition. */
319 /* Set the starting line, column. */
324 * Set the insert and overwrite counts. If overwriting characters,
325 * do insertion afterward. If not overwriting characters, assume
326 * doing insertion. If change is to a mark, emphasize it with an
327 * CH_ENDMARK character.
330 if (LF_ISSET(TXT_OVERWRITE
)) {
331 tp
->owrite
= (tm
->cno
- tp
->cno
) + 1;
332 tp
->insert
= (len
- tm
->cno
) - 1;
334 tp
->insert
= len
- tp
->cno
;
336 if (LF_ISSET(TXT_EMARK
) && txt_emark(sp
, tp
, tm
->cno
))
341 * Many of the special cases in text input are to handle autoindent
342 * support. Somebody decided that it would be a good idea if "^^D"
343 * and "0^D" deleted all of the autoindented characters. In an editor
344 * that takes single character input from the user, this beggars the
345 * imagination. Note also, "^^D" resets the next lines' autoindent,
348 * We assume that autoindent only happens on empty lines, so insert
349 * and overwrite will be zero. If doing autoindent, figure out how
350 * much indentation we need and fill it in. Update input column and
351 * screen cursor as necessary.
353 if (LF_ISSET(TXT_AUTOINDENT
) && ai_line
!= OOBLNO
) {
354 if (v_txt_auto(sp
, ai_line
, NULL
, 0, tp
))
359 * The cc and S commands have a special feature -- leading
360 * <blank> characters are handled as autoindent characters.
363 if (LF_ISSET(TXT_AICHARS
)) {
367 tp
->offset
= tp
->cno
;
370 /* If getting a command buffer from the user, there may be a prompt. */
371 if (LF_ISSET(TXT_PROMPT
)) {
372 tp
->lb
[tp
->cno
++] = prompt
;
378 * If appending after the end-of-line, add a space into the buffer
379 * and move the cursor right. This space is inserted, i.e. pushed
380 * along, and then deleted when the line is resolved. Assumes that
381 * the cursor is already positioned at the end of the line. This
382 * avoids the nastiness of having the cursor reside on a magical
383 * column, i.e. a column that doesn't really exist. The only down
384 * side is that we may wrap lines or scroll the screen before it's
385 * strictly necessary. Not a big deal.
387 if (LF_ISSET(TXT_APPENDEOL
)) {
388 tp
->lb
[tp
->cno
] = CH_CURSOR
;
391 (void)vs_change(sp
, tp
->lno
, LINE_RESET
);
395 * Historic practice is that the wrapmargin value was a distance
396 * from the RIGHT-HAND margin, not the left. It's more useful to
397 * us as a distance from the left-hand margin, i.e. the same as
398 * the wraplen value. The wrapmargin option is historic practice.
399 * Nvi added the wraplen option so that it would be possible to
400 * edit files with consistent margins without knowing the number of
401 * columns in the window.
404 * Setting margin causes a significant performance hit. Normally
405 * we don't update the screen if there are keys waiting, but we
406 * have to if margin is set, otherwise the screen routines don't
407 * know where the cursor is.
410 * Abbreviated keys were affected by the wrapmargin option in the
411 * historic 4BSD vi. Mapped keys were usually, but sometimes not.
412 * See the comment in vi/v_text():set_txt_std for more information.
415 * One more special case. If an inserted <blank> character causes
416 * wrapmargin to split the line, the next user entered character is
417 * discarded if it's a <space> character.
419 wm_set
= wm_skip
= 0;
420 if (LF_ISSET(TXT_WRAPMARGIN
))
421 if ((margin
= O_VAL(sp
, O_WRAPMARGIN
)) != 0)
422 margin
= sp
->cols
- margin
;
424 margin
= O_VAL(sp
, O_WRAPLEN
);
428 /* Initialize abbreviation checks. */
429 abcnt
= ab_turnoff
= 0;
430 abb
= F_ISSET(gp
, G_ABBREV
) &&
431 LF_ISSET(TXT_MAPINPUT
) ? AB_INWORD
: AB_NOTSET
;
434 * Set up the dot command. Dot commands are done by saving the actual
435 * characters and then reevaluating them so that things like wrapmargin
436 * can change between the insert and the replay.
439 * Historically, vi did not remap or reabbreviate replayed input. (It
440 * did beep at you if you changed an abbreviation and then replayed the
441 * input. We're not that compatible.) We don't have to do anything to
442 * avoid remapping, as we're not getting characters from the terminal
443 * routines. Turn the abbreviation check off.
446 * It would be nice if we could swallow backspaces and such, but it's
447 * not all that easy to do. What we can do is turn off the common
448 * error messages during the replay. Otherwise, when the user enters
449 * an illegal command, e.g., "Ia<erase><erase><erase><erase>b<escape>",
450 * and then does a '.', they get a list of error messages after command
454 if (LF_ISSET(TXT_REPLAY
)) {
459 /* Other text input mode setup. */
464 LF_ISSET(TXT_SEARCHINCR
) ? IS_RESTART
| IS_RUNNING
: 0);
465 filec_redraw
= hexcnt
= showmatch
= 0;
467 /* Initialize input flags. */
468 ec_flags
= LF_ISSET(TXT_MAPINPUT
) ? EC_MAPINPUT
: 0;
470 /* Refresh the screen. */
471 UPDATE_POSITION(sp
, tp
);
472 if (vs_refresh(sp
, 1))
475 /* If it's dot, just do it now. */
476 if (F_ISSET(vp
, VC_ISDOT
))
481 next
: if (v_event_get(sp
, evp
, 0, ec_flags
))
485 * If file completion overwrote part of the screen and nothing else has
486 * been displayed, clean up. We don't do this as part of the normal
487 * message resolution because we know the user is on the colon command
488 * line and there's no reason to enter explicit characters to continue.
490 if (filec_redraw
&& !F_ISSET(sp
, SC_SCR_EXWROTE
)) {
493 fc
.e_event
= E_REPAINT
;
494 fc
.e_flno
= vip
->totalcount
>=
495 sp
->rows
? 1 : sp
->rows
- vip
->totalcount
;
496 fc
.e_tlno
= sp
->rows
;
497 vip
->linecount
= vip
->lcontinue
= vip
->totalcount
= 0;
498 (void)v_erepaint(sp
, &fc
);
499 (void)vs_refresh(sp
, 1);
502 /* Deal with all non-character events. */
503 switch (evp
->e_event
) {
508 F_SET(sp
, SC_EXIT_FORCE
);
513 * Historically, <interrupt> exited the user from text input
514 * mode or cancelled a colon command, and returned to command
515 * mode. It also beeped the terminal, but that seems a bit
520 if (v_erepaint(sp
, &ev
))
524 /* <resize> interrupts the input mode. */
525 v_emsg(sp
, NULL
, VIM_WRESIZE
);
528 v_event_err(sp
, evp
);
534 * If the first character of the input is a nul, replay the previous
535 * input. (Historically, it's okay to replay non-existent input.)
536 * This was not documented as far as I know, and is a great test of vi
539 if (rcol
== 0 && !LF_ISSET(TXT_REPLAY
) && evp
->e_c
== '\0') {
540 if (vip
->rep
== NULL
)
550 * File name completion and colon command-line editing. We don't
551 * have enough meta characters, so we expect people to overload
552 * them. If the two characters are the same, then we do file name
553 * completion if the cursor is past the first column, and do colon
554 * command-line editing if it's not.
556 if (quote
== Q_NOTSET
) {
557 int L__cedit
, L__filec
;
559 L__cedit
= L__filec
= 0;
560 if (LF_ISSET(TXT_CEDIT
) && O_STR(sp
, O_CEDIT
) != NULL
&&
561 O_STR(sp
, O_CEDIT
)[0] == evp
->e_c
)
563 if (LF_ISSET(TXT_FILEC
) && O_STR(sp
, O_FILEC
) != NULL
&&
564 O_STR(sp
, O_FILEC
)[0] == evp
->e_c
)
566 if (L__cedit
== 1 && (L__filec
== 0 || tp
->cno
== tp
->offset
)) {
567 tp
->term
= TERM_CEDIT
;
571 if (txt_fc(sp
, tp
, &filec_redraw
))
577 /* Abbreviation overflow check. See comment in txt_abbrev(). */
578 #define MAX_ABBREVIATION_EXPANSION 256
579 if (FL_ISSET(evp
->e_flags
, CH_ABBREVIATED
)) {
580 if (++abcnt
> MAX_ABBREVIATION_EXPANSION
) {
581 if (v_event_flush(sp
, CH_ABBREVIATED
))
583 "191|Abbreviation exceeded expansion limit: characters discarded");
585 if (LF_ISSET(TXT_REPLAY
))
592 /* Check to see if the character fits into the replay buffers. */
593 if (LF_ISSET(TXT_RECORD
)) {
594 BINC_GOTO(sp
, EVENT
, vip
->rep
,
595 vip
->rep_len
, (rcol
+ 1) * sizeof(EVENT
));
596 vip
->rep
[rcol
++] = *evp
;
599 replay
: if (LF_ISSET(TXT_REPLAY
)) {
600 if (rcol
== vip
->rep_cnt
)
602 evp
= vip
->rep
+ rcol
++;
605 /* Wrapmargin check for leading space. */
612 /* If quoted by someone else, simply insert the character. */
613 if (FL_ISSET(evp
->e_flags
, CH_QUOTED
))
618 * If this character was quoted by a K_VLNEXT or a backslash, replace
619 * the placeholder (a carat or a backslash) with the new character.
620 * If it was quoted by a K_VLNEXT, we've already adjusted the cursor
621 * because it has to appear on top of the placeholder character. If
622 * it was quoted by a backslash, adjust the cursor now, the cursor
623 * doesn't appear on top of it. Historic practice in both cases.
625 * Skip tests for abbreviations; ":ab xa XA" followed by "ixa^V<space>"
626 * doesn't perform an abbreviation. Special case, ^V^J (not ^V^M) is
627 * the same as ^J, historically.
629 if (quote
== Q_BTHIS
|| quote
== Q_VTHIS
) {
630 FL_CLR(ec_flags
, EC_QUOTED
);
631 if (LF_ISSET(TXT_MAPINPUT
))
632 FL_SET(ec_flags
, EC_MAPINPUT
);
634 if (quote
== Q_BTHIS
&&
635 (evp
->e_value
== K_VERASE
|| evp
->e_value
== K_VKILL
)) {
641 if (quote
== Q_VTHIS
&& evp
->e_value
!= K_NL
) {
650 * Translate "<CH_HEX>[isxdigit()]*" to a character with a hex value:
651 * this test delimits the value by any non-hex character. Offset by
652 * one, we use 0 to mean that we've found <CH_HEX>.
654 if (hexcnt
> 1 && !isxdigit(evp
->e_c
)) {
660 switch (evp
->e_value
) {
661 case K_CR
: /* Carriage return. */
662 case K_NL
: /* New line. */
663 /* Return in script windows and the command line. */
664 k_cr
: if (LF_ISSET(TXT_CR
)) {
665 static CHAR_T cr
[] = { '\r', 0 };
667 * If this was a map, we may have not displayed
668 * the line. Display it, just in case.
670 * If a script window and not the colon line,
671 * push a <cr> so it gets executed.
673 if (LF_ISSET(TXT_INFOLINE
)) {
674 if (vs_change(sp
, tp
->lno
, LINE_RESET
))
676 } else if (F_ISSET(sp
, SC_SCRIPT
))
677 (void)v_event_push(sp
, NULL
, cr
, 1, CH_NOMAP
);
679 /* Set term condition: if empty. */
680 if (tp
->cno
<= tp
->offset
)
683 * Set term condition: if searching incrementally and
684 * the user entered a pattern, return a completed
685 * search, regardless if the entire pattern was found.
687 if (FL_ISSET(is_flags
, IS_RUNNING
) &&
688 tp
->cno
>= tp
->offset
+ 1)
689 tp
->term
= TERM_SEARCH
;
694 #define LINE_RESOLVE { \
696 * Handle abbreviations. If there was one, discard the \
697 * replay characters. \
699 if (abb == AB_INWORD && \
700 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) { \
701 if (txt_abbrev(sp, tp, &evp->e_c, \
702 LF_ISSET(TXT_INFOLINE), &tmp, \
706 if (LF_ISSET(TXT_RECORD)) \
711 if (abb != AB_NOTSET) \
714 txt_unmap(sp, tp, &ec_flags); \
716 * Delete any appended cursor. It's possible to get in \
717 * situations where TXT_APPENDEOL is set but tp->insert \
718 * is 0 when using the R command and all the characters \
719 * are tp->owrite characters. \
721 if (LF_ISSET(TXT_APPENDEOL) && tp->insert > 0) { \
729 * Save the current line information for restoration in
730 * txt_backup(), and set the line final length.
732 tp
->sv_len
= tp
->len
;
733 tp
->sv_cno
= tp
->cno
;
736 /* Update the old line. */
737 if (vs_change(sp
, tp
->lno
, LINE_RESET
))
741 * Historic practice, when the autoindent edit option was set,
742 * was to delete <blank> characters following the inserted
743 * newline. This affected the 'R', 'c', and 's' commands; 'c'
744 * and 's' retained the insert characters only, 'R' moved the
745 * overwrite and insert characters into the next TEXT structure.
746 * We keep track of the number of characters erased for the 'R'
747 * command so that the final resolution of the line is correct.
752 if (LF_ISSET(TXT_REPLACE
) && owrite
!= 0) {
753 for (p
= tp
->lb
+ tp
->cno
; owrite
> 0 && isblank(*p
);
754 ++p
, --owrite
, ++tp
->R_erase
);
756 for (; insert
> 0 && isblank(*p
);
757 ++p
, ++tp
->R_erase
, --insert
);
759 p
= tp
->lb
+ tp
->cno
+ owrite
;
760 if (O_ISSET(sp
, O_AUTOINDENT
))
762 isblank(*p
); ++p
, --insert
);
768 * Create a new line and insert the new TEXT into the queue.
769 * DON'T insert until the old line has been updated, or the
770 * inserted line count in line.c:db_get() will be wrong.
772 if ((ntp
= text_init(sp
, p
,
773 insert
+ owrite
, insert
+ owrite
+ 32)) == NULL
)
775 CIRCLEQ_INSERT_TAIL(&sp
->tiq
, ntp
, q
);
777 /* Set up bookkeeping for the new line. */
778 ntp
->insert
= insert
;
779 ntp
->owrite
= owrite
;
780 ntp
->lno
= tp
->lno
+ 1;
783 * Reset the autoindent line value. 0^D keeps the autoindent
784 * line from changing, ^D changes the level, even if there were
785 * no characters in the old line. Note, if using the current
786 * tp structure, use the cursor as the length, the autoindent
787 * characters may have been erased.
789 if (LF_ISSET(TXT_AUTOINDENT
)) {
792 if (v_txt_auto(sp
, OOBLNO
, &ait
, ait
.ai
, ntp
))
794 FREE_SPACEW(sp
, ait
.lb
, ait
.lb_len
);
796 if (v_txt_auto(sp
, OOBLNO
, tp
, tp
->cno
, ntp
))
801 /* Reset the cursor. */
805 * If we're here because wrapmargin was set and we've broken a
806 * line, there may be additional information (i.e. the start of
807 * a line) in the wmt structure.
810 if (wmt
.offset
!= 0 ||
811 wmt
.owrite
!= 0 || wmt
.insert
!= 0) {
812 #define WMTSPACE wmt.offset + wmt.owrite + wmt.insert
813 BINC_GOTOW(sp
, ntp
->lb
,
814 ntp
->lb_len
, ntp
->len
+ WMTSPACE
+ 32);
815 MEMMOVEW(ntp
->lb
+ ntp
->cno
, wmt
.lb
, WMTSPACE
);
816 ntp
->len
+= WMTSPACE
;
817 ntp
->cno
+= wmt
.offset
;
818 ntp
->owrite
= wmt
.owrite
;
819 ntp
->insert
= wmt
.insert
;
824 /* New lines are TXT_APPENDEOL. */
825 if (ntp
->owrite
== 0 && ntp
->insert
== 0) {
826 BINC_GOTOW(sp
, ntp
->lb
, ntp
->lb_len
, ntp
->len
+ 1);
827 LF_SET(TXT_APPENDEOL
);
828 ntp
->lb
[ntp
->cno
] = CH_CURSOR
;
833 /* Swap old and new TEXT's, and update the new line. */
835 if (vs_change(sp
, tp
->lno
, LINE_INSERT
))
839 case K_ESCAPE
: /* Escape. */
840 if (!LF_ISSET(TXT_ESCAPE
))
843 /* If we have a count, start replaying the input. */
854 * Some commands (e.g. 'o') need a <newline> for each
857 if (LF_ISSET(TXT_ADDNEWLINE
))
861 * The R command turns into the 'a' command after the
864 if (LF_ISSET(TXT_REPLACE
)) {
865 tp
->insert
= tp
->owrite
;
872 /* Set term condition: if empty. */
873 if (tp
->cno
<= tp
->offset
)
876 * Set term condition: if searching incrementally and the user
877 * entered a pattern, return a completed search, regardless if
878 * the entire pattern was found.
880 if (FL_ISSET(is_flags
, IS_RUNNING
) && tp
->cno
>= tp
->offset
+ 1)
881 tp
->term
= TERM_SEARCH
;
883 k_escape
: LINE_RESOLVE
;
886 * Clean up for the 'R' command, restoring overwrite
887 * characters, and making them into insert characters.
889 if (LF_ISSET(TXT_REPLACE
))
890 txt_Rresolve(sp
, &sp
->tiq
, tp
, len
);
893 * If there are any overwrite characters, copy down
894 * any insert characters, and decrement the length.
898 MEMMOVEW(tp
->lb
+ tp
->cno
,
899 tp
->lb
+ tp
->cno
+ tp
->owrite
, tp
->insert
);
900 tp
->len
-= tp
->owrite
;
904 * Optionally resolve the lines into the file. If not
905 * resolving the lines into the file, end the line with
906 * a nul. If the line is empty, then set the length to
907 * 0, the termination condition has already been set.
910 * This is wrong, should pass back a length.
912 if (LF_ISSET(TXT_RESOLVE
)) {
913 if (txt_resolve(sp
, &sp
->tiq
, flags
))
916 BINC_GOTOW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
917 tp
->lb
[tp
->len
] = '\0';
921 * Set the return cursor position to rest on the last
922 * inserted character.
927 /* Update the last line. */
928 if (vs_change(sp
, tp
->lno
, LINE_RESET
))
931 case K_CARAT
: /* Delete autoindent chars. */
932 if (tp
->cno
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
935 case K_ZERO
: /* Delete autoindent chars. */
936 if (tp
->cno
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
939 case K_CNTRLD
: /* Delete autoindent char. */
941 * If in the first column or no characters to erase, ignore
942 * the ^D (this matches historic practice). If not doing
943 * autoindent or already inserted non-ai characters, it's a
944 * literal. The latter test is done in the switch, as the
945 * CARAT forms are N + 1, not N.
947 if (!LF_ISSET(TXT_AUTOINDENT
))
953 case C_CARATSET
: /* ^^D */
954 if (tp
->ai
== 0 || tp
->cno
> tp
->ai
+ tp
->offset
+ 1)
957 /* Save the ai string for later. */
960 BINC_GOTOW(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
961 MEMMOVEW(ait
.lb
, tp
->lb
, tp
->ai
);
962 ait
.ai
= ait
.len
= tp
->ai
;
967 case C_ZEROSET
: /* 0^D */
968 if (tp
->ai
== 0 || tp
->cno
> tp
->ai
+ tp
->offset
+ 1)
972 leftmargin
: tp
->lb
[tp
->cno
- 1] = ' ';
973 tp
->owrite
+= tp
->cno
- tp
->offset
;
975 tp
->cno
= tp
->offset
;
977 case C_NOTSET
: /* ^D */
978 if (tp
->ai
== 0 || tp
->cno
> tp
->ai
+ tp
->offset
)
981 (void)txt_dent(sp
, tp
, 0);
987 case K_VERASE
: /* Erase the last character. */
988 /* If can erase over the prompt, return. */
989 if (tp
->cno
<= tp
->offset
&& LF_ISSET(TXT_BS
)) {
995 * If at the beginning of the line, try and drop back to a
996 * previously inserted line.
1000 txt_backup(sp
, &sp
->tiq
, tp
, &flags
)) == NULL
)
1006 /* If nothing to erase, bell the user. */
1007 if (tp
->cno
<= tp
->offset
) {
1008 if (!LF_ISSET(TXT_REPLAY
))
1013 /* Drop back one character. */
1017 * Historically, vi didn't replace the erased characters with
1018 * <blank>s, presumably because it's easier to fix a minor
1019 * typing mistake and continue on if the previous letters are
1020 * already there. This is a problem for incremental searching,
1021 * because the user can no longer tell where they are in the
1022 * colon command line because the cursor is at the last search
1023 * point in the screen. So, if incrementally searching, erase
1024 * the erased characters from the screen.
1026 if (FL_ISSET(is_flags
, IS_RUNNING
))
1027 tp
->lb
[tp
->cno
] = ' ';
1030 * Increment overwrite, decrement ai if deleted.
1033 * Historic vi did not permit users to use erase characters
1034 * to delete autoindent characters. We do. Eat hot death,
1038 if (tp
->cno
< tp
->ai
)
1041 /* Reset if we deleted an incremental search character. */
1042 if (FL_ISSET(is_flags
, IS_RUNNING
))
1043 FL_SET(is_flags
, IS_RESTART
);
1045 case K_VWERASE
: /* Skip back one word. */
1047 * If at the beginning of the line, try and drop back to a
1048 * previously inserted line.
1052 txt_backup(sp
, &sp
->tiq
, tp
, &flags
)) == NULL
)
1058 * If at offset, nothing to erase so bell the user.
1060 if (tp
->cno
<= tp
->offset
) {
1061 if (!LF_ISSET(TXT_REPLAY
))
1067 * The first werase goes back to any autoindent column and the
1068 * second werase goes back to the offset.
1071 * Historic vi did not permit users to use erase characters to
1072 * delete autoindent characters.
1074 if (tp
->ai
&& tp
->cno
> tp
->ai
)
1081 /* Skip over trailing space characters. */
1082 while (tp
->cno
> max
&& isblank(tp
->lb
[tp
->cno
- 1])) {
1089 * There are three types of word erase found on UNIX systems.
1090 * They can be identified by how the string /a/b/c is treated
1091 * -- as 1, 3, or 6 words. Historic vi had two classes of
1092 * characters, and strings were delimited by them and
1093 * <blank>'s, so, 6 words. The historic tty interface used
1094 * <blank>'s to delimit strings, so, 1 word. The algorithm
1095 * offered in the 4.4BSD tty interface (as stty altwerase)
1096 * treats it as 3 words -- there are two classes of
1097 * characters, and strings are delimited by them and
1098 * <blank>'s. The difference is that the type of the first
1099 * erased character erased is ignored, which is exactly right
1100 * when erasing pathname components. The edit options
1101 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD tty
1102 * interface and the historic tty driver behavior,
1103 * respectively, and the default is the same as the historic
1106 * Overwrite erased characters if doing incremental search;
1107 * see comment above.
1109 if (LF_ISSET(TXT_TTYWERASE
))
1110 while (tp
->cno
> max
) {
1113 if (FL_ISSET(is_flags
, IS_RUNNING
))
1114 tp
->lb
[tp
->cno
] = ' ';
1115 if (isblank(tp
->lb
[tp
->cno
- 1]))
1119 if (LF_ISSET(TXT_ALTWERASE
)) {
1122 if (FL_ISSET(is_flags
, IS_RUNNING
))
1123 tp
->lb
[tp
->cno
] = ' ';
1124 if (isblank(tp
->lb
[tp
->cno
- 1]))
1128 tmp
= inword(tp
->lb
[tp
->cno
- 1]);
1129 while (tp
->cno
> max
) {
1132 if (FL_ISSET(is_flags
, IS_RUNNING
))
1133 tp
->lb
[tp
->cno
] = ' ';
1134 if (tmp
!= inword(tp
->lb
[tp
->cno
- 1])
1135 || isblank(tp
->lb
[tp
->cno
- 1]))
1140 /* Reset if we deleted an incremental search character. */
1141 if (FL_ISSET(is_flags
, IS_RUNNING
))
1142 FL_SET(is_flags
, IS_RESTART
);
1144 case K_VKILL
: /* Restart this line. */
1147 * If at the beginning of the line, try and drop back to a
1148 * previously inserted line. Historic vi did not permit
1149 * users to go back to previous lines.
1153 txt_backup(sp
, &sp
->tiq
, tp
, &flags
)) == NULL
)
1158 /* If at offset, nothing to erase so bell the user. */
1159 if (tp
->cno
<= tp
->offset
) {
1160 if (!LF_ISSET(TXT_REPLAY
))
1166 * First kill goes back to any autoindent and second kill goes
1167 * back to the offset.
1170 * Historic vi did not permit users to use erase characters to
1171 * delete autoindent characters.
1173 if (tp
->ai
&& tp
->cno
> tp
->ai
)
1179 tp
->owrite
+= tp
->cno
- max
;
1182 * Overwrite erased characters if doing incremental search;
1183 * see comment above.
1185 if (FL_ISSET(is_flags
, IS_RUNNING
))
1187 tp
->lb
[--tp
->cno
] = ' ';
1188 } while (tp
->cno
> max
);
1192 /* Reset if we deleted an incremental search character. */
1193 if (FL_ISSET(is_flags
, IS_RUNNING
))
1194 FL_SET(is_flags
, IS_RESTART
);
1196 case K_CNTRLT
: /* Add autoindent characters. */
1197 if (!LF_ISSET(TXT_CNTRLT
))
1199 if (txt_dent(sp
, tp
, 1))
1204 if (LF_ISSET(TXT_SHOWMATCH
))
1207 case K_BACKSLASH
: /* Quote next erase/kill. */
1210 * Historic vi tried to make abbreviations after a backslash
1211 * escape work. If you did ":ab x y", and inserted "x\^H",
1212 * (assuming the erase character was ^H) you got "x^H", and
1213 * no abbreviation was done. If you inserted "x\z", however,
1214 * it tried to back up and do the abbreviation, i.e. replace
1215 * 'x' with 'y'. The problem was it got it wrong, and you
1216 * ended up with "zy\".
1218 * This is really hard to do (you have to remember the
1219 * word/non-word state, for example), and doesn't make any
1220 * sense to me. Both backslash and the characters it
1221 * (usually) escapes will individually trigger the
1222 * abbreviation, so I don't see why the combination of them
1223 * wouldn't. I don't expect to get caught on this one,
1224 * particularly since it never worked right, but I've been
1227 * Do the tests for abbreviations, so ":ab xa XA",
1228 * "ixa\<K_VERASE>" performs the abbreviation.
1232 case K_VLNEXT
: /* Quote next character. */
1236 * Turn on the quote flag so that the underlying routines
1237 * quote the next character where it's possible. Turn off
1238 * the input mapbiting flag so that we don't remap the next
1241 FL_SET(ec_flags
, EC_QUOTED
);
1242 FL_CLR(ec_flags
, EC_MAPINPUT
);
1246 * Skip the tests for abbreviations, so ":ab xa XA",
1247 * "ixa^V<space>" doesn't perform the abbreviation.
1253 default: /* Insert the character. */
1255 * Historically, vi eliminated nul's out of hand. If the
1256 * beautify option was set, it also deleted any unknown
1257 * ASCII value less than space (040) and the del character
1258 * (0177), except for tabs. Unknown is a key word here.
1259 * Most vi documentation claims that it deleted everything
1260 * but <tab>, <nl> and <ff>, as that's what the original
1261 * 4BSD documentation said. This is obviously wrong,
1262 * however, as <esc> would be included in that list. What
1263 * we do is eliminate any unquoted, iscntrl() character that
1264 * wasn't a replay and wasn't handled specially, except
1267 if (LF_ISSET(TXT_BEAUTIFY
) && ISCNTRL(evp
->e_c
) &&
1268 evp
->e_value
!= K_FORMFEED
&& evp
->e_value
!= K_TAB
) {
1270 "192|Illegal character; quote to enter");
1271 if (LF_ISSET(TXT_REPLAY
))
1277 * If entering a non-word character after a word, check for
1278 * abbreviations. If there was one, discard replay characters.
1279 * If entering a blank character, check for unmap commands,
1282 if (!inword(evp
->e_c
)) {
1283 if (abb
== AB_INWORD
&&
1284 !LF_ISSET(TXT_REPLAY
) && F_ISSET(gp
, G_ABBREV
)) {
1285 if (txt_abbrev(sp
, tp
, &evp
->e_c
,
1286 LF_ISSET(TXT_INFOLINE
), &tmp
, &ab_turnoff
))
1289 if (LF_ISSET(TXT_RECORD
))
1294 if (isblank(evp
->e_c
) && UNMAP_TST
)
1295 txt_unmap(sp
, tp
, &ec_flags
);
1297 if (abb
!= AB_NOTSET
)
1298 abb
= inword(evp
->e_c
) ? AB_INWORD
: AB_NOTWORD
;
1300 insl_ch
: if (txt_insch(sp
, tp
, &evp
->e_c
, flags
))
1304 * If we're using K_VLNEXT to quote the next character, then
1305 * we want the cursor to position itself on the ^ placeholder
1306 * we're displaying, to match historic practice.
1308 if (quote
== Q_VNEXT
) {
1315 * Translate "<CH_HEX>[isxdigit()]*" to a character with
1316 * a hex value: this test delimits the value by the max
1317 * number of hex bytes. Offset by one, we use 0 to mean
1318 * that we've found <CH_HEX>.
1320 if (hexcnt
!= 0 && hexcnt
++ == sizeof(CHAR_T
) * 2 + 1) {
1322 if (txt_hex(sp
, tp
))
1327 * Check to see if we've crossed the margin.
1330 * In the historic vi, the wrapmargin value was figured out
1331 * using the display widths of the characters, i.e. <tab>
1332 * characters were counted as two characters if the list edit
1333 * option is set, but as the tabstop edit option number of
1334 * characters otherwise. That's what the vs_column() function
1335 * gives us, so we use it.
1338 if (vs_column(sp
, &tcol
))
1340 if (tcol
>= margin
) {
1341 if (txt_margin(sp
, tp
, &wmt
, &tmp
, flags
))
1344 if (isblank(evp
->e_c
))
1353 * If we've reached the end of the buffer, then we need to
1354 * switch into insert mode. This happens when there's a
1355 * change to a mark and the user puts in more characters than
1356 * the length of the motion.
1358 ebuf_chk
: if (tp
->cno
>= tp
->len
) {
1359 BINC_GOTOW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
1360 LF_SET(TXT_APPENDEOL
);
1362 tp
->lb
[tp
->cno
] = CH_CURSOR
;
1367 /* Step the quote state forward. */
1368 if (quote
!= Q_NOTSET
) {
1369 if (quote
== Q_BNEXT
)
1371 if (quote
== Q_VNEXT
)
1378 if (tp
->cno
+ tp
->insert
+ tp
->owrite
!= tp
->len
) {
1380 "len %u != cno: %u ai: %u insert %u overwrite %u",
1381 tp
->len
, tp
->cno
, tp
->ai
, tp
->insert
, tp
->owrite
);
1382 if (LF_ISSET(TXT_REPLAY
))
1384 tp
->len
= tp
->cno
+ tp
->insert
+ tp
->owrite
;
1389 * 1: If we don't need to know where the cursor really is and we're
1390 * replaying text, keep going.
1392 if (margin
== 0 && LF_ISSET(TXT_REPLAY
))
1396 * 2: Reset the line. Don't bother unless we're about to wait on
1397 * a character or we need to know where the cursor really is.
1398 * We have to do this before showing matching characters so the
1399 * user can see what they're matching.
1401 if ((margin
!= 0 || !KEYS_WAITING(sp
)) &&
1402 vs_change(sp
, tp
->lno
, LINE_RESET
))
1406 * 3: If there aren't keys waiting, display the matching character.
1407 * We have to do this before resolving any messages, otherwise
1408 * the error message from a missing match won't appear correctly.
1411 if (!KEYS_WAITING(sp
) && txt_showmatch(sp
, tp
))
1417 * 4: If there have been messages and we're not editing on the colon
1418 * command line or doing file name completion, resolve them.
1420 if ((vip
->totalcount
!= 0 || F_ISSET(gp
, G_BELLSCHED
)) &&
1421 !F_ISSET(sp
, SC_TINPUT_INFO
) && !filec_redraw
&&
1422 vs_resolve(sp
, NULL
, 0))
1426 * 5: Refresh the screen if we're about to wait on a character or we
1427 * need to know where the cursor really is.
1429 if (margin
!= 0 || !KEYS_WAITING(sp
)) {
1430 UPDATE_POSITION(sp
, tp
);
1431 if (vs_refresh(sp
, margin
!= 0))
1435 /* 6: Proceed with the incremental search. */
1436 if (FL_ISSET(is_flags
, IS_RUNNING
) && txt_isrch(sp
, vp
, tp
, &is_flags
))
1439 /* 7: Next character... */
1440 if (LF_ISSET(TXT_REPLAY
))
1444 done
: /* Leave input mode. */
1445 F_CLR(sp
, SC_TINPUT
);
1447 /* If recording for playback, save it. */
1448 if (LF_ISSET(TXT_RECORD
))
1449 vip
->rep_cnt
= rcol
;
1452 * If not working on the colon command line, set the final cursor
1455 if (!F_ISSET(sp
, SC_TINPUT_INFO
)) {
1456 vp
->m_final
.lno
= tp
->lno
;
1457 vp
->m_final
.cno
= tp
->cno
;
1463 F_CLR(sp
, SC_TINPUT
);
1464 txt_err(sp
, &sp
->tiq
);
1470 * Handle abbreviations.
1473 txt_abbrev(SCR
*sp
, TEXT
*tp
, CHAR_T
*pushcp
, int isinfoline
, int *didsubp
, int *turnoffp
)
1480 /* Check to make sure we're not at the start of an append. */
1482 if (tp
->cno
== tp
->offset
)
1488 * Find the start of the "word".
1491 * We match historic practice, which, as far as I can tell, had an
1492 * off-by-one error. The way this worked was that when the inserted
1493 * text switched from a "word" character to a non-word character,
1494 * vi would check for possible abbreviations. It would then take the
1495 * type (i.e. word/non-word) of the character entered TWO characters
1496 * ago, and move backward in the text until reaching a character that
1497 * was not that type, or the beginning of the insert, the line, or
1498 * the file. For example, in the string "abc<space>", when the <space>
1499 * character triggered the abbreviation check, the type of the 'b'
1500 * character was used for moving through the string. Maybe there's a
1501 * reason for not using the first (i.e. 'c') character, but I can't
1504 * Terminate at the beginning of the insert or the character after the
1505 * offset character -- both can be tested for using tp->offset.
1507 off
= tp
->cno
- 1; /* Previous character. */
1509 len
= 1; /* One character test. */
1510 if (off
== tp
->offset
|| isblank(p
[-1]))
1512 if (inword(p
[-1])) /* Move backward to change. */
1515 if (off
== tp
->offset
|| !inword(p
[-1]))
1521 if (off
== tp
->offset
||
1522 inword(p
[-1]) || isblank(p
[-1]))
1528 * Historic vi exploded abbreviations on the command line. This has
1529 * obvious problems in that unabbreviating the string can be extremely
1530 * tricky, particularly if the string has, say, an embedded escape
1531 * character. Personally, I think it's a stunningly bad idea. Other
1532 * examples of problems this caused in historic vi are:
1535 * results in "bar" being abbreviated to "baz", which wasn't what the
1536 * user had in mind at all. Also, the commands:
1539 * resulted in an error message that "bar" wasn't mapped. Finally,
1540 * since the string was already exploded by the time the unabbreviate
1541 * command got it, all it knew was that an abbreviation had occurred.
1542 * Cleverly, it checked the replacement string for its unabbreviation
1543 * match, which meant that the commands:
1547 * unabbreviate "foo1", and the commands:
1550 * unabbreviate "foo"!
1552 * Anyway, people neglected to first ask my opinion before they wrote
1553 * macros that depend on this stuff, so, we make this work as follows.
1554 * When checking for an abbreviation on the command line, if we get a
1555 * string which is <blank> terminated and which starts at the beginning
1556 * of the line, we check to see it is the abbreviate or unabbreviate
1557 * commands. If it is, turn abbreviations off and return as if no
1558 * abbreviation was found. Note also, minor trickiness, so that if
1559 * the user erases the line and starts another command, we turn the
1560 * abbreviations back on.
1562 * This makes the layering look like a Nachos Supreme.
1564 search
: if (isinfoline
)
1565 if (off
== tp
->ai
|| off
== tp
->offset
)
1566 if (ex_is_abbrev(sp
, p
, len
)) {
1575 /* Check for any abbreviations. */
1576 if ((qp
= seq_find(sp
, NULL
, NULL
, p
, len
, SEQ_ABBREV
, NULL
)) == NULL
)
1580 * Push the abbreviation onto the tty stack. Historically, characters
1581 * resulting from an abbreviation expansion were themselves subject to
1582 * map expansions, O_SHOWMATCH matching etc. This means the expanded
1583 * characters will be re-tested for abbreviations. It's difficult to
1584 * know what historic practice in this case was, since abbreviations
1585 * were applied to :colon command lines, so entering abbreviations that
1586 * looped was tricky, although possible. In addition, obvious loops
1587 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
1588 * silently only implement and/or display the last abbreviation.)
1590 * This implementation doesn't recover well from such abbreviations.
1591 * The main input loop counts abbreviated characters, and, when it
1592 * reaches a limit, discards any abbreviated characters on the queue.
1593 * It's difficult to back up to the original position, as the replay
1594 * queue would have to be adjusted, and the line state when an initial
1595 * abbreviated character was received would have to be saved.
1598 if (v_event_push(sp
, NULL
, &ch
, 1, CH_ABBREVIATED
))
1600 if (v_event_push(sp
, NULL
, qp
->output
, qp
->olen
, CH_ABBREVIATED
))
1604 * If the size of the abbreviation is larger than or equal to the size
1605 * of the original text, move to the start of the replaced characters,
1606 * and add their length to the overwrite count.
1608 * If the abbreviation is smaller than the original text, we have to
1609 * delete the additional overwrite characters and copy down any insert
1613 if (qp
->olen
>= len
)
1617 MEMMOVEW(tp
->lb
+ tp
->cno
+ qp
->olen
,
1618 tp
->lb
+ tp
->cno
+ tp
->owrite
+ len
, tp
->insert
);
1619 tp
->owrite
+= qp
->olen
;
1620 tp
->len
-= len
- qp
->olen
;
1624 * We return the length of the abbreviated characters. This is so
1625 * the calling routine can replace the replay characters with the
1626 * abbreviation. This means that subsequent '.' commands will produce
1627 * the same text, regardless of intervening :[un]abbreviate commands.
1628 * This is historic practice.
1636 * Handle the unmap command.
1639 txt_unmap(SCR
*sp
, TEXT
*tp
, u_int32_t
*ec_flagsp
)
1644 /* Find the beginning of this "word". */
1645 for (off
= tp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
) {
1651 if (off
== tp
->ai
|| off
== tp
->offset
)
1657 * Historic vi exploded input mappings on the command line. See the
1658 * txt_abbrev() routine for an explanation of the problems inherent
1661 * We make this work as follows. If we get a string which is <blank>
1662 * terminated and which starts at the beginning of the line, we check
1663 * to see it is the unmap command. If it is, we return that the input
1664 * mapping should be turned off. Note also, minor trickiness, so that
1665 * if the user erases the line and starts another command, we go ahead
1666 * an turn mapping back on.
1668 if ((off
== tp
->ai
|| off
== tp
->offset
) && ex_is_unmap(sp
, p
, len
))
1669 FL_CLR(*ec_flagsp
, EC_MAPINPUT
);
1671 FL_SET(*ec_flagsp
, EC_MAPINPUT
);
1676 * When a line is resolved by <esc>, review autoindent characters.
1679 txt_ai_resolve(SCR
*sp
, TEXT
*tp
, int *changedp
)
1683 size_t cno
, len
, new, old
, scno
, spaces
, tab_after_sp
, tabs
;
1689 * If the line is empty, has an offset, or no autoindent
1690 * characters, we're done.
1692 if (!tp
->len
|| tp
->offset
|| !tp
->ai
)
1696 * If the length is less than or equal to the autoindent
1697 * characters, delete them.
1699 if (tp
->len
<= tp
->ai
) {
1700 tp
->ai
= tp
->cno
= tp
->len
= 0;
1705 * The autoindent characters plus any leading <blank> characters
1706 * in the line are resolved into the minimum number of characters.
1707 * Historic practice.
1709 ts
= O_VAL(sp
, O_TABSTOP
);
1711 /* Figure out the last <blank> screen column. */
1712 for (p
= tp
->lb
, scno
= 0, len
= tp
->len
,
1713 spaces
= tab_after_sp
= 0; len
-- && isblank(*p
); ++p
)
1717 scno
+= COL_OFF(scno
, ts
);
1724 * If there are no spaces, or no tabs after spaces and less than
1725 * ts spaces, it's already minimal.
1727 if (!spaces
|| !tab_after_sp
&& spaces
< ts
)
1730 /* Count up spaces/tabs needed to get to the target. */
1731 for (cno
= 0, tabs
= 0; cno
+ COL_OFF(cno
, ts
) <= scno
; ++tabs
)
1732 cno
+= COL_OFF(cno
, ts
);
1733 spaces
= scno
- cno
;
1736 * Figure out how many characters we're dropping -- if we're not
1737 * dropping any, it's already minimal, we're done.
1740 new = spaces
+ tabs
;
1744 /* Shift the rest of the characters down, adjust the counts. */
1746 MEMMOVEW(p
- del
, p
, tp
->len
- old
);
1750 /* Fill in space/tab characters. */
1751 for (p
= tp
->lb
; tabs
--;)
1760 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1761 * retrieve the line.
1763 * PUBLIC: int v_txt_auto __P((SCR *, db_recno_t, TEXT *, size_t, TEXT *));
1766 v_txt_auto(SCR
*sp
, db_recno_t lno
, TEXT
*aitp
, size_t len
, TEXT
*tp
)
1773 * If the ex append command is executed with an address of 0,
1774 * it's possible to get here with a line number of 0. Return
1781 if (db_get(sp
, lno
, DBG_FATAL
, &t
, &len
))
1786 /* Count whitespace characters. */
1787 for (p
= t
; len
> 0; ++p
, --len
)
1791 /* Set count, check for no indentation. */
1792 if ((nlen
= (p
- t
)) == 0)
1795 /* Make sure the buffer's big enough. */
1796 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ nlen
);
1798 /* Copy the buffer's current contents up. */
1800 MEMMOVEW(tp
->lb
+ nlen
, tp
->lb
, tp
->len
);
1803 /* Copy the indentation into the new buffer. */
1804 MEMMOVEW(tp
->lb
, t
, nlen
);
1806 /* Set the autoindent count. */
1813 * Back up to the previously edited line.
1816 txt_backup(SCR
*sp
, TEXTH
*tiqh
, TEXT
*tp
, u_int32_t
*flagsp
)
1821 /* Get a handle on the previous TEXT structure. */
1822 if ((ntp
= tp
->q
.cqe_prev
) == (void *)tiqh
) {
1823 if (!FL_ISSET(*flagsp
, TXT_REPLAY
))
1825 "193|Already at the beginning of the insert");
1830 ntp
->len
= ntp
->sv_len
;
1832 /* Handle appending to the line. */
1834 if (ntp
->owrite
== 0 && ntp
->insert
== 0) {
1835 ntp
->lb
[ntp
->len
] = CH_CURSOR
;
1838 FL_SET(*flagsp
, TXT_APPENDEOL
);
1840 FL_CLR(*flagsp
, TXT_APPENDEOL
);
1842 /* Release the current TEXT. */
1843 CIRCLEQ_REMOVE(tiqh
, tp
, q
);
1846 /* Update the old line on the screen. */
1847 if (vs_change(sp
, ntp
->lno
+ 1, LINE_DELETE
))
1850 /* Return the new/current TEXT. */
1855 * Text indentation is truly strange. ^T and ^D do movements to the next or
1856 * previous shiftwidth value, i.e. for a 1-based numbering, with shiftwidth=3,
1857 * ^T moves a cursor on the 7th, 8th or 9th column to the 10th column, and ^D
1861 * The ^T and ^D characters in historical vi had special meaning only when they
1862 * were the first characters entered after entering text input mode. As normal
1863 * erase characters couldn't erase autoindent characters (^T in this case), it
1864 * meant that inserting text into previously existing text was strange -- ^T
1865 * only worked if it was the first keystroke(s), and then could only be erased
1866 * using ^D. This implementation treats ^T specially anywhere it occurs in the
1867 * input, and permits the standard erase characters to erase the characters it
1871 * A fun test is to try:
1873 * i<CR>^Tx<CR>^Tx<CR>^Tx<CR>^Dx<CR>^Dx<CR>^Dx<esc>
1874 * Historic vi loses some of the '$' marks on the line ends, but otherwise gets
1878 * Technically, txt_dent should be part of the screen interface, as it requires
1879 * knowledge of character sizes, including <space>s, on the screen. It's here
1880 * because it's a complicated little beast, and I didn't want to shove it down
1881 * into the screen. It's probable that KEY_LEN will call into the screen once
1882 * there are screens with different character representations.
1885 * Handle ^T indents, ^D outdents.
1887 * If anything changes here, check the ex version to see if it needs similar
1891 txt_dent(SCR
*sp
, TEXT
*tp
, int isindent
)
1895 size_t cno
, current
, spaces
, target
, tabs
, off
;
1898 ts
= O_VAL(sp
, O_TABSTOP
);
1899 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
1902 * Since we don't know what precedes the character(s) being inserted
1903 * (or deleted), the preceding whitespace characters must be resolved.
1904 * An example is a <tab>, which doesn't need a full shiftwidth number
1905 * of columns because it's preceded by <space>s. This is easy to get
1906 * if the user sets shiftwidth to a value less than tabstop (or worse,
1907 * something for which tabstop isn't a multiple) and then uses ^T to
1908 * indent, and ^D to outdent.
1910 * Figure out the current and target screen columns. In the historic
1911 * vi, the autoindent column was NOT determined using display widths
1912 * of characters as was the wrapmargin column. For that reason, we
1913 * can't use the vs_column() function, but have to calculate it here.
1914 * This is slow, but it's normally only on the first few characters of
1917 for (current
= cno
= 0; cno
< tp
->cno
; ++cno
)
1918 current
+= tp
->lb
[cno
] == '\t' ?
1919 COL_OFF(current
, ts
) : KEY_COL(sp
, tp
->lb
[cno
]);
1923 target
+= COL_OFF(target
, sw
);
1925 target
-= --target
% sw
;
1928 * The AI characters will be turned into overwrite characters if the
1929 * cursor immediately follows them. We test both the cursor position
1930 * and the indent flag because there's no single test. (^T can only
1931 * be detected by the cursor position, and while we know that the test
1932 * is always true for ^D, the cursor can be in more than one place, as
1933 * "0^D" and "^D" are different.)
1935 ai_reset
= !isindent
|| tp
->cno
== tp
->ai
+ tp
->offset
;
1938 * Back up over any previous <blank> characters, changing them into
1939 * overwrite characters (including any ai characters). Then figure
1940 * out the current screen column.
1942 for (; tp
->cno
> tp
->offset
&&
1943 (tp
->lb
[tp
->cno
- 1] == ' ' || tp
->lb
[tp
->cno
- 1] == '\t');
1944 --tp
->cno
, ++tp
->owrite
);
1945 for (current
= cno
= 0; cno
< tp
->cno
; ++cno
)
1946 current
+= tp
->lb
[cno
] == '\t' ?
1947 COL_OFF(current
, ts
) : KEY_COL(sp
, tp
->lb
[cno
]);
1950 * If we didn't move up to or past the target, it's because there
1951 * weren't enough characters to delete, e.g. the first character
1952 * of the line was a tp->offset character, and the user entered
1953 * ^D to move to the beginning of a line. An example of this is:
1955 * :set ai sw=4<cr>i<space>a<esc>i^T^D
1957 * Otherwise, count up the total spaces/tabs needed to get from the
1958 * beginning of the line (or the last non-<blank> character) to the
1961 if (current
>= target
)
1965 tabs
= 0; cno
+ COL_OFF(cno
, ts
) <= target
; ++tabs
)
1966 cno
+= COL_OFF(cno
, ts
);
1967 spaces
= target
- cno
;
1970 /* If we overwrote ai characters, reset the ai count. */
1972 tp
->ai
= tabs
+ spaces
;
1975 * Call txt_insch() to insert each character, so that we get the
1976 * correct effect when we add a <tab> to replace N <spaces>.
1978 for (ch
= '\t'; tabs
> 0; --tabs
)
1979 (void)txt_insch(sp
, tp
, &ch
, 0);
1980 for (ch
= ' '; spaces
> 0; --spaces
)
1981 (void)txt_insch(sp
, tp
, &ch
, 0);
1987 * File name completion.
1990 txt_fc(SCR
*sp
, TEXT
*tp
, int *redrawp
)
1996 size_t indx
, len
, nlen
, off
;
2006 * Find the beginning of this "word" -- if we're at the beginning
2007 * of the line, it's a special case.
2013 retry
: for (len
= 0,
2014 off
= tp
->cno
- 1, p
= tp
->lb
+ off
;; --off
, --p
) {
2020 if (off
== tp
->ai
|| off
== tp
->offset
)
2025 * Get enough space for a wildcard character.
2028 * This won't work for "foo\", since the \ will escape the expansion
2029 * character. I'm not sure if that's a bug or not...
2032 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
2038 /* Build an ex command, and call the ex expansion routines. */
2039 ex_cinit(sp
, &cmd
, 0, 0, OOBLNO
, OOBLNO
, 0);
2040 if (argv_exp2(sp
, &cmd
, p
, len
+ 1)) {
2050 case 0: /* No matches. */
2052 (void)sp
->gp
->scr_bell(sp
);
2054 case 1: /* One match. */
2055 /* If something changed, do the exchange. */
2056 nlen
= STRLEN(cmd
.argv
[0]->bp
);
2057 if (len
!= nlen
|| MEMCMP(cmd
.argv
[0]->bp
, p
, len
))
2060 /* If haven't done a directory test, do it now. */
2061 INT2CHAR(sp
, cmd
.argv
[0]->bp
, cmd
.argv
[0]->len
+ 1,
2064 !stat(np
, &sb
) && S_ISDIR(sb
.st_mode
)) {
2069 /* If nothing changed, period, ring the bell. */
2071 (void)sp
->gp
->scr_bell(sp
);
2073 default: /* Multiple matches. */
2075 if (txt_fc_col(sp
, argc
, argv
))
2078 /* Find the length of the shortest match. */
2079 for (nlen
= cmd
.argv
[0]->len
; --argc
> 0;) {
2080 if (cmd
.argv
[argc
]->len
< nlen
)
2081 nlen
= cmd
.argv
[argc
]->len
;
2082 for (indx
= 0; indx
< nlen
&&
2083 cmd
.argv
[argc
]->bp
[indx
] == cmd
.argv
[0]->bp
[indx
];
2090 /* Overwrite the expanded text first. */
2091 for (t
= cmd
.argv
[0]->bp
; len
> 0 && nlen
> 0; --len
, --nlen
)
2094 /* If lost text, make the remaining old text overwrite characters. */
2100 /* Overwrite any overwrite characters next. */
2101 for (; nlen
> 0 && tp
->owrite
> 0; --nlen
, --tp
->owrite
, ++tp
->cno
)
2104 /* Shift remaining text up, and move the cursor to the end. */
2107 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ nlen
);
2113 if (tp
->insert
!= 0)
2114 (void)MEMMOVEW(p
+ nlen
, p
, tp
->insert
);
2119 /* If a single match and it's a directory, retry it. */
2120 INT2CHAR(sp
, cmd
.argv
[0]->bp
, cmd
.argv
[0]->len
+ 1, np
, nplen
);
2121 if (argc
== 1 && !stat(np
, &sb
) && S_ISDIR(sb
.st_mode
)) {
2122 isdir
: if (tp
->owrite
== 0) {
2124 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
2126 if (tp
->insert
!= 0)
2127 (void)MEMMOVEW(p
+ 1, p
, tp
->insert
);
2143 * Display file names for file name completion.
2146 txt_fc_col(SCR
*sp
, int argc
, ARGS
**argv
)
2151 size_t base
, cnt
, col
, colwidth
, numrows
, numcols
, prefix
, row
;
2158 /* Trim any directory prefix common to all of the files. */
2159 INT2CHAR(sp
, argv
[0]->bp
, argv
[0]->len
+ 1, np
, nlen
);
2160 if ((pp
= strrchr(np
, '/')) == NULL
)
2163 prefix
= (pp
- np
) + 1;
2164 for (ac
= argc
- 1, av
= argv
+ 1; ac
> 0; --ac
, ++av
)
2165 if (av
[0]->len
< prefix
||
2166 MEMCMP(av
[0]->bp
, argv
[0]->bp
,
2174 * Figure out the column width for the longest name. Output is done on
2175 * 6 character "tab" boundaries for no particular reason. (Since we
2176 * don't output tab characters, we ignore the terminal's tab settings.)
2177 * Ignore the user's tab setting because we have no idea how reasonable
2180 for (ac
= argc
, av
= argv
, colwidth
= 0; ac
> 0; --ac
, ++av
) {
2181 for (col
= 0, p
= av
[0]->bp
+ prefix
; *p
!= '\0'; ++p
)
2182 col
+= KEY_LEN(sp
, *p
);
2186 colwidth
+= COL_OFF(colwidth
, 6);
2189 * Writing to the bottom line of the screen is always turned off when
2190 * SC_TINPUT_INFO is set. Turn it back on, we know what we're doing.
2192 if (F_ISSET(sp
, SC_TINPUT_INFO
)) {
2194 F_CLR(sp
, SC_TINPUT_INFO
);
2199 if (F_ISSET(gp, G_INTERRUPTED)) \
2202 /* If the largest file name is too large, just print them. */
2203 if (colwidth
> sp
->cols
) {
2204 for (ac
= argc
, av
= argv
; ac
> 0; --ac
, ++av
) {
2205 INT2CHAR(sp
, av
[0]->bp
+prefix
, av
[0]->len
+1-prefix
,
2207 pp
= msg_print(sp
, np
, &nf
);
2208 (void)ex_printf(sp
, "%s\n", pp
);
2209 if (F_ISSET(gp
, G_INTERRUPTED
))
2213 FREE_SPACE(sp
, pp
, 0);
2216 /* Figure out the number of columns. */
2217 numcols
= (sp
->cols
- 1) / colwidth
;
2218 if (argc
> numcols
) {
2219 numrows
= argc
/ numcols
;
2225 /* Display the files in sorted order. */
2226 for (row
= 0; row
< numrows
; ++row
) {
2227 for (base
= row
, col
= 0; col
< numcols
; ++col
) {
2228 INT2CHAR(sp
, argv
[base
]->bp
+prefix
,
2229 argv
[base
]->len
+1-prefix
, np
, nlen
);
2230 pp
= msg_print(sp
, np
, &nf
);
2231 cnt
= ex_printf(sp
, "%s", pp
);
2233 FREE_SPACE(sp
, pp
, 0);
2235 if ((base
+= numrows
) >= argc
)
2238 "%*s", (int)(colwidth
- cnt
), "");
2241 (void)ex_puts(sp
, "\n");
2244 (void)ex_puts(sp
, "\n");
2247 (void)ex_fflush(sp
);
2250 intr
: F_CLR(gp
, G_INTERRUPTED
);
2253 F_SET(sp
, SC_TINPUT_INFO
);
2260 * Set the end mark on the line.
2263 txt_emark(SCR
*sp
, TEXT
*tp
, size_t cno
)
2267 size_t chlen
, nlen
, olen
;
2273 * The end mark may not be the same size as the current character.
2274 * Don't let the line shift.
2276 nlen
= KEY_LEN(sp
, ch
);
2277 if (tp
->lb
[cno
] == '\t')
2278 (void)vs_columns(sp
, tp
->lb
, tp
->lno
, &cno
, &olen
);
2280 olen
= KEY_LEN(sp
, tp
->lb
[cno
]);
2283 * If the line got longer, well, it's weird, but it's easy. If
2284 * it's the same length, it's easy. If it got shorter, we have
2288 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ olen
);
2289 chlen
= olen
- nlen
;
2290 if (tp
->insert
!= 0)
2291 MEMMOVEW(tp
->lb
+ cno
+ 1 + chlen
,
2292 tp
->lb
+ cno
+ 1, tp
->insert
);
2295 tp
->owrite
+= chlen
;
2297 if (tp
->lb
[cno
] == '\t')
2298 for (cno
+= chlen
; chlen
--;)
2301 for (kp
= KEY_NAME(sp
, tp
->lb
[cno
]),
2302 cno
+= chlen
; chlen
--;)
2306 return (vs_change(sp
, tp
->lno
, LINE_RESET
));
2311 * Handle an error during input processing.
2314 txt_err(SCR
*sp
, TEXTH
*tiqh
)
2319 * The problem with input processing is that the cursor is at an
2320 * indeterminate position since some input may have been lost due
2321 * to a malloc error. So, try to go back to the place from which
2322 * the cursor started, knowing that it may no longer be available.
2324 * We depend on at least one line number being set in the text
2327 for (lno
= tiqh
->cqh_first
->lno
;
2328 !db_exist(sp
, lno
) && lno
> 0; --lno
);
2330 sp
->lno
= lno
== 0 ? 1 : lno
;
2333 /* Redraw the screen, just in case. */
2334 F_SET(sp
, SC_SCR_REDRAW
);
2339 * Let the user insert any character value they want.
2342 * This is an extension. The pattern "^X[0-9a-fA-F]*" is a way
2343 * for the user to specify a character value which their keyboard
2344 * may not be able to enter.
2347 txt_hex(SCR
*sp
, TEXT
*tp
)
2355 * Null-terminate the string. Since nul isn't a legal hex value,
2356 * this should be okay, and lets us use a local routine, which
2357 * presumably understands the character set, to convert the value.
2359 savec
= tp
->lb
[tp
->cno
];
2360 tp
->lb
[tp
->cno
] = 0;
2362 /* Find the previous CH_HEX character. */
2363 for (off
= tp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --p
, --off
, ++len
) {
2368 /* Not on this line? Shouldn't happen. */
2369 if (off
== tp
->ai
|| off
== tp
->offset
)
2373 /* If length of 0, then it wasn't a hex value. */
2377 /* Get the value. */
2379 value
= STRTOL(wp
, NULL
, 16);
2380 if (errno
|| value
> MAX_CHAR_T
) {
2381 nothex
: tp
->lb
[tp
->cno
] = savec
;
2385 /* Restore the original character. */
2386 tp
->lb
[tp
->cno
] = savec
;
2388 /* Adjust the bookkeeping. */
2391 tp
->lb
[tp
->cno
- 1] = value
;
2393 /* Copy down any overwrite characters. */
2395 MEMMOVEW(tp
->lb
+ tp
->cno
, tp
->lb
+ tp
->cno
+ len
,
2398 /* Copy down any insert characters. */
2400 MEMMOVEW(tp
->lb
+ tp
->cno
+ tp
->owrite
,
2401 tp
->lb
+ tp
->cno
+ tp
->owrite
+ len
,
2411 * Historic vi did a special screen optimization for tab characters. As an
2412 * example, for the keystrokes "iabcd<esc>0C<tab>", the tab overwrote the
2413 * rest of the string when it was displayed.
2415 * Because early versions of this implementation redisplayed the entire line
2416 * on each keystroke, the "bcd" was pushed to the right as it ignored that
2417 * the user had "promised" to change the rest of the characters. However,
2418 * the historic vi implementation had an even worse bug: given the keystrokes
2419 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
2420 * on the second <esc> key.
2422 * POSIX 1003.2 requires (will require) that this be fixed, specifying that
2423 * vi overwrite characters the user has committed to changing, on the basis
2424 * of the screen space they require, but that it not overwrite other characters.
2427 txt_insch(SCR
*sp
, TEXT
*tp
, CHAR_T
*chp
, u_int flags
)
2431 size_t chlen
, cno
, copydown
, olen
, nlen
;
2435 * The 'R' command does one-for-one replacement, because there's
2436 * no way to know how many characters the user intends to replace.
2438 if (LF_ISSET(TXT_REPLACE
)) {
2441 tp
->lb
[tp
->cno
++] = *chp
;
2444 } else if (tp
->owrite
) { /* Overwrite a character. */
2448 * If the old or new characters are tabs, then the length of the
2449 * display depends on the character position in the display. We
2450 * don't even try to handle this here, just ask the screen.
2453 savech
= tp
->lb
[cno
];
2455 (void)vs_columns(sp
, tp
->lb
, tp
->lno
, &cno
, &nlen
);
2456 tp
->lb
[cno
] = savech
;
2458 nlen
= KEY_LEN(sp
, *chp
);
2461 * Eat overwrite characters until we run out of them or we've
2462 * handled the length of the new character. If we only eat
2463 * part of an overwrite character, break it into its component
2464 * elements and display the remaining components.
2466 for (copydown
= 0; nlen
!= 0 && tp
->owrite
!= 0;) {
2469 if (tp
->lb
[cno
] == '\t')
2470 (void)vs_columns(sp
,
2471 tp
->lb
, tp
->lno
, &cno
, &olen
);
2473 olen
= KEY_LEN(sp
, tp
->lb
[cno
]);
2484 tp
->lb
, tp
->lb_len
, tp
->len
+ olen
);
2485 chlen
= olen
- nlen
;
2486 MEMMOVEW(tp
->lb
+ cno
+ 1 + chlen
,
2488 tp
->owrite
+ tp
->insert
);
2491 tp
->owrite
+= chlen
;
2492 if (tp
->lb
[cno
] == '\t')
2493 for (p
= tp
->lb
+ cno
+ 1; chlen
--;)
2497 KEY_NAME(sp
, tp
->lb
[cno
]) + nlen
,
2498 p
= tp
->lb
+ cno
+ 1; chlen
--;)
2506 * If had to erase several characters, we adjust the total
2507 * count, and if there are any characters left, shift them
2510 if (copydown
!= 0 && (tp
->len
-= copydown
) != 0)
2511 MEMMOVEW(tp
->lb
+ cno
, tp
->lb
+ cno
+ copydown
,
2512 tp
->owrite
+ tp
->insert
+ copydown
);
2514 /* If we had enough overwrite characters, we're done. */
2516 tp
->lb
[tp
->cno
++] = *chp
;
2521 /* Check to see if the character fits into the input buffer. */
2522 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
2525 if (tp
->insert
) { /* Insert a character. */
2526 if (tp
->insert
== 1)
2527 tp
->lb
[tp
->cno
+ 1] = tp
->lb
[tp
->cno
];
2529 MEMMOVEW(tp
->lb
+ tp
->cno
+ 1,
2530 tp
->lb
+ tp
->cno
, tp
->owrite
+ tp
->insert
);
2532 tp
->lb
[tp
->cno
++] = *chp
;
2538 * Do an incremental search.
2541 txt_isrch(SCR
*sp
, VICMD
*vp
, TEXT
*tp
, u_int8_t
*is_flagsp
)
2547 /* If it's a one-line screen, we don't do incrementals. */
2548 if (IS_ONELINE(sp
)) {
2549 FL_CLR(*is_flagsp
, IS_RUNNING
);
2554 * If the user erases back to the beginning of the buffer, there's
2555 * nothing to search for. Reset the cursor to the starting point.
2558 vp
->m_final
= vp
->m_start
;
2563 * If it's an RE quote character, and not quoted, ignore it until
2564 * we get another character.
2566 if (tp
->lb
[tp
->cno
- 1] == '\\' &&
2567 (tp
->cno
== 2 || tp
->lb
[tp
->cno
- 2] != '\\'))
2571 * If it's a magic shell character, and not quoted, reset the cursor
2572 * to the starting point.
2574 if (strchr(O_STR(sp
, O_SHELLMETA
), tp
->lb
[tp
->cno
- 1]) != NULL
&&
2575 (tp
->cno
== 2 || tp
->lb
[tp
->cno
- 2] != '\\'))
2576 vp
->m_final
= vp
->m_start
;
2579 * If we see the search pattern termination character, then quit doing
2580 * an incremental search. There may be more, e.g., ":/foo/;/bar/",
2581 * and we can't handle that incrementally. Also, reset the cursor to
2582 * the original location, the ex search routines don't know anything
2583 * about incremental searches.
2585 if (tp
->lb
[0] == tp
->lb
[tp
->cno
- 1] &&
2586 (tp
->cno
== 2 || tp
->lb
[tp
->cno
- 2] != '\\')) {
2587 vp
->m_final
= vp
->m_start
;
2588 FL_CLR(*is_flagsp
, IS_RUNNING
);
2593 * Remember the input line and discard the special input map,
2594 * but don't overwrite the input line on the screen.
2597 F_SET(VIP(sp
), VIP_S_MODELINE
);
2598 F_CLR(sp
, SC_TINPUT
| SC_TINPUT_INFO
);
2599 if (txt_map_end(sp
))
2603 * Specify a starting point and search. If we find a match, move to
2604 * it and refresh the screen. If we didn't find the match, then we
2605 * beep the screen. When searching from the original cursor position,
2606 * we have to move the cursor, otherwise, we don't want to move the
2607 * cursor in case the text at the current position continues to match.
2609 if (FL_ISSET(*is_flagsp
, IS_RESTART
)) {
2610 start
= vp
->m_start
;
2613 start
= vp
->m_final
;
2614 sf
= SEARCH_INCR
| SEARCH_SET
;
2617 if (tp
->lb
[0] == '/' ?
2619 &start
, &vp
->m_final
, tp
->lb
+ 1, tp
->cno
- 1, NULL
, sf
) :
2621 &start
, &vp
->m_final
, tp
->lb
+ 1, tp
->cno
- 1, NULL
, sf
)) {
2622 sp
->lno
= vp
->m_final
.lno
;
2623 sp
->cno
= vp
->m_final
.cno
;
2624 FL_CLR(*is_flagsp
, IS_RESTART
);
2626 if (!KEYS_WAITING(sp
) && vs_refresh(sp
, 0))
2629 FL_SET(*is_flagsp
, IS_RESTART
);
2631 /* Reinstantiate the special input map. */
2632 if (txt_map_init(sp
))
2634 F_CLR(VIP(sp
), VIP_S_MODELINE
);
2635 F_SET(sp
, SC_TINPUT
| SC_TINPUT_INFO
);
2637 /* Reset the line number of the input line. */
2638 tp
->lno
= TMAP
[0].lno
;
2641 * If the colon command-line moved, i.e. the screen scrolled,
2642 * refresh the input line.
2645 * We shouldn't be calling vs_line, here -- we need dirty bits
2646 * on entries in the SMAP array.
2648 if (lno
!= TMAP
[0].lno
) {
2649 if (vs_line(sp
, &TMAP
[0], NULL
, NULL
))
2651 (void)sp
->gp
->scr_refresh(sp
, 0);
2658 * Resolve the input text chain into the file.
2661 txt_resolve(SCR
*sp
, TEXTH
*tiqh
, u_int32_t flags
)
2669 * The first line replaces a current line, and all subsequent lines
2670 * are appended into the file. Resolve autoindented characters for
2671 * each line before committing it. If the latter causes the line to
2672 * change, we have to redisplay it, otherwise the information cached
2673 * about the line will be wrong.
2676 tp
= tiqh
->cqh_first
;
2678 if (LF_ISSET(TXT_AUTOINDENT
))
2679 txt_ai_resolve(sp
, tp
, &changed
);
2682 if (db_set(sp
, tp
->lno
, tp
->lb
, tp
->len
) ||
2683 changed
&& vs_change(sp
, tp
->lno
, LINE_RESET
))
2686 for (lno
= tp
->lno
; (tp
= tp
->q
.cqe_next
) != (void *)&sp
->tiq
; ++lno
) {
2687 if (LF_ISSET(TXT_AUTOINDENT
))
2688 txt_ai_resolve(sp
, tp
, &changed
);
2691 if (db_append(sp
, 0, lno
, tp
->lb
, tp
->len
) ||
2692 changed
&& vs_change(sp
, tp
->lno
, LINE_RESET
))
2697 * Clear the input flag, the look-aside buffer is no longer valid.
2698 * Has to be done as part of text resolution, or upon return we'll
2699 * be looking at incorrect data.
2701 F_CLR(sp
, SC_TINPUT
);
2708 * Show a character match.
2711 * Historic vi tried to display matches even in the :colon command line.
2715 txt_showmatch(SCR
*sp
, TEXT
*tp
)
2720 int cnt
, endc
, startc
;
2725 * Do a refresh first, in case we haven't done one in awhile,
2726 * so the user can see what we're complaining about.
2728 UPDATE_POSITION(sp
, tp
);
2729 if (vs_refresh(sp
, 1))
2733 * We don't display the match if it's not on the screen. Find
2734 * out what the first character on the screen is.
2736 if (vs_sm_position(sp
, &m
, 0, P_TOP
))
2739 /* Initialize the getc() interface. */
2740 cs
.cs_lno
= tp
->lno
;
2741 cs
.cs_cno
= tp
->cno
- 1;
2742 if (cs_init(sp
, &cs
))
2744 startc
= (endc
= cs
.cs_ch
) == ')' ? '(' : '{';
2746 /* Search for the match. */
2748 if (cs_prev(sp
, &cs
))
2750 if (cs
.cs_flags
!= 0) {
2751 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
) {
2753 "Unmatched %s", KEY_NAME(sp
, endc
));
2758 if (cs
.cs_ch
== endc
)
2760 else if (cs
.cs_ch
== startc
&& --cnt
== 0)
2764 /* If the match is on the screen, move to it. */
2765 if (cs
.cs_lno
< m
.lno
|| cs
.cs_lno
== m
.lno
&& cs
.cs_cno
< m
.cno
)
2767 sp
->lno
= cs
.cs_lno
;
2768 sp
->cno
= cs
.cs_cno
;
2769 if (vs_refresh(sp
, 1))
2772 /* Wait for timeout or character arrival. */
2773 return (v_event_get(sp
,
2774 NULL
, O_VAL(sp
, O_MATCHTIME
) * 100, EC_TIMEOUT
));
2779 * Handle margin wrap.
2782 txt_margin(SCR
*sp
, TEXT
*tp
, TEXT
*wmtp
, int *didbreak
, u_int32_t flags
)
2788 /* Find the nearest previous blank. */
2789 for (off
= tp
->cno
- 1, p
= tp
->lb
+ off
, len
= 0;; --off
, --p
, ++len
) {
2796 * If reach the start of the line, there's nowhere to break.
2799 * Historic vi belled each time a character was entered after
2800 * crossing the margin until a space was entered which could
2801 * be used to break the line. I don't as it tends to wake the
2804 if (off
== tp
->ai
|| off
== tp
->offset
) {
2811 * Store saved information about the rest of the line in the
2812 * wrapmargin TEXT structure.
2815 * The offset field holds the length of the current characters
2816 * that the user entered, but which are getting split to the new
2817 * line -- it's going to be used to set the cursor value when we
2818 * move to the new line.
2823 wmtp
->insert
= LF_ISSET(TXT_APPENDEOL
) ? tp
->insert
- 1 : tp
->insert
;
2824 wmtp
->owrite
= tp
->owrite
;
2826 /* Correct current bookkeeping information. */
2828 if (LF_ISSET(TXT_APPENDEOL
)) {
2829 tp
->len
-= len
+ tp
->owrite
+ (tp
->insert
- 1);
2832 tp
->len
-= len
+ tp
->owrite
+ tp
->insert
;
2839 * Delete any trailing whitespace from the current line.
2841 for (;; --p
, --off
) {
2846 if (off
== tp
->ai
|| off
== tp
->offset
)
2855 * Resolve the input line for the 'R' command.
2858 txt_Rresolve(SCR
*sp
, TEXTH
*tiqh
, TEXT
*tp
, const size_t orig_len
)
2861 size_t input_len
, retain
;
2865 * Check to make sure that the cursor hasn't moved beyond
2866 * the end of the line.
2868 if (tp
->owrite
== 0)
2872 * Calculate how many characters the user has entered,
2873 * plus the blanks erased by <carriage-return>/<newline>s.
2875 for (ttp
= tiqh
->cqh_first
, input_len
= 0;;) {
2876 input_len
+= ttp
== tp
? tp
->cno
: ttp
->len
+ ttp
->R_erase
;
2877 if ((ttp
= ttp
->q
.cqe_next
) == (void *)&sp
->tiq
)
2882 * If the user has entered less characters than the original line
2883 * was long, restore any overwriteable characters to the original
2884 * characters. These characters are entered as "insert characters",
2885 * because they're after the cursor and we don't want to lose them.
2886 * (This is okay because the R command has no insert characters.)
2887 * We set owrite to 0 so that the insert characters don't get copied
2888 * to somewhere else, which means that the line and the length have
2889 * to be adjusted here as well.
2891 * We have to retrieve the original line because the original pinned
2892 * page has long since been discarded. If it doesn't exist, that's
2893 * okay, the user just extended the file.
2895 if (input_len
< orig_len
) {
2896 retain
= MIN(tp
->owrite
, orig_len
- input_len
);
2898 tiqh
->cqh_first
->lno
, DBG_FATAL
| DBG_NOCACHE
, &p
, NULL
))
2900 MEMCPYW(tp
->lb
+ tp
->cno
, p
+ input_len
, retain
);
2901 tp
->len
-= tp
->owrite
- retain
;
2903 tp
->insert
+= retain
;
2909 * No more characters message.
2912 txt_nomorech(SCR
*sp
)
2914 msgq(sp
, M_BERR
, "194|No more characters to erase");