1 /* $NetBSD: v_match.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: v_match.c,v 10.10 2001/06/25 15:19:32 skimo Exp (Berkeley) Date: 2001/06/25 15:19:32 ";
19 __RCSID("$NetBSD: v_match.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
31 #include "../common/common.h"
36 * Search to matching character.
38 * PUBLIC: int v_match __P((SCR *, VICMD *));
41 v_match(SCR
*sp
, VICMD
*vp
)
46 int cnt
, isempty
, matchc
, startc
, (*gc
)__P((SCR
*, VCS
*));
49 const char *match_chars
;
51 static MARK match
= { 0, 0 };
55 * Historically vi would match (), {} and [] however
56 * an update included <>. This is ok for editing HTML
57 * but a pain in the butt for C source.
58 * Making it an option lets the user decide what is 'right'.
59 * Also fixed to do something sensible with "".
61 match_chars
= O_STR(sp
, O_MATCHCHARS
);
65 * Historic practice; ignore the count.
68 * Historical practice was to search for the initial character in the
69 * forward direction only.
71 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
76 for (off
= vp
->m_start
.cno
;; ++off
) {
78 nomatch
: msgq(sp
, M_BERR
, "184|No match character on this line");
82 cp
= strchr(match_chars
, startc
);
86 cnt
= cp
- match_chars
;
87 matchc
= match_chars
[cnt
^ 1];
89 /* Alternate back-forward search if startc and matchc the same */
90 if (startc
== matchc
) {
91 /* are we continuing from where last match finished? */
92 if (match
.lno
== vp
->m_start
.lno
&& match
.cno
==vp
->m_start
.cno
)
93 /* yes - continue in sequence */
96 /* no - go forward, back, back, forward */
101 gc
= cnt
& 1 ? cs_prev
: cs_next
;
103 cs
.cs_lno
= vp
->m_start
.lno
;
105 if (cs_init(sp
, &cs
))
110 if (cs
.cs_flags
!= 0) {
111 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
)
115 if (cs
.cs_ch
== matchc
&& --cnt
== 0)
117 if (cs
.cs_ch
== startc
)
121 msgq(sp
, M_BERR
, "185|Matching character not found");
125 vp
->m_stop
.lno
= cs
.cs_lno
;
126 vp
->m_stop
.cno
= cs
.cs_cno
;
129 * If moving right, non-motion commands move to the end of the range.
130 * Delete and yank stay at the start.
132 * If moving left, all commands move to the end of the range.
135 * Don't correct for leftward movement -- historic vi deleted the
136 * starting cursor position when deleting to a match.
138 if (vp
->m_start
.lno
< vp
->m_stop
.lno
||
139 (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
140 vp
->m_start
.cno
< vp
->m_stop
.cno
))
141 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
143 vp
->m_final
= vp
->m_stop
;
145 match
.lno
= vp
->m_final
.lno
;
146 match
.cno
= vp
->m_final
.cno
;
150 * If the motion is across lines, and the earliest cursor position
151 * is at or before any non-blank characters in the line, i.e. the
152 * movement is cutting all of the line's text, and the later cursor
153 * position has nothing other than whitespace characters between it
154 * and the end of its line, the buffer is in line mode.
156 if (!ISMOTION(vp
) || vp
->m_start
.lno
== vp
->m_stop
.lno
)
158 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_start
: &vp
->m_stop
;
161 if (nonblank(sp
, mp
->lno
, &cno
))
166 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_stop
: &vp
->m_start
;
167 if (db_get(sp
, mp
->lno
, DBG_FATAL
, &p
, &len
))
169 for (p
+= mp
->cno
+ 1, len
-= mp
->cno
; --len
; ++p
)
170 if (!ISBLANK((UCHAR_T
)*p
))