386BSD 0.1 development
[unix-history] / usr / othersrc / public / zsh-2.2 / src / zle_move.c
CommitLineData
dbf02a84
WJ
1/*
2 *
3 * zle_move.c - editor movement
4 *
5 * This file is part of zsh, the Z shell.
6 *
7 * This software is Copyright 1992 by Paul Falstad
8 *
9 * Permission is hereby granted to copy, reproduce, redistribute or otherwise
10 * use this software as long as: there is no monetary profit gained
11 * specifically from the use or reproduction of this software, it is not
12 * sold, rented, traded or otherwise marketed, and this copyright notice is
13 * included prominently in any copy made.
14 *
15 * The author make no claims as to the fitness or correctness of this software
16 * for any use whatsoever, and it is provided as is. Any use of this software
17 * is at the user's own risk.
18 *
19 */
20
21#define ZLE
22#include "zsh.h"
23
24void beginningofline() /**/
25{
26 if (mult < 0) { mult = -mult; endofline(); return; }
27 while (mult--) {
28 if (cs == 0)
29 return;
30 if (line[cs-1] == '\n')
31 if (!--cs)
32 return;
33 while (cs && line[cs-1] != '\n') cs--;
34 }
35}
36
37void endofline() /**/
38{
39 if (mult < 0) { mult = -mult; beginningofline(); return; }
40 while (mult--) {
41 if (cs >= ll) {
42 cs = ll;
43 return;
44 }
45 if (line[cs] == '\n')
46 if (++cs == ll)
47 return;
48 while (cs != ll && line[cs] != '\n') cs++;
49 }
50}
51
52void beginningoflinehist() /**/
53{
54 if (mult < 0) { mult = -mult; endoflinehist(); return; }
55 while (mult) {
56 if (cs == 0)
57 break;
58 if (line[cs-1] == '\n')
59 if (!--cs)
60 break;
61 while (cs && line[cs-1] != '\n') cs--;
62 mult--;
63 }
64 if (mult) {
65 uphistory();
66 cs = 0;
67 }
68}
69
70void endoflinehist() /**/
71{
72 if (mult < 0) { mult = -mult; beginningoflinehist(); return; }
73 while (mult) {
74 if (cs >= ll) {
75 cs = ll;
76 break;
77 }
78 if (line[cs] == '\n')
79 if (++cs == ll)
80 break;
81 while (cs != ll && line[cs] != '\n') cs++;
82 mult--;
83 }
84 if (mult)
85 downhistory();
86}
87
88void forwardchar() /**/
89{
90 cs += mult;
91 if (cs > ll) cs = ll;
92 if (cs < 0) cs = 0;
93}
94
95void backwardchar() /**/
96{
97 cs -= mult;
98 if (cs > ll) cs = ll;
99 if (cs < 0) cs = 0;
100}
101
102void setmarkcommand() /**/
103{
104 mark = cs;
105}
106
107void exchangepointandmark() /**/
108{
109int x;
110
111 x = mark;
112 mark = cs;
113 cs = x;
114 if (cs > ll)
115 cs = ll;
116}
117
118void vigotocolumn() /**/
119{
120int x,y,ocs = cs;
121
122 if (mult > 0) mult--;
123 findline(&x,&y);
124 if (mult >= 0) cs = x+mult; else cs = y+mult;
125 if (cs < x || cs > y) {
126 feep();
127 cs = ocs;
128 }
129}
130
131void vimatchbracket() /**/
132{
133int ocs = cs,dir,ct;
134char oth,me;
135
136otog:
137 if (cs == ll)
138 {
139 feep();
140 cs = ocs;
141 return;
142 }
143 switch(me = line[cs])
144 {
145 case '{': dir = 1; oth = '}'; break;
146 case '}': dir = -1; oth = '{'; break;
147 case '(': dir = 1; oth = ')'; break;
148 case ')': dir = -1; oth = '('; break;
149 case '[': dir = 1; oth = ']'; break;
150 case ']': dir = -1; oth = '['; break;
151 default: cs++; goto otog;
152 }
153 ct = 1;
154 while (cs >= 0 && cs < ll && ct)
155 {
156 cs += dir;
157 if (line[cs] == oth)
158 ct--;
159 else if (line[cs] == me)
160 ct++;
161 }
162 if (cs < 0 || cs >= ll)
163 {
164 feep();
165 cs = ocs;
166 }
167}
168
169void viforwardchar() /**/
170{
171 if (mult < 0) { mult = -mult; vibackwardchar(); return; }
172 while (mult--) {
173 cs++;
174 if (cs >= ll || line[cs] == '\n') {
175 cs--;
176 break;
177 }
178 }
179}
180
181void vibackwardchar() /**/
182{
183 if (mult < 0) { mult = -mult; viforwardchar(); return; }
184 while (mult--) {
185 cs--;
186 if (cs < 0 || line[cs] == '\n') {
187 cs++;
188 break;
189 }
190 }
191}
192
193void viendofline() /**/
194{
195 cs = findeol();
196 if (!virangeflag && cs != 0 && line[cs-1] != '\n') cs--;
197}
198
199void vibeginningofline() /**/
200{
201 cs = findbol();
202}
203
204
205static int vfindchar,vfinddir,tailadd;
206
207void vifindnextchar() /**/
208{
209 if (vfindchar = vigetkey())
210 {
211 vfinddir = 1;
212 tailadd = 0;
213 virepeatfind();
214 }
215}
216
217void vifindprevchar() /**/
218{
219 if (vfindchar = vigetkey())
220 {
221 vfinddir = -1;
222 tailadd = 0;
223 virepeatfind();
224 }
225}
226
227void vifindnextcharskip() /**/
228{
229 if (vfindchar = vigetkey())
230 {
231 vfinddir = 1;
232 tailadd = -1;
233 virepeatfind();
234 }
235}
236
237void vifindprevcharskip() /**/
238{
239 if (vfindchar = vigetkey())
240 {
241 vfinddir = -1;
242 tailadd = 1;
243 virepeatfind();
244 }
245}
246
247void virepeatfind() /**/
248{
249int ocs = cs;
250
251 if (!vfinddir) { feep(); return; }
252 if (mult < 0) { mult = -mult; virevrepeatfind(); return; }
253 while (mult--)
254 {
255 do
256 cs += vfinddir;
257 while (cs >= 0 && cs < ll && line[cs] != vfindchar && line[cs] != '\n');
258 if (cs < 0 || cs >= ll || line[cs] == '\n')
259 {
260 feep();
261 cs = ocs;
262 return;
263 }
264 }
265 cs += tailadd;
266 if (vfinddir == 1 && virangeflag) cs++;
267}
268
269void virevrepeatfind() /**/
270{
271 if (mult < 0) { mult = -mult; virepeatfind(); return; }
272 vfinddir = -vfinddir;
273 virepeatfind();
274 vfinddir = -vfinddir;
275}
276
277void vifirstnonblank() /**/
278{
279 cs = findbol();
280 while (cs != ll && iblank(line[cs]))
281 cs++;
282}
283
284void visetmark() /**/
285{
286int ch;
287
288 ch = getkey(1);
289 if (ch < 'a' || ch > 'z') {
290 feep();
291 return;
292 }
293 ch -= 'a';
294 vimarkcs[ch] = cs;
295 vimarkline[ch] = histline;
296}
297
298void vigotomark() /**/
299{
300int ch;
301
302 ch = getkey(1);
303 if (ch == c) ch = 26;
304 else {
305 if (ch < 'a' || ch > 'z') {
306 feep();
307 return;
308 }
309 ch -= 'a';
310 }
311 if (!vimarkline[ch]) {
312 feep();
313 return;
314 }
315 if (curhist != vimarkline[ch]) {
316 mult = vimarkline[ch];
317 vifetchhistory();
318 if (curhist != vimarkline[ch]) return;
319 }
320 cs = vimarkcs[ch];
321 if (cs > ll) ch = ll;
322}
323
324void vigotomarkline() /**/
325{
326 vigotomark();
327 cs = findbol();
328}