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