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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
24 #include "../common/common.h"
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.
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.
51 #define LOG_CORRECT { \
52 if (!MAPPED_KEYS_WAITING(sp)) \
53 (void)log_cursor(sp); \
56 static u_int32_t
set_txt_std(SCR
*, VICMD
*, u_int32_t
);
60 * Append text to the end of the line.
62 * PUBLIC: int v_iA(SCR *, VICMD *);
65 v_iA(SCR
*sp
, VICMD
*vp
)
69 if (!db_get(sp
, vp
->m_start
.lno
, 0, NULL
, &len
))
70 sp
->cno
= len
== 0 ? 0 : len
- 1;
74 return (v_ia(sp
, vp
));
80 * Append text to the cursor position.
82 * PUBLIC: int v_ia(SCR *, VICMD *);
85 v_ia(SCR
*sp
, VICMD
*vp
)
92 flags
= set_txt_std(sp
, vp
, 0);
93 sp
->showmode
= SM_APPEND
;
94 sp
->lno
= vp
->m_start
.lno
;
96 /* Move the cursor one column to the right and repaint the screen. */
97 if (db_eget(sp
, sp
->lno
, &p
, &len
, &isempty
)) {
101 LF_SET(TXT_APPENDEOL
);
103 if (len
== sp
->cno
+ 1) {
105 LF_SET(TXT_APPENDEOL
);
109 LF_SET(TXT_APPENDEOL
);
111 return (v_txt(sp
, vp
, NULL
, p
, len
,
112 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
117 * Insert text at the first nonblank.
119 * PUBLIC: int v_iI(SCR *, VICMD *);
122 v_iI(SCR
*sp
, VICMD
*vp
)
125 if (nonblank(sp
, vp
->m_start
.lno
, &sp
->cno
))
130 return (v_ii(sp
, vp
));
136 * Insert text at the cursor position.
138 * PUBLIC: int v_ii(SCR *, VICMD *);
141 v_ii(SCR
*sp
, VICMD
*vp
)
148 flags
= set_txt_std(sp
, vp
, 0);
149 sp
->showmode
= SM_INSERT
;
150 sp
->lno
= vp
->m_start
.lno
;
152 if (db_eget(sp
, sp
->lno
, &p
, &len
, &isempty
)) {
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
));
164 enum which
{ o_cmd
, O_cmd
};
165 static int io(SCR
*, VICMD
*, enum which
);
169 * Insert text above this line.
171 * PUBLIC: int v_iO(SCR *, VICMD *);
174 v_iO(SCR
*sp
, VICMD
*vp
)
176 return (io(sp
, vp
, O_cmd
));
181 * Insert text after this line.
183 * PUBLIC: int v_io(SCR *, VICMD *);
186 v_io(SCR
*sp
, VICMD
*vp
)
188 return (io(sp
, vp
, o_cmd
));
192 io(SCR
*sp
, VICMD
*vp
, enum which cmd
)
194 recno_t ai_line
, lno
;
199 flags
= set_txt_std(sp
, vp
, TXT_ADDNEWLINE
| TXT_APPENDEOL
);
200 sp
->showmode
= SM_INSERT
;
203 if (db_last(sp
, &lno
))
216 if (db_insert(sp
, sp
->lno
, p
, 0))
218 if (db_get(sp
, sp
->lno
, DBG_FATAL
, &p
, &len
))
220 ai_line
= sp
->lno
+ 1;
222 if (db_append(sp
, 1, sp
->lno
, p
, 0))
224 if (db_get(sp
, ++sp
->lno
, DBG_FATAL
, &p
, &len
))
226 ai_line
= sp
->lno
- 1;
229 return (v_txt(sp
, vp
, NULL
, p
, len
,
230 0, ai_line
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
234 * v_change -- [buffer][count]c[count]motion
239 * PUBLIC: int v_change(SCR *, VICMD *);
242 v_change(SCR
*sp
, VICMD
*vp
)
246 int isempty
, lmode
, rval
;
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.
256 F_CLR(vp
, VM_RCM_MASK
);
257 F_SET(vp
, VM_RCM_SET
);
260 * Find out if the file is empty, it's easier to handle it as a
263 if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
264 db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
267 return (v_ia(sp
, vp
));
270 flags
= set_txt_std(sp
, vp
, 0);
271 sp
->showmode
= SM_CHANGE
;
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.
280 lmode
= F_ISSET(vp
, VM_LMODE
) ? CUT_LINEMODE
: 0;
283 if (O_ISSET(sp
, O_AUTOINDENT
)) {
284 if (nonblank(sp
, vp
->m_start
.lno
, &vp
->m_start
.cno
))
289 sp
->lno
= vp
->m_start
.lno
;
290 sp
->cno
= vp
->m_start
.cno
;
295 * If not in line mode and changing within a single line, copy the
296 * text and overwrite it.
298 if (!lmode
&& vp
->m_start
.lno
== vp
->m_stop
.lno
) {
301 * Historic practice, c did not cut into the numeric buffers,
302 * only the unnamed one.
305 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
306 &vp
->m_start
, &vp
->m_stop
, lmode
))
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
));
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.
323 * Copy the text. Historic practice, c did not cut into the numeric
324 * buffers, only the unnamed one.
327 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
328 &vp
->m_start
, &vp
->m_stop
, lmode
))
331 /* If replacing entire lines and there's leading text. */
332 if (lmode
&& vp
->m_start
.cno
) {
334 * Get a copy of the first line changed, and copy out the
337 if (db_get(sp
, vp
->m_start
.lno
, DBG_FATAL
, &p
, &len
))
339 GET_SPACE_RETW(sp
, bp
, blen
, vp
->m_start
.cno
);
340 MEMMOVE(bp
, p
, vp
->m_start
.cno
);
344 /* Delete the text. */
345 if (del(sp
, &vp
->m_start
, &vp
->m_stop
, lmode
))
348 /* If replacing entire lines, insert a replacement line. */
350 if (db_insert(sp
, vp
->m_start
.lno
, bp
, vp
->m_start
.cno
))
352 sp
->lno
= vp
->m_start
.lno
;
353 len
= sp
->cno
= vp
->m_start
.cno
;
356 /* Get the line we're editing. */
357 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
363 /* Check to see if we're appending to the line. */
364 if (vp
->m_start
.cno
>= len
)
365 LF_SET(TXT_APPENDEOL
);
367 rval
= v_txt(sp
, vp
, NULL
, p
, len
,
368 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
);
371 FREE_SPACEW(sp
, bp
, blen
);
376 * v_Replace -- [count]R
377 * Overwrite multiple characters.
379 * PUBLIC: int v_Replace(SCR *, VICMD *);
382 v_Replace(SCR
*sp
, VICMD
*vp
)
389 flags
= set_txt_std(sp
, vp
, 0);
390 sp
->showmode
= SM_REPLACE
;
392 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
396 LF_SET(TXT_APPENDEOL
);
399 LF_SET(TXT_APPENDEOL
);
400 LF_SET(TXT_OVERWRITE
| TXT_REPLACE
);
402 vp
->m_stop
.lno
= vp
->m_start
.lno
;
403 vp
->m_stop
.cno
= len
? len
- 1 : 0;
405 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
,
406 0, OOBLNO
, F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1, flags
));
410 * v_subst -- [buffer][count]s
411 * Substitute characters.
413 * PUBLIC: int v_subst(SCR *, VICMD *);
416 v_subst(SCR
*sp
, VICMD
*vp
)
423 flags
= set_txt_std(sp
, vp
, 0);
424 sp
->showmode
= SM_CHANGE
;
426 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
430 LF_SET(TXT_APPENDEOL
);
433 LF_SET(TXT_APPENDEOL
);
434 LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
437 vp
->m_stop
.lno
= vp
->m_start
.lno
;
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;
443 if (p
!= NULL
&& cut(sp
,
444 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
445 &vp
->m_start
, &vp
->m_stop
, 0))
448 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
, 0, OOBLNO
, 1, flags
));
453 * Initialize text processing flags.
456 set_txt_std(SCR
*sp
, VICMD
*vp
, u_int32_t flags
)
459 TXT_ESCAPE
| TXT_MAPINPUT
| TXT_RECORD
| TXT_RESOLVE
);
461 if (F_ISSET(vp
, VC_ISDOT
))
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
))
474 if (O_ISSET(sp
, O_TTYWERASE
))
475 LF_SET(TXT_TTYWERASE
);
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.
485 * :map K aABC DEF ^V<ESC><CR>KKKKK
486 * :map K 5aABC DEF ^V<ESC><CR>K
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:
497 * map #c $mq81a ^V^[81^V^V|D`qld0:s/ / /g^V^M$p
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.
503 * Once I work my courage up, this is all gonna go away. It's too
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
);