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