v_match.c revision 1.1.1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1992, 1993, 1994
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos 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 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/time.h>
19 1.1 christos
20 1.1 christos #include <bitstring.h>
21 1.1 christos #include <limits.h>
22 1.1 christos #include <stdio.h>
23 1.1 christos #include <string.h>
24 1.1 christos
25 1.1 christos #include "../common/common.h"
26 1.1 christos #include "vi.h"
27 1.1 christos
28 1.1 christos /*
29 1.1 christos * v_match -- %
30 1.1 christos * Search to matching character.
31 1.1 christos *
32 1.1 christos * PUBLIC: int v_match __P((SCR *, VICMD *));
33 1.1 christos */
34 1.1 christos int
35 1.1 christos v_match(SCR *sp, VICMD *vp)
36 1.1 christos {
37 1.1 christos VCS cs;
38 1.1 christos MARK *mp;
39 1.1 christos size_t cno, len, off;
40 1.1 christos int cnt, isempty, matchc, startc, (*gc)__P((SCR *, VCS *));
41 1.1 christos CHAR_T *p;
42 1.1 christos
43 1.1 christos /*
44 1.1 christos * !!!
45 1.1 christos * Historic practice; ignore the count.
46 1.1 christos *
47 1.1 christos * !!!
48 1.1 christos * Historical practice was to search for the initial character in the
49 1.1 christos * forward direction only.
50 1.1 christos */
51 1.1 christos if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
52 1.1 christos if (isempty)
53 1.1 christos goto nomatch;
54 1.1 christos return (1);
55 1.1 christos }
56 1.1 christos for (off = vp->m_start.cno;; ++off) {
57 1.1 christos if (off >= len) {
58 1.1 christos nomatch: msgq(sp, M_BERR, "184|No match character on this line");
59 1.1 christos return (1);
60 1.1 christos }
61 1.1 christos switch (startc = p[off]) {
62 1.1 christos case '(':
63 1.1 christos matchc = ')';
64 1.1 christos gc = cs_next;
65 1.1 christos break;
66 1.1 christos case ')':
67 1.1 christos matchc = '(';
68 1.1 christos gc = cs_prev;
69 1.1 christos break;
70 1.1 christos case '[':
71 1.1 christos matchc = ']';
72 1.1 christos gc = cs_next;
73 1.1 christos break;
74 1.1 christos case ']':
75 1.1 christos matchc = '[';
76 1.1 christos gc = cs_prev;
77 1.1 christos break;
78 1.1 christos case '{':
79 1.1 christos matchc = '}';
80 1.1 christos gc = cs_next;
81 1.1 christos break;
82 1.1 christos case '}':
83 1.1 christos matchc = '{';
84 1.1 christos gc = cs_prev;
85 1.1 christos break;
86 1.1 christos case '<':
87 1.1 christos matchc = '>';
88 1.1 christos gc = cs_next;
89 1.1 christos break;
90 1.1 christos case '>':
91 1.1 christos matchc = '<';
92 1.1 christos gc = cs_prev;
93 1.1 christos break;
94 1.1 christos default:
95 1.1 christos continue;
96 1.1 christos }
97 1.1 christos break;
98 1.1 christos }
99 1.1 christos
100 1.1 christos cs.cs_lno = vp->m_start.lno;
101 1.1 christos cs.cs_cno = off;
102 1.1 christos if (cs_init(sp, &cs))
103 1.1 christos return (1);
104 1.1 christos for (cnt = 1;;) {
105 1.1 christos if (gc(sp, &cs))
106 1.1 christos return (1);
107 1.1 christos if (cs.cs_flags != 0) {
108 1.1 christos if (cs.cs_flags == CS_EOF || cs.cs_flags == CS_SOF)
109 1.1 christos break;
110 1.1 christos continue;
111 1.1 christos }
112 1.1 christos if (cs.cs_ch == startc)
113 1.1 christos ++cnt;
114 1.1 christos else if (cs.cs_ch == matchc && --cnt == 0)
115 1.1 christos break;
116 1.1 christos }
117 1.1 christos if (cnt) {
118 1.1 christos msgq(sp, M_BERR, "185|Matching character not found");
119 1.1 christos return (1);
120 1.1 christos }
121 1.1 christos
122 1.1 christos vp->m_stop.lno = cs.cs_lno;
123 1.1 christos vp->m_stop.cno = cs.cs_cno;
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * If moving right, non-motion commands move to the end of the range.
127 1.1 christos * Delete and yank stay at the start.
128 1.1 christos *
129 1.1 christos * If moving left, all commands move to the end of the range.
130 1.1 christos *
131 1.1 christos * !!!
132 1.1 christos * Don't correct for leftward movement -- historic vi deleted the
133 1.1 christos * starting cursor position when deleting to a match.
134 1.1 christos */
135 1.1 christos if (vp->m_start.lno < vp->m_stop.lno ||
136 1.1 christos vp->m_start.lno == vp->m_stop.lno &&
137 1.1 christos vp->m_start.cno < vp->m_stop.cno)
138 1.1 christos vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
139 1.1 christos else
140 1.1 christos vp->m_final = vp->m_stop;
141 1.1 christos
142 1.1 christos /*
143 1.1 christos * !!!
144 1.1 christos * If the motion is across lines, and the earliest cursor position
145 1.1 christos * is at or before any non-blank characters in the line, i.e. the
146 1.1 christos * movement is cutting all of the line's text, and the later cursor
147 1.1 christos * position has nothing other than whitespace characters between it
148 1.1 christos * and the end of its line, the buffer is in line mode.
149 1.1 christos */
150 1.1 christos if (!ISMOTION(vp) || vp->m_start.lno == vp->m_stop.lno)
151 1.1 christos return (0);
152 1.1 christos mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_start : &vp->m_stop;
153 1.1 christos if (mp->cno != 0) {
154 1.1 christos cno = 0;
155 1.1 christos if (nonblank(sp, mp->lno, &cno))
156 1.1 christos return (1);
157 1.1 christos if (cno < mp->cno)
158 1.1 christos return (0);
159 1.1 christos }
160 1.1 christos mp = vp->m_start.lno < vp->m_stop.lno ? &vp->m_stop : &vp->m_start;
161 1.1 christos if (db_get(sp, mp->lno, DBG_FATAL, &p, &len))
162 1.1 christos return (1);
163 1.1 christos for (p += mp->cno + 1, len -= mp->cno; --len; ++p)
164 1.1 christos if (!ISBLANK(*p))
165 1.1 christos return (0);
166 1.1 christos F_SET(vp, VM_LMODE);
167 1.1 christos return (0);
168 1.1 christos }
169