tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / v_itxt.c
blob6399fe9524ff595f3bc6ad40ed930905eeb8c235
1 /* $NetBSD: v_itxt.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 static const char sccsid[] = "Id: v_itxt.c,v 10.21 2001/06/25 15:19:32 skimo Exp (Berkeley) Date: 2001/06/25 15:19:32 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "vi.h"
33 * !!!
34 * Repeated input in the historic vi is mostly wrong and this isn't very
35 * backward compatible. For example, if the user entered "3Aab\ncd" in
36 * the historic vi, the "ab" was repeated 3 times, and the "\ncd" was then
37 * appended to the result. There was also a hack which I don't remember
38 * right now, where "3o" would open 3 lines and then let the user fill them
39 * in, to make screen movements on 300 baud modems more tolerable. I don't
40 * think it's going to be missed.
42 * !!!
43 * There's a problem with the way that we do logging for change commands with
44 * implied motions (e.g. A, I, O, cc, etc.). Since the main vi loop logs the
45 * starting cursor position before the change command "moves" the cursor, the
46 * cursor position to which we return on undo will be where the user entered
47 * the change command, not the start of the change. Several of the following
48 * routines re-log the cursor to make this work correctly. Historic vi tried
49 * to do the same thing, and mostly got it right. (The only spectacular way
50 * it fails is if the user entered 'o' from anywhere but the last character of
51 * the line, the undo returned the cursor to the start of the line. If the
52 * user was on the last character of the line, the cursor returned to that
53 * position.) We also check for mapped keys waiting, i.e. if we're in the
54 * middle of a map, don't bother logging the cursor.
56 #define LOG_CORRECT { \
57 if (!MAPPED_KEYS_WAITING(sp)) \
58 (void)log_cursor(sp); \
61 static u_int32_t set_txt_std __P((SCR *, VICMD *, u_int32_t));
64 * v_iA -- [count]A
65 * Append text to the end of the line.
67 * PUBLIC: int v_iA __P((SCR *, VICMD *));
69 int
70 v_iA(SCR *sp, VICMD *vp)
72 size_t len;
74 if (!db_get(sp, vp->m_start.lno, 0, NULL, &len))
75 sp->cno = len == 0 ? 0 : len - 1;
77 LOG_CORRECT;
79 return (v_ia(sp, vp));
83 * v_ia -- [count]a
84 * [count]A
85 * Append text to the cursor position.
87 * PUBLIC: int v_ia __P((SCR *, VICMD *));
89 int
90 v_ia(SCR *sp, VICMD *vp)
92 size_t len;
93 u_int32_t flags;
94 int isempty;
95 CHAR_T *p;
97 flags = set_txt_std(sp, vp, 0);
98 sp->showmode = SM_APPEND;
99 sp->lno = vp->m_start.lno;
101 /* Move the cursor one column to the right and repaint the screen. */
102 if (db_eget(sp, sp->lno, &p, &len, &isempty)) {
103 if (!isempty)
104 return (1);
105 len = 0;
106 LF_SET(TXT_APPENDEOL);
107 } else if (len) {
108 if (len == sp->cno + 1) {
109 sp->cno = len;
110 LF_SET(TXT_APPENDEOL);
111 } else
112 ++sp->cno;
113 } else
114 LF_SET(TXT_APPENDEOL);
116 return (v_txt(sp, vp, NULL, p, len,
117 0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
121 * v_iI -- [count]I
122 * Insert text at the first nonblank.
124 * PUBLIC: int v_iI __P((SCR *, VICMD *));
127 v_iI(SCR *sp, VICMD *vp)
129 sp->cno = 0;
130 if (nonblank(sp, vp->m_start.lno, &sp->cno))
131 return (1);
133 LOG_CORRECT;
135 return (v_ii(sp, vp));
139 * v_ii -- [count]i
140 * [count]I
141 * Insert text at the cursor position.
143 * PUBLIC: int v_ii __P((SCR *, VICMD *));
146 v_ii(SCR *sp, VICMD *vp)
148 size_t len;
149 u_int32_t flags;
150 int isempty;
151 CHAR_T *p;
153 flags = set_txt_std(sp, vp, 0);
154 sp->showmode = SM_INSERT;
155 sp->lno = vp->m_start.lno;
157 if (db_eget(sp, sp->lno, &p, &len, &isempty)) {
158 if (!isempty)
159 return (1);
160 len = 0;
163 if (len == 0)
164 LF_SET(TXT_APPENDEOL);
165 return (v_txt(sp, vp, NULL, p, len,
166 0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
169 enum which { o_cmd, O_cmd };
170 static int io __P((SCR *, VICMD *, enum which));
173 * v_iO -- [count]O
174 * Insert text above this line.
176 * PUBLIC: int v_iO __P((SCR *, VICMD *));
179 v_iO(SCR *sp, VICMD *vp)
181 return (io(sp, vp, O_cmd));
185 * v_io -- [count]o
186 * Insert text after this line.
188 * PUBLIC: int v_io __P((SCR *, VICMD *));
191 v_io(SCR *sp, VICMD *vp)
193 return (io(sp, vp, o_cmd));
196 static int
197 io(SCR *sp, VICMD *vp, enum which cmd)
199 db_recno_t ai_line, lno;
200 size_t len;
201 u_int32_t flags;
202 CHAR_T *p;
204 flags = set_txt_std(sp, vp, TXT_ADDNEWLINE | TXT_APPENDEOL);
205 sp->showmode = SM_INSERT;
207 if (sp->lno == 1) {
208 if (db_last(sp, &lno))
209 return (1);
210 if (lno != 0)
211 goto insert;
212 p = NULL;
213 len = 0;
214 ai_line = OOBLNO;
215 } else {
216 static CHAR_T nul = 0;
217 insert: p = &nul;
218 sp->cno = 0;
219 LOG_CORRECT;
221 if (cmd == O_cmd) {
222 if (db_insert(sp, 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 } else {
228 if (db_append(sp, 1, sp->lno, p, 0))
229 return (1);
230 if (db_get(sp, ++sp->lno, DBG_FATAL, &p, &len))
231 return (1);
232 ai_line = sp->lno - 1;
235 return (v_txt(sp, vp, NULL, p, len,
236 0, ai_line, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
240 * v_change -- [buffer][count]c[count]motion
241 * [buffer][count]C
242 * [buffer][count]S
243 * Change command.
245 * PUBLIC: int v_change __P((SCR *, VICMD *));
248 v_change(SCR *sp, VICMD *vp)
250 size_t blen, len;
251 u_int32_t flags;
252 int isempty, lmode, rval;
253 CHAR_T *bp;
254 CHAR_T *p;
257 * 'c' can be combined with motion commands that set the resulting
258 * cursor position, i.e. "cG". Clear the VM_RCM flags and make the
259 * resulting cursor position stick, inserting text has its own rules
260 * for cursor positioning.
262 F_CLR(vp, VM_RCM_MASK);
263 F_SET(vp, VM_RCM_SET);
266 * Find out if the file is empty, it's easier to handle it as a
267 * special case.
269 if (vp->m_start.lno == vp->m_stop.lno &&
270 db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
271 if (!isempty)
272 return (1);
273 return (v_ia(sp, vp));
276 flags = set_txt_std(sp, vp, 0);
277 sp->showmode = SM_CHANGE;
280 * Move the cursor to the start of the change. Note, if autoindent
281 * is turned on, the cc command in line mode changes from the first
282 * *non-blank* character of the line, not the first character. And,
283 * to make it just a bit more exciting, the initial space is handled
284 * as auto-indent characters.
286 lmode = F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0;
287 if (lmode) {
288 vp->m_start.cno = 0;
289 if (O_ISSET(sp, O_AUTOINDENT)) {
290 if (nonblank(sp, vp->m_start.lno, &vp->m_start.cno))
291 return (1);
292 LF_SET(TXT_AICHARS);
295 sp->lno = vp->m_start.lno;
296 sp->cno = vp->m_start.cno;
298 LOG_CORRECT;
301 * If not in line mode and changing within a single line, copy the
302 * text and overwrite it.
304 if (!lmode && vp->m_start.lno == vp->m_stop.lno) {
306 * !!!
307 * Historic practice, c did not cut into the numeric buffers,
308 * only the unnamed one.
310 if (cut(sp,
311 F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
312 &vp->m_start, &vp->m_stop, lmode))
313 return (1);
314 if (len == 0)
315 LF_SET(TXT_APPENDEOL);
316 LF_SET(TXT_EMARK | TXT_OVERWRITE);
317 return (v_txt(sp, vp, &vp->m_stop, p, len,
318 0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
322 * It's trickier if in line mode or changing over multiple lines. If
323 * we're in line mode delete all of the lines and insert a replacement
324 * line which the user edits. If there was leading whitespace in the
325 * first line being changed, we copy it and use it as the replacement.
326 * If we're not in line mode, we delete the text and start inserting.
328 * !!!
329 * Copy the text. Historic practice, c did not cut into the numeric
330 * buffers, only the unnamed one.
332 if (cut(sp,
333 F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
334 &vp->m_start, &vp->m_stop, lmode))
335 return (1);
337 /* If replacing entire lines and there's leading text. */
338 if (lmode && vp->m_start.cno) {
340 * Get a copy of the first line changed, and copy out the
341 * leading text.
343 if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len))
344 return (1);
345 GET_SPACE_RETW(sp, bp, blen, vp->m_start.cno);
346 MEMMOVEW(bp, p, vp->m_start.cno);
347 } else
348 bp = NULL;
350 /* Delete the text. */
351 if (del(sp, &vp->m_start, &vp->m_stop, lmode))
352 return (1);
354 /* If replacing entire lines, insert a replacement line. */
355 if (lmode) {
356 if (db_insert(sp, vp->m_start.lno, bp, vp->m_start.cno))
357 return (1);
358 sp->lno = vp->m_start.lno;
359 len = sp->cno = vp->m_start.cno;
362 /* Get the line we're editing. */
363 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
364 if (!isempty)
365 return (1);
366 len = 0;
369 /* Check to see if we're appending to the line. */
370 if (vp->m_start.cno >= len)
371 LF_SET(TXT_APPENDEOL);
373 rval = v_txt(sp, vp, NULL, p, len,
374 0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags);
376 if (bp != NULL)
377 FREE_SPACEW(sp, bp, blen);
378 return (rval);
382 * v_Replace -- [count]R
383 * Overwrite multiple characters.
385 * PUBLIC: int v_Replace __P((SCR *, VICMD *));
388 v_Replace(SCR *sp, VICMD *vp)
390 size_t len;
391 u_int32_t flags;
392 int isempty;
393 CHAR_T *p;
395 flags = set_txt_std(sp, vp, 0);
396 sp->showmode = SM_REPLACE;
398 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
399 if (!isempty)
400 return (1);
401 len = 0;
402 LF_SET(TXT_APPENDEOL);
403 } else {
404 if (len == 0)
405 LF_SET(TXT_APPENDEOL);
406 LF_SET(TXT_OVERWRITE | TXT_REPLACE);
408 vp->m_stop.lno = vp->m_start.lno;
409 vp->m_stop.cno = len ? len - 1 : 0;
411 return (v_txt(sp, vp, &vp->m_stop, p, len,
412 0, OOBLNO, F_ISSET(vp, VC_C1SET) ? vp->count : 1, flags));
416 * v_subst -- [buffer][count]s
417 * Substitute characters.
419 * PUBLIC: int v_subst __P((SCR *, VICMD *));
422 v_subst(SCR *sp, VICMD *vp)
424 size_t len;
425 u_int32_t flags;
426 int isempty;
427 CHAR_T *p;
429 flags = set_txt_std(sp, vp, 0);
430 sp->showmode = SM_CHANGE;
432 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
433 if (!isempty)
434 return (1);
435 len = 0;
436 LF_SET(TXT_APPENDEOL);
437 } else {
438 if (len == 0)
439 LF_SET(TXT_APPENDEOL);
440 LF_SET(TXT_EMARK | TXT_OVERWRITE);
443 vp->m_stop.lno = vp->m_start.lno;
444 vp->m_stop.cno =
445 vp->m_start.cno + (F_ISSET(vp, VC_C1SET) ? vp->count - 1 : 0);
446 if (vp->m_stop.cno > len - 1)
447 vp->m_stop.cno = len - 1;
449 if (p != NULL && cut(sp,
450 F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL,
451 &vp->m_start, &vp->m_stop, 0))
452 return (1);
454 return (v_txt(sp, vp, &vp->m_stop, p, len, 0, OOBLNO, 1, flags));
458 * set_txt_std --
459 * Initialize text processing flags.
461 static u_int32_t
462 set_txt_std(SCR *sp, VICMD *vp, u_int32_t flags)
464 LF_SET(TXT_CNTRLT |
465 TXT_ESCAPE | TXT_MAPINPUT | TXT_RECORD | TXT_RESOLVE);
467 if (F_ISSET(vp, VC_ISDOT))
468 LF_SET(TXT_REPLAY);
470 if (O_ISSET(sp, O_ALTWERASE))
471 LF_SET(TXT_ALTWERASE);
472 if (O_ISSET(sp, O_AUTOINDENT))
473 LF_SET(TXT_AUTOINDENT);
474 if (O_ISSET(sp, O_BEAUTIFY))
475 LF_SET(TXT_BEAUTIFY);
476 if (O_ISSET(sp, O_SHOWMATCH))
477 LF_SET(TXT_SHOWMATCH);
478 if (F_ISSET(sp, SC_SCRIPT))
479 LF_SET(TXT_CR);
480 if (O_ISSET(sp, O_TTYWERASE))
481 LF_SET(TXT_TTYWERASE);
484 * !!!
485 * Mapped keys were sometimes unaffected by the wrapmargin option
486 * in the historic 4BSD vi. Consider the following commands, where
487 * each is executed on an empty line, in an 80 column screen, with
488 * the wrapmargin value set to 60.
490 * aABC DEF <ESC>....
491 * :map K aABC DEF ^V<ESC><CR>KKKKK
492 * :map K 5aABC DEF ^V<ESC><CR>K
494 * The first and second commands are affected by wrapmargin. The
495 * third is not. (If the inserted text is itself longer than the
496 * wrapmargin value, i.e. if the "ABC DEF " string is replaced by
497 * something that's longer than 60 columns from the beginning of
498 * the line, the first two commands behave as before, but the third
499 * command gets fairly strange.) The problem is that people wrote
500 * macros that depended on the third command NOT being affected by
501 * wrapmargin, as in this gem which centers lines:
503 * map #c $mq81a ^V^[81^V^V|D`qld0:s/ / /g^V^M$p
505 * For compatibility reasons, we try and make it all work here. I
506 * offer no hope that this is right, but it's probably pretty close.
508 * XXX
509 * Once I work my courage up, this is all gonna go away. It's too
510 * evil to survive.
512 if ((O_ISSET(sp, O_WRAPLEN) || O_ISSET(sp, O_WRAPMARGIN)) &&
513 (!MAPPED_KEYS_WAITING(sp) || !F_ISSET(vp, VC_C1SET)))
514 LF_SET(TXT_WRAPMARGIN);
515 return (flags);