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