Debian Codegraph @

move.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "re.h"
10 #include "chars.h"
11 #include "jctype.h"
12 #include "disp.h"
13 #include "move.h"
14 #include "screen.h" /* for tabstop */
15
16 private int line_pos;
17
18 void
19 f_char(long n)
20 {
21 if (n < 0) {
22 b_char(-n);
23 return;
24 }
25 while (--n >= 0) {
26 if (eolp()) { /* Go to the next Line */
27 if (curline->l_next == NULL)
28 break;
29
30 SetLine(curline->l_next);
31 } else
32 curchar += 1;
33 }
34 }
35
36 void
37 b_char(long n)
38 {
39 if (n < 0) {
40 f_char(-n);
41 return;
42 }
43 while (--n >= 0) {
44 if (bolp()) {
45 if (curline->l_prev == NULL)
46 break;
47
48 SetLine(curline->l_prev);
49 Eol();
50 } else
51 curchar -= 1;
52 }
53 }
54
55 void
56 ForChar(void)
57 {
58 f_char(arg_value());
59 }
60
61 void
62 BackChar(void)
63 {
64 b_char(arg_value());
65 }
66
67 void
68 NextLine(void)
69 {
70 if ((curline == curbuf->b_last) && eolp()) {
71 complain(NullStr);
72 /* NOTREACHED */
73 }
74 line_move(FORWARD, arg_value(), YES);
75 }
76
77 void
78 PrevLine(void)
79 {
80 if ((curline == curbuf->b_first) && bolp()) {
81 complain(NullStr);
82 /* NOTREACHED */
83 }
84 line_move(BACKWARD, arg_value(), YES);
85 }
86
87 /* moves to a different line in DIR; LINE_CMD says whether this is
88 * being called from NextLine() or PrevLine(), in which case it tries
89 * to line up the column with the column of the current line
90 */
91 void
92 line_move(int dir, long n, jbool line_cmd)
93 {
94 LinePtr (*proc)(LinePtr, long) =
95 (dir == FORWARD) ? next_line : prev_line;
96 LinePtr line;
97
98 line = (*proc)(curline, n);
99 if (line == curline) {
100 if (dir == FORWARD)
101 Eol();
102 else
103 Bol();
104 return;
105 }
106
107 if (line_cmd) {
108 this_cmd = LINECMD;
109 if (last_cmd != LINECMD)
110 line_pos = calc_pos(linebuf, curchar);
111 }
112 SetLine(line); /* curline is in linebuf now */
113 if (line_cmd)
114 curchar = how_far(curline, line_pos);
115 }
116
117 /* how_far returns what cur_char should be to be at or beyond col
118 * screen columns in to the line.
119 *
120 * Note: if col indicates a position in the middle of a Tab or other
121 * extended character, the result corresponds to that character
122 * (as if col had indicated its start).
123 *
124 * Note: the calc_pos, how_far, and DeTab must be in synch --
125 * each thinks it knows how characters are displayed.
126 */
127
128 int
129 how_far(LinePtr line, int col)
130 {
131 char *lp = lcontents(line),
132 *base = lp;
133 int pos = 0;
134 ZXchar c;
135
136 do {
137 if ((c = ZXC(*lp)) == '\t' && tabstop != 0) {
138 pos += TABDIST(pos);
139 } else if (jisprint(c)) {
140 pos += 1;
141 } else {
142 if (c <= DEL)
143 pos += 2;
144 else
145 pos += 4;
146 }
147 lp += 1;
148 } while (pos <= col && c != '\0');
149
150 return lp - base - 1;
151 }
152
153 void
154 Bol(void)
155 {
156 curchar = 0;
157 }
158
159 void
160 Eol(void)
161 {
162 curchar = length(curline);
163 }
164
165 void
166 Eof(void)
167 {
168 PushPntp(curbuf->b_last);
169 ToLast();
170 }
171
172 void
173 Bof(void)
174 {
175 PushPntp(curbuf->b_first);
176 ToFirst();
177 }
178
179 /* Move forward (if dir > 0) or backward (if dir < 0) a sentence. Deals
180 * with all the kludgery involved with paragraphs, and moving backwards
181 * is particularly yucky.
182 */
183 private void
184 to_sent(int dir)
185 {
186 for (;;) {
187 Bufpos
188 old, /* where we started */
189 *new; /* where dosearch stopped */
190
191 DOTsave(&old);
192
193 new = dosearch(
194 "^[ \t]*$\\|[?.!]\\{''\\|[\"')\\]]\\|\\}\\{$\\|[ \t]\\}",
195 dir, YES);
196 if (new == NULL) {
197 if (dir == BACKWARD)
198 ToFirst();
199 else
200 ToLast();
201 break;
202 }
203 SetDot(new);
204 if (dir < 0) {
205 to_word(FORWARD);
206 if ((old.p_line != curline || old.p_char > curchar)
207 && (!inorder(new->p_line, new->p_char, old.p_line, old.p_char)
208 || !inorder(old.p_line, old.p_char, curline, curchar)))
209 break;
210
211 SetDot(new);
212 } else {
213 if (blnkp(linebuf)) {
214 Bol();
215 b_char(1);
216 if (old.p_line != curline || old.p_char < curchar)
217 break;
218
219 to_word(FORWARD); /* Oh brother this is painful */
220 } else {
221 curchar = REbom + 1; /* Just after the [?.!] */
222 if (LookingAt("''\\|[\"')\\]]", linebuf, curchar))
223 curchar = REeom;
224 break;
225 }
226 }
227 }
228 }
229
230 void
231 Bos(void)
232 {
233 int num = arg_value();
234
235 if (num < 0) {
236 negate_arg();
237 Eos();
238 } else {
239 while (--num >= 0) {
240 to_sent(BACKWARD);
241 if (bobp())
242 break;
243 }
244 }
245 }
246
247 void
248 Eos(void)
249 {
250 int num = arg_value();
251
252 if (num < 0) {
253 negate_arg();
254 Bos();
255 } else {
256 while (--num >= 0) {
257 to_sent(FORWARD);
258 if (eobp())
259 break;
260 }
261 }
262 }
263
264 void
265 f_word(long num)
266 {
267 if (num < 0) {
268 while (++num <= 0) {
269 to_word(BACKWARD);
270 while (!bolp() && jisword(linebuf[curchar - 1]))
271 curchar -= 1;
272 if (bobp())
273 break;
274 }
275 } else {
276 while (--num >= 0) {
277 char c;
278
279 to_word(FORWARD);
280 while ((c = linebuf[curchar]) != '\0' && jisword(c))
281 curchar += 1;
282 if (eobp())
283 break;
284 }
285 }
286 /* ??? why is the following necessary? -- DHR */
287 this_cmd = OTHER_CMD; /* Semi kludge to stop some unfavorable behavior */
288 }
289
290 void
291 ForWord(void)
292 {
293 f_word(arg_value());
294 }
295
296 void
297 BackWord(void)
298 {
299 f_word(-arg_value());
300 }