1/* $NetBSD: v_replace.c,v 1.2 2008/12/05 22:51:43 christos Exp $ */ 2 3/*- 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. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12#include "config.h" 13 14#ifndef lint 15static 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"; 16#endif /* not lint */ 17 18#include <sys/types.h> 19#include <sys/queue.h> 20#include <sys/time.h> 21 22#include <bitstring.h> 23#include <ctype.h> 24#include <errno.h> 25#include <limits.h> 26#include <stdio.h> 27#include <stdlib.h> 28#include <string.h> 29 30#include "../common/common.h" 31#include "vi.h" 32 33/* 34 * v_replace -- [count]r<char> 35 * 36 * !!! 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. 44 * 45 * PUBLIC: int v_replace __P((SCR *, VICMD *)); 46 */ 47int 48v_replace(SCR *sp, VICMD *vp) 49{ 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; 58 59 vip = VIP(sp); 60 61 /* 62 * If the line doesn't exist, or it's empty, replacement isn't 63 * allowed. It's not hard to implement, but: 64 * 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. 71 */ 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); 77 } 78 79 /* 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. 88 */ 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); 95 } 96 97 /* 98 * If it's not a repeat, reset the current mode and get a replacement 99 * character. 100 */ 101 quote = 0; 102 if (!F_ISSET(vp, VC_ISDOT)) { 103 sp->showmode = SM_REPLACE; 104 if (vs_refresh(sp, 0)) 105 return (1); 106next: if (v_event_get(sp, &ev, 0, 0)) 107 return (1); 108 109 switch (ev.e_event) { 110 case E_CHARACTER: 111 /* 112 * <literal_next> means escape the next character. 113 * <escape> means they changed their minds. 114 */ 115 if (!quote) { 116 if (ev.e_value == K_VLNEXT) { 117 quote = 1; 118 goto next; 119 } 120 if (ev.e_value == K_ESCAPE) 121 return (0); 122 } 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); 144 } 145 } 146 147 /* Copy the line. */ 148 GET_SPACE_RETW(sp, bp, blen, len); 149 MEMMOVE(bp, p, len); 150 p = bp; 151 152 /* 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. 157 */ 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; 162 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; 166 167 /* 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. 173 */ 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((UCHAR_T)*p); --len, ++p); 178 179 if ((tp = text_init(sp, p, len, len)) == NULL) 180 goto err_ret; 181 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; 188 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)) 191err_ret: rval = 1; 192 else { 193 text_free(tp); 194 rval = 0; 195 } 196 } else { 197 STRSET(bp + vp->m_start.cno, vip->rlast, cnt); 198 rval = db_set(sp, vp->m_start.lno, bp, len); 199 } 200 FREE_SPACEW(sp, bp, blen); 201 202 vp->m_final = vp->m_stop; 203 return (rval); 204} 205