1 /* $NetBSD: v_replace.c,v 1.1.1.2 2008/05/18 14:31:43 aymeric Exp $ */
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_replace.c,v 10.24 2001/06/25 15:19:34 skimo Exp (Berkeley) Date: 2001/06/25 15:19:34";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
34 * v_replace -- [count]r<char>
37 * The r command in historic vi was almost beautiful in its badness. For
38 * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted
39 * a single character. "Nr<carriage return>", where N was greater than 1,
40 * inserted a single carriage return. "r<escape>" did cancel the command,
41 * but "r<literal><escape>" erased a single character. To enter a literal
42 * <literal> character, it required three <literal> characters after the
43 * command. This may not be right, but at least it's not insane.
45 * PUBLIC: int v_replace __P((SCR *, VICMD *));
48 v_replace(SCR
*sp
, VICMD
*vp
)
62 * If the line doesn't exist, or it's empty, replacement isn't
63 * allowed. It's not hard to implement, but:
65 * 1: It's historic practice (vi beeped before the replacement
66 * character was even entered).
67 * 2: For consistency, this change would require that the more
68 * general case, "Nr", when the user is < N characters from
69 * the end of the line, also work, which would be a bit odd.
70 * 3: Replacing with a <newline> has somewhat odd semantics.
72 if (db_get(sp
, vp
->m_start
.lno
, DBG_FATAL
, &p
, &len
))
75 msgq(sp
, M_BERR
, "186|No characters to replace");
80 * Figure out how many characters to be replace. For no particular
81 * reason (other than that the semantics of replacing the newline
82 * are confusing) only permit the replacement of the characters in
83 * the current line. I suppose we could append replacement characters
84 * to the line, but I see no compelling reason to do so. Check this
85 * before we get the character to match historic practice, where Nr
86 * failed immediately if there were less than N characters from the
87 * cursor to the end of the line.
89 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
90 vp
->m_stop
.lno
= vp
->m_start
.lno
;
91 vp
->m_stop
.cno
= vp
->m_start
.cno
+ cnt
- 1;
92 if (vp
->m_stop
.cno
> len
- 1) {
93 v_eol(sp
, &vp
->m_start
);
98 * If it's not a repeat, reset the current mode and get a replacement
102 if (!F_ISSET(vp
, VC_ISDOT
)) {
103 sp
->showmode
= SM_REPLACE
;
104 if (vs_refresh(sp
, 0))
106 next
: if (v_event_get(sp
, &ev
, 0, 0))
109 switch (ev
.e_event
) {
112 * <literal_next> means escape the next character.
113 * <escape> means they changed their minds.
116 if (ev
.e_value
== K_VLNEXT
) {
120 if (ev
.e_value
== K_ESCAPE
)
124 vip
->rvalue
= ev
.e_value
;
128 F_SET(sp
, SC_EXIT_FORCE
);
131 /* <interrupt> means they changed their minds. */
134 /* <resize> interrupts the input mode. */
135 v_emsg(sp
, NULL
, VIM_WRESIZE
);
138 if (v_erepaint(sp
, &ev
))
142 v_event_err(sp
, &ev
);
148 GET_SPACE_RETW(sp
, bp
, blen
, len
);
153 * Versions of nvi before 1.57 created N new lines when they replaced
154 * N characters with <carriage-return> or <newline> characters. This
155 * is different from the historic vi, which replaced N characters with
156 * a single new line. Users complained, so we match historic practice.
158 if ((!quote
&& vip
->rvalue
== K_CR
) || vip
->rvalue
== K_NL
) {
159 /* Set return line. */
160 vp
->m_stop
.lno
= vp
->m_start
.lno
+ 1;
163 /* The first part of the current line. */
164 if (db_set(sp
, vp
->m_start
.lno
, p
, vp
->m_start
.cno
))
168 * The rest of the current line. And, of course, now it gets
169 * tricky. If there are characters left in the line and if
170 * the autoindent edit option is set, white space after the
171 * replaced character is discarded, autoindent is applied, and
172 * the cursor moves to the last indent character.
174 p
+= vp
->m_start
.cno
+ cnt
;
175 len
-= vp
->m_start
.cno
+ cnt
;
176 if (len
!= 0 && O_ISSET(sp
, O_AUTOINDENT
))
177 for (; len
&& isblank(*p
); --len
, ++p
);
179 if ((tp
= text_init(sp
, p
, len
, len
)) == NULL
)
182 if (len
!= 0 && O_ISSET(sp
, O_AUTOINDENT
)) {
183 if (v_txt_auto(sp
, vp
->m_start
.lno
, NULL
, 0, tp
))
185 vp
->m_stop
.cno
= tp
->ai
? tp
->ai
- 1 : 0;
189 vp
->m_stop
.cno
= tp
->ai
? tp
->ai
- 1 : 0;
190 if (db_append(sp
, 1, vp
->m_start
.lno
, tp
->lb
, tp
->len
))
197 STRSET(bp
+ vp
->m_start
.cno
, vip
->rlast
, cnt
);
198 rval
= db_set(sp
, vp
->m_start
.lno
, bp
, len
);
200 FREE_SPACEW(sp
, bp
, blen
);
202 vp
->m_final
= vp
->m_stop
;