recno_t -> db_recno_t
[nvi.git] / vi / v_txt.c
blob5552670cb076b71c13499a997a08c2cb09623298
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.92 2000/04/21 19:00:41 skimo Exp $ (Berkeley) $Date: 2000/04/21 19:00:41 $";
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(sp, vp, prompt, flags)
67 SCR *sp;
68 VICMD *vp;
69 ARG_CHAR_T prompt;
70 u_int flags;
72 /* Normally, we end up where we started. */
73 vp->m_final.lno = sp->lno;
74 vp->m_final.cno = sp->cno;
76 /* Initialize the map. */
77 if (txt_map_init(sp))
78 return (1);
80 /* Move to the last line. */
81 sp->lno = TMAP[0].lno;
82 sp->cno = 0;
84 /* Don't update the modeline for now. */
85 F_SET(sp, SC_TINPUT_INFO);
87 /* Set the input flags. */
88 LF_SET(TXT_APPENDEOL |
89 TXT_CR | TXT_ESCAPE | TXT_INFOLINE | TXT_MAPINPUT);
90 if (O_ISSET(sp, O_ALTWERASE))
91 LF_SET(TXT_ALTWERASE);
92 if (O_ISSET(sp, O_TTYWERASE))
93 LF_SET(TXT_TTYWERASE);
95 /* Do the input thing. */
96 if (v_txt(sp, vp, NULL, NULL, 0, prompt, 0, 1, flags))
97 return (1);
99 /* Reenable the modeline updates. */
100 F_CLR(sp, SC_TINPUT_INFO);
102 /* Clean up the map. */
103 if (txt_map_end(sp))
104 return (1);
106 if (IS_ONELINE(sp))
107 F_SET(sp, SC_SCR_REDRAW); /* XXX */
109 /* Set the cursor to the resulting position. */
110 sp->lno = vp->m_final.lno;
111 sp->cno = vp->m_final.cno;
113 return (0);
117 * txt_map_init
118 * Initialize the screen map for colon command-line input.
120 static int
121 txt_map_init(sp)
122 SCR *sp;
124 SMAP *esmp;
125 VI_PRIVATE *vip;
127 vip = VIP(sp);
128 if (!IS_ONELINE(sp)) {
130 * Fake like the user is doing input on the last line of the
131 * screen. This makes all of the scrolling work correctly,
132 * and allows us the use of the vi text editing routines, not
133 * to mention practically infinite length ex commands.
135 * Save the current location.
137 vip->sv_tm_lno = TMAP->lno;
138 vip->sv_tm_soff = TMAP->soff;
139 vip->sv_tm_coff = TMAP->coff;
140 vip->sv_t_maxrows = sp->t_maxrows;
141 vip->sv_t_minrows = sp->t_minrows;
142 vip->sv_t_rows = sp->t_rows;
145 * If it's a small screen, TMAP may be small for the screen.
146 * Fix it, filling in fake lines as we go.
148 if (IS_SMALL(sp))
149 for (esmp =
150 HMAP + (sp->t_maxrows - 1); TMAP < esmp; ++TMAP) {
151 TMAP[1].lno = TMAP[0].lno + 1;
152 TMAP[1].coff = HMAP->coff;
153 TMAP[1].soff = 1;
156 /* Build the fake entry. */
157 TMAP[1].lno = TMAP[0].lno + 1;
158 TMAP[1].soff = 1;
159 TMAP[1].coff = 0;
160 SMAP_FLUSH(&TMAP[1]);
161 ++TMAP;
163 /* Reset the screen information. */
164 sp->t_rows = sp->t_minrows = ++sp->t_maxrows;
166 return (0);
170 * txt_map_end
171 * Reset the screen map for colon command-line input.
173 static int
174 txt_map_end(sp)
175 SCR *sp;
177 VI_PRIVATE *vip;
178 size_t cnt;
180 vip = VIP(sp);
181 if (!IS_ONELINE(sp)) {
182 /* Restore the screen information. */
183 sp->t_rows = vip->sv_t_rows;
184 sp->t_minrows = vip->sv_t_minrows;
185 sp->t_maxrows = vip->sv_t_maxrows;
188 * If it's a small screen, TMAP may be wrong. Clear any
189 * lines that might have been overwritten.
191 if (IS_SMALL(sp)) {
192 for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
193 (void)sp->gp->scr_move(sp, cnt, 0);
194 (void)sp->gp->scr_clrtoeol(sp);
196 TMAP = HMAP + (sp->t_rows - 1);
197 } else
198 --TMAP;
201 * The map may be wrong if the user entered more than one
202 * (logical) line. Fix it. If the user entered a whole
203 * screen, this will be slow, but we probably don't care.
205 if (!O_ISSET(sp, O_LEFTRIGHT))
206 while (vip->sv_tm_lno != TMAP->lno ||
207 vip->sv_tm_soff != TMAP->soff)
208 if (vs_sm_1down(sp))
209 return (1);
213 * Invalidate the cursor and the line size cache, the line never
214 * really existed. This fixes bugs where the user searches for
215 * the last line on the screen + 1 and the refresh routine thinks
216 * that's where we just were.
218 VI_SCR_CFLUSH(vip);
219 F_SET(vip, VIP_CUR_INVALID);
221 return (0);
225 * If doing input mapping on the colon command line, may need to unmap
226 * based on the command.
228 #define UNMAP_TST \
229 FL_ISSET(ec_flags, EC_MAPINPUT) && LF_ISSET(TXT_INFOLINE)
232 * Internally, we maintain tp->lno and tp->cno, externally, everyone uses
233 * sp->lno and sp->cno. Make them consistent as necessary.
235 #define UPDATE_POSITION(sp, tp) { \
236 (sp)->lno = (tp)->lno; \
237 (sp)->cno = (tp)->cno; \
241 * v_txt --
242 * Vi text input.
244 * PUBLIC: int v_txt __P((SCR *, VICMD *, MARK *,
245 * PUBLIC: const char *, size_t, ARG_CHAR_T, db_recno_t, u_long, u_int32_t));
248 v_txt(sp, vp, tm, lp, len, prompt, ai_line, rcount, flags)
249 SCR *sp;
250 VICMD *vp;
251 MARK *tm; /* To MARK. */
252 const char *lp; /* Input line. */
253 size_t len; /* Input line length. */
254 ARG_CHAR_T prompt; /* Prompt to display. */
255 db_recno_t ai_line; /* Line number to use for autoindent count. */
256 u_long rcount; /* Replay count. */
257 u_int32_t flags; /* TXT_* flags. */
259 EVENT ev, *evp; /* Current event. */
260 EVENT fc; /* File name completion event. */
261 GS *gp;
262 TEXT *ntp, *tp; /* Input text structures. */
263 TEXT ait; /* Autoindent text structure. */
264 TEXT wmt; /* Wrapmargin text structure. */
265 TEXTH *tiqh;
266 VI_PRIVATE *vip;
267 abb_t abb; /* State of abbreviation checks. */
268 carat_t carat; /* State of the "[^0]^D" sequences. */
269 quote_t quote; /* State of quotation. */
270 size_t owrite, insert; /* Temporary copies of TEXT fields. */
271 size_t margin; /* Wrapmargin value. */
272 size_t rcol; /* 0-N: insert offset in the replay buffer. */
273 size_t tcol; /* Temporary column. */
274 u_int32_t ec_flags; /* Input mapping flags. */
275 #define IS_RESTART 0x01 /* Reset the incremental search. */
276 #define IS_RUNNING 0x02 /* Incremental search turned on. */
277 u_int8_t is_flags;
278 int abcnt, ab_turnoff; /* Abbreviation character count, switch. */
279 int filec_redraw; /* Redraw after the file completion routine. */
280 int hexcnt; /* Hex character count. */
281 int showmatch; /* Showmatch set on this character. */
282 int wm_set, wm_skip; /* Wrapmargin happened, blank skip flags. */
283 int max, tmp;
284 char *p;
286 gp = sp->gp;
287 vip = VIP(sp);
290 * Set the input flag, so tabs get displayed correctly
291 * and everyone knows that the text buffer is in use.
293 F_SET(sp, SC_TINPUT);
296 * Get one TEXT structure with some initial buffer space, reusing
297 * the last one if it's big enough. (All TEXT bookkeeping fields
298 * default to 0 -- text_init() handles this.) If changing a line,
299 * copy it into the TEXT buffer.
301 tiqh = &sp->tiq;
302 if (tiqh->cqh_first != (void *)tiqh) {
303 tp = tiqh->cqh_first;
304 if (tp->q.cqe_next != (void *)tiqh || tp->lb_len < len + 32) {
305 text_lfree(tiqh);
306 goto newtp;
308 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
309 if (lp != NULL) {
310 tp->len = len;
311 memmove(tp->lb, lp, len);
312 } else
313 tp->len = 0;
314 } else {
315 newtp: if ((tp = text_init(sp, lp, len, len + 32)) == NULL)
316 return (1);
317 CIRCLEQ_INSERT_HEAD(tiqh, tp, q);
320 /* Set default termination condition. */
321 tp->term = TERM_OK;
323 /* Set the starting line, column. */
324 tp->lno = sp->lno;
325 tp->cno = sp->cno;
328 * Set the insert and overwrite counts. If overwriting characters,
329 * do insertion afterward. If not overwriting characters, assume
330 * doing insertion. If change is to a mark, emphasize it with an
331 * CH_ENDMARK character.
333 if (len) {
334 if (LF_ISSET(TXT_OVERWRITE)) {
335 tp->owrite = (tm->cno - tp->cno) + 1;
336 tp->insert = (len - tm->cno) - 1;
337 } else
338 tp->insert = len - tp->cno;
340 if (LF_ISSET(TXT_EMARK) && txt_emark(sp, tp, tm->cno))
341 return (1);
345 * Many of the special cases in text input are to handle autoindent
346 * support. Somebody decided that it would be a good idea if "^^D"
347 * and "0^D" deleted all of the autoindented characters. In an editor
348 * that takes single character input from the user, this beggars the
349 * imagination. Note also, "^^D" resets the next lines' autoindent,
350 * but "0^D" doesn't.
352 * We assume that autoindent only happens on empty lines, so insert
353 * and overwrite will be zero. If doing autoindent, figure out how
354 * much indentation we need and fill it in. Update input column and
355 * screen cursor as necessary.
357 if (LF_ISSET(TXT_AUTOINDENT) && ai_line != OOBLNO) {
358 if (v_txt_auto(sp, ai_line, NULL, 0, tp))
359 return (1);
360 tp->cno = tp->ai;
361 } else {
363 * The cc and S commands have a special feature -- leading
364 * <blank> characters are handled as autoindent characters.
365 * Beauty!
367 if (LF_ISSET(TXT_AICHARS)) {
368 tp->offset = 0;
369 tp->ai = tp->cno;
370 } else
371 tp->offset = tp->cno;
374 /* If getting a command buffer from the user, there may be a prompt. */
375 if (LF_ISSET(TXT_PROMPT)) {
376 tp->lb[tp->cno++] = prompt;
377 ++tp->len;
378 ++tp->offset;
382 * If appending after the end-of-line, add a space into the buffer
383 * and move the cursor right. This space is inserted, i.e. pushed
384 * along, and then deleted when the line is resolved. Assumes that
385 * the cursor is already positioned at the end of the line. This
386 * avoids the nastiness of having the cursor reside on a magical
387 * column, i.e. a column that doesn't really exist. The only down
388 * side is that we may wrap lines or scroll the screen before it's
389 * strictly necessary. Not a big deal.
391 if (LF_ISSET(TXT_APPENDEOL)) {
392 tp->lb[tp->cno] = CH_CURSOR;
393 ++tp->len;
394 ++tp->insert;
395 (void)vs_change(sp, tp->lno, LINE_RESET);
399 * Historic practice is that the wrapmargin value was a distance
400 * from the RIGHT-HAND margin, not the left. It's more useful to
401 * us as a distance from the left-hand margin, i.e. the same as
402 * the wraplen value. The wrapmargin option is historic practice.
403 * Nvi added the wraplen option so that it would be possible to
404 * edit files with consistent margins without knowing the number of
405 * columns in the window.
407 * XXX
408 * Setting margin causes a significant performance hit. Normally
409 * we don't update the screen if there are keys waiting, but we
410 * have to if margin is set, otherwise the screen routines don't
411 * know where the cursor is.
413 * !!!
414 * Abbreviated keys were affected by the wrapmargin option in the
415 * historic 4BSD vi. Mapped keys were usually, but sometimes not.
416 * See the comment in vi/v_text():set_txt_std for more information.
418 * !!!
419 * One more special case. If an inserted <blank> character causes
420 * wrapmargin to split the line, the next user entered character is
421 * discarded if it's a <space> character.
423 wm_set = wm_skip = 0;
424 if (LF_ISSET(TXT_WRAPMARGIN))
425 if ((margin = O_VAL(sp, O_WRAPMARGIN)) != 0)
426 margin = sp->cols - margin;
427 else
428 margin = O_VAL(sp, O_WRAPLEN);
429 else
430 margin = 0;
432 /* Initialize abbreviation checks. */
433 abcnt = ab_turnoff = 0;
434 abb = F_ISSET(gp, G_ABBREV) &&
435 LF_ISSET(TXT_MAPINPUT) ? AB_INWORD : AB_NOTSET;
438 * Set up the dot command. Dot commands are done by saving the actual
439 * characters and then reevaluating them so that things like wrapmargin
440 * can change between the insert and the replay.
442 * !!!
443 * Historically, vi did not remap or reabbreviate replayed input. (It
444 * did beep at you if you changed an abbreviation and then replayed the
445 * input. We're not that compatible.) We don't have to do anything to
446 * avoid remapping, as we're not getting characters from the terminal
447 * routines. Turn the abbreviation check off.
449 * XXX
450 * It would be nice if we could swallow backspaces and such, but it's
451 * not all that easy to do. What we can do is turn off the common
452 * error messages during the replay. Otherwise, when the user enters
453 * an illegal command, e.g., "Ia<erase><erase><erase><erase>b<escape>",
454 * and then does a '.', they get a list of error messages after command
455 * completion.
457 rcol = 0;
458 if (LF_ISSET(TXT_REPLAY)) {
459 abb = AB_NOTSET;
460 LF_CLR(TXT_RECORD);
463 /* Other text input mode setup. */
464 quote = Q_NOTSET;
465 carat = C_NOTSET;
466 FL_INIT(is_flags,
467 LF_ISSET(TXT_SEARCHINCR) ? IS_RESTART | IS_RUNNING : 0);
468 filec_redraw = hexcnt = showmatch = 0;
470 /* Initialize input flags. */
471 ec_flags = LF_ISSET(TXT_MAPINPUT) ? EC_MAPINPUT : 0;
473 /* Refresh the screen. */
474 UPDATE_POSITION(sp, tp);
475 if (vs_refresh(sp, 1))
476 return (1);
478 /* If it's dot, just do it now. */
479 if (F_ISSET(vp, VC_ISDOT))
480 goto replay;
482 /* Get an event. */
483 evp = &ev;
484 next: if (v_event_get(sp, evp, 0, ec_flags))
485 return (1);
488 * If file completion overwrote part of the screen and nothing else has
489 * been displayed, clean up. We don't do this as part of the normal
490 * message resolution because we know the user is on the colon command
491 * line and there's no reason to enter explicit characters to continue.
493 if (filec_redraw && !F_ISSET(sp, SC_SCR_EXWROTE)) {
494 filec_redraw = 0;
496 fc.e_event = E_REPAINT;
497 fc.e_flno = vip->totalcount >=
498 sp->rows ? 1 : sp->rows - vip->totalcount;
499 fc.e_tlno = sp->rows;
500 vip->linecount = vip->lcontinue = vip->totalcount = 0;
501 (void)v_erepaint(sp, &fc);
502 (void)vs_refresh(sp, 1);
505 /* Deal with all non-character events. */
506 switch (evp->e_event) {
507 case E_CHARACTER:
508 break;
509 case E_ERR:
510 case E_EOF:
511 F_SET(sp, SC_EXIT_FORCE);
512 return (1);
513 case E_INTERRUPT:
515 * !!!
516 * Historically, <interrupt> exited the user from text input
517 * mode or cancelled a colon command, and returned to command
518 * mode. It also beeped the terminal, but that seems a bit
519 * excessive.
521 goto k_escape;
522 case E_REPAINT:
523 if (v_erepaint(sp, &ev))
524 return (1);
525 goto next;
526 case E_WRESIZE:
527 /* <resize> interrupts the input mode. */
528 v_emsg(sp, NULL, VIM_WRESIZE);
529 goto k_escape;
530 default:
531 v_event_err(sp, evp);
532 goto k_escape;
536 * !!!
537 * If the first character of the input is a nul, replay the previous
538 * input. (Historically, it's okay to replay non-existent input.)
539 * This was not documented as far as I know, and is a great test of vi
540 * clones.
542 if (rcol == 0 && !LF_ISSET(TXT_REPLAY) && evp->e_c == '\0') {
543 if (vip->rep == NULL)
544 goto done;
546 abb = AB_NOTSET;
547 LF_CLR(TXT_RECORD);
548 LF_SET(TXT_REPLAY);
549 goto replay;
553 * File name completion and colon command-line editing. We don't
554 * have enough meta characters, so we expect people to overload
555 * them. If the two characters are the same, then we do file name
556 * completion if the cursor is past the first column, and do colon
557 * command-line editing if it's not.
559 if (quote == Q_NOTSET) {
560 int L__cedit, L__filec;
562 L__cedit = L__filec = 0;
563 if (LF_ISSET(TXT_CEDIT) && O_STR(sp, O_CEDIT) != NULL &&
564 O_STR(sp, O_CEDIT)[0] == evp->e_c)
565 L__cedit = 1;
566 if (LF_ISSET(TXT_FILEC) && O_STR(sp, O_FILEC) != NULL &&
567 O_STR(sp, O_FILEC)[0] == evp->e_c)
568 L__filec = 1;
569 if (L__cedit == 1 && (L__filec == 0 || tp->cno == tp->offset)) {
570 tp->term = TERM_CEDIT;
571 goto k_escape;
573 if (L__filec == 1) {
574 if (txt_fc(sp, tp, &filec_redraw))
575 goto err;
576 goto resolve;
580 /* Abbreviation overflow check. See comment in txt_abbrev(). */
581 #define MAX_ABBREVIATION_EXPANSION 256
582 if (FL_ISSET(evp->e_flags, CH_ABBREVIATED)) {
583 if (++abcnt > MAX_ABBREVIATION_EXPANSION) {
584 if (v_event_flush(sp, CH_ABBREVIATED))
585 msgq(sp, M_ERR,
586 "191|Abbreviation exceeded expansion limit: characters discarded");
587 abcnt = 0;
588 if (LF_ISSET(TXT_REPLAY))
589 goto done;
590 goto resolve;
592 } else
593 abcnt = 0;
595 /* Check to see if the character fits into the replay buffers. */
596 if (LF_ISSET(TXT_RECORD)) {
597 BINC_GOTO(sp, vip->rep,
598 vip->rep_len, (rcol + 1) * sizeof(EVENT));
599 vip->rep[rcol++] = *evp;
602 replay: if (LF_ISSET(TXT_REPLAY)) {
603 if (rcol == vip->rep_cnt)
604 goto k_escape;
605 evp = vip->rep + rcol++;
608 /* Wrapmargin check for leading space. */
609 if (wm_skip) {
610 wm_skip = 0;
611 if (evp->e_c == ' ')
612 goto resolve;
615 /* If quoted by someone else, simply insert the character. */
616 if (FL_ISSET(evp->e_flags, CH_QUOTED))
617 goto insq_ch;
620 * !!!
621 * If this character was quoted by a K_VLNEXT or a backslash, replace
622 * the placeholder (a carat or a backslash) with the new character.
623 * If it was quoted by a K_VLNEXT, we've already adjusted the cursor
624 * because it has to appear on top of the placeholder character. If
625 * it was quoted by a backslash, adjust the cursor now, the cursor
626 * doesn't appear on top of it. Historic practice in both cases.
628 * Skip tests for abbreviations; ":ab xa XA" followed by "ixa^V<space>"
629 * doesn't perform an abbreviation. Special case, ^V^J (not ^V^M) is
630 * the same as ^J, historically.
632 if (quote == Q_BTHIS || quote == Q_VTHIS) {
633 FL_CLR(ec_flags, EC_QUOTED);
634 if (LF_ISSET(TXT_MAPINPUT))
635 FL_SET(ec_flags, EC_MAPINPUT);
637 if (quote == Q_BTHIS &&
638 (evp->e_value == K_VERASE || evp->e_value == K_VKILL)) {
639 quote = Q_NOTSET;
640 --tp->cno;
641 ++tp->owrite;
642 goto insl_ch;
644 if (quote == Q_VTHIS && evp->e_value != K_NL) {
645 quote = Q_NOTSET;
646 goto insl_ch;
648 quote = Q_NOTSET;
652 * !!!
653 * Translate "<CH_HEX>[isxdigit()]*" to a character with a hex value:
654 * this test delimits the value by any non-hex character. Offset by
655 * one, we use 0 to mean that we've found <CH_HEX>.
657 if (hexcnt > 1 && !isxdigit(evp->e_c)) {
658 hexcnt = 0;
659 if (txt_hex(sp, tp))
660 goto err;
663 switch (evp->e_value) {
664 case K_CR: /* Carriage return. */
665 case K_NL: /* New line. */
666 /* Return in script windows and the command line. */
667 k_cr: if (LF_ISSET(TXT_CR)) {
669 * If this was a map, we may have not displayed
670 * the line. Display it, just in case.
672 * If a script window and not the colon line,
673 * push a <cr> so it gets executed.
675 if (LF_ISSET(TXT_INFOLINE)) {
676 if (vs_change(sp, tp->lno, LINE_RESET))
677 goto err;
678 } else if (F_ISSET(sp, SC_SCRIPT))
679 (void)v_event_push(sp, NULL, "\r", 1, CH_NOMAP);
681 /* Set term condition: if empty. */
682 if (tp->cno <= tp->offset)
683 tp->term = TERM_CR;
685 * Set term condition: if searching incrementally and
686 * the user entered a pattern, return a completed
687 * search, regardless if the entire pattern was found.
689 if (FL_ISSET(is_flags, IS_RUNNING) &&
690 tp->cno >= tp->offset + 1)
691 tp->term = TERM_SEARCH;
693 goto k_escape;
696 #define LINE_RESOLVE { \
697 /* \
698 * Handle abbreviations. If there was one, discard the \
699 * replay characters. \
700 */ \
701 if (abb == AB_INWORD && \
702 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) { \
703 if (txt_abbrev(sp, tp, &evp->e_c, \
704 LF_ISSET(TXT_INFOLINE), &tmp, \
705 &ab_turnoff)) \
706 goto err; \
707 if (tmp) { \
708 if (LF_ISSET(TXT_RECORD)) \
709 rcol -= tmp + 1; \
710 goto resolve; \
713 if (abb != AB_NOTSET) \
714 abb = AB_NOTWORD; \
715 if (UNMAP_TST) \
716 txt_unmap(sp, tp, &ec_flags); \
717 /* \
718 * Delete any appended cursor. It's possible to get in \
719 * situations where TXT_APPENDEOL is set but tp->insert \
720 * is 0 when using the R command and all the characters \
721 * are tp->owrite characters. \
722 */ \
723 if (LF_ISSET(TXT_APPENDEOL) && tp->insert > 0) { \
724 --tp->len; \
725 --tp->insert; \
728 LINE_RESOLVE;
731 * Save the current line information for restoration in
732 * txt_backup(), and set the line final length.
734 tp->sv_len = tp->len;
735 tp->sv_cno = tp->cno;
736 tp->len = tp->cno;
738 /* Update the old line. */
739 if (vs_change(sp, tp->lno, LINE_RESET))
740 goto err;
743 * Historic practice, when the autoindent edit option was set,
744 * was to delete <blank> characters following the inserted
745 * newline. This affected the 'R', 'c', and 's' commands; 'c'
746 * and 's' retained the insert characters only, 'R' moved the
747 * overwrite and insert characters into the next TEXT structure.
748 * We keep track of the number of characters erased for the 'R'
749 * command so that the final resolution of the line is correct.
751 tp->R_erase = 0;
752 owrite = tp->owrite;
753 insert = tp->insert;
754 if (LF_ISSET(TXT_REPLACE) && owrite != 0) {
755 for (p = tp->lb + tp->cno; owrite > 0 && isblank(*p);
756 ++p, --owrite, ++tp->R_erase);
757 if (owrite == 0)
758 for (; insert > 0 && isblank(*p);
759 ++p, ++tp->R_erase, --insert);
760 } else {
761 p = tp->lb + tp->cno + owrite;
762 if (O_ISSET(sp, O_AUTOINDENT))
763 for (; insert > 0 &&
764 isblank(*p); ++p, --insert);
765 owrite = 0;
769 * !!!
770 * Create a new line and insert the new TEXT into the queue.
771 * DON'T insert until the old line has been updated, or the
772 * inserted line count in line.c:db_get() will be wrong.
774 if ((ntp = text_init(sp, p,
775 insert + owrite, insert + owrite + 32)) == NULL)
776 goto err;
777 CIRCLEQ_INSERT_TAIL(&sp->tiq, ntp, q);
779 /* Set up bookkeeping for the new line. */
780 ntp->insert = insert;
781 ntp->owrite = owrite;
782 ntp->lno = tp->lno + 1;
785 * Reset the autoindent line value. 0^D keeps the autoindent
786 * line from changing, ^D changes the level, even if there were
787 * no characters in the old line. Note, if using the current
788 * tp structure, use the cursor as the length, the autoindent
789 * characters may have been erased.
791 if (LF_ISSET(TXT_AUTOINDENT)) {
792 if (carat == C_NOCHANGE) {
793 if (v_txt_auto(sp, OOBLNO, &ait, ait.ai, ntp))
794 goto err;
795 FREE_SPACE(sp, ait.lb, ait.lb_len);
796 } else
797 if (v_txt_auto(sp, OOBLNO, tp, tp->cno, ntp))
798 goto err;
799 carat = C_NOTSET;
802 /* Reset the cursor. */
803 ntp->cno = ntp->ai;
806 * If we're here because wrapmargin was set and we've broken a
807 * line, there may be additional information (i.e. the start of
808 * a line) in the wmt structure.
810 if (wm_set) {
811 if (wmt.offset != 0 ||
812 wmt.owrite != 0 || wmt.insert != 0) {
813 #define WMTSPACE wmt.offset + wmt.owrite + wmt.insert
814 BINC_GOTO(sp, ntp->lb,
815 ntp->lb_len, ntp->len + WMTSPACE + 32);
816 memmove(ntp->lb + ntp->cno, wmt.lb, WMTSPACE);
817 ntp->len += WMTSPACE;
818 ntp->cno += wmt.offset;
819 ntp->owrite = wmt.owrite;
820 ntp->insert = wmt.insert;
822 wm_set = 0;
825 /* New lines are TXT_APPENDEOL. */
826 if (ntp->owrite == 0 && ntp->insert == 0) {
827 BINC_GOTO(sp, ntp->lb, ntp->lb_len, ntp->len + 1);
828 LF_SET(TXT_APPENDEOL);
829 ntp->lb[ntp->cno] = CH_CURSOR;
830 ++ntp->insert;
831 ++ntp->len;
834 /* Swap old and new TEXT's, and update the new line. */
835 tp = ntp;
836 if (vs_change(sp, tp->lno, LINE_INSERT))
837 goto err;
839 goto resolve;
840 case K_ESCAPE: /* Escape. */
841 if (!LF_ISSET(TXT_ESCAPE))
842 goto ins_ch;
844 /* If we have a count, start replaying the input. */
845 if (rcount > 1) {
846 --rcount;
848 rcol = 0;
849 abb = AB_NOTSET;
850 LF_CLR(TXT_RECORD);
851 LF_SET(TXT_REPLAY);
854 * Some commands (e.g. 'o') need a <newline> for each
855 * repetition.
857 if (LF_ISSET(TXT_ADDNEWLINE))
858 goto k_cr;
861 * The R command turns into the 'a' command after the
862 * first repetition.
864 if (LF_ISSET(TXT_REPLACE)) {
865 tp->insert = tp->owrite;
866 tp->owrite = 0;
867 LF_CLR(TXT_REPLACE);
869 goto replay;
872 /* Set term condition: if empty. */
873 if (tp->cno <= tp->offset)
874 tp->term = TERM_ESC;
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.
896 if (tp->owrite) {
897 if (tp->insert)
898 memmove(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.
909 * XXX
910 * This is wrong, should pass back a length.
912 if (LF_ISSET(TXT_RESOLVE)) {
913 if (txt_resolve(sp, &sp->tiq, flags))
914 goto err;
915 } else {
916 BINC_GOTO(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.
924 if (tp->cno != 0)
925 --tp->cno;
927 /* Update the last line. */
928 if (vs_change(sp, tp->lno, LINE_RESET))
929 return (1);
930 goto done;
931 case K_CARAT: /* Delete autoindent chars. */
932 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
933 carat = C_CARATSET;
934 goto ins_ch;
935 case K_ZERO: /* Delete autoindent chars. */
936 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
937 carat = C_ZEROSET;
938 goto ins_ch;
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))
948 goto ins_ch;
949 if (tp->cno == 0)
950 goto resolve;
952 switch (carat) {
953 case C_CARATSET: /* ^^D */
954 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
955 goto ins_ch;
957 /* Save the ai string for later. */
958 ait.lb = NULL;
959 ait.lb_len = 0;
960 BINC_GOTO(sp, ait.lb, ait.lb_len, tp->ai);
961 memmove(ait.lb, tp->lb, tp->ai);
962 ait.ai = ait.len = tp->ai;
964 carat = C_NOCHANGE;
965 goto leftmargin;
966 case C_ZEROSET: /* 0^D */
967 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
968 goto ins_ch;
970 carat = C_NOTSET;
971 leftmargin: tp->lb[tp->cno - 1] = ' ';
972 tp->owrite += tp->cno - tp->offset;
973 tp->ai = 0;
974 tp->cno = tp->offset;
975 break;
976 case C_NOTSET: /* ^D */
977 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset)
978 goto ins_ch;
980 (void)txt_dent(sp, tp, 0);
981 break;
982 default:
983 abort();
985 break;
986 case K_VERASE: /* Erase the last character. */
987 /* If can erase over the prompt, return. */
988 if (tp->cno <= tp->offset && LF_ISSET(TXT_BS)) {
989 tp->term = TERM_BS;
990 goto done;
994 * If at the beginning of the line, try and drop back to a
995 * previously inserted line.
997 if (tp->cno == 0) {
998 if ((ntp =
999 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1000 goto err;
1001 tp = ntp;
1002 break;
1005 /* If nothing to erase, bell the user. */
1006 if (tp->cno <= tp->offset) {
1007 if (!LF_ISSET(TXT_REPLAY))
1008 txt_nomorech(sp);
1009 break;
1012 /* Drop back one character. */
1013 --tp->cno;
1016 * Historically, vi didn't replace the erased characters with
1017 * <blank>s, presumably because it's easier to fix a minor
1018 * typing mistake and continue on if the previous letters are
1019 * already there. This is a problem for incremental searching,
1020 * because the user can no longer tell where they are in the
1021 * colon command line because the cursor is at the last search
1022 * point in the screen. So, if incrementally searching, erase
1023 * the erased characters from the screen.
1025 if (FL_ISSET(is_flags, IS_RUNNING))
1026 tp->lb[tp->cno] = ' ';
1029 * Increment overwrite, decrement ai if deleted.
1031 * !!!
1032 * Historic vi did not permit users to use erase characters
1033 * to delete autoindent characters. We do. Eat hot death,
1034 * POSIX.
1036 ++tp->owrite;
1037 if (tp->cno < tp->ai)
1038 --tp->ai;
1040 /* Reset if we deleted an incremental search character. */
1041 if (FL_ISSET(is_flags, IS_RUNNING))
1042 FL_SET(is_flags, IS_RESTART);
1043 break;
1044 case K_VWERASE: /* Skip back one word. */
1046 * If at the beginning of the line, try and drop back to a
1047 * previously inserted line.
1049 if (tp->cno == 0) {
1050 if ((ntp =
1051 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1052 goto err;
1053 tp = ntp;
1057 * If at offset, nothing to erase so bell the user.
1059 if (tp->cno <= tp->offset) {
1060 if (!LF_ISSET(TXT_REPLAY))
1061 txt_nomorech(sp);
1062 break;
1066 * The first werase goes back to any autoindent column and the
1067 * second werase goes back to the offset.
1069 * !!!
1070 * Historic vi did not permit users to use erase characters to
1071 * delete autoindent characters.
1073 if (tp->ai && tp->cno > tp->ai)
1074 max = tp->ai;
1075 else {
1076 tp->ai = 0;
1077 max = tp->offset;
1080 /* Skip over trailing space characters. */
1081 while (tp->cno > max && isblank(tp->lb[tp->cno - 1])) {
1082 --tp->cno;
1083 ++tp->owrite;
1085 if (tp->cno == max)
1086 break;
1088 * There are three types of word erase found on UNIX systems.
1089 * They can be identified by how the string /a/b/c is treated
1090 * -- as 1, 3, or 6 words. Historic vi had two classes of
1091 * characters, and strings were delimited by them and
1092 * <blank>'s, so, 6 words. The historic tty interface used
1093 * <blank>'s to delimit strings, so, 1 word. The algorithm
1094 * offered in the 4.4BSD tty interface (as stty altwerase)
1095 * treats it as 3 words -- there are two classes of
1096 * characters, and strings are delimited by them and
1097 * <blank>'s. The difference is that the type of the first
1098 * erased character erased is ignored, which is exactly right
1099 * when erasing pathname components. The edit options
1100 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD tty
1101 * interface and the historic tty driver behavior,
1102 * respectively, and the default is the same as the historic
1103 * vi behavior.
1105 * Overwrite erased characters if doing incremental search;
1106 * see comment above.
1108 if (LF_ISSET(TXT_TTYWERASE))
1109 while (tp->cno > max) {
1110 --tp->cno;
1111 ++tp->owrite;
1112 if (FL_ISSET(is_flags, IS_RUNNING))
1113 tp->lb[tp->cno] = ' ';
1114 if (isblank(tp->lb[tp->cno - 1]))
1115 break;
1117 else {
1118 if (LF_ISSET(TXT_ALTWERASE)) {
1119 --tp->cno;
1120 ++tp->owrite;
1121 if (FL_ISSET(is_flags, IS_RUNNING))
1122 tp->lb[tp->cno] = ' ';
1123 if (isblank(tp->lb[tp->cno - 1]))
1124 break;
1126 if (tp->cno > max)
1127 tmp = inword(tp->lb[tp->cno - 1]);
1128 while (tp->cno > max) {
1129 --tp->cno;
1130 ++tp->owrite;
1131 if (FL_ISSET(is_flags, IS_RUNNING))
1132 tp->lb[tp->cno] = ' ';
1133 if (tmp != inword(tp->lb[tp->cno - 1])
1134 || isblank(tp->lb[tp->cno - 1]))
1135 break;
1139 /* Reset if we deleted an incremental search character. */
1140 if (FL_ISSET(is_flags, IS_RUNNING))
1141 FL_SET(is_flags, IS_RESTART);
1142 break;
1143 case K_VKILL: /* Restart this line. */
1145 * !!!
1146 * If at the beginning of the line, try and drop back to a
1147 * previously inserted line. Historic vi did not permit
1148 * users to go back to previous lines.
1150 if (tp->cno == 0) {
1151 if ((ntp =
1152 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1153 goto err;
1154 tp = ntp;
1157 /* If at offset, nothing to erase so bell the user. */
1158 if (tp->cno <= tp->offset) {
1159 if (!LF_ISSET(TXT_REPLAY))
1160 txt_nomorech(sp);
1161 break;
1165 * First kill goes back to any autoindent and second kill goes
1166 * back to the offset.
1168 * !!!
1169 * Historic vi did not permit users to use erase characters to
1170 * delete autoindent characters.
1172 if (tp->ai && tp->cno > tp->ai)
1173 max = tp->ai;
1174 else {
1175 tp->ai = 0;
1176 max = tp->offset;
1178 tp->owrite += tp->cno - max;
1181 * Overwrite erased characters if doing incremental search;
1182 * see comment above.
1184 if (FL_ISSET(is_flags, IS_RUNNING))
1185 do {
1186 tp->lb[--tp->cno] = ' ';
1187 } while (tp->cno > max);
1188 else
1189 tp->cno = max;
1191 /* Reset if we deleted an incremental search character. */
1192 if (FL_ISSET(is_flags, IS_RUNNING))
1193 FL_SET(is_flags, IS_RESTART);
1194 break;
1195 case K_CNTRLT: /* Add autoindent characters. */
1196 if (!LF_ISSET(TXT_CNTRLT))
1197 goto ins_ch;
1198 if (txt_dent(sp, tp, 1))
1199 goto err;
1200 goto ebuf_chk;
1201 case K_RIGHTBRACE:
1202 case K_RIGHTPAREN:
1203 if (LF_ISSET(TXT_SHOWMATCH))
1204 showmatch = 1;
1205 goto ins_ch;
1206 case K_BACKSLASH: /* Quote next erase/kill. */
1208 * !!!
1209 * Historic vi tried to make abbreviations after a backslash
1210 * escape work. If you did ":ab x y", and inserted "x\^H",
1211 * (assuming the erase character was ^H) you got "x^H", and
1212 * no abbreviation was done. If you inserted "x\z", however,
1213 * it tried to back up and do the abbreviation, i.e. replace
1214 * 'x' with 'y'. The problem was it got it wrong, and you
1215 * ended up with "zy\".
1217 * This is really hard to do (you have to remember the
1218 * word/non-word state, for example), and doesn't make any
1219 * sense to me. Both backslash and the characters it
1220 * (usually) escapes will individually trigger the
1221 * abbreviation, so I don't see why the combination of them
1222 * wouldn't. I don't expect to get caught on this one,
1223 * particularly since it never worked right, but I've been
1224 * wrong before.
1226 * Do the tests for abbreviations, so ":ab xa XA",
1227 * "ixa\<K_VERASE>" performs the abbreviation.
1229 quote = Q_BNEXT;
1230 goto insq_ch;
1231 case K_VLNEXT: /* Quote next character. */
1232 evp->e_c = '^';
1233 quote = Q_VNEXT;
1235 * Turn on the quote flag so that the underlying routines
1236 * quote the next character where it's possible. Turn off
1237 * the input mapbiting flag so that we don't remap the next
1238 * character.
1240 FL_SET(ec_flags, EC_QUOTED);
1241 FL_CLR(ec_flags, EC_MAPINPUT);
1244 * !!!
1245 * Skip the tests for abbreviations, so ":ab xa XA",
1246 * "ixa^V<space>" doesn't perform the abbreviation.
1248 goto insl_ch;
1249 case K_HEXCHAR:
1250 hexcnt = 1;
1251 goto insq_ch;
1252 default: /* Insert the character. */
1253 ins_ch: /*
1254 * Historically, vi eliminated nul's out of hand. If the
1255 * beautify option was set, it also deleted any unknown
1256 * ASCII value less than space (040) and the del character
1257 * (0177), except for tabs. Unknown is a key word here.
1258 * Most vi documentation claims that it deleted everything
1259 * but <tab>, <nl> and <ff>, as that's what the original
1260 * 4BSD documentation said. This is obviously wrong,
1261 * however, as <esc> would be included in that list. What
1262 * we do is eliminate any unquoted, iscntrl() character that
1263 * wasn't a replay and wasn't handled specially, except
1264 * <tab> or <ff>.
1266 if (LF_ISSET(TXT_BEAUTIFY) && iscntrl(evp->e_c) &&
1267 evp->e_value != K_FORMFEED && evp->e_value != K_TAB) {
1268 msgq(sp, M_BERR,
1269 "192|Illegal character; quote to enter");
1270 if (LF_ISSET(TXT_REPLAY))
1271 goto done;
1272 break;
1275 insq_ch: /*
1276 * If entering a non-word character after a word, check for
1277 * abbreviations. If there was one, discard replay characters.
1278 * If entering a blank character, check for unmap commands,
1279 * as well.
1281 if (!inword(evp->e_c)) {
1282 if (abb == AB_INWORD &&
1283 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) {
1284 if (txt_abbrev(sp, tp, &evp->e_c,
1285 LF_ISSET(TXT_INFOLINE), &tmp, &ab_turnoff))
1286 goto err;
1287 if (tmp) {
1288 if (LF_ISSET(TXT_RECORD))
1289 rcol -= tmp + 1;
1290 goto resolve;
1293 if (isblank(evp->e_c) && UNMAP_TST)
1294 txt_unmap(sp, tp, &ec_flags);
1296 if (abb != AB_NOTSET)
1297 abb = inword(evp->e_c) ? AB_INWORD : AB_NOTWORD;
1299 insl_ch: if (txt_insch(sp, tp, &evp->e_c, flags))
1300 goto err;
1303 * If we're using K_VLNEXT to quote the next character, then
1304 * we want the cursor to position itself on the ^ placeholder
1305 * we're displaying, to match historic practice.
1307 if (quote == Q_VNEXT) {
1308 --tp->cno;
1309 ++tp->owrite;
1313 * !!!
1314 * Translate "<CH_HEX>[isxdigit()]*" to a character with
1315 * a hex value: this test delimits the value by the max
1316 * number of hex bytes. Offset by one, we use 0 to mean
1317 * that we've found <CH_HEX>.
1319 if (hexcnt != 0 && hexcnt++ == sizeof(CHAR_T) * 2 + 1) {
1320 hexcnt = 0;
1321 if (txt_hex(sp, tp))
1322 goto err;
1326 * Check to see if we've crossed the margin.
1328 * !!!
1329 * In the historic vi, the wrapmargin value was figured out
1330 * using the display widths of the characters, i.e. <tab>
1331 * characters were counted as two characters if the list edit
1332 * option is set, but as the tabstop edit option number of
1333 * characters otherwise. That's what the vs_column() function
1334 * gives us, so we use it.
1336 if (margin != 0) {
1337 if (vs_column(sp, &tcol))
1338 goto err;
1339 if (tcol >= margin) {
1340 if (txt_margin(sp, tp, &wmt, &tmp, flags))
1341 goto err;
1342 if (tmp) {
1343 if (isblank(evp->e_c))
1344 wm_skip = 1;
1345 wm_set = 1;
1346 goto k_cr;
1352 * If we've reached the end of the buffer, then we need to
1353 * switch into insert mode. This happens when there's a
1354 * change to a mark and the user puts in more characters than
1355 * the length of the motion.
1357 ebuf_chk: if (tp->cno >= tp->len) {
1358 BINC_GOTO(sp, tp->lb, tp->lb_len, tp->len + 1);
1359 LF_SET(TXT_APPENDEOL);
1361 tp->lb[tp->cno] = CH_CURSOR;
1362 ++tp->insert;
1363 ++tp->len;
1366 /* Step the quote state forward. */
1367 if (quote != Q_NOTSET) {
1368 if (quote == Q_BNEXT)
1369 quote = Q_BTHIS;
1370 if (quote == Q_VNEXT)
1371 quote = Q_VTHIS;
1373 break;
1376 #ifdef DEBUG
1377 if (tp->cno + tp->insert + tp->owrite != tp->len) {
1378 msgq(sp, M_ERR,
1379 "len %u != cno: %u ai: %u insert %u overwrite %u",
1380 tp->len, tp->cno, tp->ai, tp->insert, tp->owrite);
1381 if (LF_ISSET(TXT_REPLAY))
1382 goto done;
1383 tp->len = tp->cno + tp->insert + tp->owrite;
1385 #endif
1387 resolve:/*
1388 * 1: If we don't need to know where the cursor really is and we're
1389 * replaying text, keep going.
1391 if (margin == 0 && LF_ISSET(TXT_REPLAY))
1392 goto replay;
1395 * 2: Reset the line. Don't bother unless we're about to wait on
1396 * a character or we need to know where the cursor really is.
1397 * We have to do this before showing matching characters so the
1398 * user can see what they're matching.
1400 if ((margin != 0 || !KEYS_WAITING(sp)) &&
1401 vs_change(sp, tp->lno, LINE_RESET))
1402 return (1);
1405 * 3: If there aren't keys waiting, display the matching character.
1406 * We have to do this before resolving any messages, otherwise
1407 * the error message from a missing match won't appear correctly.
1409 if (showmatch) {
1410 if (!KEYS_WAITING(sp) && txt_showmatch(sp, tp))
1411 return (1);
1412 showmatch = 0;
1416 * 4: If there have been messages and we're not editing on the colon
1417 * command line or doing file name completion, resolve them.
1419 if ((vip->totalcount != 0 || F_ISSET(gp, G_BELLSCHED)) &&
1420 !F_ISSET(sp, SC_TINPUT_INFO) && !filec_redraw &&
1421 vs_resolve(sp, NULL, 0))
1422 return (1);
1425 * 5: Refresh the screen if we're about to wait on a character or we
1426 * need to know where the cursor really is.
1428 if (margin != 0 || !KEYS_WAITING(sp)) {
1429 UPDATE_POSITION(sp, tp);
1430 if (vs_refresh(sp, margin != 0))
1431 return (1);
1434 /* 6: Proceed with the incremental search. */
1435 if (FL_ISSET(is_flags, IS_RUNNING) && txt_isrch(sp, vp, tp, &is_flags))
1436 return (1);
1438 /* 7: Next character... */
1439 if (LF_ISSET(TXT_REPLAY))
1440 goto replay;
1441 goto next;
1443 done: /* Leave input mode. */
1444 F_CLR(sp, SC_TINPUT);
1446 /* If recording for playback, save it. */
1447 if (LF_ISSET(TXT_RECORD))
1448 vip->rep_cnt = rcol;
1451 * If not working on the colon command line, set the final cursor
1452 * position.
1454 if (!F_ISSET(sp, SC_TINPUT_INFO)) {
1455 vp->m_final.lno = tp->lno;
1456 vp->m_final.cno = tp->cno;
1458 return (0);
1460 err:
1461 alloc_err:
1462 txt_err(sp, &sp->tiq);
1463 return (1);
1467 * txt_abbrev --
1468 * Handle abbreviations.
1470 static int
1471 txt_abbrev(sp, tp, pushcp, isinfoline, didsubp, turnoffp)
1472 SCR *sp;
1473 TEXT *tp;
1474 CHAR_T *pushcp;
1475 int isinfoline, *didsubp, *turnoffp;
1477 VI_PRIVATE *vip;
1478 CHAR_T ch, *p;
1479 SEQ *qp;
1480 size_t len, off;
1482 /* Check to make sure we're not at the start of an append. */
1483 *didsubp = 0;
1484 if (tp->cno == tp->offset)
1485 return (0);
1487 vip = VIP(sp);
1490 * Find the start of the "word".
1492 * !!!
1493 * We match historic practice, which, as far as I can tell, had an
1494 * off-by-one error. The way this worked was that when the inserted
1495 * text switched from a "word" character to a non-word character,
1496 * vi would check for possible abbreviations. It would then take the
1497 * type (i.e. word/non-word) of the character entered TWO characters
1498 * ago, and move backward in the text until reaching a character that
1499 * was not that type, or the beginning of the insert, the line, or
1500 * the file. For example, in the string "abc<space>", when the <space>
1501 * character triggered the abbreviation check, the type of the 'b'
1502 * character was used for moving through the string. Maybe there's a
1503 * reason for not using the first (i.e. 'c') character, but I can't
1504 * think of one.
1506 * Terminate at the beginning of the insert or the character after the
1507 * offset character -- both can be tested for using tp->offset.
1509 off = tp->cno - 1; /* Previous character. */
1510 p = tp->lb + off;
1511 len = 1; /* One character test. */
1512 if (off == tp->offset || isblank(p[-1]))
1513 goto search;
1514 if (inword(p[-1])) /* Move backward to change. */
1515 for (;;) {
1516 --off; --p; ++len;
1517 if (off == tp->offset || !inword(p[-1]))
1518 break;
1520 else
1521 for (;;) {
1522 --off; --p; ++len;
1523 if (off == tp->offset ||
1524 inword(p[-1]) || isblank(p[-1]))
1525 break;
1529 * !!!
1530 * Historic vi exploded abbreviations on the command line. This has
1531 * obvious problems in that unabbreviating the string can be extremely
1532 * tricky, particularly if the string has, say, an embedded escape
1533 * character. Personally, I think it's a stunningly bad idea. Other
1534 * examples of problems this caused in historic vi are:
1535 * :ab foo bar
1536 * :ab foo baz
1537 * results in "bar" being abbreviated to "baz", which wasn't what the
1538 * user had in mind at all. Also, the commands:
1539 * :ab foo bar
1540 * :unab foo<space>
1541 * resulted in an error message that "bar" wasn't mapped. Finally,
1542 * since the string was already exploded by the time the unabbreviate
1543 * command got it, all it knew was that an abbreviation had occurred.
1544 * Cleverly, it checked the replacement string for its unabbreviation
1545 * match, which meant that the commands:
1546 * :ab foo1 bar
1547 * :ab foo2 bar
1548 * :unab foo2
1549 * unabbreviate "foo1", and the commands:
1550 * :ab foo bar
1551 * :ab bar baz
1552 * unabbreviate "foo"!
1554 * Anyway, people neglected to first ask my opinion before they wrote
1555 * macros that depend on this stuff, so, we make this work as follows.
1556 * When checking for an abbreviation on the command line, if we get a
1557 * string which is <blank> terminated and which starts at the beginning
1558 * of the line, we check to see it is the abbreviate or unabbreviate
1559 * commands. If it is, turn abbreviations off and return as if no
1560 * abbreviation was found. Note also, minor trickiness, so that if
1561 * the user erases the line and starts another command, we turn the
1562 * abbreviations back on.
1564 * This makes the layering look like a Nachos Supreme.
1566 search: if (isinfoline)
1567 if (off == tp->ai || off == tp->offset)
1568 if (ex_is_abbrev(p, len)) {
1569 *turnoffp = 1;
1570 return (0);
1571 } else
1572 *turnoffp = 0;
1573 else
1574 if (*turnoffp)
1575 return (0);
1577 /* Check for any abbreviations. */
1578 if ((qp = seq_find(sp, NULL, NULL, p, len, SEQ_ABBREV, NULL)) == NULL)
1579 return (0);
1582 * Push the abbreviation onto the tty stack. Historically, characters
1583 * resulting from an abbreviation expansion were themselves subject to
1584 * map expansions, O_SHOWMATCH matching etc. This means the expanded
1585 * characters will be re-tested for abbreviations. It's difficult to
1586 * know what historic practice in this case was, since abbreviations
1587 * were applied to :colon command lines, so entering abbreviations that
1588 * looped was tricky, although possible. In addition, obvious loops
1589 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
1590 * silently only implement and/or display the last abbreviation.)
1592 * This implementation doesn't recover well from such abbreviations.
1593 * The main input loop counts abbreviated characters, and, when it
1594 * reaches a limit, discards any abbreviated characters on the queue.
1595 * It's difficult to back up to the original position, as the replay
1596 * queue would have to be adjusted, and the line state when an initial
1597 * abbreviated character was received would have to be saved.
1599 ch = *pushcp;
1600 if (v_event_push(sp, NULL, &ch, 1, CH_ABBREVIATED))
1601 return (1);
1602 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_ABBREVIATED))
1603 return (1);
1606 * If the size of the abbreviation is larger than or equal to the size
1607 * of the original text, move to the start of the replaced characters,
1608 * and add their length to the overwrite count.
1610 * If the abbreviation is smaller than the original text, we have to
1611 * delete the additional overwrite characters and copy down any insert
1612 * characters.
1614 tp->cno -= len;
1615 if (qp->olen >= len)
1616 tp->owrite += len;
1617 else {
1618 if (tp->insert)
1619 memmove(tp->lb + tp->cno + qp->olen,
1620 tp->lb + tp->cno + tp->owrite + len, tp->insert);
1621 tp->owrite += qp->olen;
1622 tp->len -= len - qp->olen;
1626 * We return the length of the abbreviated characters. This is so
1627 * the calling routine can replace the replay characters with the
1628 * abbreviation. This means that subsequent '.' commands will produce
1629 * the same text, regardless of intervening :[un]abbreviate commands.
1630 * This is historic practice.
1632 *didsubp = len;
1633 return (0);
1637 * txt_unmap --
1638 * Handle the unmap command.
1640 static void
1641 txt_unmap(sp, tp, ec_flagsp)
1642 SCR *sp;
1643 TEXT *tp;
1644 u_int32_t *ec_flagsp;
1646 size_t len, off;
1647 char *p;
1649 /* Find the beginning of this "word". */
1650 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1651 if (isblank(*p)) {
1652 ++p;
1653 break;
1655 ++len;
1656 if (off == tp->ai || off == tp->offset)
1657 break;
1661 * !!!
1662 * Historic vi exploded input mappings on the command line. See the
1663 * txt_abbrev() routine for an explanation of the problems inherent
1664 * in this.
1666 * We make this work as follows. If we get a string which is <blank>
1667 * terminated and which starts at the beginning of the line, we check
1668 * to see it is the unmap command. If it is, we return that the input
1669 * mapping should be turned off. Note also, minor trickiness, so that
1670 * if the user erases the line and starts another command, we go ahead
1671 * an turn mapping back on.
1673 if ((off == tp->ai || off == tp->offset) && ex_is_unmap(p, len))
1674 FL_CLR(*ec_flagsp, EC_MAPINPUT);
1675 else
1676 FL_SET(*ec_flagsp, EC_MAPINPUT);
1680 * txt_ai_resolve --
1681 * When a line is resolved by <esc>, review autoindent characters.
1683 static void
1684 txt_ai_resolve(sp, tp, changedp)
1685 SCR *sp;
1686 TEXT *tp;
1687 int *changedp;
1689 u_long ts;
1690 int del;
1691 size_t cno, len, new, old, scno, spaces, tab_after_sp, tabs;
1692 char *p;
1694 *changedp = 0;
1697 * If the line is empty, has an offset, or no autoindent
1698 * characters, we're done.
1700 if (!tp->len || tp->offset || !tp->ai)
1701 return;
1704 * If the length is less than or equal to the autoindent
1705 * characters, delete them.
1707 if (tp->len <= tp->ai) {
1708 tp->ai = tp->cno = tp->len = 0;
1709 return;
1713 * The autoindent characters plus any leading <blank> characters
1714 * in the line are resolved into the minimum number of characters.
1715 * Historic practice.
1717 ts = O_VAL(sp, O_TABSTOP);
1719 /* Figure out the last <blank> screen column. */
1720 for (p = tp->lb, scno = 0, len = tp->len,
1721 spaces = tab_after_sp = 0; len-- && isblank(*p); ++p)
1722 if (*p == '\t') {
1723 if (spaces)
1724 tab_after_sp = 1;
1725 scno += COL_OFF(scno, ts);
1726 } else {
1727 ++spaces;
1728 ++scno;
1732 * If there are no spaces, or no tabs after spaces and less than
1733 * ts spaces, it's already minimal.
1735 if (!spaces || !tab_after_sp && spaces < ts)
1736 return;
1738 /* Count up spaces/tabs needed to get to the target. */
1739 for (cno = 0, tabs = 0; cno + COL_OFF(cno, ts) <= scno; ++tabs)
1740 cno += COL_OFF(cno, ts);
1741 spaces = scno - cno;
1744 * Figure out how many characters we're dropping -- if we're not
1745 * dropping any, it's already minimal, we're done.
1747 old = p - tp->lb;
1748 new = spaces + tabs;
1749 if (old == new)
1750 return;
1752 /* Shift the rest of the characters down, adjust the counts. */
1753 del = old - new;
1754 memmove(p - del, p, tp->len - old);
1755 tp->len -= del;
1756 tp->cno -= del;
1758 /* Fill in space/tab characters. */
1759 for (p = tp->lb; tabs--;)
1760 *p++ = '\t';
1761 while (spaces--)
1762 *p++ = ' ';
1763 *changedp = 1;
1767 * v_txt_auto --
1768 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1769 * retrieve the line.
1771 * PUBLIC: int v_txt_auto __P((SCR *, db_recno_t, TEXT *, size_t, TEXT *));
1774 v_txt_auto(sp, lno, aitp, len, tp)
1775 SCR *sp;
1776 db_recno_t lno;
1777 TEXT *aitp, *tp;
1778 size_t len;
1780 size_t nlen;
1781 char *p, *t;
1783 if (aitp == NULL) {
1785 * If the ex append command is executed with an address of 0,
1786 * it's possible to get here with a line number of 0. Return
1787 * an indent of 0.
1789 if (lno == 0) {
1790 tp->ai = 0;
1791 return (0);
1793 if (db_get(sp, lno, DBG_FATAL, &t, &len))
1794 return (1);
1795 } else
1796 t = aitp->lb;
1798 /* Count whitespace characters. */
1799 for (p = t; len > 0; ++p, --len)
1800 if (!isblank(*p))
1801 break;
1803 /* Set count, check for no indentation. */
1804 if ((nlen = (p - t)) == 0)
1805 return (0);
1807 /* Make sure the buffer's big enough. */
1808 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + nlen);
1810 /* Copy the buffer's current contents up. */
1811 if (tp->len != 0)
1812 memmove(tp->lb + nlen, tp->lb, tp->len);
1813 tp->len += nlen;
1815 /* Copy the indentation into the new buffer. */
1816 memmove(tp->lb, t, nlen);
1818 /* Set the autoindent count. */
1819 tp->ai = nlen;
1820 return (0);
1824 * txt_backup --
1825 * Back up to the previously edited line.
1827 static TEXT *
1828 txt_backup(sp, tiqh, tp, flagsp)
1829 SCR *sp;
1830 TEXTH *tiqh;
1831 TEXT *tp;
1832 u_int32_t *flagsp;
1834 VI_PRIVATE *vip;
1835 TEXT *ntp;
1837 /* Get a handle on the previous TEXT structure. */
1838 if ((ntp = tp->q.cqe_prev) == (void *)tiqh) {
1839 if (!FL_ISSET(*flagsp, TXT_REPLAY))
1840 msgq(sp, M_BERR,
1841 "193|Already at the beginning of the insert");
1842 return (tp);
1845 /* Bookkeeping. */
1846 ntp->len = ntp->sv_len;
1848 /* Handle appending to the line. */
1849 vip = VIP(sp);
1850 if (ntp->owrite == 0 && ntp->insert == 0) {
1851 ntp->lb[ntp->len] = CH_CURSOR;
1852 ++ntp->insert;
1853 ++ntp->len;
1854 FL_SET(*flagsp, TXT_APPENDEOL);
1855 } else
1856 FL_CLR(*flagsp, TXT_APPENDEOL);
1858 /* Release the current TEXT. */
1859 CIRCLEQ_REMOVE(tiqh, tp, q);
1860 text_free(tp);
1862 /* Update the old line on the screen. */
1863 if (vs_change(sp, ntp->lno + 1, LINE_DELETE))
1864 return (NULL);
1866 /* Return the new/current TEXT. */
1867 return (ntp);
1871 * Text indentation is truly strange. ^T and ^D do movements to the next or
1872 * previous shiftwidth value, i.e. for a 1-based numbering, with shiftwidth=3,
1873 * ^T moves a cursor on the 7th, 8th or 9th column to the 10th column, and ^D
1874 * moves it back.
1876 * !!!
1877 * The ^T and ^D characters in historical vi had special meaning only when they
1878 * were the first characters entered after entering text input mode. As normal
1879 * erase characters couldn't erase autoindent characters (^T in this case), it
1880 * meant that inserting text into previously existing text was strange -- ^T
1881 * only worked if it was the first keystroke(s), and then could only be erased
1882 * using ^D. This implementation treats ^T specially anywhere it occurs in the
1883 * input, and permits the standard erase characters to erase the characters it
1884 * inserts.
1886 * !!!
1887 * A fun test is to try:
1888 * :se sw=4 ai list
1889 * i<CR>^Tx<CR>^Tx<CR>^Tx<CR>^Dx<CR>^Dx<CR>^Dx<esc>
1890 * Historic vi loses some of the '$' marks on the line ends, but otherwise gets
1891 * it right.
1893 * XXX
1894 * Technically, txt_dent should be part of the screen interface, as it requires
1895 * knowledge of character sizes, including <space>s, on the screen. It's here
1896 * because it's a complicated little beast, and I didn't want to shove it down
1897 * into the screen. It's probable that KEY_LEN will call into the screen once
1898 * there are screens with different character representations.
1900 * txt_dent --
1901 * Handle ^T indents, ^D outdents.
1903 * If anything changes here, check the ex version to see if it needs similar
1904 * changes.
1906 static int
1907 txt_dent(sp, tp, isindent)
1908 SCR *sp;
1909 TEXT *tp;
1910 int isindent;
1912 CHAR_T ch;
1913 u_long sw, ts;
1914 size_t cno, current, spaces, target, tabs, off;
1915 int ai_reset;
1917 ts = O_VAL(sp, O_TABSTOP);
1918 sw = O_VAL(sp, O_SHIFTWIDTH);
1921 * Since we don't know what precedes the character(s) being inserted
1922 * (or deleted), the preceding whitespace characters must be resolved.
1923 * An example is a <tab>, which doesn't need a full shiftwidth number
1924 * of columns because it's preceded by <space>s. This is easy to get
1925 * if the user sets shiftwidth to a value less than tabstop (or worse,
1926 * something for which tabstop isn't a multiple) and then uses ^T to
1927 * indent, and ^D to outdent.
1929 * Figure out the current and target screen columns. In the historic
1930 * vi, the autoindent column was NOT determined using display widths
1931 * of characters as was the wrapmargin column. For that reason, we
1932 * can't use the vs_column() function, but have to calculate it here.
1933 * This is slow, but it's normally only on the first few characters of
1934 * a line.
1936 for (current = cno = 0; cno < tp->cno; ++cno)
1937 current += tp->lb[cno] == '\t' ?
1938 COL_OFF(current, ts) : KEY_LEN(sp, tp->lb[cno]);
1940 target = current;
1941 if (isindent)
1942 target += COL_OFF(target, sw);
1943 else
1944 target -= --target % sw;
1947 * The AI characters will be turned into overwrite characters if the
1948 * cursor immediately follows them. We test both the cursor position
1949 * and the indent flag because there's no single test. (^T can only
1950 * be detected by the cursor position, and while we know that the test
1951 * is always true for ^D, the cursor can be in more than one place, as
1952 * "0^D" and "^D" are different.)
1954 ai_reset = !isindent || tp->cno == tp->ai + tp->offset;
1957 * Back up over any previous <blank> characters, changing them into
1958 * overwrite characters (including any ai characters). Then figure
1959 * out the current screen column.
1961 for (; tp->cno > tp->offset &&
1962 (tp->lb[tp->cno - 1] == ' ' || tp->lb[tp->cno - 1] == '\t');
1963 --tp->cno, ++tp->owrite);
1964 for (current = cno = 0; cno < tp->cno; ++cno)
1965 current += tp->lb[cno] == '\t' ?
1966 COL_OFF(current, ts) : KEY_LEN(sp, tp->lb[cno]);
1969 * If we didn't move up to or past the target, it's because there
1970 * weren't enough characters to delete, e.g. the first character
1971 * of the line was a tp->offset character, and the user entered
1972 * ^D to move to the beginning of a line. An example of this is:
1974 * :set ai sw=4<cr>i<space>a<esc>i^T^D
1976 * Otherwise, count up the total spaces/tabs needed to get from the
1977 * beginning of the line (or the last non-<blank> character) to the
1978 * target.
1980 if (current >= target)
1981 spaces = tabs = 0;
1982 else {
1983 for (cno = current,
1984 tabs = 0; cno + COL_OFF(cno, ts) <= target; ++tabs)
1985 cno += COL_OFF(cno, ts);
1986 spaces = target - cno;
1989 /* If we overwrote ai characters, reset the ai count. */
1990 if (ai_reset)
1991 tp->ai = tabs + spaces;
1994 * Call txt_insch() to insert each character, so that we get the
1995 * correct effect when we add a <tab> to replace N <spaces>.
1997 for (ch = '\t'; tabs > 0; --tabs)
1998 (void)txt_insch(sp, tp, &ch, 0);
1999 for (ch = ' '; spaces > 0; --spaces)
2000 (void)txt_insch(sp, tp, &ch, 0);
2001 return (0);
2005 * txt_fc --
2006 * File name completion.
2008 static int
2009 txt_fc(sp, tp, redrawp)
2010 SCR *sp;
2011 TEXT *tp;
2012 int *redrawp;
2014 struct stat sb;
2015 ARGS **argv;
2016 CHAR_T s_ch;
2017 EXCMD cmd;
2018 size_t indx, len, nlen, off;
2019 int argc, trydir;
2020 char *p, *t;
2022 trydir = 0;
2023 *redrawp = 0;
2026 * Find the beginning of this "word" -- if we're at the beginning
2027 * of the line, it's a special case.
2029 if (tp->cno == 1) {
2030 len = 0;
2031 p = tp->lb;
2032 } else
2033 retry: for (len = 0,
2034 off = tp->cno - 1, p = tp->lb + off;; --off, --p) {
2035 if (isblank(*p)) {
2036 ++p;
2037 break;
2039 ++len;
2040 if (off == tp->ai || off == tp->offset)
2041 break;
2045 * Get enough space for a wildcard character.
2047 * XXX
2048 * This won't work for "foo\", since the \ will escape the expansion
2049 * character. I'm not sure if that's a bug or not...
2051 off = p - tp->lb;
2052 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + 1);
2053 p = tp->lb + off;
2055 s_ch = p[len];
2056 p[len] = '*';
2058 /* Build an ex command, and call the ex expansion routines. */
2059 ex_cinit(sp, &cmd, 0, 0, OOBLNO, OOBLNO, 0);
2060 if (argv_exp2(sp, &cmd, p, len + 1)) {
2061 p[len] = s_ch;
2062 return (0);
2064 argc = cmd.argc;
2065 argv = cmd.argv;
2067 p[len] = s_ch;
2069 switch (argc) {
2070 case 0: /* No matches. */
2071 if (!trydir)
2072 (void)sp->gp->scr_bell(sp);
2073 return (0);
2074 case 1: /* One match. */
2075 /* If something changed, do the exchange. */
2076 nlen = strlen(cmd.argv[0]->bp);
2077 if (len != nlen || memcmp(cmd.argv[0]->bp, p, len))
2078 break;
2080 /* If haven't done a directory test, do it now. */
2081 if (!trydir &&
2082 !stat(cmd.argv[0]->bp, &sb) && S_ISDIR(sb.st_mode)) {
2083 p += len;
2084 goto isdir;
2087 /* If nothing changed, period, ring the bell. */
2088 if (!trydir)
2089 (void)sp->gp->scr_bell(sp);
2090 return (0);
2091 default: /* Multiple matches. */
2092 *redrawp = 1;
2093 if (txt_fc_col(sp, argc, argv))
2094 return (1);
2096 /* Find the length of the shortest match. */
2097 for (nlen = cmd.argv[0]->len; --argc > 0;) {
2098 if (cmd.argv[argc]->len < nlen)
2099 nlen = cmd.argv[argc]->len;
2100 for (indx = 0; indx < nlen &&
2101 cmd.argv[argc]->bp[indx] == cmd.argv[0]->bp[indx];
2102 ++indx);
2103 nlen = indx;
2105 break;
2108 /* Overwrite the expanded text first. */
2109 for (t = cmd.argv[0]->bp; len > 0 && nlen > 0; --len, --nlen)
2110 *p++ = *t++;
2112 /* If lost text, make the remaining old text overwrite characters. */
2113 if (len) {
2114 tp->cno -= len;
2115 tp->owrite += len;
2118 /* Overwrite any overwrite characters next. */
2119 for (; nlen > 0 && tp->owrite > 0; --nlen, --tp->owrite, ++tp->cno)
2120 *p++ = *t++;
2122 /* Shift remaining text up, and move the cursor to the end. */
2123 if (nlen) {
2124 off = p - tp->lb;
2125 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + nlen);
2126 p = tp->lb + off;
2128 tp->cno += nlen;
2129 tp->len += nlen;
2131 if (tp->insert != 0)
2132 (void)memmove(p + nlen, p, tp->insert);
2133 while (nlen--)
2134 *p++ = *t++;
2137 /* If a single match and it's a directory, retry it. */
2138 if (argc == 1 && !stat(cmd.argv[0]->bp, &sb) && S_ISDIR(sb.st_mode)) {
2139 isdir: if (tp->owrite == 0) {
2140 off = p - tp->lb;
2141 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + 1);
2142 p = tp->lb + off;
2143 if (tp->insert != 0)
2144 (void)memmove(p + 1, p, tp->insert);
2145 ++tp->len;
2146 } else
2147 --tp->owrite;
2149 ++tp->cno;
2150 *p++ = '/';
2152 trydir = 1;
2153 goto retry;
2155 return (0);
2159 * txt_fc_col --
2160 * Display file names for file name completion.
2162 static int
2163 txt_fc_col(sp, argc, argv)
2164 SCR *sp;
2165 int argc;
2166 ARGS **argv;
2168 ARGS **av;
2169 CHAR_T *p;
2170 GS *gp;
2171 size_t base, cnt, col, colwidth, numrows, numcols, prefix, row;
2172 int ac, nf, reset;
2174 gp = sp->gp;
2176 /* Trim any directory prefix common to all of the files. */
2177 if ((p = strrchr(argv[0]->bp, '/')) == NULL)
2178 prefix = 0;
2179 else {
2180 prefix = (p - argv[0]->bp) + 1;
2181 for (ac = argc - 1, av = argv + 1; ac > 0; --ac, ++av)
2182 if (av[0]->len < prefix ||
2183 memcmp(av[0]->bp, argv[0]->bp, prefix)) {
2184 prefix = 0;
2185 break;
2190 * Figure out the column width for the longest name. Output is done on
2191 * 6 character "tab" boundaries for no particular reason. (Since we
2192 * don't output tab characters, we ignore the terminal's tab settings.)
2193 * Ignore the user's tab setting because we have no idea how reasonable
2194 * it is.
2196 for (ac = argc, av = argv, colwidth = 0; ac > 0; --ac, ++av) {
2197 for (col = 0, p = av[0]->bp + prefix; *p != '\0'; ++p)
2198 col += KEY_LEN(sp, *p);
2199 if (col > colwidth)
2200 colwidth = col;
2202 colwidth += COL_OFF(colwidth, 6);
2205 * Writing to the bottom line of the screen is always turned off when
2206 * SC_TINPUT_INFO is set. Turn it back on, we know what we're doing.
2208 if (F_ISSET(sp, SC_TINPUT_INFO)) {
2209 reset = 1;
2210 F_CLR(sp, SC_TINPUT_INFO);
2211 } else
2212 reset = 0;
2214 #define CHK_INTR \
2215 if (F_ISSET(gp, G_INTERRUPTED)) \
2216 goto intr;
2218 /* If the largest file name is too large, just print them. */
2219 if (colwidth > sp->cols) {
2220 p = msg_print(sp, av[0]->bp + prefix, &nf);
2221 for (ac = argc, av = argv; ac > 0; --ac, ++av) {
2222 (void)ex_printf(sp, "%s\n", p);
2223 if (F_ISSET(gp, G_INTERRUPTED))
2224 break;
2226 if (nf)
2227 FREE_SPACE(sp, p, 0);
2228 CHK_INTR;
2229 } else {
2230 /* Figure out the number of columns. */
2231 numcols = (sp->cols - 1) / colwidth;
2232 if (argc > numcols) {
2233 numrows = argc / numcols;
2234 if (argc % numcols)
2235 ++numrows;
2236 } else
2237 numrows = 1;
2239 /* Display the files in sorted order. */
2240 for (row = 0; row < numrows; ++row) {
2241 for (base = row, col = 0; col < numcols; ++col) {
2242 p = msg_print(sp, argv[base]->bp + prefix, &nf);
2243 cnt = ex_printf(sp, "%s", p);
2244 if (nf)
2245 FREE_SPACE(sp, p, 0);
2246 CHK_INTR;
2247 if ((base += numrows) >= argc)
2248 break;
2249 (void)ex_printf(sp,
2250 "%*s", (int)(colwidth - cnt), "");
2251 CHK_INTR;
2253 (void)ex_puts(sp, "\n");
2254 CHK_INTR;
2256 (void)ex_puts(sp, "\n");
2257 CHK_INTR;
2259 (void)ex_fflush(sp);
2261 if (0) {
2262 intr: F_CLR(gp, G_INTERRUPTED);
2264 if (reset)
2265 F_SET(sp, SC_TINPUT_INFO);
2267 return (0);
2271 * txt_emark --
2272 * Set the end mark on the line.
2274 static int
2275 txt_emark(sp, tp, cno)
2276 SCR *sp;
2277 TEXT *tp;
2278 size_t cno;
2280 CHAR_T ch, *kp;
2281 size_t chlen, nlen, olen;
2282 char *p;
2284 ch = CH_ENDMARK;
2287 * The end mark may not be the same size as the current character.
2288 * Don't let the line shift.
2290 nlen = KEY_LEN(sp, ch);
2291 if (tp->lb[cno] == '\t')
2292 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &olen);
2293 else
2294 olen = KEY_LEN(sp, tp->lb[cno]);
2297 * If the line got longer, well, it's weird, but it's easy. If
2298 * it's the same length, it's easy. If it got shorter, we have
2299 * to fix it up.
2301 if (olen > nlen) {
2302 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + olen);
2303 chlen = olen - nlen;
2304 if (tp->insert != 0)
2305 memmove(tp->lb + cno + 1 + chlen,
2306 tp->lb + cno + 1, tp->insert);
2308 tp->len += chlen;
2309 tp->owrite += chlen;
2310 p = tp->lb + cno;
2311 if (tp->lb[cno] == '\t')
2312 for (cno += chlen; chlen--;)
2313 *p++ = ' ';
2314 else
2315 for (kp = KEY_NAME(sp, tp->lb[cno]),
2316 cno += chlen; chlen--;)
2317 *p++ = *kp++;
2319 tp->lb[cno] = ch;
2320 return (vs_change(sp, tp->lno, LINE_RESET));
2324 * txt_err --
2325 * Handle an error during input processing.
2327 static void
2328 txt_err(sp, tiqh)
2329 SCR *sp;
2330 TEXTH *tiqh;
2332 db_recno_t lno;
2335 * The problem with input processing is that the cursor is at an
2336 * indeterminate position since some input may have been lost due
2337 * to a malloc error. So, try to go back to the place from which
2338 * the cursor started, knowing that it may no longer be available.
2340 * We depend on at least one line number being set in the text
2341 * chain.
2343 for (lno = tiqh->cqh_first->lno;
2344 !db_exist(sp, lno) && lno > 0; --lno);
2346 sp->lno = lno == 0 ? 1 : lno;
2347 sp->cno = 0;
2349 /* Redraw the screen, just in case. */
2350 F_SET(sp, SC_SCR_REDRAW);
2354 * txt_hex --
2355 * Let the user insert any character value they want.
2357 * !!!
2358 * This is an extension. The pattern "^X[0-9a-fA-F]*" is a way
2359 * for the user to specify a character value which their keyboard
2360 * may not be able to enter.
2362 static int
2363 txt_hex(sp, tp)
2364 SCR *sp;
2365 TEXT *tp;
2367 CHAR_T savec;
2368 size_t len, off;
2369 u_long value;
2370 char *p, *wp;
2373 * Null-terminate the string. Since nul isn't a legal hex value,
2374 * this should be okay, and lets us use a local routine, which
2375 * presumably understands the character set, to convert the value.
2377 savec = tp->lb[tp->cno];
2378 tp->lb[tp->cno] = 0;
2380 /* Find the previous CH_HEX character. */
2381 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off, ++len) {
2382 if (*p == CH_HEX) {
2383 wp = p + 1;
2384 break;
2386 /* Not on this line? Shouldn't happen. */
2387 if (off == tp->ai || off == tp->offset)
2388 goto nothex;
2391 /* If length of 0, then it wasn't a hex value. */
2392 if (len == 0)
2393 goto nothex;
2395 /* Get the value. */
2396 errno = 0;
2397 value = strtol(wp, NULL, 16);
2398 if (errno || value > MAX_CHAR_T) {
2399 nothex: tp->lb[tp->cno] = savec;
2400 return (0);
2403 /* Restore the original character. */
2404 tp->lb[tp->cno] = savec;
2406 /* Adjust the bookkeeping. */
2407 tp->cno -= len;
2408 tp->len -= len;
2409 tp->lb[tp->cno - 1] = value;
2411 /* Copy down any overwrite characters. */
2412 if (tp->owrite)
2413 memmove(tp->lb + tp->cno, tp->lb + tp->cno + len, tp->owrite);
2415 /* Copy down any insert characters. */
2416 if (tp->insert)
2417 memmove(tp->lb + tp->cno + tp->owrite,
2418 tp->lb + tp->cno + tp->owrite + len, tp->insert);
2420 return (0);
2424 * txt_insch --
2426 * !!!
2427 * Historic vi did a special screen optimization for tab characters. As an
2428 * example, for the keystrokes "iabcd<esc>0C<tab>", the tab overwrote the
2429 * rest of the string when it was displayed.
2431 * Because early versions of this implementation redisplayed the entire line
2432 * on each keystroke, the "bcd" was pushed to the right as it ignored that
2433 * the user had "promised" to change the rest of the characters. However,
2434 * the historic vi implementation had an even worse bug: given the keystrokes
2435 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
2436 * on the second <esc> key.
2438 * POSIX 1003.2 requires (will require) that this be fixed, specifying that
2439 * vi overwrite characters the user has committed to changing, on the basis
2440 * of the screen space they require, but that it not overwrite other characters.
2442 static int
2443 txt_insch(sp, tp, chp, flags)
2444 SCR *sp;
2445 TEXT *tp;
2446 CHAR_T *chp;
2447 u_int flags;
2449 CHAR_T *kp, savech;
2450 size_t chlen, cno, copydown, olen, nlen;
2451 char *p;
2454 * The 'R' command does one-for-one replacement, because there's
2455 * no way to know how many characters the user intends to replace.
2457 if (LF_ISSET(TXT_REPLACE)) {
2458 if (tp->owrite) {
2459 --tp->owrite;
2460 tp->lb[tp->cno++] = *chp;
2461 return (0);
2463 } else if (tp->owrite) { /* Overwrite a character. */
2464 cno = tp->cno;
2467 * If the old or new characters are tabs, then the length of the
2468 * display depends on the character position in the display. We
2469 * don't even try to handle this here, just ask the screen.
2471 if (*chp == '\t') {
2472 savech = tp->lb[cno];
2473 tp->lb[cno] = '\t';
2474 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &nlen);
2475 tp->lb[cno] = savech;
2476 } else
2477 nlen = KEY_LEN(sp, *chp);
2480 * Eat overwrite characters until we run out of them or we've
2481 * handled the length of the new character. If we only eat
2482 * part of an overwrite character, break it into its component
2483 * elements and display the remaining components.
2485 for (copydown = 0; nlen != 0 && tp->owrite != 0;) {
2486 --tp->owrite;
2488 if (tp->lb[cno] == '\t')
2489 (void)vs_columns(sp,
2490 tp->lb, tp->lno, &cno, &olen);
2491 else
2492 olen = KEY_LEN(sp, tp->lb[cno]);
2494 if (olen == nlen) {
2495 nlen = 0;
2496 break;
2498 if (olen < nlen) {
2499 ++copydown;
2500 nlen -= olen;
2501 } else {
2502 BINC_RET(sp,
2503 tp->lb, tp->lb_len, tp->len + olen);
2504 chlen = olen - nlen;
2505 memmove(tp->lb + cno + 1 + chlen,
2506 tp->lb + cno + 1, tp->owrite + tp->insert);
2508 tp->len += chlen;
2509 tp->owrite += chlen;
2510 if (tp->lb[cno] == '\t')
2511 for (p = tp->lb + cno + 1; chlen--;)
2512 *p++ = ' ';
2513 else
2514 for (kp =
2515 KEY_NAME(sp, tp->lb[cno]) + nlen,
2516 p = tp->lb + cno + 1; chlen--;)
2517 *p++ = *kp++;
2518 nlen = 0;
2519 break;
2524 * If had to erase several characters, we adjust the total
2525 * count, and if there are any characters left, shift them
2526 * into position.
2528 if (copydown != 0 && (tp->len -= copydown) != 0)
2529 memmove(tp->lb + cno, tp->lb + cno + copydown,
2530 tp->owrite + tp->insert + copydown);
2532 /* If we had enough overwrite characters, we're done. */
2533 if (nlen == 0) {
2534 tp->lb[tp->cno++] = *chp;
2535 return (0);
2539 /* Check to see if the character fits into the input buffer. */
2540 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + 1);
2542 ++tp->len;
2543 if (tp->insert) { /* Insert a character. */
2544 if (tp->insert == 1)
2545 tp->lb[tp->cno + 1] = tp->lb[tp->cno];
2546 else
2547 memmove(tp->lb + tp->cno + 1,
2548 tp->lb + tp->cno, tp->owrite + tp->insert);
2550 tp->lb[tp->cno++] = *chp;
2551 return (0);
2555 * txt_isrch --
2556 * Do an incremental search.
2558 static int
2559 txt_isrch(sp, vp, tp, is_flagsp)
2560 SCR *sp;
2561 VICMD *vp;
2562 TEXT *tp;
2563 u_int8_t *is_flagsp;
2565 MARK start;
2566 db_recno_t lno;
2567 u_int sf;
2569 /* If it's a one-line screen, we don't do incrementals. */
2570 if (IS_ONELINE(sp)) {
2571 FL_CLR(*is_flagsp, IS_RUNNING);
2572 return (0);
2576 * If the user erases back to the beginning of the buffer, there's
2577 * nothing to search for. Reset the cursor to the starting point.
2579 if (tp->cno <= 1) {
2580 vp->m_final = vp->m_start;
2581 return (0);
2585 * If it's an RE quote character, and not quoted, ignore it until
2586 * we get another character.
2588 if (tp->lb[tp->cno - 1] == '\\' &&
2589 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2590 return (0);
2593 * If it's a magic shell character, and not quoted, reset the cursor
2594 * to the starting point.
2596 if (strchr(O_STR(sp, O_SHELLMETA), tp->lb[tp->cno - 1]) != NULL &&
2597 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2598 vp->m_final = vp->m_start;
2601 * If we see the search pattern termination character, then quit doing
2602 * an incremental search. There may be more, e.g., ":/foo/;/bar/",
2603 * and we can't handle that incrementally. Also, reset the cursor to
2604 * the original location, the ex search routines don't know anything
2605 * about incremental searches.
2607 if (tp->lb[0] == tp->lb[tp->cno - 1] &&
2608 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\')) {
2609 vp->m_final = vp->m_start;
2610 FL_CLR(*is_flagsp, IS_RUNNING);
2611 return (0);
2615 * Remember the input line and discard the special input map,
2616 * but don't overwrite the input line on the screen.
2618 lno = tp->lno;
2619 F_SET(VIP(sp), VIP_S_MODELINE);
2620 F_CLR(sp, SC_TINPUT | SC_TINPUT_INFO);
2621 if (txt_map_end(sp))
2622 return (1);
2625 * Specify a starting point and search. If we find a match, move to
2626 * it and refresh the screen. If we didn't find the match, then we
2627 * beep the screen. When searching from the original cursor position,
2628 * we have to move the cursor, otherwise, we don't want to move the
2629 * cursor in case the text at the current position continues to match.
2631 if (FL_ISSET(*is_flagsp, IS_RESTART)) {
2632 start = vp->m_start;
2633 sf = SEARCH_SET;
2634 } else {
2635 start = vp->m_final;
2636 sf = SEARCH_INCR | SEARCH_SET;
2639 if (tp->lb[0] == '/' ?
2640 !f_search(sp,
2641 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf) :
2642 !b_search(sp,
2643 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf)) {
2644 sp->lno = vp->m_final.lno;
2645 sp->cno = vp->m_final.cno;
2646 FL_CLR(*is_flagsp, IS_RESTART);
2648 if (!KEYS_WAITING(sp) && vs_refresh(sp, 0))
2649 return (1);
2650 } else
2651 FL_SET(*is_flagsp, IS_RESTART);
2653 /* Reinstantiate the special input map. */
2654 if (txt_map_init(sp))
2655 return (1);
2656 F_CLR(VIP(sp), VIP_S_MODELINE);
2657 F_SET(sp, SC_TINPUT | SC_TINPUT_INFO);
2659 /* Reset the line number of the input line. */
2660 tp->lno = TMAP[0].lno;
2663 * If the colon command-line moved, i.e. the screen scrolled,
2664 * refresh the input line.
2666 * XXX
2667 * We shouldn't be calling vs_line, here -- we need dirty bits
2668 * on entries in the SMAP array.
2670 if (lno != TMAP[0].lno) {
2671 if (vs_line(sp, &TMAP[0], NULL, NULL))
2672 return (1);
2673 (void)sp->gp->scr_refresh(sp, 0);
2675 return (0);
2679 * txt_resolve --
2680 * Resolve the input text chain into the file.
2682 static int
2683 txt_resolve(sp, tiqh, flags)
2684 SCR *sp;
2685 TEXTH *tiqh;
2686 u_int32_t flags;
2688 VI_PRIVATE *vip;
2689 TEXT *tp;
2690 db_recno_t lno;
2691 int changed;
2694 * The first line replaces a current line, and all subsequent lines
2695 * are appended into the file. Resolve autoindented characters for
2696 * each line before committing it. If the latter causes the line to
2697 * change, we have to redisplay it, otherwise the information cached
2698 * about the line will be wrong.
2700 vip = VIP(sp);
2701 tp = tiqh->cqh_first;
2703 if (LF_ISSET(TXT_AUTOINDENT))
2704 txt_ai_resolve(sp, tp, &changed);
2705 else
2706 changed = 0;
2707 if (db_set(sp, tp->lno, tp->lb, tp->len) ||
2708 changed && vs_change(sp, tp->lno, LINE_RESET))
2709 return (1);
2711 for (lno = tp->lno; (tp = tp->q.cqe_next) != (void *)&sp->tiq; ++lno) {
2712 if (LF_ISSET(TXT_AUTOINDENT))
2713 txt_ai_resolve(sp, tp, &changed);
2714 else
2715 changed = 0;
2716 if (db_append(sp, 0, lno, tp->lb, tp->len) ||
2717 changed && vs_change(sp, tp->lno, LINE_RESET))
2718 return (1);
2722 * Clear the input flag, the look-aside buffer is no longer valid.
2723 * Has to be done as part of text resolution, or upon return we'll
2724 * be looking at incorrect data.
2726 F_CLR(sp, SC_TINPUT);
2728 return (0);
2732 * txt_showmatch --
2733 * Show a character match.
2735 * !!!
2736 * Historic vi tried to display matches even in the :colon command line.
2737 * I think not.
2739 static int
2740 txt_showmatch(sp, tp)
2741 SCR *sp;
2742 TEXT *tp;
2744 GS *gp;
2745 VCS cs;
2746 MARK m;
2747 int cnt, endc, startc;
2749 gp = sp->gp;
2752 * Do a refresh first, in case we haven't done one in awhile,
2753 * so the user can see what we're complaining about.
2755 UPDATE_POSITION(sp, tp);
2756 if (vs_refresh(sp, 1))
2757 return (1);
2760 * We don't display the match if it's not on the screen. Find
2761 * out what the first character on the screen is.
2763 if (vs_sm_position(sp, &m, 0, P_TOP))
2764 return (1);
2766 /* Initialize the getc() interface. */
2767 cs.cs_lno = tp->lno;
2768 cs.cs_cno = tp->cno - 1;
2769 if (cs_init(sp, &cs))
2770 return (1);
2771 startc = (endc = cs.cs_ch) == ')' ? '(' : '{';
2773 /* Search for the match. */
2774 for (cnt = 1;;) {
2775 if (cs_prev(sp, &cs))
2776 return (1);
2777 if (cs.cs_flags != 0) {
2778 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF) {
2779 msgq(sp, M_BERR,
2780 "Unmatched %s", KEY_NAME(sp, endc));
2781 return (0);
2783 continue;
2785 if (cs.cs_ch == endc)
2786 ++cnt;
2787 else if (cs.cs_ch == startc && --cnt == 0)
2788 break;
2791 /* If the match is on the screen, move to it. */
2792 if (cs.cs_lno < m.lno || cs.cs_lno == m.lno && cs.cs_cno < m.cno)
2793 return (0);
2794 sp->lno = cs.cs_lno;
2795 sp->cno = cs.cs_cno;
2796 if (vs_refresh(sp, 1))
2797 return (1);
2799 /* Wait for timeout or character arrival. */
2800 return (v_event_get(sp,
2801 NULL, O_VAL(sp, O_MATCHTIME) * 100, EC_TIMEOUT));
2805 * txt_margin --
2806 * Handle margin wrap.
2808 static int
2809 txt_margin(sp, tp, wmtp, didbreak, flags)
2810 SCR *sp;
2811 TEXT *tp, *wmtp;
2812 int *didbreak;
2813 u_int32_t flags;
2815 VI_PRIVATE *vip;
2816 size_t len, off;
2817 char *p, *wp;
2819 /* Find the nearest previous blank. */
2820 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --off, --p, ++len) {
2821 if (isblank(*p)) {
2822 wp = p + 1;
2823 break;
2827 * If reach the start of the line, there's nowhere to break.
2829 * !!!
2830 * Historic vi belled each time a character was entered after
2831 * crossing the margin until a space was entered which could
2832 * be used to break the line. I don't as it tends to wake the
2833 * cats.
2835 if (off == tp->ai || off == tp->offset) {
2836 *didbreak = 0;
2837 return (0);
2842 * Store saved information about the rest of the line in the
2843 * wrapmargin TEXT structure.
2845 * !!!
2846 * The offset field holds the length of the current characters
2847 * that the user entered, but which are getting split to the new
2848 * line -- it's going to be used to set the cursor value when we
2849 * move to the new line.
2851 vip = VIP(sp);
2852 wmtp->lb = p + 1;
2853 wmtp->offset = len;
2854 wmtp->insert = LF_ISSET(TXT_APPENDEOL) ? tp->insert - 1 : tp->insert;
2855 wmtp->owrite = tp->owrite;
2857 /* Correct current bookkeeping information. */
2858 tp->cno -= len;
2859 if (LF_ISSET(TXT_APPENDEOL)) {
2860 tp->len -= len + tp->owrite + (tp->insert - 1);
2861 tp->insert = 1;
2862 } else {
2863 tp->len -= len + tp->owrite + tp->insert;
2864 tp->insert = 0;
2866 tp->owrite = 0;
2869 * !!!
2870 * Delete any trailing whitespace from the current line.
2872 for (;; --p, --off) {
2873 if (!isblank(*p))
2874 break;
2875 --tp->cno;
2876 --tp->len;
2877 if (off == tp->ai || off == tp->offset)
2878 break;
2880 *didbreak = 1;
2881 return (0);
2885 * txt_Rresolve --
2886 * Resolve the input line for the 'R' command.
2888 static void
2889 txt_Rresolve(sp, tiqh, tp, orig_len)
2890 SCR *sp;
2891 TEXTH *tiqh;
2892 TEXT *tp;
2893 const size_t orig_len;
2895 TEXT *ttp;
2896 size_t input_len, retain;
2897 char *p;
2900 * Check to make sure that the cursor hasn't moved beyond
2901 * the end of the line.
2903 if (tp->owrite == 0)
2904 return;
2907 * Calculate how many characters the user has entered,
2908 * plus the blanks erased by <carriage-return>/<newline>s.
2910 for (ttp = tiqh->cqh_first, input_len = 0;;) {
2911 input_len += ttp == tp ? tp->cno : ttp->len + ttp->R_erase;
2912 if ((ttp = ttp->q.cqe_next) == (void *)&sp->tiq)
2913 break;
2917 * If the user has entered less characters than the original line
2918 * was long, restore any overwriteable characters to the original
2919 * characters. These characters are entered as "insert characters",
2920 * because they're after the cursor and we don't want to lose them.
2921 * (This is okay because the R command has no insert characters.)
2922 * We set owrite to 0 so that the insert characters don't get copied
2923 * to somewhere else, which means that the line and the length have
2924 * to be adjusted here as well.
2926 * We have to retrieve the original line because the original pinned
2927 * page has long since been discarded. If it doesn't exist, that's
2928 * okay, the user just extended the file.
2930 if (input_len < orig_len) {
2931 retain = MIN(tp->owrite, orig_len - input_len);
2932 if (db_get(sp,
2933 tiqh->cqh_first->lno, DBG_FATAL | DBG_NOCACHE, &p, NULL))
2934 return;
2935 memcpy(tp->lb + tp->cno, p + input_len, retain);
2936 tp->len -= tp->owrite - retain;
2937 tp->owrite = 0;
2938 tp->insert += retain;
2943 * txt_nomorech --
2944 * No more characters message.
2946 static void
2947 txt_nomorech(sp)
2948 SCR *sp;
2950 msgq(sp, M_BERR, "194|No more characters to erase");