xref: /netbsd/external/bsd/nvi/dist/vi/v_right.c (revision cc73507a)
1*cc73507aSchristos /*	$NetBSD: v_right.c,v 1.3 2014/01/26 21:43:45 christos Exp $	*/
23a571abcSchristos /*-
33a571abcSchristos  * Copyright (c) 1992, 1993, 1994
43a571abcSchristos  *	The Regents of the University of California.  All rights reserved.
53a571abcSchristos  * Copyright (c) 1992, 1993, 1994, 1995, 1996
63a571abcSchristos  *	Keith Bostic.  All rights reserved.
73a571abcSchristos  *
83a571abcSchristos  * See the LICENSE file for redistribution information.
93a571abcSchristos  */
103a571abcSchristos 
113a571abcSchristos #include "config.h"
123a571abcSchristos 
13*cc73507aSchristos #include <sys/cdefs.h>
14*cc73507aSchristos #if 0
153a571abcSchristos #ifndef lint
163a571abcSchristos static const char sccsid[] = "Id: v_right.c,v 10.8 2001/06/25 15:19:34 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:34 ";
173a571abcSchristos #endif /* not lint */
18*cc73507aSchristos #else
19*cc73507aSchristos __RCSID("$NetBSD: v_right.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*cc73507aSchristos #endif
213a571abcSchristos 
223a571abcSchristos #include <sys/types.h>
233a571abcSchristos #include <sys/queue.h>
243a571abcSchristos #include <sys/time.h>
253a571abcSchristos 
263a571abcSchristos #include <bitstring.h>
273a571abcSchristos #include <limits.h>
283a571abcSchristos #include <stdio.h>
293a571abcSchristos 
303a571abcSchristos #include "../common/common.h"
313a571abcSchristos #include "vi.h"
323a571abcSchristos 
333a571abcSchristos /*
343a571abcSchristos  * v_right -- [count]' ', [count]l
353a571abcSchristos  *	Move right by columns.
363a571abcSchristos  *
373a571abcSchristos  * PUBLIC: int v_right __P((SCR *, VICMD *));
383a571abcSchristos  */
393a571abcSchristos int
v_right(SCR * sp,VICMD * vp)403a571abcSchristos v_right(SCR *sp, VICMD *vp)
413a571abcSchristos {
423a571abcSchristos 	size_t len;
433a571abcSchristos 	int isempty;
443a571abcSchristos 
453a571abcSchristos 	if (db_eget(sp, vp->m_start.lno, NULL, &len, &isempty)) {
463a571abcSchristos 		if (isempty)
473a571abcSchristos 			goto eol;
483a571abcSchristos 		return (1);
493a571abcSchristos 	}
503a571abcSchristos 
513a571abcSchristos 	/* It's always illegal to move right on empty lines. */
523a571abcSchristos 	if (len == 0) {
533a571abcSchristos eol:		v_eol(sp, NULL);
543a571abcSchristos 		return (1);
553a571abcSchristos 	}
563a571abcSchristos 
573a571abcSchristos 	/*
583a571abcSchristos 	 * Non-motion commands move to the end of the range.  Delete and
593a571abcSchristos 	 * yank stay at the start.  Ignore others.  Adjust the end of the
603a571abcSchristos 	 * range for motion commands.
613a571abcSchristos 	 *
623a571abcSchristos 	 * !!!
633a571abcSchristos 	 * Historically, "[cdsy]l" worked at the end of a line.  Also,
643a571abcSchristos 	 * EOL is a count sink.
653a571abcSchristos 	 */
663a571abcSchristos 	vp->m_stop.cno = vp->m_start.cno +
673a571abcSchristos 	    (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
683a571abcSchristos 	if (vp->m_start.cno == len - 1 && !ISMOTION(vp)) {
693a571abcSchristos 		v_eol(sp, NULL);
703a571abcSchristos 		return (1);
713a571abcSchristos 	}
723a571abcSchristos 	if (vp->m_stop.cno >= len) {
733a571abcSchristos 		vp->m_stop.cno = len - 1;
743a571abcSchristos 		vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
753a571abcSchristos 	} else if (ISMOTION(vp)) {
763a571abcSchristos 		--vp->m_stop.cno;
773a571abcSchristos 		vp->m_final = vp->m_start;
783a571abcSchristos 	} else
793a571abcSchristos 		vp->m_final = vp->m_stop;
803a571abcSchristos 	return (0);
813a571abcSchristos }
823a571abcSchristos 
833a571abcSchristos /*
843a571abcSchristos  * v_dollar -- [count]$
853a571abcSchristos  *	Move to the last column.
863a571abcSchristos  *
873a571abcSchristos  * PUBLIC: int v_dollar __P((SCR *, VICMD *));
883a571abcSchristos  */
893a571abcSchristos int
v_dollar(SCR * sp,VICMD * vp)903a571abcSchristos v_dollar(SCR *sp, VICMD *vp)
913a571abcSchristos {
923a571abcSchristos 	size_t len;
933a571abcSchristos 	int isempty;
943a571abcSchristos 
953a571abcSchristos 	/*
963a571abcSchristos 	 * !!!
973a571abcSchristos 	 * A count moves down count - 1 rows, so, "3$" is the same as "2j$".
983a571abcSchristos 	 */
993a571abcSchristos 	if ((F_ISSET(vp, VC_C1SET) ? vp->count : 1) != 1) {
1003a571abcSchristos 		/*
1013a571abcSchristos 		 * !!!
1023a571abcSchristos 		 * Historically, if the $ is a motion, and deleting from
1033a571abcSchristos 		 * at or before the first non-blank of the line, it's a
1043a571abcSchristos 		 * line motion, and the line motion flag is set.
1053a571abcSchristos 		 */
1063a571abcSchristos 		vp->m_stop.cno = 0;
1073a571abcSchristos 		if (nonblank(sp, vp->m_start.lno, &vp->m_stop.cno))
1083a571abcSchristos 			return (1);
1093a571abcSchristos 		if (ISMOTION(vp) && vp->m_start.cno <= vp->m_stop.cno)
1103a571abcSchristos 			F_SET(vp, VM_LMODE);
1113a571abcSchristos 
1123a571abcSchristos 		--vp->count;
1133a571abcSchristos 		if (v_down(sp, vp))
1143a571abcSchristos 			return (1);
1153a571abcSchristos 	}
1163a571abcSchristos 
1173a571abcSchristos 	/*
1183a571abcSchristos 	 * !!!
1193a571abcSchristos 	 * Historically, it was illegal to use $ as a motion command on
1203a571abcSchristos 	 * an empty line.  Unfortunately, even though C was historically
1213a571abcSchristos 	 * aliased to c$, it (and not c$) was special cased to work on
1223a571abcSchristos 	 * empty lines.  Since we alias C to c$ too, we have a problem.
1233a571abcSchristos 	 * To fix it, we let c$ go through, on the assumption that it's
1243a571abcSchristos 	 * not a problem for it to work.
1253a571abcSchristos 	 */
1263a571abcSchristos 	if (db_eget(sp, vp->m_stop.lno, NULL, &len, &isempty)) {
1273a571abcSchristos 		if (!isempty)
1283a571abcSchristos 			return (1);
1293a571abcSchristos 		len = 0;
1303a571abcSchristos 	}
1313a571abcSchristos 
1323a571abcSchristos 	if (len == 0) {
1333a571abcSchristos 		if (ISMOTION(vp) && !ISCMD(vp->rkp, 'c')) {
1343a571abcSchristos 			v_eol(sp, NULL);
1353a571abcSchristos 			return (1);
1363a571abcSchristos 		}
1373a571abcSchristos 		return (0);
1383a571abcSchristos 	}
1393a571abcSchristos 
1403a571abcSchristos 	/*
1413a571abcSchristos 	 * Non-motion commands move to the end of the range.  Delete
1423a571abcSchristos 	 * and yank stay at the start.  Ignore others.
1433a571abcSchristos 	 */
1443a571abcSchristos 	vp->m_stop.cno = len ? len - 1 : 0;
1453a571abcSchristos 	vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
1463a571abcSchristos 	return (0);
1473a571abcSchristos }
148