2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: v_match.c,v 8.6 1993/11/07 15:19:32 bostic Exp $ (Berkeley) $Date: 1993/11/07 15:19:32 $";
12 #include <sys/types.h>
19 static int findmatchc
__P((MARK
*, char *, size_t, MARK
*));
23 * Search to matching character.
26 v_match(sp
, ep
, vp
, fm
, tm
, rp
)
32 register int cnt
, matchc
, startc
;
36 int (*gc
)__P((SCR
*, EXF
*, VCS
*));
39 if ((p
= file_gline(sp
, ep
, fm
->lno
, &len
)) == NULL
) {
40 if (file_lline(sp
, ep
, &lno
))
44 GETLINE_ERR(sp
, fm
->lno
);
51 retry
: switch (startc
= p
[fm
->cno
]) {
77 if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
)) {
79 "No proximity match for motion commands.");
82 if (len
== 0 || findmatchc(fm
, p
, len
, rp
)) {
83 nomatch
: msgq(sp
, M_BERR
, "No match character on this line.");
92 if (cs_init(sp
, ep
, &cs
))
97 if (cs
.cs_flags
!= 0) {
98 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
)
102 if (cs
.cs_ch
== startc
)
104 else if (cs
.cs_ch
== matchc
&& --cnt
== 0)
108 msgq(sp
, M_BERR
, "Matching character not found.");
115 * Movement commands go one space further. Increment the return
116 * MARK or from MARK depending on the direction of the search.
118 if (F_ISSET(vp
, VC_C
| VC_D
| VC_Y
)) {
119 if (file_gline(sp
, ep
, rp
->lno
, &len
) == NULL
) {
120 GETLINE_ERR(sp
, rp
->lno
);
134 * If we're not on a character we know how to match, try and find the
135 * closest character from the set "{}[]()". The historic vi did this
136 * as well, but it only searched forward from the cursor. This seems
137 * wrong, so we search both forward and backward on the line, going
138 * to the closest hit. Ties go left because differently handed people
139 * have been traditionally discriminated against by our society.
142 findmatchc(fm
, p
, len
, rp
)
148 size_t left
, right
; /* Can't be uninitialized. */
149 int leftfound
, rightfound
;
152 leftfound
= rightfound
= 0;
153 for (off
= 0, t
= &p
[off
]; off
++ < fm
->cno
;)
154 if (strchr("{}[]()", *t
++)) {
160 for (off
= fm
->cno
+ 1, t
= &p
[off
]; off
++ < len
;)
161 if (strchr("{}[]()", *t
++)) {
170 if (fm
->cno
- left
> right
- fm
->cno
)