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.11 2012/02/11 00:33:46 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
27 #include "../common/common.h"
32 * Search to matching character.
34 * PUBLIC: int v_match(SCR *, VICMD *);
37 v_match(SCR
*sp
, VICMD
*vp
)
42 int cnt
, isempty
, matchc
, startc
, (*gc
)(SCR
*, VCS
*);
45 const CHAR_T
*match_chars
;
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'.
53 match_chars
= VIP(sp
)->mcs
;
57 * Historic practice; ignore the count.
60 * Historical practice was to search for the initial character in the
61 * forward direction only.
63 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
68 for (off
= vp
->m_start
.cno
;; ++off
) {
70 nomatch
: msgq(sp
, M_BERR
, "184|No match character on this line");
74 cp
= STRCHR(match_chars
, startc
);
76 cnt
= cp
- match_chars
;
77 matchc
= match_chars
[cnt
^ 1];
78 gc
= cnt
& 1 ? cs_prev
: cs_next
;
83 cs
.cs_lno
= vp
->m_start
.lno
;
90 if (cs
.cs_flags
!= 0) {
91 if (cs
.cs_flags
== CS_EOF
|| cs
.cs_flags
== CS_SOF
)
95 if (cs
.cs_ch
== startc
)
97 else if (cs
.cs_ch
== matchc
&& --cnt
== 0)
101 msgq(sp
, M_BERR
, "185|Matching character not found");
105 vp
->m_stop
.lno
= cs
.cs_lno
;
106 vp
->m_stop
.cno
= cs
.cs_cno
;
109 * If moving right, non-motion commands move to the end of the range.
110 * Delete and yank stay at the start.
112 * If moving left, all commands move to the end of the range.
115 * Don't correct for leftward movement -- historic vi deleted the
116 * starting cursor position when deleting to a match.
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
;
123 vp
->m_final
= vp
->m_stop
;
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.
133 if (!ISMOTION(vp
) || vp
->m_start
.lno
== vp
->m_stop
.lno
)
135 mp
= vp
->m_start
.lno
< vp
->m_stop
.lno
? &vp
->m_start
: &vp
->m_stop
;
138 if (nonblank(sp
, mp
->lno
, &cno
))
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
))
146 for (p
+= mp
->cno
+ 1, len
-= mp
->cno
; --len
; ++p
)
155 * Build the match character list.
157 * PUBLIC: int v_buildmcs(SCR *, char *);
160 v_buildmcs(SCR
*sp
, char *str
)
162 CHAR_T
**mp
= &VIP(sp
)->mcs
;
163 size_t len
= strlen(str
) + 1;
167 MALLOC(sp
, *mp
, CHAR_T
*, len
* sizeof(CHAR_T
));
171 if (mbstowcs(*mp
, str
, len
) == (size_t)-1)
174 memcpy(*mp
, str
, len
);