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.
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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
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.
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
));
66 * Append text to the end of the line.
68 * PUBLIC: int v_iA __P((SCR *, VICMD *));
71 v_iA(SCR
*sp
, VICMD
*vp
)
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 *));
91 v_ia(SCR
*sp
, VICMD
*vp
)
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
)) {
107 LF_SET(TXT_APPENDEOL
);
109 if (len
== sp
->cno
+ 1) {
111 LF_SET(TXT_APPENDEOL
);
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
));
123 * Insert text at the first nonblank.
125 * PUBLIC: int v_iI __P((SCR *, VICMD *));
128 v_iI(SCR
*sp
, VICMD
*vp
)
131 if (nonblank(sp
, vp
->m_start
.lno
, &sp
->cno
))
136 return (v_ii(sp
, vp
));
142 * Insert text at the cursor position.
144 * PUBLIC: int v_ii __P((SCR *, VICMD *));
147 v_ii(SCR
*sp
, VICMD
*vp
)
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
)) {
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
));
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
));
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
));
198 io(SCR
*sp
, VICMD
*vp
, enum which cmd
)
200 db_recno_t ai_line
, lno
;
205 flags
= set_txt_std(sp
, vp
, TXT_ADDNEWLINE
| TXT_APPENDEOL
);
206 sp
->showmode
= SM_INSERT
;
209 if (db_last(sp
, &lno
))
217 static CHAR_T nul
= 0;
223 if (db_insert(sp
, sp
->lno
, p
, 0))
225 if (db_get(sp
, sp
->lno
, DBG_FATAL
, &p
, &len
))
227 ai_line
= sp
->lno
+ 1;
229 if (db_append(sp
, 1, sp
->lno
, p
, 0))
231 if (db_get(sp
, ++sp
->lno
, DBG_FATAL
, &p
, &len
))
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
246 * PUBLIC: int v_change __P((SCR *, VICMD *));
249 v_change(SCR
*sp
, VICMD
*vp
)
253 int isempty
, lmode
, rval
;
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
270 if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
271 db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
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;
290 if (O_ISSET(sp
, O_AUTOINDENT
)) {
291 if (nonblank(sp
, vp
->m_start
.lno
, &vp
->m_start
.cno
))
296 sp
->lno
= vp
->m_start
.lno
;
297 sp
->cno
= vp
->m_start
.cno
;
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
) {
308 * Historic practice, c did not cut into the numeric buffers,
309 * only the unnamed one.
312 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
313 &vp
->m_start
, &vp
->m_stop
, lmode
))
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.
330 * Copy the text. Historic practice, c did not cut into the numeric
331 * buffers, only the unnamed one.
334 F_ISSET(vp
, VC_BUFFER
) ? &vp
->buffer
: NULL
,
335 &vp
->m_start
, &vp
->m_stop
, lmode
))
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
344 if (db_get(sp
, vp
->m_start
.lno
, DBG_FATAL
, &p
, &len
))
346 GET_SPACE_RETW(sp
, bp
, blen
, vp
->m_start
.cno
);
347 MEMMOVEW(bp
, p
, vp
->m_start
.cno
);
351 /* Delete the text. */
352 if (del(sp
, &vp
->m_start
, &vp
->m_stop
, lmode
))
355 /* If replacing entire lines, insert a replacement line. */
357 if (db_insert(sp
, vp
->m_start
.lno
, bp
, vp
->m_start
.cno
))
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
)) {
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
);
378 FREE_SPACEW(sp
, bp
, blen
);
383 * v_Replace -- [count]R
384 * Overwrite multiple characters.
386 * PUBLIC: int v_Replace __P((SCR *, VICMD *));
389 v_Replace(SCR
*sp
, VICMD
*vp
)
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
)) {
403 LF_SET(TXT_APPENDEOL
);
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
)
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
)) {
437 LF_SET(TXT_APPENDEOL
);
440 LF_SET(TXT_APPENDEOL
);
441 LF_SET(TXT_EMARK
| TXT_OVERWRITE
);
444 vp
->m_stop
.lno
= vp
->m_start
.lno
;
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))
455 return (v_txt(sp
, vp
, &vp
->m_stop
, p
, len
, 0, OOBLNO
, 1, flags
));
460 * Initialize text processing flags.
463 set_txt_std(SCR
*sp
, VICMD
*vp
, u_int32_t flags
)
466 TXT_ESCAPE
| TXT_MAPINPUT
| TXT_RECORD
| TXT_RESOLVE
);
468 if (F_ISSET(vp
, VC_ISDOT
))
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
))
481 if (O_ISSET(sp
, O_TTYWERASE
))
482 LF_SET(TXT_TTYWERASE
);
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.
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.
510 * Once I work my courage up, this is all gonna go away. It's too
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
);