small screen updating optimization in gtk front end
[nvi.git] / vi / v_replace.c
blob44dda43e1ed51ae879d8a7f1312b2701c82a78ad
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_replace.c,v 10.23 2000/07/21 17:35:05 skimo Exp $ (Berkeley) $Date: 2000/07/21 17:35:05 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
29 #include "vi.h"
32 * v_replace -- [count]r<char>
34 * !!!
35 * The r command in historic vi was almost beautiful in its badness. For
36 * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted
37 * a single character. "Nr<carriage return>", where N was greater than 1,
38 * inserted a single carriage return. "r<escape>" did cancel the command,
39 * but "r<literal><escape>" erased a single character. To enter a literal
40 * <literal> character, it required three <literal> characters after the
41 * command. This may not be right, but at least it's not insane.
43 * PUBLIC: int v_replace __P((SCR *, VICMD *));
45 int
46 v_replace(sp, vp)
47 SCR *sp;
48 VICMD *vp;
50 EVENT ev;
51 VI_PRIVATE *vip;
52 TEXT *tp;
53 size_t blen, len;
54 u_long cnt;
55 int quote, rval;
56 CHAR_T *bp;
57 CHAR_T *p;
59 vip = VIP(sp);
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))
73 return (1);
74 if (len == 0) {
75 msgq(sp, M_BERR, "186|No characters to replace");
76 return (1);
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);
94 return (1);
98 * If it's not a repeat, reset the current mode and get a replacement
99 * character.
101 quote = 0;
102 if (!F_ISSET(vp, VC_ISDOT)) {
103 sp->showmode = SM_REPLACE;
104 if (vs_refresh(sp, 0))
105 return (1);
106 next: if (v_event_get(sp, &ev, 0, 0))
107 return (1);
109 switch (ev.e_event) {
110 case E_CHARACTER:
112 * <literal_next> means escape the next character.
113 * <escape> means they changed their minds.
115 if (!quote) {
116 if (ev.e_value == K_VLNEXT) {
117 quote = 1;
118 goto next;
120 if (ev.e_value == K_ESCAPE)
121 return (0);
123 vip->rlast = ev.e_c;
124 vip->rvalue = ev.e_value;
125 break;
126 case E_ERR:
127 case E_EOF:
128 F_SET(sp, SC_EXIT_FORCE);
129 return (1);
130 case E_INTERRUPT:
131 /* <interrupt> means they changed their minds. */
132 return (0);
133 case E_WRESIZE:
134 /* <resize> interrupts the input mode. */
135 v_emsg(sp, NULL, VIM_WRESIZE);
136 return (0);
137 case E_REPAINT:
138 if (v_erepaint(sp, &ev))
139 return (1);
140 goto next;
141 default:
142 v_event_err(sp, &ev);
143 return (0);
147 /* Copy the line. */
148 GET_SPACE_RETW(sp, bp, blen, len);
149 MEMMOVE(bp, p, len);
150 p = bp;
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;
161 vp->m_stop.cno = 0;
163 /* The first part of the current line. */
164 if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno))
165 goto err_ret;
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)
180 goto err_ret;
182 if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) {
183 if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp))
184 goto err_ret;
185 vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0;
186 } else
187 vp->m_stop.cno = 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))
191 err_ret: rval = 1;
192 else {
193 text_free(tp);
194 rval = 0;
196 } else {
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;
203 return (rval);