ex/ex_display.c (ex_display): fix argument checking in case of wide characters
[nvi.git] / vi / v_txt.c
blob76b56b814bc77793ee86ff322daada088c551242
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "../common/common.h"
31 #include "vi.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
55 #undef CH_CURSOR
56 #define CH_CURSOR '+'
57 #endif
60 * v_tcmd --
61 * Fill a buffer from the terminal for vi.
63 * PUBLIC: int v_tcmd __P((SCR *, VICMD *, ARG_CHAR_T, u_int));
65 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. */
73 if (txt_map_init(sp))
74 return (1);
76 /* Move to the last line. */
77 sp->lno = TMAP[0].lno;
78 sp->cno = 0;
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))
93 return (1);
95 /* Reenable the modeline updates. */
96 F_CLR(sp, SC_TINPUT_INFO);
98 /* Clean up the map. */
99 if (txt_map_end(sp))
100 return (1);
102 if (IS_ONELINE(sp))
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;
109 return (0);
113 * txt_map_init
114 * Initialize the screen map for colon command-line input.
116 static int
117 txt_map_init(SCR *sp)
119 SMAP *esmp;
120 VI_PRIVATE *vip;
122 vip = VIP(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.
143 if (IS_SMALL(sp))
144 for (esmp =
145 HMAP + (sp->t_maxrows - 1); TMAP < esmp; ++TMAP) {
146 TMAP[1].lno = TMAP[0].lno + 1;
147 TMAP[1].coff = HMAP->coff;
148 TMAP[1].soff = 1;
151 /* Build the fake entry. */
152 TMAP[1].lno = TMAP[0].lno + 1;
153 TMAP[1].soff = 1;
154 TMAP[1].coff = 0;
155 SMAP_FLUSH(&TMAP[1]);
156 ++TMAP;
158 /* Reset the screen information. */
159 sp->t_rows = sp->t_minrows = ++sp->t_maxrows;
161 return (0);
165 * txt_map_end
166 * Reset the screen map for colon command-line input.
168 static int
169 txt_map_end(SCR *sp)
171 VI_PRIVATE *vip;
172 size_t cnt;
174 vip = VIP(sp);
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.
185 if (IS_SMALL(sp)) {
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);
191 } else
192 --TMAP;
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)
202 if (vs_sm_1down(sp))
203 return (1);
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.
212 VI_SCR_CFLUSH(vip);
213 F_SET(vip, VIP_CUR_INVALID);
215 return (0);
219 * If doing input mapping on the colon command line, may need to unmap
220 * based on the command.
222 #define UNMAP_TST \
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; \
235 * v_txt --
236 * Vi text input.
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)
245 /* To MARK. */
246 /* Input line. */
247 /* Input line length. */
248 /* Prompt to display. */
249 /* Line number to use for autoindent count. */
250 /* Replay count. */
251 /* TXT_* flags. */
253 EVENT ev, *evp; /* Current event. */
254 EVENT fc; /* File name completion event. */
255 GS *gp;
256 TEXT *ntp, *tp; /* Input text structures. */
257 TEXT ait; /* Autoindent text structure. */
258 TEXT wmt; /* Wrapmargin text structure. */
259 TEXTH *tiqh;
260 VI_PRIVATE *vip;
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. */
271 u_int8_t is_flags;
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. */
277 int max, tmp;
278 CHAR_T *p;
280 gp = sp->gp;
281 vip = VIP(sp);
284 * Set the input flag, so tabs get displayed correctly
285 * and everyone knows that the text buffer is in use.
287 F_SET(sp, SC_TINPUT);
290 * Get one TEXT structure with some initial buffer space, reusing
291 * the last one if it's big enough. (All TEXT bookkeeping fields
292 * default to 0 -- text_init() handles this.) If changing a line,
293 * copy it into the TEXT buffer.
295 tiqh = &sp->tiq;
296 if (tiqh->cqh_first != (void *)tiqh) {
297 tp = tiqh->cqh_first;
298 if (tp->q.cqe_next != (void *)tiqh || tp->lb_len < len + 32) {
299 text_lfree(tiqh);
300 goto newtp;
302 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
303 if (lp != NULL) {
304 tp->len = len;
305 BINC_RETW(sp, tp->lb, tp->lb_len, len);
306 MEMMOVEW(tp->lb, lp, len);
307 } else
308 tp->len = 0;
309 } else {
310 newtp: if ((tp = text_init(sp, lp, len, len + 32)) == NULL)
311 return (1);
312 CIRCLEQ_INSERT_HEAD(tiqh, tp, q);
315 /* Set default termination condition. */
316 tp->term = TERM_OK;
318 /* Set the starting line, column. */
319 tp->lno = sp->lno;
320 tp->cno = sp->cno;
323 * Set the insert and overwrite counts. If overwriting characters,
324 * do insertion afterward. If not overwriting characters, assume
325 * doing insertion. If change is to a mark, emphasize it with an
326 * CH_ENDMARK character.
328 if (len) {
329 if (LF_ISSET(TXT_OVERWRITE)) {
330 tp->owrite = (tm->cno - tp->cno) + 1;
331 tp->insert = (len - tm->cno) - 1;
332 } else
333 tp->insert = len - tp->cno;
335 if (LF_ISSET(TXT_EMARK) && txt_emark(sp, tp, tm->cno))
336 return (1);
340 * Many of the special cases in text input are to handle autoindent
341 * support. Somebody decided that it would be a good idea if "^^D"
342 * and "0^D" deleted all of the autoindented characters. In an editor
343 * that takes single character input from the user, this beggars the
344 * imagination. Note also, "^^D" resets the next lines' autoindent,
345 * but "0^D" doesn't.
347 * We assume that autoindent only happens on empty lines, so insert
348 * and overwrite will be zero. If doing autoindent, figure out how
349 * much indentation we need and fill it in. Update input column and
350 * screen cursor as necessary.
352 if (LF_ISSET(TXT_AUTOINDENT) && ai_line != OOBLNO) {
353 if (v_txt_auto(sp, ai_line, NULL, 0, tp))
354 return (1);
355 tp->cno = tp->ai;
356 } else {
358 * The cc and S commands have a special feature -- leading
359 * <blank> characters are handled as autoindent characters.
360 * Beauty!
362 if (LF_ISSET(TXT_AICHARS)) {
363 tp->offset = 0;
364 tp->ai = tp->cno;
365 } else
366 tp->offset = tp->cno;
369 /* If getting a command buffer from the user, there may be a prompt. */
370 if (LF_ISSET(TXT_PROMPT)) {
371 tp->lb[tp->cno++] = prompt;
372 ++tp->len;
373 ++tp->offset;
377 * If appending after the end-of-line, add a space into the buffer
378 * and move the cursor right. This space is inserted, i.e. pushed
379 * along, and then deleted when the line is resolved. Assumes that
380 * the cursor is already positioned at the end of the line. This
381 * avoids the nastiness of having the cursor reside on a magical
382 * column, i.e. a column that doesn't really exist. The only down
383 * side is that we may wrap lines or scroll the screen before it's
384 * strictly necessary. Not a big deal.
386 if (LF_ISSET(TXT_APPENDEOL)) {
387 tp->lb[tp->cno] = CH_CURSOR;
388 ++tp->len;
389 ++tp->insert;
390 (void)vs_change(sp, tp->lno, LINE_RESET);
394 * Historic practice is that the wrapmargin value was a distance
395 * from the RIGHT-HAND margin, not the left. It's more useful to
396 * us as a distance from the left-hand margin, i.e. the same as
397 * the wraplen value. The wrapmargin option is historic practice.
398 * Nvi added the wraplen option so that it would be possible to
399 * edit files with consistent margins without knowing the number of
400 * columns in the window.
402 * XXX
403 * Setting margin causes a significant performance hit. Normally
404 * we don't update the screen if there are keys waiting, but we
405 * have to if margin is set, otherwise the screen routines don't
406 * know where the cursor is.
408 * !!!
409 * Abbreviated keys were affected by the wrapmargin option in the
410 * historic 4BSD vi. Mapped keys were usually, but sometimes not.
411 * See the comment in vi/v_text():set_txt_std for more information.
413 * !!!
414 * One more special case. If an inserted <blank> character causes
415 * wrapmargin to split the line, the next user entered character is
416 * discarded if it's a <space> character.
418 wm_set = wm_skip = 0;
419 if (LF_ISSET(TXT_WRAPMARGIN))
420 if ((margin = O_VAL(sp, O_WRAPMARGIN)) != 0)
421 margin = sp->cols - margin;
422 else
423 margin = O_VAL(sp, O_WRAPLEN);
424 else
425 margin = 0;
427 /* Initialize abbreviation checks. */
428 abcnt = ab_turnoff = 0;
429 abb = F_ISSET(gp, G_ABBREV) &&
430 LF_ISSET(TXT_MAPINPUT) ? AB_INWORD : AB_NOTSET;
433 * Set up the dot command. Dot commands are done by saving the actual
434 * characters and then reevaluating them so that things like wrapmargin
435 * can change between the insert and the replay.
437 * !!!
438 * Historically, vi did not remap or reabbreviate replayed input. (It
439 * did beep at you if you changed an abbreviation and then replayed the
440 * input. We're not that compatible.) We don't have to do anything to
441 * avoid remapping, as we're not getting characters from the terminal
442 * routines. Turn the abbreviation check off.
444 * XXX
445 * It would be nice if we could swallow backspaces and such, but it's
446 * not all that easy to do. What we can do is turn off the common
447 * error messages during the replay. Otherwise, when the user enters
448 * an illegal command, e.g., "Ia<erase><erase><erase><erase>b<escape>",
449 * and then does a '.', they get a list of error messages after command
450 * completion.
452 rcol = 0;
453 if (LF_ISSET(TXT_REPLAY)) {
454 abb = AB_NOTSET;
455 LF_CLR(TXT_RECORD);
458 /* Other text input mode setup. */
459 quote = Q_NOTSET;
460 carat = C_NOTSET;
461 FL_INIT(is_flags,
462 LF_ISSET(TXT_SEARCHINCR) ? IS_RESTART | IS_RUNNING : 0);
463 filec_redraw = hexcnt = showmatch = 0;
465 /* Initialize input flags. */
466 ec_flags = LF_ISSET(TXT_MAPINPUT) ? EC_MAPINPUT : 0;
468 /* Refresh the screen. */
469 UPDATE_POSITION(sp, tp);
470 if (vs_refresh(sp, 1))
471 return (1);
473 /* If it's dot, just do it now. */
474 if (F_ISSET(vp, VC_ISDOT))
475 goto replay;
477 /* Get an event. */
478 evp = &ev;
479 next: if (v_event_get(sp, evp, 0, ec_flags))
480 return (1);
483 * If file completion overwrote part of the screen and nothing else has
484 * been displayed, clean up. We don't do this as part of the normal
485 * message resolution because we know the user is on the colon command
486 * line and there's no reason to enter explicit characters to continue.
488 if (filec_redraw && !F_ISSET(sp, SC_SCR_EXWROTE)) {
489 filec_redraw = 0;
491 fc.e_event = E_REPAINT;
492 fc.e_flno = vip->totalcount >=
493 sp->rows ? 1 : sp->rows - vip->totalcount;
494 fc.e_tlno = sp->rows;
495 vip->linecount = vip->lcontinue = vip->totalcount = 0;
496 (void)v_erepaint(sp, &fc);
497 (void)vs_refresh(sp, 1);
500 /* Deal with all non-character events. */
501 switch (evp->e_event) {
502 case E_CHARACTER:
503 break;
504 case E_ERR:
505 case E_EOF:
506 F_SET(sp, SC_EXIT_FORCE);
507 return (1);
508 case E_INTERRUPT:
510 * !!!
511 * Historically, <interrupt> exited the user from text input
512 * mode or cancelled a colon command, and returned to command
513 * mode. It also beeped the terminal, but that seems a bit
514 * excessive.
516 goto k_escape;
517 case E_REPAINT:
518 if (v_erepaint(sp, &ev))
519 return (1);
520 goto next;
521 case E_WRESIZE:
522 /* <resize> interrupts the input mode. */
523 v_emsg(sp, NULL, VIM_WRESIZE);
524 goto k_escape;
525 default:
526 v_event_err(sp, evp);
527 goto k_escape;
531 * !!!
532 * If the first character of the input is a nul, replay the previous
533 * input. (Historically, it's okay to replay non-existent input.)
534 * This was not documented as far as I know, and is a great test of vi
535 * clones.
537 if (rcol == 0 && !LF_ISSET(TXT_REPLAY) && evp->e_c == '\0') {
538 if (vip->rep == NULL)
539 goto done;
541 abb = AB_NOTSET;
542 LF_CLR(TXT_RECORD);
543 LF_SET(TXT_REPLAY);
544 goto replay;
548 * File name completion and colon command-line editing. We don't
549 * have enough meta characters, so we expect people to overload
550 * them. If the two characters are the same, then we do file name
551 * completion if the cursor is past the first column, and do colon
552 * command-line editing if it's not.
554 if (quote == Q_NOTSET) {
555 int L__cedit, L__filec;
557 L__cedit = L__filec = 0;
558 if (LF_ISSET(TXT_CEDIT) && O_STR(sp, O_CEDIT) != NULL &&
559 O_STR(sp, O_CEDIT)[0] == evp->e_c)
560 L__cedit = 1;
561 if (LF_ISSET(TXT_FILEC) && O_STR(sp, O_FILEC) != NULL &&
562 O_STR(sp, O_FILEC)[0] == evp->e_c)
563 L__filec = 1;
564 if (L__cedit == 1 && (L__filec == 0 || tp->cno == tp->offset)) {
565 tp->term = TERM_CEDIT;
566 goto k_escape;
568 if (L__filec == 1) {
569 if (txt_fc(sp, tp, &filec_redraw))
570 goto err;
571 goto resolve;
575 /* Abbreviation overflow check. See comment in txt_abbrev(). */
576 #define MAX_ABBREVIATION_EXPANSION 256
577 if (FL_ISSET(evp->e_flags, CH_ABBREVIATED)) {
578 if (++abcnt > MAX_ABBREVIATION_EXPANSION) {
579 if (v_event_flush(sp, CH_ABBREVIATED))
580 msgq(sp, M_ERR,
581 "191|Abbreviation exceeded expansion limit: characters discarded");
582 abcnt = 0;
583 if (LF_ISSET(TXT_REPLAY))
584 goto done;
585 goto resolve;
587 } else
588 abcnt = 0;
590 /* Check to see if the character fits into the replay buffers. */
591 if (LF_ISSET(TXT_RECORD)) {
592 BINC_GOTO(sp, EVENT, vip->rep,
593 vip->rep_len, (rcol + 1) * sizeof(EVENT));
594 vip->rep[rcol++] = *evp;
597 replay: if (LF_ISSET(TXT_REPLAY)) {
598 if (rcol == vip->rep_cnt)
599 goto k_escape;
600 evp = vip->rep + rcol++;
603 /* Wrapmargin check for leading space. */
604 if (wm_skip) {
605 wm_skip = 0;
606 if (evp->e_c == ' ')
607 goto resolve;
610 /* If quoted by someone else, simply insert the character. */
611 if (FL_ISSET(evp->e_flags, CH_QUOTED))
612 goto insq_ch;
615 * !!!
616 * If this character was quoted by a K_VLNEXT or a backslash, replace
617 * the placeholder (a carat or a backslash) with the new character.
618 * If it was quoted by a K_VLNEXT, we've already adjusted the cursor
619 * because it has to appear on top of the placeholder character. If
620 * it was quoted by a backslash, adjust the cursor now, the cursor
621 * doesn't appear on top of it. Historic practice in both cases.
623 * Skip tests for abbreviations; ":ab xa XA" followed by "ixa^V<space>"
624 * doesn't perform an abbreviation. Special case, ^V^J (not ^V^M) is
625 * the same as ^J, historically.
627 if (quote == Q_BTHIS || quote == Q_VTHIS) {
628 FL_CLR(ec_flags, EC_QUOTED);
629 if (LF_ISSET(TXT_MAPINPUT))
630 FL_SET(ec_flags, EC_MAPINPUT);
632 if (quote == Q_BTHIS &&
633 (evp->e_value == K_VERASE || evp->e_value == K_VKILL)) {
634 quote = Q_NOTSET;
635 --tp->cno;
636 ++tp->owrite;
637 goto insl_ch;
639 if (quote == Q_VTHIS && evp->e_value != K_NL) {
640 quote = Q_NOTSET;
641 goto insl_ch;
643 quote = Q_NOTSET;
647 * !!!
648 * Translate "<CH_HEX>[isxdigit()]*" to a character with a hex value:
649 * this test delimits the value by any non-hex character. Offset by
650 * one, we use 0 to mean that we've found <CH_HEX>.
652 if (hexcnt > 1 && !isxdigit(evp->e_c)) {
653 hexcnt = 0;
654 if (txt_hex(sp, tp))
655 goto err;
658 switch (evp->e_value) {
659 case K_CR: /* Carriage return. */
660 case K_NL: /* New line. */
661 /* Return in script windows and the command line. */
662 k_cr: if (LF_ISSET(TXT_CR)) {
663 static CHAR_T cr[] = { '\r', 0 };
665 * If this was a map, we may have not displayed
666 * the line. Display it, just in case.
668 * If a script window and not the colon line,
669 * push a <cr> so it gets executed.
671 if (LF_ISSET(TXT_INFOLINE)) {
672 if (vs_change(sp, tp->lno, LINE_RESET))
673 goto err;
674 } else if (F_ISSET(sp, SC_SCRIPT))
675 (void)v_event_push(sp, NULL, cr, 1, CH_NOMAP);
677 /* Set term condition: if empty. */
678 if (tp->cno <= tp->offset)
679 tp->term = TERM_CR;
681 * Set term condition: if searching incrementally and
682 * the user entered a pattern, return a completed
683 * search, regardless if the entire pattern was found.
685 if (FL_ISSET(is_flags, IS_RUNNING) &&
686 tp->cno >= tp->offset + 1)
687 tp->term = TERM_SEARCH;
689 goto k_escape;
692 #define LINE_RESOLVE { \
693 /* \
694 * Handle abbreviations. If there was one, discard the \
695 * replay characters. \
696 */ \
697 if (abb == AB_INWORD && \
698 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) { \
699 if (txt_abbrev(sp, tp, &evp->e_c, \
700 LF_ISSET(TXT_INFOLINE), &tmp, \
701 &ab_turnoff)) \
702 goto err; \
703 if (tmp) { \
704 if (LF_ISSET(TXT_RECORD)) \
705 rcol -= tmp + 1; \
706 goto resolve; \
709 if (abb != AB_NOTSET) \
710 abb = AB_NOTWORD; \
711 if (UNMAP_TST) \
712 txt_unmap(sp, tp, &ec_flags); \
713 /* \
714 * Delete any appended cursor. It's possible to get in \
715 * situations where TXT_APPENDEOL is set but tp->insert \
716 * is 0 when using the R command and all the characters \
717 * are tp->owrite characters. \
718 */ \
719 if (LF_ISSET(TXT_APPENDEOL) && tp->insert > 0) { \
720 --tp->len; \
721 --tp->insert; \
724 LINE_RESOLVE;
727 * Save the current line information for restoration in
728 * txt_backup(), and set the line final length.
730 tp->sv_len = tp->len;
731 tp->sv_cno = tp->cno;
732 tp->len = tp->cno;
734 /* Update the old line. */
735 if (vs_change(sp, tp->lno, LINE_RESET))
736 goto err;
739 * Historic practice, when the autoindent edit option was set,
740 * was to delete <blank> characters following the inserted
741 * newline. This affected the 'R', 'c', and 's' commands; 'c'
742 * and 's' retained the insert characters only, 'R' moved the
743 * overwrite and insert characters into the next TEXT structure.
744 * We keep track of the number of characters erased for the 'R'
745 * command so that the final resolution of the line is correct.
747 tp->R_erase = 0;
748 owrite = tp->owrite;
749 insert = tp->insert;
750 if (LF_ISSET(TXT_REPLACE) && owrite != 0) {
751 for (p = tp->lb + tp->cno; owrite > 0 && isblank(*p);
752 ++p, --owrite, ++tp->R_erase);
753 if (owrite == 0)
754 for (; insert > 0 && isblank(*p);
755 ++p, ++tp->R_erase, --insert);
756 } else {
757 p = tp->lb + tp->cno + owrite;
758 if (O_ISSET(sp, O_AUTOINDENT))
759 for (; insert > 0 &&
760 isblank(*p); ++p, --insert);
761 owrite = 0;
765 * !!!
766 * Create a new line and insert the new TEXT into the queue.
767 * DON'T insert until the old line has been updated, or the
768 * inserted line count in line.c:db_get() will be wrong.
770 if ((ntp = text_init(sp, p,
771 insert + owrite, insert + owrite + 32)) == NULL)
772 goto err;
773 CIRCLEQ_INSERT_TAIL(&sp->tiq, ntp, q);
775 /* Set up bookkeeping for the new line. */
776 ntp->insert = insert;
777 ntp->owrite = owrite;
778 ntp->lno = tp->lno + 1;
781 * Reset the autoindent line value. 0^D keeps the autoindent
782 * line from changing, ^D changes the level, even if there were
783 * no characters in the old line. Note, if using the current
784 * tp structure, use the cursor as the length, the autoindent
785 * characters may have been erased.
787 if (LF_ISSET(TXT_AUTOINDENT)) {
788 if (carat == C_NOCHANGE) {
789 if (v_txt_auto(sp, OOBLNO, &ait, ait.ai, ntp))
790 goto err;
791 FREE_SPACEW(sp, ait.lb, ait.lb_len);
792 } else
793 if (v_txt_auto(sp, OOBLNO, tp, tp->cno, ntp))
794 goto err;
795 carat = C_NOTSET;
798 /* Reset the cursor. */
799 ntp->cno = ntp->ai;
802 * If we're here because wrapmargin was set and we've broken a
803 * line, there may be additional information (i.e. the start of
804 * a line) in the wmt structure.
806 if (wm_set) {
807 if (wmt.offset != 0 ||
808 wmt.owrite != 0 || wmt.insert != 0) {
809 #define WMTSPACE wmt.offset + wmt.owrite + wmt.insert
810 BINC_GOTOW(sp, ntp->lb,
811 ntp->lb_len, ntp->len + WMTSPACE + 32);
812 MEMMOVEW(ntp->lb + ntp->cno, wmt.lb, WMTSPACE);
813 ntp->len += WMTSPACE;
814 ntp->cno += wmt.offset;
815 ntp->owrite = wmt.owrite;
816 ntp->insert = wmt.insert;
818 wm_set = 0;
821 /* New lines are TXT_APPENDEOL. */
822 if (ntp->owrite == 0 && ntp->insert == 0) {
823 BINC_GOTOW(sp, ntp->lb, ntp->lb_len, ntp->len + 1);
824 LF_SET(TXT_APPENDEOL);
825 ntp->lb[ntp->cno] = CH_CURSOR;
826 ++ntp->insert;
827 ++ntp->len;
830 /* Swap old and new TEXT's, and update the new line. */
831 tp = ntp;
832 if (vs_change(sp, tp->lno, LINE_INSERT))
833 goto err;
835 goto resolve;
836 case K_ESCAPE: /* Escape. */
837 if (!LF_ISSET(TXT_ESCAPE))
838 goto ins_ch;
840 /* If we have a count, start replaying the input. */
841 if (rcount > 1) {
842 --rcount;
844 vip->rep_cnt = rcol;
845 rcol = 0;
846 abb = AB_NOTSET;
847 LF_CLR(TXT_RECORD);
848 LF_SET(TXT_REPLAY);
851 * Some commands (e.g. 'o') need a <newline> for each
852 * repetition.
854 if (LF_ISSET(TXT_ADDNEWLINE))
855 goto k_cr;
858 * The R command turns into the 'a' command after the
859 * first repetition.
861 if (LF_ISSET(TXT_REPLACE)) {
862 tp->insert = tp->owrite;
863 tp->owrite = 0;
864 LF_CLR(TXT_REPLACE);
866 goto replay;
869 /* Set term condition: if empty. */
870 if (tp->cno <= tp->offset)
871 tp->term = TERM_ESC;
873 * Set term condition: if searching incrementally and the user
874 * entered a pattern, return a completed search, regardless if
875 * the entire pattern was found.
877 if (FL_ISSET(is_flags, IS_RUNNING) && tp->cno >= tp->offset + 1)
878 tp->term = TERM_SEARCH;
880 k_escape: LINE_RESOLVE;
883 * Clean up for the 'R' command, restoring overwrite
884 * characters, and making them into insert characters.
886 if (LF_ISSET(TXT_REPLACE))
887 txt_Rresolve(sp, &sp->tiq, tp, len);
890 * If there are any overwrite characters, copy down
891 * any insert characters, and decrement the length.
893 if (tp->owrite) {
894 if (tp->insert)
895 MEMMOVEW(tp->lb + tp->cno,
896 tp->lb + tp->cno + tp->owrite, tp->insert);
897 tp->len -= tp->owrite;
901 * Optionally resolve the lines into the file. If not
902 * resolving the lines into the file, end the line with
903 * a nul. If the line is empty, then set the length to
904 * 0, the termination condition has already been set.
906 * XXX
907 * This is wrong, should pass back a length.
909 if (LF_ISSET(TXT_RESOLVE)) {
910 if (txt_resolve(sp, &sp->tiq, flags))
911 goto err;
912 } else {
913 BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
914 tp->lb[tp->len] = '\0';
918 * Set the return cursor position to rest on the last
919 * inserted character.
921 if (tp->cno != 0)
922 --tp->cno;
924 /* Update the last line. */
925 if (vs_change(sp, tp->lno, LINE_RESET))
926 return (1);
927 goto done;
928 case K_CARAT: /* Delete autoindent chars. */
929 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
930 carat = C_CARATSET;
931 goto ins_ch;
932 case K_ZERO: /* Delete autoindent chars. */
933 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
934 carat = C_ZEROSET;
935 goto ins_ch;
936 case K_CNTRLD: /* Delete autoindent char. */
938 * If in the first column or no characters to erase, ignore
939 * the ^D (this matches historic practice). If not doing
940 * autoindent or already inserted non-ai characters, it's a
941 * literal. The latter test is done in the switch, as the
942 * CARAT forms are N + 1, not N.
944 if (!LF_ISSET(TXT_AUTOINDENT))
945 goto ins_ch;
946 if (tp->cno == 0)
947 goto resolve;
949 switch (carat) {
950 case C_CARATSET: /* ^^D */
951 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
952 goto ins_ch;
954 /* Save the ai string for later. */
955 ait.lb = NULL;
956 ait.lb_len = 0;
957 BINC_GOTOW(sp, ait.lb, ait.lb_len, tp->ai);
958 MEMMOVEW(ait.lb, tp->lb, tp->ai);
959 ait.ai = ait.len = tp->ai;
961 carat = C_NOCHANGE;
962 goto leftmargin;
963 case C_ZEROSET: /* 0^D */
964 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
965 goto ins_ch;
967 carat = C_NOTSET;
968 leftmargin: tp->lb[tp->cno - 1] = ' ';
969 tp->owrite += tp->cno - tp->offset;
970 tp->ai = 0;
971 tp->cno = tp->offset;
972 break;
973 case C_NOTSET: /* ^D */
974 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset)
975 goto ins_ch;
977 (void)txt_dent(sp, tp, 0);
978 break;
979 default:
980 abort();
982 break;
983 case K_VERASE: /* Erase the last character. */
984 /* If can erase over the prompt, return. */
985 if (tp->cno <= tp->offset && LF_ISSET(TXT_BS)) {
986 tp->term = TERM_BS;
987 goto done;
991 * If at the beginning of the line, try and drop back to a
992 * previously inserted line.
994 if (tp->cno == 0) {
995 if ((ntp =
996 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
997 goto err;
998 tp = ntp;
999 break;
1002 /* If nothing to erase, bell the user. */
1003 if (tp->cno <= tp->offset) {
1004 if (!LF_ISSET(TXT_REPLAY))
1005 txt_nomorech(sp);
1006 break;
1009 /* Drop back one character. */
1010 --tp->cno;
1013 * Historically, vi didn't replace the erased characters with
1014 * <blank>s, presumably because it's easier to fix a minor
1015 * typing mistake and continue on if the previous letters are
1016 * already there. This is a problem for incremental searching,
1017 * because the user can no longer tell where they are in the
1018 * colon command line because the cursor is at the last search
1019 * point in the screen. So, if incrementally searching, erase
1020 * the erased characters from the screen.
1022 if (FL_ISSET(is_flags, IS_RUNNING))
1023 tp->lb[tp->cno] = ' ';
1026 * Increment overwrite, decrement ai if deleted.
1028 * !!!
1029 * Historic vi did not permit users to use erase characters
1030 * to delete autoindent characters. We do. Eat hot death,
1031 * POSIX.
1033 ++tp->owrite;
1034 if (tp->cno < tp->ai)
1035 --tp->ai;
1037 /* Reset if we deleted an incremental search character. */
1038 if (FL_ISSET(is_flags, IS_RUNNING))
1039 FL_SET(is_flags, IS_RESTART);
1040 break;
1041 case K_VWERASE: /* Skip back one word. */
1043 * If at the beginning of the line, try and drop back to a
1044 * previously inserted line.
1046 if (tp->cno == 0) {
1047 if ((ntp =
1048 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1049 goto err;
1050 tp = ntp;
1054 * If at offset, nothing to erase so bell the user.
1056 if (tp->cno <= tp->offset) {
1057 if (!LF_ISSET(TXT_REPLAY))
1058 txt_nomorech(sp);
1059 break;
1063 * The first werase goes back to any autoindent column and the
1064 * second werase goes back to the offset.
1066 * !!!
1067 * Historic vi did not permit users to use erase characters to
1068 * delete autoindent characters.
1070 if (tp->ai && tp->cno > tp->ai)
1071 max = tp->ai;
1072 else {
1073 tp->ai = 0;
1074 max = tp->offset;
1077 /* Skip over trailing space characters. */
1078 while (tp->cno > max && isblank(tp->lb[tp->cno - 1])) {
1079 --tp->cno;
1080 ++tp->owrite;
1082 if (tp->cno == max)
1083 break;
1085 * There are three types of word erase found on UNIX systems.
1086 * They can be identified by how the string /a/b/c is treated
1087 * -- as 1, 3, or 6 words. Historic vi had two classes of
1088 * characters, and strings were delimited by them and
1089 * <blank>'s, so, 6 words. The historic tty interface used
1090 * <blank>'s to delimit strings, so, 1 word. The algorithm
1091 * offered in the 4.4BSD tty interface (as stty altwerase)
1092 * treats it as 3 words -- there are two classes of
1093 * characters, and strings are delimited by them and
1094 * <blank>'s. The difference is that the type of the first
1095 * erased character erased is ignored, which is exactly right
1096 * when erasing pathname components. The edit options
1097 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD tty
1098 * interface and the historic tty driver behavior,
1099 * respectively, and the default is the same as the historic
1100 * vi behavior.
1102 * Overwrite erased characters if doing incremental search;
1103 * see comment above.
1105 if (LF_ISSET(TXT_TTYWERASE))
1106 while (tp->cno > max) {
1107 --tp->cno;
1108 ++tp->owrite;
1109 if (FL_ISSET(is_flags, IS_RUNNING))
1110 tp->lb[tp->cno] = ' ';
1111 if (isblank(tp->lb[tp->cno - 1]))
1112 break;
1114 else {
1115 if (LF_ISSET(TXT_ALTWERASE)) {
1116 --tp->cno;
1117 ++tp->owrite;
1118 if (FL_ISSET(is_flags, IS_RUNNING))
1119 tp->lb[tp->cno] = ' ';
1120 if (isblank(tp->lb[tp->cno - 1]))
1121 break;
1123 if (tp->cno > max)
1124 tmp = inword(tp->lb[tp->cno - 1]);
1125 while (tp->cno > max) {
1126 --tp->cno;
1127 ++tp->owrite;
1128 if (FL_ISSET(is_flags, IS_RUNNING))
1129 tp->lb[tp->cno] = ' ';
1130 if (tmp != inword(tp->lb[tp->cno - 1])
1131 || isblank(tp->lb[tp->cno - 1]))
1132 break;
1136 /* Reset if we deleted an incremental search character. */
1137 if (FL_ISSET(is_flags, IS_RUNNING))
1138 FL_SET(is_flags, IS_RESTART);
1139 break;
1140 case K_VKILL: /* Restart this line. */
1142 * !!!
1143 * If at the beginning of the line, try and drop back to a
1144 * previously inserted line. Historic vi did not permit
1145 * users to go back to previous lines.
1147 if (tp->cno == 0) {
1148 if ((ntp =
1149 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1150 goto err;
1151 tp = ntp;
1154 /* If at offset, nothing to erase so bell the user. */
1155 if (tp->cno <= tp->offset) {
1156 if (!LF_ISSET(TXT_REPLAY))
1157 txt_nomorech(sp);
1158 break;
1162 * First kill goes back to any autoindent and second kill goes
1163 * back to the offset.
1165 * !!!
1166 * Historic vi did not permit users to use erase characters to
1167 * delete autoindent characters.
1169 if (tp->ai && tp->cno > tp->ai)
1170 max = tp->ai;
1171 else {
1172 tp->ai = 0;
1173 max = tp->offset;
1175 tp->owrite += tp->cno - max;
1178 * Overwrite erased characters if doing incremental search;
1179 * see comment above.
1181 if (FL_ISSET(is_flags, IS_RUNNING))
1182 do {
1183 tp->lb[--tp->cno] = ' ';
1184 } while (tp->cno > max);
1185 else
1186 tp->cno = max;
1188 /* Reset if we deleted an incremental search character. */
1189 if (FL_ISSET(is_flags, IS_RUNNING))
1190 FL_SET(is_flags, IS_RESTART);
1191 break;
1192 case K_CNTRLT: /* Add autoindent characters. */
1193 if (!LF_ISSET(TXT_CNTRLT))
1194 goto ins_ch;
1195 if (txt_dent(sp, tp, 1))
1196 goto err;
1197 goto ebuf_chk;
1198 case K_RIGHTBRACE:
1199 case K_RIGHTPAREN:
1200 if (LF_ISSET(TXT_SHOWMATCH))
1201 showmatch = 1;
1202 goto ins_ch;
1203 case K_BACKSLASH: /* Quote next erase/kill. */
1205 * !!!
1206 * Historic vi tried to make abbreviations after a backslash
1207 * escape work. If you did ":ab x y", and inserted "x\^H",
1208 * (assuming the erase character was ^H) you got "x^H", and
1209 * no abbreviation was done. If you inserted "x\z", however,
1210 * it tried to back up and do the abbreviation, i.e. replace
1211 * 'x' with 'y'. The problem was it got it wrong, and you
1212 * ended up with "zy\".
1214 * This is really hard to do (you have to remember the
1215 * word/non-word state, for example), and doesn't make any
1216 * sense to me. Both backslash and the characters it
1217 * (usually) escapes will individually trigger the
1218 * abbreviation, so I don't see why the combination of them
1219 * wouldn't. I don't expect to get caught on this one,
1220 * particularly since it never worked right, but I've been
1221 * wrong before.
1223 * Do the tests for abbreviations, so ":ab xa XA",
1224 * "ixa\<K_VERASE>" performs the abbreviation.
1226 quote = Q_BNEXT;
1227 goto insq_ch;
1228 case K_VLNEXT: /* Quote next character. */
1229 evp->e_c = '^';
1230 quote = Q_VNEXT;
1232 * Turn on the quote flag so that the underlying routines
1233 * quote the next character where it's possible. Turn off
1234 * the input mapbiting flag so that we don't remap the next
1235 * character.
1237 FL_SET(ec_flags, EC_QUOTED);
1238 FL_CLR(ec_flags, EC_MAPINPUT);
1241 * !!!
1242 * Skip the tests for abbreviations, so ":ab xa XA",
1243 * "ixa^V<space>" doesn't perform the abbreviation.
1245 goto insl_ch;
1246 case K_HEXCHAR:
1247 hexcnt = 1;
1248 goto insq_ch;
1249 default: /* Insert the character. */
1250 ins_ch: /*
1251 * Historically, vi eliminated nul's out of hand. If the
1252 * beautify option was set, it also deleted any unknown
1253 * ASCII value less than space (040) and the del character
1254 * (0177), except for tabs. Unknown is a key word here.
1255 * Most vi documentation claims that it deleted everything
1256 * but <tab>, <nl> and <ff>, as that's what the original
1257 * 4BSD documentation said. This is obviously wrong,
1258 * however, as <esc> would be included in that list. What
1259 * we do is eliminate any unquoted, iscntrl() character that
1260 * wasn't a replay and wasn't handled specially, except
1261 * <tab> or <ff>.
1263 if (LF_ISSET(TXT_BEAUTIFY) && ISCNTRL(evp->e_c) &&
1264 evp->e_value != K_FORMFEED && evp->e_value != K_TAB) {
1265 msgq(sp, M_BERR,
1266 "192|Illegal character; quote to enter");
1267 if (LF_ISSET(TXT_REPLAY))
1268 goto done;
1269 break;
1272 insq_ch: /*
1273 * If entering a non-word character after a word, check for
1274 * abbreviations. If there was one, discard replay characters.
1275 * If entering a blank character, check for unmap commands,
1276 * as well.
1278 if (!inword(evp->e_c)) {
1279 if (abb == AB_INWORD &&
1280 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) {
1281 if (txt_abbrev(sp, tp, &evp->e_c,
1282 LF_ISSET(TXT_INFOLINE), &tmp, &ab_turnoff))
1283 goto err;
1284 if (tmp) {
1285 if (LF_ISSET(TXT_RECORD))
1286 rcol -= tmp + 1;
1287 goto resolve;
1290 if (isblank(evp->e_c) && UNMAP_TST)
1291 txt_unmap(sp, tp, &ec_flags);
1293 if (abb != AB_NOTSET)
1294 abb = inword(evp->e_c) ? AB_INWORD : AB_NOTWORD;
1296 insl_ch: if (txt_insch(sp, tp, &evp->e_c, flags))
1297 goto err;
1300 * If we're using K_VLNEXT to quote the next character, then
1301 * we want the cursor to position itself on the ^ placeholder
1302 * we're displaying, to match historic practice.
1304 if (quote == Q_VNEXT) {
1305 --tp->cno;
1306 ++tp->owrite;
1310 * !!!
1311 * Translate "<CH_HEX>[isxdigit()]*" to a character with
1312 * a hex value: this test delimits the value by the max
1313 * number of hex bytes. Offset by one, we use 0 to mean
1314 * that we've found <CH_HEX>.
1316 if (hexcnt != 0 && hexcnt++ == sizeof(CHAR_T) * 2 + 1) {
1317 hexcnt = 0;
1318 if (txt_hex(sp, tp))
1319 goto err;
1323 * Check to see if we've crossed the margin.
1325 * !!!
1326 * In the historic vi, the wrapmargin value was figured out
1327 * using the display widths of the characters, i.e. <tab>
1328 * characters were counted as two characters if the list edit
1329 * option is set, but as the tabstop edit option number of
1330 * characters otherwise. That's what the vs_column() function
1331 * gives us, so we use it.
1333 if (margin != 0) {
1334 if (vs_column(sp, &tcol))
1335 goto err;
1336 if (tcol >= margin) {
1337 if (txt_margin(sp, tp, &wmt, &tmp, flags))
1338 goto err;
1339 if (tmp) {
1340 if (isblank(evp->e_c))
1341 wm_skip = 1;
1342 wm_set = 1;
1343 goto k_cr;
1349 * If we've reached the end of the buffer, then we need to
1350 * switch into insert mode. This happens when there's a
1351 * change to a mark and the user puts in more characters than
1352 * the length of the motion.
1354 ebuf_chk: if (tp->cno >= tp->len) {
1355 BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
1356 LF_SET(TXT_APPENDEOL);
1358 tp->lb[tp->cno] = CH_CURSOR;
1359 ++tp->insert;
1360 ++tp->len;
1363 /* Step the quote state forward. */
1364 if (quote != Q_NOTSET) {
1365 if (quote == Q_BNEXT)
1366 quote = Q_BTHIS;
1367 if (quote == Q_VNEXT)
1368 quote = Q_VTHIS;
1370 break;
1373 #ifdef DEBUG
1374 if (tp->cno + tp->insert + tp->owrite != tp->len) {
1375 msgq(sp, M_ERR,
1376 "len %u != cno: %u ai: %u insert %u overwrite %u",
1377 tp->len, tp->cno, tp->ai, tp->insert, tp->owrite);
1378 if (LF_ISSET(TXT_REPLAY))
1379 goto done;
1380 tp->len = tp->cno + tp->insert + tp->owrite;
1382 #endif
1384 resolve:/*
1385 * 1: If we don't need to know where the cursor really is and we're
1386 * replaying text, keep going.
1388 if (margin == 0 && LF_ISSET(TXT_REPLAY))
1389 goto replay;
1392 * 2: Reset the line. Don't bother unless we're about to wait on
1393 * a character or we need to know where the cursor really is.
1394 * We have to do this before showing matching characters so the
1395 * user can see what they're matching.
1397 if ((margin != 0 || !KEYS_WAITING(sp)) &&
1398 vs_change(sp, tp->lno, LINE_RESET))
1399 return (1);
1402 * 3: If there aren't keys waiting, display the matching character.
1403 * We have to do this before resolving any messages, otherwise
1404 * the error message from a missing match won't appear correctly.
1406 if (showmatch) {
1407 if (!KEYS_WAITING(sp) && txt_showmatch(sp, tp))
1408 return (1);
1409 showmatch = 0;
1413 * 4: If there have been messages and we're not editing on the colon
1414 * command line or doing file name completion, resolve them.
1416 if ((vip->totalcount != 0 || F_ISSET(gp, G_BELLSCHED)) &&
1417 !F_ISSET(sp, SC_TINPUT_INFO) && !filec_redraw &&
1418 vs_resolve(sp, NULL, 0))
1419 return (1);
1422 * 5: Refresh the screen if we're about to wait on a character or we
1423 * need to know where the cursor really is.
1425 if (margin != 0 || !KEYS_WAITING(sp)) {
1426 UPDATE_POSITION(sp, tp);
1427 if (vs_refresh(sp, margin != 0))
1428 return (1);
1431 /* 6: Proceed with the incremental search. */
1432 if (FL_ISSET(is_flags, IS_RUNNING) && txt_isrch(sp, vp, tp, &is_flags))
1433 return (1);
1435 /* 7: Next character... */
1436 if (LF_ISSET(TXT_REPLAY))
1437 goto replay;
1438 goto next;
1440 done: /* Leave input mode. */
1441 F_CLR(sp, SC_TINPUT);
1443 /* If recording for playback, save it. */
1444 if (LF_ISSET(TXT_RECORD))
1445 vip->rep_cnt = rcol;
1448 * If not working on the colon command line, set the final cursor
1449 * position.
1451 if (!F_ISSET(sp, SC_TINPUT_INFO)) {
1452 vp->m_final.lno = tp->lno;
1453 vp->m_final.cno = tp->cno;
1455 return (0);
1457 err:
1458 alloc_err:
1459 F_CLR(sp, SC_TINPUT);
1460 txt_err(sp, &sp->tiq);
1461 return (1);
1465 * txt_abbrev --
1466 * Handle abbreviations.
1468 static int
1469 txt_abbrev(SCR *sp, TEXT *tp, CHAR_T *pushcp, int isinfoline, int *didsubp, int *turnoffp)
1471 VI_PRIVATE *vip;
1472 CHAR_T ch, *p;
1473 SEQ *qp;
1474 size_t len, off;
1476 /* Check to make sure we're not at the start of an append. */
1477 *didsubp = 0;
1478 if (tp->cno == tp->offset)
1479 return (0);
1481 vip = VIP(sp);
1484 * Find the start of the "word".
1486 * !!!
1487 * We match historic practice, which, as far as I can tell, had an
1488 * off-by-one error. The way this worked was that when the inserted
1489 * text switched from a "word" character to a non-word character,
1490 * vi would check for possible abbreviations. It would then take the
1491 * type (i.e. word/non-word) of the character entered TWO characters
1492 * ago, and move backward in the text until reaching a character that
1493 * was not that type, or the beginning of the insert, the line, or
1494 * the file. For example, in the string "abc<space>", when the <space>
1495 * character triggered the abbreviation check, the type of the 'b'
1496 * character was used for moving through the string. Maybe there's a
1497 * reason for not using the first (i.e. 'c') character, but I can't
1498 * think of one.
1500 * Terminate at the beginning of the insert or the character after the
1501 * offset character -- both can be tested for using tp->offset.
1503 off = tp->cno - 1; /* Previous character. */
1504 p = tp->lb + off;
1505 len = 1; /* One character test. */
1506 if (off == tp->offset || isblank(p[-1]))
1507 goto search;
1508 if (inword(p[-1])) /* Move backward to change. */
1509 for (;;) {
1510 --off; --p; ++len;
1511 if (off == tp->offset || !inword(p[-1]))
1512 break;
1514 else
1515 for (;;) {
1516 --off; --p; ++len;
1517 if (off == tp->offset ||
1518 inword(p[-1]) || isblank(p[-1]))
1519 break;
1523 * !!!
1524 * Historic vi exploded abbreviations on the command line. This has
1525 * obvious problems in that unabbreviating the string can be extremely
1526 * tricky, particularly if the string has, say, an embedded escape
1527 * character. Personally, I think it's a stunningly bad idea. Other
1528 * examples of problems this caused in historic vi are:
1529 * :ab foo bar
1530 * :ab foo baz
1531 * results in "bar" being abbreviated to "baz", which wasn't what the
1532 * user had in mind at all. Also, the commands:
1533 * :ab foo bar
1534 * :unab foo<space>
1535 * resulted in an error message that "bar" wasn't mapped. Finally,
1536 * since the string was already exploded by the time the unabbreviate
1537 * command got it, all it knew was that an abbreviation had occurred.
1538 * Cleverly, it checked the replacement string for its unabbreviation
1539 * match, which meant that the commands:
1540 * :ab foo1 bar
1541 * :ab foo2 bar
1542 * :unab foo2
1543 * unabbreviate "foo1", and the commands:
1544 * :ab foo bar
1545 * :ab bar baz
1546 * unabbreviate "foo"!
1548 * Anyway, people neglected to first ask my opinion before they wrote
1549 * macros that depend on this stuff, so, we make this work as follows.
1550 * When checking for an abbreviation on the command line, if we get a
1551 * string which is <blank> terminated and which starts at the beginning
1552 * of the line, we check to see it is the abbreviate or unabbreviate
1553 * commands. If it is, turn abbreviations off and return as if no
1554 * abbreviation was found. Note also, minor trickiness, so that if
1555 * the user erases the line and starts another command, we turn the
1556 * abbreviations back on.
1558 * This makes the layering look like a Nachos Supreme.
1560 search: if (isinfoline)
1561 if (off == tp->ai || off == tp->offset)
1562 if (ex_is_abbrev(sp, p, len)) {
1563 *turnoffp = 1;
1564 return (0);
1565 } else
1566 *turnoffp = 0;
1567 else
1568 if (*turnoffp)
1569 return (0);
1571 /* Check for any abbreviations. */
1572 if ((qp = seq_find(sp, NULL, NULL, p, len, SEQ_ABBREV, NULL)) == NULL)
1573 return (0);
1576 * Push the abbreviation onto the tty stack. Historically, characters
1577 * resulting from an abbreviation expansion were themselves subject to
1578 * map expansions, O_SHOWMATCH matching etc. This means the expanded
1579 * characters will be re-tested for abbreviations. It's difficult to
1580 * know what historic practice in this case was, since abbreviations
1581 * were applied to :colon command lines, so entering abbreviations that
1582 * looped was tricky, although possible. In addition, obvious loops
1583 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
1584 * silently only implement and/or display the last abbreviation.)
1586 * This implementation doesn't recover well from such abbreviations.
1587 * The main input loop counts abbreviated characters, and, when it
1588 * reaches a limit, discards any abbreviated characters on the queue.
1589 * It's difficult to back up to the original position, as the replay
1590 * queue would have to be adjusted, and the line state when an initial
1591 * abbreviated character was received would have to be saved.
1593 ch = *pushcp;
1594 if (v_event_push(sp, NULL, &ch, 1, CH_ABBREVIATED))
1595 return (1);
1596 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_ABBREVIATED))
1597 return (1);
1600 * If the size of the abbreviation is larger than or equal to the size
1601 * of the original text, move to the start of the replaced characters,
1602 * and add their length to the overwrite count.
1604 * If the abbreviation is smaller than the original text, we have to
1605 * delete the additional overwrite characters and copy down any insert
1606 * characters.
1608 tp->cno -= len;
1609 if (qp->olen >= len)
1610 tp->owrite += len;
1611 else {
1612 if (tp->insert)
1613 MEMMOVEW(tp->lb + tp->cno + qp->olen,
1614 tp->lb + tp->cno + tp->owrite + len, tp->insert);
1615 tp->owrite += qp->olen;
1616 tp->len -= len - qp->olen;
1620 * We return the length of the abbreviated characters. This is so
1621 * the calling routine can replace the replay characters with the
1622 * abbreviation. This means that subsequent '.' commands will produce
1623 * the same text, regardless of intervening :[un]abbreviate commands.
1624 * This is historic practice.
1626 *didsubp = len;
1627 return (0);
1631 * txt_unmap --
1632 * Handle the unmap command.
1634 static void
1635 txt_unmap(SCR *sp, TEXT *tp, u_int32_t *ec_flagsp)
1637 size_t len, off;
1638 CHAR_T *p;
1640 /* Find the beginning of this "word". */
1641 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1642 if (isblank(*p)) {
1643 ++p;
1644 break;
1646 ++len;
1647 if (off == tp->ai || off == tp->offset)
1648 break;
1652 * !!!
1653 * Historic vi exploded input mappings on the command line. See the
1654 * txt_abbrev() routine for an explanation of the problems inherent
1655 * in this.
1657 * We make this work as follows. If we get a string which is <blank>
1658 * terminated and which starts at the beginning of the line, we check
1659 * to see it is the unmap command. If it is, we return that the input
1660 * mapping should be turned off. Note also, minor trickiness, so that
1661 * if the user erases the line and starts another command, we go ahead
1662 * an turn mapping back on.
1664 if ((off == tp->ai || off == tp->offset) && ex_is_unmap(sp, p, len))
1665 FL_CLR(*ec_flagsp, EC_MAPINPUT);
1666 else
1667 FL_SET(*ec_flagsp, EC_MAPINPUT);
1671 * txt_ai_resolve --
1672 * When a line is resolved by <esc>, review autoindent characters.
1674 static void
1675 txt_ai_resolve(SCR *sp, TEXT *tp, int *changedp)
1677 u_long ts;
1678 int del;
1679 size_t cno, len, new, old, scno, spaces, tab_after_sp, tabs;
1680 CHAR_T *p;
1682 *changedp = 0;
1685 * If the line is empty, has an offset, or no autoindent
1686 * characters, we're done.
1688 if (!tp->len || tp->offset || !tp->ai)
1689 return;
1692 * If the length is less than or equal to the autoindent
1693 * characters, delete them.
1695 if (tp->len <= tp->ai) {
1696 tp->ai = tp->cno = tp->len = 0;
1697 return;
1701 * The autoindent characters plus any leading <blank> characters
1702 * in the line are resolved into the minimum number of characters.
1703 * Historic practice.
1705 ts = O_VAL(sp, O_TABSTOP);
1707 /* Figure out the last <blank> screen column. */
1708 for (p = tp->lb, scno = 0, len = tp->len,
1709 spaces = tab_after_sp = 0; len-- && isblank(*p); ++p)
1710 if (*p == '\t') {
1711 if (spaces)
1712 tab_after_sp = 1;
1713 scno += COL_OFF(scno, ts);
1714 } else {
1715 ++spaces;
1716 ++scno;
1720 * If there are no spaces, or no tabs after spaces and less than
1721 * ts spaces, it's already minimal.
1723 if (!spaces || !tab_after_sp && spaces < ts)
1724 return;
1726 /* Count up spaces/tabs needed to get to the target. */
1727 for (cno = 0, tabs = 0; cno + COL_OFF(cno, ts) <= scno; ++tabs)
1728 cno += COL_OFF(cno, ts);
1729 spaces = scno - cno;
1732 * Figure out how many characters we're dropping -- if we're not
1733 * dropping any, it's already minimal, we're done.
1735 old = p - tp->lb;
1736 new = spaces + tabs;
1737 if (old == new)
1738 return;
1740 /* Shift the rest of the characters down, adjust the counts. */
1741 del = old - new;
1742 MEMMOVEW(p - del, p, tp->len - old);
1743 tp->len -= del;
1744 tp->cno -= del;
1746 /* Fill in space/tab characters. */
1747 for (p = tp->lb; tabs--;)
1748 *p++ = '\t';
1749 while (spaces--)
1750 *p++ = ' ';
1751 *changedp = 1;
1755 * v_txt_auto --
1756 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1757 * retrieve the line.
1759 * PUBLIC: int v_txt_auto __P((SCR *, db_recno_t, TEXT *, size_t, TEXT *));
1762 v_txt_auto(SCR *sp, db_recno_t lno, TEXT *aitp, size_t len, TEXT *tp)
1764 size_t nlen;
1765 CHAR_T *p, *t;
1767 if (aitp == NULL) {
1769 * If the ex append command is executed with an address of 0,
1770 * it's possible to get here with a line number of 0. Return
1771 * an indent of 0.
1773 if (lno == 0) {
1774 tp->ai = 0;
1775 return (0);
1777 if (db_get(sp, lno, DBG_FATAL, &t, &len))
1778 return (1);
1779 } else
1780 t = aitp->lb;
1782 /* Count whitespace characters. */
1783 for (p = t; len > 0; ++p, --len)
1784 if (!isblank(*p))
1785 break;
1787 /* Set count, check for no indentation. */
1788 if ((nlen = (p - t)) == 0)
1789 return (0);
1791 /* Make sure the buffer's big enough. */
1792 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
1794 /* Copy the buffer's current contents up. */
1795 if (tp->len != 0)
1796 MEMMOVEW(tp->lb + nlen, tp->lb, tp->len);
1797 tp->len += nlen;
1799 /* Copy the indentation into the new buffer. */
1800 MEMMOVEW(tp->lb, t, nlen);
1802 /* Set the autoindent count. */
1803 tp->ai = nlen;
1804 return (0);
1808 * txt_backup --
1809 * Back up to the previously edited line.
1811 static TEXT *
1812 txt_backup(SCR *sp, TEXTH *tiqh, TEXT *tp, u_int32_t *flagsp)
1814 VI_PRIVATE *vip;
1815 TEXT *ntp;
1817 /* Get a handle on the previous TEXT structure. */
1818 if ((ntp = tp->q.cqe_prev) == (void *)tiqh) {
1819 if (!FL_ISSET(*flagsp, TXT_REPLAY))
1820 msgq(sp, M_BERR,
1821 "193|Already at the beginning of the insert");
1822 return (tp);
1825 /* Bookkeeping. */
1826 ntp->len = ntp->sv_len;
1828 /* Handle appending to the line. */
1829 vip = VIP(sp);
1830 if (ntp->owrite == 0 && ntp->insert == 0) {
1831 ntp->lb[ntp->len] = CH_CURSOR;
1832 ++ntp->insert;
1833 ++ntp->len;
1834 FL_SET(*flagsp, TXT_APPENDEOL);
1835 } else
1836 FL_CLR(*flagsp, TXT_APPENDEOL);
1838 /* Release the current TEXT. */
1839 CIRCLEQ_REMOVE(tiqh, tp, q);
1840 text_free(tp);
1842 /* Update the old line on the screen. */
1843 if (vs_change(sp, ntp->lno + 1, LINE_DELETE))
1844 return (NULL);
1846 /* Return the new/current TEXT. */
1847 return (ntp);
1851 * Text indentation is truly strange. ^T and ^D do movements to the next or
1852 * previous shiftwidth value, i.e. for a 1-based numbering, with shiftwidth=3,
1853 * ^T moves a cursor on the 7th, 8th or 9th column to the 10th column, and ^D
1854 * moves it back.
1856 * !!!
1857 * The ^T and ^D characters in historical vi had special meaning only when they
1858 * were the first characters entered after entering text input mode. As normal
1859 * erase characters couldn't erase autoindent characters (^T in this case), it
1860 * meant that inserting text into previously existing text was strange -- ^T
1861 * only worked if it was the first keystroke(s), and then could only be erased
1862 * using ^D. This implementation treats ^T specially anywhere it occurs in the
1863 * input, and permits the standard erase characters to erase the characters it
1864 * inserts.
1866 * !!!
1867 * A fun test is to try:
1868 * :se sw=4 ai list
1869 * i<CR>^Tx<CR>^Tx<CR>^Tx<CR>^Dx<CR>^Dx<CR>^Dx<esc>
1870 * Historic vi loses some of the '$' marks on the line ends, but otherwise gets
1871 * it right.
1873 * XXX
1874 * Technically, txt_dent should be part of the screen interface, as it requires
1875 * knowledge of character sizes, including <space>s, on the screen. It's here
1876 * because it's a complicated little beast, and I didn't want to shove it down
1877 * into the screen. It's probable that KEY_LEN will call into the screen once
1878 * there are screens with different character representations.
1880 * txt_dent --
1881 * Handle ^T indents, ^D outdents.
1883 * If anything changes here, check the ex version to see if it needs similar
1884 * changes.
1886 static int
1887 txt_dent(SCR *sp, TEXT *tp, int isindent)
1889 CHAR_T ch;
1890 u_long sw, ts;
1891 size_t cno, current, spaces, target, tabs, off;
1892 int ai_reset;
1894 ts = O_VAL(sp, O_TABSTOP);
1895 sw = O_VAL(sp, O_SHIFTWIDTH);
1898 * Since we don't know what precedes the character(s) being inserted
1899 * (or deleted), the preceding whitespace characters must be resolved.
1900 * An example is a <tab>, which doesn't need a full shiftwidth number
1901 * of columns because it's preceded by <space>s. This is easy to get
1902 * if the user sets shiftwidth to a value less than tabstop (or worse,
1903 * something for which tabstop isn't a multiple) and then uses ^T to
1904 * indent, and ^D to outdent.
1906 * Figure out the current and target screen columns. In the historic
1907 * vi, the autoindent column was NOT determined using display widths
1908 * of characters as was the wrapmargin column. For that reason, we
1909 * can't use the vs_column() function, but have to calculate it here.
1910 * This is slow, but it's normally only on the first few characters of
1911 * a line.
1913 for (current = cno = 0; cno < tp->cno; ++cno)
1914 current += tp->lb[cno] == '\t' ?
1915 COL_OFF(current, ts) : KEY_COL(sp, tp->lb[cno]);
1917 target = current;
1918 if (isindent)
1919 target += COL_OFF(target, sw);
1920 else
1921 target -= --target % sw;
1924 * The AI characters will be turned into overwrite characters if the
1925 * cursor immediately follows them. We test both the cursor position
1926 * and the indent flag because there's no single test. (^T can only
1927 * be detected by the cursor position, and while we know that the test
1928 * is always true for ^D, the cursor can be in more than one place, as
1929 * "0^D" and "^D" are different.)
1931 ai_reset = !isindent || tp->cno == tp->ai + tp->offset;
1934 * Back up over any previous <blank> characters, changing them into
1935 * overwrite characters (including any ai characters). Then figure
1936 * out the current screen column.
1938 for (; tp->cno > tp->offset &&
1939 (tp->lb[tp->cno - 1] == ' ' || tp->lb[tp->cno - 1] == '\t');
1940 --tp->cno, ++tp->owrite);
1941 for (current = cno = 0; cno < tp->cno; ++cno)
1942 current += tp->lb[cno] == '\t' ?
1943 COL_OFF(current, ts) : KEY_COL(sp, tp->lb[cno]);
1946 * If we didn't move up to or past the target, it's because there
1947 * weren't enough characters to delete, e.g. the first character
1948 * of the line was a tp->offset character, and the user entered
1949 * ^D to move to the beginning of a line. An example of this is:
1951 * :set ai sw=4<cr>i<space>a<esc>i^T^D
1953 * Otherwise, count up the total spaces/tabs needed to get from the
1954 * beginning of the line (or the last non-<blank> character) to the
1955 * target.
1957 if (current >= target)
1958 spaces = tabs = 0;
1959 else {
1960 for (cno = current,
1961 tabs = 0; cno + COL_OFF(cno, ts) <= target; ++tabs)
1962 cno += COL_OFF(cno, ts);
1963 spaces = target - cno;
1966 /* If we overwrote ai characters, reset the ai count. */
1967 if (ai_reset)
1968 tp->ai = tabs + spaces;
1971 * Call txt_insch() to insert each character, so that we get the
1972 * correct effect when we add a <tab> to replace N <spaces>.
1974 for (ch = '\t'; tabs > 0; --tabs)
1975 (void)txt_insch(sp, tp, &ch, 0);
1976 for (ch = ' '; spaces > 0; --spaces)
1977 (void)txt_insch(sp, tp, &ch, 0);
1978 return (0);
1982 * txt_fc --
1983 * File name completion.
1985 static int
1986 txt_fc(SCR *sp, TEXT *tp, int *redrawp)
1988 struct stat sb;
1989 ARGS **argv;
1990 CHAR_T s_ch;
1991 EXCMD cmd;
1992 size_t indx, len, nlen, off;
1993 int argc, trydir;
1994 CHAR_T *p, *t;
1995 char *np;
1996 size_t nplen;
1998 trydir = 0;
1999 *redrawp = 0;
2002 * Find the beginning of this "word" -- if we're at the beginning
2003 * of the line, it's a special case.
2005 if (tp->cno == 1) {
2006 len = 0;
2007 p = tp->lb;
2008 } else
2009 retry: for (len = 0,
2010 off = tp->cno - 1, p = tp->lb + off;; --off, --p) {
2011 if (isblank(*p)) {
2012 ++p;
2013 break;
2015 ++len;
2016 if (off == tp->ai || off == tp->offset)
2017 break;
2021 * Get enough space for a wildcard character.
2023 * XXX
2024 * This won't work for "foo\", since the \ will escape the expansion
2025 * character. I'm not sure if that's a bug or not...
2027 off = p - tp->lb;
2028 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2029 p = tp->lb + off;
2031 s_ch = p[len];
2032 p[len] = '*';
2034 /* Build an ex command, and call the ex expansion routines. */
2035 ex_cinit(sp, &cmd, 0, 0, OOBLNO, OOBLNO, 0);
2036 if (argv_exp2(sp, &cmd, p, len + 1)) {
2037 p[len] = s_ch;
2038 return (0);
2040 argc = cmd.argc;
2041 argv = cmd.argv;
2043 p[len] = s_ch;
2045 switch (argc) {
2046 case 0: /* No matches. */
2047 if (!trydir)
2048 (void)sp->gp->scr_bell(sp);
2049 return (0);
2050 case 1: /* One match. */
2051 /* If something changed, do the exchange. */
2052 nlen = STRLEN(cmd.argv[0]->bp);
2053 if (len != nlen || MEMCMP(cmd.argv[0]->bp, p, len))
2054 break;
2056 /* If haven't done a directory test, do it now. */
2057 INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1,
2058 np, nplen);
2059 if (!trydir &&
2060 !stat(np, &sb) && S_ISDIR(sb.st_mode)) {
2061 p += len;
2062 goto isdir;
2065 /* If nothing changed, period, ring the bell. */
2066 if (!trydir)
2067 (void)sp->gp->scr_bell(sp);
2068 return (0);
2069 default: /* Multiple matches. */
2070 *redrawp = 1;
2071 if (txt_fc_col(sp, argc, argv))
2072 return (1);
2074 /* Find the length of the shortest match. */
2075 for (nlen = cmd.argv[0]->len; --argc > 0;) {
2076 if (cmd.argv[argc]->len < nlen)
2077 nlen = cmd.argv[argc]->len;
2078 for (indx = 0; indx < nlen &&
2079 cmd.argv[argc]->bp[indx] == cmd.argv[0]->bp[indx];
2080 ++indx);
2081 nlen = indx;
2083 break;
2086 /* Overwrite the expanded text first. */
2087 for (t = cmd.argv[0]->bp; len > 0 && nlen > 0; --len, --nlen)
2088 *p++ = *t++;
2090 /* If lost text, make the remaining old text overwrite characters. */
2091 if (len) {
2092 tp->cno -= len;
2093 tp->owrite += len;
2096 /* Overwrite any overwrite characters next. */
2097 for (; nlen > 0 && tp->owrite > 0; --nlen, --tp->owrite, ++tp->cno)
2098 *p++ = *t++;
2100 /* Shift remaining text up, and move the cursor to the end. */
2101 if (nlen) {
2102 off = p - tp->lb;
2103 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
2104 p = tp->lb + off;
2106 tp->cno += nlen;
2107 tp->len += nlen;
2109 if (tp->insert != 0)
2110 (void)MEMMOVEW(p + nlen, p, tp->insert);
2111 while (nlen--)
2112 *p++ = *t++;
2115 /* If a single match and it's a directory, retry it. */
2116 INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1, np, nplen);
2117 if (argc == 1 && !stat(np, &sb) && S_ISDIR(sb.st_mode)) {
2118 isdir: if (tp->owrite == 0) {
2119 off = p - tp->lb;
2120 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2121 p = tp->lb + off;
2122 if (tp->insert != 0)
2123 (void)MEMMOVEW(p + 1, p, tp->insert);
2124 ++tp->len;
2125 } else
2126 --tp->owrite;
2128 ++tp->cno;
2129 *p++ = '/';
2131 trydir = 1;
2132 goto retry;
2134 return (0);
2138 * txt_fc_col --
2139 * Display file names for file name completion.
2141 static int
2142 txt_fc_col(SCR *sp, int argc, ARGS **argv)
2144 ARGS **av;
2145 CHAR_T *p;
2146 GS *gp;
2147 size_t base, cnt, col, colwidth, numrows, numcols, prefix, row;
2148 int ac, nf, reset;
2149 char *np, *pp;
2150 size_t nlen;
2152 gp = sp->gp;
2154 /* Trim any directory prefix common to all of the files. */
2155 INT2CHAR(sp, argv[0]->bp, argv[0]->len + 1, np, nlen);
2156 if ((pp = strrchr(np, '/')) == NULL)
2157 prefix = 0;
2158 else {
2159 prefix = (pp - np) + 1;
2160 for (ac = argc - 1, av = argv + 1; ac > 0; --ac, ++av)
2161 if (av[0]->len < prefix ||
2162 MEMCMP(av[0]->bp, argv[0]->bp,
2163 prefix)) {
2164 prefix = 0;
2165 break;
2170 * Figure out the column width for the longest name. Output is done on
2171 * 6 character "tab" boundaries for no particular reason. (Since we
2172 * don't output tab characters, we ignore the terminal's tab settings.)
2173 * Ignore the user's tab setting because we have no idea how reasonable
2174 * it is.
2176 for (ac = argc, av = argv, colwidth = 0; ac > 0; --ac, ++av) {
2177 for (col = 0, p = av[0]->bp + prefix; *p != '\0'; ++p)
2178 col += KEY_LEN(sp, *p);
2179 if (col > colwidth)
2180 colwidth = col;
2182 colwidth += COL_OFF(colwidth, 6);
2185 * Writing to the bottom line of the screen is always turned off when
2186 * SC_TINPUT_INFO is set. Turn it back on, we know what we're doing.
2188 if (F_ISSET(sp, SC_TINPUT_INFO)) {
2189 reset = 1;
2190 F_CLR(sp, SC_TINPUT_INFO);
2191 } else
2192 reset = 0;
2194 #define CHK_INTR \
2195 if (F_ISSET(gp, G_INTERRUPTED)) \
2196 goto intr;
2198 /* If the largest file name is too large, just print them. */
2199 if (colwidth > sp->cols) {
2200 for (ac = argc, av = argv; ac > 0; --ac, ++av) {
2201 INT2CHAR(sp, av[0]->bp+prefix, av[0]->len+1-prefix,
2202 np, nlen);
2203 pp = msg_print(sp, np, &nf);
2204 (void)ex_printf(sp, "%s\n", pp);
2205 if (F_ISSET(gp, G_INTERRUPTED))
2206 break;
2208 if (nf)
2209 FREE_SPACE(sp, pp, 0);
2210 CHK_INTR;
2211 } else {
2212 /* Figure out the number of columns. */
2213 numcols = (sp->cols - 1) / colwidth;
2214 if (argc > numcols) {
2215 numrows = argc / numcols;
2216 if (argc % numcols)
2217 ++numrows;
2218 } else
2219 numrows = 1;
2221 /* Display the files in sorted order. */
2222 for (row = 0; row < numrows; ++row) {
2223 for (base = row, col = 0; col < numcols; ++col) {
2224 INT2CHAR(sp, argv[base]->bp+prefix,
2225 argv[base]->len+1-prefix, np, nlen);
2226 pp = msg_print(sp, np, &nf);
2227 cnt = ex_printf(sp, "%s", pp);
2228 if (nf)
2229 FREE_SPACE(sp, pp, 0);
2230 CHK_INTR;
2231 if ((base += numrows) >= argc)
2232 break;
2233 (void)ex_printf(sp,
2234 "%*s", (int)(colwidth - cnt), "");
2235 CHK_INTR;
2237 (void)ex_puts(sp, "\n");
2238 CHK_INTR;
2240 (void)ex_puts(sp, "\n");
2241 CHK_INTR;
2243 (void)ex_fflush(sp);
2245 if (0) {
2246 intr: F_CLR(gp, G_INTERRUPTED);
2248 if (reset)
2249 F_SET(sp, SC_TINPUT_INFO);
2251 return (0);
2255 * txt_emark --
2256 * Set the end mark on the line.
2258 static int
2259 txt_emark(SCR *sp, TEXT *tp, size_t cno)
2261 CHAR_T ch;
2262 char *kp;
2263 size_t chlen, nlen, olen;
2264 CHAR_T *p;
2266 ch = CH_ENDMARK;
2269 * The end mark may not be the same size as the current character.
2270 * Don't let the line shift.
2272 nlen = KEY_LEN(sp, ch);
2273 if (tp->lb[cno] == '\t')
2274 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &olen);
2275 else
2276 olen = KEY_LEN(sp, tp->lb[cno]);
2279 * If the line got longer, well, it's weird, but it's easy. If
2280 * it's the same length, it's easy. If it got shorter, we have
2281 * to fix it up.
2283 if (olen > nlen) {
2284 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + olen);
2285 chlen = olen - nlen;
2286 if (tp->insert != 0)
2287 MEMMOVEW(tp->lb + cno + 1 + chlen,
2288 tp->lb + cno + 1, tp->insert);
2290 tp->len += chlen;
2291 tp->owrite += chlen;
2292 p = tp->lb + cno;
2293 if (tp->lb[cno] == '\t')
2294 for (cno += chlen; chlen--;)
2295 *p++ = ' ';
2296 else
2297 for (kp = KEY_NAME(sp, tp->lb[cno]),
2298 cno += chlen; chlen--;)
2299 *p++ = *kp++;
2301 tp->lb[cno] = ch;
2302 return (vs_change(sp, tp->lno, LINE_RESET));
2306 * txt_err --
2307 * Handle an error during input processing.
2309 static void
2310 txt_err(SCR *sp, TEXTH *tiqh)
2312 db_recno_t lno;
2315 * The problem with input processing is that the cursor is at an
2316 * indeterminate position since some input may have been lost due
2317 * to a malloc error. So, try to go back to the place from which
2318 * the cursor started, knowing that it may no longer be available.
2320 * We depend on at least one line number being set in the text
2321 * chain.
2323 for (lno = tiqh->cqh_first->lno;
2324 !db_exist(sp, lno) && lno > 0; --lno);
2326 sp->lno = lno == 0 ? 1 : lno;
2327 sp->cno = 0;
2329 /* Redraw the screen, just in case. */
2330 F_SET(sp, SC_SCR_REDRAW);
2334 * txt_hex --
2335 * Let the user insert any character value they want.
2337 * !!!
2338 * This is an extension. The pattern "^X[0-9a-fA-F]*" is a way
2339 * for the user to specify a character value which their keyboard
2340 * may not be able to enter.
2342 static int
2343 txt_hex(SCR *sp, TEXT *tp)
2345 CHAR_T savec;
2346 size_t len, off;
2347 u_long value;
2348 CHAR_T *p, *wp;
2351 * Null-terminate the string. Since nul isn't a legal hex value,
2352 * this should be okay, and lets us use a local routine, which
2353 * presumably understands the character set, to convert the value.
2355 savec = tp->lb[tp->cno];
2356 tp->lb[tp->cno] = 0;
2358 /* Find the previous CH_HEX character. */
2359 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off, ++len) {
2360 if (*p == CH_HEX) {
2361 wp = p + 1;
2362 break;
2364 /* Not on this line? Shouldn't happen. */
2365 if (off == tp->ai || off == tp->offset)
2366 goto nothex;
2369 /* If length of 0, then it wasn't a hex value. */
2370 if (len == 0)
2371 goto nothex;
2373 /* Get the value. */
2374 errno = 0;
2375 value = STRTOL(wp, NULL, 16);
2376 if (errno || value > MAX_CHAR_T) {
2377 nothex: tp->lb[tp->cno] = savec;
2378 return (0);
2381 /* Restore the original character. */
2382 tp->lb[tp->cno] = savec;
2384 /* Adjust the bookkeeping. */
2385 tp->cno -= len;
2386 tp->len -= len;
2387 tp->lb[tp->cno - 1] = value;
2389 /* Copy down any overwrite characters. */
2390 if (tp->owrite)
2391 MEMMOVEW(tp->lb + tp->cno, tp->lb + tp->cno + len,
2392 tp->owrite);
2394 /* Copy down any insert characters. */
2395 if (tp->insert)
2396 MEMMOVEW(tp->lb + tp->cno + tp->owrite,
2397 tp->lb + tp->cno + tp->owrite + len,
2398 tp->insert);
2400 return (0);
2404 * txt_insch --
2406 * !!!
2407 * Historic vi did a special screen optimization for tab characters. As an
2408 * example, for the keystrokes "iabcd<esc>0C<tab>", the tab overwrote the
2409 * rest of the string when it was displayed.
2411 * Because early versions of this implementation redisplayed the entire line
2412 * on each keystroke, the "bcd" was pushed to the right as it ignored that
2413 * the user had "promised" to change the rest of the characters. However,
2414 * the historic vi implementation had an even worse bug: given the keystrokes
2415 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
2416 * on the second <esc> key.
2418 * POSIX 1003.2 requires (will require) that this be fixed, specifying that
2419 * vi overwrite characters the user has committed to changing, on the basis
2420 * of the screen space they require, but that it not overwrite other characters.
2422 static int
2423 txt_insch(SCR *sp, TEXT *tp, CHAR_T *chp, u_int flags)
2425 char *kp;
2426 CHAR_T savech;
2427 size_t chlen, cno, copydown, olen, nlen;
2428 CHAR_T *p;
2431 * The 'R' command does one-for-one replacement, because there's
2432 * no way to know how many characters the user intends to replace.
2434 if (LF_ISSET(TXT_REPLACE)) {
2435 if (tp->owrite) {
2436 --tp->owrite;
2437 tp->lb[tp->cno++] = *chp;
2438 return (0);
2440 } else if (tp->owrite) { /* Overwrite a character. */
2441 cno = tp->cno;
2444 * If the old or new characters are tabs, then the length of the
2445 * display depends on the character position in the display. We
2446 * don't even try to handle this here, just ask the screen.
2448 if (*chp == '\t') {
2449 savech = tp->lb[cno];
2450 tp->lb[cno] = '\t';
2451 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &nlen);
2452 tp->lb[cno] = savech;
2453 } else
2454 nlen = KEY_LEN(sp, *chp);
2457 * Eat overwrite characters until we run out of them or we've
2458 * handled the length of the new character. If we only eat
2459 * part of an overwrite character, break it into its component
2460 * elements and display the remaining components.
2462 for (copydown = 0; nlen != 0 && tp->owrite != 0;) {
2463 --tp->owrite;
2465 if (tp->lb[cno] == '\t')
2466 (void)vs_columns(sp,
2467 tp->lb, tp->lno, &cno, &olen);
2468 else
2469 olen = KEY_LEN(sp, tp->lb[cno]);
2471 if (olen == nlen) {
2472 nlen = 0;
2473 break;
2475 if (olen < nlen) {
2476 ++copydown;
2477 nlen -= olen;
2478 } else {
2479 BINC_RETW(sp,
2480 tp->lb, tp->lb_len, tp->len + olen);
2481 chlen = olen - nlen;
2482 MEMMOVEW(tp->lb + cno + 1 + chlen,
2483 tp->lb + cno + 1,
2484 tp->owrite + tp->insert);
2486 tp->len += chlen;
2487 tp->owrite += chlen;
2488 if (tp->lb[cno] == '\t')
2489 for (p = tp->lb + cno + 1; chlen--;)
2490 *p++ = ' ';
2491 else
2492 for (kp =
2493 KEY_NAME(sp, tp->lb[cno]) + nlen,
2494 p = tp->lb + cno + 1; chlen--;)
2495 *p++ = *kp++;
2496 nlen = 0;
2497 break;
2502 * If had to erase several characters, we adjust the total
2503 * count, and if there are any characters left, shift them
2504 * into position.
2506 if (copydown != 0 && (tp->len -= copydown) != 0)
2507 MEMMOVEW(tp->lb + cno, tp->lb + cno + copydown,
2508 tp->owrite + tp->insert + copydown);
2510 /* If we had enough overwrite characters, we're done. */
2511 if (nlen == 0) {
2512 tp->lb[tp->cno++] = *chp;
2513 return (0);
2517 /* Check to see if the character fits into the input buffer. */
2518 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2520 ++tp->len;
2521 if (tp->insert) { /* Insert a character. */
2522 if (tp->insert == 1)
2523 tp->lb[tp->cno + 1] = tp->lb[tp->cno];
2524 else
2525 MEMMOVEW(tp->lb + tp->cno + 1,
2526 tp->lb + tp->cno, tp->owrite + tp->insert);
2528 tp->lb[tp->cno++] = *chp;
2529 return (0);
2533 * txt_isrch --
2534 * Do an incremental search.
2536 static int
2537 txt_isrch(SCR *sp, VICMD *vp, TEXT *tp, u_int8_t *is_flagsp)
2539 MARK start;
2540 db_recno_t lno;
2541 u_int sf;
2543 /* If it's a one-line screen, we don't do incrementals. */
2544 if (IS_ONELINE(sp)) {
2545 FL_CLR(*is_flagsp, IS_RUNNING);
2546 return (0);
2550 * If the user erases back to the beginning of the buffer, there's
2551 * nothing to search for. Reset the cursor to the starting point.
2553 if (tp->cno <= 1) {
2554 vp->m_final = vp->m_start;
2555 return (0);
2559 * If it's an RE quote character, and not quoted, ignore it until
2560 * we get another character.
2562 if (tp->lb[tp->cno - 1] == '\\' &&
2563 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2564 return (0);
2567 * If it's a magic shell character, and not quoted, reset the cursor
2568 * to the starting point.
2570 if (strchr(O_STR(sp, O_SHELLMETA), tp->lb[tp->cno - 1]) != NULL &&
2571 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2572 vp->m_final = vp->m_start;
2575 * If we see the search pattern termination character, then quit doing
2576 * an incremental search. There may be more, e.g., ":/foo/;/bar/",
2577 * and we can't handle that incrementally. Also, reset the cursor to
2578 * the original location, the ex search routines don't know anything
2579 * about incremental searches.
2581 if (tp->lb[0] == tp->lb[tp->cno - 1] &&
2582 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\')) {
2583 vp->m_final = vp->m_start;
2584 FL_CLR(*is_flagsp, IS_RUNNING);
2585 return (0);
2589 * Remember the input line and discard the special input map,
2590 * but don't overwrite the input line on the screen.
2592 lno = tp->lno;
2593 F_SET(VIP(sp), VIP_S_MODELINE);
2594 F_CLR(sp, SC_TINPUT | SC_TINPUT_INFO);
2595 if (txt_map_end(sp))
2596 return (1);
2599 * Specify a starting point and search. If we find a match, move to
2600 * it and refresh the screen. If we didn't find the match, then we
2601 * beep the screen. When searching from the original cursor position,
2602 * we have to move the cursor, otherwise, we don't want to move the
2603 * cursor in case the text at the current position continues to match.
2605 if (FL_ISSET(*is_flagsp, IS_RESTART)) {
2606 start = vp->m_start;
2607 sf = SEARCH_SET;
2608 } else {
2609 start = vp->m_final;
2610 sf = SEARCH_INCR | SEARCH_SET;
2613 if (tp->lb[0] == '/' ?
2614 !f_search(sp,
2615 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf) :
2616 !b_search(sp,
2617 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf)) {
2618 sp->lno = vp->m_final.lno;
2619 sp->cno = vp->m_final.cno;
2620 FL_CLR(*is_flagsp, IS_RESTART);
2622 if (!KEYS_WAITING(sp) && vs_refresh(sp, 0))
2623 return (1);
2624 } else
2625 FL_SET(*is_flagsp, IS_RESTART);
2627 /* Reinstantiate the special input map. */
2628 if (txt_map_init(sp))
2629 return (1);
2630 F_CLR(VIP(sp), VIP_S_MODELINE);
2631 F_SET(sp, SC_TINPUT | SC_TINPUT_INFO);
2633 /* Reset the line number of the input line. */
2634 tp->lno = TMAP[0].lno;
2637 * If the colon command-line moved, i.e. the screen scrolled,
2638 * refresh the input line.
2640 * XXX
2641 * We shouldn't be calling vs_line, here -- we need dirty bits
2642 * on entries in the SMAP array.
2644 if (lno != TMAP[0].lno) {
2645 if (vs_line(sp, &TMAP[0], NULL, NULL))
2646 return (1);
2647 (void)sp->gp->scr_refresh(sp, 0);
2649 return (0);
2653 * txt_resolve --
2654 * Resolve the input text chain into the file.
2656 static int
2657 txt_resolve(SCR *sp, TEXTH *tiqh, u_int32_t flags)
2659 VI_PRIVATE *vip;
2660 TEXT *tp;
2661 db_recno_t lno;
2662 int changed;
2665 * The first line replaces a current line, and all subsequent lines
2666 * are appended into the file. Resolve autoindented characters for
2667 * each line before committing it. If the latter causes the line to
2668 * change, we have to redisplay it, otherwise the information cached
2669 * about the line will be wrong.
2671 vip = VIP(sp);
2672 tp = tiqh->cqh_first;
2674 if (LF_ISSET(TXT_AUTOINDENT))
2675 txt_ai_resolve(sp, tp, &changed);
2676 else
2677 changed = 0;
2678 if (db_set(sp, tp->lno, tp->lb, tp->len) ||
2679 changed && vs_change(sp, tp->lno, LINE_RESET))
2680 return (1);
2682 for (lno = tp->lno; (tp = tp->q.cqe_next) != (void *)&sp->tiq; ++lno) {
2683 if (LF_ISSET(TXT_AUTOINDENT))
2684 txt_ai_resolve(sp, tp, &changed);
2685 else
2686 changed = 0;
2687 if (db_append(sp, 0, lno, tp->lb, tp->len) ||
2688 changed && vs_change(sp, tp->lno, LINE_RESET))
2689 return (1);
2693 * Clear the input flag, the look-aside buffer is no longer valid.
2694 * Has to be done as part of text resolution, or upon return we'll
2695 * be looking at incorrect data.
2697 F_CLR(sp, SC_TINPUT);
2699 return (0);
2703 * txt_showmatch --
2704 * Show a character match.
2706 * !!!
2707 * Historic vi tried to display matches even in the :colon command line.
2708 * I think not.
2710 static int
2711 txt_showmatch(SCR *sp, TEXT *tp)
2713 GS *gp;
2714 VCS cs;
2715 MARK m;
2716 int cnt, endc, startc;
2718 gp = sp->gp;
2721 * Do a refresh first, in case we haven't done one in awhile,
2722 * so the user can see what we're complaining about.
2724 UPDATE_POSITION(sp, tp);
2725 if (vs_refresh(sp, 1))
2726 return (1);
2729 * We don't display the match if it's not on the screen. Find
2730 * out what the first character on the screen is.
2732 if (vs_sm_position(sp, &m, 0, P_TOP))
2733 return (1);
2735 /* Initialize the getc() interface. */
2736 cs.cs_lno = tp->lno;
2737 cs.cs_cno = tp->cno - 1;
2738 if (cs_init(sp, &cs))
2739 return (1);
2740 startc = (endc = cs.cs_ch) == ')' ? '(' : '{';
2742 /* Search for the match. */
2743 for (cnt = 1;;) {
2744 if (cs_prev(sp, &cs))
2745 return (1);
2746 if (cs.cs_flags != 0) {
2747 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF) {
2748 msgq(sp, M_BERR,
2749 "Unmatched %s", KEY_NAME(sp, endc));
2750 return (0);
2752 continue;
2754 if (cs.cs_ch == endc)
2755 ++cnt;
2756 else if (cs.cs_ch == startc && --cnt == 0)
2757 break;
2760 /* If the match is on the screen, move to it. */
2761 if (cs.cs_lno < m.lno || cs.cs_lno == m.lno && cs.cs_cno < m.cno)
2762 return (0);
2763 sp->lno = cs.cs_lno;
2764 sp->cno = cs.cs_cno;
2765 if (vs_refresh(sp, 1))
2766 return (1);
2768 /* Wait for timeout or character arrival. */
2769 return (v_event_get(sp,
2770 NULL, O_VAL(sp, O_MATCHTIME) * 100, EC_TIMEOUT));
2774 * txt_margin --
2775 * Handle margin wrap.
2777 static int
2778 txt_margin(SCR *sp, TEXT *tp, TEXT *wmtp, int *didbreak, u_int32_t flags)
2780 VI_PRIVATE *vip;
2781 size_t len, off;
2782 CHAR_T *p, *wp;
2784 /* Find the nearest previous blank. */
2785 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --off, --p, ++len) {
2786 if (isblank(*p)) {
2787 wp = p + 1;
2788 break;
2792 * If reach the start of the line, there's nowhere to break.
2794 * !!!
2795 * Historic vi belled each time a character was entered after
2796 * crossing the margin until a space was entered which could
2797 * be used to break the line. I don't as it tends to wake the
2798 * cats.
2800 if (off == tp->ai || off == tp->offset) {
2801 *didbreak = 0;
2802 return (0);
2807 * Store saved information about the rest of the line in the
2808 * wrapmargin TEXT structure.
2810 * !!!
2811 * The offset field holds the length of the current characters
2812 * that the user entered, but which are getting split to the new
2813 * line -- it's going to be used to set the cursor value when we
2814 * move to the new line.
2816 vip = VIP(sp);
2817 wmtp->lb = p + 1;
2818 wmtp->offset = len;
2819 wmtp->insert = LF_ISSET(TXT_APPENDEOL) ? tp->insert - 1 : tp->insert;
2820 wmtp->owrite = tp->owrite;
2822 /* Correct current bookkeeping information. */
2823 tp->cno -= len;
2824 if (LF_ISSET(TXT_APPENDEOL)) {
2825 tp->len -= len + tp->owrite + (tp->insert - 1);
2826 tp->insert = 1;
2827 } else {
2828 tp->len -= len + tp->owrite + tp->insert;
2829 tp->insert = 0;
2831 tp->owrite = 0;
2834 * !!!
2835 * Delete any trailing whitespace from the current line.
2837 for (;; --p, --off) {
2838 if (!isblank(*p))
2839 break;
2840 --tp->cno;
2841 --tp->len;
2842 if (off == tp->ai || off == tp->offset)
2843 break;
2845 *didbreak = 1;
2846 return (0);
2850 * txt_Rresolve --
2851 * Resolve the input line for the 'R' command.
2853 static void
2854 txt_Rresolve(SCR *sp, TEXTH *tiqh, TEXT *tp, const size_t orig_len)
2856 TEXT *ttp;
2857 size_t input_len, retain;
2858 CHAR_T *p;
2861 * Check to make sure that the cursor hasn't moved beyond
2862 * the end of the line.
2864 if (tp->owrite == 0)
2865 return;
2868 * Calculate how many characters the user has entered,
2869 * plus the blanks erased by <carriage-return>/<newline>s.
2871 for (ttp = tiqh->cqh_first, input_len = 0;;) {
2872 input_len += ttp == tp ? tp->cno : ttp->len + ttp->R_erase;
2873 if ((ttp = ttp->q.cqe_next) == (void *)&sp->tiq)
2874 break;
2878 * If the user has entered less characters than the original line
2879 * was long, restore any overwriteable characters to the original
2880 * characters. These characters are entered as "insert characters",
2881 * because they're after the cursor and we don't want to lose them.
2882 * (This is okay because the R command has no insert characters.)
2883 * We set owrite to 0 so that the insert characters don't get copied
2884 * to somewhere else, which means that the line and the length have
2885 * to be adjusted here as well.
2887 * We have to retrieve the original line because the original pinned
2888 * page has long since been discarded. If it doesn't exist, that's
2889 * okay, the user just extended the file.
2891 if (input_len < orig_len) {
2892 retain = MIN(tp->owrite, orig_len - input_len);
2893 if (db_get(sp,
2894 tiqh->cqh_first->lno, DBG_FATAL | DBG_NOCACHE, &p, NULL))
2895 return;
2896 MEMCPYW(tp->lb + tp->cno, p + input_len, retain);
2897 tp->len -= tp->owrite - retain;
2898 tp->owrite = 0;
2899 tp->insert += retain;
2904 * txt_nomorech --
2905 * No more characters message.
2907 static void
2908 txt_nomorech(SCR *sp)
2910 msgq(sp, M_BERR, "194|No more characters to erase");