xref: /illumos-gate/usr/src/cmd/vi/port/ex_vops3.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*23a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*23a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "ex.h"
337c478bd9Sstevel@tonic-gate #include "ex_tty.h"
347c478bd9Sstevel@tonic-gate #include "ex_vis.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Routines to handle structure.
387c478bd9Sstevel@tonic-gate  * Operations supported are:
397c478bd9Sstevel@tonic-gate  *	( ) { } [ ]
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * These cover:		LISP		TEXT
427c478bd9Sstevel@tonic-gate  *	( )		s-exprs		sentences
437c478bd9Sstevel@tonic-gate  *	{ }		list at same	paragraphs
447c478bd9Sstevel@tonic-gate  *	[ ]		defuns		sections
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * { and } for C used to attempt to do something with matching {}'s, but
477c478bd9Sstevel@tonic-gate  * I couldn't find definitions which worked intuitively very well, so I
487c478bd9Sstevel@tonic-gate  * scrapped this.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * The code here is very hard to understand.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate line	*llimit;
537c478bd9Sstevel@tonic-gate int	(*lf)();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int	lindent();
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate bool	wasend;
587c478bd9Sstevel@tonic-gate 
59f6db9f27Scf46844 int endsent(bool);
60f6db9f27Scf46844 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Find over structure, repeated count times.
637c478bd9Sstevel@tonic-gate  * Don't go past line limit.  F is the operation to
647c478bd9Sstevel@tonic-gate  * be performed eventually.  If pastatom then the user said {}
657c478bd9Sstevel@tonic-gate  * rather than (), implying past atoms in a list (or a paragraph
667c478bd9Sstevel@tonic-gate  * rather than a sentence.
677c478bd9Sstevel@tonic-gate  */
68f6db9f27Scf46844 int
lfind(pastatom,cnt,f,limit)697c478bd9Sstevel@tonic-gate lfind(pastatom, cnt, f, limit)
707c478bd9Sstevel@tonic-gate 	bool pastatom;
717c478bd9Sstevel@tonic-gate 	int cnt, (*f)();
727c478bd9Sstevel@tonic-gate 	line *limit;
737c478bd9Sstevel@tonic-gate {
74f6db9f27Scf46844 	int c;
75f6db9f27Scf46844 	int rc = 0;
767c478bd9Sstevel@tonic-gate 	unsigned char save[LBSIZE];
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/*
797c478bd9Sstevel@tonic-gate 	 * Initialize, saving the current line buffer state
807c478bd9Sstevel@tonic-gate 	 * and computing the limit; a 0 argument means
817c478bd9Sstevel@tonic-gate 	 * directional end of file.
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	wasend = 0;
847c478bd9Sstevel@tonic-gate 	lf = f;
857c478bd9Sstevel@tonic-gate 	strcpy(save, linebuf);
867c478bd9Sstevel@tonic-gate 	if (limit == 0)
877c478bd9Sstevel@tonic-gate 		limit = dir < 0 ? one : dol;
887c478bd9Sstevel@tonic-gate 	llimit = limit;
897c478bd9Sstevel@tonic-gate 	wdot = dot;
907c478bd9Sstevel@tonic-gate 	wcursor = cursor;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (pastatom >= 2) {
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate  		if (pastatom == 3) {
957c478bd9Sstevel@tonic-gate 			while(eend(f) && cnt-- > 0) {
967c478bd9Sstevel@tonic-gate 				;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		} else {
997c478bd9Sstevel@tonic-gate 			while (cnt > 0 && word(f, cnt))
1007c478bd9Sstevel@tonic-gate 				cnt--;
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		if (dot == wdot) {
1047c478bd9Sstevel@tonic-gate 			wdot = 0;
1057c478bd9Sstevel@tonic-gate 			if (cursor == wcursor)
1067c478bd9Sstevel@tonic-gate 				rc = -1;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	else if (!value(vi_LISP)) {
1107c478bd9Sstevel@tonic-gate 		unsigned char *icurs;
1117c478bd9Sstevel@tonic-gate 		line *idot;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		if (linebuf[0] == 0) {
1147c478bd9Sstevel@tonic-gate 			do
1157c478bd9Sstevel@tonic-gate 				if (!lnext())
1167c478bd9Sstevel@tonic-gate 					goto ret;
1177c478bd9Sstevel@tonic-gate 			while (linebuf[0] == 0);
1187c478bd9Sstevel@tonic-gate 			if (dir > 0) {
1197c478bd9Sstevel@tonic-gate 				wdot--;
1207c478bd9Sstevel@tonic-gate 				linebuf[0] = 0;
1217c478bd9Sstevel@tonic-gate 				wcursor = linebuf;
1227c478bd9Sstevel@tonic-gate 				/*
1237c478bd9Sstevel@tonic-gate 				 * If looking for sentence, next line
1247c478bd9Sstevel@tonic-gate 				 * starts one.
1257c478bd9Sstevel@tonic-gate 				 */
1267c478bd9Sstevel@tonic-gate 				if (!pastatom) {
1277c478bd9Sstevel@tonic-gate 					icurs = wcursor;
1287c478bd9Sstevel@tonic-gate 					idot = wdot;
1297c478bd9Sstevel@tonic-gate 					goto begin;
1307c478bd9Sstevel@tonic-gate 				}
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 		icurs = wcursor;
1347c478bd9Sstevel@tonic-gate 		idot = wdot;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * Advance so as to not find same thing again.
1387c478bd9Sstevel@tonic-gate 		 */
1397c478bd9Sstevel@tonic-gate 		if (dir > 0) {
1407c478bd9Sstevel@tonic-gate 			if (!lnext()) {
1417c478bd9Sstevel@tonic-gate 				rc = -1;
1427c478bd9Sstevel@tonic-gate 				goto ret;
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate #ifdef XPG4
1457c478bd9Sstevel@tonic-gate 		} else {
1467c478bd9Sstevel@tonic-gate 			if (!lnext()) {
1477c478bd9Sstevel@tonic-gate 				rc = -1;
1487c478bd9Sstevel@tonic-gate 				goto ret;
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 			(void) ltosol1("");
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate #else /* ! XPG4 */
1537c478bd9Sstevel@tonic-gate 		} else
1547c478bd9Sstevel@tonic-gate 			(void)lskipa1("");
1557c478bd9Sstevel@tonic-gate #endif /* XPG4 */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * Count times find end of sentence/paragraph.
1597c478bd9Sstevel@tonic-gate 		 */
1607c478bd9Sstevel@tonic-gate begin:
1617c478bd9Sstevel@tonic-gate 		for (;;) {
1627c478bd9Sstevel@tonic-gate 			while (!endsent(pastatom))
1637c478bd9Sstevel@tonic-gate 				if (!lnext())
1647c478bd9Sstevel@tonic-gate 					goto ret;
1657c478bd9Sstevel@tonic-gate 			if (!pastatom || wcursor == linebuf && endPS())
1667c478bd9Sstevel@tonic-gate 				if (--cnt <= 0)
1677c478bd9Sstevel@tonic-gate 					break;
1687c478bd9Sstevel@tonic-gate 			if (linebuf[0] == 0) {
1697c478bd9Sstevel@tonic-gate 				do
1707c478bd9Sstevel@tonic-gate 					if (!lnext())
1717c478bd9Sstevel@tonic-gate 						goto ret;
1727c478bd9Sstevel@tonic-gate 				while (linebuf[0] == 0);
1737c478bd9Sstevel@tonic-gate 			} else
1747c478bd9Sstevel@tonic-gate 				if (!lnext())
1757c478bd9Sstevel@tonic-gate 					goto ret;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/*
1797c478bd9Sstevel@tonic-gate 		 * If going backwards, and didn't hit the end of the buffer,
1807c478bd9Sstevel@tonic-gate 		 * then reverse direction.
1817c478bd9Sstevel@tonic-gate 		 */
1827c478bd9Sstevel@tonic-gate 		if (dir < 0 && (wdot != llimit || wcursor != linebuf)) {
1837c478bd9Sstevel@tonic-gate 			dir = 1;
1847c478bd9Sstevel@tonic-gate 			llimit = dot;
1857c478bd9Sstevel@tonic-gate 			/*
1867c478bd9Sstevel@tonic-gate 			 * Empty line needs special treatement.
1877c478bd9Sstevel@tonic-gate 			 * If moved to it from other than beginning of next line,
1887c478bd9Sstevel@tonic-gate 			 * then a sentence starts on next line.
1897c478bd9Sstevel@tonic-gate 			 */
1907c478bd9Sstevel@tonic-gate 			if (linebuf[0] == 0 && !pastatom &&
1917c478bd9Sstevel@tonic-gate 			   (wdot != dot - 1 || cursor != linebuf)) {
192f6db9f27Scf46844 				(void) lnext();
1937c478bd9Sstevel@tonic-gate 				goto ret;
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * If we are not at a section/paragraph division,
1997c478bd9Sstevel@tonic-gate 		 * advance to next.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		if (wcursor == icurs && wdot == idot || wcursor != linebuf || !endPS())
2027c478bd9Sstevel@tonic-gate 			(void)lskipa1("");
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	else {
2057c478bd9Sstevel@tonic-gate 		c = *wcursor;
2067c478bd9Sstevel@tonic-gate 		/*
2077c478bd9Sstevel@tonic-gate 		 * Startup by skipping if at a ( going left or a ) going
2087c478bd9Sstevel@tonic-gate 		 * right to keep from getting stuck immediately.
2097c478bd9Sstevel@tonic-gate 		 */
2107c478bd9Sstevel@tonic-gate 		if (dir < 0 && c == '(' || dir > 0 && c == ')') {
2117c478bd9Sstevel@tonic-gate 			if (!lnext()) {
2127c478bd9Sstevel@tonic-gate 				rc = -1;
2137c478bd9Sstevel@tonic-gate 				goto ret;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		/*
2177c478bd9Sstevel@tonic-gate 		 * Now chew up repetition count.  Each time around
2187c478bd9Sstevel@tonic-gate 		 * if at the beginning of an s-exp (going forwards)
2197c478bd9Sstevel@tonic-gate 		 * or the end of an s-exp (going backwards)
2207c478bd9Sstevel@tonic-gate 		 * skip the s-exp.  If not at beg/end resp, then stop
2217c478bd9Sstevel@tonic-gate 		 * if we hit a higher level paren, else skip an atom,
2227c478bd9Sstevel@tonic-gate 		 * counting it unless pastatom.
2237c478bd9Sstevel@tonic-gate 		 */
2247c478bd9Sstevel@tonic-gate 		while (cnt > 0) {
2257c478bd9Sstevel@tonic-gate 			c = *wcursor;
2267c478bd9Sstevel@tonic-gate 			if (dir < 0 && c == ')' || dir > 0 && c == '(') {
2277c478bd9Sstevel@tonic-gate 				if (!lskipbal("()"))
2287c478bd9Sstevel@tonic-gate 					goto ret;
2297c478bd9Sstevel@tonic-gate 				/*
2307c478bd9Sstevel@tonic-gate  				 * Unless this is the last time going
2317c478bd9Sstevel@tonic-gate 				 * backwards, skip past the matching paren
2327c478bd9Sstevel@tonic-gate 				 * so we don't think it is a higher level paren.
2337c478bd9Sstevel@tonic-gate 				 */
2347c478bd9Sstevel@tonic-gate 				if (dir < 0 && cnt == 1)
2357c478bd9Sstevel@tonic-gate 					goto ret;
2367c478bd9Sstevel@tonic-gate 				if (!lnext() || !ltosolid())
2377c478bd9Sstevel@tonic-gate 					goto ret;
2387c478bd9Sstevel@tonic-gate 				--cnt;
2397c478bd9Sstevel@tonic-gate 			} else if (dir < 0 && c == '(' || dir > 0 && c == ')')
2407c478bd9Sstevel@tonic-gate 				/* Found a higher level paren */
2417c478bd9Sstevel@tonic-gate 				goto ret;
2427c478bd9Sstevel@tonic-gate 			else {
2437c478bd9Sstevel@tonic-gate 				if (!lskipatom())
2447c478bd9Sstevel@tonic-gate 					goto ret;
2457c478bd9Sstevel@tonic-gate 				if (!pastatom)
2467c478bd9Sstevel@tonic-gate 					--cnt;
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate ret:
2517c478bd9Sstevel@tonic-gate 	strcLIN(save);
2527c478bd9Sstevel@tonic-gate 	return (rc);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Is this the end of a sentence?
2577c478bd9Sstevel@tonic-gate  */
258f6db9f27Scf46844 int
endsent(bool pastatom)259f6db9f27Scf46844 endsent(bool pastatom)
2607c478bd9Sstevel@tonic-gate {
261f6db9f27Scf46844 	unsigned char *cp = wcursor;
262f6db9f27Scf46844 	int c, d;
2637c478bd9Sstevel@tonic-gate 	int	len;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * If this is the beginning of a line, then
2677c478bd9Sstevel@tonic-gate 	 * check for the end of a paragraph or section.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	if (cp == linebuf)
2707c478bd9Sstevel@tonic-gate 		return (endPS());
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Sentences end with . ! ? not at the beginning
2747c478bd9Sstevel@tonic-gate 	 * of the line, and must be either at the end of the line,
2757c478bd9Sstevel@tonic-gate 	 * or followed by 2 spaces.  Any number of intervening ) ] ' "
2767c478bd9Sstevel@tonic-gate 	 * characters are allowed.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	if (!any(c = *cp, ".!?"))
2797c478bd9Sstevel@tonic-gate 		goto tryps;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	do {
2827c478bd9Sstevel@tonic-gate 		if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0)
2837c478bd9Sstevel@tonic-gate 			len = 1;
2847c478bd9Sstevel@tonic-gate 		cp += len;
2857c478bd9Sstevel@tonic-gate 		if ((d = *cp) == 0)
2867c478bd9Sstevel@tonic-gate 			return (1);
2877c478bd9Sstevel@tonic-gate #ifdef XPG4
2887c478bd9Sstevel@tonic-gate 	} while (any(d, ")]'\""));
2897c478bd9Sstevel@tonic-gate #else /* ! XPG4 */
2907c478bd9Sstevel@tonic-gate 	} while (any(d, ")]'"));
2917c478bd9Sstevel@tonic-gate #endif /* XPG4 */
2927c478bd9Sstevel@tonic-gate 	if (*cp == 0 || *cp++ == ' ' && *cp == ' ')
2937c478bd9Sstevel@tonic-gate 		return (1);
2947c478bd9Sstevel@tonic-gate tryps:
2957c478bd9Sstevel@tonic-gate 	if (cp[1] == 0)
2967c478bd9Sstevel@tonic-gate 		return (endPS());
2977c478bd9Sstevel@tonic-gate 	return (0);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * End of paragraphs/sections are respective
3027c478bd9Sstevel@tonic-gate  * macros as well as blank lines and form feeds.
3037c478bd9Sstevel@tonic-gate  */
304f6db9f27Scf46844 int
endPS(void)305f6db9f27Scf46844 endPS(void)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (linebuf[0] == 0 ||
3097c478bd9Sstevel@tonic-gate #ifdef XPG4
3107c478bd9Sstevel@tonic-gate 		/* POSIX 1003.2 Section 5.35.7.1: control-L, "{"	*/
3117c478bd9Sstevel@tonic-gate 		linebuf[0] == '{' ||
3127c478bd9Sstevel@tonic-gate 		linebuf[0] == CTRL('L') ||
3137c478bd9Sstevel@tonic-gate #endif /* XPG4 */
3147c478bd9Sstevel@tonic-gate 		isa(svalue(vi_PARAGRAPHS)) || isa(svalue(vi_SECTIONS)));
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
318f6db9f27Scf46844 int
lindent(line * addr)319f6db9f27Scf46844 lindent(line *addr)
3207c478bd9Sstevel@tonic-gate {
321f6db9f27Scf46844 	int i;
3227c478bd9Sstevel@tonic-gate 	unsigned char *swcurs = wcursor;
3237c478bd9Sstevel@tonic-gate 	line *swdot = wdot;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate again:
3267c478bd9Sstevel@tonic-gate 	if (addr > one) {
327f6db9f27Scf46844 		unsigned char *cp;
328f6db9f27Scf46844 		int cnt = 0;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		addr--;
331*23a1cceaSRoger A. Faulkner 		getaline(*addr);
3327c478bd9Sstevel@tonic-gate 		for (cp = linebuf; *cp; cp++)
3337c478bd9Sstevel@tonic-gate 			if (*cp == '(')
3347c478bd9Sstevel@tonic-gate 				cnt++;
3357c478bd9Sstevel@tonic-gate 			else if (*cp == ')')
3367c478bd9Sstevel@tonic-gate 				cnt--;
3377c478bd9Sstevel@tonic-gate 		cp = vpastwh(linebuf);
3387c478bd9Sstevel@tonic-gate 		if (*cp == 0)
3397c478bd9Sstevel@tonic-gate 			goto again;
3407c478bd9Sstevel@tonic-gate 		if (cnt == 0)
3417c478bd9Sstevel@tonic-gate 			return (whitecnt(linebuf));
3427c478bd9Sstevel@tonic-gate 		addr++;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	wcursor = linebuf;
3457c478bd9Sstevel@tonic-gate 	linebuf[0] = 0;
3467c478bd9Sstevel@tonic-gate 	wdot = addr;
3477c478bd9Sstevel@tonic-gate 	dir = -1;
3487c478bd9Sstevel@tonic-gate 	llimit = one;
3497c478bd9Sstevel@tonic-gate 	lf = lindent;
3507c478bd9Sstevel@tonic-gate 	if (!lskipbal("()"))
3517c478bd9Sstevel@tonic-gate 		i = 0;
3527c478bd9Sstevel@tonic-gate 	else if (wcursor == linebuf)
3537c478bd9Sstevel@tonic-gate 		i = 2;
3547c478bd9Sstevel@tonic-gate 	else {
355f6db9f27Scf46844 		unsigned char *wp = wcursor;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		dir = 1;
3587c478bd9Sstevel@tonic-gate 		llimit = wdot;
3597c478bd9Sstevel@tonic-gate 		if (!lnext() || !ltosolid() || !lskipatom()) {
3607c478bd9Sstevel@tonic-gate 			wcursor = wp;
3617c478bd9Sstevel@tonic-gate 			i = 1;
3627c478bd9Sstevel@tonic-gate 		} else
3637c478bd9Sstevel@tonic-gate 			i = 0;
3647c478bd9Sstevel@tonic-gate 		i += column(wcursor) - 1;
3657c478bd9Sstevel@tonic-gate 		if (!inopen)
3667c478bd9Sstevel@tonic-gate 			i--;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	wdot = swdot;
3697c478bd9Sstevel@tonic-gate 	wcursor = swcurs;
3707c478bd9Sstevel@tonic-gate 	return (i);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
373f6db9f27Scf46844 int
lmatchp(line * addr)374f6db9f27Scf46844 lmatchp(line *addr)
3757c478bd9Sstevel@tonic-gate {
376f6db9f27Scf46844 	int i;
377f6db9f27Scf46844 	unsigned char *parens, *cp;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	for (cp = cursor; !any(*cp, "({[)}]");) {
3807c478bd9Sstevel@tonic-gate 		if (*cp == 0)
3817c478bd9Sstevel@tonic-gate 			return (0);
3827c478bd9Sstevel@tonic-gate 		if ((i = mblen((char *)cp, MB_CUR_MAX)) <= 0)
3837c478bd9Sstevel@tonic-gate 			i = 1;
3847c478bd9Sstevel@tonic-gate 		cp += i;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	lf = 0;
3887c478bd9Sstevel@tonic-gate 	parens = any(*cp, "()") ? (unsigned char *)"()" : any(*cp, "[]") ? (unsigned char *)"[]" : (unsigned char *)"{}";
3897c478bd9Sstevel@tonic-gate 	if (*cp == parens[1]) {
3907c478bd9Sstevel@tonic-gate 		dir = -1;
3917c478bd9Sstevel@tonic-gate 		llimit = one;
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 		dir = 1;
3947c478bd9Sstevel@tonic-gate 		llimit = dol;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	if (addr)
3977c478bd9Sstevel@tonic-gate 		llimit = addr;
3987c478bd9Sstevel@tonic-gate 	if (splitw)
3997c478bd9Sstevel@tonic-gate 		llimit = dot;
4007c478bd9Sstevel@tonic-gate 	wcursor = cp;
4017c478bd9Sstevel@tonic-gate 	wdot = dot;
4027c478bd9Sstevel@tonic-gate 	i = lskipbal(parens);
4037c478bd9Sstevel@tonic-gate 	return (i);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
406f6db9f27Scf46844 void
lsmatch(unsigned char * cp)407f6db9f27Scf46844 lsmatch(unsigned char *cp)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	unsigned char save[LBSIZE];
410f6db9f27Scf46844 	unsigned char *sp = save;
411f6db9f27Scf46844 	unsigned char *scurs = cursor;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	wcursor = cp;
4147c478bd9Sstevel@tonic-gate 	strcpy(sp, linebuf);
4157c478bd9Sstevel@tonic-gate 	*wcursor = 0;
4167c478bd9Sstevel@tonic-gate 	strcpy(cursor, genbuf);
4177c478bd9Sstevel@tonic-gate 	cursor = strend(linebuf);
4187c478bd9Sstevel@tonic-gate 	cursor = lastchr(linebuf, cursor);
4197c478bd9Sstevel@tonic-gate 	if (lmatchp(dot - vcline)) {
420f6db9f27Scf46844 		int i = insmode;
421f6db9f27Scf46844 		int c = outcol;
422f6db9f27Scf46844 		int l = outline;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		if (!move_insert_mode)
4257c478bd9Sstevel@tonic-gate 			endim();
4267c478bd9Sstevel@tonic-gate 		vgoto(splitw ? WECHO : LINE(wdot - llimit), column(wcursor) - 1);
4277c478bd9Sstevel@tonic-gate 		flush();
4287c478bd9Sstevel@tonic-gate 		sleep(1);
4297c478bd9Sstevel@tonic-gate 		vgoto(l, c);
4307c478bd9Sstevel@tonic-gate 		if (i)
4317c478bd9Sstevel@tonic-gate 			goim();
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	else {
4347c478bd9Sstevel@tonic-gate 		strcLIN(sp);
4357c478bd9Sstevel@tonic-gate 		strcpy(scurs, genbuf);
4367c478bd9Sstevel@tonic-gate 		if (!lmatchp((line *) 0))
437f6db9f27Scf46844 			(void) beep();
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	strcLIN(sp);
4407c478bd9Sstevel@tonic-gate 	wdot = 0;
4417c478bd9Sstevel@tonic-gate 	wcursor = 0;
4427c478bd9Sstevel@tonic-gate 	cursor = scurs;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
445f6db9f27Scf46844 int
ltosolid(void)446f6db9f27Scf46844 ltosolid(void)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	return (ltosol1("()"));
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
452f6db9f27Scf46844 int
ltosol1(unsigned char * parens)453f6db9f27Scf46844 ltosol1(unsigned char *parens)
4547c478bd9Sstevel@tonic-gate {
455f6db9f27Scf46844 	unsigned char *cp;
4567c478bd9Sstevel@tonic-gate 	int	len;
4577c478bd9Sstevel@tonic-gate 	unsigned char	*ocp;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (*parens && !*wcursor && !lnext())
4607c478bd9Sstevel@tonic-gate 		return (0);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	while (isspace(*wcursor) || (*wcursor == 0 && *parens))
4637c478bd9Sstevel@tonic-gate 		if (!lnext())
4647c478bd9Sstevel@tonic-gate 			return (0);
4657c478bd9Sstevel@tonic-gate 	if (any(*wcursor, parens) || dir > 0)
4667c478bd9Sstevel@tonic-gate 		return (1);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	ocp = linebuf;
4697c478bd9Sstevel@tonic-gate 	for (cp = linebuf; cp < wcursor; cp += len) {
4707c478bd9Sstevel@tonic-gate 		if (isascii(*cp)) {
4717c478bd9Sstevel@tonic-gate 			len = 1;
4727c478bd9Sstevel@tonic-gate 			if (isspace(*cp) || any(*cp, parens))
4737c478bd9Sstevel@tonic-gate 				ocp = cp + 1;
4747c478bd9Sstevel@tonic-gate 			continue;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		if ((len = mblen((char *)cp, MB_CUR_MAX)) <= 0)
4777c478bd9Sstevel@tonic-gate 			len = 1;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	wcursor = ocp;
4807c478bd9Sstevel@tonic-gate 	return (1);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
483f6db9f27Scf46844 int
lskipbal(unsigned char * parens)484f6db9f27Scf46844 lskipbal(unsigned char *parens)
4857c478bd9Sstevel@tonic-gate {
486f6db9f27Scf46844 	int level = dir;
487f6db9f27Scf46844 	int c;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	do {
4907c478bd9Sstevel@tonic-gate 		if (!lnext()) {
4917c478bd9Sstevel@tonic-gate 			wdot = NOLINE;
4927c478bd9Sstevel@tonic-gate 			return (0);
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 		c = *wcursor;
4957c478bd9Sstevel@tonic-gate 		if (c == parens[1])
4967c478bd9Sstevel@tonic-gate 			level--;
4977c478bd9Sstevel@tonic-gate 		else if (c == parens[0])
4987c478bd9Sstevel@tonic-gate 			level++;
4997c478bd9Sstevel@tonic-gate 	} while (level);
5007c478bd9Sstevel@tonic-gate 	return (1);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
503f6db9f27Scf46844 int
lskipatom(void)504f6db9f27Scf46844 lskipatom(void)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	return (lskipa1("()"));
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
510f6db9f27Scf46844 int
lskipa1(unsigned char * parens)511f6db9f27Scf46844 lskipa1(unsigned char *parens)
5127c478bd9Sstevel@tonic-gate {
513f6db9f27Scf46844 	int c;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	for (;;) {
5167c478bd9Sstevel@tonic-gate 		if (dir < 0 && wcursor == linebuf) {
5177c478bd9Sstevel@tonic-gate 			if (!lnext())
5187c478bd9Sstevel@tonic-gate 				return (0);
5197c478bd9Sstevel@tonic-gate 			break;
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		c = *wcursor;
5227c478bd9Sstevel@tonic-gate 		if (c && (isspace(c) || any(c, parens)))
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if (!lnext())
5267c478bd9Sstevel@tonic-gate 			return (0);
5277c478bd9Sstevel@tonic-gate 		if (dir > 0 && wcursor == linebuf)
5287c478bd9Sstevel@tonic-gate 			break;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	return (ltosol1(parens));
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
533f6db9f27Scf46844 int
lnext(void)534f6db9f27Scf46844 lnext(void)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (dir > 0) {
5387c478bd9Sstevel@tonic-gate 		if (*wcursor)
5397c478bd9Sstevel@tonic-gate 			wcursor = nextchr(wcursor);
5407c478bd9Sstevel@tonic-gate 		if (*wcursor)
5417c478bd9Sstevel@tonic-gate 			return (1);
5427c478bd9Sstevel@tonic-gate 		if (wdot >= llimit) {
5437c478bd9Sstevel@tonic-gate 			if (lf == vmove && wcursor > linebuf)
5447c478bd9Sstevel@tonic-gate 				wcursor = lastchr(linebuf, wcursor);
5457c478bd9Sstevel@tonic-gate 			return (0);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		wdot++;
548*23a1cceaSRoger A. Faulkner 		getaline(*wdot);
5497c478bd9Sstevel@tonic-gate 		wcursor = linebuf;
5507c478bd9Sstevel@tonic-gate 		return (1);
5517c478bd9Sstevel@tonic-gate 	} else {
5527c478bd9Sstevel@tonic-gate 		wcursor = lastchr(linebuf, wcursor);
5537c478bd9Sstevel@tonic-gate 		if (wcursor >= linebuf)
5547c478bd9Sstevel@tonic-gate 			return (1);
5557c478bd9Sstevel@tonic-gate 		if (lf == lindent && linebuf[0] == '(')
5567c478bd9Sstevel@tonic-gate 			llimit = wdot;
5577c478bd9Sstevel@tonic-gate 		if (wdot <= llimit) {
5587c478bd9Sstevel@tonic-gate 			wcursor = linebuf;
5597c478bd9Sstevel@tonic-gate 			return (0);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		wdot--;
562*23a1cceaSRoger A. Faulkner 		getaline(*wdot);
5637c478bd9Sstevel@tonic-gate 		if(!*linebuf)
5647c478bd9Sstevel@tonic-gate 			wcursor = linebuf;
5657c478bd9Sstevel@tonic-gate 		else {
5667c478bd9Sstevel@tonic-gate 			wcursor = strend(linebuf);
5677c478bd9Sstevel@tonic-gate 			wcursor = lastchr(linebuf, wcursor);
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 		return (1);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
573f6db9f27Scf46844 int
lbrack(int c,int (* f)())574f6db9f27Scf46844 lbrack(int c, int (*f)())
5757c478bd9Sstevel@tonic-gate {
576f6db9f27Scf46844 	line *addr;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	addr = dot;
5797c478bd9Sstevel@tonic-gate 	for (;;) {
5807c478bd9Sstevel@tonic-gate 		addr += dir;
5817c478bd9Sstevel@tonic-gate 		if (addr < one || addr > dol) {
5827c478bd9Sstevel@tonic-gate 			addr -= dir;
5837c478bd9Sstevel@tonic-gate 			break;
5847c478bd9Sstevel@tonic-gate 		}
585*23a1cceaSRoger A. Faulkner 		getaline(*addr);
5867c478bd9Sstevel@tonic-gate 		if (linebuf[0] == '{' ||
5877c478bd9Sstevel@tonic-gate #ifdef XPG4
5887c478bd9Sstevel@tonic-gate 		    /* POSIX 1003.2 Section 5.35.7.1: control-L		*/
5897c478bd9Sstevel@tonic-gate 		    linebuf[0] == CTRL('L') ||
5907c478bd9Sstevel@tonic-gate #endif /* XPG4 */
5917c478bd9Sstevel@tonic-gate 		    value(vi_LISP) && linebuf[0] == '(' ||
5927c478bd9Sstevel@tonic-gate 		    isa(svalue(vi_SECTIONS))) {
5937c478bd9Sstevel@tonic-gate 			if (c == ']' && f != vmove) {
5947c478bd9Sstevel@tonic-gate 				addr--;
595*23a1cceaSRoger A. Faulkner 				getaline(*addr);
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			break;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		if (c == ']' && f != vmove && linebuf[0] == '}')
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	if (addr == dot)
6037c478bd9Sstevel@tonic-gate 		return (0);
6047c478bd9Sstevel@tonic-gate 	if (f != vmove)
6057c478bd9Sstevel@tonic-gate 		wcursor = c == ']' ? strend(linebuf) : linebuf;
6067c478bd9Sstevel@tonic-gate 	else
6077c478bd9Sstevel@tonic-gate 		wcursor = 0;
6087c478bd9Sstevel@tonic-gate 	wdot = addr;
6097c478bd9Sstevel@tonic-gate 	vmoving = 0;
6107c478bd9Sstevel@tonic-gate 	return (1);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
613f6db9f27Scf46844 int
isa(unsigned char * cp)614f6db9f27Scf46844 isa(unsigned char *cp)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (linebuf[0] != '.')
6187c478bd9Sstevel@tonic-gate 		return (0);
6197c478bd9Sstevel@tonic-gate 	for (; cp[0] && cp[1]; cp += 2)
6207c478bd9Sstevel@tonic-gate 		if (linebuf[1] == cp[0]) {
6217c478bd9Sstevel@tonic-gate 			if (linebuf[2] == cp[1])
6227c478bd9Sstevel@tonic-gate 				return (1);
6237c478bd9Sstevel@tonic-gate 			if (linebuf[2] == 0 && cp[1] == ' ')
6247c478bd9Sstevel@tonic-gate 				return (1);
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
628