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.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: v_itxt.c,v 10.18 2000/06/27 17:19:07 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:07 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
33 * Repeated input in the historic vi is mostly wrong and this isn't very
34 * backward compatible. For example, if the user entered "3Aab\ncd" in
35 * the historic vi, the "ab" was repeated 3 times, and the "\ncd" was then
36 * appended to the result. There was also a hack which I don't remember
37 * right now, where "3o" would open 3 lines and then let the user fill them
38 * in, to make screen movements on 300 baud modems more tolerable. I don't
39 * think it's going to be missed.
42 * There's a problem with the way that we do logging for change commands with
43 * implied motions (e.g. A, I, O, cc, etc.). Since the main vi loop logs the
44 * starting cursor position before the change command "moves" the cursor, the
45 * cursor position to which we return on undo will be where the user entered
46 * the change command, not the start of the change. Several of the following
47 * routines re-log the cursor to make this work correctly. Historic vi tried
48 * to do the same thing, and mostly got it right. (The only spectacular way
49 * it fails is if the user entered 'o' from anywhere but the last character of
50 * the line, the undo returned the cursor to the start of the line. If the
51 * user was on the last character of the line, the cursor returned to that
52 * position.) We also check for mapped keys waiting, i.e. if we're in the
53 * middle of a map, don't bother logging the cursor.
55 #define LOG_CORRECT { \
56 if (!MAPPED_KEYS_WAITING(sp)) \
57 (void)log_cursor(sp); \
60 static u_int32_t set_txt_std
__P((SCR
*, VICMD
*, u_int32_t
));
64 * Append text to the end of the line.
66 * PUBLIC: int v_iA __P((SCR *, VICMD *));
75 if (!db_get(sp
, vp
->m_start
.lno
, 0, NULL
, &len
))
76 sp
->cno
= len
== 0 ? 0 : len
- 1;
80 return (v_ia(sp
, vp
));
86 * Append text to the cursor position.
88 * PUBLIC: int v_ia __P((SCR *, VICMD *));
100 flags
= set_txt_std(sp
, vp
, 0);
101 sp
->showmode
= SM_APPEND
;
102 sp
->lno
= vp
->m_start
.lno
;
104 /* Move the cursor one column to the right and repaint the screen. */
105 if (db_eget(sp
, sp
->lno
, &p
, &len
, &isempty
)) {
109 LF_SET(TXT_APPENDEOL
);
111 if (len
== sp
->cno
+ 1) {
113 LF_SET(TXT_APPENDEOL
);
117 LF_SET(TXT_APPENDEOL
);
119 return (v_txt(sp
, vp
, NULL
, p
, len
,
120 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
125 * Insert text at the first nonblank.
127 * PUBLIC: int v_iI __P((SCR *, VICMD *));
135 if (nonblank(sp
, vp
->m_start
.lno
, &sp
->cno
))
140 return (v_ii(sp
, vp
));
146 * Insert text at the cursor position.
148 * PUBLIC: int v_ii __P((SCR *, VICMD *));
160 flags
= set_txt_std(sp
, vp
, 0);
161 sp
->showmode
= SM_INSERT
;
162 sp
->lno
= vp
->m_start
.lno
;
164 if (db_eget(sp
, sp
->lno
, &p
, &len
, &isempty
)) {
171 LF_SET(TXT_APPENDEOL
);
172 return (v_txt(sp
, vp
, NULL
, p
, len
,
173 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
176 enum which
{ o_cmd
, O_cmd
};
177 static int io
__P((SCR
*, VICMD
*, enum which
));
181 * Insert text above this line.
183 * PUBLIC: int v_iO __P((SCR *, VICMD *));
190 return (io(sp
, vp
, O_cmd
));
195 * Insert text after this line.
197 * PUBLIC: int v_io __P((SCR *, VICMD *));
204 return (io(sp
, vp
, o_cmd
));
213 db_recno_t ai_line
, lno
;
218 flags
= set_txt_std(sp
, vp
, TXT_ADDNEWLINE
| TXT_APPENDEOL
);
219 sp
->showmode
= SM_INSERT
;
222 if (db_last(sp
, &lno
))
235 if (db_insert(sp
, sp
->lno
, p
, 0))
237 if (db_get(sp
, sp
->lno
, DBG_FATAL
, &p
, &len
))
239 ai_line
= sp
->lno
+ 1;
241 if (db_append(sp
, 1, sp
->lno
, p
, 0))
243 if (db_get(sp
, ++sp
->lno
, DBG_FATAL
, &p
, &len
))
245 ai_line
= sp
->lno
- 1;
248 return (v_txt(sp
, vp
, NULL
, p
, len
,
249 0, ai_line
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
253 * v_change -- [buffer][count]c[count]motion
258 * PUBLIC: int v_change __P((SCR *, VICMD *));
267 int isempty
, lmode
, rval
;
272 * 'c' can be combined with motion commands that set the resulting
273 * cursor position, i.e. "cG". Clear the VM_RCM flags and make the
274 * resulting cursor position stick, inserting text has its own rules
275 * for cursor positioning.
277 F_CLR(vp
, VM_RCM_MASK
);
278 F_SET(vp
, VM_RCM_SET
);
281 * Find out if the file is empty, it's easier to handle it as a
284 if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
285 db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
288 return (v_ia(sp
, vp
));
291 flags
= set_txt_std(sp
, vp
, 0);
292 sp
->showmode
= SM_CHANGE
;
295 * Move the cursor to the start of the change. Note, if autoindent
296 * is turned on, the cc command in line mode changes from the first
297 * *non-blank* character of the line, not the first character. And,
298 * to make it just a bit more exciting, the initial space is handled
299 * as auto-indent characters.
301 lmode
= F_ISSET(vp
, VM_LMODE
) ? CUT_LINEMODE
: 0;
304 if (O_ISSET(sp
, O_AUTOINDENT
)) {
305 if (nonblank(sp
, vp
->m_start
.lno
, &vp
->m_start
.cno
))
310 sp
->lno
= vp
->m_start
.lno
;
311 sp
->cno
= vp
->m_start
.cno
;
316 * If not in line mode and changing within a single line, copy the
317 * text and overwrite it.
319 if (!lmode
&& vp
->m_start
.lno
== vp
->m_stop
.lno
) {
322 * Historic practice, c did not cut into the numeric buffers,
323 * only the unnamed one.
326 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
327 &vp
->m_start
, &vp
->m_stop
, lmode
))
330 LF_SET(TXT_APPENDEOL
);
331 LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
332 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
,
333 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
337 * It's trickier if in line mode or changing over multiple lines. If
338 * we're in line mode delete all of the lines and insert a replacement
339 * line which the user edits. If there was leading whitespace in the
340 * first line being changed, we copy it and use it as the replacement.
341 * If we're not in line mode, we delete the text and start inserting.
344 * Copy the text. Historic practice, c did not cut into the numeric
345 * buffers, only the unnamed one.
348 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
349 &vp
->m_start
, &vp
->m_stop
, lmode
))
352 /* If replacing entire lines and there's leading text. */
353 if (lmode
&& vp
->m_start
.cno
) {
355 * Get a copy of the first line changed, and copy out the
358 if (db_get(sp
, vp
->m_start
.lno
, DBG_FATAL
, &p
, &len
))
360 GET_SPACE_RET(sp
, bp
, blen
, vp
->m_start
.cno
);
361 memmove(bp
, p
, vp
->m_start
.cno
);
365 /* Delete the text. */
366 if (del(sp
, &vp
->m_start
, &vp
->m_stop
, lmode
))
369 /* If replacing entire lines, insert a replacement line. */
371 if (db_insert(sp
, vp
->m_start
.lno
, bp
, vp
->m_start
.cno
))
373 sp
->lno
= vp
->m_start
.lno
;
374 len
= sp
->cno
= vp
->m_start
.cno
;
377 /* Get the line we're editing. */
378 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
384 /* Check to see if we're appending to the line. */
385 if (vp
->m_start
.cno
>= len
)
386 LF_SET(TXT_APPENDEOL
);
388 rval
= v_txt(sp
, vp
, NULL
, p
, len
,
389 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
);
392 FREE_SPACE(sp
, bp
, blen
);
397 * v_Replace -- [count]R
398 * Overwrite multiple characters.
400 * PUBLIC: int v_Replace __P((SCR *, VICMD *));
412 flags
= set_txt_std(sp
, vp
, 0);
413 sp
->showmode
= SM_REPLACE
;
415 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
419 LF_SET(TXT_APPENDEOL
);
422 LF_SET(TXT_APPENDEOL
);
423 LF_SET(TXT_OVERWRITE
| TXT_REPLACE
);
425 vp
->m_stop
.lno
= vp
->m_start
.lno
;
426 vp
->m_stop
.cno
= len
? len
- 1 : 0;
428 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
,
429 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
433 * v_subst -- [buffer][count]s
434 * Substitute characters.
436 * PUBLIC: int v_subst __P((SCR *, VICMD *));
448 flags
= set_txt_std(sp
, vp
, 0);
449 sp
->showmode
= SM_CHANGE
;
451 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
455 LF_SET(TXT_APPENDEOL
);
458 LF_SET(TXT_APPENDEOL
);
459 LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
462 vp
->m_stop
.lno
= vp
->m_start
.lno
;
464 vp
->m_start
.cno
+ (F_ISSET(vp
, VC_C1SET
) ? vp
->count
- 1 : 0);
465 if (vp
->m_stop
.cno
> len
- 1)
466 vp
->m_stop
.cno
= len
- 1;
468 if (p
!= NULL
&& cut(sp
,
469 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
470 &vp
->m_start
, &vp
->m_stop
, 0))
473 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
, 0, OOBLNO
, 1, flags
));
478 * Initialize text processing flags.
481 set_txt_std(sp
, vp
, flags
)
487 TXT_ESCAPE
| TXT_MAPINPUT
| TXT_RECORD
| TXT_RESOLVE
);
489 if (F_ISSET(vp
, VC_ISDOT
))
492 if (O_ISSET(sp
, O_ALTWERASE
))
493 LF_SET(TXT_ALTWERASE
);
494 if (O_ISSET(sp
, O_AUTOINDENT
))
495 LF_SET(TXT_AUTOINDENT
);
496 if (O_ISSET(sp
, O_BEAUTIFY
))
497 LF_SET(TXT_BEAUTIFY
);
498 if (O_ISSET(sp
, O_SHOWMATCH
))
499 LF_SET(TXT_SHOWMATCH
);
500 if (F_ISSET(sp
, SC_SCRIPT
))
502 if (O_ISSET(sp
, O_TTYWERASE
))
503 LF_SET(TXT_TTYWERASE
);
507 * Mapped keys were sometimes unaffected by the wrapmargin option
508 * in the historic 4BSD vi. Consider the following commands, where
509 * each is executed on an empty line, in an 80 column screen, with
510 * the wrapmargin value set to 60.
513 * :map K aABC DEF ^V<ESC><CR>KKKKK
514 * :map K 5aABC DEF ^V<ESC><CR>K
516 * The first and second commands are affected by wrapmargin. The
517 * third is not. (If the inserted text is itself longer than the
518 * wrapmargin value, i.e. if the "ABC DEF " string is replaced by
519 * something that's longer than 60 columns from the beginning of
520 * the line, the first two commands behave as before, but the third
521 * command gets fairly strange.) The problem is that people wrote
522 * macros that depended on the third command NOT being affected by
523 * wrapmargin, as in this gem which centers lines:
525 * map #c $mq81a ^V^[81^V^V|D`qld0:s/ / /g^V^M$p
527 * For compatibility reasons, we try and make it all work here. I
528 * offer no hope that this is right, but it's probably pretty close.
531 * Once I work my courage up, this is all gonna go away. It's too
534 if ((O_ISSET(sp
, O_WRAPLEN
) || O_ISSET(sp
, O_WRAPMARGIN
)) &&
535 (!MAPPED_KEYS_WAITING(sp
) || !F_ISSET(vp
, VC_C1SET
)))
536 LF_SET(TXT_WRAPMARGIN
);