xref: /minix3/external/bsd/nvi/dist/vi/v_replace.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: v_replace.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: v_replace.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/time.h>
2584d9c625SLionel Sambuc 
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <ctype.h>
2884d9c625SLionel Sambuc #include <errno.h>
2984d9c625SLionel Sambuc #include <limits.h>
3084d9c625SLionel Sambuc #include <stdio.h>
3184d9c625SLionel Sambuc #include <stdlib.h>
3284d9c625SLionel Sambuc #include <string.h>
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc #include "vi.h"
3684d9c625SLionel Sambuc 
3784d9c625SLionel Sambuc /*
3884d9c625SLionel Sambuc  * v_replace -- [count]r<char>
3984d9c625SLionel Sambuc  *
4084d9c625SLionel Sambuc  * !!!
4184d9c625SLionel Sambuc  * The r command in historic vi was almost beautiful in its badness.  For
4284d9c625SLionel Sambuc  * example, "r<erase>" and "r<word erase>" beeped the terminal and deleted
4384d9c625SLionel Sambuc  * a single character.  "Nr<carriage return>", where N was greater than 1,
4484d9c625SLionel Sambuc  * inserted a single carriage return.  "r<escape>" did cancel the command,
4584d9c625SLionel Sambuc  * but "r<literal><escape>" erased a single character.  To enter a literal
4684d9c625SLionel Sambuc  * <literal> character, it required three <literal> characters after the
4784d9c625SLionel Sambuc  * command.  This may not be right, but at least it's not insane.
4884d9c625SLionel Sambuc  *
4984d9c625SLionel Sambuc  * PUBLIC: int v_replace __P((SCR *, VICMD *));
5084d9c625SLionel Sambuc  */
5184d9c625SLionel Sambuc int
v_replace(SCR * sp,VICMD * vp)5284d9c625SLionel Sambuc v_replace(SCR *sp, VICMD *vp)
5384d9c625SLionel Sambuc {
5484d9c625SLionel Sambuc 	EVENT ev;
5584d9c625SLionel Sambuc 	VI_PRIVATE *vip;
5684d9c625SLionel Sambuc 	TEXT *tp;
5784d9c625SLionel Sambuc 	size_t blen, len;
5884d9c625SLionel Sambuc 	u_long cnt;
5984d9c625SLionel Sambuc 	int quote, rval;
6084d9c625SLionel Sambuc 	CHAR_T *bp;
6184d9c625SLionel Sambuc 	CHAR_T *p;
6284d9c625SLionel Sambuc 
6384d9c625SLionel Sambuc 	vip = VIP(sp);
6484d9c625SLionel Sambuc 
6584d9c625SLionel Sambuc 	/*
6684d9c625SLionel Sambuc 	 * If the line doesn't exist, or it's empty, replacement isn't
6784d9c625SLionel Sambuc 	 * allowed.  It's not hard to implement, but:
6884d9c625SLionel Sambuc 	 *
6984d9c625SLionel Sambuc 	 *	1: It's historic practice (vi beeped before the replacement
7084d9c625SLionel Sambuc 	 *	   character was even entered).
7184d9c625SLionel Sambuc 	 *	2: For consistency, this change would require that the more
7284d9c625SLionel Sambuc 	 *	   general case, "Nr", when the user is < N characters from
7384d9c625SLionel Sambuc 	 *	   the end of the line, also work, which would be a bit odd.
7484d9c625SLionel Sambuc 	 *	3: Replacing with a <newline> has somewhat odd semantics.
7584d9c625SLionel Sambuc 	 */
7684d9c625SLionel Sambuc 	if (db_get(sp, vp->m_start.lno, DBG_FATAL, &p, &len))
7784d9c625SLionel Sambuc 		return (1);
7884d9c625SLionel Sambuc 	if (len == 0) {
7984d9c625SLionel Sambuc 		msgq(sp, M_BERR, "186|No characters to replace");
8084d9c625SLionel Sambuc 		return (1);
8184d9c625SLionel Sambuc 	}
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc 	/*
8484d9c625SLionel Sambuc 	 * Figure out how many characters to be replace.  For no particular
8584d9c625SLionel Sambuc 	 * reason (other than that the semantics of replacing the newline
8684d9c625SLionel Sambuc 	 * are confusing) only permit the replacement of the characters in
8784d9c625SLionel Sambuc 	 * the current line.  I suppose we could append replacement characters
8884d9c625SLionel Sambuc 	 * to the line, but I see no compelling reason to do so.  Check this
8984d9c625SLionel Sambuc 	 * before we get the character to match historic practice, where Nr
9084d9c625SLionel Sambuc 	 * failed immediately if there were less than N characters from the
9184d9c625SLionel Sambuc 	 * cursor to the end of the line.
9284d9c625SLionel Sambuc 	 */
9384d9c625SLionel Sambuc 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
9484d9c625SLionel Sambuc 	vp->m_stop.lno = vp->m_start.lno;
9584d9c625SLionel Sambuc 	vp->m_stop.cno = vp->m_start.cno + cnt - 1;
9684d9c625SLionel Sambuc 	if (vp->m_stop.cno > len - 1) {
9784d9c625SLionel Sambuc 		v_eol(sp, &vp->m_start);
9884d9c625SLionel Sambuc 		return (1);
9984d9c625SLionel Sambuc 	}
10084d9c625SLionel Sambuc 
10184d9c625SLionel Sambuc 	/*
10284d9c625SLionel Sambuc 	 * If it's not a repeat, reset the current mode and get a replacement
10384d9c625SLionel Sambuc 	 * character.
10484d9c625SLionel Sambuc 	 */
10584d9c625SLionel Sambuc 	quote = 0;
10684d9c625SLionel Sambuc 	if (!F_ISSET(vp, VC_ISDOT)) {
10784d9c625SLionel Sambuc 		sp->showmode = SM_REPLACE;
10884d9c625SLionel Sambuc 		if (vs_refresh(sp, 0))
10984d9c625SLionel Sambuc 			return (1);
11084d9c625SLionel Sambuc next:		if (v_event_get(sp, &ev, 0, 0))
11184d9c625SLionel Sambuc 			return (1);
11284d9c625SLionel Sambuc 
11384d9c625SLionel Sambuc 		switch (ev.e_event) {
11484d9c625SLionel Sambuc 		case E_CHARACTER:
11584d9c625SLionel Sambuc 			/*
11684d9c625SLionel Sambuc 			 * <literal_next> means escape the next character.
11784d9c625SLionel Sambuc 			 * <escape> means they changed their minds.
11884d9c625SLionel Sambuc 			 */
11984d9c625SLionel Sambuc 			if (!quote) {
12084d9c625SLionel Sambuc 				if (ev.e_value == K_VLNEXT) {
12184d9c625SLionel Sambuc 					quote = 1;
12284d9c625SLionel Sambuc 					goto next;
12384d9c625SLionel Sambuc 				}
12484d9c625SLionel Sambuc 				if (ev.e_value == K_ESCAPE)
12584d9c625SLionel Sambuc 					return (0);
12684d9c625SLionel Sambuc 			}
12784d9c625SLionel Sambuc 			vip->rlast = ev.e_c;
12884d9c625SLionel Sambuc 			vip->rvalue = ev.e_value;
12984d9c625SLionel Sambuc 			break;
13084d9c625SLionel Sambuc 		case E_ERR:
13184d9c625SLionel Sambuc 		case E_EOF:
13284d9c625SLionel Sambuc 			F_SET(sp, SC_EXIT_FORCE);
13384d9c625SLionel Sambuc 			return (1);
13484d9c625SLionel Sambuc 		case E_INTERRUPT:
13584d9c625SLionel Sambuc 			/* <interrupt> means they changed their minds. */
13684d9c625SLionel Sambuc 			return (0);
13784d9c625SLionel Sambuc 		case E_WRESIZE:
13884d9c625SLionel Sambuc 			/* <resize> interrupts the input mode. */
13984d9c625SLionel Sambuc 			v_emsg(sp, NULL, VIM_WRESIZE);
14084d9c625SLionel Sambuc 			return (0);
14184d9c625SLionel Sambuc 		case E_REPAINT:
14284d9c625SLionel Sambuc 			if (v_erepaint(sp, &ev))
14384d9c625SLionel Sambuc 				return (1);
14484d9c625SLionel Sambuc 			goto next;
14584d9c625SLionel Sambuc 		default:
14684d9c625SLionel Sambuc 			v_event_err(sp, &ev);
14784d9c625SLionel Sambuc 			return (0);
14884d9c625SLionel Sambuc 		}
14984d9c625SLionel Sambuc 	}
15084d9c625SLionel Sambuc 
15184d9c625SLionel Sambuc 	/* Copy the line. */
15284d9c625SLionel Sambuc 	GET_SPACE_RETW(sp, bp, blen, len);
15384d9c625SLionel Sambuc 	MEMMOVE(bp, p, len);
15484d9c625SLionel Sambuc 	p = bp;
15584d9c625SLionel Sambuc 
15684d9c625SLionel Sambuc 	/*
15784d9c625SLionel Sambuc 	 * Versions of nvi before 1.57 created N new lines when they replaced
15884d9c625SLionel Sambuc 	 * N characters with <carriage-return> or <newline> characters.  This
15984d9c625SLionel Sambuc 	 * is different from the historic vi, which replaced N characters with
16084d9c625SLionel Sambuc 	 * a single new line.  Users complained, so we match historic practice.
16184d9c625SLionel Sambuc 	 */
16284d9c625SLionel Sambuc 	if ((!quote && vip->rvalue == K_CR) || vip->rvalue == K_NL) {
16384d9c625SLionel Sambuc 		/* Set return line. */
16484d9c625SLionel Sambuc 		vp->m_stop.lno = vp->m_start.lno + 1;
16584d9c625SLionel Sambuc 		vp->m_stop.cno = 0;
16684d9c625SLionel Sambuc 
16784d9c625SLionel Sambuc 		/* The first part of the current line. */
16884d9c625SLionel Sambuc 		if (db_set(sp, vp->m_start.lno, p, vp->m_start.cno))
16984d9c625SLionel Sambuc 			goto err_ret;
17084d9c625SLionel Sambuc 
17184d9c625SLionel Sambuc 		/*
17284d9c625SLionel Sambuc 		 * The rest of the current line.  And, of course, now it gets
17384d9c625SLionel Sambuc 		 * tricky.  If there are characters left in the line and if
17484d9c625SLionel Sambuc 		 * the autoindent edit option is set, white space after the
17584d9c625SLionel Sambuc 		 * replaced character is discarded, autoindent is applied, and
17684d9c625SLionel Sambuc 		 * the cursor moves to the last indent character.
17784d9c625SLionel Sambuc 		 */
17884d9c625SLionel Sambuc 		p += vp->m_start.cno + cnt;
17984d9c625SLionel Sambuc 		len -= vp->m_start.cno + cnt;
18084d9c625SLionel Sambuc 		if (len != 0 && O_ISSET(sp, O_AUTOINDENT))
18184d9c625SLionel Sambuc 			for (; len && ISBLANK((UCHAR_T)*p); --len, ++p);
18284d9c625SLionel Sambuc 
18384d9c625SLionel Sambuc 		if ((tp = text_init(sp, p, len, len)) == NULL)
18484d9c625SLionel Sambuc 			goto err_ret;
18584d9c625SLionel Sambuc 
18684d9c625SLionel Sambuc 		if (len != 0 && O_ISSET(sp, O_AUTOINDENT)) {
18784d9c625SLionel Sambuc 			if (v_txt_auto(sp, vp->m_start.lno, NULL, 0, tp))
18884d9c625SLionel Sambuc 				goto err_ret;
18984d9c625SLionel Sambuc 			vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0;
19084d9c625SLionel Sambuc 		} else
19184d9c625SLionel Sambuc 			vp->m_stop.cno = 0;
19284d9c625SLionel Sambuc 
19384d9c625SLionel Sambuc 		vp->m_stop.cno = tp->ai ? tp->ai - 1 : 0;
19484d9c625SLionel Sambuc 		if (db_append(sp, 1, vp->m_start.lno, tp->lb, tp->len))
19584d9c625SLionel Sambuc err_ret:		rval = 1;
19684d9c625SLionel Sambuc 		else {
19784d9c625SLionel Sambuc 			text_free(tp);
19884d9c625SLionel Sambuc 			rval = 0;
19984d9c625SLionel Sambuc 		}
20084d9c625SLionel Sambuc 	} else {
20184d9c625SLionel Sambuc 		STRSET(bp + vp->m_start.cno, vip->rlast, cnt);
20284d9c625SLionel Sambuc 		rval = db_set(sp, vp->m_start.lno, bp, len);
20384d9c625SLionel Sambuc 	}
20484d9c625SLionel Sambuc 	FREE_SPACEW(sp, bp, blen);
20584d9c625SLionel Sambuc 
20684d9c625SLionel Sambuc 	vp->m_final = vp->m_stop;
20784d9c625SLionel Sambuc 	return (rval);
20884d9c625SLionel Sambuc }
209