xref: /freebsd/contrib/nvi/ex/ex_txt.c (revision 110d525ec6188f3c9dc4f54c4bc1cced2f7184cd)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #include <sys/types.h>
13b8ba871bSPeter Wemm #include <sys/queue.h>
14f0957ccaSPeter Wemm #include <sys/time.h>
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <bitstring.h>
17b8ba871bSPeter Wemm #include <ctype.h>
18b8ba871bSPeter Wemm #include <limits.h>
19b8ba871bSPeter Wemm #include <stdio.h>
20b8ba871bSPeter Wemm #include <stdlib.h>
21b8ba871bSPeter Wemm #include <string.h>
22b8ba871bSPeter Wemm 
23b8ba871bSPeter Wemm #include "../common/common.h"
24f0957ccaSPeter Wemm #include "../vi/vi.h"
25b8ba871bSPeter Wemm 
26b8ba871bSPeter Wemm /*
27b8ba871bSPeter Wemm  * !!!
28b8ba871bSPeter Wemm  * The backslash characters was special when it preceded a newline as part of
29b8ba871bSPeter Wemm  * a substitution replacement pattern.  For example, the input ":a\<cr>" would
30b8ba871bSPeter Wemm  * failed immediately with an error, as the <cr> wasn't part of a substitution
31b8ba871bSPeter Wemm  * replacement pattern.  This implies a frightening integration of the editor
32b8ba871bSPeter Wemm  * and the parser and/or the RE engine.  There's no way I'm going to reproduce
33b8ba871bSPeter Wemm  * those semantics.
34b8ba871bSPeter Wemm  *
35b8ba871bSPeter Wemm  * So, if backslashes are special, this code inserts the backslash and the next
36b8ba871bSPeter Wemm  * character into the string, without regard for the character or the command
37b8ba871bSPeter Wemm  * being entered.  Since "\<cr>" was illegal historically (except for the one
38b8ba871bSPeter Wemm  * special case), and the command will fail eventually, no historical scripts
39b8ba871bSPeter Wemm  * should break (presuming they didn't depend on the failure mode itself or the
40b8ba871bSPeter Wemm  * characters remaining when failure occurred.
41b8ba871bSPeter Wemm  */
42b8ba871bSPeter Wemm 
43c271fa92SBaptiste Daroussin static int	txt_dent(SCR *, TEXT *);
44c271fa92SBaptiste Daroussin static void	txt_prompt(SCR *, TEXT *, ARG_CHAR_T, u_int32_t);
45b8ba871bSPeter Wemm 
46b8ba871bSPeter Wemm /*
47b8ba871bSPeter Wemm  * ex_txt --
48b8ba871bSPeter Wemm  *	Get lines from the terminal for ex.
49b8ba871bSPeter Wemm  *
50c271fa92SBaptiste Daroussin  * PUBLIC: int ex_txt(SCR *, TEXTH *, ARG_CHAR_T, u_int32_t);
51b8ba871bSPeter Wemm  */
52b8ba871bSPeter Wemm int
ex_txt(SCR * sp,TEXTH * tiqh,ARG_CHAR_T prompt,u_int32_t flags)53f0957ccaSPeter Wemm ex_txt(SCR *sp, TEXTH *tiqh, ARG_CHAR_T prompt, u_int32_t flags)
54b8ba871bSPeter Wemm {
55b8ba871bSPeter Wemm 	EVENT ev;
56b8ba871bSPeter Wemm 	GS *gp;
57b8ba871bSPeter Wemm 	TEXT ait, *ntp, *tp;
58b8ba871bSPeter Wemm 	carat_t carat_st;
59b8ba871bSPeter Wemm 	size_t cnt;
60b8ba871bSPeter Wemm 	int rval;
61f0957ccaSPeter Wemm 	int nochange;
62b8ba871bSPeter Wemm 
63b8ba871bSPeter Wemm 	rval = 0;
64b8ba871bSPeter Wemm 
65b8ba871bSPeter Wemm 	/*
66b8ba871bSPeter Wemm 	 * Get a TEXT structure with some initial buffer space, reusing the
67b8ba871bSPeter Wemm 	 * last one if it's big enough.  (All TEXT bookkeeping fields default
68b8ba871bSPeter Wemm 	 * to 0 -- text_init() handles this.)
69b8ba871bSPeter Wemm 	 */
70f0957ccaSPeter Wemm 	if (!TAILQ_EMPTY(tiqh)) {
71f0957ccaSPeter Wemm 		tp = TAILQ_FIRST(tiqh);
72f0957ccaSPeter Wemm 		if (TAILQ_NEXT(tp, q) != NULL || tp->lb_len < 32) {
73b8ba871bSPeter Wemm 			text_lfree(tiqh);
74b8ba871bSPeter Wemm 			goto newtp;
75b8ba871bSPeter Wemm 		}
76b8ba871bSPeter Wemm 		tp->len = 0;
77b8ba871bSPeter Wemm 	} else {
78b8ba871bSPeter Wemm newtp:		if ((tp = text_init(sp, NULL, 0, 32)) == NULL)
79b8ba871bSPeter Wemm 			goto err;
80f0957ccaSPeter Wemm 		TAILQ_INSERT_HEAD(tiqh, tp, q);
81b8ba871bSPeter Wemm 	}
82b8ba871bSPeter Wemm 
83b8ba871bSPeter Wemm 	/* Set the starting line number. */
84b8ba871bSPeter Wemm 	tp->lno = sp->lno + 1;
85b8ba871bSPeter Wemm 
86b8ba871bSPeter Wemm 	/*
87b8ba871bSPeter Wemm 	 * If it's a terminal, set up autoindent, put out the prompt, and
88b8ba871bSPeter Wemm 	 * set it up so we know we were suspended.  Otherwise, turn off
89b8ba871bSPeter Wemm 	 * the autoindent flag, as that requires less special casing below.
90b8ba871bSPeter Wemm 	 *
91b8ba871bSPeter Wemm 	 * XXX
92b8ba871bSPeter Wemm 	 * Historic practice is that ^Z suspended command mode (but, because
93b8ba871bSPeter Wemm 	 * it ran in cooked mode, it was unaffected by the autowrite option.)
94b8ba871bSPeter Wemm 	 * On restart, any "current" input was discarded, whether in insert
95b8ba871bSPeter Wemm 	 * mode or not, and ex was in command mode.  This code matches historic
96b8ba871bSPeter Wemm 	 * practice, but not 'cause it's easier.
97b8ba871bSPeter Wemm 	 */
98b8ba871bSPeter Wemm 	gp = sp->gp;
99b8ba871bSPeter Wemm 	if (F_ISSET(gp, G_SCRIPTED))
100b8ba871bSPeter Wemm 		LF_CLR(TXT_AUTOINDENT);
101b8ba871bSPeter Wemm 	else {
102b8ba871bSPeter Wemm 		if (LF_ISSET(TXT_AUTOINDENT)) {
103b8ba871bSPeter Wemm 			LF_SET(TXT_EOFCHAR);
104b8ba871bSPeter Wemm 			if (v_txt_auto(sp, sp->lno, NULL, 0, tp))
105b8ba871bSPeter Wemm 				goto err;
106b8ba871bSPeter Wemm 		}
107b8ba871bSPeter Wemm 		txt_prompt(sp, tp, prompt, flags);
108b8ba871bSPeter Wemm 	}
109b8ba871bSPeter Wemm 
110f0957ccaSPeter Wemm 	for (carat_st = C_NOTSET, nochange = 0;;) {
111b8ba871bSPeter Wemm 		if (v_event_get(sp, &ev, 0, 0))
112b8ba871bSPeter Wemm 			goto err;
113b8ba871bSPeter Wemm 
114b8ba871bSPeter Wemm 		/* Deal with all non-character events. */
115b8ba871bSPeter Wemm 		switch (ev.e_event) {
116b8ba871bSPeter Wemm 		case E_CHARACTER:
117b8ba871bSPeter Wemm 			break;
118b8ba871bSPeter Wemm 		case E_ERR:
119b8ba871bSPeter Wemm 			goto err;
120b8ba871bSPeter Wemm 		case E_REPAINT:
121b8ba871bSPeter Wemm 		case E_WRESIZE:
122b8ba871bSPeter Wemm 			continue;
123b8ba871bSPeter Wemm 		case E_EOF:
124b8ba871bSPeter Wemm 			rval = 1;
125b8ba871bSPeter Wemm 			/* FALLTHROUGH */
126b8ba871bSPeter Wemm 		case E_INTERRUPT:
127b8ba871bSPeter Wemm 			/*
128b8ba871bSPeter Wemm 			 * Handle EOF/SIGINT events by discarding partially
129b8ba871bSPeter Wemm 			 * entered text and returning.  EOF returns failure,
130b8ba871bSPeter Wemm 			 * E_INTERRUPT returns success.
131b8ba871bSPeter Wemm 			 */
132b8ba871bSPeter Wemm 			goto notlast;
133b8ba871bSPeter Wemm 		default:
134b8ba871bSPeter Wemm 			v_event_err(sp, &ev);
135b8ba871bSPeter Wemm 			goto notlast;
136b8ba871bSPeter Wemm 		}
137b8ba871bSPeter Wemm 
138b8ba871bSPeter Wemm 		/*
139b8ba871bSPeter Wemm 		 * Deal with character events.
140b8ba871bSPeter Wemm 		 *
141b8ba871bSPeter Wemm 		 * Check to see if the character fits into the input buffer.
142b8ba871bSPeter Wemm 		 * (Use tp->len, ignore overwrite and non-printable chars.)
143b8ba871bSPeter Wemm 		 */
144f0957ccaSPeter Wemm 		BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
145b8ba871bSPeter Wemm 
146b8ba871bSPeter Wemm 		switch (ev.e_value) {
147b8ba871bSPeter Wemm 		case K_CR:
148b8ba871bSPeter Wemm 			/*
149b8ba871bSPeter Wemm 			 * !!!
150b8ba871bSPeter Wemm 			 * Historically, <carriage-return>'s in the command
151b8ba871bSPeter Wemm 			 * weren't special, so the ex parser would return an
152b8ba871bSPeter Wemm 			 * unknown command error message.  However, if they
153b8ba871bSPeter Wemm 			 * terminated the command if they were in a map.  I'm
154b8ba871bSPeter Wemm 			 * pretty sure this still isn't right, but it handles
155b8ba871bSPeter Wemm 			 * what I've seen so far.
156b8ba871bSPeter Wemm 			 */
157b8ba871bSPeter Wemm 			if (!F_ISSET(&ev.e_ch, CH_MAPPED))
158b8ba871bSPeter Wemm 				goto ins_ch;
159b8ba871bSPeter Wemm 			/* FALLTHROUGH */
160b8ba871bSPeter Wemm 		case K_NL:
161b8ba871bSPeter Wemm 			/*
162b8ba871bSPeter Wemm 			 * '\' can escape <carriage-return>/<newline>.  We
163b8ba871bSPeter Wemm 			 * don't discard the backslash because we need it
164b8ba871bSPeter Wemm 			 * to get the <newline> through the ex parser.
165b8ba871bSPeter Wemm 			 */
166b8ba871bSPeter Wemm 			if (LF_ISSET(TXT_BACKSLASH) &&
167b8ba871bSPeter Wemm 			    tp->len != 0 && tp->lb[tp->len - 1] == '\\')
168b8ba871bSPeter Wemm 				goto ins_ch;
169b8ba871bSPeter Wemm 
170b8ba871bSPeter Wemm 			/*
171b8ba871bSPeter Wemm 			 * CR returns from the ex command line.
172b8ba871bSPeter Wemm 			 *
173b8ba871bSPeter Wemm 			 * XXX
174b8ba871bSPeter Wemm 			 * Terminate with a nul, needed by filter.
175b8ba871bSPeter Wemm 			 */
176b8ba871bSPeter Wemm 			if (LF_ISSET(TXT_CR)) {
177b8ba871bSPeter Wemm 				tp->lb[tp->len] = '\0';
178b8ba871bSPeter Wemm 				goto done;
179b8ba871bSPeter Wemm 			}
180b8ba871bSPeter Wemm 
181b8ba871bSPeter Wemm 			/*
182b8ba871bSPeter Wemm 			 * '.' may terminate text input mode; free the current
183b8ba871bSPeter Wemm 			 * TEXT.
184b8ba871bSPeter Wemm 			 */
185b8ba871bSPeter Wemm 			if (LF_ISSET(TXT_DOTTERM) && tp->len == tp->ai + 1 &&
186b8ba871bSPeter Wemm 			    tp->lb[tp->len - 1] == '.') {
187f0957ccaSPeter Wemm notlast:			TAILQ_REMOVE(tiqh, tp, q);
188b8ba871bSPeter Wemm 				text_free(tp);
189b8ba871bSPeter Wemm 				goto done;
190b8ba871bSPeter Wemm 			}
191b8ba871bSPeter Wemm 
192b8ba871bSPeter Wemm 			/* Set up bookkeeping for the new line. */
193b8ba871bSPeter Wemm 			if ((ntp = text_init(sp, NULL, 0, 32)) == NULL)
194b8ba871bSPeter Wemm 				goto err;
195b8ba871bSPeter Wemm 			ntp->lno = tp->lno + 1;
196b8ba871bSPeter Wemm 
197b8ba871bSPeter Wemm 			/*
198b8ba871bSPeter Wemm 			 * Reset the autoindent line value.  0^D keeps the ai
199b8ba871bSPeter Wemm 			 * line from changing, ^D changes the level, even if
200b8ba871bSPeter Wemm 			 * there were no characters in the old line.  Note, if
201b8ba871bSPeter Wemm 			 * using the current tp structure, use the cursor as
202b8ba871bSPeter Wemm 			 * the length, the autoindent characters may have been
203b8ba871bSPeter Wemm 			 * erased.
204b8ba871bSPeter Wemm 			 */
205b8ba871bSPeter Wemm 			if (LF_ISSET(TXT_AUTOINDENT)) {
206f0957ccaSPeter Wemm 				if (nochange) {
207f0957ccaSPeter Wemm 					nochange = 0;
208b8ba871bSPeter Wemm 					if (v_txt_auto(sp,
209b8ba871bSPeter Wemm 					    OOBLNO, &ait, ait.ai, ntp))
210b8ba871bSPeter Wemm 						goto err;
211b8ba871bSPeter Wemm 					free(ait.lb);
212b8ba871bSPeter Wemm 				} else
213b8ba871bSPeter Wemm 					if (v_txt_auto(sp,
214b8ba871bSPeter Wemm 					    OOBLNO, tp, tp->len, ntp))
215b8ba871bSPeter Wemm 						goto err;
216b8ba871bSPeter Wemm 				carat_st = C_NOTSET;
217b8ba871bSPeter Wemm 			}
218b8ba871bSPeter Wemm 			txt_prompt(sp, ntp, prompt, flags);
219b8ba871bSPeter Wemm 
220b8ba871bSPeter Wemm 			/*
221b8ba871bSPeter Wemm 			 * Swap old and new TEXT's, and insert the new TEXT
222b8ba871bSPeter Wemm 			 * into the queue.
223b8ba871bSPeter Wemm 			 */
224b8ba871bSPeter Wemm 			tp = ntp;
225f0957ccaSPeter Wemm 			TAILQ_INSERT_TAIL(tiqh, tp, q);
226b8ba871bSPeter Wemm 			break;
227b8ba871bSPeter Wemm 		case K_CARAT:			/* Delete autoindent chars. */
228b8ba871bSPeter Wemm 			if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
229b8ba871bSPeter Wemm 				carat_st = C_CARATSET;
230b8ba871bSPeter Wemm 			goto ins_ch;
231b8ba871bSPeter Wemm 		case K_ZERO:			/* Delete autoindent chars. */
232b8ba871bSPeter Wemm 			if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
233b8ba871bSPeter Wemm 				carat_st = C_ZEROSET;
234b8ba871bSPeter Wemm 			goto ins_ch;
235b8ba871bSPeter Wemm 		case K_CNTRLD:			/* Delete autoindent char. */
236b8ba871bSPeter Wemm 			/*
237b8ba871bSPeter Wemm 			 * !!!
238b8ba871bSPeter Wemm 			 * Historically, the ^D command took (but then ignored)
239b8ba871bSPeter Wemm 			 * a count.  For simplicity, we don't return it unless
240b8ba871bSPeter Wemm 			 * it's the first character entered.  The check for len
241b8ba871bSPeter Wemm 			 * equal to 0 is okay, TXT_AUTOINDENT won't be set.
242b8ba871bSPeter Wemm 			 */
243b8ba871bSPeter Wemm 			if (LF_ISSET(TXT_CNTRLD)) {
244b8ba871bSPeter Wemm 				for (cnt = 0; cnt < tp->len; ++cnt)
245b8ba871bSPeter Wemm 					if (!isblank(tp->lb[cnt]))
246b8ba871bSPeter Wemm 						break;
247b8ba871bSPeter Wemm 				if (cnt == tp->len) {
248b8ba871bSPeter Wemm 					tp->len = 1;
249b8ba871bSPeter Wemm 					tp->lb[0] = ev.e_c;
250b8ba871bSPeter Wemm 					tp->lb[1] = '\0';
251b8ba871bSPeter Wemm 
252b8ba871bSPeter Wemm 					/*
253b8ba871bSPeter Wemm 					 * Put out a line separator, in case
254b8ba871bSPeter Wemm 					 * the command fails.
255b8ba871bSPeter Wemm 					 */
256b8ba871bSPeter Wemm 					(void)putchar('\n');
257b8ba871bSPeter Wemm 					goto done;
258b8ba871bSPeter Wemm 				}
259b8ba871bSPeter Wemm 			}
260b8ba871bSPeter Wemm 
261b8ba871bSPeter Wemm 			/*
262b8ba871bSPeter Wemm 			 * POSIX 1003.1b-1993, paragraph 7.1.1.9, states that
263b8ba871bSPeter Wemm 			 * the EOF characters are discarded if there are other
264b8ba871bSPeter Wemm 			 * characters to process in the line, i.e. if the EOF
265b8ba871bSPeter Wemm 			 * is not the first character in the line.  For this
266b8ba871bSPeter Wemm 			 * reason, historic ex discarded the EOF characters,
267b8ba871bSPeter Wemm 			 * even if occurring in the middle of the input line.
268b8ba871bSPeter Wemm 			 * We match that historic practice.
269b8ba871bSPeter Wemm 			 *
270b8ba871bSPeter Wemm 			 * !!!
271b8ba871bSPeter Wemm 			 * The test for discarding in the middle of the line is
272b8ba871bSPeter Wemm 			 * done in the switch, because the CARAT forms are N+1,
273b8ba871bSPeter Wemm 			 * not N.
274b8ba871bSPeter Wemm 			 *
275b8ba871bSPeter Wemm 			 * !!!
276b8ba871bSPeter Wemm 			 * There's considerable magic to make the terminal code
277b8ba871bSPeter Wemm 			 * return the EOF character at all.  See that code for
278b8ba871bSPeter Wemm 			 * details.
279b8ba871bSPeter Wemm 			 */
280b8ba871bSPeter Wemm 			if (!LF_ISSET(TXT_AUTOINDENT) || tp->len == 0)
281b8ba871bSPeter Wemm 				continue;
282b8ba871bSPeter Wemm 			switch (carat_st) {
283b8ba871bSPeter Wemm 			case C_CARATSET:		/* ^^D */
284b8ba871bSPeter Wemm 				if (tp->len > tp->ai + 1)
285b8ba871bSPeter Wemm 					continue;
286b8ba871bSPeter Wemm 
287b8ba871bSPeter Wemm 				/* Save the ai string for later. */
288b8ba871bSPeter Wemm 				ait.lb = NULL;
289b8ba871bSPeter Wemm 				ait.lb_len = 0;
290f0957ccaSPeter Wemm 				BINC_GOTOW(sp, ait.lb, ait.lb_len, tp->ai);
291f0957ccaSPeter Wemm 				MEMCPY(ait.lb, tp->lb, tp->ai);
292b8ba871bSPeter Wemm 				ait.ai = ait.len = tp->ai;
293b8ba871bSPeter Wemm 
294f0957ccaSPeter Wemm 				carat_st = C_NOTSET;
295f0957ccaSPeter Wemm 				nochange = 1;
296b8ba871bSPeter Wemm 				goto leftmargin;
297b8ba871bSPeter Wemm 			case C_ZEROSET:			/* 0^D */
298b8ba871bSPeter Wemm 				if (tp->len > tp->ai + 1)
299b8ba871bSPeter Wemm 					continue;
300b8ba871bSPeter Wemm 
301b8ba871bSPeter Wemm 				carat_st = C_NOTSET;
302b8ba871bSPeter Wemm leftmargin:			(void)gp->scr_ex_adjust(sp, EX_TERM_CE);
303b8ba871bSPeter Wemm 				tp->ai = tp->len = 0;
304b8ba871bSPeter Wemm 				break;
305b8ba871bSPeter Wemm 			case C_NOTSET:			/* ^D */
306b8ba871bSPeter Wemm 				if (tp->len > tp->ai)
307b8ba871bSPeter Wemm 					continue;
308b8ba871bSPeter Wemm 
309b8ba871bSPeter Wemm 				if (txt_dent(sp, tp))
310b8ba871bSPeter Wemm 					goto err;
311b8ba871bSPeter Wemm 				break;
312b8ba871bSPeter Wemm 			default:
313b8ba871bSPeter Wemm 				abort();
314b8ba871bSPeter Wemm 			}
315b8ba871bSPeter Wemm 
316b8ba871bSPeter Wemm 			/* Clear and redisplay the line. */
317b8ba871bSPeter Wemm 			(void)gp->scr_ex_adjust(sp, EX_TERM_CE);
318b8ba871bSPeter Wemm 			txt_prompt(sp, tp, prompt, flags);
319b8ba871bSPeter Wemm 			break;
320b8ba871bSPeter Wemm 		default:
321b8ba871bSPeter Wemm 			/*
322b8ba871bSPeter Wemm 			 * See the TXT_BEAUTIFY comment in vi/v_txt_ev.c.
323b8ba871bSPeter Wemm 			 *
324b8ba871bSPeter Wemm 			 * Silently eliminate any iscntrl() character that was
325b8ba871bSPeter Wemm 			 * not already handled specially, except for <tab> and
326b8ba871bSPeter Wemm 			 * <ff>.
327b8ba871bSPeter Wemm 			 */
328f0957ccaSPeter Wemm ins_ch:			if (LF_ISSET(TXT_BEAUTIFY) && ISCNTRL(ev.e_c) &&
329b8ba871bSPeter Wemm 			    ev.e_value != K_FORMFEED && ev.e_value != K_TAB)
330b8ba871bSPeter Wemm 				break;
331b8ba871bSPeter Wemm 
332b8ba871bSPeter Wemm 			tp->lb[tp->len++] = ev.e_c;
333b8ba871bSPeter Wemm 			break;
334b8ba871bSPeter Wemm 		}
335b8ba871bSPeter Wemm 	}
336b8ba871bSPeter Wemm 	/* NOTREACHED */
337b8ba871bSPeter Wemm 
338b8ba871bSPeter Wemm done:	return (rval);
339b8ba871bSPeter Wemm 
340b8ba871bSPeter Wemm err:
341b8ba871bSPeter Wemm alloc_err:
342b8ba871bSPeter Wemm 	return (1);
343b8ba871bSPeter Wemm }
344b8ba871bSPeter Wemm 
345b8ba871bSPeter Wemm /*
346b8ba871bSPeter Wemm  * txt_prompt --
347b8ba871bSPeter Wemm  *	Display the ex prompt, line number, ai characters.  Characters had
348b8ba871bSPeter Wemm  *	better be printable by the terminal driver, but that's its problem,
349b8ba871bSPeter Wemm  *	not ours.
350b8ba871bSPeter Wemm  */
351b8ba871bSPeter Wemm static void
txt_prompt(SCR * sp,TEXT * tp,ARG_CHAR_T prompt,u_int32_t flags)352f0957ccaSPeter Wemm txt_prompt(SCR *sp, TEXT *tp, ARG_CHAR_T prompt, u_int32_t flags)
353b8ba871bSPeter Wemm {
354b8ba871bSPeter Wemm 	/* Display the prompt. */
355b8ba871bSPeter Wemm 	if (LF_ISSET(TXT_PROMPT))
356f0957ccaSPeter Wemm 		(void)ex_printf(sp, "%c", prompt);
357b8ba871bSPeter Wemm 
358b8ba871bSPeter Wemm 	/* Display the line number. */
359b8ba871bSPeter Wemm 	if (LF_ISSET(TXT_NUMBER) && O_ISSET(sp, O_NUMBER))
360f0957ccaSPeter Wemm 		(void)ex_printf(sp, "%6lu  ", (u_long)tp->lno);
361b8ba871bSPeter Wemm 
362b8ba871bSPeter Wemm 	/* Print out autoindent string. */
363b8ba871bSPeter Wemm 	if (LF_ISSET(TXT_AUTOINDENT))
364f0957ccaSPeter Wemm 		(void)ex_printf(sp, WVS, (int)tp->ai, tp->lb);
365f0957ccaSPeter Wemm 	(void)ex_fflush(sp);
366b8ba871bSPeter Wemm }
367b8ba871bSPeter Wemm 
368b8ba871bSPeter Wemm /*
369b8ba871bSPeter Wemm  * txt_dent --
370b8ba871bSPeter Wemm  *	Handle ^D outdents.
371b8ba871bSPeter Wemm  *
372b8ba871bSPeter Wemm  * Ex version of vi/v_ntext.c:txt_dent().  See that code for the (usual)
373b8ba871bSPeter Wemm  * ranting and raving.  This is a fair bit simpler as ^T isn't special.
374b8ba871bSPeter Wemm  */
375b8ba871bSPeter Wemm static int
txt_dent(SCR * sp,TEXT * tp)376f0957ccaSPeter Wemm txt_dent(SCR *sp, TEXT *tp)
377b8ba871bSPeter Wemm {
378b8ba871bSPeter Wemm 	u_long sw, ts;
379b8ba871bSPeter Wemm 	size_t cno, off, scno, spaces, tabs;
380b8ba871bSPeter Wemm 
381b8ba871bSPeter Wemm 	ts = O_VAL(sp, O_TABSTOP);
382b8ba871bSPeter Wemm 	sw = O_VAL(sp, O_SHIFTWIDTH);
383b8ba871bSPeter Wemm 
384b8ba871bSPeter Wemm 	/* Get the current screen column. */
385b8ba871bSPeter Wemm 	for (off = scno = 0; off < tp->len; ++off)
386b8ba871bSPeter Wemm 		if (tp->lb[off] == '\t')
387b8ba871bSPeter Wemm 			scno += COL_OFF(scno, ts);
388b8ba871bSPeter Wemm 		else
389b8ba871bSPeter Wemm 			++scno;
390b8ba871bSPeter Wemm 
391b8ba871bSPeter Wemm 	/* Get the previous shiftwidth column. */
392d21f31a1SDimitry Andric 	cno = scno--;
393d21f31a1SDimitry Andric 	scno -= scno % sw;
394b8ba871bSPeter Wemm 
395b8ba871bSPeter Wemm 	/*
396b8ba871bSPeter Wemm 	 * Since we don't know what comes before the character(s) being
397b8ba871bSPeter Wemm 	 * deleted, we have to resolve the autoindent characters .  The
398b8ba871bSPeter Wemm 	 * example is a <tab>, which doesn't take up a full shiftwidth
399b8ba871bSPeter Wemm 	 * number of columns because it's preceded by <space>s.  This is
400b8ba871bSPeter Wemm 	 * easy to get if the user sets shiftwidth to a value less than
401b8ba871bSPeter Wemm 	 * tabstop, and then uses ^T to indent, and ^D to outdent.
402b8ba871bSPeter Wemm 	 *
403b8ba871bSPeter Wemm 	 * Count up spaces/tabs needed to get to the target.
404b8ba871bSPeter Wemm 	 */
405*110d525eSBaptiste Daroussin 	cno = 0;
406*110d525eSBaptiste Daroussin 	tabs = 0;
407*110d525eSBaptiste Daroussin 	if (!O_ISSET(sp, O_EXPANDTAB)) {
408*110d525eSBaptiste Daroussin 		for (; cno + COL_OFF(cno, ts) <= scno; ++tabs)
409b8ba871bSPeter Wemm 			cno += COL_OFF(cno, ts);
410*110d525eSBaptiste Daroussin 	}
411b8ba871bSPeter Wemm 	spaces = scno - cno;
412b8ba871bSPeter Wemm 
413b8ba871bSPeter Wemm 	/* Make sure there's enough room. */
414f0957ccaSPeter Wemm 	BINC_RETW(sp, tp->lb, tp->lb_len, tabs + spaces + 1);
415b8ba871bSPeter Wemm 
416b8ba871bSPeter Wemm 	/* Adjust the final ai character count. */
417b8ba871bSPeter Wemm 	tp->ai = tabs + spaces;
418b8ba871bSPeter Wemm 
419b8ba871bSPeter Wemm 	/* Enter the replacement characters. */
420b8ba871bSPeter Wemm 	for (tp->len = 0; tabs > 0; --tabs)
421b8ba871bSPeter Wemm 		tp->lb[tp->len++] = '\t';
422b8ba871bSPeter Wemm 	for (; spaces > 0; --spaces)
423b8ba871bSPeter Wemm 		tp->lb[tp->len++] = ' ';
424b8ba871bSPeter Wemm 	return (0);
425b8ba871bSPeter Wemm }
426