xref: /minix3/external/bsd/nvi/dist/vi/v_section.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: v_section.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_section.c,v 10.10 2001/06/25 15:19:35 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:35 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: v_section.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 <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <string.h>
3084d9c625SLionel Sambuc 
3184d9c625SLionel Sambuc #include "../common/common.h"
3284d9c625SLionel Sambuc #include "vi.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * !!!
3684d9c625SLionel Sambuc  * In historic vi, the section commands ignored empty lines, unlike the
3784d9c625SLionel Sambuc  * paragraph commands, which was probably okay.  However, they also moved
3884d9c625SLionel Sambuc  * to the start of the last line when there where no more sections instead
3984d9c625SLionel Sambuc  * of the end of the last line like the paragraph commands.  I've changed
4084d9c625SLionel Sambuc  * the latter behavior to match the paragraph commands.
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * In historic vi, a section was defined as the first character(s) of the
4384d9c625SLionel Sambuc  * line matching, which could be followed by anything.  This implementation
4484d9c625SLionel Sambuc  * follows that historic practice.
4584d9c625SLionel Sambuc  *
4684d9c625SLionel Sambuc  * !!!
4784d9c625SLionel Sambuc  * The historic vi documentation (USD:15-10) claimed:
4884d9c625SLionel Sambuc  *	The section commands interpret a preceding count as a different
4984d9c625SLionel Sambuc  *	window size in which to redraw the screen at the new location,
5084d9c625SLionel Sambuc  *	and this window size is the base size for newly drawn windows
5184d9c625SLionel Sambuc  *	until another size is specified.  This is very useful if you are
5284d9c625SLionel Sambuc  *	on a slow terminal ...
5384d9c625SLionel Sambuc  *
5484d9c625SLionel Sambuc  * I can't get the 4BSD vi to do this, it just beeps at me.  For now, a
5584d9c625SLionel Sambuc  * count to the section commands simply repeats the command.
5684d9c625SLionel Sambuc  */
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc /*
5984d9c625SLionel Sambuc  * v_sectionf -- [count]]]
6084d9c625SLionel Sambuc  *	Move forward count sections/functions.
6184d9c625SLionel Sambuc  *
6284d9c625SLionel Sambuc  * !!!
6384d9c625SLionel Sambuc  * Using ]] as a motion command was a bit special, historically.  It could
6484d9c625SLionel Sambuc  * match } as well as the usual { and section values.  If it matched a { or
6584d9c625SLionel Sambuc  * a section, it did NOT include the matched line.  If it matched a }, it
6684d9c625SLionel Sambuc  * did include the line.  No clue why.
6784d9c625SLionel Sambuc  *
6884d9c625SLionel Sambuc  * PUBLIC: int v_sectionf __P((SCR *, VICMD *));
6984d9c625SLionel Sambuc  */
7084d9c625SLionel Sambuc int
v_sectionf(SCR * sp,VICMD * vp)7184d9c625SLionel Sambuc v_sectionf(SCR *sp, VICMD *vp)
7284d9c625SLionel Sambuc {
7384d9c625SLionel Sambuc 	db_recno_t cnt, lno;
7484d9c625SLionel Sambuc 	size_t len;
7584d9c625SLionel Sambuc 	CHAR_T *p;
7684d9c625SLionel Sambuc 	const char *list, *lp;
7784d9c625SLionel Sambuc 
7884d9c625SLionel Sambuc 	/* Get the macro list. */
7984d9c625SLionel Sambuc 	if ((list = O_STR(sp, O_SECTIONS)) == NULL)
8084d9c625SLionel Sambuc 		return (1);
8184d9c625SLionel Sambuc 
8284d9c625SLionel Sambuc 	/*
8384d9c625SLionel Sambuc 	 * !!!
8484d9c625SLionel Sambuc 	 * If the starting cursor position is at or before any non-blank
8584d9c625SLionel Sambuc 	 * characters in the line, i.e. the movement is cutting all of the
8684d9c625SLionel Sambuc 	 * line's text, the buffer is in line mode.  It's a lot easier to
8784d9c625SLionel Sambuc 	 * check here, because we know that the end is going to be the start
8884d9c625SLionel Sambuc 	 * or end of a line.
8984d9c625SLionel Sambuc 	 */
9084d9c625SLionel Sambuc 	if (ISMOTION(vp)) {
9184d9c625SLionel Sambuc 		if (vp->m_start.cno == 0)
9284d9c625SLionel Sambuc 			F_SET(vp, VM_LMODE);
9384d9c625SLionel Sambuc 		else {
9484d9c625SLionel Sambuc 			vp->m_stop = vp->m_start;
9584d9c625SLionel Sambuc 			vp->m_stop.cno = 0;
9684d9c625SLionel Sambuc 			if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
9784d9c625SLionel Sambuc 				return (1);
9884d9c625SLionel Sambuc 			if (vp->m_start.cno <= vp->m_stop.cno)
9984d9c625SLionel Sambuc 				F_SET(vp, VM_LMODE);
10084d9c625SLionel Sambuc 		}
10184d9c625SLionel Sambuc 	}
10284d9c625SLionel Sambuc 
10384d9c625SLionel Sambuc 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
10484d9c625SLionel Sambuc 	for (lno = vp->m_start.lno; !db_get(sp, ++lno, 0, &p, &len);) {
10584d9c625SLionel Sambuc 		if (len == 0)
10684d9c625SLionel Sambuc 			continue;
10784d9c625SLionel Sambuc 		if (p[0] == '{' || (ISMOTION(vp) && p[0] == '}')) {
10884d9c625SLionel Sambuc 			if (!--cnt) {
10984d9c625SLionel Sambuc 				if (p[0] == '{')
11084d9c625SLionel Sambuc 					goto adjust1;
11184d9c625SLionel Sambuc 				goto adjust2;
11284d9c625SLionel Sambuc 			}
11384d9c625SLionel Sambuc 			continue;
11484d9c625SLionel Sambuc 		}
11584d9c625SLionel Sambuc 		/*
11684d9c625SLionel Sambuc 		 * !!!
11784d9c625SLionel Sambuc 		 * Historic documentation (USD:15-11, 4.2) said that formfeed
11884d9c625SLionel Sambuc 		 * characters (^L) in the first column delimited sections.
11984d9c625SLionel Sambuc 		 * The historic code mentions formfeed characters, but never
12084d9c625SLionel Sambuc 		 * implements them.  Seems reasonable, do it.
12184d9c625SLionel Sambuc 		 */
12284d9c625SLionel Sambuc 		if (p[0] == '\014') {
12384d9c625SLionel Sambuc 			if (!--cnt)
12484d9c625SLionel Sambuc 				goto adjust1;
12584d9c625SLionel Sambuc 			continue;
12684d9c625SLionel Sambuc 		}
12784d9c625SLionel Sambuc 		if (p[0] != '.' || len < 2)
12884d9c625SLionel Sambuc 			continue;
12984d9c625SLionel Sambuc 		for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
13084d9c625SLionel Sambuc 			if (lp[0] == p[1] &&
13184d9c625SLionel Sambuc 			    ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
13284d9c625SLionel Sambuc 			    !--cnt) {
13384d9c625SLionel Sambuc 				/*
13484d9c625SLionel Sambuc 				 * !!!
13584d9c625SLionel Sambuc 				 * If not cutting this line, adjust to the end
13684d9c625SLionel Sambuc 				 * of the previous one.  Otherwise, position to
13784d9c625SLionel Sambuc 				 * column 0.
13884d9c625SLionel Sambuc 				 */
13984d9c625SLionel Sambuc adjust1:			if (ISMOTION(vp))
14084d9c625SLionel Sambuc 					goto ret1;
14184d9c625SLionel Sambuc 
14284d9c625SLionel Sambuc adjust2:			vp->m_stop.lno = lno;
14384d9c625SLionel Sambuc 				vp->m_stop.cno = 0;
14484d9c625SLionel Sambuc 				goto ret2;
14584d9c625SLionel Sambuc 			}
14684d9c625SLionel Sambuc 	}
14784d9c625SLionel Sambuc 
14884d9c625SLionel Sambuc 	/* If moving forward, reached EOF, check to see if we started there. */
14984d9c625SLionel Sambuc 	if (vp->m_start.lno == lno - 1) {
15084d9c625SLionel Sambuc 		v_eof(sp, NULL);
15184d9c625SLionel Sambuc 		return (1);
15284d9c625SLionel Sambuc 	}
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc ret1:	if (db_get(sp, --lno, DBG_FATAL, NULL, &len))
15584d9c625SLionel Sambuc 		return (1);
15684d9c625SLionel Sambuc 	vp->m_stop.lno = lno;
15784d9c625SLionel Sambuc 	vp->m_stop.cno = len ? len - 1 : 0;
15884d9c625SLionel Sambuc 
15984d9c625SLionel Sambuc 	/*
16084d9c625SLionel Sambuc 	 * Non-motion commands go to the end of the range.  Delete and
16184d9c625SLionel Sambuc 	 * yank stay at the start of the range.  Ignore others.
16284d9c625SLionel Sambuc 	 */
16384d9c625SLionel Sambuc ret2:	if (ISMOTION(vp)) {
16484d9c625SLionel Sambuc 		vp->m_final = vp->m_start;
16584d9c625SLionel Sambuc 		if (F_ISSET(vp, VM_LMODE))
16684d9c625SLionel Sambuc 			vp->m_final.cno = 0;
16784d9c625SLionel Sambuc 	} else
16884d9c625SLionel Sambuc 		vp->m_final = vp->m_stop;
16984d9c625SLionel Sambuc 	return (0);
17084d9c625SLionel Sambuc }
17184d9c625SLionel Sambuc 
17284d9c625SLionel Sambuc /*
17384d9c625SLionel Sambuc  * v_sectionb -- [count][[
17484d9c625SLionel Sambuc  *	Move backward count sections/functions.
17584d9c625SLionel Sambuc  *
17684d9c625SLionel Sambuc  * PUBLIC: int v_sectionb __P((SCR *, VICMD *));
17784d9c625SLionel Sambuc  */
17884d9c625SLionel Sambuc int
v_sectionb(SCR * sp,VICMD * vp)17984d9c625SLionel Sambuc v_sectionb(SCR *sp, VICMD *vp)
18084d9c625SLionel Sambuc {
18184d9c625SLionel Sambuc 	size_t len;
18284d9c625SLionel Sambuc 	db_recno_t cnt, lno;
18384d9c625SLionel Sambuc 	CHAR_T *p;
18484d9c625SLionel Sambuc 	const char *list, *lp;
18584d9c625SLionel Sambuc 
18684d9c625SLionel Sambuc 	/* An empty file or starting from line 1 is always illegal. */
18784d9c625SLionel Sambuc 	if (vp->m_start.lno <= 1) {
18884d9c625SLionel Sambuc 		v_sof(sp, NULL);
18984d9c625SLionel Sambuc 		return (1);
19084d9c625SLionel Sambuc 	}
19184d9c625SLionel Sambuc 
19284d9c625SLionel Sambuc 	/* Get the macro list. */
19384d9c625SLionel Sambuc 	if ((list = O_STR(sp, O_SECTIONS)) == NULL)
19484d9c625SLionel Sambuc 		return (1);
19584d9c625SLionel Sambuc 
19684d9c625SLionel Sambuc 	cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
19784d9c625SLionel Sambuc 	for (lno = vp->m_start.lno; !db_get(sp, --lno, 0, &p, &len);) {
19884d9c625SLionel Sambuc 		if (len == 0)
19984d9c625SLionel Sambuc 			continue;
20084d9c625SLionel Sambuc 		if (p[0] == '{') {
20184d9c625SLionel Sambuc 			if (!--cnt)
20284d9c625SLionel Sambuc 				goto adjust1;
20384d9c625SLionel Sambuc 			continue;
20484d9c625SLionel Sambuc 		}
20584d9c625SLionel Sambuc 		/*
20684d9c625SLionel Sambuc 		 * !!!
20784d9c625SLionel Sambuc 		 * Historic documentation (USD:15-11, 4.2) said that formfeed
20884d9c625SLionel Sambuc 		 * characters (^L) in the first column delimited sections.
20984d9c625SLionel Sambuc 		 * The historic code mentions formfeed characters, but never
21084d9c625SLionel Sambuc 		 * implements them.  Seems reasonable, do it.
21184d9c625SLionel Sambuc 		 */
21284d9c625SLionel Sambuc 		if (p[0] == '\014') {
21384d9c625SLionel Sambuc 			if (!--cnt)
21484d9c625SLionel Sambuc 				goto adjust1;
21584d9c625SLionel Sambuc 			continue;
21684d9c625SLionel Sambuc 		}
21784d9c625SLionel Sambuc 		if (p[0] != '.' || len < 2)
21884d9c625SLionel Sambuc 			continue;
21984d9c625SLionel Sambuc 		for (lp = list; *lp != '\0'; lp += 2 * sizeof(*lp))
22084d9c625SLionel Sambuc 			if (lp[0] == p[1] &&
22184d9c625SLionel Sambuc 			    ((lp[1] == ' ' && len == 2) || lp[1] == p[2]) &&
22284d9c625SLionel Sambuc 			    !--cnt) {
22384d9c625SLionel Sambuc adjust1:			vp->m_stop.lno = lno;
22484d9c625SLionel Sambuc 				vp->m_stop.cno = 0;
22584d9c625SLionel Sambuc 				goto ret1;
22684d9c625SLionel Sambuc 			}
22784d9c625SLionel Sambuc 	}
22884d9c625SLionel Sambuc 
22984d9c625SLionel Sambuc 	/*
23084d9c625SLionel Sambuc 	 * If moving backward, reached SOF, which is a movement sink.
23184d9c625SLionel Sambuc 	 * We already checked for starting there.
23284d9c625SLionel Sambuc 	 */
23384d9c625SLionel Sambuc 	vp->m_stop.lno = 1;
23484d9c625SLionel Sambuc 	vp->m_stop.cno = 0;
23584d9c625SLionel Sambuc 
23684d9c625SLionel Sambuc 	/*
23784d9c625SLionel Sambuc 	 * All commands move to the end of the range.
23884d9c625SLionel Sambuc 	 *
23984d9c625SLionel Sambuc 	 * !!!
24084d9c625SLionel Sambuc 	 * Historic practice is the section cut was in line mode if it started
24184d9c625SLionel Sambuc 	 * from column 0 and was in the backward direction.  Otherwise, left
24284d9c625SLionel Sambuc 	 * motion commands adjust the starting point to the character before
24384d9c625SLionel Sambuc 	 * the current one.  What makes this worse is that if it cut to line
24484d9c625SLionel Sambuc 	 * mode it also went to the first non-<blank>.
24584d9c625SLionel Sambuc 	 */
24684d9c625SLionel Sambuc ret1:	if (vp->m_start.cno == 0) {
24784d9c625SLionel Sambuc 		F_CLR(vp, VM_RCM_MASK);
24884d9c625SLionel Sambuc 		F_SET(vp, VM_RCM_SETFNB);
24984d9c625SLionel Sambuc 
25084d9c625SLionel Sambuc 		--vp->m_start.lno;
25184d9c625SLionel Sambuc 		F_SET(vp, VM_LMODE);
25284d9c625SLionel Sambuc 	} else
25384d9c625SLionel Sambuc 		--vp->m_start.cno;
25484d9c625SLionel Sambuc 
25584d9c625SLionel Sambuc 	vp->m_final = vp->m_stop;
25684d9c625SLionel Sambuc 	return (0);
25784d9c625SLionel Sambuc }
258