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.
7 * See the LICENSE file for redistribution information.
13 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 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
25 #include "../common/common.h"
30 * Search to matching character.
32 * PUBLIC: int v_match __P((SCR *, VICMD *));
35 v_match(SCR
*sp
, VICMD
*vp
)
40 int cnt
, isempty
, matchc
, startc
, (*gc
)__P((SCR
*, VCS
*));
45 * Historic practice; ignore the count.
48 * Historical practice was to search for the initial character in the
49 * forward direction only.
51 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
56 for (off
= vp
->m_start
.cno
;; ++off
) {
58 nomatch
: msgq(sp
, M_BERR
, "184|No match character on this line");
61 switch (startc
= p
[off
]) {
100 cs
.cs_lno
= vp
->m_start
.lno
;
102 if (cs_init(sp
, &cs
))
107 if (cs
.cs_flags
!= 0) {
108 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
)
112 if (cs
.cs_ch
== startc
)
114 else if (cs
.cs_ch
== matchc
&& --cnt
== 0)
118 msgq(sp
, M_BERR
, "185|Matching character not found");
122 vp
->m_stop
.lno
= cs
.cs_lno
;
123 vp
->m_stop
.cno
= cs
.cs_cno
;
126 * If moving right, non-motion commands move to the end of the range.
127 * Delete and yank stay at the start.
129 * If moving left, all commands move to the end of the range.
132 * Don't correct for leftward movement -- historic vi deleted the
133 * starting cursor position when deleting to a match.
135 if (vp
->m_start
.lno
< vp
->m_stop
.lno
||
136 vp
->m_start
.lno
== vp
->m_stop
.lno
&&
137 vp
->m_start
.cno
< vp
->m_stop
.cno
)
138 vp
->m_final
= ISMOTION(vp
) ? vp
->m_start
: vp
->m_stop
;
140 vp
->m_final
= vp
->m_stop
;
144 * If the motion is across lines, and the earliest cursor position
145 * is at or before any non-blank characters in the line, i.e. the
146 * movement is cutting all of the line's text, and the later cursor
147 * position has nothing other than whitespace characters between it
148 * and the end of its line, the buffer is in line mode.
150 if (!ISMOTION(vp
) || vp
->m_start
.lno
== vp
->m_stop
.lno
)
152 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_start
: &vp
->m_stop
;
155 if (nonblank(sp
, mp
->lno
, &cno
))
160 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_stop
: &vp
->m_start
;
161 if (db_get(sp
, mp
->lno
, DBG_FATAL
, &p
, &len
))
163 for (p
+= mp
->cno
+ 1, len
-= mp
->cno
; --len
; ++p
)