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
[] = "$Id: ex_txt.c,v 10.23 2001/06/25 15:19:21 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:21 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
32 * The backslash characters was special when it preceded a newline as part of
33 * a substitution replacement pattern. For example, the input ":a\<cr>" would
34 * failed immediately with an error, as the <cr> wasn't part of a substitution
35 * replacement pattern. This implies a frightening integration of the editor
36 * and the parser and/or the RE engine. There's no way I'm going to reproduce
39 * So, if backslashes are special, this code inserts the backslash and the next
40 * character into the string, without regard for the character or the command
41 * being entered. Since "\<cr>" was illegal historically (except for the one
42 * special case), and the command will fail eventually, no historical scripts
43 * should break (presuming they didn't depend on the failure mode itself or the
44 * characters remaining when failure occurred.
47 static int txt_dent
__P((SCR
*, TEXT
*));
48 static void txt_prompt
__P((SCR
*, TEXT
*, ARG_CHAR_T
, u_int32_t
));
52 * Get lines from the terminal for ex.
54 * PUBLIC: int ex_txt __P((SCR *, TEXTH *, ARG_CHAR_T, u_int32_t));
57 ex_txt(SCR
*sp
, TEXTH
*tiqh
, ARG_CHAR_T prompt
, u_int32_t flags
)
69 * Get a TEXT structure with some initial buffer space, reusing the
70 * last one if it's big enough. (All TEXT bookkeeping fields default
71 * to 0 -- text_init() handles this.)
73 if (tiqh
->cqh_first
!= (void *)tiqh
) {
75 if (tp
->q
.cqe_next
!= (void *)tiqh
|| tp
->lb_len
< 32) {
81 newtp
: if ((tp
= text_init(sp
, NULL
, 0, 32)) == NULL
)
83 CIRCLEQ_INSERT_HEAD(tiqh
, tp
, q
);
86 /* Set the starting line number. */
87 tp
->lno
= sp
->lno
+ 1;
90 * If it's a terminal, set up autoindent, put out the prompt, and
91 * set it up so we know we were suspended. Otherwise, turn off
92 * the autoindent flag, as that requires less special casing below.
95 * Historic practice is that ^Z suspended command mode (but, because
96 * it ran in cooked mode, it was unaffected by the autowrite option.)
97 * On restart, any "current" input was discarded, whether in insert
98 * mode or not, and ex was in command mode. This code matches historic
99 * practice, but not 'cause it's easier.
102 if (F_ISSET(gp
, G_SCRIPTED
))
103 LF_CLR(TXT_AUTOINDENT
);
105 if (LF_ISSET(TXT_AUTOINDENT
)) {
107 if (v_txt_auto(sp
, sp
->lno
, NULL
, 0, tp
))
110 txt_prompt(sp
, tp
, prompt
, flags
);
113 for (carat_st
= C_NOTSET
;;) {
114 if (v_event_get(sp
, &ev
, 0, 0))
117 /* Deal with all non-character events. */
118 switch (ev
.e_event
) {
131 * Handle EOF/SIGINT events by discarding partially
132 * entered text and returning. EOF returns failure,
133 * E_INTERRUPT returns success.
137 v_event_err(sp
, &ev
);
142 * Deal with character events.
144 * Check to see if the character fits into the input buffer.
145 * (Use tp->len, ignore overwrite and non-printable chars.)
147 BINC_GOTOW(sp
, tp
->lb
, tp
->lb_len
, tp
->len
+ 1);
149 switch (ev
.e_value
) {
153 * Historically, <carriage-return>'s in the command
154 * weren't special, so the ex parser would return an
155 * unknown command error message. However, if they
156 * terminated the command if they were in a map. I'm
157 * pretty sure this still isn't right, but it handles
158 * what I've seen so far.
160 if (!FL_ISSET(ev
.e_flags
, CH_MAPPED
))
165 * '\' can escape <carriage-return>/<newline>. We
166 * don't discard the backslash because we need it
167 * to get the <newline> through the ex parser.
169 if (LF_ISSET(TXT_BACKSLASH
) &&
170 tp
->len
!= 0 && tp
->lb
[tp
->len
- 1] == '\\')
174 * CR returns from the ex command line.
177 * Terminate with a nul, needed by filter.
179 if (LF_ISSET(TXT_CR
)) {
180 tp
->lb
[tp
->len
] = '\0';
185 * '.' may terminate text input mode; free the current
188 if (LF_ISSET(TXT_DOTTERM
) && tp
->len
== tp
->ai
+ 1 &&
189 tp
->lb
[tp
->len
- 1] == '.') {
190 notlast
: CIRCLEQ_REMOVE(tiqh
, tp
, q
);
195 /* Set up bookkeeping for the new line. */
196 if ((ntp
= text_init(sp
, NULL
, 0, 32)) == NULL
)
198 ntp
->lno
= tp
->lno
+ 1;
201 * Reset the autoindent line value. 0^D keeps the ai
202 * line from changing, ^D changes the level, even if
203 * there were no characters in the old line. Note, if
204 * using the current tp structure, use the cursor as
205 * the length, the autoindent characters may have been
208 if (LF_ISSET(TXT_AUTOINDENT
)) {
209 if (carat_st
== C_NOCHANGE
) {
211 OOBLNO
, &ait
, ait
.ai
, ntp
))
216 OOBLNO
, tp
, tp
->len
, ntp
))
220 txt_prompt(sp
, ntp
, prompt
, flags
);
223 * Swap old and new TEXT's, and insert the new TEXT
227 CIRCLEQ_INSERT_TAIL(tiqh
, tp
, q
);
229 case K_CARAT
: /* Delete autoindent chars. */
230 if (tp
->len
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
231 carat_st
= C_CARATSET
;
233 case K_ZERO
: /* Delete autoindent chars. */
234 if (tp
->len
<= tp
->ai
&& LF_ISSET(TXT_AUTOINDENT
))
235 carat_st
= C_ZEROSET
;
237 case K_CNTRLD
: /* Delete autoindent char. */
240 * Historically, the ^D command took (but then ignored)
241 * a count. For simplicity, we don't return it unless
242 * it's the first character entered. The check for len
243 * equal to 0 is okay, TXT_AUTOINDENT won't be set.
245 if (LF_ISSET(TXT_CNTRLD
)) {
246 for (cnt
= 0; cnt
< tp
->len
; ++cnt
)
247 if (!isblank(tp
->lb
[cnt
]))
249 if (cnt
== tp
->len
) {
255 * Put out a line separator, in case
264 * POSIX 1003.1b-1993, paragraph 7.1.1.9, states that
265 * the EOF characters are discarded if there are other
266 * characters to process in the line, i.e. if the EOF
267 * is not the first character in the line. For this
268 * reason, historic ex discarded the EOF characters,
269 * even if occurring in the middle of the input line.
270 * We match that historic practice.
273 * The test for discarding in the middle of the line is
274 * done in the switch, because the CARAT forms are N+1,
278 * There's considerable magic to make the terminal code
279 * return the EOF character at all. See that code for
282 if (!LF_ISSET(TXT_AUTOINDENT
) || tp
->len
== 0)
285 case C_CARATSET
: /* ^^D */
286 if (tp
->len
> tp
->ai
+ 1)
289 /* Save the ai string for later. */
292 BINC_GOTOW(sp
, ait
.lb
, ait
.lb_len
, tp
->ai
);
293 MEMCPYW(ait
.lb
, tp
->lb
, tp
->ai
);
294 ait
.ai
= ait
.len
= tp
->ai
;
296 carat_st
= C_NOCHANGE
;
298 case C_ZEROSET
: /* 0^D */
299 if (tp
->len
> tp
->ai
+ 1)
303 leftmargin
: (void)gp
->scr_ex_adjust(sp
, EX_TERM_CE
);
304 tp
->ai
= tp
->len
= 0;
306 case C_NOTSET
: /* ^D */
307 if (tp
->len
> tp
->ai
)
310 if (txt_dent(sp
, tp
))
317 /* Clear and redisplay the line. */
318 (void)gp
->scr_ex_adjust(sp
, EX_TERM_CE
);
319 txt_prompt(sp
, tp
, prompt
, flags
);
323 * See the TXT_BEAUTIFY comment in vi/v_txt_ev.c.
325 * Silently eliminate any iscntrl() character that was
326 * not already handled specially, except for <tab> and
329 ins_ch
: if (LF_ISSET(TXT_BEAUTIFY
) && ISCNTRL(ev
.e_c
) &&
330 ev
.e_value
!= K_FORMFEED
&& ev
.e_value
!= K_TAB
)
333 tp
->lb
[tp
->len
++] = ev
.e_c
;
348 * Display the ex prompt, line number, ai characters. Characters had
349 * better be printable by the terminal driver, but that's its problem,
353 txt_prompt(SCR
*sp
, TEXT
*tp
, ARG_CHAR_T prompt
, u_int32_t flags
)
355 /* Display the prompt. */
356 if (LF_ISSET(TXT_PROMPT
))
357 (void)ex_printf(sp
, "%c", prompt
);
359 /* Display the line number. */
360 if (LF_ISSET(TXT_NUMBER
) && O_ISSET(sp
, O_NUMBER
))
361 (void)ex_printf(sp
, "%6lu ", (u_long
)tp
->lno
);
363 /* Print out autoindent string. */
364 if (LF_ISSET(TXT_AUTOINDENT
))
365 (void)ex_printf(sp
, "%.*s", (int)tp
->ai
, tp
->lb
);
371 * Handle ^D outdents.
373 * Ex version of vi/v_ntext.c:txt_dent(). See that code for the (usual)
374 * ranting and raving. This is a fair bit simpler as ^T isn't special.
377 txt_dent(SCR
*sp
, TEXT
*tp
)
380 size_t cno
, off
, scno
, spaces
, tabs
;
382 ts
= O_VAL(sp
, O_TABSTOP
);
383 sw
= O_VAL(sp
, O_SHIFTWIDTH
);
385 /* Get the current screen column. */
386 for (off
= scno
= 0; off
< tp
->len
; ++off
)
387 if (tp
->lb
[off
] == '\t')
388 scno
+= COL_OFF(scno
, ts
);
392 /* Get the previous shiftwidth column. */
397 * Since we don't know what comes before the character(s) being
398 * deleted, we have to resolve the autoindent characters . The
399 * example is a <tab>, which doesn't take up a full shiftwidth
400 * number of columns because it's preceded by <space>s. This is
401 * easy to get if the user sets shiftwidth to a value less than
402 * tabstop, and then uses ^T to indent, and ^D to outdent.
404 * Count up spaces/tabs needed to get to the target.
406 for (cno
= 0, tabs
= 0; cno
+ COL_OFF(cno
, ts
) <= scno
; ++tabs
)
407 cno
+= COL_OFF(cno
, ts
);
410 /* Make sure there's enough room. */
411 BINC_RETW(sp
, tp
->lb
, tp
->lb_len
, tabs
+ spaces
+ 1);
413 /* Adjust the final ai character count. */
414 tp
->ai
= tabs
+ spaces
;
416 /* Enter the replacement characters. */
417 for (tp
->len
= 0; tabs
> 0; --tabs
)
418 tp
->lb
[tp
->len
++] = '\t';
419 for (; spaces
> 0; --spaces
)
420 tp
->lb
[tp
->len
++] = ' ';