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. 6 * 7 * See the LICENSE file for redistribution information. 8 */ 9 10 #include "config.h" 11 12 #ifndef lint 13 static const char sccsid[] = "$Id: v_replace.c,v 10.24 2001/06/25 15:19:34 skimo Exp $"; 14 #endif /* not lint */ 15 16 #include <sys/types.h> 17 #include <sys/queue.h> 18 #include <sys/time.h> 19 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> 27 28 #include "../common/common.h" 29 #include "vi.h" 30 31 /* 32 * v_replace -- [count]r<char> 33 * 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. 42 * 43 * PUBLIC: int v_replace __P((SCR *, VICMD *)); 44 */ 45 int 46 v_replace(SCR *sp, VICMD *vp) 47 { 48 EVENT ev; 49 VI_PRIVATE *vip; 50 TEXT *tp; 51 size_t blen, len; 52 u_long cnt; 53 int quote, rval; 54 CHAR_T *bp; 55 CHAR_T *p; 56 57 vip = VIP(sp); 58 59 /* 60 * If the line doesn't exist, or it's empty, replacement isn't 61 * allowed. It's not hard to implement, but: 62 * 63 * 1: It's historic practice (vi beeped before the replacement 64 * character was even entered). 65 * 2: For consistency, this change would require that the more 66 * general case, "Nr", when the user is < N characters from 67 * the end of the line, also work, which would be a bit odd. 68 * 3: Replacing with a <newline> has somewhat odd semantics. 69 */ 70 if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len)) 71 return (1); 72 if (len == 0) { 73 msgq(sp, M_BERR, "186|No characters to replace"); 74 return (1); 75 } 76 77 /* 78 * Figure out how many characters to be replace. For no particular 79 * reason (other than that the semantics of replacing the newline 80 * are confusing) only permit the replacement of the characters in 81 * the current line. I suppose we could append replacement characters 82 * to the line, but I see no compelling reason to do so. Check this 83 * before we get the character to match historic practice, where Nr 84 * failed immediately if there were less than N characters from the 85 * cursor to the end of the line. 86 */ 87 cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; 88 vp->m_stop.lno = vp->m_start.lno; 89 vp->m_stop.cno = vp->m_start.cno + cnt - 1; 90 if (vp->m_stop.cno > len - 1) { 91 v_eol(sp, &vp->m_start); 92 return (1); 93 } 94 95 /* 96 * If it's not a repeat, reset the current mode and get a replacement 97 * character. 98 */ 99 quote = 0; 100 if (!F_ISSET(vp, VC_ISDOT)) { 101 sp->showmode = SM_REPLACE; 102 if (vs_refresh(sp, 0)) 103 return (1); 104 next: if (v_event_get(sp, &ev, 0, 0)) 105 return (1); 106 107 switch (ev.e_event) { 108 case E_CHARACTER: 109 /* 110 * <literal_next> means escape the next character. 111 * <escape> means they changed their minds. 112 */ 113 if (!quote) { 114 if (ev.e_value == K_VLNEXT) { 115 quote = 1; 116 goto next; 117 } 118 if (ev.e_value == K_ESCAPE) 119 return (0); 120 } 121 vip->rlast = ev.e_c; 122 vip->rvalue = ev.e_value; 123 break; 124 case E_ERR: 125 case E_EOF: 126 F_SET(sp, SC_EXIT_FORCE); 127 return (1); 128 case E_INTERRUPT: 129 /* <interrupt> means they changed their minds. */ 130 return (0); 131 case E_WRESIZE: 132 /* <resize> interrupts the input mode. */ 133 v_emsg(sp, NULL, VIM_WRESIZE); 134 return (0); 135 case E_REPAINT: 136 if (vs_repaint(sp, &ev)) 137 return (1); 138 goto next; 139 default: 140 v_event_err(sp, &ev); 141 return (0); 142 } 143 } 144 145 /* Copy the line. */ 146 GET_SPACE_RETW(sp, bp, blen, len); 147 MEMMOVE(bp, p, len); 148 p = bp; 149 150 /* 151 * Versions of nvi before 1.57 created N new lines when they replaced 152 * N characters with <carriage-return> or <newline> characters. This 153 * is different from the historic vi, which replaced N characters with 154 * a single new line. Users complained, so we match historic practice. 155 */ 156 if ((!quote && vip->rvalue == K_CR) || vip->rvalue == K_NL) { 157 /* Set return line. */ 158 vp->m_stop.lno = vp->m_start.lno + 1; 159 vp->m_stop.cno = 0; 160 161 /* The first part of the current line. */ 162 if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno)) 163 goto err_ret; 164 165 /* 166 * The rest of the current line. And, of course, now it gets 167 * tricky. If there are characters left in the line and if 168 * the autoindent edit option is set, white space after the 169 * replaced character is discarded, autoindent is applied, and 170 * the cursor moves to the last indent character. 171 */ 172 p += vp->m_start.cno + cnt; 173 len -= vp->m_start.cno + cnt; 174 if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) 175 for (; len && isblank(*p); --len, ++p); 176 177 if ((tp = text_init(sp, p, len, len)) == NULL) 178 goto err_ret; 179 180 if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) { 181 if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp)) 182 goto err_ret; 183 vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; 184 } else 185 vp->m_stop.cno = 0; 186 187 vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; 188 if (db_append(sp, 1, vp->m_start.lno, tp->lb, tp->len)) 189 err_ret: rval = 1; 190 else { 191 text_free(tp); 192 rval = 0; 193 } 194 } else { 195 STRSET(bp + vp->m_start.cno, vip->rlast, cnt); 196 rval = db_set(sp, vp->m_start.lno, bp, len); 197 } 198 FREE_SPACEW(sp, bp, blen); 199 200 vp->m_final = vp->m_stop; 201 return (rval); 202 } 203