another CHAR_T buffer size calculation bug fix
[nvi.git] / vi / v_txt.c
blob5580f4d4ede944fb1daf07947f11e2b30250b773
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.100 2000/08/30 18:03:44 skimo Exp $ (Berkeley) $Date: 2000/08/30 18:03:44 $";
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_T *, 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_T *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_T *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 BINC_RETW(sp, tp->lb, tp->lb_len, len);
312 MEMMOVEW(tp->lb, lp, len);
313 } else
314 tp->len = 0;
315 } else {
316 newtp: if ((tp = text_init(sp, lp, len, len + 32)) == NULL)
317 return (1);
318 CIRCLEQ_INSERT_HEAD(tiqh, tp, q);
321 /* Set default termination condition. */
322 tp->term = TERM_OK;
324 /* Set the starting line, column. */
325 tp->lno = sp->lno;
326 tp->cno = sp->cno;
329 * Set the insert and overwrite counts. If overwriting characters,
330 * do insertion afterward. If not overwriting characters, assume
331 * doing insertion. If change is to a mark, emphasize it with an
332 * CH_ENDMARK character.
334 if (len) {
335 if (LF_ISSET(TXT_OVERWRITE)) {
336 tp->owrite = (tm->cno - tp->cno) + 1;
337 tp->insert = (len - tm->cno) - 1;
338 } else
339 tp->insert = len - tp->cno;
341 if (LF_ISSET(TXT_EMARK) && txt_emark(sp, tp, tm->cno))
342 return (1);
346 * Many of the special cases in text input are to handle autoindent
347 * support. Somebody decided that it would be a good idea if "^^D"
348 * and "0^D" deleted all of the autoindented characters. In an editor
349 * that takes single character input from the user, this beggars the
350 * imagination. Note also, "^^D" resets the next lines' autoindent,
351 * but "0^D" doesn't.
353 * We assume that autoindent only happens on empty lines, so insert
354 * and overwrite will be zero. If doing autoindent, figure out how
355 * much indentation we need and fill it in. Update input column and
356 * screen cursor as necessary.
358 if (LF_ISSET(TXT_AUTOINDENT) && ai_line != OOBLNO) {
359 if (v_txt_auto(sp, ai_line, NULL, 0, tp))
360 return (1);
361 tp->cno = tp->ai;
362 } else {
364 * The cc and S commands have a special feature -- leading
365 * <blank> characters are handled as autoindent characters.
366 * Beauty!
368 if (LF_ISSET(TXT_AICHARS)) {
369 tp->offset = 0;
370 tp->ai = tp->cno;
371 } else
372 tp->offset = tp->cno;
375 /* If getting a command buffer from the user, there may be a prompt. */
376 if (LF_ISSET(TXT_PROMPT)) {
377 tp->lb[tp->cno++] = prompt;
378 ++tp->len;
379 ++tp->offset;
383 * If appending after the end-of-line, add a space into the buffer
384 * and move the cursor right. This space is inserted, i.e. pushed
385 * along, and then deleted when the line is resolved. Assumes that
386 * the cursor is already positioned at the end of the line. This
387 * avoids the nastiness of having the cursor reside on a magical
388 * column, i.e. a column that doesn't really exist. The only down
389 * side is that we may wrap lines or scroll the screen before it's
390 * strictly necessary. Not a big deal.
392 if (LF_ISSET(TXT_APPENDEOL)) {
393 tp->lb[tp->cno] = CH_CURSOR;
394 ++tp->len;
395 ++tp->insert;
396 (void)vs_change(sp, tp->lno, LINE_RESET);
400 * Historic practice is that the wrapmargin value was a distance
401 * from the RIGHT-HAND margin, not the left. It's more useful to
402 * us as a distance from the left-hand margin, i.e. the same as
403 * the wraplen value. The wrapmargin option is historic practice.
404 * Nvi added the wraplen option so that it would be possible to
405 * edit files with consistent margins without knowing the number of
406 * columns in the window.
408 * XXX
409 * Setting margin causes a significant performance hit. Normally
410 * we don't update the screen if there are keys waiting, but we
411 * have to if margin is set, otherwise the screen routines don't
412 * know where the cursor is.
414 * !!!
415 * Abbreviated keys were affected by the wrapmargin option in the
416 * historic 4BSD vi. Mapped keys were usually, but sometimes not.
417 * See the comment in vi/v_text():set_txt_std for more information.
419 * !!!
420 * One more special case. If an inserted <blank> character causes
421 * wrapmargin to split the line, the next user entered character is
422 * discarded if it's a <space> character.
424 wm_set = wm_skip = 0;
425 if (LF_ISSET(TXT_WRAPMARGIN))
426 if ((margin = O_VAL(sp, O_WRAPMARGIN)) != 0)
427 margin = sp->cols - margin;
428 else
429 margin = O_VAL(sp, O_WRAPLEN);
430 else
431 margin = 0;
433 /* Initialize abbreviation checks. */
434 abcnt = ab_turnoff = 0;
435 abb = F_ISSET(gp, G_ABBREV) &&
436 LF_ISSET(TXT_MAPINPUT) ? AB_INWORD : AB_NOTSET;
439 * Set up the dot command. Dot commands are done by saving the actual
440 * characters and then reevaluating them so that things like wrapmargin
441 * can change between the insert and the replay.
443 * !!!
444 * Historically, vi did not remap or reabbreviate replayed input. (It
445 * did beep at you if you changed an abbreviation and then replayed the
446 * input. We're not that compatible.) We don't have to do anything to
447 * avoid remapping, as we're not getting characters from the terminal
448 * routines. Turn the abbreviation check off.
450 * XXX
451 * It would be nice if we could swallow backspaces and such, but it's
452 * not all that easy to do. What we can do is turn off the common
453 * error messages during the replay. Otherwise, when the user enters
454 * an illegal command, e.g., "Ia<erase><erase><erase><erase>b<escape>",
455 * and then does a '.', they get a list of error messages after command
456 * completion.
458 rcol = 0;
459 if (LF_ISSET(TXT_REPLAY)) {
460 abb = AB_NOTSET;
461 LF_CLR(TXT_RECORD);
464 /* Other text input mode setup. */
465 quote = Q_NOTSET;
466 carat = C_NOTSET;
467 FL_INIT(is_flags,
468 LF_ISSET(TXT_SEARCHINCR) ? IS_RESTART | IS_RUNNING : 0);
469 filec_redraw = hexcnt = showmatch = 0;
471 /* Initialize input flags. */
472 ec_flags = LF_ISSET(TXT_MAPINPUT) ? EC_MAPINPUT : 0;
474 /* Refresh the screen. */
475 UPDATE_POSITION(sp, tp);
476 if (vs_refresh(sp, 1))
477 return (1);
479 /* If it's dot, just do it now. */
480 if (F_ISSET(vp, VC_ISDOT))
481 goto replay;
483 /* Get an event. */
484 evp = &ev;
485 next: if (v_event_get(sp, evp, 0, ec_flags))
486 return (1);
489 * If file completion overwrote part of the screen and nothing else has
490 * been displayed, clean up. We don't do this as part of the normal
491 * message resolution because we know the user is on the colon command
492 * line and there's no reason to enter explicit characters to continue.
494 if (filec_redraw && !F_ISSET(sp, SC_SCR_EXWROTE)) {
495 filec_redraw = 0;
497 fc.e_event = E_REPAINT;
498 fc.e_flno = vip->totalcount >=
499 sp->rows ? 1 : sp->rows - vip->totalcount;
500 fc.e_tlno = sp->rows;
501 vip->linecount = vip->lcontinue = vip->totalcount = 0;
502 (void)v_erepaint(sp, &fc);
503 (void)vs_refresh(sp, 1);
506 /* Deal with all non-character events. */
507 switch (evp->e_event) {
508 case E_CHARACTER:
509 break;
510 case E_ERR:
511 case E_EOF:
512 F_SET(sp, SC_EXIT_FORCE);
513 return (1);
514 case E_INTERRUPT:
516 * !!!
517 * Historically, <interrupt> exited the user from text input
518 * mode or cancelled a colon command, and returned to command
519 * mode. It also beeped the terminal, but that seems a bit
520 * excessive.
522 goto k_escape;
523 case E_REPAINT:
524 if (v_erepaint(sp, &ev))
525 return (1);
526 goto next;
527 case E_WRESIZE:
528 /* <resize> interrupts the input mode. */
529 v_emsg(sp, NULL, VIM_WRESIZE);
530 goto k_escape;
531 default:
532 v_event_err(sp, evp);
533 goto k_escape;
537 * !!!
538 * If the first character of the input is a nul, replay the previous
539 * input. (Historically, it's okay to replay non-existent input.)
540 * This was not documented as far as I know, and is a great test of vi
541 * clones.
543 if (rcol == 0 && !LF_ISSET(TXT_REPLAY) && evp->e_c == '\0') {
544 if (vip->rep == NULL)
545 goto done;
547 abb = AB_NOTSET;
548 LF_CLR(TXT_RECORD);
549 LF_SET(TXT_REPLAY);
550 goto replay;
554 * File name completion and colon command-line editing. We don't
555 * have enough meta characters, so we expect people to overload
556 * them. If the two characters are the same, then we do file name
557 * completion if the cursor is past the first column, and do colon
558 * command-line editing if it's not.
560 if (quote == Q_NOTSET) {
561 int L__cedit, L__filec;
563 L__cedit = L__filec = 0;
564 if (LF_ISSET(TXT_CEDIT) && O_STR(sp, O_CEDIT) != NULL &&
565 O_STR(sp, O_CEDIT)[0] == evp->e_c)
566 L__cedit = 1;
567 if (LF_ISSET(TXT_FILEC) && O_STR(sp, O_FILEC) != NULL &&
568 O_STR(sp, O_FILEC)[0] == evp->e_c)
569 L__filec = 1;
570 if (L__cedit == 1 && (L__filec == 0 || tp->cno == tp->offset)) {
571 tp->term = TERM_CEDIT;
572 goto k_escape;
574 if (L__filec == 1) {
575 if (txt_fc(sp, tp, &filec_redraw))
576 goto err;
577 goto resolve;
581 /* Abbreviation overflow check. See comment in txt_abbrev(). */
582 #define MAX_ABBREVIATION_EXPANSION 256
583 if (FL_ISSET(evp->e_flags, CH_ABBREVIATED)) {
584 if (++abcnt > MAX_ABBREVIATION_EXPANSION) {
585 if (v_event_flush(sp, CH_ABBREVIATED))
586 msgq(sp, M_ERR,
587 "191|Abbreviation exceeded expansion limit: characters discarded");
588 abcnt = 0;
589 if (LF_ISSET(TXT_REPLAY))
590 goto done;
591 goto resolve;
593 } else
594 abcnt = 0;
596 /* Check to see if the character fits into the replay buffers. */
597 if (LF_ISSET(TXT_RECORD)) {
598 BINC_GOTO(sp, (char *)vip->rep,
599 vip->rep_len, (rcol + 1) * sizeof(EVENT));
600 vip->rep[rcol++] = *evp;
603 replay: if (LF_ISSET(TXT_REPLAY)) {
604 if (rcol == vip->rep_cnt)
605 goto k_escape;
606 evp = vip->rep + rcol++;
609 /* Wrapmargin check for leading space. */
610 if (wm_skip) {
611 wm_skip = 0;
612 if (evp->e_c == ' ')
613 goto resolve;
616 /* If quoted by someone else, simply insert the character. */
617 if (FL_ISSET(evp->e_flags, CH_QUOTED))
618 goto insq_ch;
621 * !!!
622 * If this character was quoted by a K_VLNEXT or a backslash, replace
623 * the placeholder (a carat or a backslash) with the new character.
624 * If it was quoted by a K_VLNEXT, we've already adjusted the cursor
625 * because it has to appear on top of the placeholder character. If
626 * it was quoted by a backslash, adjust the cursor now, the cursor
627 * doesn't appear on top of it. Historic practice in both cases.
629 * Skip tests for abbreviations; ":ab xa XA" followed by "ixa^V<space>"
630 * doesn't perform an abbreviation. Special case, ^V^J (not ^V^M) is
631 * the same as ^J, historically.
633 if (quote == Q_BTHIS || quote == Q_VTHIS) {
634 FL_CLR(ec_flags, EC_QUOTED);
635 if (LF_ISSET(TXT_MAPINPUT))
636 FL_SET(ec_flags, EC_MAPINPUT);
638 if (quote == Q_BTHIS &&
639 (evp->e_value == K_VERASE || evp->e_value == K_VKILL)) {
640 quote = Q_NOTSET;
641 --tp->cno;
642 ++tp->owrite;
643 goto insl_ch;
645 if (quote == Q_VTHIS && evp->e_value != K_NL) {
646 quote = Q_NOTSET;
647 goto insl_ch;
649 quote = Q_NOTSET;
653 * !!!
654 * Translate "<CH_HEX>[isxdigit()]*" to a character with a hex value:
655 * this test delimits the value by any non-hex character. Offset by
656 * one, we use 0 to mean that we've found <CH_HEX>.
658 if (hexcnt > 1 && !isxdigit(evp->e_c)) {
659 hexcnt = 0;
660 if (txt_hex(sp, tp))
661 goto err;
664 switch (evp->e_value) {
665 case K_CR: /* Carriage return. */
666 case K_NL: /* New line. */
667 /* Return in script windows and the command line. */
668 k_cr: if (LF_ISSET(TXT_CR)) {
669 static CHAR_T cr[] = { '\r', 0 };
671 * If this was a map, we may have not displayed
672 * the line. Display it, just in case.
674 * If a script window and not the colon line,
675 * push a <cr> so it gets executed.
677 if (LF_ISSET(TXT_INFOLINE)) {
678 if (vs_change(sp, tp->lno, LINE_RESET))
679 goto err;
680 } else if (F_ISSET(sp, SC_SCRIPT))
681 (void)v_event_push(sp, NULL, cr, 1, CH_NOMAP);
683 /* Set term condition: if empty. */
684 if (tp->cno <= tp->offset)
685 tp->term = TERM_CR;
687 * Set term condition: if searching incrementally and
688 * the user entered a pattern, return a completed
689 * search, regardless if the entire pattern was found.
691 if (FL_ISSET(is_flags, IS_RUNNING) &&
692 tp->cno >= tp->offset + 1)
693 tp->term = TERM_SEARCH;
695 goto k_escape;
698 #define LINE_RESOLVE { \
699 /* \
700 * Handle abbreviations. If there was one, discard the \
701 * replay characters. \
702 */ \
703 if (abb == AB_INWORD && \
704 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) { \
705 if (txt_abbrev(sp, tp, &evp->e_c, \
706 LF_ISSET(TXT_INFOLINE), &tmp, \
707 &ab_turnoff)) \
708 goto err; \
709 if (tmp) { \
710 if (LF_ISSET(TXT_RECORD)) \
711 rcol -= tmp + 1; \
712 goto resolve; \
715 if (abb != AB_NOTSET) \
716 abb = AB_NOTWORD; \
717 if (UNMAP_TST) \
718 txt_unmap(sp, tp, &ec_flags); \
719 /* \
720 * Delete any appended cursor. It's possible to get in \
721 * situations where TXT_APPENDEOL is set but tp->insert \
722 * is 0 when using the R command and all the characters \
723 * are tp->owrite characters. \
724 */ \
725 if (LF_ISSET(TXT_APPENDEOL) && tp->insert > 0) { \
726 --tp->len; \
727 --tp->insert; \
730 LINE_RESOLVE;
733 * Save the current line information for restoration in
734 * txt_backup(), and set the line final length.
736 tp->sv_len = tp->len;
737 tp->sv_cno = tp->cno;
738 tp->len = tp->cno;
740 /* Update the old line. */
741 if (vs_change(sp, tp->lno, LINE_RESET))
742 goto err;
745 * Historic practice, when the autoindent edit option was set,
746 * was to delete <blank> characters following the inserted
747 * newline. This affected the 'R', 'c', and 's' commands; 'c'
748 * and 's' retained the insert characters only, 'R' moved the
749 * overwrite and insert characters into the next TEXT structure.
750 * We keep track of the number of characters erased for the 'R'
751 * command so that the final resolution of the line is correct.
753 tp->R_erase = 0;
754 owrite = tp->owrite;
755 insert = tp->insert;
756 if (LF_ISSET(TXT_REPLACE) && owrite != 0) {
757 for (p = tp->lb + tp->cno; owrite > 0 && isblank(*p);
758 ++p, --owrite, ++tp->R_erase);
759 if (owrite == 0)
760 for (; insert > 0 && isblank(*p);
761 ++p, ++tp->R_erase, --insert);
762 } else {
763 p = tp->lb + tp->cno + owrite;
764 if (O_ISSET(sp, O_AUTOINDENT))
765 for (; insert > 0 &&
766 isblank(*p); ++p, --insert);
767 owrite = 0;
771 * !!!
772 * Create a new line and insert the new TEXT into the queue.
773 * DON'T insert until the old line has been updated, or the
774 * inserted line count in line.c:db_get() will be wrong.
776 if ((ntp = text_init(sp, p,
777 insert + owrite, insert + owrite + 32)) == NULL)
778 goto err;
779 CIRCLEQ_INSERT_TAIL(&sp->tiq, ntp, q);
781 /* Set up bookkeeping for the new line. */
782 ntp->insert = insert;
783 ntp->owrite = owrite;
784 ntp->lno = tp->lno + 1;
787 * Reset the autoindent line value. 0^D keeps the autoindent
788 * line from changing, ^D changes the level, even if there were
789 * no characters in the old line. Note, if using the current
790 * tp structure, use the cursor as the length, the autoindent
791 * characters may have been erased.
793 if (LF_ISSET(TXT_AUTOINDENT)) {
794 if (carat == C_NOCHANGE) {
795 if (v_txt_auto(sp, OOBLNO, &ait, ait.ai, ntp))
796 goto err;
797 FREE_SPACEW(sp, ait.lb, ait.lb_len);
798 } else
799 if (v_txt_auto(sp, OOBLNO, tp, tp->cno, ntp))
800 goto err;
801 carat = C_NOTSET;
804 /* Reset the cursor. */
805 ntp->cno = ntp->ai;
808 * If we're here because wrapmargin was set and we've broken a
809 * line, there may be additional information (i.e. the start of
810 * a line) in the wmt structure.
812 if (wm_set) {
813 if (wmt.offset != 0 ||
814 wmt.owrite != 0 || wmt.insert != 0) {
815 #define WMTSPACE wmt.offset + wmt.owrite + wmt.insert
816 BINC_GOTOW(sp, ntp->lb,
817 ntp->lb_len, ntp->len + WMTSPACE + 32);
818 MEMMOVEW(ntp->lb + ntp->cno, wmt.lb, WMTSPACE);
819 ntp->len += WMTSPACE;
820 ntp->cno += wmt.offset;
821 ntp->owrite = wmt.owrite;
822 ntp->insert = wmt.insert;
824 wm_set = 0;
827 /* New lines are TXT_APPENDEOL. */
828 if (ntp->owrite == 0 && ntp->insert == 0) {
829 BINC_GOTOW(sp, ntp->lb, ntp->lb_len, ntp->len + 1);
830 LF_SET(TXT_APPENDEOL);
831 ntp->lb[ntp->cno] = CH_CURSOR;
832 ++ntp->insert;
833 ++ntp->len;
836 /* Swap old and new TEXT's, and update the new line. */
837 tp = ntp;
838 if (vs_change(sp, tp->lno, LINE_INSERT))
839 goto err;
841 goto resolve;
842 case K_ESCAPE: /* Escape. */
843 if (!LF_ISSET(TXT_ESCAPE))
844 goto ins_ch;
846 /* If we have a count, start replaying the input. */
847 if (rcount > 1) {
848 --rcount;
850 rcol = 0;
851 abb = AB_NOTSET;
852 LF_CLR(TXT_RECORD);
853 LF_SET(TXT_REPLAY);
856 * Some commands (e.g. 'o') need a <newline> for each
857 * repetition.
859 if (LF_ISSET(TXT_ADDNEWLINE))
860 goto k_cr;
863 * The R command turns into the 'a' command after the
864 * first repetition.
866 if (LF_ISSET(TXT_REPLACE)) {
867 tp->insert = tp->owrite;
868 tp->owrite = 0;
869 LF_CLR(TXT_REPLACE);
871 goto replay;
874 /* Set term condition: if empty. */
875 if (tp->cno <= tp->offset)
876 tp->term = TERM_ESC;
878 * Set term condition: if searching incrementally and the user
879 * entered a pattern, return a completed search, regardless if
880 * the entire pattern was found.
882 if (FL_ISSET(is_flags, IS_RUNNING) && tp->cno >= tp->offset + 1)
883 tp->term = TERM_SEARCH;
885 k_escape: LINE_RESOLVE;
888 * Clean up for the 'R' command, restoring overwrite
889 * characters, and making them into insert characters.
891 if (LF_ISSET(TXT_REPLACE))
892 txt_Rresolve(sp, &sp->tiq, tp, len);
895 * If there are any overwrite characters, copy down
896 * any insert characters, and decrement the length.
898 if (tp->owrite) {
899 if (tp->insert)
900 MEMMOVEW(tp->lb + tp->cno,
901 tp->lb + tp->cno + tp->owrite, tp->insert);
902 tp->len -= tp->owrite;
906 * Optionally resolve the lines into the file. If not
907 * resolving the lines into the file, end the line with
908 * a nul. If the line is empty, then set the length to
909 * 0, the termination condition has already been set.
911 * XXX
912 * This is wrong, should pass back a length.
914 if (LF_ISSET(TXT_RESOLVE)) {
915 if (txt_resolve(sp, &sp->tiq, flags))
916 goto err;
917 } else {
918 BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
919 tp->lb[tp->len] = '\0';
923 * Set the return cursor position to rest on the last
924 * inserted character.
926 if (tp->cno != 0)
927 --tp->cno;
929 /* Update the last line. */
930 if (vs_change(sp, tp->lno, LINE_RESET))
931 return (1);
932 goto done;
933 case K_CARAT: /* Delete autoindent chars. */
934 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
935 carat = C_CARATSET;
936 goto ins_ch;
937 case K_ZERO: /* Delete autoindent chars. */
938 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
939 carat = C_ZEROSET;
940 goto ins_ch;
941 case K_CNTRLD: /* Delete autoindent char. */
943 * If in the first column or no characters to erase, ignore
944 * the ^D (this matches historic practice). If not doing
945 * autoindent or already inserted non-ai characters, it's a
946 * literal. The latter test is done in the switch, as the
947 * CARAT forms are N + 1, not N.
949 if (!LF_ISSET(TXT_AUTOINDENT))
950 goto ins_ch;
951 if (tp->cno == 0)
952 goto resolve;
954 switch (carat) {
955 case C_CARATSET: /* ^^D */
956 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
957 goto ins_ch;
959 /* Save the ai string for later. */
960 ait.lb = NULL;
961 ait.lb_len = 0;
962 BINC_GOTOW(sp, ait.lb, ait.lb_len, tp->ai);
963 MEMMOVEW(ait.lb, tp->lb, tp->ai);
964 ait.ai = ait.len = tp->ai;
966 carat = C_NOCHANGE;
967 goto leftmargin;
968 case C_ZEROSET: /* 0^D */
969 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
970 goto ins_ch;
972 carat = C_NOTSET;
973 leftmargin: tp->lb[tp->cno - 1] = ' ';
974 tp->owrite += tp->cno - tp->offset;
975 tp->ai = 0;
976 tp->cno = tp->offset;
977 break;
978 case C_NOTSET: /* ^D */
979 if (tp->ai == 0 || tp->cno > tp->ai + tp->offset)
980 goto ins_ch;
982 (void)txt_dent(sp, tp, 0);
983 break;
984 default:
985 abort();
987 break;
988 case K_VERASE: /* Erase the last character. */
989 /* If can erase over the prompt, return. */
990 if (tp->cno <= tp->offset && LF_ISSET(TXT_BS)) {
991 tp->term = TERM_BS;
992 goto done;
996 * If at the beginning of the line, try and drop back to a
997 * previously inserted line.
999 if (tp->cno == 0) {
1000 if ((ntp =
1001 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1002 goto err;
1003 tp = ntp;
1004 break;
1007 /* If nothing to erase, bell the user. */
1008 if (tp->cno <= tp->offset) {
1009 if (!LF_ISSET(TXT_REPLAY))
1010 txt_nomorech(sp);
1011 break;
1014 /* Drop back one character. */
1015 --tp->cno;
1018 * Historically, vi didn't replace the erased characters with
1019 * <blank>s, presumably because it's easier to fix a minor
1020 * typing mistake and continue on if the previous letters are
1021 * already there. This is a problem for incremental searching,
1022 * because the user can no longer tell where they are in the
1023 * colon command line because the cursor is at the last search
1024 * point in the screen. So, if incrementally searching, erase
1025 * the erased characters from the screen.
1027 if (FL_ISSET(is_flags, IS_RUNNING))
1028 tp->lb[tp->cno] = ' ';
1031 * Increment overwrite, decrement ai if deleted.
1033 * !!!
1034 * Historic vi did not permit users to use erase characters
1035 * to delete autoindent characters. We do. Eat hot death,
1036 * POSIX.
1038 ++tp->owrite;
1039 if (tp->cno < tp->ai)
1040 --tp->ai;
1042 /* Reset if we deleted an incremental search character. */
1043 if (FL_ISSET(is_flags, IS_RUNNING))
1044 FL_SET(is_flags, IS_RESTART);
1045 break;
1046 case K_VWERASE: /* Skip back one word. */
1048 * If at the beginning of the line, try and drop back to a
1049 * previously inserted line.
1051 if (tp->cno == 0) {
1052 if ((ntp =
1053 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1054 goto err;
1055 tp = ntp;
1059 * If at offset, nothing to erase so bell the user.
1061 if (tp->cno <= tp->offset) {
1062 if (!LF_ISSET(TXT_REPLAY))
1063 txt_nomorech(sp);
1064 break;
1068 * The first werase goes back to any autoindent column and the
1069 * second werase goes back to the offset.
1071 * !!!
1072 * Historic vi did not permit users to use erase characters to
1073 * delete autoindent characters.
1075 if (tp->ai && tp->cno > tp->ai)
1076 max = tp->ai;
1077 else {
1078 tp->ai = 0;
1079 max = tp->offset;
1082 /* Skip over trailing space characters. */
1083 while (tp->cno > max && isblank(tp->lb[tp->cno - 1])) {
1084 --tp->cno;
1085 ++tp->owrite;
1087 if (tp->cno == max)
1088 break;
1090 * There are three types of word erase found on UNIX systems.
1091 * They can be identified by how the string /a/b/c is treated
1092 * -- as 1, 3, or 6 words. Historic vi had two classes of
1093 * characters, and strings were delimited by them and
1094 * <blank>'s, so, 6 words. The historic tty interface used
1095 * <blank>'s to delimit strings, so, 1 word. The algorithm
1096 * offered in the 4.4BSD tty interface (as stty altwerase)
1097 * treats it as 3 words -- there are two classes of
1098 * characters, and strings are delimited by them and
1099 * <blank>'s. The difference is that the type of the first
1100 * erased character erased is ignored, which is exactly right
1101 * when erasing pathname components. The edit options
1102 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD tty
1103 * interface and the historic tty driver behavior,
1104 * respectively, and the default is the same as the historic
1105 * vi behavior.
1107 * Overwrite erased characters if doing incremental search;
1108 * see comment above.
1110 if (LF_ISSET(TXT_TTYWERASE))
1111 while (tp->cno > max) {
1112 --tp->cno;
1113 ++tp->owrite;
1114 if (FL_ISSET(is_flags, IS_RUNNING))
1115 tp->lb[tp->cno] = ' ';
1116 if (isblank(tp->lb[tp->cno - 1]))
1117 break;
1119 else {
1120 if (LF_ISSET(TXT_ALTWERASE)) {
1121 --tp->cno;
1122 ++tp->owrite;
1123 if (FL_ISSET(is_flags, IS_RUNNING))
1124 tp->lb[tp->cno] = ' ';
1125 if (isblank(tp->lb[tp->cno - 1]))
1126 break;
1128 if (tp->cno > max)
1129 tmp = inword(tp->lb[tp->cno - 1]);
1130 while (tp->cno > max) {
1131 --tp->cno;
1132 ++tp->owrite;
1133 if (FL_ISSET(is_flags, IS_RUNNING))
1134 tp->lb[tp->cno] = ' ';
1135 if (tmp != inword(tp->lb[tp->cno - 1])
1136 || isblank(tp->lb[tp->cno - 1]))
1137 break;
1141 /* Reset if we deleted an incremental search character. */
1142 if (FL_ISSET(is_flags, IS_RUNNING))
1143 FL_SET(is_flags, IS_RESTART);
1144 break;
1145 case K_VKILL: /* Restart this line. */
1147 * !!!
1148 * If at the beginning of the line, try and drop back to a
1149 * previously inserted line. Historic vi did not permit
1150 * users to go back to previous lines.
1152 if (tp->cno == 0) {
1153 if ((ntp =
1154 txt_backup(sp, &sp->tiq, tp, &flags)) == NULL)
1155 goto err;
1156 tp = ntp;
1159 /* If at offset, nothing to erase so bell the user. */
1160 if (tp->cno <= tp->offset) {
1161 if (!LF_ISSET(TXT_REPLAY))
1162 txt_nomorech(sp);
1163 break;
1167 * First kill goes back to any autoindent and second kill goes
1168 * back to the offset.
1170 * !!!
1171 * Historic vi did not permit users to use erase characters to
1172 * delete autoindent characters.
1174 if (tp->ai && tp->cno > tp->ai)
1175 max = tp->ai;
1176 else {
1177 tp->ai = 0;
1178 max = tp->offset;
1180 tp->owrite += tp->cno - max;
1183 * Overwrite erased characters if doing incremental search;
1184 * see comment above.
1186 if (FL_ISSET(is_flags, IS_RUNNING))
1187 do {
1188 tp->lb[--tp->cno] = ' ';
1189 } while (tp->cno > max);
1190 else
1191 tp->cno = max;
1193 /* Reset if we deleted an incremental search character. */
1194 if (FL_ISSET(is_flags, IS_RUNNING))
1195 FL_SET(is_flags, IS_RESTART);
1196 break;
1197 case K_CNTRLT: /* Add autoindent characters. */
1198 if (!LF_ISSET(TXT_CNTRLT))
1199 goto ins_ch;
1200 if (txt_dent(sp, tp, 1))
1201 goto err;
1202 goto ebuf_chk;
1203 case K_RIGHTBRACE:
1204 case K_RIGHTPAREN:
1205 if (LF_ISSET(TXT_SHOWMATCH))
1206 showmatch = 1;
1207 goto ins_ch;
1208 case K_BACKSLASH: /* Quote next erase/kill. */
1210 * !!!
1211 * Historic vi tried to make abbreviations after a backslash
1212 * escape work. If you did ":ab x y", and inserted "x\^H",
1213 * (assuming the erase character was ^H) you got "x^H", and
1214 * no abbreviation was done. If you inserted "x\z", however,
1215 * it tried to back up and do the abbreviation, i.e. replace
1216 * 'x' with 'y'. The problem was it got it wrong, and you
1217 * ended up with "zy\".
1219 * This is really hard to do (you have to remember the
1220 * word/non-word state, for example), and doesn't make any
1221 * sense to me. Both backslash and the characters it
1222 * (usually) escapes will individually trigger the
1223 * abbreviation, so I don't see why the combination of them
1224 * wouldn't. I don't expect to get caught on this one,
1225 * particularly since it never worked right, but I've been
1226 * wrong before.
1228 * Do the tests for abbreviations, so ":ab xa XA",
1229 * "ixa\<K_VERASE>" performs the abbreviation.
1231 quote = Q_BNEXT;
1232 goto insq_ch;
1233 case K_VLNEXT: /* Quote next character. */
1234 evp->e_c = '^';
1235 quote = Q_VNEXT;
1237 * Turn on the quote flag so that the underlying routines
1238 * quote the next character where it's possible. Turn off
1239 * the input mapbiting flag so that we don't remap the next
1240 * character.
1242 FL_SET(ec_flags, EC_QUOTED);
1243 FL_CLR(ec_flags, EC_MAPINPUT);
1246 * !!!
1247 * Skip the tests for abbreviations, so ":ab xa XA",
1248 * "ixa^V<space>" doesn't perform the abbreviation.
1250 goto insl_ch;
1251 case K_HEXCHAR:
1252 hexcnt = 1;
1253 goto insq_ch;
1254 default: /* Insert the character. */
1255 ins_ch: /*
1256 * Historically, vi eliminated nul's out of hand. If the
1257 * beautify option was set, it also deleted any unknown
1258 * ASCII value less than space (040) and the del character
1259 * (0177), except for tabs. Unknown is a key word here.
1260 * Most vi documentation claims that it deleted everything
1261 * but <tab>, <nl> and <ff>, as that's what the original
1262 * 4BSD documentation said. This is obviously wrong,
1263 * however, as <esc> would be included in that list. What
1264 * we do is eliminate any unquoted, iscntrl() character that
1265 * wasn't a replay and wasn't handled specially, except
1266 * <tab> or <ff>.
1268 if (LF_ISSET(TXT_BEAUTIFY) && ISCNTRL(evp->e_c) &&
1269 evp->e_value != K_FORMFEED && evp->e_value != K_TAB) {
1270 msgq(sp, M_BERR,
1271 "192|Illegal character; quote to enter");
1272 if (LF_ISSET(TXT_REPLAY))
1273 goto done;
1274 break;
1277 insq_ch: /*
1278 * If entering a non-word character after a word, check for
1279 * abbreviations. If there was one, discard replay characters.
1280 * If entering a blank character, check for unmap commands,
1281 * as well.
1283 if (!inword(evp->e_c)) {
1284 if (abb == AB_INWORD &&
1285 !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) {
1286 if (txt_abbrev(sp, tp, &evp->e_c,
1287 LF_ISSET(TXT_INFOLINE), &tmp, &ab_turnoff))
1288 goto err;
1289 if (tmp) {
1290 if (LF_ISSET(TXT_RECORD))
1291 rcol -= tmp + 1;
1292 goto resolve;
1295 if (isblank(evp->e_c) && UNMAP_TST)
1296 txt_unmap(sp, tp, &ec_flags);
1298 if (abb != AB_NOTSET)
1299 abb = inword(evp->e_c) ? AB_INWORD : AB_NOTWORD;
1301 insl_ch: if (txt_insch(sp, tp, &evp->e_c, flags))
1302 goto err;
1305 * If we're using K_VLNEXT to quote the next character, then
1306 * we want the cursor to position itself on the ^ placeholder
1307 * we're displaying, to match historic practice.
1309 if (quote == Q_VNEXT) {
1310 --tp->cno;
1311 ++tp->owrite;
1315 * !!!
1316 * Translate "<CH_HEX>[isxdigit()]*" to a character with
1317 * a hex value: this test delimits the value by the max
1318 * number of hex bytes. Offset by one, we use 0 to mean
1319 * that we've found <CH_HEX>.
1321 if (hexcnt != 0 && hexcnt++ == sizeof(CHAR_T) * 2 + 1) {
1322 hexcnt = 0;
1323 if (txt_hex(sp, tp))
1324 goto err;
1328 * Check to see if we've crossed the margin.
1330 * !!!
1331 * In the historic vi, the wrapmargin value was figured out
1332 * using the display widths of the characters, i.e. <tab>
1333 * characters were counted as two characters if the list edit
1334 * option is set, but as the tabstop edit option number of
1335 * characters otherwise. That's what the vs_column() function
1336 * gives us, so we use it.
1338 if (margin != 0) {
1339 if (vs_column(sp, &tcol))
1340 goto err;
1341 if (tcol >= margin) {
1342 if (txt_margin(sp, tp, &wmt, &tmp, flags))
1343 goto err;
1344 if (tmp) {
1345 if (isblank(evp->e_c))
1346 wm_skip = 1;
1347 wm_set = 1;
1348 goto k_cr;
1354 * If we've reached the end of the buffer, then we need to
1355 * switch into insert mode. This happens when there's a
1356 * change to a mark and the user puts in more characters than
1357 * the length of the motion.
1359 ebuf_chk: if (tp->cno >= tp->len) {
1360 BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
1361 LF_SET(TXT_APPENDEOL);
1363 tp->lb[tp->cno] = CH_CURSOR;
1364 ++tp->insert;
1365 ++tp->len;
1368 /* Step the quote state forward. */
1369 if (quote != Q_NOTSET) {
1370 if (quote == Q_BNEXT)
1371 quote = Q_BTHIS;
1372 if (quote == Q_VNEXT)
1373 quote = Q_VTHIS;
1375 break;
1378 #ifdef DEBUG
1379 if (tp->cno + tp->insert + tp->owrite != tp->len) {
1380 msgq(sp, M_ERR,
1381 "len %u != cno: %u ai: %u insert %u overwrite %u",
1382 tp->len, tp->cno, tp->ai, tp->insert, tp->owrite);
1383 if (LF_ISSET(TXT_REPLAY))
1384 goto done;
1385 tp->len = tp->cno + tp->insert + tp->owrite;
1387 #endif
1389 resolve:/*
1390 * 1: If we don't need to know where the cursor really is and we're
1391 * replaying text, keep going.
1393 if (margin == 0 && LF_ISSET(TXT_REPLAY))
1394 goto replay;
1397 * 2: Reset the line. Don't bother unless we're about to wait on
1398 * a character or we need to know where the cursor really is.
1399 * We have to do this before showing matching characters so the
1400 * user can see what they're matching.
1402 if ((margin != 0 || !KEYS_WAITING(sp)) &&
1403 vs_change(sp, tp->lno, LINE_RESET))
1404 return (1);
1407 * 3: If there aren't keys waiting, display the matching character.
1408 * We have to do this before resolving any messages, otherwise
1409 * the error message from a missing match won't appear correctly.
1411 if (showmatch) {
1412 if (!KEYS_WAITING(sp) && txt_showmatch(sp, tp))
1413 return (1);
1414 showmatch = 0;
1418 * 4: If there have been messages and we're not editing on the colon
1419 * command line or doing file name completion, resolve them.
1421 if ((vip->totalcount != 0 || F_ISSET(gp, G_BELLSCHED)) &&
1422 !F_ISSET(sp, SC_TINPUT_INFO) && !filec_redraw &&
1423 vs_resolve(sp, NULL, 0))
1424 return (1);
1427 * 5: Refresh the screen if we're about to wait on a character or we
1428 * need to know where the cursor really is.
1430 if (margin != 0 || !KEYS_WAITING(sp)) {
1431 UPDATE_POSITION(sp, tp);
1432 if (vs_refresh(sp, margin != 0))
1433 return (1);
1436 /* 6: Proceed with the incremental search. */
1437 if (FL_ISSET(is_flags, IS_RUNNING) && txt_isrch(sp, vp, tp, &is_flags))
1438 return (1);
1440 /* 7: Next character... */
1441 if (LF_ISSET(TXT_REPLAY))
1442 goto replay;
1443 goto next;
1445 done: /* Leave input mode. */
1446 F_CLR(sp, SC_TINPUT);
1448 /* If recording for playback, save it. */
1449 if (LF_ISSET(TXT_RECORD))
1450 vip->rep_cnt = rcol;
1453 * If not working on the colon command line, set the final cursor
1454 * position.
1456 if (!F_ISSET(sp, SC_TINPUT_INFO)) {
1457 vp->m_final.lno = tp->lno;
1458 vp->m_final.cno = tp->cno;
1460 return (0);
1462 err:
1463 alloc_err:
1464 txt_err(sp, &sp->tiq);
1465 return (1);
1469 * txt_abbrev --
1470 * Handle abbreviations.
1472 static int
1473 txt_abbrev(sp, tp, pushcp, isinfoline, didsubp, turnoffp)
1474 SCR *sp;
1475 TEXT *tp;
1476 CHAR_T *pushcp;
1477 int isinfoline, *didsubp, *turnoffp;
1479 VI_PRIVATE *vip;
1480 CHAR_T ch, *p;
1481 SEQ *qp;
1482 size_t len, off;
1484 /* Check to make sure we're not at the start of an append. */
1485 *didsubp = 0;
1486 if (tp->cno == tp->offset)
1487 return (0);
1489 vip = VIP(sp);
1492 * Find the start of the "word".
1494 * !!!
1495 * We match historic practice, which, as far as I can tell, had an
1496 * off-by-one error. The way this worked was that when the inserted
1497 * text switched from a "word" character to a non-word character,
1498 * vi would check for possible abbreviations. It would then take the
1499 * type (i.e. word/non-word) of the character entered TWO characters
1500 * ago, and move backward in the text until reaching a character that
1501 * was not that type, or the beginning of the insert, the line, or
1502 * the file. For example, in the string "abc<space>", when the <space>
1503 * character triggered the abbreviation check, the type of the 'b'
1504 * character was used for moving through the string. Maybe there's a
1505 * reason for not using the first (i.e. 'c') character, but I can't
1506 * think of one.
1508 * Terminate at the beginning of the insert or the character after the
1509 * offset character -- both can be tested for using tp->offset.
1511 off = tp->cno - 1; /* Previous character. */
1512 p = tp->lb + off;
1513 len = 1; /* One character test. */
1514 if (off == tp->offset || isblank(p[-1]))
1515 goto search;
1516 if (inword(p[-1])) /* Move backward to change. */
1517 for (;;) {
1518 --off; --p; ++len;
1519 if (off == tp->offset || !inword(p[-1]))
1520 break;
1522 else
1523 for (;;) {
1524 --off; --p; ++len;
1525 if (off == tp->offset ||
1526 inword(p[-1]) || isblank(p[-1]))
1527 break;
1531 * !!!
1532 * Historic vi exploded abbreviations on the command line. This has
1533 * obvious problems in that unabbreviating the string can be extremely
1534 * tricky, particularly if the string has, say, an embedded escape
1535 * character. Personally, I think it's a stunningly bad idea. Other
1536 * examples of problems this caused in historic vi are:
1537 * :ab foo bar
1538 * :ab foo baz
1539 * results in "bar" being abbreviated to "baz", which wasn't what the
1540 * user had in mind at all. Also, the commands:
1541 * :ab foo bar
1542 * :unab foo<space>
1543 * resulted in an error message that "bar" wasn't mapped. Finally,
1544 * since the string was already exploded by the time the unabbreviate
1545 * command got it, all it knew was that an abbreviation had occurred.
1546 * Cleverly, it checked the replacement string for its unabbreviation
1547 * match, which meant that the commands:
1548 * :ab foo1 bar
1549 * :ab foo2 bar
1550 * :unab foo2
1551 * unabbreviate "foo1", and the commands:
1552 * :ab foo bar
1553 * :ab bar baz
1554 * unabbreviate "foo"!
1556 * Anyway, people neglected to first ask my opinion before they wrote
1557 * macros that depend on this stuff, so, we make this work as follows.
1558 * When checking for an abbreviation on the command line, if we get a
1559 * string which is <blank> terminated and which starts at the beginning
1560 * of the line, we check to see it is the abbreviate or unabbreviate
1561 * commands. If it is, turn abbreviations off and return as if no
1562 * abbreviation was found. Note also, minor trickiness, so that if
1563 * the user erases the line and starts another command, we turn the
1564 * abbreviations back on.
1566 * This makes the layering look like a Nachos Supreme.
1568 search: if (isinfoline)
1569 if (off == tp->ai || off == tp->offset)
1570 if (ex_is_abbrev(sp, p, len)) {
1571 *turnoffp = 1;
1572 return (0);
1573 } else
1574 *turnoffp = 0;
1575 else
1576 if (*turnoffp)
1577 return (0);
1579 /* Check for any abbreviations. */
1580 if ((qp = seq_find(sp, NULL, NULL, p, len, SEQ_ABBREV, NULL)) == NULL)
1581 return (0);
1584 * Push the abbreviation onto the tty stack. Historically, characters
1585 * resulting from an abbreviation expansion were themselves subject to
1586 * map expansions, O_SHOWMATCH matching etc. This means the expanded
1587 * characters will be re-tested for abbreviations. It's difficult to
1588 * know what historic practice in this case was, since abbreviations
1589 * were applied to :colon command lines, so entering abbreviations that
1590 * looped was tricky, although possible. In addition, obvious loops
1591 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
1592 * silently only implement and/or display the last abbreviation.)
1594 * This implementation doesn't recover well from such abbreviations.
1595 * The main input loop counts abbreviated characters, and, when it
1596 * reaches a limit, discards any abbreviated characters on the queue.
1597 * It's difficult to back up to the original position, as the replay
1598 * queue would have to be adjusted, and the line state when an initial
1599 * abbreviated character was received would have to be saved.
1601 ch = *pushcp;
1602 if (v_event_push(sp, NULL, &ch, 1, CH_ABBREVIATED))
1603 return (1);
1604 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_ABBREVIATED))
1605 return (1);
1608 * If the size of the abbreviation is larger than or equal to the size
1609 * of the original text, move to the start of the replaced characters,
1610 * and add their length to the overwrite count.
1612 * If the abbreviation is smaller than the original text, we have to
1613 * delete the additional overwrite characters and copy down any insert
1614 * characters.
1616 tp->cno -= len;
1617 if (qp->olen >= len)
1618 tp->owrite += len;
1619 else {
1620 if (tp->insert)
1621 MEMMOVEW(tp->lb + tp->cno + qp->olen,
1622 tp->lb + tp->cno + tp->owrite + len, tp->insert);
1623 tp->owrite += qp->olen;
1624 tp->len -= len - qp->olen;
1628 * We return the length of the abbreviated characters. This is so
1629 * the calling routine can replace the replay characters with the
1630 * abbreviation. This means that subsequent '.' commands will produce
1631 * the same text, regardless of intervening :[un]abbreviate commands.
1632 * This is historic practice.
1634 *didsubp = len;
1635 return (0);
1639 * txt_unmap --
1640 * Handle the unmap command.
1642 static void
1643 txt_unmap(sp, tp, ec_flagsp)
1644 SCR *sp;
1645 TEXT *tp;
1646 u_int32_t *ec_flagsp;
1648 size_t len, off;
1649 CHAR_T *p;
1651 /* Find the beginning of this "word". */
1652 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1653 if (isblank(*p)) {
1654 ++p;
1655 break;
1657 ++len;
1658 if (off == tp->ai || off == tp->offset)
1659 break;
1663 * !!!
1664 * Historic vi exploded input mappings on the command line. See the
1665 * txt_abbrev() routine for an explanation of the problems inherent
1666 * in this.
1668 * We make this work as follows. If we get a string which is <blank>
1669 * terminated and which starts at the beginning of the line, we check
1670 * to see it is the unmap command. If it is, we return that the input
1671 * mapping should be turned off. Note also, minor trickiness, so that
1672 * if the user erases the line and starts another command, we go ahead
1673 * an turn mapping back on.
1675 if ((off == tp->ai || off == tp->offset) && ex_is_unmap(sp, p, len))
1676 FL_CLR(*ec_flagsp, EC_MAPINPUT);
1677 else
1678 FL_SET(*ec_flagsp, EC_MAPINPUT);
1682 * txt_ai_resolve --
1683 * When a line is resolved by <esc>, review autoindent characters.
1685 static void
1686 txt_ai_resolve(sp, tp, changedp)
1687 SCR *sp;
1688 TEXT *tp;
1689 int *changedp;
1691 u_long ts;
1692 int del;
1693 size_t cno, len, new, old, scno, spaces, tab_after_sp, tabs;
1694 CHAR_T *p;
1696 *changedp = 0;
1699 * If the line is empty, has an offset, or no autoindent
1700 * characters, we're done.
1702 if (!tp->len || tp->offset || !tp->ai)
1703 return;
1706 * If the length is less than or equal to the autoindent
1707 * characters, delete them.
1709 if (tp->len <= tp->ai) {
1710 tp->ai = tp->cno = tp->len = 0;
1711 return;
1715 * The autoindent characters plus any leading <blank> characters
1716 * in the line are resolved into the minimum number of characters.
1717 * Historic practice.
1719 ts = O_VAL(sp, O_TABSTOP);
1721 /* Figure out the last <blank> screen column. */
1722 for (p = tp->lb, scno = 0, len = tp->len,
1723 spaces = tab_after_sp = 0; len-- && isblank(*p); ++p)
1724 if (*p == '\t') {
1725 if (spaces)
1726 tab_after_sp = 1;
1727 scno += COL_OFF(scno, ts);
1728 } else {
1729 ++spaces;
1730 ++scno;
1734 * If there are no spaces, or no tabs after spaces and less than
1735 * ts spaces, it's already minimal.
1737 if (!spaces || !tab_after_sp && spaces < ts)
1738 return;
1740 /* Count up spaces/tabs needed to get to the target. */
1741 for (cno = 0, tabs = 0; cno + COL_OFF(cno, ts) <= scno; ++tabs)
1742 cno += COL_OFF(cno, ts);
1743 spaces = scno - cno;
1746 * Figure out how many characters we're dropping -- if we're not
1747 * dropping any, it's already minimal, we're done.
1749 old = p - tp->lb;
1750 new = spaces + tabs;
1751 if (old == new)
1752 return;
1754 /* Shift the rest of the characters down, adjust the counts. */
1755 del = old - new;
1756 MEMMOVEW(p - del, p, tp->len - old);
1757 tp->len -= del;
1758 tp->cno -= del;
1760 /* Fill in space/tab characters. */
1761 for (p = tp->lb; tabs--;)
1762 *p++ = '\t';
1763 while (spaces--)
1764 *p++ = ' ';
1765 *changedp = 1;
1769 * v_txt_auto --
1770 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1771 * retrieve the line.
1773 * PUBLIC: int v_txt_auto __P((SCR *, db_recno_t, TEXT *, size_t, TEXT *));
1776 v_txt_auto(sp, lno, aitp, len, tp)
1777 SCR *sp;
1778 db_recno_t lno;
1779 TEXT *aitp, *tp;
1780 size_t len;
1782 size_t nlen;
1783 CHAR_T *p, *t;
1785 if (aitp == NULL) {
1787 * If the ex append command is executed with an address of 0,
1788 * it's possible to get here with a line number of 0. Return
1789 * an indent of 0.
1791 if (lno == 0) {
1792 tp->ai = 0;
1793 return (0);
1795 if (db_get(sp, lno, DBG_FATAL, &t, &len))
1796 return (1);
1797 } else
1798 t = aitp->lb;
1800 /* Count whitespace characters. */
1801 for (p = t; len > 0; ++p, --len)
1802 if (!isblank(*p))
1803 break;
1805 /* Set count, check for no indentation. */
1806 if ((nlen = (p - t)) == 0)
1807 return (0);
1809 /* Make sure the buffer's big enough. */
1810 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
1812 /* Copy the buffer's current contents up. */
1813 if (tp->len != 0)
1814 MEMMOVEW(tp->lb + nlen, tp->lb, tp->len);
1815 tp->len += nlen;
1817 /* Copy the indentation into the new buffer. */
1818 MEMMOVEW(tp->lb, t, nlen);
1820 /* Set the autoindent count. */
1821 tp->ai = nlen;
1822 return (0);
1826 * txt_backup --
1827 * Back up to the previously edited line.
1829 static TEXT *
1830 txt_backup(sp, tiqh, tp, flagsp)
1831 SCR *sp;
1832 TEXTH *tiqh;
1833 TEXT *tp;
1834 u_int32_t *flagsp;
1836 VI_PRIVATE *vip;
1837 TEXT *ntp;
1839 /* Get a handle on the previous TEXT structure. */
1840 if ((ntp = tp->q.cqe_prev) == (void *)tiqh) {
1841 if (!FL_ISSET(*flagsp, TXT_REPLAY))
1842 msgq(sp, M_BERR,
1843 "193|Already at the beginning of the insert");
1844 return (tp);
1847 /* Bookkeeping. */
1848 ntp->len = ntp->sv_len;
1850 /* Handle appending to the line. */
1851 vip = VIP(sp);
1852 if (ntp->owrite == 0 && ntp->insert == 0) {
1853 ntp->lb[ntp->len] = CH_CURSOR;
1854 ++ntp->insert;
1855 ++ntp->len;
1856 FL_SET(*flagsp, TXT_APPENDEOL);
1857 } else
1858 FL_CLR(*flagsp, TXT_APPENDEOL);
1860 /* Release the current TEXT. */
1861 CIRCLEQ_REMOVE(tiqh, tp, q);
1862 text_free(tp);
1864 /* Update the old line on the screen. */
1865 if (vs_change(sp, ntp->lno + 1, LINE_DELETE))
1866 return (NULL);
1868 /* Return the new/current TEXT. */
1869 return (ntp);
1873 * Text indentation is truly strange. ^T and ^D do movements to the next or
1874 * previous shiftwidth value, i.e. for a 1-based numbering, with shiftwidth=3,
1875 * ^T moves a cursor on the 7th, 8th or 9th column to the 10th column, and ^D
1876 * moves it back.
1878 * !!!
1879 * The ^T and ^D characters in historical vi had special meaning only when they
1880 * were the first characters entered after entering text input mode. As normal
1881 * erase characters couldn't erase autoindent characters (^T in this case), it
1882 * meant that inserting text into previously existing text was strange -- ^T
1883 * only worked if it was the first keystroke(s), and then could only be erased
1884 * using ^D. This implementation treats ^T specially anywhere it occurs in the
1885 * input, and permits the standard erase characters to erase the characters it
1886 * inserts.
1888 * !!!
1889 * A fun test is to try:
1890 * :se sw=4 ai list
1891 * i<CR>^Tx<CR>^Tx<CR>^Tx<CR>^Dx<CR>^Dx<CR>^Dx<esc>
1892 * Historic vi loses some of the '$' marks on the line ends, but otherwise gets
1893 * it right.
1895 * XXX
1896 * Technically, txt_dent should be part of the screen interface, as it requires
1897 * knowledge of character sizes, including <space>s, on the screen. It's here
1898 * because it's a complicated little beast, and I didn't want to shove it down
1899 * into the screen. It's probable that KEY_LEN will call into the screen once
1900 * there are screens with different character representations.
1902 * txt_dent --
1903 * Handle ^T indents, ^D outdents.
1905 * If anything changes here, check the ex version to see if it needs similar
1906 * changes.
1908 static int
1909 txt_dent(sp, tp, isindent)
1910 SCR *sp;
1911 TEXT *tp;
1912 int isindent;
1914 CHAR_T ch;
1915 u_long sw, ts;
1916 size_t cno, current, spaces, target, tabs, off;
1917 int ai_reset;
1919 ts = O_VAL(sp, O_TABSTOP);
1920 sw = O_VAL(sp, O_SHIFTWIDTH);
1923 * Since we don't know what precedes the character(s) being inserted
1924 * (or deleted), the preceding whitespace characters must be resolved.
1925 * An example is a <tab>, which doesn't need a full shiftwidth number
1926 * of columns because it's preceded by <space>s. This is easy to get
1927 * if the user sets shiftwidth to a value less than tabstop (or worse,
1928 * something for which tabstop isn't a multiple) and then uses ^T to
1929 * indent, and ^D to outdent.
1931 * Figure out the current and target screen columns. In the historic
1932 * vi, the autoindent column was NOT determined using display widths
1933 * of characters as was the wrapmargin column. For that reason, we
1934 * can't use the vs_column() function, but have to calculate it here.
1935 * This is slow, but it's normally only on the first few characters of
1936 * a line.
1938 for (current = cno = 0; cno < tp->cno; ++cno)
1939 current += tp->lb[cno] == '\t' ?
1940 COL_OFF(current, ts) : KEY_LEN(sp, tp->lb[cno]);
1942 target = current;
1943 if (isindent)
1944 target += COL_OFF(target, sw);
1945 else
1946 target -= --target % sw;
1949 * The AI characters will be turned into overwrite characters if the
1950 * cursor immediately follows them. We test both the cursor position
1951 * and the indent flag because there's no single test. (^T can only
1952 * be detected by the cursor position, and while we know that the test
1953 * is always true for ^D, the cursor can be in more than one place, as
1954 * "0^D" and "^D" are different.)
1956 ai_reset = !isindent || tp->cno == tp->ai + tp->offset;
1959 * Back up over any previous <blank> characters, changing them into
1960 * overwrite characters (including any ai characters). Then figure
1961 * out the current screen column.
1963 for (; tp->cno > tp->offset &&
1964 (tp->lb[tp->cno - 1] == ' ' || tp->lb[tp->cno - 1] == '\t');
1965 --tp->cno, ++tp->owrite);
1966 for (current = cno = 0; cno < tp->cno; ++cno)
1967 current += tp->lb[cno] == '\t' ?
1968 COL_OFF(current, ts) : KEY_LEN(sp, tp->lb[cno]);
1971 * If we didn't move up to or past the target, it's because there
1972 * weren't enough characters to delete, e.g. the first character
1973 * of the line was a tp->offset character, and the user entered
1974 * ^D to move to the beginning of a line. An example of this is:
1976 * :set ai sw=4<cr>i<space>a<esc>i^T^D
1978 * Otherwise, count up the total spaces/tabs needed to get from the
1979 * beginning of the line (or the last non-<blank> character) to the
1980 * target.
1982 if (current >= target)
1983 spaces = tabs = 0;
1984 else {
1985 for (cno = current,
1986 tabs = 0; cno + COL_OFF(cno, ts) <= target; ++tabs)
1987 cno += COL_OFF(cno, ts);
1988 spaces = target - cno;
1991 /* If we overwrote ai characters, reset the ai count. */
1992 if (ai_reset)
1993 tp->ai = tabs + spaces;
1996 * Call txt_insch() to insert each character, so that we get the
1997 * correct effect when we add a <tab> to replace N <spaces>.
1999 for (ch = '\t'; tabs > 0; --tabs)
2000 (void)txt_insch(sp, tp, &ch, 0);
2001 for (ch = ' '; spaces > 0; --spaces)
2002 (void)txt_insch(sp, tp, &ch, 0);
2003 return (0);
2007 * txt_fc --
2008 * File name completion.
2010 static int
2011 txt_fc(sp, tp, redrawp)
2012 SCR *sp;
2013 TEXT *tp;
2014 int *redrawp;
2016 struct stat sb;
2017 ARGS **argv;
2018 CHAR_T s_ch;
2019 EXCMD cmd;
2020 size_t indx, len, nlen, off;
2021 int argc, trydir;
2022 CHAR_T *p, *t;
2023 char *np;
2024 size_t nplen;
2026 trydir = 0;
2027 *redrawp = 0;
2030 * Find the beginning of this "word" -- if we're at the beginning
2031 * of the line, it's a special case.
2033 if (tp->cno == 1) {
2034 len = 0;
2035 p = tp->lb;
2036 } else
2037 retry: for (len = 0,
2038 off = tp->cno - 1, p = tp->lb + off;; --off, --p) {
2039 if (isblank(*p)) {
2040 ++p;
2041 break;
2043 ++len;
2044 if (off == tp->ai || off == tp->offset)
2045 break;
2049 * Get enough space for a wildcard character.
2051 * XXX
2052 * This won't work for "foo\", since the \ will escape the expansion
2053 * character. I'm not sure if that's a bug or not...
2055 off = p - tp->lb;
2056 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2057 p = tp->lb + off;
2059 s_ch = p[len];
2060 p[len] = '*';
2062 /* Build an ex command, and call the ex expansion routines. */
2063 ex_cinit(sp, &cmd, 0, 0, OOBLNO, OOBLNO, 0);
2064 if (argv_exp2(sp, &cmd, p, len + 1)) {
2065 p[len] = s_ch;
2066 return (0);
2068 argc = cmd.argc;
2069 argv = cmd.argv;
2071 p[len] = s_ch;
2073 switch (argc) {
2074 case 0: /* No matches. */
2075 if (!trydir)
2076 (void)sp->gp->scr_bell(sp);
2077 return (0);
2078 case 1: /* One match. */
2079 /* If something changed, do the exchange. */
2080 nlen = v_strlen(cmd.argv[0]->bp);
2081 if (len != nlen || MEMCMP(cmd.argv[0]->bp, p, len))
2082 break;
2084 /* If haven't done a directory test, do it now. */
2085 INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1,
2086 np, nplen);
2087 if (!trydir &&
2088 !stat(np, &sb) && S_ISDIR(sb.st_mode)) {
2089 p += len;
2090 goto isdir;
2093 /* If nothing changed, period, ring the bell. */
2094 if (!trydir)
2095 (void)sp->gp->scr_bell(sp);
2096 return (0);
2097 default: /* Multiple matches. */
2098 *redrawp = 1;
2099 if (txt_fc_col(sp, argc, argv))
2100 return (1);
2102 /* Find the length of the shortest match. */
2103 for (nlen = cmd.argv[0]->len; --argc > 0;) {
2104 if (cmd.argv[argc]->len < nlen)
2105 nlen = cmd.argv[argc]->len;
2106 for (indx = 0; indx < nlen &&
2107 cmd.argv[argc]->bp[indx] == cmd.argv[0]->bp[indx];
2108 ++indx);
2109 nlen = indx;
2111 break;
2114 /* Overwrite the expanded text first. */
2115 for (t = cmd.argv[0]->bp; len > 0 && nlen > 0; --len, --nlen)
2116 *p++ = *t++;
2118 /* If lost text, make the remaining old text overwrite characters. */
2119 if (len) {
2120 tp->cno -= len;
2121 tp->owrite += len;
2124 /* Overwrite any overwrite characters next. */
2125 for (; nlen > 0 && tp->owrite > 0; --nlen, --tp->owrite, ++tp->cno)
2126 *p++ = *t++;
2128 /* Shift remaining text up, and move the cursor to the end. */
2129 if (nlen) {
2130 off = p - tp->lb;
2131 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
2132 p = tp->lb + off;
2134 tp->cno += nlen;
2135 tp->len += nlen;
2137 if (tp->insert != 0)
2138 (void)MEMMOVEW(p + nlen, p, tp->insert);
2139 while (nlen--)
2140 *p++ = *t++;
2143 /* If a single match and it's a directory, retry it. */
2144 INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1, np, nplen);
2145 if (argc == 1 && !stat(np, &sb) && S_ISDIR(sb.st_mode)) {
2146 isdir: if (tp->owrite == 0) {
2147 off = p - tp->lb;
2148 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2149 p = tp->lb + off;
2150 if (tp->insert != 0)
2151 (void)MEMMOVEW(p + 1, p, tp->insert);
2152 ++tp->len;
2153 } else
2154 --tp->owrite;
2156 ++tp->cno;
2157 *p++ = '/';
2159 trydir = 1;
2160 goto retry;
2162 return (0);
2166 * txt_fc_col --
2167 * Display file names for file name completion.
2169 static int
2170 txt_fc_col(sp, argc, argv)
2171 SCR *sp;
2172 int argc;
2173 ARGS **argv;
2175 ARGS **av;
2176 CHAR_T *p;
2177 GS *gp;
2178 size_t base, cnt, col, colwidth, numrows, numcols, prefix, row;
2179 int ac, nf, reset;
2180 char *np, *pp;
2181 size_t nlen;
2183 gp = sp->gp;
2185 /* Trim any directory prefix common to all of the files. */
2186 INT2CHAR(sp, argv[0]->bp, argv[0]->len + 1, np, nlen);
2187 if ((pp = strrchr(np, '/')) == NULL)
2188 prefix = 0;
2189 else {
2190 prefix = (pp - np) + 1;
2191 for (ac = argc - 1, av = argv + 1; ac > 0; --ac, ++av)
2192 if (av[0]->len < prefix ||
2193 MEMCMP(av[0]->bp, argv[0]->bp,
2194 prefix)) {
2195 prefix = 0;
2196 break;
2201 * Figure out the column width for the longest name. Output is done on
2202 * 6 character "tab" boundaries for no particular reason. (Since we
2203 * don't output tab characters, we ignore the terminal's tab settings.)
2204 * Ignore the user's tab setting because we have no idea how reasonable
2205 * it is.
2207 for (ac = argc, av = argv, colwidth = 0; ac > 0; --ac, ++av) {
2208 for (col = 0, p = av[0]->bp + prefix; *p != '\0'; ++p)
2209 col += KEY_LEN(sp, *p);
2210 if (col > colwidth)
2211 colwidth = col;
2213 colwidth += COL_OFF(colwidth, 6);
2216 * Writing to the bottom line of the screen is always turned off when
2217 * SC_TINPUT_INFO is set. Turn it back on, we know what we're doing.
2219 if (F_ISSET(sp, SC_TINPUT_INFO)) {
2220 reset = 1;
2221 F_CLR(sp, SC_TINPUT_INFO);
2222 } else
2223 reset = 0;
2225 #define CHK_INTR \
2226 if (F_ISSET(gp, G_INTERRUPTED)) \
2227 goto intr;
2229 /* If the largest file name is too large, just print them. */
2230 if (colwidth > sp->cols) {
2231 for (ac = argc, av = argv; ac > 0; --ac, ++av) {
2232 INT2CHAR(sp, av[0]->bp+prefix, av[0]->len+1-prefix,
2233 np, nlen);
2234 pp = msg_print(sp, np, &nf);
2235 (void)ex_printf(sp, "%s\n", pp);
2236 if (F_ISSET(gp, G_INTERRUPTED))
2237 break;
2239 if (nf)
2240 FREE_SPACEW(sp, p, 0);
2241 CHK_INTR;
2242 } else {
2243 /* Figure out the number of columns. */
2244 numcols = (sp->cols - 1) / colwidth;
2245 if (argc > numcols) {
2246 numrows = argc / numcols;
2247 if (argc % numcols)
2248 ++numrows;
2249 } else
2250 numrows = 1;
2252 /* Display the files in sorted order. */
2253 for (row = 0; row < numrows; ++row) {
2254 for (base = row, col = 0; col < numcols; ++col) {
2255 INT2CHAR(sp, argv[base]->bp+prefix,
2256 argv[base]->len+1-prefix, np, nlen);
2257 pp = msg_print(sp, np, &nf);
2258 cnt = ex_printf(sp, "%s", pp);
2259 if (nf)
2260 FREE_SPACEW(sp, p, 0);
2261 CHK_INTR;
2262 if ((base += numrows) >= argc)
2263 break;
2264 (void)ex_printf(sp,
2265 "%*s", (int)(colwidth - cnt), "");
2266 CHK_INTR;
2268 (void)ex_puts(sp, "\n");
2269 CHK_INTR;
2271 (void)ex_puts(sp, "\n");
2272 CHK_INTR;
2274 (void)ex_fflush(sp);
2276 if (0) {
2277 intr: F_CLR(gp, G_INTERRUPTED);
2279 if (reset)
2280 F_SET(sp, SC_TINPUT_INFO);
2282 return (0);
2286 * txt_emark --
2287 * Set the end mark on the line.
2289 static int
2290 txt_emark(sp, tp, cno)
2291 SCR *sp;
2292 TEXT *tp;
2293 size_t cno;
2295 CHAR_T ch;
2296 char *kp;
2297 size_t chlen, nlen, olen;
2298 CHAR_T *p;
2300 ch = CH_ENDMARK;
2303 * The end mark may not be the same size as the current character.
2304 * Don't let the line shift.
2306 nlen = KEY_LEN(sp, ch);
2307 if (tp->lb[cno] == '\t')
2308 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &olen);
2309 else
2310 olen = KEY_LEN(sp, tp->lb[cno]);
2313 * If the line got longer, well, it's weird, but it's easy. If
2314 * it's the same length, it's easy. If it got shorter, we have
2315 * to fix it up.
2317 if (olen > nlen) {
2318 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + olen);
2319 chlen = olen - nlen;
2320 if (tp->insert != 0)
2321 MEMMOVEW(tp->lb + cno + 1 + chlen,
2322 tp->lb + cno + 1, tp->insert);
2324 tp->len += chlen;
2325 tp->owrite += chlen;
2326 p = tp->lb + cno;
2327 if (tp->lb[cno] == '\t')
2328 for (cno += chlen; chlen--;)
2329 *p++ = ' ';
2330 else
2331 for (kp = KEY_NAME(sp, tp->lb[cno]),
2332 cno += chlen; chlen--;)
2333 *p++ = *kp++;
2335 tp->lb[cno] = ch;
2336 return (vs_change(sp, tp->lno, LINE_RESET));
2340 * txt_err --
2341 * Handle an error during input processing.
2343 static void
2344 txt_err(sp, tiqh)
2345 SCR *sp;
2346 TEXTH *tiqh;
2348 db_recno_t lno;
2351 * The problem with input processing is that the cursor is at an
2352 * indeterminate position since some input may have been lost due
2353 * to a malloc error. So, try to go back to the place from which
2354 * the cursor started, knowing that it may no longer be available.
2356 * We depend on at least one line number being set in the text
2357 * chain.
2359 for (lno = tiqh->cqh_first->lno;
2360 !db_exist(sp, lno) && lno > 0; --lno);
2362 sp->lno = lno == 0 ? 1 : lno;
2363 sp->cno = 0;
2365 /* Redraw the screen, just in case. */
2366 F_SET(sp, SC_SCR_REDRAW);
2370 * txt_hex --
2371 * Let the user insert any character value they want.
2373 * !!!
2374 * This is an extension. The pattern "^X[0-9a-fA-F]*" is a way
2375 * for the user to specify a character value which their keyboard
2376 * may not be able to enter.
2378 static int
2379 txt_hex(sp, tp)
2380 SCR *sp;
2381 TEXT *tp;
2383 CHAR_T savec;
2384 size_t len, off;
2385 u_long value;
2386 CHAR_T *p, *wp;
2387 char *np;
2388 size_t nlen;
2391 * Null-terminate the string. Since nul isn't a legal hex value,
2392 * this should be okay, and lets us use a local routine, which
2393 * presumably understands the character set, to convert the value.
2395 savec = tp->lb[tp->cno];
2396 tp->lb[tp->cno] = 0;
2398 /* Find the previous CH_HEX character. */
2399 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off, ++len) {
2400 if (*p == CH_HEX) {
2401 wp = p + 1;
2402 break;
2404 /* Not on this line? Shouldn't happen. */
2405 if (off == tp->ai || off == tp->offset)
2406 goto nothex;
2409 /* If length of 0, then it wasn't a hex value. */
2410 if (len == 0)
2411 goto nothex;
2413 /* Get the value. */
2414 errno = 0;
2415 INT2CHAR(sp, wp, v_strlen(wp)+1, np, nlen);
2416 value = strtol(np, NULL, 16);
2417 if (errno || value > MAX_CHAR_T) {
2418 nothex: tp->lb[tp->cno] = savec;
2419 return (0);
2422 /* Restore the original character. */
2423 tp->lb[tp->cno] = savec;
2425 /* Adjust the bookkeeping. */
2426 tp->cno -= len;
2427 tp->len -= len;
2428 tp->lb[tp->cno - 1] = value;
2430 /* Copy down any overwrite characters. */
2431 if (tp->owrite)
2432 MEMMOVEW(tp->lb + tp->cno, tp->lb + tp->cno + len,
2433 tp->owrite);
2435 /* Copy down any insert characters. */
2436 if (tp->insert)
2437 MEMMOVEW(tp->lb + tp->cno + tp->owrite,
2438 tp->lb + tp->cno + tp->owrite + len,
2439 tp->insert);
2441 return (0);
2445 * txt_insch --
2447 * !!!
2448 * Historic vi did a special screen optimization for tab characters. As an
2449 * example, for the keystrokes "iabcd<esc>0C<tab>", the tab overwrote the
2450 * rest of the string when it was displayed.
2452 * Because early versions of this implementation redisplayed the entire line
2453 * on each keystroke, the "bcd" was pushed to the right as it ignored that
2454 * the user had "promised" to change the rest of the characters. However,
2455 * the historic vi implementation had an even worse bug: given the keystrokes
2456 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
2457 * on the second <esc> key.
2459 * POSIX 1003.2 requires (will require) that this be fixed, specifying that
2460 * vi overwrite characters the user has committed to changing, on the basis
2461 * of the screen space they require, but that it not overwrite other characters.
2463 static int
2464 txt_insch(sp, tp, chp, flags)
2465 SCR *sp;
2466 TEXT *tp;
2467 CHAR_T *chp;
2468 u_int flags;
2470 char *kp;
2471 CHAR_T savech;
2472 size_t chlen, cno, copydown, olen, nlen;
2473 CHAR_T *p;
2476 * The 'R' command does one-for-one replacement, because there's
2477 * no way to know how many characters the user intends to replace.
2479 if (LF_ISSET(TXT_REPLACE)) {
2480 if (tp->owrite) {
2481 --tp->owrite;
2482 tp->lb[tp->cno++] = *chp;
2483 return (0);
2485 } else if (tp->owrite) { /* Overwrite a character. */
2486 cno = tp->cno;
2489 * If the old or new characters are tabs, then the length of the
2490 * display depends on the character position in the display. We
2491 * don't even try to handle this here, just ask the screen.
2493 if (*chp == '\t') {
2494 savech = tp->lb[cno];
2495 tp->lb[cno] = '\t';
2496 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &nlen);
2497 tp->lb[cno] = savech;
2498 } else
2499 nlen = KEY_LEN(sp, *chp);
2502 * Eat overwrite characters until we run out of them or we've
2503 * handled the length of the new character. If we only eat
2504 * part of an overwrite character, break it into its component
2505 * elements and display the remaining components.
2507 for (copydown = 0; nlen != 0 && tp->owrite != 0;) {
2508 --tp->owrite;
2510 if (tp->lb[cno] == '\t')
2511 (void)vs_columns(sp,
2512 tp->lb, tp->lno, &cno, &olen);
2513 else
2514 olen = KEY_LEN(sp, tp->lb[cno]);
2516 if (olen == nlen) {
2517 nlen = 0;
2518 break;
2520 if (olen < nlen) {
2521 ++copydown;
2522 nlen -= olen;
2523 } else {
2524 BINC_RETW(sp,
2525 tp->lb, tp->lb_len, tp->len + olen);
2526 chlen = olen - nlen;
2527 MEMMOVEW(tp->lb + cno + 1 + chlen,
2528 tp->lb + cno + 1,
2529 tp->owrite + tp->insert);
2531 tp->len += chlen;
2532 tp->owrite += chlen;
2533 if (tp->lb[cno] == '\t')
2534 for (p = tp->lb + cno + 1; chlen--;)
2535 *p++ = ' ';
2536 else
2537 for (kp =
2538 KEY_NAME(sp, tp->lb[cno]) + nlen,
2539 p = tp->lb + cno + 1; chlen--;)
2540 *p++ = *kp++;
2541 nlen = 0;
2542 break;
2547 * If had to erase several characters, we adjust the total
2548 * count, and if there are any characters left, shift them
2549 * into position.
2551 if (copydown != 0 && (tp->len -= copydown) != 0)
2552 MEMMOVEW(tp->lb + cno, tp->lb + cno + copydown,
2553 tp->owrite + tp->insert + copydown);
2555 /* If we had enough overwrite characters, we're done. */
2556 if (nlen == 0) {
2557 tp->lb[tp->cno++] = *chp;
2558 return (0);
2562 /* Check to see if the character fits into the input buffer. */
2563 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2565 ++tp->len;
2566 if (tp->insert) { /* Insert a character. */
2567 if (tp->insert == 1)
2568 tp->lb[tp->cno + 1] = tp->lb[tp->cno];
2569 else
2570 MEMMOVEW(tp->lb + tp->cno + 1,
2571 tp->lb + tp->cno, tp->owrite + tp->insert);
2573 tp->lb[tp->cno++] = *chp;
2574 return (0);
2578 * txt_isrch --
2579 * Do an incremental search.
2581 static int
2582 txt_isrch(sp, vp, tp, is_flagsp)
2583 SCR *sp;
2584 VICMD *vp;
2585 TEXT *tp;
2586 u_int8_t *is_flagsp;
2588 MARK start;
2589 db_recno_t lno;
2590 u_int sf;
2592 /* If it's a one-line screen, we don't do incrementals. */
2593 if (IS_ONELINE(sp)) {
2594 FL_CLR(*is_flagsp, IS_RUNNING);
2595 return (0);
2599 * If the user erases back to the beginning of the buffer, there's
2600 * nothing to search for. Reset the cursor to the starting point.
2602 if (tp->cno <= 1) {
2603 vp->m_final = vp->m_start;
2604 return (0);
2608 * If it's an RE quote character, and not quoted, ignore it until
2609 * we get another character.
2611 if (tp->lb[tp->cno - 1] == '\\' &&
2612 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2613 return (0);
2616 * If it's a magic shell character, and not quoted, reset the cursor
2617 * to the starting point.
2619 if (strchr(O_STR(sp, O_SHELLMETA), tp->lb[tp->cno - 1]) != NULL &&
2620 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2621 vp->m_final = vp->m_start;
2624 * If we see the search pattern termination character, then quit doing
2625 * an incremental search. There may be more, e.g., ":/foo/;/bar/",
2626 * and we can't handle that incrementally. Also, reset the cursor to
2627 * the original location, the ex search routines don't know anything
2628 * about incremental searches.
2630 if (tp->lb[0] == tp->lb[tp->cno - 1] &&
2631 (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\')) {
2632 vp->m_final = vp->m_start;
2633 FL_CLR(*is_flagsp, IS_RUNNING);
2634 return (0);
2638 * Remember the input line and discard the special input map,
2639 * but don't overwrite the input line on the screen.
2641 lno = tp->lno;
2642 F_SET(VIP(sp), VIP_S_MODELINE);
2643 F_CLR(sp, SC_TINPUT | SC_TINPUT_INFO);
2644 if (txt_map_end(sp))
2645 return (1);
2648 * Specify a starting point and search. If we find a match, move to
2649 * it and refresh the screen. If we didn't find the match, then we
2650 * beep the screen. When searching from the original cursor position,
2651 * we have to move the cursor, otherwise, we don't want to move the
2652 * cursor in case the text at the current position continues to match.
2654 if (FL_ISSET(*is_flagsp, IS_RESTART)) {
2655 start = vp->m_start;
2656 sf = SEARCH_SET;
2657 } else {
2658 start = vp->m_final;
2659 sf = SEARCH_INCR | SEARCH_SET;
2662 if (tp->lb[0] == '/' ?
2663 !f_search(sp,
2664 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf) :
2665 !b_search(sp,
2666 &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf)) {
2667 sp->lno = vp->m_final.lno;
2668 sp->cno = vp->m_final.cno;
2669 FL_CLR(*is_flagsp, IS_RESTART);
2671 if (!KEYS_WAITING(sp) && vs_refresh(sp, 0))
2672 return (1);
2673 } else
2674 FL_SET(*is_flagsp, IS_RESTART);
2676 /* Reinstantiate the special input map. */
2677 if (txt_map_init(sp))
2678 return (1);
2679 F_CLR(VIP(sp), VIP_S_MODELINE);
2680 F_SET(sp, SC_TINPUT | SC_TINPUT_INFO);
2682 /* Reset the line number of the input line. */
2683 tp->lno = TMAP[0].lno;
2686 * If the colon command-line moved, i.e. the screen scrolled,
2687 * refresh the input line.
2689 * XXX
2690 * We shouldn't be calling vs_line, here -- we need dirty bits
2691 * on entries in the SMAP array.
2693 if (lno != TMAP[0].lno) {
2694 if (vs_line(sp, &TMAP[0], NULL, NULL))
2695 return (1);
2696 (void)sp->gp->scr_refresh(sp, 0);
2698 return (0);
2702 * txt_resolve --
2703 * Resolve the input text chain into the file.
2705 static int
2706 txt_resolve(sp, tiqh, flags)
2707 SCR *sp;
2708 TEXTH *tiqh;
2709 u_int32_t flags;
2711 VI_PRIVATE *vip;
2712 TEXT *tp;
2713 db_recno_t lno;
2714 int changed;
2717 * The first line replaces a current line, and all subsequent lines
2718 * are appended into the file. Resolve autoindented characters for
2719 * each line before committing it. If the latter causes the line to
2720 * change, we have to redisplay it, otherwise the information cached
2721 * about the line will be wrong.
2723 vip = VIP(sp);
2724 tp = tiqh->cqh_first;
2726 if (LF_ISSET(TXT_AUTOINDENT))
2727 txt_ai_resolve(sp, tp, &changed);
2728 else
2729 changed = 0;
2730 if (db_set(sp, tp->lno, tp->lb, tp->len) ||
2731 changed && vs_change(sp, tp->lno, LINE_RESET))
2732 return (1);
2734 for (lno = tp->lno; (tp = tp->q.cqe_next) != (void *)&sp->tiq; ++lno) {
2735 if (LF_ISSET(TXT_AUTOINDENT))
2736 txt_ai_resolve(sp, tp, &changed);
2737 else
2738 changed = 0;
2739 if (db_append(sp, 0, lno, tp->lb, tp->len) ||
2740 changed && vs_change(sp, tp->lno, LINE_RESET))
2741 return (1);
2745 * Clear the input flag, the look-aside buffer is no longer valid.
2746 * Has to be done as part of text resolution, or upon return we'll
2747 * be looking at incorrect data.
2749 F_CLR(sp, SC_TINPUT);
2751 return (0);
2755 * txt_showmatch --
2756 * Show a character match.
2758 * !!!
2759 * Historic vi tried to display matches even in the :colon command line.
2760 * I think not.
2762 static int
2763 txt_showmatch(sp, tp)
2764 SCR *sp;
2765 TEXT *tp;
2767 GS *gp;
2768 VCS cs;
2769 MARK m;
2770 int cnt, endc, startc;
2772 gp = sp->gp;
2775 * Do a refresh first, in case we haven't done one in awhile,
2776 * so the user can see what we're complaining about.
2778 UPDATE_POSITION(sp, tp);
2779 if (vs_refresh(sp, 1))
2780 return (1);
2783 * We don't display the match if it's not on the screen. Find
2784 * out what the first character on the screen is.
2786 if (vs_sm_position(sp, &m, 0, P_TOP))
2787 return (1);
2789 /* Initialize the getc() interface. */
2790 cs.cs_lno = tp->lno;
2791 cs.cs_cno = tp->cno - 1;
2792 if (cs_init(sp, &cs))
2793 return (1);
2794 startc = (endc = cs.cs_ch) == ')' ? '(' : '{';
2796 /* Search for the match. */
2797 for (cnt = 1;;) {
2798 if (cs_prev(sp, &cs))
2799 return (1);
2800 if (cs.cs_flags != 0) {
2801 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF) {
2802 msgq(sp, M_BERR,
2803 "Unmatched %s", KEY_NAME(sp, endc));
2804 return (0);
2806 continue;
2808 if (cs.cs_ch == endc)
2809 ++cnt;
2810 else if (cs.cs_ch == startc && --cnt == 0)
2811 break;
2814 /* If the match is on the screen, move to it. */
2815 if (cs.cs_lno < m.lno || cs.cs_lno == m.lno && cs.cs_cno < m.cno)
2816 return (0);
2817 sp->lno = cs.cs_lno;
2818 sp->cno = cs.cs_cno;
2819 if (vs_refresh(sp, 1))
2820 return (1);
2822 /* Wait for timeout or character arrival. */
2823 return (v_event_get(sp,
2824 NULL, O_VAL(sp, O_MATCHTIME) * 100, EC_TIMEOUT));
2828 * txt_margin --
2829 * Handle margin wrap.
2831 static int
2832 txt_margin(sp, tp, wmtp, didbreak, flags)
2833 SCR *sp;
2834 TEXT *tp, *wmtp;
2835 int *didbreak;
2836 u_int32_t flags;
2838 VI_PRIVATE *vip;
2839 size_t len, off;
2840 CHAR_T *p, *wp;
2842 /* Find the nearest previous blank. */
2843 for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --off, --p, ++len) {
2844 if (isblank(*p)) {
2845 wp = p + 1;
2846 break;
2850 * If reach the start of the line, there's nowhere to break.
2852 * !!!
2853 * Historic vi belled each time a character was entered after
2854 * crossing the margin until a space was entered which could
2855 * be used to break the line. I don't as it tends to wake the
2856 * cats.
2858 if (off == tp->ai || off == tp->offset) {
2859 *didbreak = 0;
2860 return (0);
2865 * Store saved information about the rest of the line in the
2866 * wrapmargin TEXT structure.
2868 * !!!
2869 * The offset field holds the length of the current characters
2870 * that the user entered, but which are getting split to the new
2871 * line -- it's going to be used to set the cursor value when we
2872 * move to the new line.
2874 vip = VIP(sp);
2875 wmtp->lb = p + 1;
2876 wmtp->offset = len;
2877 wmtp->insert = LF_ISSET(TXT_APPENDEOL) ? tp->insert - 1 : tp->insert;
2878 wmtp->owrite = tp->owrite;
2880 /* Correct current bookkeeping information. */
2881 tp->cno -= len;
2882 if (LF_ISSET(TXT_APPENDEOL)) {
2883 tp->len -= len + tp->owrite + (tp->insert - 1);
2884 tp->insert = 1;
2885 } else {
2886 tp->len -= len + tp->owrite + tp->insert;
2887 tp->insert = 0;
2889 tp->owrite = 0;
2892 * !!!
2893 * Delete any trailing whitespace from the current line.
2895 for (;; --p, --off) {
2896 if (!isblank(*p))
2897 break;
2898 --tp->cno;
2899 --tp->len;
2900 if (off == tp->ai || off == tp->offset)
2901 break;
2903 *didbreak = 1;
2904 return (0);
2908 * txt_Rresolve --
2909 * Resolve the input line for the 'R' command.
2911 static void
2912 txt_Rresolve(sp, tiqh, tp, orig_len)
2913 SCR *sp;
2914 TEXTH *tiqh;
2915 TEXT *tp;
2916 const size_t orig_len;
2918 TEXT *ttp;
2919 size_t input_len, retain;
2920 CHAR_T *p;
2923 * Check to make sure that the cursor hasn't moved beyond
2924 * the end of the line.
2926 if (tp->owrite == 0)
2927 return;
2930 * Calculate how many characters the user has entered,
2931 * plus the blanks erased by <carriage-return>/<newline>s.
2933 for (ttp = tiqh->cqh_first, input_len = 0;;) {
2934 input_len += ttp == tp ? tp->cno : ttp->len + ttp->R_erase;
2935 if ((ttp = ttp->q.cqe_next) == (void *)&sp->tiq)
2936 break;
2940 * If the user has entered less characters than the original line
2941 * was long, restore any overwriteable characters to the original
2942 * characters. These characters are entered as "insert characters",
2943 * because they're after the cursor and we don't want to lose them.
2944 * (This is okay because the R command has no insert characters.)
2945 * We set owrite to 0 so that the insert characters don't get copied
2946 * to somewhere else, which means that the line and the length have
2947 * to be adjusted here as well.
2949 * We have to retrieve the original line because the original pinned
2950 * page has long since been discarded. If it doesn't exist, that's
2951 * okay, the user just extended the file.
2953 if (input_len < orig_len) {
2954 retain = MIN(tp->owrite, orig_len - input_len);
2955 if (db_get(sp,
2956 tiqh->cqh_first->lno, DBG_FATAL | DBG_NOCACHE, &p, NULL))
2957 return;
2958 MEMCPYW(tp->lb + tp->cno, p + input_len, retain);
2959 tp->len -= tp->owrite - retain;
2960 tp->owrite = 0;
2961 tp->insert += retain;
2966 * txt_nomorech --
2967 * No more characters message.
2969 static void
2970 txt_nomorech(sp)
2971 SCR *sp;
2973 msgq(sp, M_BERR, "194|No more characters to erase");