1 /* $NetBSD: v_match.c,v 1.2 2013/11/22 15:52:06 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
26 #include "../common/common.h"
31 * Search to matching character.
33 * PUBLIC: int v_match __P((SCR *, VICMD *));
36 v_match(SCR
*sp
, VICMD
*vp
)
41 int cnt
, isempty
, matchc
, startc
, (*gc
)__P((SCR
*, VCS
*));
44 const char *match_chars
;
46 static MARK match
= { 0, 0 };
50 * Historically vi would match (), {} and [] however
51 * an update included <>. This is ok for editing HTML
52 * but a pain in the butt for C source.
53 * Making it an option lets the user decide what is 'right'.
54 * Also fixed to do something sensible with "".
56 match_chars
= O_STR(sp
, O_MATCHCHARS
);
60 * Historic practice; ignore the count.
63 * Historical practice was to search for the initial character in the
64 * forward direction only.
66 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
71 for (off
= vp
->m_start
.cno
;; ++off
) {
73 nomatch
: msgq(sp
, M_BERR
, "184|No match character on this line");
77 cp
= strchr(match_chars
, startc
);
81 cnt
= cp
- match_chars
;
82 matchc
= match_chars
[cnt
^ 1];
84 /* Alternate back-forward search if startc and matchc the same */
85 if (startc
== matchc
) {
86 /* are we continuing from where last match finished? */
87 if (match
.lno
== vp
->m_start
.lno
&& match
.cno
==vp
->m_start
.cno
)
88 /* yes - continue in sequence */
91 /* no - go forward, back, back, forward */
96 gc
= cnt
& 1 ? cs_prev
: cs_next
;
98 cs
.cs_lno
= vp
->m_start
.lno
;
100 if (cs_init(sp
, &cs
))
105 if (cs
.cs_flags
!= 0) {
106 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
)
110 if (cs
.cs_ch
== matchc
&& --cnt
== 0)
112 if (cs
.cs_ch
== startc
)
116 msgq(sp
, M_BERR
, "185|Matching character not found");
120 vp
->m_stop
.lno
= cs
.cs_lno
;
121 vp
->m_stop
.cno
= cs
.cs_cno
;
124 * If moving right, non-motion commands move to the end of the range.
125 * Delete and yank stay at the start.
127 * If moving left, all commands move to the end of the range.
130 * Don't correct for leftward movement -- historic vi deleted the
131 * starting cursor position when deleting to a match.
133 if (vp
->m_start
.lno
< vp
->m_stop
.lno
||
134 (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
135 vp
->m_start
.cno
< vp
->m_stop
.cno
))
136 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
138 vp
->m_final
= vp
->m_stop
;
140 match
.lno
= vp
->m_final
.lno
;
141 match
.cno
= vp
->m_final
.cno
;
145 * If the motion is across lines, and the earliest cursor position
146 * is at or before any non-blank characters in the line, i.e. the
147 * movement is cutting all of the line's text, and the later cursor
148 * position has nothing other than whitespace characters between it
149 * and the end of its line, the buffer is in line mode.
151 if (!ISMOTION(vp
) || vp
->m_start
.lno
== vp
->m_stop
.lno
)
153 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_start
: &vp
->m_stop
;
156 if (nonblank(sp
, mp
->lno
, &cno
))
161 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_stop
: &vp
->m_start
;
162 if (db_get(sp
, mp
->lno
, DBG_FATAL
, &p
, &len
))
164 for (p
+= mp
->cno
+ 1, len
-= mp
->cno
; --len
; ++p
)
165 if (!ISBLANK((UCHAR_T
)*p
))