1 /* $NetBSD: v_replace.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: v_replace.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
34 #include "../common/common.h"
38 * v_replace -- [count]r<char>
41 * The r command in historic vi was almost beautiful in its badness. For
42 * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted
43 * a single character. "Nr<carriage return>", where N was greater than 1,
44 * inserted a single carriage return. "r<escape>" did cancel the command,
45 * but "r<literal><escape>" erased a single character. To enter a literal
46 * <literal> character, it required three <literal> characters after the
47 * command. This may not be right, but at least it's not insane.
49 * PUBLIC: int v_replace __P((SCR *, VICMD *));
52 v_replace(SCR
*sp
, VICMD
*vp
)
66 * If the line doesn't exist, or it's empty, replacement isn't
67 * allowed. It's not hard to implement, but:
69 * 1: It's historic practice (vi beeped before the replacement
70 * character was even entered).
71 * 2: For consistency, this change would require that the more
72 * general case, "Nr", when the user is < N characters from
73 * the end of the line, also work, which would be a bit odd.
74 * 3: Replacing with a <newline> has somewhat odd semantics.
76 if (db_get(sp
, vp
->m_start
.lno
, DBG_FATAL
, &p
, &len
))
79 msgq(sp
, M_BERR
, "186|No characters to replace");
84 * Figure out how many characters to be replace. For no particular
85 * reason (other than that the semantics of replacing the newline
86 * are confusing) only permit the replacement of the characters in
87 * the current line. I suppose we could append replacement characters
88 * to the line, but I see no compelling reason to do so. Check this
89 * before we get the character to match historic practice, where Nr
90 * failed immediately if there were less than N characters from the
91 * cursor to the end of the line.
93 cnt
= F_ISSET(vp
, VC_C1SET
) ? vp
->count
: 1;
94 vp
->m_stop
.lno
= vp
->m_start
.lno
;
95 vp
->m_stop
.cno
= vp
->m_start
.cno
+ cnt
- 1;
96 if (vp
->m_stop
.cno
> len
- 1) {
97 v_eol(sp
, &vp
->m_start
);
102 * If it's not a repeat, reset the current mode and get a replacement
106 if (!F_ISSET(vp
, VC_ISDOT
)) {
107 sp
->showmode
= SM_REPLACE
;
108 if (vs_refresh(sp
, 0))
110 next
: if (v_event_get(sp
, &ev
, 0, 0))
113 switch (ev
.e_event
) {
116 * <literal_next> means escape the next character.
117 * <escape> means they changed their minds.
120 if (ev
.e_value
== K_VLNEXT
) {
124 if (ev
.e_value
== K_ESCAPE
)
128 vip
->rvalue
= ev
.e_value
;
132 F_SET(sp
, SC_EXIT_FORCE
);
135 /* <interrupt> means they changed their minds. */
138 /* <resize> interrupts the input mode. */
139 v_emsg(sp
, NULL
, VIM_WRESIZE
);
142 if (v_erepaint(sp
, &ev
))
146 v_event_err(sp
, &ev
);
152 GET_SPACE_RETW(sp
, bp
, blen
, len
);
157 * Versions of nvi before 1.57 created N new lines when they replaced
158 * N characters with <carriage-return> or <newline> characters. This
159 * is different from the historic vi, which replaced N characters with
160 * a single new line. Users complained, so we match historic practice.
162 if ((!quote
&& vip
->rvalue
== K_CR
) || vip
->rvalue
== K_NL
) {
163 /* Set return line. */
164 vp
->m_stop
.lno
= vp
->m_start
.lno
+ 1;
167 /* The first part of the current line. */
168 if (db_set(sp
, vp
->m_start
.lno
, p
, vp
->m_start
.cno
))
172 * The rest of the current line. And, of course, now it gets
173 * tricky. If there are characters left in the line and if
174 * the autoindent edit option is set, white space after the
175 * replaced character is discarded, autoindent is applied, and
176 * the cursor moves to the last indent character.
178 p
+= vp
->m_start
.cno
+ cnt
;
179 len
-= vp
->m_start
.cno
+ cnt
;
180 if (len
!= 0 && O_ISSET(sp
, O_AUTOINDENT
))
181 for (; len
&& ISBLANK((UCHAR_T
)*p
); --len
, ++p
);
183 if ((tp
= text_init(sp
, p
, len
, len
)) == NULL
)
186 if (len
!= 0 && O_ISSET(sp
, O_AUTOINDENT
)) {
187 if (v_txt_auto(sp
, vp
->m_start
.lno
, NULL
, 0, tp
))
189 vp
->m_stop
.cno
= tp
->ai
? tp
->ai
- 1 : 0;
193 vp
->m_stop
.cno
= tp
->ai
? tp
->ai
- 1 : 0;
194 if (db_append(sp
, 1, vp
->m_start
.lno
, tp
->lb
, tp
->len
))
201 STRSET(bp
+ vp
->m_start
.cno
, vip
->rlast
, cnt
);
202 rval
= db_set(sp
, vp
->m_start
.lno
, bp
, len
);
204 FREE_SPACEW(sp
, bp
, blen
);
206 vp
->m_final
= vp
->m_stop
;