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[] = "@(#)v_match.c 10.8 (Berkeley) 3/6/96";
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 *));
42 int cnt, isempty, matchc, startc, (*gc)__P((SCR *, VCS *));
47 * Historic practice; ignore the count.
50 * Historical practice was to search for the initial character in the
51 * forward direction only.
53 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
58 for (off = vp->m_start.cno;; ++off) {
60 nomatch: msgq(sp, M_BERR, "184|No match character on this line");
63 switch (startc = p[off]) {
102 cs.cs_lno = vp->m_start.lno;
104 if (cs_init(sp, &cs))
109 if (cs.cs_flags != 0) {
110 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF)
114 if (cs.cs_ch == startc)
116 else if (cs.cs_ch == matchc && --cnt == 0)
120 msgq(sp, M_BERR, "185|Matching character not found");
124 vp->m_stop.lno = cs.cs_lno;
125 vp->m_stop.cno = cs.cs_cno;
128 * If moving right, non-motion commands move to the end of the range.
129 * Delete and yank stay at the start.
131 * If moving left, all commands move to the end of the range.
134 * Don't correct for leftward movement -- historic vi deleted the
135 * starting cursor position when deleting to a match.
137 if (vp->m_start.lno < vp->m_stop.lno ||
138 vp->m_start.lno == vp->m_stop.lno &&
139 vp->m_start.cno < vp->m_stop.cno)
140 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
142 vp->m_final = vp->m_stop;
146 * If the motion is across lines, and the earliest cursor position
147 * is at or before any non-blank characters in the line, i.e. the
148 * movement is cutting all of the line's text, and the later cursor
149 * position has nothing other than whitespace characters between it
150 * and the end of its line, the buffer is in line mode.
152 if (!ISMOTION(vp) || vp->m_start.lno == vp->m_stop.lno)
154 mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_start : &vp->m_stop;
157 if (nonblank(sp, mp->lno, &cno))
162 mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_stop : &vp->m_start;
163 if (db_get(sp, mp->lno, DBG_FATAL, &p, &len))
165 for (p += mp->cno + 1, len -= mp->cno; --len; ++p)