2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "@(#)ex_txt.c 10.17 (Berkeley) 10/10/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
26 #include "../common/common.h"
30 * The backslash characters was special when it preceded a newline as part of
31 * a substitution replacement pattern. For example, the input ":a\<cr>" would
32 * failed immediately with an error, as the <cr> wasn't part of a substitution
33 * replacement pattern. This implies a frightening integration of the editor
34 * and the parser and/or the RE engine. There's no way I'm going to reproduce
37 * So, if backslashes are special, this code inserts the backslash and the next
38 * character into the string, without regard for the character or the command
39 * being entered. Since "\<cr>" was illegal historically (except for the one
40 * special case), and the command will fail eventually, no historical scripts
41 * should break (presuming they didn't depend on the failure mode itself or the
42 * characters remaining when failure occurred.
45 static int txt_dent
__P((SCR
*, TEXT
*));
46 static void txt_prompt
__P((SCR
*, TEXT
*, ARG_CHAR_T
, u_int32_t
));
50 * Get lines from the terminal for ex.
52 * PUBLIC: int ex_txt __P((SCR *, TEXTH *, ARG_CHAR_T, u_int32_t));
55 ex_txt(sp
, tiqh
, prompt
, flags
)
71 * Get a TEXT structure with some initial buffer space, reusing the
72 * last one if it's big enough. (All TEXT bookkeeping fields default
73 * to 0 -- text_init() handles this.)
75 if (tiqh
->cqh_first
!= (void *)tiqh
) {
77 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< 32) {
83 newtp
: if ((tp
= text_init(sp
, NULL
, 0, 32)) == NULL
)
85 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
88 /* Set the starting line number. */
89 tp
->lno
= sp
->lno
+ 1;
92 * If it's a terminal, set up autoindent, put out the prompt, and
93 * set it up so we know we were suspended. Otherwise, turn off
94 * the autoindent flag, as that requires less special casing below.
97 * Historic practice is that ^Z suspended command mode (but, because
98 * it ran in cooked mode, it was unaffected by the autowrite option.)
99 * On restart, any "current" input was discarded, whether in insert
100 * mode or not, and ex was in command mode. This code matches historic
101 * practice, but not 'cause it's easier.
104 if (F_ISSET(gp
, G_SCRIPTED
))
105 LF_CLR(TXT_AUTOINDENT
);
107 if (LF_ISSET(TXT_AUTOINDENT
)) {
109 if (v_txt_auto(sp
, sp
->lno
, NULL
, 0, tp
))
112 txt_prompt(sp
, tp
, prompt
, flags
);
115 for (carat_st
= C_NOTSET
;;) {
116 if (v_event_get(sp
, &ev
, 0, 0))
119 /* Deal with all non-character events. */
120 switch (ev
.e_event
) {
133 * Handle EOF/SIGINT events by discarding partially
134 * entered text and returning. EOF returns failure,
135 * E_INTERRUPT returns success.
139 v_event_err(sp
, &ev
);
144 * Deal with character events.
146 * Check to see if the character fits into the input buffer.
147 * (Use tp->len, ignore overwrite and non-printable chars.)
149 BINC_GOTO(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
151 switch (ev
.e_value
) {
155 * Historically, <carriage-return>'s in the command
156 * weren't special, so the ex parser would return an
157 * unknown command error message. However, if they
158 * terminated the command if they were in a map. I'm
159 * pretty sure this still isn't right, but it handles
160 * what I've seen so far.
162 if (!F_ISSET(&ev
.e_ch
, CH_MAPPED
))
167 * '\' can escape <carriage-return>/<newline>. We
168 * don't discard the backslash because we need it
169 * to get the <newline> through the ex parser.
171 if (LF_ISSET(TXT_BACKSLASH
) &&
172 tp
->len
!= 0 && tp
->lb
[tp
->len
- 1] == '\\')
176 * CR returns from the ex command line.
179 * Terminate with a nul, needed by filter.
181 if (LF_ISSET(TXT_CR
)) {
182 tp
->lb
[tp
->len
] = '\0';
187 * '.' may terminate text input mode; free the current
190 if (LF_ISSET(TXT_DOTTERM
) && tp
->len
== tp
->ai
+ 1 &&
191 tp
->lb
[tp
->len
- 1] == '.') {
192 notlast
: CIRCLEQ_REMOVE(tiqh
, tp
, q
);
197 /* Set up bookkeeping for the new line. */
198 if ((ntp
= text_init(sp
, NULL
, 0, 32)) == NULL
)
200 ntp
->lno
= tp
->lno
+ 1;
203 * Reset the autoindent line value. 0^D keeps the ai
204 * line from changing, ^D changes the level, even if
205 * there were no characters in the old line. Note, if
206 * using the current tp structure, use the cursor as
207 * the length, the autoindent characters may have been
210 if (LF_ISSET(TXT_AUTOINDENT
)) {
211 if (carat_st
== C_NOCHANGE
) {
213 OOBLNO
, &ait
, ait
.ai
, ntp
))
218 OOBLNO
, tp
, tp
->len
, ntp
))
222 txt_prompt(sp
, ntp
, prompt
, flags
);
225 * Swap old and new TEXT's, and insert the new TEXT
229 CIRCLEQ_INSERT_TAIL(tiqh
, tp
, q
);
231 case K_CARAT
: /* Delete autoindent chars. */
232 if (tp
->len
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
233 carat_st
= C_CARATSET
;
235 case K_ZERO
: /* Delete autoindent chars. */
236 if (tp
->len
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
237 carat_st
= C_ZEROSET
;
239 case K_CNTRLD
: /* Delete autoindent char. */
242 * Historically, the ^D command took (but then ignored)
243 * a count. For simplicity, we don't return it unless
244 * it's the first character entered. The check for len
245 * equal to 0 is okay, TXT_AUTOINDENT won't be set.
247 if (LF_ISSET(TXT_CNTRLD
)) {
248 for (cnt
= 0; cnt
< tp
->len
; ++cnt
)
249 if (!isblank(tp
->lb
[cnt
]))
251 if (cnt
== tp
->len
) {
257 * Put out a line separator, in case
266 * POSIX 1003.1b-1993, paragraph 7.1.1.9, states that
267 * the EOF characters are discarded if there are other
268 * characters to process in the line, i.e. if the EOF
269 * is not the first character in the line. For this
270 * reason, historic ex discarded the EOF characters,
271 * even if occurring in the middle of the input line.
272 * We match that historic practice.
275 * The test for discarding in the middle of the line is
276 * done in the switch, because the CARAT forms are N+1,
280 * There's considerable magic to make the terminal code
281 * return the EOF character at all. See that code for
284 if (!LF_ISSET(TXT_AUTOINDENT
) || tp
->len
== 0)
287 case C_CARATSET
: /* ^^D */
288 if (tp
->len
> tp
->ai
+ 1)
291 /* Save the ai string for later. */
294 BINC_GOTO(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
295 memcpy(ait
.lb
, tp
->lb
, tp
->ai
);
296 ait
.ai
= ait
.len
= tp
->ai
;
298 carat_st
= C_NOCHANGE
;
300 case C_ZEROSET
: /* 0^D */
301 if (tp
->len
> tp
->ai
+ 1)
305 leftmargin
: (void)gp
->scr_ex_adjust(sp
, EX_TERM_CE
);
306 tp
->ai
= tp
->len
= 0;
308 case C_NOTSET
: /* ^D */
309 if (tp
->len
> tp
->ai
)
312 if (txt_dent(sp
, tp
))
319 /* Clear and redisplay the line. */
320 (void)gp
->scr_ex_adjust(sp
, EX_TERM_CE
);
321 txt_prompt(sp
, tp
, prompt
, flags
);
325 * See the TXT_BEAUTIFY comment in vi/v_txt_ev.c.
327 * Silently eliminate any iscntrl() character that was
328 * not already handled specially, except for <tab> and
331 ins_ch
: if (LF_ISSET(TXT_BEAUTIFY
) && iscntrl(ev
.e_c
) &&
332 ev
.e_value
!= K_FORMFEED
&& ev
.e_value
!= K_TAB
)
335 tp
->lb
[tp
->len
++] = ev
.e_c
;
350 * Display the ex prompt, line number, ai characters. Characters had
351 * better be printable by the terminal driver, but that's its problem,
355 txt_prompt(sp
, tp
, prompt
, flags
)
361 /* Display the prompt. */
362 if (LF_ISSET(TXT_PROMPT
))
363 (void)printf("%c", prompt
);
365 /* Display the line number. */
366 if (LF_ISSET(TXT_NUMBER
) && O_ISSET(sp
, O_NUMBER
))
367 (void)printf("%6lu ", (u_long
)tp
->lno
);
369 /* Print out autoindent string. */
370 if (LF_ISSET(TXT_AUTOINDENT
))
371 (void)printf("%.*s", (int)tp
->ai
, tp
->lb
);
372 (void)fflush(stdout
);
377 * Handle ^D outdents.
379 * Ex version of vi/v_ntext.c:txt_dent(). See that code for the (usual)
380 * ranting and raving. This is a fair bit simpler as ^T isn't special.
388 size_t cno
, off
, scno
, spaces
, tabs
;
390 ts
= O_VAL(sp
, O_TABSTOP
);
391 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
393 /* Get the current screen column. */
394 for (off
= scno
= 0; off
< tp
->len
; ++off
)
395 if (tp
->lb
[off
] == '\t')
396 scno
+= COL_OFF(scno
, ts
);
400 /* Get the previous shiftwidth column. */
405 * Since we don't know what comes before the character(s) being
406 * deleted, we have to resolve the autoindent characters . The
407 * example is a <tab>, which doesn't take up a full shiftwidth
408 * number of columns because it's preceded by <space>s. This is
409 * easy to get if the user sets shiftwidth to a value less than
410 * tabstop, and then uses ^T to indent, and ^D to outdent.
412 * Count up spaces/tabs needed to get to the target.
414 for (cno
= 0, tabs
= 0; cno
+ COL_OFF(cno
, ts
) <= scno
; ++tabs
)
415 cno
+= COL_OFF(cno
, ts
);
418 /* Make sure there's enough room. */
419 BINC_RET(sp
, tp
->lb
, tp
->lb_len
, tabs
+ spaces
+ 1);
421 /* Adjust the final ai character count. */
422 tp
->ai
= tabs
+ spaces
;
424 /* Enter the replacement characters. */
425 for (tp
->len
= 0; tabs
> 0; --tabs
)
426 tp
->lb
[tp
->len
++] = '\t';
427 for (; spaces
> 0; --spaces
)
428 tp
->lb
[tp
->len
++] = ' ';