acpi: Narrow workaround for broken interrupt settings
[dragonfly.git] / contrib / nvi2 / vi / v_replace.c
blobc0493e809e2447144bf2a44308f3da31e0d6633d
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "../common/common.h"
25 #include "vi.h"
28 * v_replace -- [count]r<char>
30 * !!!
31 * The r command in historic vi was almost beautiful in its badness. For
32 * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted
33 * a single character. "Nr<carriage return>", where N was greater than 1,
34 * inserted a single carriage return. "r<escape>" did cancel the command,
35 * but "r<literal><escape>" erased a single character. To enter a literal
36 * <literal> character, it required three <literal> characters after the
37 * command. This may not be right, but at least it's not insane.
39 * PUBLIC: int v_replace(SCR *, VICMD *);
41 int
42 v_replace(SCR *sp, VICMD *vp)
44 EVENT ev;
45 VI_PRIVATE *vip;
46 TEXT *tp;
47 size_t blen, len;
48 u_long cnt;
49 int quote, rval;
50 CHAR_T *bp;
51 CHAR_T *p;
53 vip = VIP(sp);
56 * If the line doesn't exist, or it's empty, replacement isn't
57 * allowed. It's not hard to implement, but:
59 * 1: It's historic practice (vi beeped before the replacement
60 * character was even entered).
61 * 2: For consistency, this change would require that the more
62 * general case, "Nr", when the user is < N characters from
63 * the end of the line, also work, which would be a bit odd.
64 * 3: Replacing with a <newline> has somewhat odd semantics.
66 if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len))
67 return (1);
68 if (len == 0) {
69 msgq(sp, M_BERR, "186|No characters to replace");
70 return (1);
74 * Figure out how many characters to be replace. For no particular
75 * reason (other than that the semantics of replacing the newline
76 * are confusing) only permit the replacement of the characters in
77 * the current line. I suppose we could append replacement characters
78 * to the line, but I see no compelling reason to do so. Check this
79 * before we get the character to match historic practice, where Nr
80 * failed immediately if there were less than N characters from the
81 * cursor to the end of the line.
83 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
84 vp->m_stop.lno = vp->m_start.lno;
85 vp->m_stop.cno = vp->m_start.cno + cnt - 1;
86 if (vp->m_stop.cno > len - 1) {
87 v_eol(sp, &vp->m_start);
88 return (1);
92 * If it's not a repeat, reset the current mode and get a replacement
93 * character.
95 quote = 0;
96 if (!F_ISSET(vp, VC_ISDOT)) {
97 sp->showmode = SM_REPLACE;
98 if (vs_refresh(sp, 0))
99 return (1);
100 next: if (v_event_get(sp, &ev, 0, 0))
101 return (1);
103 switch (ev.e_event) {
104 case E_CHARACTER:
106 * <literal_next> means escape the next character.
107 * <escape> means they changed their minds.
109 if (!quote) {
110 if (ev.e_value == K_VLNEXT) {
111 quote = 1;
112 goto next;
114 if (ev.e_value == K_ESCAPE)
115 return (0);
117 vip->rlast = ev.e_c;
118 vip->rvalue = ev.e_value;
119 break;
120 case E_ERR:
121 case E_EOF:
122 F_SET(sp, SC_EXIT_FORCE);
123 return (1);
124 case E_INTERRUPT:
125 /* <interrupt> means they changed their minds. */
126 return (0);
127 case E_WRESIZE:
128 /* <resize> interrupts the input mode. */
129 v_emsg(sp, NULL, VIM_WRESIZE);
130 return (0);
131 case E_REPAINT:
132 if (vs_repaint(sp, &ev))
133 return (1);
134 goto next;
135 default:
136 v_event_err(sp, &ev);
137 return (0);
141 /* Copy the line. */
142 GET_SPACE_RETW(sp, bp, blen, len);
143 MEMMOVE(bp, p, len);
144 p = bp;
147 * Versions of nvi before 1.57 created N new lines when they replaced
148 * N characters with <carriage-return> or <newline> characters. This
149 * is different from the historic vi, which replaced N characters with
150 * a single new line. Users complained, so we match historic practice.
152 if ((!quote && vip->rvalue == K_CR) || vip->rvalue == K_NL) {
153 /* Set return line. */
154 vp->m_stop.lno = vp->m_start.lno + 1;
155 vp->m_stop.cno = 0;
157 /* The first part of the current line. */
158 if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno))
159 goto err_ret;
162 * The rest of the current line. And, of course, now it gets
163 * tricky. If there are characters left in the line and if
164 * the autoindent edit option is set, white space after the
165 * replaced character is discarded, autoindent is applied, and
166 * the cursor moves to the last indent character.
168 p += vp->m_start.cno + cnt;
169 len -= vp->m_start.cno + cnt;
170 if (len != 0 && O_ISSET(sp, O_AUTOINDENT))
171 for (; len && isblank(*p); --len, ++p);
173 if ((tp = text_init(sp, p, len, len)) == NULL)
174 goto err_ret;
176 if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) {
177 if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp))
178 goto err_ret;
179 vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0;
180 } else
181 vp->m_stop.cno = 0;
183 vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0;
184 if (db_append(sp, 1, vp->m_start.lno, tp->lb, tp->len))
185 err_ret: rval = 1;
186 else {
187 text_free(tp);
188 rval = 0;
190 } else {
191 STRSET(bp + vp->m_start.cno, vip->rlast, cnt);
192 rval = db_set(sp, vp->m_start.lno, bp, len);
194 FREE_SPACEW(sp, bp, blen);
196 vp->m_final = vp->m_stop;
197 return (rval);