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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
23 #include "../common/common.h"
28 * Search to matching character.
30 * PUBLIC: int v_match(SCR *, VICMD *);
33 v_match(SCR *sp, VICMD *vp)
38 int cnt, isempty, matchc, startc, (*gc)(SCR *, VCS *);
41 const CHAR_T *match_chars;
44 * Historically vi would match (), {} and [] however
45 * an update included <>. This is ok for editing HTML
46 * but a pain in the butt for C source.
47 * Making it an option lets the user decide what is 'right'.
49 match_chars = VIP(sp)->mcs;
53 * Historic practice; ignore the count.
56 * Historical practice was to search for the initial character in the
57 * forward direction only.
59 if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
64 for (off = vp->m_start.cno;; ++off) {
66 nomatch: msgq(sp, M_BERR, "184|No match character on this line");
70 cp = STRCHR(match_chars, startc);
72 cnt = cp - match_chars;
73 matchc = match_chars[cnt ^ 1];
74 gc = cnt & 1 ? cs_prev : cs_next;
79 cs.cs_lno = vp->m_start.lno;
86 if (cs.cs_flags != 0) {
87 if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF)
91 if (cs.cs_ch == startc)
93 else if (cs.cs_ch == matchc && --cnt == 0)
97 msgq(sp, M_BERR, "185|Matching character not found");
101 vp->m_stop.lno = cs.cs_lno;
102 vp->m_stop.cno = cs.cs_cno;
105 * If moving right, non-motion commands move to the end of the range.
106 * Delete and yank stay at the start.
108 * If moving left, all commands move to the end of the range.
111 * Don't correct for leftward movement -- historic vi deleted the
112 * starting cursor position when deleting to a match.
114 if (vp->m_start.lno < vp->m_stop.lno ||
115 (vp->m_start.lno == vp->m_stop.lno &&
116 vp->m_start.cno < vp->m_stop.cno))
117 vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
119 vp->m_final = vp->m_stop;
123 * If the motion is across lines, and the earliest cursor position
124 * is at or before any non-blank characters in the line, i.e. the
125 * movement is cutting all of the line's text, and the later cursor
126 * position has nothing other than whitespace characters between it
127 * and the end of its line, the buffer is in line mode.
129 if (!ISMOTION(vp) || vp->m_start.lno == vp->m_stop.lno)
131 mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_start : &vp->m_stop;
134 if (nonblank(sp, mp->lno, &cno))
139 mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_stop : &vp->m_start;
140 if (db_get(sp, mp->lno, DBG_FATAL, &p, &len))
142 for (p += mp->cno + 1, len -= mp->cno; --len; ++p)
151 * Build the match character list.
153 * PUBLIC: int v_buildmcs(SCR *, char *);
156 v_buildmcs(SCR *sp, char *str)
158 CHAR_T **mp = &VIP(sp)->mcs;
159 size_t len = strlen(str) + 1;
162 MALLOC(sp, *mp, len * sizeof(CHAR_T));
166 if (mbstowcs(*mp, str, len) == (size_t)-1)
169 memcpy(*mp, str, len);