nvi: Bring in version 2.1.3 (update from 2.1.1)
[dragonfly.git] / contrib / nvi / vi / v_txt.c
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9
10 #include "config.h"
11
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_txt.c,v 11.5 2013/05/19 20:37:45 bentley Exp $";
14 #endif /* not lint */
15
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "../common/common.h"
30 #include "vi.h"
31
32 static int       txt_abbrev __P((SCR *, TEXT *, CHAR_T *, int, int *, int *));
33 static void      txt_ai_resolve __P((SCR *, TEXT *, int *));
34 static TEXT     *txt_backup __P((SCR *, TEXTH *, TEXT *, u_int32_t *));
35 static int       txt_dent __P((SCR *, TEXT *, int));
36 static int       txt_emark __P((SCR *, TEXT *, size_t));
37 static void      txt_err __P((SCR *, TEXTH *));
38 static int       txt_fc __P((SCR *, TEXT *, int *));
39 static int       txt_fc_col __P((SCR *, int, ARGS **));
40 static int       txt_hex __P((SCR *, TEXT *));
41 static int       txt_insch __P((SCR *, TEXT *, CHAR_T *, u_int));
42 static int       txt_isrch __P((SCR *, VICMD *, TEXT *, u_int8_t *));
43 static int       txt_map_end __P((SCR *));
44 static int       txt_map_init __P((SCR *));
45 static int       txt_margin __P((SCR *, TEXT *, TEXT *, int *, u_int32_t));
46 static void      txt_nomorech __P((SCR *));
47 static void      txt_Rresolve __P((SCR *, TEXTH *, TEXT *, const size_t));
48 static int       txt_resolve __P((SCR *, TEXTH *, u_int32_t));
49 static int       txt_showmatch __P((SCR *, TEXT *));
50 static void      txt_unmap __P((SCR *, TEXT *, u_int32_t *));
51
52 /* Cursor character (space is hard to track on the screen). */
53 #if defined(DEBUG) && 0
54 #undef  CH_CURSOR
55 #define CH_CURSOR       '+'
56 #endif
57
58 /*
59  * v_tcmd --
60  *      Fill a buffer from the terminal for vi.
61  *
62  * PUBLIC: int v_tcmd __P((SCR *, VICMD *, ARG_CHAR_T, u_int));
63  */
64 int
65 v_tcmd(SCR *sp, VICMD *vp, ARG_CHAR_T prompt, u_int flags)
66 {
67         /* Normally, we end up where we started. */
68         vp->m_final.lno = sp->lno;
69         vp->m_final.cno = sp->cno;
70
71         /* Initialize the map. */
72         if (txt_map_init(sp))
73                 return (1);
74
75         /* Move to the last line. */
76         sp->lno = TMAP[0].lno;
77         sp->cno = 0;
78
79         /* Don't update the modeline for now. */
80         F_SET(sp, SC_TINPUT_INFO);
81
82         /* Set the input flags. */
83         LF_SET(TXT_APPENDEOL |
84             TXT_CR | TXT_ESCAPE | TXT_INFOLINE | TXT_MAPINPUT);
85         if (O_ISSET(sp, O_ALTWERASE))
86                 LF_SET(TXT_ALTWERASE);
87         if (O_ISSET(sp, O_TTYWERASE))
88                 LF_SET(TXT_TTYWERASE);
89
90         /* Do the input thing. */
91         if (v_txt(sp, vp, NULL, NULL, 0, prompt, 0, 1, flags))
92                 return (1);
93
94         /* Reenable the modeline updates. */
95         F_CLR(sp, SC_TINPUT_INFO);
96
97         /* Clean up the map. */
98         if (txt_map_end(sp))
99                 return (1);
100
101         if (IS_ONELINE(sp))
102                 F_SET(sp, SC_SCR_REDRAW);       /* XXX */
103
104         /* Set the cursor to the resulting position. */
105         sp->lno = vp->m_final.lno;
106         sp->cno = vp->m_final.cno;
107
108         return (0);
109 }
110
111 /*
112  * txt_map_init
113  *      Initialize the screen map for colon command-line input.
114  */
115 static int
116 txt_map_init(SCR *sp)
117 {
118         SMAP *esmp;
119         VI_PRIVATE *vip;
120
121         vip = VIP(sp);
122         if (!IS_ONELINE(sp)) {
123                 /*
124                  * Fake like the user is doing input on the last line of the
125                  * screen.  This makes all of the scrolling work correctly,
126                  * and allows us the use of the vi text editing routines, not
127                  * to mention practically infinite length ex commands.
128                  *
129                  * Save the current location.
130                  */
131                 vip->sv_tm_lno = TMAP->lno;
132                 vip->sv_tm_soff = TMAP->soff;
133                 vip->sv_tm_coff = TMAP->coff;
134                 vip->sv_t_maxrows = sp->t_maxrows;
135                 vip->sv_t_minrows = sp->t_minrows;
136                 vip->sv_t_rows = sp->t_rows;
137
138                 /*
139                  * If it's a small screen, TMAP may be small for the screen.
140                  * Fix it, filling in fake lines as we go.
141                  */
142                 if (IS_SMALL(sp))
143                         for (esmp =
144                             HMAP + (sp->t_maxrows - 1); TMAP < esmp; ++TMAP) {
145                                 TMAP[1].lno = TMAP[0].lno + 1;
146                                 TMAP[1].coff = HMAP->coff;
147                                 TMAP[1].soff = 1;
148                         }
149
150                 /* Build the fake entry. */
151                 TMAP[1].lno = TMAP[0].lno + 1;
152                 TMAP[1].soff = 1;
153                 TMAP[1].coff = 0;
154                 SMAP_FLUSH(&TMAP[1]);
155                 ++TMAP;
156
157                 /* Reset the screen information. */
158                 sp->t_rows = sp->t_minrows = ++sp->t_maxrows;
159         }
160         return (0);
161 }
162
163 /*
164  * txt_map_end
165  *      Reset the screen map for colon command-line input.
166  */
167 static int
168 txt_map_end(SCR *sp)
169 {
170         VI_PRIVATE *vip;
171         size_t cnt;
172
173         vip = VIP(sp);
174         if (!IS_ONELINE(sp)) {
175                 /* Restore the screen information. */
176                 sp->t_rows = vip->sv_t_rows;
177                 sp->t_minrows = vip->sv_t_minrows;
178                 sp->t_maxrows = vip->sv_t_maxrows;
179
180                 /*
181                  * If it's a small screen, TMAP may be wrong.  Clear any
182                  * lines that might have been overwritten.
183                  */
184                 if (IS_SMALL(sp)) {
185                         for (cnt = sp->t_rows; cnt <= sp->t_maxrows; ++cnt) {
186                                 (void)sp->gp->scr_move(sp, cnt, 0);
187                                 (void)sp->gp->scr_clrtoeol(sp);
188                         }
189                         TMAP = HMAP + (sp->t_rows - 1);
190                 } else
191                         --TMAP;
192
193                 /*
194                  * The map may be wrong if the user entered more than one
195                  * (logical) line.  Fix it.  If the user entered a whole
196                  * screen, this will be slow, but we probably don't care.
197                  */
198                 if (!O_ISSET(sp, O_LEFTRIGHT))
199                         while (vip->sv_tm_lno != TMAP->lno ||
200                             vip->sv_tm_soff != TMAP->soff)
201                                 if (vs_sm_1down(sp))
202                                         return (1);
203         }
204
205         /*
206          * Invalidate the cursor and the line size cache, the line never
207          * really existed.  This fixes bugs where the user searches for
208          * the last line on the screen + 1 and the refresh routine thinks
209          * that's where we just were.
210          */
211         VI_SCR_CFLUSH(vip);
212         F_SET(vip, VIP_CUR_INVALID);
213
214         return (0);
215 }
216
217 /*
218  * If doing input mapping on the colon command line, may need to unmap
219  * based on the command.
220  */
221 #define UNMAP_TST                                                       \
222         FL_ISSET(ec_flags, EC_MAPINPUT) && LF_ISSET(TXT_INFOLINE)
223
224 /* 
225  * Internally, we maintain tp->lno and tp->cno, externally, everyone uses
226  * sp->lno and sp->cno.  Make them consistent as necessary.
227  */
228 #define UPDATE_POSITION(sp, tp) {                                       \
229         (sp)->lno = (tp)->lno;                                          \
230         (sp)->cno = (tp)->cno;                                          \
231 }
232
233 /*
234  * v_txt --
235  *      Vi text input.
236  *
237  * PUBLIC: int v_txt __P((SCR *, VICMD *, MARK *,
238  * PUBLIC:    const CHAR_T *, size_t, ARG_CHAR_T, recno_t, u_long, u_int32_t));
239  */
240 int
241 v_txt(
242         SCR *sp,
243         VICMD *vp,
244         MARK *tm,               /* To MARK. */
245         const CHAR_T *lp,       /* Input line. */
246         size_t len,             /* Input line length. */
247         ARG_CHAR_T prompt,      /* Prompt to display. */
248         recno_t ai_line,        /* Line number to use for autoindent count. */
249         u_long rcount,          /* Replay count. */
250         u_int32_t flags)        /* TXT_* flags. */
251 {
252         EVENT ev, *evp = NULL;  /* Current event. */
253         EVENT fc;               /* File name completion event. */
254         GS *gp;
255         TEXT *ntp, *tp;         /* Input text structures. */
256         TEXT ait;               /* Autoindent text structure. */
257         TEXT wmt = {{ 0 }};     /* Wrapmargin text structure. */
258         TEXTH *tiqh;
259         VI_PRIVATE *vip;
260         abb_t abb;              /* State of abbreviation checks. */
261         carat_t carat;          /* State of the "[^0]^D" sequences. */
262         quote_t quote;          /* State of quotation. */
263         size_t owrite, insert;  /* Temporary copies of TEXT fields. */
264         size_t margin;          /* Wrapmargin value. */
265         size_t rcol;            /* 0-N: insert offset in the replay buffer. */
266         size_t tcol;            /* Temporary column. */
267         u_int32_t ec_flags;     /* Input mapping flags. */
268 #define IS_RESTART      0x01    /* Reset the incremental search. */
269 #define IS_RUNNING      0x02    /* Incremental search turned on. */
270         u_int8_t is_flags;
271         int abcnt, ab_turnoff;  /* Abbreviation character count, switch. */
272         int filec_redraw;       /* Redraw after the file completion routine. */
273         int hexcnt;             /* Hex character count. */
274         int showmatch;          /* Showmatch set on this character. */
275         int wm_set, wm_skip;    /* Wrapmargin happened, blank skip flags. */
276         int max, tmp;
277         int nochange;
278         CHAR_T *p;
279
280         gp = sp->gp;
281         vip = VIP(sp);
282
283         /*
284          * Set the input flag, so tabs get displayed correctly
285          * and everyone knows that the text buffer is in use.
286          */
287         F_SET(sp, SC_TINPUT);
288
289         /*
290          * Get one TEXT structure with some initial buffer space, reusing
291          * the last one if it's big enough.  (All TEXT bookkeeping fields
292          * default to 0 -- text_init() handles this.)  If changing a line,
293          * copy it into the TEXT buffer.
294          */
295         tiqh = sp->tiq;
296         if (!TAILQ_EMPTY(tiqh)) {
297                 tp = TAILQ_FIRST(tiqh);
298                 if (TAILQ_NEXT(tp, q) != NULL || tp->lb_len < len + 32) {
299                         text_lfree(tiqh);
300                         goto newtp;
301                 }
302                 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
303                 if (lp != NULL) {
304                         tp->len = len;
305                         BINC_RETW(sp, tp->lb, tp->lb_len, len);
306                         MEMMOVE(tp->lb, lp, len);
307                 } else
308                         tp->len = 0;
309         } else {
310 newtp:          if ((tp = text_init(sp, lp, len, len + 32)) == NULL)
311                         return (1);
312                 TAILQ_INSERT_HEAD(tiqh, tp, q);
313         }
314
315         /* Set default termination condition. */
316         tp->term = TERM_OK;
317
318         /* Set the starting line, column. */
319         tp->lno = sp->lno;
320         tp->cno = sp->cno;
321
322         /*
323          * Set the insert and overwrite counts.  If overwriting characters,
324          * do insertion afterward.  If not overwriting characters, assume
325          * doing insertion.  If change is to a mark, emphasize it with an
326          * CH_ENDMARK character.
327          */
328         if (len) {
329                 if (LF_ISSET(TXT_OVERWRITE)) {
330                         tp->owrite = (tm->cno - tp->cno) + 1;
331                         tp->insert = (len - tm->cno) - 1;
332                 } else
333                         tp->insert = len - tp->cno;
334
335                 if (LF_ISSET(TXT_EMARK) && txt_emark(sp, tp, tm->cno))
336                         return (1);
337         }
338
339         /*
340          * Many of the special cases in text input are to handle autoindent
341          * support.  Somebody decided that it would be a good idea if "^^D"
342          * and "0^D" deleted all of the autoindented characters.  In an editor
343          * that takes single character input from the user, this beggars the
344          * imagination.  Note also, "^^D" resets the next lines' autoindent,
345          * but "0^D" doesn't.
346          *
347          * We assume that autoindent only happens on empty lines, so insert
348          * and overwrite will be zero.  If doing autoindent, figure out how
349          * much indentation we need and fill it in.  Update input column and
350          * screen cursor as necessary.
351          */
352         if (LF_ISSET(TXT_AUTOINDENT) && ai_line != OOBLNO) {
353                 if (v_txt_auto(sp, ai_line, NULL, 0, tp))
354                         return (1);
355                 tp->cno = tp->ai;
356         } else {
357                 /*
358                  * The cc and S commands have a special feature -- leading
359                  * <blank> characters are handled as autoindent characters.
360                  * Beauty!
361                  */
362                 if (LF_ISSET(TXT_AICHARS)) {
363                         tp->offset = 0;
364                         tp->ai = tp->cno;
365                 } else
366                         tp->offset = tp->cno;
367         }
368
369         /* If getting a command buffer from the user, there may be a prompt. */
370         if (LF_ISSET(TXT_PROMPT)) {
371                 tp->lb[tp->cno++] = prompt;
372                 ++tp->len;
373                 ++tp->offset;
374         }
375
376         /*
377          * If appending after the end-of-line, add a space into the buffer
378          * and move the cursor right.  This space is inserted, i.e. pushed
379          * along, and then deleted when the line is resolved.  Assumes that
380          * the cursor is already positioned at the end of the line.  This
381          * avoids the nastiness of having the cursor reside on a magical
382          * column, i.e. a column that doesn't really exist.  The only down
383          * side is that we may wrap lines or scroll the screen before it's
384          * strictly necessary.  Not a big deal.
385          */
386         if (LF_ISSET(TXT_APPENDEOL)) {
387                 tp->lb[tp->cno] = CH_CURSOR;
388                 ++tp->len;
389                 ++tp->insert;
390                 (void)vs_change(sp, tp->lno, LINE_RESET);
391         }
392
393         /*
394          * Historic practice is that the wrapmargin value was a distance
395          * from the RIGHT-HAND margin, not the left.  It's more useful to
396          * us as a distance from the left-hand margin, i.e. the same as
397          * the wraplen value.  The wrapmargin option is historic practice.
398          * Nvi added the wraplen option so that it would be possible to
399          * edit files with consistent margins without knowing the number of
400          * columns in the window.
401          *
402          * XXX
403          * Setting margin causes a significant performance hit.  Normally
404          * we don't update the screen if there are keys waiting, but we
405          * have to if margin is set, otherwise the screen routines don't
406          * know where the cursor is.
407          *
408          * !!!
409          * Abbreviated keys were affected by the wrapmargin option in the
410          * historic 4BSD vi.  Mapped keys were usually, but sometimes not.
411          * See the comment in vi/v_text():set_txt_std for more information.
412          *
413          * !!!
414          * One more special case.  If an inserted <blank> character causes
415          * wrapmargin to split the line, the next user entered character is
416          * discarded if it's a <space> character.
417          */
418         wm_set = wm_skip = 0;
419         if (LF_ISSET(TXT_WRAPMARGIN))
420                 if ((margin = O_VAL(sp, O_WRAPMARGIN)) != 0)
421                         margin = sp->cols - margin;
422                 else
423                         margin = O_VAL(sp, O_WRAPLEN);
424         else
425                 margin = 0;
426
427         /* Initialize abbreviation checks. */
428         abcnt = ab_turnoff = 0;
429         abb = F_ISSET(gp, G_ABBREV) &&
430             LF_ISSET(TXT_MAPINPUT) ? AB_INWORD : AB_NOTSET;
431
432         /*
433          * Set up the dot command.  Dot commands are done by saving the actual
434          * characters and then reevaluating them so that things like wrapmargin
435          * can change between the insert and the replay.
436          *
437          * !!!
438          * Historically, vi did not remap or reabbreviate replayed input.  (It
439          * did beep at you if you changed an abbreviation and then replayed the
440          * input.  We're not that compatible.)  We don't have to do anything to
441          * avoid remapping, as we're not getting characters from the terminal
442          * routines.  Turn the abbreviation check off.
443          *
444          * XXX
445          * It would be nice if we could swallow backspaces and such, but it's
446          * not all that easy to do.  What we can do is turn off the common
447          * error messages during the replay.  Otherwise, when the user enters
448          * an illegal command, e.g., "Ia<erase><erase><erase><erase>b<escape>",
449          * and then does a '.', they get a list of error messages after command
450          * completion.
451          */
452         rcol = 0;
453         if (LF_ISSET(TXT_REPLAY)) {
454                 abb = AB_NOTSET;
455                 LF_CLR(TXT_RECORD);
456         }
457
458         /* Other text input mode setup. */
459         quote = Q_NOTSET;
460         carat = C_NOTSET;
461         nochange = 0;
462         FL_INIT(is_flags,
463             LF_ISSET(TXT_SEARCHINCR) ? IS_RESTART | IS_RUNNING : 0);
464         filec_redraw = hexcnt = showmatch = 0;
465
466         /* Initialize input flags. */
467         ec_flags = LF_ISSET(TXT_MAPINPUT) ? EC_MAPINPUT : 0;
468
469         /* Refresh the screen. */
470         UPDATE_POSITION(sp, tp);
471         if (vs_refresh(sp, 1))
472                 return (1);
473
474         /* If it's dot, just do it now. */
475         if (F_ISSET(vp, VC_ISDOT))
476                 goto replay;
477
478         /* Get an event. */
479         evp = &ev;
480 next:   if (v_event_get(sp, evp, 0, ec_flags))
481                 return (1);
482
483         /*
484          * If file completion overwrote part of the screen and nothing else has
485          * been displayed, clean up.  We don't do this as part of the normal
486          * message resolution because we know the user is on the colon command
487          * line and there's no reason to enter explicit characters to continue.
488          */
489         if (filec_redraw && !F_ISSET(sp, SC_SCR_EXWROTE)) {
490                 filec_redraw = 0;
491
492                 fc.e_event = E_REPAINT;
493                 fc.e_flno = vip->totalcount >=
494                     sp->rows ? 1 : sp->rows - vip->totalcount;
495                 fc.e_tlno = sp->rows;
496                 vip->linecount = vip->lcontinue = vip->totalcount = 0;
497                 (void)vs_repaint(sp, &fc);
498                 (void)vs_refresh(sp, 1);
499         }
500
501         /* Deal with all non-character events. */
502         switch (evp->e_event) {
503         case E_CHARACTER:
504                 break;
505         case E_ERR:
506         case E_EOF:
507                 F_SET(sp, SC_EXIT_FORCE);
508                 return (1);
509         case E_INTERRUPT:
510                 /*
511                  * !!!
512                  * Historically, <interrupt> exited the user from text input
513                  * mode or cancelled a colon command, and returned to command
514                  * mode.  It also beeped the terminal, but that seems a bit
515                  * excessive.
516                  */
517                 goto k_escape;
518         case E_REPAINT:
519                 if (vs_repaint(sp, &ev))
520                         return (1);
521                 goto next;
522         case E_WRESIZE:
523                 /* <resize> interrupts the input mode. */
524                 v_emsg(sp, NULL, VIM_WRESIZE);
525                 goto k_escape;
526         default:
527                 v_event_err(sp, evp);
528                 goto k_escape;
529         }
530
531         /*
532          * !!!
533          * If the first character of the input is a nul, replay the previous
534          * input.  (Historically, it's okay to replay non-existent input.)
535          * This was not documented as far as I know, and is a great test of vi
536          * clones.
537          */
538         if (LF_ISSET(TXT_RECORD) && rcol == 0 && evp->e_c == '\0') {
539                 if (vip->rep == NULL)
540                         goto done;
541
542                 abb = AB_NOTSET;
543                 LF_CLR(TXT_RECORD);
544                 LF_SET(TXT_REPLAY);
545                 goto replay;
546         }
547
548         /*
549          * File name completion and colon command-line editing.   We don't
550          * have enough meta characters, so we expect people to overload
551          * them.  If the two characters are the same, then we do file name
552          * completion if the cursor is past the first column, and do colon
553          * command-line editing if it's not.
554          */
555         if (quote == Q_NOTSET) {
556                 int L__cedit, L__filec;
557
558                 L__cedit = L__filec = 0;
559                 if (LF_ISSET(TXT_CEDIT) && O_STR(sp, O_CEDIT) != NULL &&
560                     O_STR(sp, O_CEDIT)[0] == evp->e_c)
561                         L__cedit = 1;
562                 if (LF_ISSET(TXT_FILEC) && O_STR(sp, O_FILEC) != NULL &&
563                     O_STR(sp, O_FILEC)[0] == evp->e_c)
564                         L__filec = 1;
565                 if (L__cedit == 1 && (L__filec == 0 || tp->cno == tp->offset)) {
566                         tp->term = TERM_CEDIT;
567                         goto k_escape;
568                 }
569                 if (L__filec == 1) {
570                         if (txt_fc(sp, tp, &filec_redraw))
571                                 goto err;
572                         goto resolve;
573                 }
574         }
575
576         /* Abbreviation overflow check.  See comment in txt_abbrev(). */
577 #define MAX_ABBREVIATION_EXPANSION      256
578         if (F_ISSET(&evp->e_ch, CH_ABBREVIATED)) {
579                 if (++abcnt > MAX_ABBREVIATION_EXPANSION) {
580                         if (v_event_flush(sp, CH_ABBREVIATED))
581                                 msgq(sp, M_ERR,
582 "191|Abbreviation exceeded expansion limit: characters discarded");
583                         abcnt = 0;
584                         if (LF_ISSET(TXT_REPLAY))
585                                 goto done;
586                         goto resolve;
587                 }
588         } else
589                 abcnt = 0;
590
591         /* Check to see if the character fits into the replay buffers. */
592         if (LF_ISSET(TXT_RECORD)) {
593                 BINC_GOTO(sp, EVENT, vip->rep,
594                     vip->rep_len, (rcol + 1) * sizeof(EVENT));
595                 vip->rep[rcol++] = *evp;
596         }
597
598 replay: if (LF_ISSET(TXT_REPLAY)) {
599                 if (rcol == vip->rep_cnt)
600                         goto k_escape;
601                 evp = vip->rep + rcol++;
602         }
603
604         /* Wrapmargin check for leading space. */
605         if (wm_skip) {
606                 wm_skip = 0;
607                 if (evp->e_c == ' ')
608                         goto resolve;
609         }
610
611         /* If quoted by someone else, simply insert the character. */
612         if (F_ISSET(&evp->e_ch, CH_QUOTED))
613                 goto insq_ch;
614
615         /*
616          * !!!
617          * If this character was quoted by a K_VLNEXT or a backslash, replace
618          * the placeholder (a carat or a backslash) with the new character.
619          * If it was quoted by a K_VLNEXT, we've already adjusted the cursor
620          * because it has to appear on top of the placeholder character.  If
621          * it was quoted by a backslash, adjust the cursor now, the cursor
622          * doesn't appear on top of it.  Historic practice in both cases.
623          *
624          * Skip tests for abbreviations; ":ab xa XA" followed by "ixa^V<space>"
625          * doesn't perform an abbreviation.  Special case, ^V^J (not ^V^M) is
626          * the same as ^J, historically.
627          */
628         if (quote == Q_BTHIS || quote == Q_VTHIS) {
629                 FL_CLR(ec_flags, EC_QUOTED);
630                 if (LF_ISSET(TXT_MAPINPUT))
631                         FL_SET(ec_flags, EC_MAPINPUT);
632
633                 if (quote == Q_BTHIS &&
634                     (evp->e_value == K_VERASE || evp->e_value == K_VKILL)) {
635                         quote = Q_NOTSET;
636                         --tp->cno;
637                         ++tp->owrite;
638                         goto insl_ch;
639                 }
640                 if (quote == Q_VTHIS && evp->e_value != K_NL) {
641                         quote = Q_NOTSET;
642                         goto insl_ch;
643                 }
644                 quote = Q_NOTSET;
645         }
646
647         /*
648          * !!!
649          * Translate "<CH_HEX>[isxdigit()]*" to a character with a hex value:
650          * this test delimits the value by any non-hex character.  Offset by
651          * one, we use 0 to mean that we've found <CH_HEX>.
652          */
653         if (hexcnt > 1 && !ISXDIGIT(evp->e_c)) {
654                 hexcnt = 0;
655                 if (txt_hex(sp, tp))
656                         goto err;
657         }
658
659         switch (evp->e_value) {
660         case K_CR:                              /* Carriage return. */
661         case K_NL:                              /* New line. */
662                 /* Return in script windows and the command line. */
663 k_cr:           if (LF_ISSET(TXT_CR)) {
664                         /*
665                          * If this was a map, we may have not displayed
666                          * the line.  Display it, just in case.
667                          *
668                          * If a script window and not the colon line,
669                          * push a <cr> so it gets executed.
670                          */
671                         if (LF_ISSET(TXT_INFOLINE)) {
672                                 if (vs_change(sp, tp->lno, LINE_RESET))
673                                         goto err;
674                         } else if (F_ISSET(sp, SC_SCRIPT))
675                                 (void)v_event_push(sp, NULL, L("\r"), 1, CH_NOMAP);
676
677                         /* Set term condition: if empty. */
678                         if (tp->cno <= tp->offset)
679                                 tp->term = TERM_CR;
680                         /*
681                          * Set term condition: if searching incrementally and
682                          * the user entered a pattern, return a completed
683                          * search, regardless if the entire pattern was found.
684                          */
685                         if (FL_ISSET(is_flags, IS_RUNNING) &&
686                             tp->cno >= tp->offset + 1)
687                                 tp->term = TERM_SEARCH;
688
689                         goto k_escape;
690                 }
691
692 #define LINE_RESOLVE {                                                  \
693                 /*                                                      \
694                  * Handle abbreviations.  If there was one, discard the \
695                  * replay characters.                                   \
696                  */                                                     \
697                 if (abb == AB_INWORD &&                                 \
698                     !LF_ISSET(TXT_REPLAY) && F_ISSET(gp, G_ABBREV)) {   \
699                         if (txt_abbrev(sp, tp, &evp->e_c,               \
700                             LF_ISSET(TXT_INFOLINE), &tmp,               \
701                             &ab_turnoff))                               \
702                                 goto err;                               \
703                         if (tmp) {                                      \
704                                 if (LF_ISSET(TXT_RECORD))               \
705                                         rcol -= tmp + 1;                \
706                                 goto resolve;                           \
707                         }                                               \
708                 }                                                       \
709                 if (abb != AB_NOTSET)                                   \
710                         abb = AB_NOTWORD;                               \
711                 if (UNMAP_TST)                                          \
712                         txt_unmap(sp, tp, &ec_flags);                   \
713                 /*                                                      \
714                  * Delete any appended cursor.  It's possible to get in \
715                  * situations where TXT_APPENDEOL is set but tp->insert \
716                  * is 0 when using the R command and all the characters \
717                  * are tp->owrite characters.                           \
718                  */                                                     \
719                 if (LF_ISSET(TXT_APPENDEOL) && tp->insert > 0) {        \
720                         --tp->len;                                      \
721                         --tp->insert;                                   \
722                 }                                                       \
723 }
724                 LINE_RESOLVE;
725
726                 /*
727                  * Save the current line information for restoration in
728                  * txt_backup(), and set the line final length.
729                  */
730                 tp->sv_len = tp->len;
731                 tp->sv_cno = tp->cno;
732                 tp->len = tp->cno;
733
734                 /* Update the old line. */
735                 if (vs_change(sp, tp->lno, LINE_RESET))
736                         goto err;
737
738                 /*
739                  * Historic practice, when the autoindent edit option was set,
740                  * was to delete <blank> characters following the inserted
741                  * newline.  This affected the 'R', 'c', and 's' commands; 'c'
742                  * and 's' retained the insert characters only, 'R' moved the
743                  * overwrite and insert characters into the next TEXT structure.
744                  * We keep track of the number of characters erased for the 'R'
745                  * command so that the final resolution of the line is correct.
746                  */
747                 tp->R_erase = 0;
748                 owrite = tp->owrite;
749                 insert = tp->insert;
750                 if (LF_ISSET(TXT_REPLACE) && owrite != 0) {
751                         for (p = tp->lb + tp->cno; owrite > 0 && isblank(*p);
752                             ++p, --owrite, ++tp->R_erase);
753                         if (owrite == 0)
754                                 for (; insert > 0 && isblank(*p);
755                                     ++p, ++tp->R_erase, --insert);
756                 } else {
757                         p = tp->lb + tp->cno + owrite;
758                         if (O_ISSET(sp, O_AUTOINDENT))
759                                 for (; insert > 0 &&
760                                     isblank(*p); ++p, --insert);
761                         owrite = 0;
762                 }
763
764                 /*
765                  * !!!
766                  * Create a new line and insert the new TEXT into the queue.
767                  * DON'T insert until the old line has been updated, or the
768                  * inserted line count in line.c:db_get() will be wrong.
769                  */
770                 if ((ntp = text_init(sp, p,
771                     insert + owrite, insert + owrite + 32)) == NULL)
772                         goto err;
773                 TAILQ_INSERT_TAIL(sp->tiq, ntp, q);
774
775                 /* Set up bookkeeping for the new line. */
776                 ntp->insert = insert;
777                 ntp->owrite = owrite;
778                 ntp->lno = tp->lno + 1;
779
780                 /*
781                  * Reset the autoindent line value.  0^D keeps the autoindent
782                  * line from changing, ^D changes the level, even if there were
783                  * no characters in the old line.  Note, if using the current
784                  * tp structure, use the cursor as the length, the autoindent
785                  * characters may have been erased.
786                  */
787                 if (LF_ISSET(TXT_AUTOINDENT)) {
788                         if (nochange) {
789                                 nochange = 0;
790                                 if (v_txt_auto(sp, OOBLNO, &ait, ait.ai, ntp))
791                                         goto err;
792                                 FREE_SPACEW(sp, ait.lb, ait.lb_len);
793                         } else
794                                 if (v_txt_auto(sp, OOBLNO, tp, tp->cno, ntp))
795                                         goto err;
796                         carat = C_NOTSET;
797                 }
798
799                 /* Reset the cursor. */
800                 ntp->cno = ntp->ai;
801
802                 /*
803                  * If we're here because wrapmargin was set and we've broken a
804                  * line, there may be additional information (i.e. the start of
805                  * a line) in the wmt structure.
806                  */
807                 if (wm_set) {
808                         if (wmt.offset != 0 ||
809                             wmt.owrite != 0 || wmt.insert != 0) {
810 #define WMTSPACE        wmt.offset + wmt.owrite + wmt.insert
811                                 BINC_GOTOW(sp, ntp->lb,
812                                     ntp->lb_len, ntp->len + WMTSPACE + 32);
813                                 MEMMOVE(ntp->lb + ntp->cno, wmt.lb, WMTSPACE);
814                                 ntp->len += WMTSPACE;
815                                 ntp->cno += wmt.offset;
816                                 ntp->owrite = wmt.owrite;
817                                 ntp->insert = wmt.insert;
818                         }
819                         wm_set = 0;
820                 }
821
822                 /* New lines are TXT_APPENDEOL. */
823                 if (ntp->owrite == 0 && ntp->insert == 0) {
824                         BINC_GOTOW(sp, ntp->lb, ntp->lb_len, ntp->len + 1);
825                         LF_SET(TXT_APPENDEOL);
826                         ntp->lb[ntp->cno] = CH_CURSOR;
827                         ++ntp->insert;
828                         ++ntp->len;
829                 }
830
831                 /* Swap old and new TEXT's, and update the new line. */
832                 tp = ntp;
833                 if (vs_change(sp, tp->lno, LINE_INSERT))
834                         goto err;
835
836                 goto resolve;
837         case K_ESCAPE:                          /* Escape. */
838                 if (!LF_ISSET(TXT_ESCAPE))
839                         goto ins_ch;
840
841                 /* If we have a count, start replaying the input. */
842                 if (rcount > 1) {
843                         --rcount;
844
845                         vip->rep_cnt = rcol;
846                         rcol = 0;
847                         abb = AB_NOTSET;
848                         LF_CLR(TXT_RECORD);
849                         LF_SET(TXT_REPLAY);
850
851                         /*
852                          * Some commands (e.g. 'o') need a <newline> for each
853                          * repetition.
854                          */
855                         if (LF_ISSET(TXT_ADDNEWLINE))
856                                 goto k_cr;
857
858                         /*
859                          * The R command turns into the 'a' command after the
860                          * first repetition.
861                          */
862                         if (LF_ISSET(TXT_REPLACE)) {
863                                 tp->insert = tp->owrite;
864                                 tp->owrite = 0;
865                                 LF_CLR(TXT_REPLACE);
866                         }
867                         goto replay;
868                 }
869
870                 /* Set term condition: if empty. */
871                 if (tp->cno <= tp->offset)
872                         tp->term = TERM_ESC;
873                 /*
874                  * Set term condition: if searching incrementally and the user
875                  * entered a pattern, return a completed search, regardless if
876                  * the entire pattern was found.
877                  */
878                 if (FL_ISSET(is_flags, IS_RUNNING) && tp->cno >= tp->offset + 1)
879                         tp->term = TERM_SEARCH;
880
881 k_escape:       LINE_RESOLVE;
882
883                 /*
884                  * Clean up for the 'R' command, restoring overwrite
885                  * characters, and making them into insert characters.
886                  */
887                 if (LF_ISSET(TXT_REPLACE))
888                         txt_Rresolve(sp, sp->tiq, tp, len);
889
890                 /*
891                  * If there are any overwrite characters, copy down
892                  * any insert characters, and decrement the length.
893                  */
894                 if (tp->owrite) {
895                         if (tp->insert)
896                                 MEMMOVE(tp->lb + tp->cno,
897                                     tp->lb + tp->cno + tp->owrite, tp->insert);
898                         tp->len -= tp->owrite;
899                 }
900
901                 /*
902                  * Optionally resolve the lines into the file.  If not
903                  * resolving the lines into the file, end the line with
904                  * a nul.  If the line is empty, then set the length to
905                  * 0, the termination condition has already been set.
906                  *
907                  * XXX
908                  * This is wrong, should pass back a length.
909                  */
910                 if (LF_ISSET(TXT_RESOLVE)) {
911                         if (txt_resolve(sp, sp->tiq, flags))
912                                 goto err;
913                 } else {
914                         BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
915                         tp->lb[tp->len] = '\0';
916                 }
917
918                 /*
919                  * Set the return cursor position to rest on the last
920                  * inserted character.
921                  */
922                 if (tp->cno != 0)
923                         --tp->cno;
924
925                 /* Update the last line. */
926                 if (vs_change(sp, tp->lno, LINE_RESET))
927                         return (1);
928                 goto done;
929         case K_CARAT:                   /* Delete autoindent chars. */
930                 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
931                         carat = C_CARATSET;
932                 goto ins_ch;
933         case K_ZERO:                    /* Delete autoindent chars. */
934                 if (tp->cno <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
935                         carat = C_ZEROSET;
936                 goto ins_ch;
937         case K_CNTRLD:                  /* Delete autoindent char. */
938                 /*
939                  * If in the first column or no characters to erase, ignore
940                  * the ^D (this matches historic practice).  If not doing
941                  * autoindent or already inserted non-ai characters, it's a
942                  * literal.  The latter test is done in the switch, as the
943                  * CARAT forms are N + 1, not N.
944                  */
945                 if (!LF_ISSET(TXT_AUTOINDENT))
946                         goto ins_ch;
947                 if (tp->cno == 0)
948                         goto resolve;
949
950                 switch (carat) {
951                 case C_CARATSET:        /* ^^D */
952                         if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
953                                 goto ins_ch;
954
955                         /* Save the ai string for later. */
956                         ait.lb = NULL;
957                         ait.lb_len = 0;
958                         BINC_GOTOW(sp, ait.lb, ait.lb_len, tp->ai);
959                         MEMMOVE(ait.lb, tp->lb, tp->ai);
960                         ait.ai = ait.len = tp->ai;
961
962                         carat = C_NOTSET;
963                         nochange = 1;
964                         goto leftmargin;
965                 case C_ZEROSET:         /* 0^D */
966                         if (tp->ai == 0 || tp->cno > tp->ai + tp->offset + 1)
967                                 goto ins_ch;
968
969                         carat = C_NOTSET;
970 leftmargin:             tp->lb[tp->cno - 1] = ' ';
971                         tp->owrite += tp->cno - tp->offset;
972                         tp->ai = 0;
973                         tp->cno = tp->offset;
974                         break;
975                 case C_NOTSET:          /* ^D */
976                         if (tp->ai == 0 || tp->cno > tp->ai + tp->offset)
977                                 goto ins_ch;
978
979                         (void)txt_dent(sp, tp, 0);
980                         break;
981                 default:
982                         abort();
983                 }
984                 break;
985         case K_VERASE:                  /* Erase the last character. */
986                 /* If can erase over the prompt, return. */
987                 if (tp->cno <= tp->offset && LF_ISSET(TXT_BS)) {
988                         tp->term = TERM_BS;
989                         goto done;
990                 }
991
992                 /*
993                  * If at the beginning of the line, try and drop back to a
994                  * previously inserted line.
995                  */
996                 if (tp->cno == 0) {
997                         if ((ntp =
998                             txt_backup(sp, sp->tiq, tp, &flags)) == NULL)
999                                 goto err;
1000                         tp = ntp;
1001                         break;
1002                 }
1003
1004                 /* If nothing to erase, bell the user. */
1005                 if (tp->cno <= tp->offset) {
1006                         if (!LF_ISSET(TXT_REPLAY))
1007                                 txt_nomorech(sp);
1008                         break;
1009                 }
1010
1011                 /* Drop back one character. */
1012                 --tp->cno;
1013
1014                 /*
1015                  * Historically, vi didn't replace the erased characters with
1016                  * <blank>s, presumably because it's easier to fix a minor
1017                  * typing mistake and continue on if the previous letters are
1018                  * already there.  This is a problem for incremental searching,
1019                  * because the user can no longer tell where they are in the
1020                  * colon command line because the cursor is at the last search
1021                  * point in the screen.  So, if incrementally searching, erase
1022                  * the erased characters from the screen.
1023                  */
1024                 if (FL_ISSET(is_flags, IS_RUNNING))
1025                         tp->lb[tp->cno] = ' ';
1026
1027                 /*
1028                  * Increment overwrite, decrement ai if deleted.
1029                  *
1030                  * !!!
1031                  * Historic vi did not permit users to use erase characters
1032                  * to delete autoindent characters.  We do.  Eat hot death,
1033                  * POSIX.
1034                  */
1035                 ++tp->owrite;
1036                 if (tp->cno < tp->ai)
1037                         --tp->ai;
1038
1039                 /* Reset if we deleted an incremental search character. */
1040                 if (FL_ISSET(is_flags, IS_RUNNING))
1041                         FL_SET(is_flags, IS_RESTART);
1042                 break;
1043         case K_VWERASE:                 /* Skip back one word. */
1044                 /*
1045                  * If at the beginning of the line, try and drop back to a
1046                  * previously inserted line.
1047                  */
1048                 if (tp->cno == 0) {
1049                         if ((ntp =
1050                             txt_backup(sp, sp->tiq, tp, &flags)) == NULL)
1051                                 goto err;
1052                         tp = ntp;
1053                 }
1054
1055                 /*
1056                  * If at offset, nothing to erase so bell the user.
1057                  */
1058                 if (tp->cno <= tp->offset) {
1059                         if (!LF_ISSET(TXT_REPLAY))
1060                                 txt_nomorech(sp);
1061                         break;
1062                 }
1063
1064                 /*
1065                  * The first werase goes back to any autoindent column and the
1066                  * second werase goes back to the offset.
1067                  *
1068                  * !!!
1069                  * Historic vi did not permit users to use erase characters to
1070                  * delete autoindent characters.
1071                  */
1072                 if (tp->ai && tp->cno > tp->ai)
1073                         max = tp->ai;
1074                 else {
1075                         tp->ai = 0;
1076                         max = tp->offset;
1077                 }
1078
1079                 /* Skip over trailing space characters. */
1080                 while (tp->cno > max && ISBLANK(tp->lb[tp->cno - 1])) {
1081                         --tp->cno;
1082                         ++tp->owrite;
1083                 }
1084                 if (tp->cno == max)
1085                         break;
1086                 /*
1087                  * There are three types of word erase found on UNIX systems.
1088                  * They can be identified by how the string /a/b/c is treated
1089                  * -- as 1, 3, or 6 words.  Historic vi had two classes of
1090                  * characters, and strings were delimited by them and
1091                  * <blank>'s, so, 6 words.  The historic tty interface used
1092                  * <blank>'s to delimit strings, so, 1 word.  The algorithm
1093                  * offered in the 4.4BSD tty interface (as stty altwerase)
1094                  * treats it as 3 words -- there are two classes of
1095                  * characters, and strings are delimited by them and
1096                  * <blank>'s.  The difference is that the type of the first
1097                  * erased character erased is ignored, which is exactly right
1098                  * when erasing pathname components.  The edit options
1099                  * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD tty
1100                  * interface and the historic tty driver behavior,
1101                  * respectively, and the default is the same as the historic
1102                  * vi behavior.
1103                  *
1104                  * Overwrite erased characters if doing incremental search;
1105                  * see comment above.
1106                  */
1107                 if (LF_ISSET(TXT_TTYWERASE))
1108                         while (tp->cno > max) {
1109                                 if (ISBLANK(tp->lb[tp->cno - 1]))
1110                                         break;
1111                                 --tp->cno;
1112                                 ++tp->owrite;
1113                                 if (FL_ISSET(is_flags, IS_RUNNING))
1114                                         tp->lb[tp->cno] = ' ';
1115                         }
1116                 else {
1117                         if (LF_ISSET(TXT_ALTWERASE)) {
1118                                 --tp->cno;
1119                                 ++tp->owrite;
1120                                 if (FL_ISSET(is_flags, IS_RUNNING))
1121                                         tp->lb[tp->cno] = ' ';
1122                         }
1123                         if (tp->cno > max)
1124                                 tmp = inword(tp->lb[tp->cno - 1]);
1125                         while (tp->cno > max) {
1126                                 if (tmp != inword(tp->lb[tp->cno - 1])
1127                                     || ISBLANK(tp->lb[tp->cno - 1]))
1128                                         break;
1129                                 --tp->cno;
1130                                 ++tp->owrite;
1131                                 if (FL_ISSET(is_flags, IS_RUNNING))
1132                                         tp->lb[tp->cno] = ' ';
1133                         }
1134                 }
1135
1136                 /* Reset if we deleted an incremental search character. */
1137                 if (FL_ISSET(is_flags, IS_RUNNING))
1138                         FL_SET(is_flags, IS_RESTART);
1139                 break;
1140         case K_VKILL:                   /* Restart this line. */
1141                 /*
1142                  * !!!
1143                  * If at the beginning of the line, try and drop back to a
1144                  * previously inserted line.  Historic vi did not permit
1145                  * users to go back to previous lines.
1146                  */
1147                 if (tp->cno == 0) {
1148                         if ((ntp =
1149                             txt_backup(sp, sp->tiq, tp, &flags)) == NULL)
1150                                 goto err;
1151                         tp = ntp;
1152                 }
1153
1154                 /* If at offset, nothing to erase so bell the user. */
1155                 if (tp->cno <= tp->offset) {
1156                         if (!LF_ISSET(TXT_REPLAY))
1157                                 txt_nomorech(sp);
1158                         break;
1159                 }
1160
1161                 /*
1162                  * First kill goes back to any autoindent and second kill goes
1163                  * back to the offset.
1164                  *
1165                  * !!!
1166                  * Historic vi did not permit users to use erase characters to
1167                  * delete autoindent characters.
1168                  */
1169                 if (tp->ai && tp->cno > tp->ai)
1170                         max = tp->ai;
1171                 else {
1172                         tp->ai = 0;
1173                         max = tp->offset;
1174                 }
1175                 tp->owrite += tp->cno - max;
1176
1177                 /*
1178                  * Overwrite erased characters if doing incremental search;
1179                  * see comment above.
1180                  */
1181                 if (FL_ISSET(is_flags, IS_RUNNING))
1182                         do {
1183                                 tp->lb[--tp->cno] = ' ';
1184                         } while (tp->cno > max);
1185                 else
1186                         tp->cno = max;
1187
1188                 /* Reset if we deleted an incremental search character. */
1189                 if (FL_ISSET(is_flags, IS_RUNNING))
1190                         FL_SET(is_flags, IS_RESTART);
1191                 break;
1192         case K_CNTRLT:                  /* Add autoindent characters. */
1193                 if (!LF_ISSET(TXT_CNTRLT))
1194                         goto ins_ch;
1195                 if (txt_dent(sp, tp, 1))
1196                         goto err;
1197                 goto ebuf_chk;
1198         case K_BACKSLASH:               /* Quote next erase/kill. */
1199                 /*
1200                  * !!!
1201                  * Historic vi tried to make abbreviations after a backslash
1202                  * escape work.  If you did ":ab x y", and inserted "x\^H",
1203                  * (assuming the erase character was ^H) you got "x^H", and
1204                  * no abbreviation was done.  If you inserted "x\z", however,
1205                  * it tried to back up and do the abbreviation, i.e. replace
1206                  * 'x' with 'y'.  The problem was it got it wrong, and you
1207                  * ended up with "zy\".
1208                  *
1209                  * This is really hard to do (you have to remember the
1210                  * word/non-word state, for example), and doesn't make any
1211                  * sense to me.  Both backslash and the characters it
1212                  * (usually) escapes will individually trigger the
1213                  * abbreviation, so I don't see why the combination of them
1214                  * wouldn't.  I don't expect to get caught on this one,
1215                  * particularly since it never worked right, but I've been
1216                  * wrong before.
1217                  *
1218                  * Do the tests for abbreviations, so ":ab xa XA",
1219                  * "ixa\<K_VERASE>" performs the abbreviation.
1220                  */
1221                 quote = Q_BNEXT;
1222                 goto insq_ch;
1223         case K_VLNEXT:                  /* Quote next character. */
1224                 evp->e_c = '^';
1225                 quote = Q_VNEXT;
1226                 /*
1227                  * Turn on the quote flag so that the underlying routines
1228                  * quote the next character where it's possible. Turn off
1229                  * the input mapbiting flag so that we don't remap the next
1230                  * character.
1231                  */
1232                 FL_SET(ec_flags, EC_QUOTED);
1233                 FL_CLR(ec_flags, EC_MAPINPUT);
1234
1235                 /*
1236                  * !!!
1237                  * Skip the tests for abbreviations, so ":ab xa XA",
1238                  * "ixa^V<space>" doesn't perform the abbreviation.
1239                  */
1240                 goto insl_ch;
1241         case K_HEXCHAR:
1242                 hexcnt = 1;
1243                 goto insq_ch;
1244         default:                        /* Insert the character. */
1245                 if (LF_ISSET(TXT_SHOWMATCH)) {
1246                         CHAR_T *match_chars, *cp;
1247
1248                         match_chars = VIP(sp)->mcs;
1249                         cp = STRCHR(match_chars, evp->e_c);
1250                         if (cp != NULL && (cp - match_chars) & 1)
1251                                 showmatch = 1;
1252                 }
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>.
1265                  */
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;
1273                 }
1274
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.
1280                  */
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;
1291                                 }
1292                         }
1293                         if (isblank(evp->e_c) && UNMAP_TST)
1294                                 txt_unmap(sp, tp, &ec_flags);
1295                 }
1296                 if (abb != AB_NOTSET)
1297                         abb = inword(evp->e_c) ? AB_INWORD : AB_NOTWORD;
1298
1299 insl_ch:        if (txt_insch(sp, tp, &evp->e_c, flags))
1300                         goto err;
1301
1302                 /*
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.
1306                  */
1307                 if (quote == Q_VNEXT) {
1308                         --tp->cno;
1309                         ++tp->owrite;
1310                 }
1311
1312                 /*
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>.
1318                  */
1319                 if (hexcnt != 0 && hexcnt++ == 3) {
1320                         hexcnt = 0;
1321                         if (txt_hex(sp, tp))
1322                                 goto err;
1323                 }
1324
1325                 /*
1326                  * Check to see if we've crossed the margin.
1327                  *
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.
1335                  */
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;
1347                                 }
1348                         }
1349                 }
1350
1351                 /*
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.
1356                  */
1357 ebuf_chk:       if (tp->cno >= tp->len) {
1358                         BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
1359                         LF_SET(TXT_APPENDEOL);
1360
1361                         tp->lb[tp->cno] = CH_CURSOR;
1362                         ++tp->insert;
1363                         ++tp->len;
1364                 }
1365
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;
1372                 }
1373                 break;
1374         }
1375
1376 #ifdef DEBUG
1377         if (tp->cno + tp->insert + tp->owrite != tp->len) {
1378                 msgq(sp, M_ERR,
1379                     "len %zu != cno: %zu ai: %zu insert %zu overwrite %zu",
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;
1384         }
1385 #endif
1386
1387 resolve:/*
1388          * 1: If we don't need to know where the cursor really is and we're
1389          *    replaying text, keep going.
1390          */
1391         if (margin == 0 && LF_ISSET(TXT_REPLAY))
1392                 goto replay;
1393
1394         /*
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.
1399          */
1400         if ((margin != 0 || !KEYS_WAITING(sp)) &&
1401             vs_change(sp, tp->lno, LINE_RESET))
1402                 return (1);
1403
1404         /*
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.
1408          */
1409         if (showmatch) {
1410                 if (!KEYS_WAITING(sp) && txt_showmatch(sp, tp))
1411                         return (1);
1412                 showmatch = 0;
1413         }
1414
1415         /*
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.
1418          */
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);
1423
1424         /*
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.
1427          */
1428         if (margin != 0 || !KEYS_WAITING(sp)) {
1429                 UPDATE_POSITION(sp, tp);
1430                 if (vs_refresh(sp, margin != 0))
1431                         return (1);
1432         }
1433
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);
1437
1438         /* 7: Next character... */
1439         if (LF_ISSET(TXT_REPLAY))
1440                 goto replay;
1441         goto next;
1442
1443 done:   /* Leave input mode. */
1444         F_CLR(sp, SC_TINPUT);
1445
1446         /* If recording for playback, save it. */
1447         if (LF_ISSET(TXT_RECORD))
1448                 vip->rep_cnt = rcol;
1449
1450         /*
1451          * If not working on the colon command line, set the final cursor
1452          * position.
1453          */
1454         if (!F_ISSET(sp, SC_TINPUT_INFO)) {
1455                 vp->m_final.lno = tp->lno;
1456                 vp->m_final.cno = tp->cno;
1457         }
1458         return (0);
1459
1460 err:
1461 alloc_err:
1462         F_CLR(sp, SC_TINPUT);
1463         txt_err(sp, sp->tiq);
1464         return (1);
1465 }
1466
1467 /*
1468  * txt_abbrev --
1469  *      Handle abbreviations.
1470  */
1471 static int
1472 txt_abbrev(SCR *sp, TEXT *tp, CHAR_T *pushcp, int isinfoline, int *didsubp, int *turnoffp)
1473 {
1474         VI_PRIVATE *vip;
1475         CHAR_T ch, *p;
1476         SEQ *qp;
1477         size_t len, off;
1478
1479         /* Check to make sure we're not at the start of an append. */
1480         *didsubp = 0;
1481         if (tp->cno == tp->offset)
1482                 return (0);
1483
1484         vip = VIP(sp);
1485
1486         /*
1487          * Find the start of the "word".
1488          *
1489          * !!!
1490          * We match historic practice, which, as far as I can tell, had an
1491          * off-by-one error.  The way this worked was that when the inserted
1492          * text switched from a "word" character to a non-word character,
1493          * vi would check for possible abbreviations.  It would then take the
1494          * type (i.e. word/non-word) of the character entered TWO characters
1495          * ago, and move backward in the text until reaching a character that
1496          * was not that type, or the beginning of the insert, the line, or
1497          * the file.  For example, in the string "abc<space>", when the <space>
1498          * character triggered the abbreviation check, the type of the 'b'
1499          * character was used for moving through the string.  Maybe there's a
1500          * reason for not using the first (i.e. 'c') character, but I can't
1501          * think of one.
1502          *
1503          * Terminate at the beginning of the insert or the character after the
1504          * offset character -- both can be tested for using tp->offset.
1505          */
1506         off = tp->cno - 1;                      /* Previous character. */
1507         p = tp->lb + off;
1508         len = 1;                                /* One character test. */
1509         if (off == tp->offset || isblank(p[-1]))
1510                 goto search;
1511         if (inword(p[-1]))                      /* Move backward to change. */
1512                 for (;;) {
1513                         --off; --p; ++len;
1514                         if (off == tp->offset || !inword(p[-1]))
1515                                 break;
1516                 }
1517         else
1518                 for (;;) {
1519                         --off; --p; ++len;
1520                         if (off == tp->offset ||
1521                             inword(p[-1]) || isblank(p[-1]))
1522                                 break;
1523                 }
1524
1525         /*
1526          * !!!
1527          * Historic vi exploded abbreviations on the command line.  This has
1528          * obvious problems in that unabbreviating the string can be extremely
1529          * tricky, particularly if the string has, say, an embedded escape
1530          * character.  Personally, I think it's a stunningly bad idea.  Other
1531          * examples of problems this caused in historic vi are:
1532          *      :ab foo bar
1533          *      :ab foo baz
1534          * results in "bar" being abbreviated to "baz", which wasn't what the
1535          * user had in mind at all.  Also, the commands:
1536          *      :ab foo bar
1537          *      :unab foo<space>
1538          * resulted in an error message that "bar" wasn't mapped.  Finally,
1539          * since the string was already exploded by the time the unabbreviate
1540          * command got it, all it knew was that an abbreviation had occurred.
1541          * Cleverly, it checked the replacement string for its unabbreviation
1542          * match, which meant that the commands:
1543          *      :ab foo1 bar
1544          *      :ab foo2 bar
1545          *      :unab foo2
1546          * unabbreviate "foo1", and the commands:
1547          *      :ab foo bar
1548          *      :ab bar baz
1549          * unabbreviate "foo"!
1550          *
1551          * Anyway, people neglected to first ask my opinion before they wrote
1552          * macros that depend on this stuff, so, we make this work as follows.
1553          * When checking for an abbreviation on the command line, if we get a
1554          * string which is <blank> terminated and which starts at the beginning
1555          * of the line, we check to see it is the abbreviate or unabbreviate
1556          * commands.  If it is, turn abbreviations off and return as if no
1557          * abbreviation was found.  Note also, minor trickiness, so that if
1558          * the user erases the line and starts another command, we turn the
1559          * abbreviations back on.
1560          *
1561          * This makes the layering look like a Nachos Supreme.
1562          */
1563 search: if (isinfoline)
1564                 if (off == tp->ai || off == tp->offset)
1565                         if (ex_is_abbrev(p, len)) {
1566                                 *turnoffp = 1;
1567                                 return (0);
1568                         } else
1569                                 *turnoffp = 0;
1570                 else
1571                         if (*turnoffp)
1572                                 return (0);
1573
1574         /* Check for any abbreviations. */
1575         if ((qp = seq_find(sp, NULL, NULL, p, len, SEQ_ABBREV, NULL)) == NULL)
1576                 return (0);
1577
1578         /*
1579          * Push the abbreviation onto the tty stack.  Historically, characters
1580          * resulting from an abbreviation expansion were themselves subject to
1581          * map expansions, O_SHOWMATCH matching etc.  This means the expanded
1582          * characters will be re-tested for abbreviations.  It's difficult to
1583          * know what historic practice in this case was, since abbreviations
1584          * were applied to :colon command lines, so entering abbreviations that
1585          * looped was tricky, although possible.  In addition, obvious loops
1586          * didn't work as expected.  (The command ':ab a b|ab b c|ab c a' will
1587          * silently only implement and/or display the last abbreviation.)
1588          *
1589          * This implementation doesn't recover well from such abbreviations.
1590          * The main input loop counts abbreviated characters, and, when it
1591          * reaches a limit, discards any abbreviated characters on the queue.
1592          * It's difficult to back up to the original position, as the replay
1593          * queue would have to be adjusted, and the line state when an initial
1594          * abbreviated character was received would have to be saved.
1595          */
1596         ch = *pushcp;
1597         if (v_event_push(sp, NULL, &ch, 1, CH_ABBREVIATED))
1598                 return (1);
1599         if (v_event_push(sp, NULL, qp->output, qp->olen, CH_ABBREVIATED))
1600                 return (1);
1601
1602         /*
1603          * If the size of the abbreviation is larger than or equal to the size
1604          * of the original text, move to the start of the replaced characters,
1605          * and add their length to the overwrite count.
1606          *
1607          * If the abbreviation is smaller than the original text, we have to
1608          * delete the additional overwrite characters and copy down any insert
1609          * characters.
1610          */
1611         tp->cno -= len;
1612         if (qp->olen >= len)
1613                 tp->owrite += len;
1614         else {
1615                 if (tp->insert)
1616                         MEMMOVE(tp->lb + tp->cno + qp->olen,
1617                             tp->lb + tp->cno + tp->owrite + len, tp->insert);
1618                 tp->owrite += qp->olen;
1619                 tp->len -= len - qp->olen;
1620         }
1621
1622         /*
1623          * We return the length of the abbreviated characters.  This is so
1624          * the calling routine can replace the replay characters with the
1625          * abbreviation.  This means that subsequent '.' commands will produce
1626          * the same text, regardless of intervening :[un]abbreviate commands.
1627          * This is historic practice.
1628          */
1629         *didsubp = len;
1630         return (0);
1631 }
1632
1633 /*
1634  * txt_unmap --
1635  *      Handle the unmap command.
1636  */
1637 static void
1638 txt_unmap(SCR *sp, TEXT *tp, u_int32_t *ec_flagsp)
1639 {
1640         size_t len, off;
1641         CHAR_T *p;
1642
1643         /* Find the beginning of this "word". */
1644         for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1645                 if (isblank(*p)) {
1646                         ++p;
1647                         break;
1648                 }
1649                 ++len;
1650                 if (off == tp->ai || off == tp->offset)
1651                         break;
1652         }
1653
1654         /*
1655          * !!!
1656          * Historic vi exploded input mappings on the command line.  See the
1657          * txt_abbrev() routine for an explanation of the problems inherent
1658          * in this.
1659          *
1660          * We make this work as follows.  If we get a string which is <blank>
1661          * terminated and which starts at the beginning of the line, we check
1662          * to see it is the unmap command.  If it is, we return that the input
1663          * mapping should be turned off.  Note also, minor trickiness, so that
1664          * if the user erases the line and starts another command, we go ahead
1665          * an turn mapping back on.
1666          */
1667         if ((off == tp->ai || off == tp->offset) && ex_is_unmap(p, len))
1668                 FL_CLR(*ec_flagsp, EC_MAPINPUT);
1669         else
1670                 FL_SET(*ec_flagsp, EC_MAPINPUT);
1671 }
1672
1673 /*
1674  * txt_ai_resolve --
1675  *      When a line is resolved by <esc>, review autoindent characters.
1676  */
1677 static void
1678 txt_ai_resolve(SCR *sp, TEXT *tp, int *changedp)
1679 {
1680         u_long ts;
1681         int del;
1682         size_t cno, len, new, old, scno, spaces, tab_after_sp, tabs;
1683         CHAR_T *p;
1684
1685         *changedp = 0;
1686
1687         /*
1688          * If the line is empty, has an offset, or no autoindent
1689          * characters, we're done.
1690          */
1691         if (!tp->len || tp->offset || !tp->ai)
1692                 return;
1693
1694         /*
1695          * If the length is less than or equal to the autoindent
1696          * characters, delete them.
1697          */
1698         if (tp->len <= tp->ai) {
1699                 tp->ai = tp->cno = tp->len = 0;
1700                 return;
1701         }
1702
1703         /*
1704          * The autoindent characters plus any leading <blank> characters
1705          * in the line are resolved into the minimum number of characters.
1706          * Historic practice.
1707          */
1708         ts = O_VAL(sp, O_TABSTOP);
1709
1710         /* Figure out the last <blank> screen column. */
1711         for (p = tp->lb, scno = 0, len = tp->len,
1712             spaces = tab_after_sp = 0; len-- && isblank(*p); ++p)
1713                 if (*p == '\t') {
1714                         if (spaces)
1715                                 tab_after_sp = 1;
1716                         scno += COL_OFF(scno, ts);
1717                 } else {
1718                         ++spaces;
1719                         ++scno;
1720                 }
1721
1722         /*
1723          * If there are no spaces, or no tabs after spaces and less than
1724          * ts spaces, it's already minimal.
1725          */
1726         if (!spaces || (!tab_after_sp && spaces < ts))
1727                 return;
1728
1729         /* Count up spaces/tabs needed to get to the target. */
1730         for (cno = 0, tabs = 0; cno + COL_OFF(cno, ts) <= scno; ++tabs)
1731                 cno += COL_OFF(cno, ts);
1732         spaces = scno - cno;
1733
1734         /*
1735          * Figure out how many characters we're dropping -- if we're not
1736          * dropping any, it's already minimal, we're done.
1737          */
1738         old = p - tp->lb;
1739         new = spaces + tabs;
1740         if (old == new)
1741                 return;
1742
1743         /* Shift the rest of the characters down, adjust the counts. */
1744         del = old - new;
1745         MEMMOVE(p - del, p, tp->len - old);
1746         tp->len -= del;
1747         tp->cno -= del;
1748
1749         /* Fill in space/tab characters. */
1750         for (p = tp->lb; tabs--;)
1751                 *p++ = '\t';
1752         while (spaces--)
1753                 *p++ = ' ';
1754         *changedp = 1;
1755 }
1756
1757 /*
1758  * v_txt_auto --
1759  *      Handle autoindent.  If aitp isn't NULL, use it, otherwise,
1760  *      retrieve the line.
1761  *
1762  * PUBLIC: int v_txt_auto __P((SCR *, recno_t, TEXT *, size_t, TEXT *));
1763  */
1764 int
1765 v_txt_auto(SCR *sp, recno_t lno, TEXT *aitp, size_t len, TEXT *tp)
1766 {
1767         size_t nlen;
1768         CHAR_T *p, *t;
1769
1770         if (aitp == NULL) {
1771                 /*
1772                  * If the ex append command is executed with an address of 0,
1773                  * it's possible to get here with a line number of 0.  Return
1774                  * an indent of 0.
1775                  */
1776                 if (lno == 0) {
1777                         tp->ai = 0;
1778                         return (0);
1779                 }
1780                 if (db_get(sp, lno, DBG_FATAL, &t, &len))
1781                         return (1);
1782         } else
1783                 t = aitp->lb;
1784
1785         /* Count whitespace characters. */
1786         for (p = t; len > 0; ++p, --len)
1787                 if (!isblank(*p))
1788                         break;
1789
1790         /* Set count, check for no indentation. */
1791         if ((nlen = (p - t)) == 0)
1792                 return (0);
1793
1794         /* Make sure the buffer's big enough. */
1795         BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
1796
1797         /* Copy the buffer's current contents up. */
1798         if (tp->len != 0)
1799                 MEMMOVE(tp->lb + nlen, tp->lb, tp->len);
1800         tp->len += nlen;
1801
1802         /* Copy the indentation into the new buffer. */
1803         MEMMOVE(tp->lb, t, nlen);
1804
1805         /* Set the autoindent count. */
1806         tp->ai = nlen;
1807         return (0);
1808 }
1809
1810 /*
1811  * txt_backup --
1812  *      Back up to the previously edited line.
1813  */
1814 static TEXT *
1815 txt_backup(SCR *sp, TEXTH *tiqh, TEXT *tp, u_int32_t *flagsp)
1816 {
1817         VI_PRIVATE *vip;
1818         TEXT *ntp;
1819
1820         /* Get a handle on the previous TEXT structure. */
1821         if ((ntp = TAILQ_PREV(tp, _texth, q)) == NULL) {
1822                 if (!FL_ISSET(*flagsp, TXT_REPLAY))
1823                         msgq(sp, M_BERR,
1824                             "193|Already at the beginning of the insert");
1825                 return (tp);
1826         }
1827
1828         /* Bookkeeping. */
1829         ntp->len = ntp->sv_len;
1830
1831         /* Handle appending to the line. */
1832         vip = VIP(sp);
1833         if (ntp->owrite == 0 && ntp->insert == 0) {
1834                 ntp->lb[ntp->len] = CH_CURSOR;
1835                 ++ntp->insert;
1836                 ++ntp->len;
1837                 FL_SET(*flagsp, TXT_APPENDEOL);
1838         } else
1839                 FL_CLR(*flagsp, TXT_APPENDEOL);
1840
1841         /* Release the current TEXT. */
1842         TAILQ_REMOVE(tiqh, tp, q);
1843         text_free(tp);
1844
1845         /* Update the old line on the screen. */
1846         if (vs_change(sp, ntp->lno + 1, LINE_DELETE))
1847                 return (NULL);
1848
1849         /* Return the new/current TEXT. */
1850         return (ntp);
1851 }
1852
1853 /*
1854  * Text indentation is truly strange.  ^T and ^D do movements to the next or
1855  * previous shiftwidth value, i.e. for a 1-based numbering, with shiftwidth=3,
1856  * ^T moves a cursor on the 7th, 8th or 9th column to the 10th column, and ^D
1857  * moves it back.
1858  *
1859  * !!!
1860  * The ^T and ^D characters in historical vi had special meaning only when they
1861  * were the first characters entered after entering text input mode.  As normal
1862  * erase characters couldn't erase autoindent characters (^T in this case), it
1863  * meant that inserting text into previously existing text was strange -- ^T
1864  * only worked if it was the first keystroke(s), and then could only be erased
1865  * using ^D.  This implementation treats ^T specially anywhere it occurs in the
1866  * input, and permits the standard erase characters to erase the characters it
1867  * inserts.
1868  *
1869  * !!!
1870  * A fun test is to try:
1871  *      :se sw=4 ai list
1872  *      i<CR>^Tx<CR>^Tx<CR>^Tx<CR>^Dx<CR>^Dx<CR>^Dx<esc>
1873  * Historic vi loses some of the '$' marks on the line ends, but otherwise gets
1874  * it right.
1875  *
1876  * XXX
1877  * Technically, txt_dent should be part of the screen interface, as it requires
1878  * knowledge of character sizes, including <space>s, on the screen.  It's here
1879  * because it's a complicated little beast, and I didn't want to shove it down
1880  * into the screen.  It's probable that KEY_COL will call into the screen once
1881  * there are screens with different character representations.
1882  *
1883  * txt_dent --
1884  *      Handle ^T indents, ^D outdents.
1885  *
1886  * If anything changes here, check the ex version to see if it needs similar
1887  * changes.
1888  */
1889 static int
1890 txt_dent(SCR *sp, TEXT *tp, int isindent)
1891 {
1892         CHAR_T ch;
1893         u_long sw, ts;
1894         size_t cno, current, spaces, target, tabs;
1895         int ai_reset;
1896
1897         ts = O_VAL(sp, O_TABSTOP);
1898         sw = O_VAL(sp, O_SHIFTWIDTH);
1899
1900         /*
1901          * Since we don't know what precedes the character(s) being inserted
1902          * (or deleted), the preceding whitespace characters must be resolved.
1903          * An example is a <tab>, which doesn't need a full shiftwidth number
1904          * of columns because it's preceded by <space>s.  This is easy to get
1905          * if the user sets shiftwidth to a value less than tabstop (or worse,
1906          * something for which tabstop isn't a multiple) and then uses ^T to
1907          * indent, and ^D to outdent.
1908          *
1909          * Figure out the current and target screen columns.  In the historic
1910          * vi, the autoindent column was NOT determined using display widths
1911          * of characters as was the wrapmargin column.  For that reason, we
1912          * can't use the vs_column() function, but have to calculate it here.
1913          * This is slow, but it's normally only on the first few characters of
1914          * a line.
1915          */
1916         for (current = cno = 0; cno < tp->cno; ++cno)
1917                 current += tp->lb[cno] == '\t' ?
1918                     COL_OFF(current, ts) : KEY_COL(sp, tp->lb[cno]);
1919
1920         target = current;
1921         if (isindent)
1922                 target += COL_OFF(target, sw);
1923         else {
1924                 --target;
1925                 target -= target % sw;
1926         }
1927
1928         /*
1929          * The AI characters will be turned into overwrite characters if the
1930          * cursor immediately follows them.  We test both the cursor position
1931          * and the indent flag because there's no single test.  (^T can only
1932          * be detected by the cursor position, and while we know that the test
1933          * is always true for ^D, the cursor can be in more than one place, as
1934          * "0^D" and "^D" are different.)
1935          */
1936         ai_reset = !isindent || tp->cno == tp->ai + tp->offset;
1937
1938         /*
1939          * Back up over any previous <blank> characters, changing them into
1940          * overwrite characters (including any ai characters).  Then figure
1941          * out the current screen column.
1942          */
1943         for (; tp->cno > tp->offset &&
1944             (tp->lb[tp->cno - 1] == ' ' || tp->lb[tp->cno - 1] == '\t');
1945             --tp->cno, ++tp->owrite);
1946         for (current = cno = 0; cno < tp->cno; ++cno)
1947                 current += tp->lb[cno] == '\t' ?
1948                     COL_OFF(current, ts) : KEY_COL(sp, tp->lb[cno]);
1949
1950         /*
1951          * If we didn't move up to or past the target, it's because there
1952          * weren't enough characters to delete, e.g. the first character
1953          * of the line was a tp->offset character, and the user entered
1954          * ^D to move to the beginning of a line.  An example of this is:
1955          *
1956          *      :set ai sw=4<cr>i<space>a<esc>i^T^D
1957          *
1958          * Otherwise, count up the total spaces/tabs needed to get from the
1959          * beginning of the line (or the last non-<blank> character) to the
1960          * target.
1961          */
1962         if (current >= target)
1963                 spaces = tabs = 0;
1964         else {
1965                 for (cno = current,
1966                     tabs = 0; cno + COL_OFF(cno, ts) <= target; ++tabs)
1967                         cno += COL_OFF(cno, ts);
1968                 spaces = target - cno;
1969         }
1970
1971         /* If we overwrote ai characters, reset the ai count. */
1972         if (ai_reset)
1973                 tp->ai = tabs + spaces;
1974
1975         /*
1976          * Call txt_insch() to insert each character, so that we get the
1977          * correct effect when we add a <tab> to replace N <spaces>.
1978          */
1979         for (ch = '\t'; tabs > 0; --tabs)
1980                 (void)txt_insch(sp, tp, &ch, 0);
1981         for (ch = ' '; spaces > 0; --spaces)
1982                 (void)txt_insch(sp, tp, &ch, 0);
1983         return (0);
1984 }
1985
1986 /*
1987  * txt_fc --
1988  *      File name and ex command completion.
1989  */
1990 static int
1991 txt_fc(SCR *sp, TEXT *tp, int *redrawp)
1992 {
1993         struct stat sb;
1994         ARGS **argv;
1995         EXCMD cmd;
1996         size_t indx, len, nlen, off;
1997         int argc;
1998         CHAR_T *p, *t, *bp;
1999         char *np, *epd = NULL;
2000         size_t nplen;
2001         int fstwd = 1;
2002
2003         *redrawp = 0;
2004         ex_cinit(sp, &cmd, 0, 0, OOBLNO, OOBLNO, 0);
2005
2006         /*
2007          * Find the beginning of this "word" -- if we're at the beginning
2008          * of the line, it's a special case.
2009          */
2010         if (tp->cno == 1) {
2011                 len = 0;
2012                 p = tp->lb;
2013         } else {
2014                 CHAR_T *ap;
2015
2016                 for (len = 0,
2017                     off = MAX(tp->ai, tp->offset), ap = tp->lb + off, p = ap;
2018                     off < tp->cno; ++off, ++ap) {
2019                         if (IS_ESCAPE(sp, &cmd, *ap)) {
2020                                 if (++off == tp->cno)
2021                                         break;
2022                                 ++ap;
2023                                 len += 2;
2024                         } else if (cmdskip(*ap)) {
2025                                 p = ap + 1;
2026                                 if (len > 0)
2027                                         fstwd = 0;
2028                                 len = 0;
2029                         } else
2030                                 ++len;
2031                 }
2032         }
2033
2034         /*
2035          * If we are at the first word, do ex command completion instead of
2036          * file name completion.
2037          */
2038         if (fstwd)
2039                 (void)argv_flt_ex(sp, &cmd, p, len);
2040         else {
2041                 if ((bp = argv_uesc(sp, &cmd, p, len)) == NULL)
2042                         return (1);
2043                 if (argv_flt_path(sp, &cmd, bp, STRLEN(bp))) {
2044                         FREE_SPACEW(sp, bp, 0);
2045                         return (0);
2046                 }
2047                 FREE_SPACEW(sp, bp, 0);
2048         }
2049         argc = cmd.argc;
2050         argv = cmd.argv;
2051
2052         switch (argc) {
2053         case 0:                         /* No matches. */
2054                 (void)sp->gp->scr_bell(sp);
2055                 return (0);
2056         case 1:                         /* One match. */
2057                 /* Always overwrite the old text. */
2058                 nlen = STRLEN(cmd.argv[0]->bp);
2059                 break;
2060         default:                        /* Multiple matches. */
2061                 *redrawp = 1;
2062                 if (txt_fc_col(sp, argc, argv))
2063                         return (1);
2064
2065                 /* Find the length of the shortest match. */
2066                 for (nlen = cmd.argv[0]->len; --argc > 0;) {
2067                         if (cmd.argv[argc]->len < nlen)
2068                                 nlen = cmd.argv[argc]->len;
2069                         for (indx = 0; indx < nlen &&
2070                             cmd.argv[argc]->bp[indx] == cmd.argv[0]->bp[indx];
2071                             ++indx);
2072                         nlen = indx;
2073                 }
2074                 break;
2075         }
2076
2077         /* Escape the matched part of the path. */
2078         if (fstwd)
2079                 bp = cmd.argv[0]->bp;
2080         else {
2081                 if ((bp = argv_esc(sp, &cmd, cmd.argv[0]->bp, nlen)) == NULL)
2082                         return (1);
2083                 nlen = STRLEN(bp);
2084         }
2085
2086         /* Overwrite the expanded text first. */
2087         for (t = bp; len > 0 && nlen > 0; --len, --nlen)
2088                 *p++ = *t++;
2089
2090         /* If lost text, make the remaining old text overwrite characters. */
2091         if (len) {
2092                 tp->cno -= len;
2093                 tp->owrite += len;
2094         }
2095
2096         /* Overwrite any overwrite characters next. */
2097         for (; nlen > 0 && tp->owrite > 0; --nlen, --tp->owrite, ++tp->cno)
2098                 *p++ = *t++;
2099
2100         /* Shift remaining text up, and move the cursor to the end. */
2101         if (nlen) {
2102                 off = p - tp->lb;
2103                 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + nlen);
2104                 p = tp->lb + off;
2105
2106                 tp->cno += nlen;
2107                 tp->len += nlen;
2108
2109                 if (tp->insert != 0)
2110                         (void)MEMMOVE(p + nlen, p, tp->insert);
2111                 while (nlen--)
2112                         *p++ = *t++;
2113         }
2114
2115         if (!fstwd)
2116                 FREE_SPACEW(sp, bp, 0);
2117
2118         /* If not a single match of path, we've done. */
2119         if (argc != 1 || fstwd)
2120                 return (0);
2121
2122         /* If a single match and it's a directory, append a '/'. */
2123         INT2CHAR(sp, cmd.argv[0]->bp, cmd.argv[0]->len + 1, np, nplen);
2124         if ((epd = expanduser(np)) != NULL)
2125                 np = epd;
2126         if (!stat(np, &sb) && S_ISDIR(sb.st_mode)) {
2127                 if (tp->owrite == 0) {
2128                         off = p - tp->lb;
2129                         BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2130                         p = tp->lb + off;
2131                         if (tp->insert != 0)
2132                                 (void)MEMMOVE(p + 1, p, tp->insert);
2133                         ++tp->len;
2134                 } else
2135                         --tp->owrite;
2136
2137                 ++tp->cno;
2138                 *p++ = '/';
2139         }
2140         free(epd);
2141         return (0);
2142 }
2143
2144 /*
2145  * txt_fc_col --
2146  *      Display file names for file name completion.
2147  */
2148 static int
2149 txt_fc_col(SCR *sp, int argc, ARGS **argv)
2150 {
2151         ARGS **av;
2152         CHAR_T *p;
2153         GS *gp;
2154         size_t base, cnt, col, colwidth, numrows, numcols, prefix, row;
2155         int ac, nf, reset;
2156         char *np, *pp;
2157         size_t nlen;
2158
2159         gp = sp->gp;
2160
2161         /* Trim any directory prefix common to all of the files. */
2162         INT2CHAR(sp, argv[0]->bp, argv[0]->len + 1, np, nlen);
2163         if ((pp = strrchr(np, '/')) == NULL)
2164                 prefix = 0;
2165         else {
2166                 prefix = (pp - np) + 1;
2167                 for (ac = argc - 1, av = argv + 1; ac > 0; --ac, ++av)
2168                         if (av[0]->len < prefix ||
2169                             MEMCMP(av[0]->bp, argv[0]->bp, 
2170                                    prefix)) {
2171                                 prefix = 0;
2172                                 break;
2173                         }
2174         }
2175
2176         /*
2177          * Figure out the column width for the longest name.  Output is done on
2178          * 6 character "tab" boundaries for no particular reason.  (Since we
2179          * don't output tab characters, we ignore the terminal's tab settings.)
2180          * Ignore the user's tab setting because we have no idea how reasonable
2181          * it is.
2182          */
2183         for (ac = argc, av = argv, colwidth = 0; ac > 0; --ac, ++av) {
2184                 for (col = 0, p = av[0]->bp + prefix; *p != '\0'; ++p)
2185                         col += KEY_COL(sp, *p);
2186                 if (col > colwidth)
2187                         colwidth = col;
2188         }
2189         colwidth += COL_OFF(colwidth, 6);
2190
2191         /*
2192          * Writing to the bottom line of the screen is always turned off when
2193          * SC_TINPUT_INFO is set.  Turn it back on, we know what we're doing.
2194          */
2195         if (F_ISSET(sp, SC_TINPUT_INFO)) {
2196                 reset = 1;
2197                 F_CLR(sp, SC_TINPUT_INFO);
2198         } else
2199                 reset = 0;
2200
2201 #define CHK_INTR                                                        \
2202         if (F_ISSET(gp, G_INTERRUPTED))                                 \
2203                 goto intr;
2204
2205         /* If the largest file name is too large, just print them. */
2206         if (colwidth >= sp->cols) {
2207                 for (ac = argc, av = argv; ac > 0; --ac, ++av) {
2208                         INT2CHAR(sp, av[0]->bp+prefix, av[0]->len+1-prefix,
2209                                  np, nlen);
2210                         pp = msg_print(sp, np, &nf);
2211                         (void)ex_printf(sp, "%s\n", pp);
2212                         if (nf)
2213                                 FREE_SPACE(sp, pp, 0);
2214                         if (F_ISSET(gp, G_INTERRUPTED))
2215                                 break;
2216                 }
2217                 CHK_INTR;
2218         } else {
2219                 /* Figure out the number of columns. */
2220                 numcols = (sp->cols - 1) / colwidth;
2221                 if (argc > numcols) {
2222                         numrows = argc / numcols;
2223                         if (argc % numcols)
2224                                 ++numrows;
2225                 } else
2226                         numrows = 1;
2227
2228                 /* Display the files in sorted order. */
2229                 for (row = 0; row < numrows; ++row) {
2230                         for (base = row, col = 0; col < numcols; ++col) {
2231                                 INT2CHAR(sp, argv[base]->bp+prefix, 
2232                                         argv[base]->len+1-prefix, np, nlen);
2233                                 pp = msg_print(sp, np, &nf);
2234                                 cnt = ex_printf(sp, "%s", pp);
2235                                 if (nf)
2236                                         FREE_SPACE(sp, pp, 0);
2237                                 CHK_INTR;
2238                                 if ((base += numrows) >= argc)
2239                                         break;
2240                                 (void)ex_printf(sp,
2241                                     "%*s", (int)(colwidth - cnt), "");
2242                                 CHK_INTR;
2243                         }
2244                         (void)ex_puts(sp, "\n");
2245                         CHK_INTR;
2246                 }
2247                 (void)ex_puts(sp, "\n");
2248                 CHK_INTR;
2249         }
2250         (void)ex_fflush(sp);
2251
2252         if (0) {
2253 intr:           F_CLR(gp, G_INTERRUPTED);
2254         }
2255         if (reset)
2256                 F_SET(sp, SC_TINPUT_INFO);
2257
2258         return (0);
2259 }
2260
2261 /*
2262  * txt_emark --
2263  *      Set the end mark on the line.
2264  */
2265 static int
2266 txt_emark(SCR *sp, TEXT *tp, size_t cno)
2267 {
2268         CHAR_T ch;
2269         u_char *kp;
2270         size_t chlen, nlen, olen;
2271         CHAR_T *p;
2272
2273         ch = CH_ENDMARK;
2274
2275         /*
2276          * The end mark may not be the same size as the current character.
2277          * Don't let the line shift.
2278          */
2279         nlen = KEY_COL(sp, ch);
2280         if (tp->lb[cno] == '\t')
2281                 (void)vs_columns(sp, tp->lb, tp->lno, &cno, &olen);
2282         else
2283                 olen = KEY_COL(sp, tp->lb[cno]);
2284
2285         /*
2286          * If the line got longer, well, it's weird, but it's easy.  If
2287          * it's the same length, it's easy.  If it got shorter, we have
2288          * to fix it up.
2289          */
2290         if (olen > nlen) {
2291                 BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + olen);
2292                 chlen = olen - nlen;
2293                 if (tp->insert != 0)
2294                         MEMMOVE(tp->lb + cno + 1 + chlen,
2295                             tp->lb + cno + 1, tp->insert);
2296
2297                 tp->len += chlen;
2298                 tp->owrite += chlen;
2299                 p = tp->lb + cno;
2300                 if (tp->lb[cno] == '\t' ||
2301                     KEY_NEEDSWIDE(sp, tp->lb[cno]))
2302                         for (cno += chlen; chlen--;)
2303                                 *p++ = ' ';
2304                 else
2305                         for (kp = (u_char *)
2306                             KEY_NAME(sp, tp->lb[cno]),
2307                             cno += chlen; chlen--;)
2308                                 *p++ = *kp++;
2309         }
2310         tp->lb[cno] = ch;
2311         return (vs_change(sp, tp->lno, LINE_RESET));
2312 }
2313
2314 /*
2315  * txt_err --
2316  *      Handle an error during input processing.
2317  */
2318 static void
2319 txt_err(SCR *sp, TEXTH *tiqh)
2320 {
2321         recno_t lno;
2322
2323         /*
2324          * The problem with input processing is that the cursor is at an
2325          * indeterminate position since some input may have been lost due
2326          * to a malloc error.  So, try to go back to the place from which
2327          * the cursor started, knowing that it may no longer be available.
2328          *
2329          * We depend on at least one line number being set in the text
2330          * chain.
2331          */
2332         for (lno = TAILQ_FIRST(tiqh)->lno;
2333             !db_exist(sp, lno) && lno > 0; --lno);
2334
2335         sp->lno = lno == 0 ? 1 : lno;
2336         sp->cno = 0;
2337
2338         /* Redraw the screen, just in case. */
2339         F_SET(sp, SC_SCR_REDRAW);
2340 }
2341
2342 /*
2343  * txt_hex --
2344  *      Let the user insert any character value they want.
2345  *
2346  * !!!
2347  * This is an extension.  The pattern "^X[0-9a-fA-F]*" is a way
2348  * for the user to specify a character value which their keyboard
2349  * may not be able to enter.
2350  */
2351 static int
2352 txt_hex(SCR *sp, TEXT *tp)
2353 {
2354         CHAR_T savec;
2355         size_t len, off;
2356         u_long value;
2357         CHAR_T *p, *wp;
2358
2359         /*
2360          * Null-terminate the string.  Since nul isn't a legal hex value,
2361          * this should be okay, and lets us use a local routine, which
2362          * presumably understands the character set, to convert the value.
2363          */
2364         savec = tp->lb[tp->cno];
2365         tp->lb[tp->cno] = 0;
2366
2367         /* Find the previous CH_HEX character. */
2368         for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --p, --off, ++len) {
2369                 if (*p == CH_HEX) {
2370                         wp = p + 1;
2371                         break;
2372                 }
2373                 /* Not on this line?  Shouldn't happen. */
2374                 if (off == tp->ai || off == tp->offset)
2375                         goto nothex;
2376         }
2377
2378         /* If length of 0, then it wasn't a hex value. */
2379         if (len == 0)
2380                 goto nothex;
2381
2382         /* Get the value. */
2383         errno = 0;
2384         value = STRTOL(wp, NULL, 16);
2385         if (errno || value > UCHAR_MAX) {
2386 nothex:         tp->lb[tp->cno] = savec;
2387                 return (0);
2388         }
2389
2390         /* Restore the original character. */
2391         tp->lb[tp->cno] = savec;
2392
2393         /* Adjust the bookkeeping. */
2394         tp->cno -= len;
2395         tp->len -= len;
2396         tp->lb[tp->cno - 1] = value;
2397
2398         /* Copy down any overwrite characters. */
2399         if (tp->owrite)
2400                 MEMMOVE(tp->lb + tp->cno, tp->lb + tp->cno + len, 
2401                     tp->owrite);
2402
2403         /* Copy down any insert characters. */
2404         if (tp->insert)
2405                 MEMMOVE(tp->lb + tp->cno + tp->owrite,
2406                     tp->lb + tp->cno + tp->owrite + len, 
2407                     tp->insert);
2408
2409         return (0);
2410 }
2411
2412 /*
2413  * txt_insch --
2414  *
2415  * !!!
2416  * Historic vi did a special screen optimization for tab characters.  As an
2417  * example, for the keystrokes "iabcd<esc>0C<tab>", the tab overwrote the
2418  * rest of the string when it was displayed.
2419  *
2420  * Because early versions of this implementation redisplayed the entire line
2421  * on each keystroke, the "bcd" was pushed to the right as it ignored that
2422  * the user had "promised" to change the rest of the characters.  However,
2423  * the historic vi implementation had an even worse bug: given the keystrokes
2424  * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
2425  * on the second <esc> key.
2426  *
2427  * POSIX 1003.2 requires (will require) that this be fixed, specifying that
2428  * vi overwrite characters the user has committed to changing, on the basis
2429  * of the screen space they require, but that it not overwrite other characters.
2430  */
2431 static int
2432 txt_insch(SCR *sp, TEXT *tp, CHAR_T *chp, u_int flags)
2433 {
2434         u_char *kp;
2435         CHAR_T savech;
2436         size_t chlen, cno, copydown, olen, nlen;
2437         CHAR_T *p;
2438
2439         /*
2440          * The 'R' command does one-for-one replacement, because there's
2441          * no way to know how many characters the user intends to replace.
2442          */
2443         if (LF_ISSET(TXT_REPLACE)) {
2444                 if (tp->owrite) {
2445                         --tp->owrite;
2446                         tp->lb[tp->cno++] = *chp;
2447                         return (0);
2448                 }
2449         } else if (tp->owrite) {                /* Overwrite a character. */
2450                 cno = tp->cno;
2451
2452                 /*
2453                  * If the old or new characters are tabs, then the length of the
2454                  * display depends on the character position in the display.  We
2455                  * don't even try to handle this here, just ask the screen.
2456                  */
2457                 if (*chp == '\t') {
2458                         savech = tp->lb[cno];
2459                         tp->lb[cno] = '\t';
2460                         (void)vs_columns(sp, tp->lb, tp->lno, &cno, &nlen);
2461                         tp->lb[cno] = savech;
2462                 } else
2463                         nlen = KEY_COL(sp, *chp);
2464
2465                 /*
2466                  * Eat overwrite characters until we run out of them or we've
2467                  * handled the length of the new character.  If we only eat
2468                  * part of an overwrite character, break it into its component
2469                  * elements and display the remaining components.
2470                  */
2471                 for (copydown = 0; nlen != 0 && tp->owrite != 0;) {
2472                         --tp->owrite;
2473
2474                         if (tp->lb[cno] == '\t')
2475                                 (void)vs_columns(sp,
2476                                     tp->lb, tp->lno, &cno, &olen);
2477                         else
2478                                 olen = KEY_COL(sp, tp->lb[cno]);
2479
2480                         if (olen == nlen) {
2481                                 nlen = 0;
2482                                 break;
2483                         }
2484                         if (olen < nlen) {
2485                                 ++copydown;
2486                                 nlen -= olen;
2487                         } else {
2488                                 BINC_RETW(sp,
2489                                     tp->lb, tp->lb_len, tp->len + olen);
2490                                 chlen = olen - nlen;
2491                                 MEMMOVE(tp->lb + cno + 1 + chlen,
2492                                     tp->lb + cno + 1, 
2493                                     tp->owrite + tp->insert);
2494
2495                                 tp->len += chlen;
2496                                 tp->owrite += chlen;
2497                                 if (tp->lb[cno] == '\t' ||
2498                                    KEY_NEEDSWIDE(sp, tp->lb[cno]))
2499                                         for (p = tp->lb + cno + 1; chlen--;)
2500                                                 *p++ = ' ';
2501                                 else
2502                                         for (kp = (u_char *)
2503                                             KEY_NAME(sp, tp->lb[cno]) + nlen,
2504                                             p = tp->lb + cno + 1; chlen--;)
2505                                                 *p++ = *kp++;
2506                                 nlen = 0;
2507                                 break;
2508                         }
2509                 }
2510
2511                 /*
2512                  * If had to erase several characters, we adjust the total
2513                  * count, and if there are any characters left, shift them
2514                  * into position.
2515                  */
2516                 if (copydown != 0 && (tp->len -= copydown) != 0)
2517                         MEMMOVE(tp->lb + cno, tp->lb + cno + copydown,
2518                             tp->owrite + tp->insert + copydown);
2519
2520                 /* If we had enough overwrite characters, we're done. */
2521                 if (nlen == 0) {
2522                         tp->lb[tp->cno++] = *chp;
2523                         return (0);
2524                 }
2525         }
2526
2527         /* Check to see if the character fits into the input buffer. */
2528         BINC_RETW(sp, tp->lb, tp->lb_len, tp->len + 1);
2529
2530         ++tp->len;
2531         if (tp->insert) {                       /* Insert a character. */
2532                 if (tp->insert == 1)
2533                         tp->lb[tp->cno + 1] = tp->lb[tp->cno];
2534                 else
2535                         MEMMOVE(tp->lb + tp->cno + 1,
2536                             tp->lb + tp->cno, tp->owrite + tp->insert);
2537         }
2538         tp->lb[tp->cno++] = *chp;
2539         return (0);
2540 }
2541
2542 /*
2543  * txt_isrch --
2544  *      Do an incremental search.
2545  */
2546 static int
2547 txt_isrch(SCR *sp, VICMD *vp, TEXT *tp, u_int8_t *is_flagsp)
2548 {
2549         MARK start;
2550         recno_t lno;
2551         u_int sf;
2552
2553         /* If it's a one-line screen, we don't do incrementals. */
2554         if (IS_ONELINE(sp)) {
2555                 FL_CLR(*is_flagsp, IS_RUNNING);
2556                 return (0);
2557         }
2558
2559         /*
2560          * If the user erases back to the beginning of the buffer, there's
2561          * nothing to search for.  Reset the cursor to the starting point.
2562          */
2563         if (tp->cno <= 1) {
2564                 vp->m_final = vp->m_start;
2565                 return (0);
2566         }
2567
2568         /*
2569          * If it's an RE quote character, and not quoted, ignore it until
2570          * we get another character.
2571          */
2572         if (tp->lb[tp->cno - 1] == '\\' &&
2573             (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2574                 return (0);
2575
2576         /*
2577          * If it's a magic shell character, and not quoted, reset the cursor
2578          * to the starting point.
2579          */
2580         if (IS_SHELLMETA(sp, tp->lb[tp->cno - 1]) &&
2581             (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\'))
2582                 vp->m_final = vp->m_start;
2583
2584         /*
2585          * If we see the search pattern termination character, then quit doing
2586          * an incremental search.  There may be more, e.g., ":/foo/;/bar/",
2587          * and we can't handle that incrementally.  Also, reset the cursor to
2588          * the original location, the ex search routines don't know anything
2589          * about incremental searches.
2590          */
2591         if (tp->lb[0] == tp->lb[tp->cno - 1] &&
2592             (tp->cno == 2 || tp->lb[tp->cno - 2] != '\\')) {
2593                 vp->m_final = vp->m_start;
2594                 FL_CLR(*is_flagsp, IS_RUNNING);
2595                 return (0);
2596         }
2597                 
2598         /*
2599          * Remember the input line and discard the special input map,
2600          * but don't overwrite the input line on the screen.
2601          */
2602         lno = tp->lno;
2603         F_SET(VIP(sp), VIP_S_MODELINE);
2604         F_CLR(sp, SC_TINPUT | SC_TINPUT_INFO);
2605         if (txt_map_end(sp))
2606                 return (1);
2607
2608         /*
2609          * Specify a starting point and search.  If we find a match, move to
2610          * it and refresh the screen.  If we didn't find the match, then we
2611          * beep the screen.  When searching from the original cursor position, 
2612          * we have to move the cursor, otherwise, we don't want to move the
2613          * cursor in case the text at the current position continues to match.
2614          */
2615         if (FL_ISSET(*is_flagsp, IS_RESTART)) {
2616                 start = vp->m_start;
2617                 sf = SEARCH_SET;
2618         } else {
2619                 start = vp->m_final;
2620                 sf = SEARCH_INCR | SEARCH_SET;
2621         }
2622
2623         if (tp->lb[0] == '/' ?
2624             !f_search(sp,
2625             &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf) :
2626             !b_search(sp,
2627             &start, &vp->m_final, tp->lb + 1, tp->cno - 1, NULL, sf)) {
2628                 sp->lno = vp->m_final.lno;
2629                 sp->cno = vp->m_final.cno;
2630                 FL_CLR(*is_flagsp, IS_RESTART);
2631
2632                 if (!KEYS_WAITING(sp) && vs_refresh(sp, 0))
2633                         return (1);
2634         } else
2635                 FL_SET(*is_flagsp, IS_RESTART);
2636
2637         /* Reinstantiate the special input map. */
2638         if (txt_map_init(sp))
2639                 return (1);
2640         F_CLR(VIP(sp), VIP_S_MODELINE);
2641         F_SET(sp, SC_TINPUT | SC_TINPUT_INFO);
2642
2643         /* Reset the line number of the input line. */
2644         tp->lno = TMAP[0].lno; 
2645
2646         /*
2647          * If the colon command-line moved, i.e. the screen scrolled,
2648          * refresh the input line.
2649          *
2650          * XXX
2651          * We shouldn't be calling vs_line, here -- we need dirty bits
2652          * on entries in the SMAP array.
2653          */
2654         if (lno != TMAP[0].lno) {
2655                 if (vs_line(sp, &TMAP[0], NULL, NULL))
2656                         return (1);
2657                 (void)sp->gp->scr_refresh(sp, 0);
2658         }
2659         return (0);
2660 }
2661
2662 /*
2663  * txt_resolve --
2664  *      Resolve the input text chain into the file.
2665  */
2666 static int
2667 txt_resolve(SCR *sp, TEXTH *tiqh, u_int32_t flags)
2668 {
2669         VI_PRIVATE *vip;
2670         TEXT *tp;
2671         recno_t lno;
2672         int changed;
2673
2674         /*
2675          * The first line replaces a current line, and all subsequent lines
2676          * are appended into the file.  Resolve autoindented characters for
2677          * each line before committing it.  If the latter causes the line to
2678          * change, we have to redisplay it, otherwise the information cached
2679          * about the line will be wrong.
2680          */
2681         vip = VIP(sp);
2682         tp = TAILQ_FIRST(tiqh);
2683
2684         if (LF_ISSET(TXT_AUTOINDENT))
2685                 txt_ai_resolve(sp, tp, &changed);
2686         else
2687                 changed = 0;
2688         if (db_set(sp, tp->lno, tp->lb, tp->len) ||
2689             (changed && vs_change(sp, tp->lno, LINE_RESET)))
2690                 return (1);
2691
2692         for (lno = tp->lno; (tp = TAILQ_NEXT(tp, q)) != NULL; ++lno) {
2693                 if (LF_ISSET(TXT_AUTOINDENT))
2694                         txt_ai_resolve(sp, tp, &changed);
2695                 else
2696                         changed = 0;
2697                 if (db_append(sp, 0, lno, tp->lb, tp->len) ||
2698                     (changed && vs_change(sp, tp->lno, LINE_RESET)))
2699                         return (1);
2700         }
2701
2702         /*
2703          * Clear the input flag, the look-aside buffer is no longer valid.
2704          * Has to be done as part of text resolution, or upon return we'll
2705          * be looking at incorrect data.
2706          */
2707         F_CLR(sp, SC_TINPUT);
2708
2709         return (0);
2710 }
2711
2712 /*
2713  * txt_showmatch --
2714  *      Show a character match.
2715  *
2716  * !!!
2717  * Historic vi tried to display matches even in the :colon command line.
2718  * I think not.
2719  */
2720 static int
2721 txt_showmatch(SCR *sp, TEXT *tp)
2722 {
2723         GS *gp;
2724         VCS cs;
2725         MARK m;
2726         int cnt, endc, startc;
2727
2728         gp = sp->gp;
2729
2730         /*
2731          * Do a refresh first, in case we haven't done one in awhile,
2732          * so the user can see what we're complaining about.
2733          */
2734         UPDATE_POSITION(sp, tp);
2735         if (vs_refresh(sp, 1))
2736                 return (1);
2737
2738         /*
2739          * We don't display the match if it's not on the screen.  Find
2740          * out what the first character on the screen is.
2741          */
2742         if (vs_sm_position(sp, &m, 0, P_TOP))
2743                 return (1);
2744
2745         /* Initialize the getc() interface. */
2746         cs.cs_lno = tp->lno;
2747         cs.cs_cno = tp->cno - 1;
2748         if (cs_init(sp, &cs))
2749                 return (1);
2750         startc = STRCHR(VIP(sp)->mcs, endc = cs.cs_ch)[-1];
2751
2752         /* Search for the match. */
2753         for (cnt = 1;;) {
2754                 if (cs_prev(sp, &cs))
2755                         return (1);
2756                 if (cs.cs_flags != 0) {
2757                         if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF) {
2758                                 msgq(sp, M_BERR,
2759                                     "Unmatched %s", KEY_NAME(sp, endc));
2760                                 return (0);
2761                         }
2762                         continue;
2763                 }
2764                 if (cs.cs_ch == endc)
2765                         ++cnt;
2766                 else if (cs.cs_ch == startc && --cnt == 0)
2767                         break;
2768         }
2769
2770         /* If the match is on the screen, move to it. */
2771         if (cs.cs_lno < m.lno || (cs.cs_lno == m.lno && cs.cs_cno < m.cno))
2772                 return (0);
2773         sp->lno = cs.cs_lno;
2774         sp->cno = cs.cs_cno;
2775         if (vs_refresh(sp, 1))
2776                 return (1);
2777
2778         /* Wait for timeout or character arrival. */
2779         return (v_event_get(sp,
2780             NULL, O_VAL(sp, O_MATCHTIME) * 100, EC_TIMEOUT));
2781 }
2782
2783 /*
2784  * txt_margin --
2785  *      Handle margin wrap.
2786  */
2787 static int
2788 txt_margin(SCR *sp, TEXT *tp, TEXT *wmtp, int *didbreak, u_int32_t flags)
2789 {
2790         VI_PRIVATE *vip;
2791         size_t len, off;
2792         CHAR_T *p, *wp;
2793
2794         /* Find the nearest previous blank. */
2795         for (off = tp->cno - 1, p = tp->lb + off, len = 0;; --off, --p, ++len) {
2796                 if (isblank(*p)) {
2797                         wp = p + 1;
2798                         break;
2799                 }
2800
2801                 /*
2802                  * If reach the start of the line, there's nowhere to break.
2803                  *
2804                  * !!!
2805                  * Historic vi belled each time a character was entered after
2806                  * crossing the margin until a space was entered which could
2807                  * be used to break the line.  I don't as it tends to wake the
2808                  * cats.
2809                  */
2810                 if (off == tp->ai || off == tp->offset) {
2811                         *didbreak = 0;
2812                         return (0);
2813                 }
2814         }
2815
2816         /*
2817          * Store saved information about the rest of the line in the
2818          * wrapmargin TEXT structure.
2819          *
2820          * !!!
2821          * The offset field holds the length of the current characters
2822          * that the user entered, but which are getting split to the new
2823          * line -- it's going to be used to set the cursor value when we
2824          * move to the new line.
2825          */
2826         vip = VIP(sp);
2827         wmtp->lb = p + 1;
2828         wmtp->offset = len;
2829         wmtp->insert = LF_ISSET(TXT_APPENDEOL) ?  tp->insert - 1 : tp->insert;
2830         wmtp->owrite = tp->owrite;
2831
2832         /* Correct current bookkeeping information. */
2833         tp->cno -= len;
2834         if (LF_ISSET(TXT_APPENDEOL)) {
2835                 tp->len -= len + tp->owrite + (tp->insert - 1);
2836                 tp->insert = 1;
2837         } else {
2838                 tp->len -= len + tp->owrite + tp->insert;
2839                 tp->insert = 0;
2840         }
2841         tp->owrite = 0;
2842
2843         /*
2844          * !!!
2845          * Delete any trailing whitespace from the current line.
2846          */
2847         for (;; --p, --off) {
2848                 if (!isblank(*p))
2849                         break;
2850                 --tp->cno;
2851                 --tp->len;
2852                 if (off == tp->ai || off == tp->offset)
2853                         break;
2854         }
2855         *didbreak = 1;
2856         return (0);
2857 }
2858
2859 /*
2860  * txt_Rresolve --
2861  *      Resolve the input line for the 'R' command.
2862  */
2863 static void
2864 txt_Rresolve(SCR *sp, TEXTH *tiqh, TEXT *tp, const size_t orig_len)
2865 {
2866         TEXT *ttp;
2867         size_t input_len, retain;
2868         CHAR_T *p;
2869
2870         /*
2871          * Check to make sure that the cursor hasn't moved beyond
2872          * the end of the line.
2873          */
2874         if (tp->owrite == 0)
2875                 return;
2876
2877         /*
2878          * Calculate how many characters the user has entered,
2879          * plus the blanks erased by <carriage-return>/<newline>s.
2880          */
2881         for (ttp = TAILQ_FIRST(tiqh), input_len = 0;;) {
2882                 input_len += ttp == tp ? tp->cno : ttp->len + ttp->R_erase;
2883                 if ((ttp = TAILQ_NEXT(ttp, q)) == NULL)
2884                         break;
2885         }
2886
2887         /*
2888          * If the user has entered less characters than the original line
2889          * was long, restore any overwriteable characters to the original
2890          * characters.  These characters are entered as "insert characters",
2891          * because they're after the cursor and we don't want to lose them.
2892          * (This is okay because the R command has no insert characters.)
2893          * We set owrite to 0 so that the insert characters don't get copied
2894          * to somewhere else, which means that the line and the length have
2895          * to be adjusted here as well.
2896          *
2897          * We have to retrieve the original line because the original pinned
2898          * page has long since been discarded.  If it doesn't exist, that's
2899          * okay, the user just extended the file.
2900          */
2901         if (input_len < orig_len) {
2902                 retain = MIN(tp->owrite, orig_len - input_len);
2903                 if (db_get(sp,
2904                     TAILQ_FIRST(tiqh)->lno, DBG_FATAL | DBG_NOCACHE, &p, NULL))
2905                         return;
2906                 MEMCPY(tp->lb + tp->cno, p + input_len, retain);
2907                 tp->len -= tp->owrite - retain;
2908                 tp->owrite = 0;
2909                 tp->insert += retain;
2910         }
2911 }
2912
2913 /*
2914  * txt_nomorech --
2915  *      No more characters message.
2916  */
2917 static void
2918 txt_nomorech(SCR *sp)
2919 {
2920         msgq(sp, M_BERR, "194|No more characters to erase");
2921 }