Add preliminary db1.c from Aymeric Vincent <xmimic@free.fr>
[nvi.git] / ex / ex_txt.c
blob410d984f942406396b5284e794f4575c18fa83ac
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "../vi/vi.h"
31 * !!!
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
37 * those semantics.
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));
51 * ex_txt --
52 * Get lines from the terminal for ex.
54 * PUBLIC: int ex_txt __P((SCR *, TEXTH *, ARG_CHAR_T, u_int32_t));
56 int
57 ex_txt(SCR *sp, TEXTH *tiqh, ARG_CHAR_T prompt, u_int32_t flags)
59 EVENT ev;
60 GS *gp;
61 TEXT ait, *ntp, *tp;
62 carat_t carat_st;
63 size_t cnt;
64 int rval;
66 rval = 0;
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) {
74 tp = tiqh->cqh_first;
75 if (tp->q.cqe_next != (void *)tiqh || tp->lb_len < 32) {
76 text_lfree(tiqh);
77 goto newtp;
79 tp->len = 0;
80 } else {
81 newtp: if ((tp = text_init(sp, NULL, 0, 32)) == NULL)
82 goto err;
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.
94 * XXX
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.
101 gp = sp->gp;
102 if (F_ISSET(gp, G_SCRIPTED))
103 LF_CLR(TXT_AUTOINDENT);
104 else {
105 if (LF_ISSET(TXT_AUTOINDENT)) {
106 LF_SET(TXT_EOFCHAR);
107 if (v_txt_auto(sp, sp->lno, NULL, 0, tp))
108 goto err;
110 txt_prompt(sp, tp, prompt, flags);
113 for (carat_st = C_NOTSET;;) {
114 if (v_event_get(sp, &ev, 0, 0))
115 goto err;
117 /* Deal with all non-character events. */
118 switch (ev.e_event) {
119 case E_CHARACTER:
120 break;
121 case E_ERR:
122 goto err;
123 case E_REPAINT:
124 case E_WRESIZE:
125 continue;
126 case E_EOF:
127 rval = 1;
128 /* FALLTHROUGH */
129 case E_INTERRUPT:
131 * Handle EOF/SIGINT events by discarding partially
132 * entered text and returning. EOF returns failure,
133 * E_INTERRUPT returns success.
135 goto notlast;
136 default:
137 v_event_err(sp, &ev);
138 goto notlast;
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) {
150 case K_CR:
152 * !!!
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))
161 goto ins_ch;
162 /* FALLTHROUGH */
163 case K_NL:
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] == '\\')
171 goto ins_ch;
174 * CR returns from the ex command line.
176 * XXX
177 * Terminate with a nul, needed by filter.
179 if (LF_ISSET(TXT_CR)) {
180 tp->lb[tp->len] = '\0';
181 goto done;
185 * '.' may terminate text input mode; free the current
186 * TEXT.
188 if (LF_ISSET(TXT_DOTTERM) && tp->len == tp->ai + 1 &&
189 tp->lb[tp->len - 1] == '.') {
190 notlast: CIRCLEQ_REMOVE(tiqh, tp, q);
191 text_free(tp);
192 goto done;
195 /* Set up bookkeeping for the new line. */
196 if ((ntp = text_init(sp, NULL, 0, 32)) == NULL)
197 goto err;
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
206 * erased.
208 if (LF_ISSET(TXT_AUTOINDENT)) {
209 if (carat_st == C_NOCHANGE) {
210 if (v_txt_auto(sp,
211 OOBLNO, &ait, ait.ai, ntp))
212 goto err;
213 free(ait.lb);
214 } else
215 if (v_txt_auto(sp,
216 OOBLNO, tp, tp->len, ntp))
217 goto err;
218 carat_st = C_NOTSET;
220 txt_prompt(sp, ntp, prompt, flags);
223 * Swap old and new TEXT's, and insert the new TEXT
224 * into the queue.
226 tp = ntp;
227 CIRCLEQ_INSERT_TAIL(tiqh, tp, q);
228 break;
229 case K_CARAT: /* Delete autoindent chars. */
230 if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
231 carat_st = C_CARATSET;
232 goto ins_ch;
233 case K_ZERO: /* Delete autoindent chars. */
234 if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
235 carat_st = C_ZEROSET;
236 goto ins_ch;
237 case K_CNTRLD: /* Delete autoindent char. */
239 * !!!
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]))
248 break;
249 if (cnt == tp->len) {
250 tp->len = 1;
251 tp->lb[0] = ev.e_c;
252 tp->lb[1] = '\0';
255 * Put out a line separator, in case
256 * the command fails.
258 (void)putchar('\n');
259 goto done;
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.
272 * !!!
273 * The test for discarding in the middle of the line is
274 * done in the switch, because the CARAT forms are N+1,
275 * not N.
277 * !!!
278 * There's considerable magic to make the terminal code
279 * return the EOF character at all. See that code for
280 * details.
282 if (!LF_ISSET(TXT_AUTOINDENT) || tp->len == 0)
283 continue;
284 switch (carat_st) {
285 case C_CARATSET: /* ^^D */
286 if (tp->len > tp->ai + 1)
287 continue;
289 /* Save the ai string for later. */
290 ait.lb = NULL;
291 ait.lb_len = 0;
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;
297 goto leftmargin;
298 case C_ZEROSET: /* 0^D */
299 if (tp->len > tp->ai + 1)
300 continue;
302 carat_st = C_NOTSET;
303 leftmargin: (void)gp->scr_ex_adjust(sp, EX_TERM_CE);
304 tp->ai = tp->len = 0;
305 break;
306 case C_NOTSET: /* ^D */
307 if (tp->len > tp->ai)
308 continue;
310 if (txt_dent(sp, tp))
311 goto err;
312 break;
313 default:
314 abort();
317 /* Clear and redisplay the line. */
318 (void)gp->scr_ex_adjust(sp, EX_TERM_CE);
319 txt_prompt(sp, tp, prompt, flags);
320 break;
321 default:
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
327 * <ff>.
329 ins_ch: if (LF_ISSET(TXT_BEAUTIFY) && ISCNTRL(ev.e_c) &&
330 ev.e_value != K_FORMFEED && ev.e_value != K_TAB)
331 break;
333 tp->lb[tp->len++] = ev.e_c;
334 break;
337 /* NOTREACHED */
339 done: return (rval);
341 err:
342 alloc_err:
343 return (1);
347 * txt_prompt --
348 * Display the ex prompt, line number, ai characters. Characters had
349 * better be printable by the terminal driver, but that's its problem,
350 * not ours.
352 static void
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);
366 (void)ex_fflush(sp);
370 * txt_dent --
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.
376 static int
377 txt_dent(SCR *sp, TEXT *tp)
379 u_long sw, ts;
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);
389 else
390 ++scno;
392 /* Get the previous shiftwidth column. */
393 cno = scno;
394 scno -= --scno % sw;
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);
408 spaces = scno - cno;
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++] = ' ';
421 return (0);