add TXT_INFOLINE, don't push a \r if editing the colon line
[nvi.git] / vi / v_txt.c
blob70f2930cb608beb712a605c2a2b12927a5e16e69
1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: v_txt.c,v 8.65 1993/12/19 19:00:32 bostic Exp $ (Berkeley) $Date: 1993/12/19 19:00:32 $";
10 #endif /* not lint */
12 #include <sys/types.h>
13 #include <sys/time.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "vi.h"
22 #include "seq.h"
23 #include "vcmd.h"
25 static int txt_abbrev __P((SCR *, TEXT *, int *, ARG_CHAR_T));
26 static void txt_ai_resolve __P((SCR *, TEXT *));
27 static TEXT *txt_backup __P((SCR *, EXF *, TEXTH *, TEXT *, u_int));
28 static void txt_err __P((SCR *, EXF *, TEXTH *));
29 static int txt_hex __P((SCR *, TEXT *, int *, ARG_CHAR_T));
30 static int txt_indent __P((SCR *, TEXT *));
31 static int txt_margin __P((SCR *, TEXT *, int *, ARG_CHAR_T));
32 static int txt_outdent __P((SCR *, TEXT *));
33 static void txt_showmatch __P((SCR *, EXF *));
34 static int txt_resolve __P((SCR *, EXF *, TEXTH *));
36 /* Cursor character (space is hard to track on the screen). */
37 #if defined(DEBUG) && 0
38 #undef CURSOR_CH
39 #define CURSOR_CH '+'
40 #endif
42 /* Local version of BINC. */
43 #define TBINC(sp, lp, llen, nlen) { \
44 if ((nlen) > llen && binc(sp, &(lp), &(llen), nlen)) \
45 goto err; \
49 * newtext --
50 * Read in text from the user.
52 * !!!
53 * Historic vi always used:
55 * ^D: autoindent deletion
56 * ^H: last character deletion
57 * ^W: last word deletion
58 * ^V: quote the next character
60 * regardless of the user's choices for these characters. The user's erase
61 * and kill characters worked in addition to these characters. Ex was not
62 * completely consistent with this, as it did map the scroll command to the
63 * user's EOF character.
65 * This implementation does not use fixed characters, but uses whatever the
66 * user specified as described by the termios structure. I'm getting away
67 * with something here, but I think I'm unlikely to get caught.
69 * !!!
70 * Historic vi did a special screen optimization for tab characters. For
71 * the keystrokes "iabcd<esc>0C<tab>", the tab would overwrite the rest of
72 * the string when it was displayed. Because this implementation redisplays
73 * the entire line on each keystroke, the "bcd" gets pushed to the right as
74 * we ignore that the user has "promised" to change the rest of the characters.
75 * Users have noticed, but this isn't worth fixing, and, the way that the
76 * historic vi did it results in an even worse bug. Given the keystrokes
77 * "iabcd<esc>0R<tab><esc>", the "bcd" disappears, and magically reappears
78 * on the second <esc> key.
80 int
81 v_ntext(sp, ep, tiqh, tm, lp, len, rp, prompt, ai_line, flags)
82 SCR *sp;
83 EXF *ep;
84 TEXTH *tiqh;
85 MARK *tm; /* To MARK. */
86 const char *lp; /* Input line. */
87 const size_t len; /* Input line length. */
88 MARK *rp; /* Return MARK. */
89 int prompt; /* Prompt to display. */
90 recno_t ai_line; /* Line number to use for autoindent count. */
91 u_int flags; /* TXT_ flags. */
93 /* State of abbreviation checks. */
94 enum { A_NOTSET, A_SPACE, A_NOTSPACE } abb;
95 /* State of the "[^0]^D" sequences. */
96 enum { C_NOTSET, C_CARATSET, C_NOCHANGE, C_ZEROSET } carat_st;
97 /* State of the hex input character. */
98 enum { H_NOTSET, H_NEXTCHAR, H_INHEX } hex;
99 /* State of quotation. */
100 enum { Q_NOTSET, Q_NEXTCHAR, Q_THISCHAR } quoted;
101 CH ikey; /* Input character structure. */
102 CHAR_T ch; /* Input character. */
103 GS *gp; /* Global pointer. */
104 TEXT *tp, *ntp, ait; /* Input and autoindent text structures. */
105 size_t rcol; /* 0-N: insert offset in the replay buffer. */
106 size_t col; /* Current column. */
107 u_long margin; /* Wrapmargin value. */
108 u_int iflags; /* Input flags. */
109 int ab_cnt; /* Abbreviation count. */
110 int eval; /* Routine return value. */
111 int replay; /* If replaying a set of input. */
112 int showmatch; /* Showmatch set on this character. */
113 int testnr; /* Test first character for nul replay. */
114 int max, tmp;
115 char *p;
118 * Set the input flag, so tabs get displayed correctly
119 * and everyone knows that the text buffer is in use.
121 F_SET(sp, S_INPUT);
123 /* Set return value. */
124 eval = 0;
127 * Get one TEXT structure with some initial buffer space, reusing
128 * the last one if it's big enough. (All TEXT bookkeeping fields
129 * default to 0 -- text_init() handles this.) If changing a line,
130 * copy it into the TEXT buffer.
132 if (tiqh->cqh_first != (void *)tiqh) {
133 tp = tiqh->cqh_first;
134 if (tp->q.cqe_next != (void *)tiqh || tp->lb_len < len + 32) {
135 text_lfree(tiqh);
136 goto newtp;
138 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
139 if (lp != NULL) {
140 tp->len = len;
141 memmove(tp->lb, lp, len);
142 } else
143 tp->len = 0;
144 } else {
145 newtp: if ((tp = text_init(sp, lp, len, len + 32)) == NULL)
146 return (1);
147 CIRCLEQ_INSERT_HEAD(tiqh, tp, q);
150 /* Set the starting line number. */
151 tp->lno = sp->lno;
154 * Set the insert and overwrite counts. If overwriting characters,
155 * do insertion afterward. If not overwriting characters, assume
156 * doing insertion. If change is to a mark, emphasize it with an
157 * END_CH.
159 if (len) {
160 if (LF_ISSET(TXT_OVERWRITE)) {
161 tp->owrite = tm->cno - sp->cno;
162 tp->insert = len - tm->cno;
163 } else
164 tp->insert = len - sp->cno;
166 if (LF_ISSET(TXT_EMARK))
167 tp->lb[tm->cno - 1] = END_CH;
171 * Many of the special cases in this routine are to handle autoindent
172 * support. Somebody decided that it would be a good idea if "^^D"
173 * and "0^D" deleted all of the autoindented characters. In an editor
174 * that takes single character input from the user, this wasn't a very
175 * good idea. Note also that "^^D" resets the next lines' autoindent,
176 * but "0^D" doesn't.
178 * We assume that autoindent only happens on empty lines, so insert
179 * and overwrite will be zero. If doing autoindent, figure out how
180 * much indentation we need and fill it in. Update input column and
181 * screen cursor as necessary.
183 if (LF_ISSET(TXT_AUTOINDENT) && ai_line != OOBLNO) {
184 if (txt_auto(sp, ep, ai_line, NULL, 0, tp))
185 return (1);
186 sp->cno = tp->ai;
187 } else {
189 * The cc and S commands have a special feature -- leading
190 * <blank> characters are handled as autoindent characters.
191 * Beauty!
193 if (LF_ISSET(TXT_AICHARS)) {
194 tp->offset = 0;
195 tp->ai = sp->cno;
196 } else
197 tp->offset = sp->cno;
200 /* If getting a command buffer from the user, there may be a prompt. */
201 if (LF_ISSET(TXT_PROMPT)) {
202 tp->lb[sp->cno++] = prompt;
203 ++tp->len;
204 ++tp->offset;
208 * If appending after the end-of-line, add a space into the buffer
209 * and move the cursor right. This space is inserted, i.e. pushed
210 * along, and then deleted when the line is resolved. Assumes that
211 * the cursor is already positioned at the end of the line. This
212 * avoids the nastiness of having the cursor reside on a magical
213 * column, i.e. a column that doesn't really exist. The only down
214 * side is that we may wrap lines or scroll the screen before it's
215 * strictly necessary. Not a big deal.
217 if (LF_ISSET(TXT_APPENDEOL)) {
218 tp->lb[sp->cno] = CURSOR_CH;
219 ++tp->len;
220 ++tp->insert;
224 * Historic practice is that the wrapmargin value was a distance
225 * from the RIGHT-HAND column, not the left. It's more useful to
226 * us as a distance from the left-hand column.
228 * XXX
229 * Setting margin causes a significant performance hit. Normally
230 * we don't update the screen if there are keys waiting, but we
231 * have to if margin is set, otherwise the screen routines don't
232 * know where the cursor is.
234 if (!LF_ISSET(TXT_WRAPMARGIN))
235 margin = 0;
236 else if ((margin = O_VAL(sp, O_WRAPMARGIN)) != 0)
237 margin = sp->cols - margin;
239 /* Initialize abbreviations checks. */
240 if (F_ISSET(sp, S_ABBREV) && LF_ISSET(TXT_MAPINPUT)) {
241 abb = A_NOTSPACE;
242 ab_cnt = 0;
243 } else
244 abb = A_NOTSET;
247 * Set up the dot command. Dot commands are done by saving the
248 * actual characters and replaying the input. We have to push
249 * the characters onto the key stack and then handle them normally,
250 * otherwise things like wrapmargin will fail.
252 * XXX
253 * It would be nice if we could swallow backspaces and such, but
254 * it's not all that easy to do. Another possibility would be to
255 * recognize full line insertions, which could be performed quickly,
256 * without replay.
258 nullreplay:
259 rcol = 0;
260 if (replay = LF_ISSET(TXT_REPLAY)) {
262 * !!!
263 * Historically, it wasn't an error to replay non-existent
264 * input. This test is necessary, we get here by the user
265 * doing an input command followed by a nul.
267 * !!!
268 * Historically, vi did not remap or reabbreviate replayed
269 * input. It did, however, beep at you if you changed an
270 * abbreviation and then replayed the input. We're not that
271 * compatible.
273 if (VIP(sp)->rep == NULL)
274 return (0);
275 if (term_push(sp, VIP(sp)->rep, VIP(sp)->rep_cnt, 0, CH_NOMAP))
276 return (1);
277 testnr = 0;
278 abb = A_NOTSET;
279 LF_CLR(TXT_RECORD);
280 } else
281 testnr = 1;
283 iflags = LF_ISSET(TXT_MAPCOMMAND | TXT_MAPINPUT);
284 for (gp = sp->gp, showmatch = 0,
285 carat_st = C_NOTSET, hex = H_NOTSET, quoted = Q_NOTSET;;) {
287 * Reset the line and update the screen. (The txt_showmatch()
288 * code refreshes the screen for us.) Don't refresh unless
289 * we're about to wait on a character or we need to know where
290 * the cursor really is.
292 if (showmatch || margin || !KEYS_WAITING(sp)) {
293 if (sp->s_change(sp, ep, tp->lno, LINE_RESET))
294 goto err;
295 if (showmatch) {
296 showmatch = 0;
297 txt_showmatch(sp, ep);
298 } else if (sp->s_refresh(sp, ep))
299 goto err;
302 /* Get the next character. */
303 next_ch: if (term_key(sp, &ikey, iflags) != INP_OK)
304 goto err;
305 ch = ikey.ch;
307 /* Abbreviation check. See comment in txt_abbrev(). */
308 #define MAX_ABBREVIATION_EXPANSION 256
309 if (ikey.flags & CH_ABBREVIATED) {
310 if (++ab_cnt > MAX_ABBREVIATION_EXPANSION) {
311 term_ab_flush(sp,
312 "Abbreviation exceeded maximum number of characters");
313 ab_cnt = 0;
314 continue;
316 } else
317 ab_cnt = 0;
320 * !!!
321 * Historic feature. If the first character of the input is
322 * a nul, replay the previous input. This isn't documented
323 * anywhere, and is a great test of vi clones.
325 if (ch == '\0' && testnr) {
326 LF_SET(TXT_REPLAY);
327 goto nullreplay;
329 testnr = 0;
332 * Check to see if the character fits into the input (and
333 * replay, if necessary) buffers. It isn't necessary to
334 * have tp->len bytes, since it doesn't consider overwrite
335 * characters, but not worth fixing.
337 if (LF_ISSET(TXT_RECORD)) {
338 TBINC(sp, VIP(sp)->rep, VIP(sp)->rep_len, rcol + 1);
339 VIP(sp)->rep[rcol++] = ch;
341 TBINC(sp, tp->lb, tp->lb_len, tp->len + 1);
344 * If the character was quoted, replace the last character
345 * (the literal mark) with the new character. If quoted
346 * by someone else, simply insert the character.
348 * !!!
349 * Extension -- if the quoted character is HEX_CH, enter hex
350 * mode. If the user enters "<HEX_CH>[isxdigit()]*" we will
351 * try to use the value as a character. Anything else resets
352 * hex mode.
354 if (ikey.flags & CH_QUOTED)
355 goto ins_ch;
356 if (quoted == Q_THISCHAR) {
357 --sp->cno;
358 ++tp->owrite;
359 quoted = Q_NOTSET;
361 if (ch == HEX_CH)
362 hex = H_NEXTCHAR;
363 goto ins_ch;
366 switch (ikey.value) {
367 case K_CR:
368 case K_NL: /* New line. */
369 #define LINE_RESOLVE { \
370 /* \
371 * Handle abbreviations. If there was one, \
372 * discard the replay characters. \
373 */ \
374 if (abb == A_NOTSPACE && !replay) { \
375 if (txt_abbrev(sp, tp, &tmp, ch)) \
376 goto err; \
377 if (tmp) { \
378 if (LF_ISSET(TXT_RECORD)) \
379 rcol -= tmp; \
380 goto next_ch; \
383 if (abb != A_NOTSET) \
384 abb = A_SPACE; \
385 /* Handle hex numbers. */ \
386 if (hex == H_INHEX) { \
387 if (txt_hex(sp, tp, &tmp, ch)) \
388 goto err; \
389 if (tmp) { \
390 hex = H_NOTSET; \
391 goto next_ch; \
394 /* \
395 * The 'R' command returns any overwriteable \
396 * characters in the first line to the original \
397 * characters.
398 */ \
399 if (LF_ISSET(TXT_REPLACE) && tp->owrite && \
400 tp == tiqh->cqh_first) { \
401 memmove(tp->lb + sp->cno, \
402 lp + sp->cno, tp->owrite); \
403 tp->insert += tp->owrite; \
404 tp->owrite = 0; \
406 /* Delete any appended cursor. */ \
407 if (LF_ISSET(TXT_APPENDEOL)) { \
408 --tp->len; \
409 --tp->insert; \
412 LINE_RESOLVE;
414 /* CR returns from the vi command line. */
415 if (LF_ISSET(TXT_CR)) {
417 * If a script window and not the colon
418 * line, push a <cr> so it gets executed.
420 if (F_ISSET(sp, S_SCRIPT) &&
421 !LF_ISSET(TXT_INFOLINE))
422 (void)term_push(sp,
423 "\r", 1, 0, CH_NOMAP);
424 goto k_escape;
428 * Historic practice was to delete any <blank>
429 * characters following the inserted newline.
430 * This affects the 'R', 'c', and 's' commands.
432 for (p = tp->lb + sp->cno + tp->owrite;
433 tp->insert && isblank(*p);
434 ++p, ++tp->owrite, --tp->insert);
437 * Move any remaining insert characters into
438 * a new TEXT structure.
440 if ((ntp = text_init(sp,
441 tp->lb + sp->cno + tp->owrite,
442 tp->insert, tp->insert + 32)) == NULL)
443 goto err;
444 CIRCLEQ_INSERT_TAIL(tiqh, ntp, q);
446 /* Set bookkeeping for the new line. */
447 ntp->lno = tp->lno + 1;
448 ntp->insert = tp->insert;
451 * Note if the user inserted any characters on this
452 * line. Done before calling txt_ai_resolve() because
453 * it changes the value of sp->cno without making the
454 * corresponding changes to tp->ai.
456 tmp = sp->cno <= tp->ai;
459 * Resolve autoindented characters for the old line.
460 * Reset the autoindent line value. 0^D keeps the ai
461 * line from changing, ^D changes the level, even if
462 * there are no characters in the old line. Note,
463 * if using the current tp structure, use the cursor
464 * as the length, the user may have erased autoindent
465 * characters.
467 if (LF_ISSET(TXT_AUTOINDENT)) {
468 txt_ai_resolve(sp, tp);
470 if (carat_st == C_NOCHANGE) {
471 if (txt_auto(sp, ep,
472 OOBLNO, &ait, ait.ai, ntp))
473 goto err;
474 FREE_SPACE(sp, ait.lb, ait.lb_len);
475 } else
476 if (txt_auto(sp, ep,
477 OOBLNO, tp, sp->cno, ntp))
478 goto err;
479 carat_st = C_NOTSET;
483 * If the user hasn't entered any characters, delete
484 * any autoindent characters.
486 * !!!
487 * Historic vi didn't get the insert test right, if
488 * there were characters after the cursor, entering
489 * a <cr> left the autoindent characters on the line.
491 if (tmp)
492 sp->cno = 0;
494 /* Reset bookkeeping for the old line. */
495 tp->len = sp->cno;
496 tp->ai = tp->insert = tp->owrite = 0;
498 /* New cursor position. */
499 sp->cno = ntp->ai;
501 /* New lines are TXT_APPENDEOL if nothing to insert. */
502 if (ntp->insert == 0) {
503 TBINC(sp, tp->lb, tp->lb_len, tp->len + 1);
504 LF_SET(TXT_APPENDEOL);
505 ntp->lb[sp->cno] = CURSOR_CH;
506 ++ntp->insert;
507 ++ntp->len;
510 /* Update the old line. */
511 if (sp->s_change(sp, ep, tp->lno, LINE_RESET))
512 goto err;
514 /* Swap old and new TEXT's. */
515 tp = ntp;
517 /* Reset the cursor. */
518 sp->lno = tp->lno;
520 /* Update the new line. */
521 if (sp->s_change(sp, ep, tp->lno, LINE_INSERT))
522 goto err;
524 /* Set the renumber bit. */
525 F_SET(sp, S_RENUMBER);
527 /* Refresh if nothing waiting. */
528 if ((margin || !KEYS_WAITING(sp)) &&
529 sp->s_refresh(sp, ep))
530 goto err;
531 goto next_ch;
532 case K_ESCAPE: /* Escape. */
533 if (!LF_ISSET(TXT_ESCAPE))
534 goto ins_ch;
536 LINE_RESOLVE;
539 * If there aren't any trailing characters in the line
540 * and the user hasn't entered any characters, delete
541 * the autoindent characters.
543 if (!tp->insert && sp->cno <= tp->ai) {
544 tp->len = tp->owrite = 0;
545 sp->cno = 0;
546 } else if (LF_ISSET(TXT_AUTOINDENT))
547 txt_ai_resolve(sp, tp);
549 /* If there are insert characters, copy them down. */
550 k_escape: if (tp->insert && tp->owrite)
551 memmove(tp->lb + sp->cno,
552 tp->lb + sp->cno + tp->owrite, tp->insert);
553 tp->len -= tp->owrite;
556 * Delete any lines that were inserted into the text
557 * structure and then erased.
559 while (tp->q.cqe_next != (void *)tiqh) {
560 ntp = tp->q.cqe_next;
561 CIRCLEQ_REMOVE(tiqh, ntp, q);
562 text_free(ntp);
566 * If not resolving the lines into the file, end
567 * it with a nul.
569 * XXX
570 * This is wrong, should pass back a length.
572 if (LF_ISSET(TXT_RESOLVE)) {
573 if (txt_resolve(sp, ep, tiqh))
574 goto err;
575 } else {
576 TBINC(sp, tp->lb, tp->lb_len, tp->len + 1);
577 tp->lb[tp->len] = '\0';
581 * Set the return cursor position to rest on the last
582 * inserted character.
584 if (rp != NULL) {
585 rp->lno = tp->lno;
586 rp->cno = sp->cno ? sp->cno - 1 : 0;
587 if (sp->s_change(sp, ep, rp->lno, LINE_RESET))
588 goto err;
590 goto ret;
591 case K_CARAT: /* Delete autoindent chars. */
592 if (LF_ISSET(TXT_AUTOINDENT) && sp->cno <= tp->ai)
593 carat_st = C_CARATSET;
594 goto ins_ch;
595 case K_ZERO: /* Delete autoindent chars. */
596 if (LF_ISSET(TXT_AUTOINDENT) && sp->cno <= tp->ai)
597 carat_st = C_ZEROSET;
598 goto ins_ch;
599 case K_VEOF: /* Delete autoindent char. */
601 * If not doing autoindent, in the first column, no
602 * characters to erase, or already inserted non-ai
603 * characters, it's a literal. The last test is done
604 * in the switch, as the CARAT forms are N + 1, not N.
606 if (!LF_ISSET(TXT_AUTOINDENT) ||
607 sp->cno == 0 || tp->ai == 0)
608 goto ins_ch;
609 switch (carat_st) {
610 case C_CARATSET: /* ^^D */
611 if (sp->cno > tp->ai + tp->offset + 1)
612 goto ins_ch;
614 /* Save the ai string for later. */
615 ait.lb = NULL;
616 ait.lb_len = 0;
617 TBINC(sp, ait.lb, ait.lb_len, tp->ai);
618 memmove(ait.lb, tp->lb, tp->ai);
619 ait.ai = ait.len = tp->ai;
621 carat_st = C_NOCHANGE;
622 goto leftmargin;
623 case C_ZEROSET: /* 0^D */
624 if (sp->cno > tp->ai + tp->offset + 1)
625 goto ins_ch;
626 carat_st = C_NOTSET;
627 leftmargin: tp->lb[sp->cno - 1] = ' ';
628 tp->owrite += sp->cno - tp->offset;
629 tp->ai = 0;
630 sp->cno = tp->offset;
631 break;
632 case C_NOTSET: /* ^D */
633 if (sp->cno > tp->ai + tp->offset)
634 goto ins_ch;
635 (void)txt_outdent(sp, tp);
636 break;
637 default:
638 abort();
640 break;
641 case K_VERASE: /* Erase the last character. */
643 * If can erase over the prompt, return. Len is 0
644 * if backspaced over the prompt, 1 if only CR entered.
646 if (LF_ISSET(TXT_BS) && sp->cno <= tp->offset) {
647 tp->len = 0;
648 goto ret;
652 * If at the beginning of the line, try and drop back
653 * to a previously inserted line.
655 if (sp->cno == 0) {
656 if ((ntp = txt_backup(sp,
657 ep, tiqh, tp, flags)) == NULL)
658 goto err;
659 tp = ntp;
660 break;
663 /* If nothing to erase, bell the user. */
664 if (sp->cno <= tp->offset) {
665 msgq(sp, M_BERR,
666 "No more characters to erase.");
667 break;
670 /* Drop back one character. */
671 --sp->cno;
674 * Increment overwrite, decrement ai if deleted.
676 * !!!
677 * Historic vi did not permit users to use erase
678 * characters to delete autoindent characters.
680 ++tp->owrite;
681 if (sp->cno < tp->ai)
682 --tp->ai;
683 break;
684 case K_VWERASE: /* Skip back one word. */
686 * If at the beginning of the line, try and drop back
687 * to a previously inserted line.
689 if (sp->cno == 0) {
690 if ((ntp = txt_backup(sp,
691 ep, tiqh, tp, flags)) == NULL)
692 goto err;
693 tp = ntp;
697 * If at offset, nothing to erase so bell the user.
699 if (sp->cno <= tp->offset) {
700 msgq(sp, M_BERR,
701 "No more characters to erase.");
702 break;
706 * First werase goes back to any autoindent
707 * and second werase goes back to the offset.
709 * !!!
710 * Historic vi did not permit users to use erase
711 * characters to delete autoindent characters.
713 if (tp->ai && sp->cno > tp->ai)
714 max = tp->ai;
715 else {
716 tp->ai = 0;
717 max = tp->offset;
720 /* Skip over trailing space characters. */
721 while (sp->cno > max && isblank(tp->lb[sp->cno - 1])) {
722 --sp->cno;
723 ++tp->owrite;
725 if (sp->cno == max)
726 break;
728 * There are three types of word erase found on UNIX
729 * systems. They can be identified by how the string
730 * /a/b/c is treated -- as 1, 3, or 6 words. Historic
731 * vi had two classes of characters, and strings were
732 * delimited by them and <blank>'s, so, 6 words. The
733 * historic tty interface used <blank>'s to delimit
734 * strings, so, 1 word. The algorithm offered in the
735 * 4.4BSD tty interface (as stty altwerase) treats it
736 * as 3 words -- there are two classes of characters,
737 * and strings are delimited by them and <blank>'s.
738 * The difference is that the type of the first erased
739 * character erased is ignored, which is exactly right
740 * when erasing pathname components. Here, the options
741 * TXT_ALTWERASE and TXT_TTYWERASE specify the 4.4BSD
742 * tty interface and the historic tty driver behavior,
743 * respectively, and the default is the same as the
744 * historic vi behavior.
746 if (LF_ISSET(TXT_TTYWERASE))
747 while (sp->cno > max) {
748 --sp->cno;
749 ++tp->owrite;
750 if (isblank(tp->lb[sp->cno - 1]))
751 break;
753 else {
754 if (LF_ISSET(TXT_ALTWERASE)) {
755 --sp->cno;
756 ++tp->owrite;
757 if (isblank(tp->lb[sp->cno - 1]))
758 break;
760 if (sp->cno > max)
761 tmp = inword(tp->lb[sp->cno - 1]);
762 while (sp->cno > max) {
763 --sp->cno;
764 ++tp->owrite;
765 if (tmp != inword(tp->lb[sp->cno - 1])
766 || isblank(tp->lb[sp->cno - 1]))
767 break;
770 break;
771 case K_VKILL: /* Restart this line. */
773 * If at the beginning of the line, try and drop back
774 * to a previously inserted line.
776 if (sp->cno == 0) {
777 if ((ntp = txt_backup(sp,
778 ep, tiqh, tp, flags)) == NULL)
779 goto err;
780 tp = ntp;
783 /* If at offset, nothing to erase so bell the user. */
784 if (sp->cno <= tp->offset) {
785 msgq(sp, M_BERR,
786 "No more characters to erase.");
787 break;
791 * First kill goes back to any autoindent
792 * and second kill goes back to the offset.
794 * !!!
795 * Historic vi did not permit users to use erase
796 * characters to delete autoindent characters.
798 if (tp->ai && sp->cno > tp->ai)
799 max = tp->ai;
800 else {
801 tp->ai = 0;
802 max = tp->offset;
804 tp->owrite += sp->cno - max;
805 sp->cno = max;
806 break;
807 case K_CNTRLT: /* Add autoindent char. */
808 if (!LF_ISSET(TXT_CNTRLT))
809 goto ins_ch;
810 if (txt_indent(sp, tp))
811 goto err;
812 goto ebuf_chk;
813 case K_CNTRLZ:
814 (void)sp->s_suspend(sp);
815 break;
816 #ifdef HISTORIC_PRACTICE_IS_TO_INSERT_NOT_REPAINT
817 case K_FORMFEED:
818 F_SET(sp, S_REFRESH);
819 break;
820 #endif
821 case K_RIGHTBRACE:
822 case K_RIGHTPAREN:
823 showmatch = LF_ISSET(TXT_SHOWMATCH);
824 goto ins_ch;
825 case K_VLNEXT: /* Quote the next character. */
826 /* If in hex mode, see if we've entered a hex value. */
827 if (hex == H_INHEX) {
828 if (txt_hex(sp, tp, &tmp, ch))
829 goto err;
830 if (tmp) {
831 hex = H_NOTSET;
832 goto next_ch;
835 ch = '^';
836 quoted = Q_NEXTCHAR;
837 /* FALLTHROUGH */
838 default: /* Insert the character. */
839 ins_ch: /*
840 * If entering a space character after a word, check
841 * for abbreviations. If there was one, discard the
842 * replay characters.
844 if (isblank(ch) && abb == A_NOTSPACE && !replay) {
845 if (txt_abbrev(sp, tp, &tmp, ch))
846 goto err;
847 if (tmp) {
848 if (LF_ISSET(TXT_RECORD))
849 rcol -= tmp;
850 goto next_ch;
853 /* If in hex mode, see if we've entered a hex value. */
854 if (hex == H_INHEX && !isxdigit(ch)) {
855 if (txt_hex(sp, tp, &tmp, ch))
856 goto err;
857 if (tmp) {
858 hex = H_NOTSET;
859 goto next_ch;
862 /* Check to see if we've crossed the margin. */
863 if (margin) {
864 if (sp->s_column(sp, ep, &col))
865 goto err;
866 if (col >= margin) {
867 if (txt_margin(sp, tp, &tmp, ch))
868 goto err;
869 if (tmp)
870 goto next_ch;
873 if (abb != A_NOTSET)
874 abb = isblank(ch) ? A_SPACE : A_NOTSPACE;
876 if (tp->owrite) /* Overwrite a character. */
877 --tp->owrite;
878 else if (tp->insert) { /* Insert a character. */
879 ++tp->len;
880 if (tp->insert == 1)
881 tp->lb[sp->cno + 1] = tp->lb[sp->cno];
882 else
883 memmove(tp->lb + sp->cno + 1,
884 tp->lb + sp->cno, tp->insert);
887 tp->lb[sp->cno++] = ch;
890 * If we've reached the end of the buffer, then we
891 * need to switch into insert mode. This happens
892 * when there's a change to a mark and the user puts
893 * in more characters than the length of the motion.
895 ebuf_chk: if (sp->cno >= tp->len) {
896 TBINC(sp, tp->lb, tp->lb_len, tp->len + 1);
897 LF_SET(TXT_APPENDEOL);
898 tp->lb[sp->cno] = CURSOR_CH;
899 ++tp->insert;
900 ++tp->len;
903 if (hex == H_NEXTCHAR)
904 hex = H_INHEX;
905 if (quoted == Q_NEXTCHAR)
906 quoted = Q_THISCHAR;
907 break;
909 #if defined(DEBUG) && 1
910 if (sp->cno + tp->insert + tp->owrite != tp->len)
911 msgq(sp, M_ERR,
912 "len %u != cno: %u ai: %u insert %u overwrite %u",
913 tp->len, sp->cno, tp->ai, tp->insert, tp->owrite);
914 tp->len = sp->cno + tp->insert + tp->owrite;
915 #endif
918 /* Clear input flag. */
919 ret: F_CLR(sp, S_INPUT);
921 if (LF_ISSET(TXT_RECORD))
922 VIP(sp)->rep_cnt = rcol;
923 return (eval);
925 /* Error jump. */
926 err: eval = 1;
927 txt_err(sp, ep, tiqh);
928 goto ret;
932 * txt_abbrev --
933 * Handle abbreviations.
935 static int
936 txt_abbrev(sp, tp, didsubp, pushc)
937 SCR *sp;
938 TEXT *tp;
939 int *didsubp;
940 ARG_CHAR_T pushc;
942 CHAR_T ch;
943 SEQ *qp;
944 size_t len, off;
945 char *p;
947 /* Find the beginning of this "word". */
948 for (off = sp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
949 if (isblank(*p)) {
950 ++p;
951 break;
953 ++len;
954 if (off == tp->ai || off == tp->offset)
955 break;
958 /* Check for any abbreviations. */
959 if ((qp = seq_find(sp, NULL, p, len, SEQ_ABBREV, NULL)) == NULL) {
960 *didsubp = 0;
961 return (0);
965 * Push the abbreviation onto the tty stack. Historically, characters
966 * resulting from an abbreviation expansion were themselves subject to
967 * map expansions, O_SHOWMATCH matching etc. This means the expanded
968 * characters will be re-tested for abbreviations. It's difficult to
969 * know what historic practice in this case was, since abbreviations
970 * were applied to :colon command lines, so entering abbreviations that
971 * looped was tricky, although possible. In addition, obvious loops
972 * didn't work as expected. (The command ':ab a b|ab b c|ab c a' will
973 * silently only implement and/or display the last abbreviation.)
975 * This implementation doesn't recover well from such abbreviations.
976 * The main input loop counts abbreviated characters, and, when it
977 * reaches a limit, discards any abbreviated characters on the queue.
978 * It's difficult to back up to the original position, as the replay
979 * queue would have to be adjusted, and the line state when an initial
980 * abbreviated character was received would have to be saved.
982 ch = pushc;
983 if (term_push(sp, &ch, 1, 0, CH_ABBREVIATED))
984 return (1);
985 if (term_push(sp, qp->output, qp->olen, 0, CH_ABBREVIATED))
986 return (1);
989 * Move the cursor to the start of the abbreviation,
990 * adjust the length.
992 sp->cno -= len;
993 tp->len -= len;
995 /* Copy any insert characters back. */
996 if (tp->insert)
997 memmove(tp->lb + sp->cno + tp->owrite,
998 tp->lb + sp->cno + tp->owrite + len, tp->insert);
1001 * We return the length of the abbreviated characters. This is so
1002 * the calling routine can replace the replay characters with the
1003 * abbreviation. This means that subsequent '.' commands will produce
1004 * the same text, regardless of intervening :[un]abbreviate commands.
1005 * This is historic practice.
1007 *didsubp = len;
1008 return (0);
1011 /* Offset to next column of stop size. */
1012 #define STOP_OFF(c, stop) (stop - (c) % stop)
1015 * txt_ai_resolve --
1016 * When a line is resolved by <esc> or <cr>, review autoindent
1017 * characters.
1019 static void
1020 txt_ai_resolve(sp, tp)
1021 SCR *sp;
1022 TEXT *tp;
1024 u_long ts;
1025 int del;
1026 size_t cno, len, new, old, scno, spaces, tab_after_sp, tabs;
1027 char *p;
1030 * If the line is empty, has an offset, or no autoindent
1031 * characters, we're done.
1033 if (!tp->len || tp->offset || !tp->ai)
1034 return;
1037 * The autoindent characters plus any leading <blank> characters
1038 * in the line are resolved into the minimum number of characters.
1039 * Historic practice.
1041 ts = O_VAL(sp, O_TABSTOP);
1043 /* Figure out the last <blank> screen column. */
1044 for (p = tp->lb, scno = 0, len = tp->len,
1045 spaces = tab_after_sp = 0; len-- && isblank(*p); ++p)
1046 if (*p == '\t') {
1047 if (spaces)
1048 tab_after_sp = 1;
1049 scno += STOP_OFF(scno, ts);
1050 } else {
1051 ++spaces;
1052 ++scno;
1056 * If there are no spaces, or no tabs after spaces and less than
1057 * ts spaces, it's already minimal.
1059 if (!spaces || !tab_after_sp && spaces < ts)
1060 return;
1062 /* Count up spaces/tabs needed to get to the target. */
1063 for (cno = 0, tabs = 0; cno + STOP_OFF(cno, ts) <= scno; ++tabs)
1064 cno += STOP_OFF(cno, ts);
1065 spaces = scno - cno;
1068 * Figure out how many characters we're dropping -- if we're not
1069 * dropping any, it's already minimal, we're done.
1071 old = p - tp->lb;
1072 new = spaces + tabs;
1073 if (old == new)
1074 return;
1076 /* Shift the rest of the characters down, adjust the counts. */
1077 del = old - new;
1078 memmove(p - del, p, tp->len - old);
1079 sp->cno -= del;
1080 tp->len -= del;
1082 /* Fill in space/tab characters. */
1083 for (p = tp->lb; tabs--;)
1084 *p++ = '\t';
1085 while (spaces--)
1086 *p++ = ' ';
1090 * txt_auto --
1091 * Handle autoindent. If aitp isn't NULL, use it, otherwise,
1092 * retrieve the line.
1095 txt_auto(sp, ep, lno, aitp, len, tp)
1096 SCR *sp;
1097 EXF *ep;
1098 recno_t lno;
1099 size_t len;
1100 TEXT *aitp, *tp;
1102 size_t nlen;
1103 char *p, *t;
1105 if (aitp == NULL) {
1106 if ((p = t = file_gline(sp, ep, lno, &len)) == NULL)
1107 return (0);
1108 } else
1109 p = t = aitp->lb;
1110 for (nlen = 0; len; ++p) {
1111 if (!isblank(*p))
1112 break;
1113 /* If last character is a space, it counts. */
1114 if (--len == 0) {
1115 ++p;
1116 break;
1120 /* No indentation. */
1121 if (p == t)
1122 return (0);
1124 /* Set count. */
1125 nlen = p - t;
1127 /* Make sure the buffer's big enough. */
1128 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + nlen);
1130 /* Copy the indentation into the new buffer. */
1131 memmove(tp->lb + nlen, tp->lb, tp->len);
1132 memmove(tp->lb, t, nlen);
1133 tp->len += nlen;
1135 /* Return the additional length. */
1136 tp->ai = nlen;
1137 return (0);
1141 * txt_backup --
1142 * Back up to the previously edited line.
1144 static TEXT *
1145 txt_backup(sp, ep, tiqh, tp, flags)
1146 SCR *sp;
1147 EXF *ep;
1148 TEXTH *tiqh;
1149 TEXT *tp;
1150 u_int flags;
1152 TEXT *ntp;
1153 size_t col;
1155 if (tp->q.cqe_prev == (void *)tiqh) {
1156 msgq(sp, M_BERR, "Already at the beginning of the insert");
1157 return (tp);
1160 /* Update the old line on the screen. */
1161 if (sp->s_change(sp, ep, tp->lno, LINE_DELETE))
1162 return (NULL);
1164 /* Get a handle on the previous TEXT structure. */
1165 ntp = tp->q.cqe_prev;
1167 /* Make sure that we can get enough space. */
1168 if (LF_ISSET(TXT_APPENDEOL) && ntp->len + 1 > ntp->lb_len &&
1169 binc(sp, &ntp->lb, &ntp->lb_len, ntp->len + 1))
1170 return (NULL);
1173 * Release current TEXT; now committed to the swap, nothing
1174 * better fail.
1176 CIRCLEQ_REMOVE(tiqh, tp, q);
1177 text_free(tp);
1179 /* Swap TEXT's. */
1180 tp = ntp;
1182 /* Set bookkeeping information. */
1183 col = tp->len;
1184 if (LF_ISSET(TXT_APPENDEOL)) {
1185 tp->lb[col] = CURSOR_CH;
1186 ++tp->insert;
1187 ++tp->len;
1189 sp->lno = tp->lno;
1190 sp->cno = col;
1191 return (tp);
1195 * txt_err --
1196 * Handle an error during input processing.
1198 static void
1199 txt_err(sp, ep, tiqh)
1200 SCR *sp;
1201 EXF *ep;
1202 TEXTH *tiqh;
1204 recno_t lno;
1205 size_t len;
1208 * The problem with input processing is that the cursor is at an
1209 * indeterminate position since some input may have been lost due
1210 * to a malloc error. So, try to go back to the place from which
1211 * the cursor started, knowing that it may no longer be available.
1213 * We depend on at least one line number being set in the text
1214 * chain.
1216 for (lno = tiqh->cqh_first->lno;
1217 file_gline(sp, ep, lno, &len) == NULL && lno > 0; --lno);
1219 sp->lno = lno == 0 ? 1 : lno;
1220 sp->cno = 0;
1222 /* Redraw the screen, just in case. */
1223 F_SET(sp, S_REDRAW);
1227 * txt_hex --
1228 * Let the user insert any character value they want.
1230 * !!!
1231 * This is an extension. The pattern "^Vx[0-9a-fA-F]*" is a way
1232 * for the user to specify a character value which their keyboard
1233 * may not be able to enter.
1235 static int
1236 txt_hex(sp, tp, was_hex, pushc)
1237 SCR *sp;
1238 TEXT *tp;
1239 int *was_hex;
1240 ARG_CHAR_T pushc;
1242 CHAR_T ch, savec;
1243 size_t len, off;
1244 u_long value;
1245 char *p, *wp;
1248 * Null-terminate the string. Since nul isn't a legal hex value,
1249 * this should be okay, and lets us use a local routine, which
1250 * presumably understands the character set, to convert the value.
1252 savec = tp->lb[sp->cno];
1253 tp->lb[sp->cno] = 0;
1255 /* Find the previous HEX_CH. */
1256 for (off = sp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1257 if (*p == HEX_CH) {
1258 wp = p + 1;
1259 break;
1261 ++len;
1262 /* If not on this line, there's nothing to do. */
1263 if (off == tp->ai || off == tp->offset)
1264 goto nothex;
1267 /* If no length, then it wasn't a hex value. */
1268 if (len == 0)
1269 goto nothex;
1271 /* Get the value. */
1272 value = strtol(wp, NULL, 16);
1273 if (value == LONG_MIN || value == LONG_MAX || value > MAX_CHAR_T) {
1274 nothex: tp->lb[sp->cno] = savec;
1275 *was_hex = 0;
1276 return (0);
1279 ch = pushc;
1280 if (term_push(sp, &ch, 1, 0, CH_NOMAP | CH_QUOTED))
1281 return (1);
1282 ch = value;
1283 if (term_push(sp, &ch, 1, 0, CH_NOMAP | CH_QUOTED))
1284 return (1);
1286 tp->lb[sp->cno] = savec;
1288 /* Move the cursor to the start of the hex value, adjust the length. */
1289 sp->cno -= len + 1;
1290 tp->len -= len + 1;
1292 /* Copy any insert characters back. */
1293 if (tp->insert)
1294 memmove(tp->lb + sp->cno + tp->owrite,
1295 tp->lb + sp->cno + tp->owrite + len + 1, tp->insert);
1297 *was_hex = 1;
1298 return (0);
1302 * Txt_indent and txt_outdent are truly strange. ^T and ^D do movements
1303 * to the next or previous shiftwidth value, i.e. for a 1-based numbering,
1304 * with shiftwidth=3, ^T moves a cursor on the 7th, 8th or 9th column to
1305 * the 10th column, and ^D moves it back.
1307 * !!!
1308 * The ^T and ^D characters in historical vi only had special meaning when
1309 * they were the first characters typed after entering text input mode.
1310 * Since normal erase characters couldn't erase autoindent (in this case
1311 * ^T) characters, this meant that inserting text into previously existing
1312 * text was quite strange, ^T only worked if it was the first keystroke,
1313 * and then it could only be erased by using ^D. This implementation treats
1314 * ^T specially anywhere it occurs in the input, and permits the standard
1315 * erase characters to erase characters inserted using it.
1317 * XXX
1318 * Technically, txt_indent, txt_outdent should part of the screen interface,
1319 * as they require knowledge of the size of a space character on the screen.
1320 * (Not the size of tabs, because tabs are logically composed of spaces.)
1321 * They're left in the text code because they're complicated, not to mention
1322 * the gruesome awareness that if spaces aren't a single column on the screen
1323 * for any language, we're into some serious, ah, for lack of a better word,
1324 * "issues".
1328 * txt_indent --
1329 * Handle ^T indents.
1331 static int
1332 txt_indent(sp, tp)
1333 SCR *sp;
1334 TEXT *tp;
1336 u_long sw, ts;
1337 size_t cno, off, scno, spaces, tabs;
1339 ts = O_VAL(sp, O_TABSTOP);
1340 sw = O_VAL(sp, O_SHIFTWIDTH);
1342 /* Get the current screen column. */
1343 for (off = scno = 0; off < sp->cno; ++off)
1344 if (tp->lb[off] == '\t')
1345 scno += STOP_OFF(scno, ts);
1346 else
1347 ++scno;
1349 /* Count up spaces/tabs needed to get to the target. */
1350 for (cno = scno, scno += STOP_OFF(scno, sw), tabs = 0;
1351 cno + STOP_OFF(cno, ts) <= scno; ++tabs)
1352 cno += STOP_OFF(cno, ts);
1353 spaces = scno - cno;
1355 /* Put space/tab characters in place of any overwrite characters. */
1356 for (; tp->owrite && tabs; --tp->owrite, --tabs, ++tp->ai)
1357 tp->lb[sp->cno++] = '\t';
1358 for (; tp->owrite && spaces; --tp->owrite, --spaces, ++tp->ai)
1359 tp->lb[sp->cno++] = ' ';
1361 if (!tabs && !spaces)
1362 return (0);
1364 /* Make sure there's enough room. */
1365 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + spaces + tabs);
1367 /* Move the insert characters out of the way. */
1368 if (tp->insert)
1369 memmove(tp->lb + sp->cno + spaces + tabs,
1370 tp->lb + sp->cno, tp->insert);
1372 /* Add new space/tab characters. */
1373 for (; tabs--; ++tp->len, ++tp->ai)
1374 tp->lb[sp->cno++] = '\t';
1375 for (; spaces--; ++tp->len, ++tp->ai)
1376 tp->lb[sp->cno++] = ' ';
1377 return (0);
1381 * txt_outdent --
1382 * Handle ^D outdents.
1385 static int
1386 txt_outdent(sp, tp)
1387 SCR *sp;
1388 TEXT *tp;
1390 u_long sw, ts;
1391 size_t cno, off, scno, spaces;
1393 ts = O_VAL(sp, O_TABSTOP);
1394 sw = O_VAL(sp, O_SHIFTWIDTH);
1396 /* Get the current screen column. */
1397 for (off = scno = 0; off < sp->cno; ++off)
1398 if (tp->lb[off] == '\t')
1399 scno += STOP_OFF(scno, ts);
1400 else
1401 ++scno;
1403 /* Get the previous shiftwidth column. */
1404 for (cno = scno; --scno % sw != 0;);
1406 /* Decrement characters until less than or equal to that slot. */
1407 for (; cno > scno; --sp->cno, --tp->ai, ++tp->owrite)
1408 if (tp->lb[--off] == '\t')
1409 cno -= STOP_OFF(cno, ts);
1410 else
1411 --cno;
1413 /* Spaces needed to get to the target. */
1414 spaces = scno - cno;
1416 /* Maybe just a delete. */
1417 if (spaces == 0)
1418 return (0);
1420 /* Make sure there's enough room. */
1421 BINC_RET(sp, tp->lb, tp->lb_len, tp->len + spaces);
1423 /* Use up any overwrite characters. */
1424 for (; tp->owrite && spaces; --spaces, ++tp->ai, --tp->owrite)
1425 tp->lb[sp->cno++] = ' ';
1427 /* Maybe that was enough. */
1428 if (spaces == 0)
1429 return (0);
1431 /* Move the insert characters out of the way. */
1432 if (tp->insert)
1433 memmove(tp->lb + sp->cno + spaces,
1434 tp->lb + sp->cno, tp->insert);
1436 /* Add new space characters. */
1437 for (; spaces--; ++tp->len, ++tp->ai)
1438 tp->lb[sp->cno++] = ' ';
1439 return (0);
1443 * txt_resolve --
1444 * Resolve the input text chain into the file.
1446 static int
1447 txt_resolve(sp, ep, tiqh)
1448 SCR *sp;
1449 EXF *ep;
1450 TEXTH *tiqh;
1452 TEXT *tp;
1453 recno_t lno;
1455 /* The first line replaces a current line. */
1456 tp = tiqh->cqh_first;
1457 if (file_sline(sp, ep, tp->lno, tp->lb, tp->len))
1458 return (1);
1460 /* All subsequent lines are appended into the file. */
1461 for (lno = tp->lno; (tp = tp->q.cqe_next) != (void *)&sp->tiq; ++lno)
1462 if (file_aline(sp, ep, 0, lno, tp->lb, tp->len))
1463 return (1);
1464 return (0);
1468 * txt_showmatch --
1469 * Show a character match.
1471 * !!!
1472 * Historic vi tried to display matches even in the :colon command line.
1473 * I think not.
1475 static void
1476 txt_showmatch(sp, ep)
1477 SCR *sp;
1478 EXF *ep;
1480 struct timeval second;
1481 VCS cs;
1482 MARK m;
1483 fd_set zero;
1484 int cnt, endc, startc;
1487 * Do a refresh first, in case the v_ntext() code hasn't done
1488 * one in awhile, so the user can see what we're complaining
1489 * about.
1491 if (sp->s_refresh(sp, ep))
1492 return;
1494 * We don't display the match if it's not on the screen. Find
1495 * out what the first character on the screen is.
1497 if (sp->s_position(sp, ep, &m, 0, P_TOP))
1498 return;
1500 /* Initialize the getc() interface. */
1501 cs.cs_lno = sp->lno;
1502 cs.cs_cno = sp->cno - 1;
1503 if (cs_init(sp, ep, &cs))
1504 return;
1505 startc = (endc = cs.cs_ch) == ')' ? '(' : '{';
1507 /* Search for the match. */
1508 for (cnt = 1;;) {
1509 if (cs_prev(sp, ep, &cs))
1510 return;
1511 if (cs.cs_lno < m.lno ||
1512 cs.cs_lno == m.lno && cs.cs_cno < m.cno)
1513 return;
1514 if (cs.cs_flags != 0) {
1515 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF) {
1516 (void)sp->s_bell(sp);
1517 return;
1519 continue;
1521 if (cs.cs_ch == endc)
1522 ++cnt;
1523 else if (cs.cs_ch == startc && --cnt == 0)
1524 break;
1527 /* Move to the match. */
1528 m.lno = sp->lno;
1529 m.cno = sp->cno;
1530 sp->lno = cs.cs_lno;
1531 sp->cno = cs.cs_cno;
1532 (void)sp->s_refresh(sp, ep);
1535 * Sleep(3) is eight system calls. Do it fast -- besides,
1536 * I don't want to wait an entire second.
1538 FD_ZERO(&zero);
1539 second.tv_sec = O_VAL(sp, O_MATCHTIME) / 10;
1540 second.tv_usec = (O_VAL(sp, O_MATCHTIME) % 10) * 100000L;
1541 (void)select(0, &zero, &zero, &zero, &second);
1543 /* Return to the current location. */
1544 sp->lno = m.lno;
1545 sp->cno = m.cno;
1546 (void)sp->s_refresh(sp, ep);
1550 * txt_margin --
1551 * Handle margin wrap.
1553 * !!!
1554 * Historic vi belled the user each time a character was entered after
1555 * crossing the margin until a space was entered which could be used to
1556 * break the line. I don't, it tends to wake the cats.
1558 static int
1559 txt_margin(sp, tp, didbreak, pushc)
1560 SCR *sp;
1561 TEXT *tp;
1562 int *didbreak;
1563 ARG_CHAR_T pushc;
1565 CHAR_T ch;
1566 size_t len, off, tlen;
1567 char *p, *wp;
1569 /* Find the closest previous blank. */
1570 for (off = sp->cno - 1, p = tp->lb + off, len = 0;; --p, --off) {
1571 if (isblank(*p)) {
1572 wp = p + 1;
1573 break;
1575 ++len;
1576 /* If it's the beginning of the line, there's nothing to do. */
1577 if (off == tp->ai || off == tp->offset) {
1578 *didbreak = 0;
1579 return (0);
1584 * Historic practice is to delete any trailing whitespace
1585 * from the previous line.
1587 for (tlen = len;; --p, --off) {
1588 if (!isblank(*p))
1589 break;
1590 ++tlen;
1591 if (off == tp->ai || off == tp->offset)
1592 break;
1595 ch = pushc;
1596 if (term_push(sp, &ch, 1, 0, CH_NOMAP | CH_QUOTED))
1597 return (1);
1598 if (len && term_push(sp, wp, len, 0, CH_NOMAP | CH_QUOTED))
1599 return (1);
1600 ch = '\n';
1601 if (term_push(sp, &ch, 1, 0, CH_NOMAP))
1602 return (1);
1604 sp->cno -= tlen;
1605 tp->owrite += tlen;
1606 *didbreak = 1;
1607 return (0);