xref: /freebsd/contrib/nvi/vi/v_match.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "../common/common.h"
24 #include "vi.h"
25 
26 /*
27  * v_match -- %
28  *	Search to matching character.
29  *
30  * PUBLIC: int v_match(SCR *, VICMD *);
31  */
32 int
33 v_match(SCR *sp, VICMD *vp)
34 {
35 	VCS cs;
36 	MARK *mp;
37 	size_t cno, len, off;
38 	int cnt, isempty, matchc, startc, (*gc)(SCR *, VCS *);
39 	CHAR_T *p;
40 	CHAR_T *cp;
41 	const CHAR_T *match_chars;
42 
43 	/*
44 	 * Historically vi would match (), {} and [] however
45 	 * an update included <>.  This is ok for editing HTML
46 	 * but a pain in the butt for C source.
47 	 * Making it an option lets the user decide what is 'right'.
48 	 */
49 	match_chars = VIP(sp)->mcs;
50 
51 	/*
52 	 * !!!
53 	 * Historic practice; ignore the count.
54 	 *
55 	 * !!!
56 	 * Historical practice was to search for the initial character in the
57 	 * forward direction only.
58 	 */
59 	if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
60 		if (isempty)
61 			goto nomatch;
62 		return (1);
63 	}
64 	for (off = vp->m_start.cno;; ++off) {
65 		if (off >= len) {
66 nomatch:		msgq(sp, M_BERR, "184|No match character on this line");
67 			return (1);
68 		}
69 		startc = p[off];
70 		cp = STRCHR(match_chars, startc);
71 		if (cp != NULL) {
72 			cnt = cp - match_chars;
73 			matchc = match_chars[cnt ^ 1];
74 			gc = cnt & 1 ? cs_prev : cs_next;
75 			break;
76 		}
77 	}
78 
79 	cs.cs_lno = vp->m_start.lno;
80 	cs.cs_cno = off;
81 	if (cs_init(sp, &cs))
82 		return (1);
83 	for (cnt = 1;;) {
84 		if (gc(sp, &cs))
85 			return (1);
86 		if (cs.cs_flags != 0) {
87 			if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF)
88 				break;
89 			continue;
90 		}
91 		if (cs.cs_ch == startc)
92 			++cnt;
93 		else if (cs.cs_ch == matchc && --cnt == 0)
94 			break;
95 	}
96 	if (cnt) {
97 		msgq(sp, M_BERR, "185|Matching character not found");
98 		return (1);
99 	}
100 
101 	vp->m_stop.lno = cs.cs_lno;
102 	vp->m_stop.cno = cs.cs_cno;
103 
104 	/*
105 	 * If moving right, non-motion commands move to the end of the range.
106 	 * Delete and yank stay at the start.
107 	 *
108 	 * If moving left, all commands move to the end of the range.
109 	 *
110 	 * !!!
111 	 * Don't correct for leftward movement -- historic vi deleted the
112 	 * starting cursor position when deleting to a match.
113 	 */
114 	if (vp->m_start.lno < vp->m_stop.lno ||
115 	    (vp->m_start.lno == vp->m_stop.lno &&
116 	    vp->m_start.cno < vp->m_stop.cno))
117 		vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
118 	else
119 		vp->m_final = vp->m_stop;
120 
121 	/*
122 	 * !!!
123 	 * If the motion is across lines, and the earliest cursor position
124 	 * is at or before any non-blank characters in the line, i.e. the
125 	 * movement is cutting all of the line's text, and the later cursor
126 	 * position has nothing other than whitespace characters between it
127 	 * and the end of its line, the buffer is in line mode.
128 	 */
129 	if (!ISMOTION(vp) || vp->m_start.lno == vp->m_stop.lno)
130 		return (0);
131 	mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_start : &vp->m_stop;
132 	if (mp->cno != 0) {
133 		cno = 0;
134 		if (nonblank(sp, mp->lno, &cno))
135 			return (1);
136 		if (cno < mp->cno)
137 			return (0);
138 	}
139 	mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_stop : &vp->m_start;
140 	if (db_get(sp, mp->lno, DBG_FATAL, &p, &len))
141 		return (1);
142 	for (p += mp->cno + 1, len -= mp->cno; --len; ++p)
143 		if (!isblank(*p))
144 			return (0);
145 	F_SET(vp, VM_LMODE);
146 	return (0);
147 }
148 
149 /*
150  * v_buildmcs --
151  *	Build the match character list.
152  *
153  * PUBLIC: int v_buildmcs(SCR *, char *);
154  */
155 int
156 v_buildmcs(SCR *sp, char *str)
157 {
158 	CHAR_T **mp = &VIP(sp)->mcs;
159 	size_t len = strlen(str) + 1;
160 
161 	free(*mp);
162 	MALLOC(sp, *mp, len * sizeof(CHAR_T));
163 	if (*mp == NULL)
164 		return (1);
165 #ifdef USE_WIDECHAR
166 	if (mbstowcs(*mp, str, len) == (size_t)-1)
167 		return (1);
168 #else
169 	memcpy(*mp, str, len);
170 #endif
171 	return (0);
172 }
173