xref: /freebsd/contrib/nvi/vi/v_itxt.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "../common/common.h"
25 #include "vi.h"
26 
27 /*
28  * !!!
29  * Repeated input in the historic vi is mostly wrong and this isn't very
30  * backward compatible.  For example, if the user entered "3Aab\ncd" in
31  * the historic vi, the "ab" was repeated 3 times, and the "\ncd" was then
32  * appended to the result.  There was also a hack which I don't remember
33  * right now, where "3o" would open 3 lines and then let the user fill them
34  * in, to make screen movements on 300 baud modems more tolerable.  I don't
35  * think it's going to be missed.
36  *
37  * !!!
38  * There's a problem with the way that we do logging for change commands with
39  * implied motions (e.g. A, I, O, cc, etc.).  Since the main vi loop logs the
40  * starting cursor position before the change command "moves" the cursor, the
41  * cursor position to which we return on undo will be where the user entered
42  * the change command, not the start of the change.  Several of the following
43  * routines re-log the cursor to make this work correctly.  Historic vi tried
44  * to do the same thing, and mostly got it right.  (The only spectacular way
45  * it fails is if the user entered 'o' from anywhere but the last character of
46  * the line, the undo returned the cursor to the start of the line.  If the
47  * user was on the last character of the line, the cursor returned to that
48  * position.)  We also check for mapped keys waiting, i.e. if we're in the
49  * middle of a map, don't bother logging the cursor.
50  */
51 #define	LOG_CORRECT do {						\
52 	if (!MAPPED_KEYS_WAITING(sp))					\
53 		(void)log_cursor(sp);					\
54 } while (0)
55 
56 static u_int32_t set_txt_std(SCR *, VICMD *, u_int32_t);
57 
58 /*
59  * v_iA -- [count]A
60  *	Append text to the end of the line.
61  *
62  * PUBLIC: int v_iA(SCR *, VICMD *);
63  */
64 int
65 v_iA(SCR *sp, VICMD *vp)
66 {
67 	size_t len;
68 
69 	if (!db_get(sp, vp->m_start.lno, 0, NULL, &len))
70 		sp->cno = len == 0 ? 0 : len - 1;
71 
72 	LOG_CORRECT;
73 
74 	return (v_ia(sp, vp));
75 }
76 
77 /*
78  * v_ia -- [count]a
79  *	   [count]A
80  *	Append text to the cursor position.
81  *
82  * PUBLIC: int v_ia(SCR *, VICMD *);
83  */
84 int
85 v_ia(SCR *sp, VICMD *vp)
86 {
87 	size_t len;
88 	u_int32_t flags;
89 	int isempty;
90 	CHAR_T *p;
91 
92 	flags = set_txt_std(sp, vp, 0);
93 	sp->showmode = SM_APPEND;
94 	sp->lno = vp->m_start.lno;
95 
96 	/* Move the cursor one column to the right and repaint the screen. */
97 	if (db_eget(sp, sp->lno, &p, &len, &isempty)) {
98 		if (!isempty)
99 			return (1);
100 		len = 0;
101 		LF_SET(TXT_APPENDEOL);
102 	} else if (len) {
103 		if (len == sp->cno + 1) {
104 			sp->cno = len;
105 			LF_SET(TXT_APPENDEOL);
106 		} else
107 			++sp->cno;
108 	} else
109 		LF_SET(TXT_APPENDEOL);
110 
111 	return (v_txt(sp, vp, NULL, p, len,
112 	    0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
113 }
114 
115 /*
116  * v_iI -- [count]I
117  *	Insert text at the first nonblank.
118  *
119  * PUBLIC: int v_iI(SCR *, VICMD *);
120  */
121 int
122 v_iI(SCR *sp, VICMD *vp)
123 {
124 	sp->cno = 0;
125 	if (nonblank(sp, vp->m_start.lno, &sp->cno))
126 		return (1);
127 
128 	LOG_CORRECT;
129 
130 	return (v_ii(sp, vp));
131 }
132 
133 /*
134  * v_ii -- [count]i
135  *	   [count]I
136  *	Insert text at the cursor position.
137  *
138  * PUBLIC: int v_ii(SCR *, VICMD *);
139  */
140 int
141 v_ii(SCR *sp, VICMD *vp)
142 {
143 	size_t len;
144 	u_int32_t flags;
145 	int isempty;
146 	CHAR_T *p;
147 
148 	flags = set_txt_std(sp, vp, 0);
149 	sp->showmode = SM_INSERT;
150 	sp->lno = vp->m_start.lno;
151 
152 	if (db_eget(sp, sp->lno, &p, &len, &isempty)) {
153 		if (!isempty)
154 			return (1);
155 		len = 0;
156 	}
157 
158 	if (len == 0)
159 		LF_SET(TXT_APPENDEOL);
160 	return (v_txt(sp, vp, NULL, p, len,
161 	    0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
162 }
163 
164 enum which { o_cmd, O_cmd };
165 static int io(SCR *, VICMD *, enum which);
166 
167 /*
168  * v_iO -- [count]O
169  *	Insert text above this line.
170  *
171  * PUBLIC: int v_iO(SCR *, VICMD *);
172  */
173 int
174 v_iO(SCR *sp, VICMD *vp)
175 {
176 	return (io(sp, vp, O_cmd));
177 }
178 
179 /*
180  * v_io -- [count]o
181  *	Insert text after this line.
182  *
183  * PUBLIC: int v_io(SCR *, VICMD *);
184  */
185 int
186 v_io(SCR *sp, VICMD *vp)
187 {
188 	return (io(sp, vp, o_cmd));
189 }
190 
191 static int
192 io(SCR *sp, VICMD *vp, enum which cmd)
193 {
194 	recno_t ai_line, lno;
195 	size_t len;
196 	u_int32_t flags;
197 	CHAR_T *p;
198 
199 	flags = set_txt_std(sp, vp, TXT_ADDNEWLINE | TXT_APPENDEOL);
200 	sp->showmode = SM_INSERT;
201 
202 	if (sp->lno == 1) {
203 		if (db_last(sp, &lno))
204 			return (1);
205 		if (lno != 0)
206 			goto insert;
207 		p = NULL;
208 		len = 0;
209 		ai_line = OOBLNO;
210 	} else {
211 insert:		p = L("");
212 		sp->cno = 0;
213 		LOG_CORRECT;
214 
215 		if (cmd == O_cmd) {
216 			if (db_insert(sp, sp->lno, p, 0))
217 				return (1);
218 			if (db_get(sp, sp->lno, DBG_FATAL, &p, &len))
219 				return (1);
220 			ai_line = sp->lno + 1;
221 		} else {
222 			if (db_append(sp, 1, sp->lno, p, 0))
223 				return (1);
224 			if (db_get(sp, ++sp->lno, DBG_FATAL, &p, &len))
225 				return (1);
226 			ai_line = sp->lno - 1;
227 		}
228 	}
229 	return (v_txt(sp, vp, NULL, p, len,
230 	    0, ai_line, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
231 }
232 
233 /*
234  * v_change -- [buffer][count]c[count]motion
235  *	       [buffer][count]C
236  *	       [buffer][count]S
237  *	Change command.
238  *
239  * PUBLIC: int v_change(SCR *, VICMD *);
240  */
241 int
242 v_change(SCR *sp, VICMD *vp)
243 {
244 	size_t blen, len;
245 	u_int32_t flags;
246 	int isempty, lmode, rval;
247 	CHAR_T *bp;
248 	CHAR_T *p;
249 
250 	/*
251 	 * 'c' can be combined with motion commands that set the resulting
252 	 * cursor position, i.e. "cG".  Clear the VM_RCM flags and make the
253 	 * resulting cursor position stick, inserting text has its own rules
254 	 * for cursor positioning.
255 	 */
256 	F_CLR(vp, VM_RCM_MASK);
257 	F_SET(vp, VM_RCM_SET);
258 
259 	/*
260 	 * Find out if the file is empty, it's easier to handle it as a
261 	 * special case.
262 	 */
263 	if (vp->m_start.lno == vp->m_stop.lno &&
264 	    db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
265 		if (!isempty)
266 			return (1);
267 		return (v_ia(sp, vp));
268 	}
269 
270 	flags = set_txt_std(sp, vp, 0);
271 	sp->showmode = SM_CHANGE;
272 
273 	/*
274 	 * Move the cursor to the start of the change.  Note, if autoindent
275 	 * is turned on, the cc command in line mode changes from the first
276 	 * *non-blank* character of the line, not the first character.  And,
277 	 * to make it just a bit more exciting, the initial space is handled
278 	 * as auto-indent characters.
279 	 */
280 	lmode = F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0;
281 	if (lmode) {
282 		vp->m_start.cno = 0;
283 		if (O_ISSET(sp, O_AUTOINDENT)) {
284 			if (nonblank(sp, vp->m_start.lno, &vp->m_start.cno))
285 				return (1);
286 			LF_SET(TXT_AICHARS);
287 		}
288 	}
289 	sp->lno = vp->m_start.lno;
290 	sp->cno = vp->m_start.cno;
291 
292 	LOG_CORRECT;
293 
294 	/*
295 	 * If not in line mode and changing within a single line, copy the
296 	 * text and overwrite it.
297 	 */
298 	if (!lmode && vp->m_start.lno == vp->m_stop.lno) {
299 		/*
300 		 * !!!
301 		 * Historic practice, c did not cut into the numeric buffers,
302 		 * only the unnamed one.
303 		 */
304 		if (cut(sp,
305 		    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
306 		    &vp->m_start, &vp->m_stop, lmode))
307 			return (1);
308 		if (len == 0)
309 			LF_SET(TXT_APPENDEOL);
310 		LF_SET(TXT_EMARK | TXT_OVERWRITE);
311 		return (v_txt(sp, vp, &vp->m_stop, p, len,
312 		    0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
313 	}
314 
315 	/*
316 	 * It's trickier if in line mode or changing over multiple lines.  If
317 	 * we're in line mode delete all of the lines and insert a replacement
318 	 * line which the user edits.  If there was leading whitespace in the
319 	 * first line being changed, we copy it and use it as the replacement.
320 	 * If we're not in line mode, we delete the text and start inserting.
321 	 *
322 	 * !!!
323 	 * Copy the text.  Historic practice, c did not cut into the numeric
324 	 * buffers, only the unnamed one.
325 	 */
326 	if (cut(sp,
327 	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
328 	    &vp->m_start, &vp->m_stop, lmode))
329 		return (1);
330 
331 	/* If replacing entire lines and there's leading text. */
332 	if (lmode && vp->m_start.cno) {
333 		/*
334 		 * Get a copy of the first line changed, and copy out the
335 		 * leading text.
336 		 */
337 		if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len))
338 			return (1);
339 		GET_SPACE_RETW(sp, bp, blen, vp->m_start.cno);
340 		MEMMOVE(bp, p, vp->m_start.cno);
341 	} else
342 		bp = NULL;
343 
344 	/* Delete the text. */
345 	if (del(sp, &vp->m_start, &vp->m_stop, lmode))
346 		return (1);
347 
348 	/* If replacing entire lines, insert a replacement line. */
349 	if (lmode) {
350 		if (db_insert(sp, vp->m_start.lno, bp, vp->m_start.cno))
351 			return (1);
352 		sp->lno = vp->m_start.lno;
353 		len = sp->cno = vp->m_start.cno;
354 	}
355 
356 	/* Get the line we're editing. */
357 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
358 		if (!isempty)
359 			return (1);
360 		len = 0;
361 	}
362 
363 	/* Check to see if we're appending to the line. */
364 	if (vp->m_start.cno >= len)
365 		LF_SET(TXT_APPENDEOL);
366 
367 	rval = v_txt(sp, vp, NULL, p, len,
368 	    0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags);
369 
370 	if (bp != NULL)
371 		FREE_SPACEW(sp, bp, blen);
372 	return (rval);
373 }
374 
375 /*
376  * v_Replace -- [count]R
377  *	Overwrite multiple characters.
378  *
379  * PUBLIC: int v_Replace(SCR *, VICMD *);
380  */
381 int
382 v_Replace(SCR *sp, VICMD *vp)
383 {
384 	size_t len;
385 	u_int32_t flags;
386 	int isempty;
387 	CHAR_T *p;
388 
389 	flags = set_txt_std(sp, vp, 0);
390 	sp->showmode = SM_REPLACE;
391 
392 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
393 		if (!isempty)
394 			return (1);
395 		len = 0;
396 		LF_SET(TXT_APPENDEOL);
397 	} else {
398 		if (len == 0)
399 			LF_SET(TXT_APPENDEOL);
400 		LF_SET(TXT_OVERWRITE | TXT_REPLACE);
401 	}
402 	vp->m_stop.lno = vp->m_start.lno;
403 	vp->m_stop.cno = len ? len - 1 : 0;
404 
405 	return (v_txt(sp, vp, &vp->m_stop, p, len,
406 	    0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
407 }
408 
409 /*
410  * v_subst -- [buffer][count]s
411  *	Substitute characters.
412  *
413  * PUBLIC: int v_subst(SCR *, VICMD *);
414  */
415 int
416 v_subst(SCR *sp, VICMD *vp)
417 {
418 	size_t len;
419 	u_int32_t flags;
420 	int isempty;
421 	CHAR_T *p;
422 
423 	flags = set_txt_std(sp, vp, 0);
424 	sp->showmode = SM_CHANGE;
425 
426 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
427 		if (!isempty)
428 			return (1);
429 		len = 0;
430 		LF_SET(TXT_APPENDEOL);
431 	} else {
432 		if (len == 0)
433 			LF_SET(TXT_APPENDEOL);
434 		LF_SET(TXT_EMARK | TXT_OVERWRITE);
435 	}
436 
437 	vp->m_stop.lno = vp->m_start.lno;
438 	vp->m_stop.cno =
439 	    vp->m_start.cno + (F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0);
440 	if (vp->m_stop.cno > len - 1)
441 		vp->m_stop.cno = len - 1;
442 
443 	if (p != NULL && cut(sp,
444 	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
445 	    &vp->m_start, &vp->m_stop, 0))
446 		return (1);
447 
448 	return (v_txt(sp, vp, &vp->m_stop, p, len, 0, OOBLNO, 1, flags));
449 }
450 
451 /*
452  * set_txt_std --
453  *	Initialize text processing flags.
454  */
455 static u_int32_t
456 set_txt_std(SCR *sp, VICMD *vp, u_int32_t flags)
457 {
458 	LF_SET(TXT_CNTRLT |
459 	    TXT_ESCAPE | TXT_MAPINPUT | TXT_RECORD | TXT_RESOLVE);
460 
461 	if (F_ISSET(vp, VC_ISDOT))
462 		LF_SET(TXT_REPLAY);
463 
464 	if (O_ISSET(sp, O_ALTWERASE))
465 		LF_SET(TXT_ALTWERASE);
466 	if (O_ISSET(sp, O_AUTOINDENT))
467 		LF_SET(TXT_AUTOINDENT);
468 	if (O_ISSET(sp, O_BEAUTIFY))
469 		LF_SET(TXT_BEAUTIFY);
470 	if (O_ISSET(sp, O_SHOWMATCH))
471 		LF_SET(TXT_SHOWMATCH);
472 	if (F_ISSET(sp, SC_SCRIPT))
473 		LF_SET(TXT_CR);
474 	if (O_ISSET(sp, O_TTYWERASE))
475 		LF_SET(TXT_TTYWERASE);
476 
477 	/*
478 	 * !!!
479 	 * Mapped keys were sometimes unaffected by the wrapmargin option
480 	 * in the historic 4BSD vi.  Consider the following commands, where
481 	 * each is executed on an empty line, in an 80 column screen, with
482 	 * the wrapmargin value set to 60.
483 	 *
484 	 *	aABC DEF <ESC>....
485 	 *	:map K aABC DEF ^V<ESC><CR>KKKKK
486 	 *	:map K 5aABC DEF ^V<ESC><CR>K
487 	 *
488 	 * The first and second commands are affected by wrapmargin.  The
489 	 * third is not.  (If the inserted text is itself longer than the
490 	 * wrapmargin value, i.e. if the "ABC DEF " string is replaced by
491 	 * something that's longer than 60 columns from the beginning of
492 	 * the line, the first two commands behave as before, but the third
493 	 * command gets fairly strange.)  The problem is that people wrote
494 	 * macros that depended on the third command NOT being affected by
495 	 * wrapmargin, as in this gem which centers lines:
496 	 *
497 	 *	map #c $mq81a ^V^[81^V^V|D`qld0:s/  / /g^V^M$p
498 	 *
499 	 * For compatibility reasons, we try and make it all work here.  I
500 	 * offer no hope that this is right, but it's probably pretty close.
501 	 *
502 	 * XXX
503 	 * Once I work my courage up, this is all gonna go away.  It's too
504 	 * evil to survive.
505 	 */
506 	if ((O_ISSET(sp, O_WRAPLEN) || O_ISSET(sp, O_WRAPMARGIN)) &&
507 	    (!MAPPED_KEYS_WAITING(sp) || !F_ISSET(vp, VC_C1SET)))
508 		LF_SET(TXT_WRAPMARGIN);
509 	return (flags);
510 }
511