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