119304Speter/*- 219304Speter * Copyright (c) 1992, 1993, 1994 319304Speter * The Regents of the University of California. All rights reserved. 419304Speter * Copyright (c) 1992, 1993, 1994, 1995, 1996 519304Speter * Keith Bostic. All rights reserved. 619304Speter * 719304Speter * See the LICENSE file for redistribution information. 819304Speter */ 919304Speter 1019304Speter#include "config.h" 1119304Speter 1219304Speter#ifndef lint 13254225Speterstatic const char sccsid[] = "$Id: v_replace.c,v 10.24 2001/06/25 15:19:34 skimo Exp $"; 1419304Speter#endif /* not lint */ 1519304Speter 1619304Speter#include <sys/types.h> 1719304Speter#include <sys/queue.h> 1819304Speter#include <sys/time.h> 1919304Speter 2019304Speter#include <bitstring.h> 2119304Speter#include <ctype.h> 2219304Speter#include <errno.h> 2319304Speter#include <limits.h> 2419304Speter#include <stdio.h> 2519304Speter#include <stdlib.h> 2619304Speter#include <string.h> 2719304Speter 2819304Speter#include "../common/common.h" 2919304Speter#include "vi.h" 3019304Speter 3119304Speter/* 3219304Speter * v_replace -- [count]r<char> 3319304Speter * 3419304Speter * !!! 3519304Speter * The r command in historic vi was almost beautiful in its badness. For 3619304Speter * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted 3719304Speter * a single character. "Nr<carriage return>", where N was greater than 1, 3819304Speter * inserted a single carriage return. "r<escape>" did cancel the command, 3919304Speter * but "r<literal><escape>" erased a single character. To enter a literal 4019304Speter * <literal> character, it required three <literal> characters after the 4119304Speter * command. This may not be right, but at least it's not insane. 4219304Speter * 4319304Speter * PUBLIC: int v_replace __P((SCR *, VICMD *)); 4419304Speter */ 4519304Speterint 46254225Speterv_replace(SCR *sp, VICMD *vp) 4719304Speter{ 4819304Speter EVENT ev; 4919304Speter VI_PRIVATE *vip; 5019304Speter TEXT *tp; 5119304Speter size_t blen, len; 5219304Speter u_long cnt; 5319304Speter int quote, rval; 54254225Speter CHAR_T *bp; 55254225Speter CHAR_T *p; 5619304Speter 5719304Speter vip = VIP(sp); 5819304Speter 5919304Speter /* 6019304Speter * If the line doesn't exist, or it's empty, replacement isn't 6119304Speter * allowed. It's not hard to implement, but: 6219304Speter * 6319304Speter * 1: It's historic practice (vi beeped before the replacement 6419304Speter * character was even entered). 6519304Speter * 2: For consistency, this change would require that the more 6619304Speter * general case, "Nr", when the user is < N characters from 6719304Speter * the end of the line, also work, which would be a bit odd. 6819304Speter * 3: Replacing with a <newline> has somewhat odd semantics. 6919304Speter */ 7019304Speter if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len)) 7119304Speter return (1); 7219304Speter if (len == 0) { 7319304Speter msgq(sp, M_BERR, "186|No characters to replace"); 7419304Speter return (1); 7519304Speter } 7619304Speter 7719304Speter /* 7819304Speter * Figure out how many characters to be replace. For no particular 7919304Speter * reason (other than that the semantics of replacing the newline 8019304Speter * are confusing) only permit the replacement of the characters in 8119304Speter * the current line. I suppose we could append replacement characters 8219304Speter * to the line, but I see no compelling reason to do so. Check this 8319304Speter * before we get the character to match historic practice, where Nr 8419304Speter * failed immediately if there were less than N characters from the 8519304Speter * cursor to the end of the line. 8619304Speter */ 8719304Speter cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; 8819304Speter vp->m_stop.lno = vp->m_start.lno; 8919304Speter vp->m_stop.cno = vp->m_start.cno + cnt - 1; 9019304Speter if (vp->m_stop.cno > len - 1) { 9119304Speter v_eol(sp, &vp->m_start); 9219304Speter return (1); 9319304Speter } 9419304Speter 9519304Speter /* 9619304Speter * If it's not a repeat, reset the current mode and get a replacement 9719304Speter * character. 9819304Speter */ 9919304Speter quote = 0; 10019304Speter if (!F_ISSET(vp, VC_ISDOT)) { 10119304Speter sp->showmode = SM_REPLACE; 10219304Speter if (vs_refresh(sp, 0)) 10319304Speter return (1); 10419304Speternext: if (v_event_get(sp, &ev, 0, 0)) 10519304Speter return (1); 10619304Speter 10719304Speter switch (ev.e_event) { 10819304Speter case E_CHARACTER: 10919304Speter /* 11019304Speter * <literal_next> means escape the next character. 11119304Speter * <escape> means they changed their minds. 11219304Speter */ 11319304Speter if (!quote) { 11419304Speter if (ev.e_value == K_VLNEXT) { 11519304Speter quote = 1; 11619304Speter goto next; 11719304Speter } 11819304Speter if (ev.e_value == K_ESCAPE) 11919304Speter return (0); 12019304Speter } 12119304Speter vip->rlast = ev.e_c; 12219304Speter vip->rvalue = ev.e_value; 12319304Speter break; 12419304Speter case E_ERR: 12519304Speter case E_EOF: 12619304Speter F_SET(sp, SC_EXIT_FORCE); 12719304Speter return (1); 12819304Speter case E_INTERRUPT: 12919304Speter /* <interrupt> means they changed their minds. */ 13019304Speter return (0); 13119304Speter case E_WRESIZE: 13219304Speter /* <resize> interrupts the input mode. */ 13319304Speter v_emsg(sp, NULL, VIM_WRESIZE); 13419304Speter return (0); 13519304Speter case E_REPAINT: 13619304Speter if (vs_repaint(sp, &ev)) 13719304Speter return (1); 13819304Speter goto next; 13919304Speter default: 14019304Speter v_event_err(sp, &ev); 14119304Speter return (0); 14219304Speter } 14319304Speter } 14419304Speter 14519304Speter /* Copy the line. */ 146254225Speter GET_SPACE_RETW(sp, bp, blen, len); 147254225Speter MEMMOVE(bp, p, len); 14819304Speter p = bp; 14919304Speter 15019304Speter /* 15119304Speter * Versions of nvi before 1.57 created N new lines when they replaced 15219304Speter * N characters with <carriage-return> or <newline> characters. This 15319304Speter * is different from the historic vi, which replaced N characters with 15419304Speter * a single new line. Users complained, so we match historic practice. 15519304Speter */ 156254225Speter if ((!quote && vip->rvalue == K_CR) || vip->rvalue == K_NL) { 15719304Speter /* Set return line. */ 15819304Speter vp->m_stop.lno = vp->m_start.lno + 1; 15919304Speter vp->m_stop.cno = 0; 16019304Speter 16119304Speter /* The first part of the current line. */ 16219304Speter if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno)) 16319304Speter goto err_ret; 16419304Speter 16519304Speter /* 16619304Speter * The rest of the current line. And, of course, now it gets 16719304Speter * tricky. If there are characters left in the line and if 16819304Speter * the autoindent edit option is set, white space after the 16919304Speter * replaced character is discarded, autoindent is applied, and 17019304Speter * the cursor moves to the last indent character. 17119304Speter */ 17219304Speter p += vp->m_start.cno + cnt; 17319304Speter len -= vp->m_start.cno + cnt; 17419304Speter if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) 17519304Speter for (; len && isblank(*p); --len, ++p); 17619304Speter 17719304Speter if ((tp = text_init(sp, p, len, len)) == NULL) 17819304Speter goto err_ret; 17919304Speter 18019304Speter if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) { 18119304Speter if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp)) 18219304Speter goto err_ret; 18319304Speter vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; 18419304Speter } else 18519304Speter vp->m_stop.cno = 0; 18619304Speter 18719304Speter vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0; 18819304Speter if (db_append(sp, 1, vp->m_start.lno, tp->lb, tp->len)) 18919304Spetererr_ret: rval = 1; 19019304Speter else { 19119304Speter text_free(tp); 19219304Speter rval = 0; 19319304Speter } 19419304Speter } else { 195254225Speter STRSET(bp + vp->m_start.cno, vip->rlast, cnt); 19619304Speter rval = db_set(sp, vp->m_start.lno, bp, len); 19719304Speter } 198254225Speter FREE_SPACEW(sp, bp, blen); 19919304Speter 20019304Speter vp->m_final = vp->m_stop; 20119304Speter return (rval); 20219304Speter} 203