vi.c revision 1.9 1 1.9 christos /* $NetBSD: vi.c,v 1.9 2002/03/18 16:01:01 christos Exp $ */
2 1.2 lukem
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Christos Zoulas of Cornell University.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.9 christos #include "config.h"
40 1.1 cgd #if !defined(lint) && !defined(SCCSID)
41 1.2 lukem #if 0
42 1.1 cgd static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
43 1.2 lukem #else
44 1.9 christos __RCSID("$NetBSD: vi.c,v 1.9 2002/03/18 16:01:01 christos Exp $");
45 1.2 lukem #endif
46 1.1 cgd #endif /* not lint && not SCCSID */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * vi.c: Vi mode commands.
50 1.1 cgd */
51 1.1 cgd #include "el.h"
52 1.1 cgd
53 1.8 lukem private el_action_t cv_action(EditLine *, int);
54 1.8 lukem private el_action_t cv_paste(EditLine *, int);
55 1.1 cgd
56 1.1 cgd /* cv_action():
57 1.1 cgd * Handle vi actions.
58 1.1 cgd */
59 1.1 cgd private el_action_t
60 1.8 lukem cv_action(EditLine *el, int c)
61 1.1 cgd {
62 1.8 lukem char *cp, *kp;
63 1.1 cgd
64 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
65 1.8 lukem el->el_chared.c_vcmd.action = NOP;
66 1.8 lukem el->el_chared.c_vcmd.pos = 0;
67 1.8 lukem
68 1.8 lukem el->el_chared.c_undo.isize = 0;
69 1.8 lukem el->el_chared.c_undo.dsize = 0;
70 1.8 lukem kp = el->el_chared.c_undo.buf;
71 1.8 lukem for (cp = el->el_line.buffer; cp < el->el_line.lastchar; cp++) {
72 1.8 lukem *kp++ = *cp;
73 1.8 lukem el->el_chared.c_undo.dsize++;
74 1.8 lukem }
75 1.8 lukem
76 1.8 lukem el->el_chared.c_undo.action = INSERT;
77 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.buffer;
78 1.8 lukem el->el_line.lastchar = el->el_line.buffer;
79 1.8 lukem el->el_line.cursor = el->el_line.buffer;
80 1.8 lukem if (c & INSERT)
81 1.8 lukem el->el_map.current = el->el_map.key;
82 1.7 simonb
83 1.8 lukem return (CC_REFRESH);
84 1.1 cgd }
85 1.8 lukem el->el_chared.c_vcmd.pos = el->el_line.cursor;
86 1.8 lukem el->el_chared.c_vcmd.action = c;
87 1.8 lukem return (CC_ARGHACK);
88 1.1 cgd
89 1.1 cgd #ifdef notdef
90 1.8 lukem /*
91 1.8 lukem * I don't think that this is needed. But we keep it for now
92 1.8 lukem */
93 1.8 lukem else
94 1.8 lukem if (el_chared.c_vcmd.action == NOP) {
95 1.8 lukem el->el_chared.c_vcmd.pos = el->el_line.cursor;
96 1.8 lukem el->el_chared.c_vcmd.action = c;
97 1.8 lukem return (CC_ARGHACK);
98 1.8 lukem } else {
99 1.8 lukem el->el_chared.c_vcmd.action = 0;
100 1.8 lukem el->el_chared.c_vcmd.pos = 0;
101 1.8 lukem return (CC_ERROR);
102 1.8 lukem }
103 1.1 cgd #endif
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd
107 1.1 cgd /* cv_paste():
108 1.1 cgd * Paste previous deletion before or after the cursor
109 1.1 cgd */
110 1.3 christos private el_action_t
111 1.8 lukem cv_paste(EditLine *el, int c)
112 1.1 cgd {
113 1.8 lukem char *ptr;
114 1.8 lukem c_undo_t *un = &el->el_chared.c_undo;
115 1.8 lukem
116 1.1 cgd #ifdef DEBUG_PASTE
117 1.8 lukem (void) fprintf(el->el_errfile, "Paste: %x \"%s\" +%d -%d\n",
118 1.8 lukem un->action, un->buf, un->isize, un->dsize);
119 1.1 cgd #endif
120 1.8 lukem if (un->isize == 0)
121 1.8 lukem return (CC_ERROR);
122 1.1 cgd
123 1.8 lukem if (!c && el->el_line.cursor < el->el_line.lastchar)
124 1.8 lukem el->el_line.cursor++;
125 1.8 lukem ptr = el->el_line.cursor;
126 1.8 lukem
127 1.8 lukem c_insert(el, (int) un->isize);
128 1.8 lukem if (el->el_line.cursor + un->isize > el->el_line.lastchar)
129 1.8 lukem return (CC_ERROR);
130 1.8 lukem (void) memcpy(ptr, un->buf, un->isize);
131 1.8 lukem return (CC_REFRESH);
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd
135 1.7 simonb /* vi_paste_next():
136 1.1 cgd * Vi paste previous deletion to the right of the cursor
137 1.1 cgd * [p]
138 1.1 cgd */
139 1.1 cgd protected el_action_t
140 1.1 cgd /*ARGSUSED*/
141 1.8 lukem vi_paste_next(EditLine *el, int c)
142 1.1 cgd {
143 1.8 lukem
144 1.8 lukem return (cv_paste(el, 0));
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd
148 1.7 simonb /* vi_paste_prev():
149 1.1 cgd * Vi paste previous deletion to the left of the cursor
150 1.1 cgd * [P]
151 1.1 cgd */
152 1.1 cgd protected el_action_t
153 1.1 cgd /*ARGSUSED*/
154 1.8 lukem vi_paste_prev(EditLine *el, int c)
155 1.1 cgd {
156 1.8 lukem
157 1.8 lukem return (cv_paste(el, 1));
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd
161 1.7 simonb /* vi_prev_space_word():
162 1.1 cgd * Vi move to the previous space delimited word
163 1.1 cgd * [B]
164 1.1 cgd */
165 1.1 cgd protected el_action_t
166 1.1 cgd /*ARGSUSED*/
167 1.8 lukem vi_prev_space_word(EditLine *el, int c)
168 1.8 lukem {
169 1.8 lukem
170 1.8 lukem if (el->el_line.cursor == el->el_line.buffer)
171 1.8 lukem return (CC_ERROR);
172 1.1 cgd
173 1.8 lukem el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
174 1.8 lukem el->el_line.buffer,
175 1.8 lukem el->el_state.argument,
176 1.8 lukem cv__isword);
177 1.8 lukem
178 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
179 1.8 lukem cv_delfini(el);
180 1.8 lukem return (CC_REFRESH);
181 1.8 lukem }
182 1.8 lukem return (CC_CURSOR);
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd
186 1.7 simonb /* vi_prev_word():
187 1.1 cgd * Vi move to the previous word
188 1.1 cgd * [B]
189 1.1 cgd */
190 1.1 cgd protected el_action_t
191 1.1 cgd /*ARGSUSED*/
192 1.8 lukem vi_prev_word(EditLine *el, int c)
193 1.8 lukem {
194 1.1 cgd
195 1.8 lukem if (el->el_line.cursor == el->el_line.buffer)
196 1.8 lukem return (CC_ERROR);
197 1.8 lukem
198 1.8 lukem el->el_line.cursor = cv_prev_word(el, el->el_line.cursor,
199 1.8 lukem el->el_line.buffer,
200 1.8 lukem el->el_state.argument,
201 1.8 lukem ce__isword);
202 1.8 lukem
203 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
204 1.8 lukem cv_delfini(el);
205 1.8 lukem return (CC_REFRESH);
206 1.8 lukem }
207 1.8 lukem return (CC_CURSOR);
208 1.1 cgd }
209 1.1 cgd
210 1.1 cgd
211 1.7 simonb /* vi_next_space_word():
212 1.1 cgd * Vi move to the next space delimited word
213 1.1 cgd * [W]
214 1.1 cgd */
215 1.1 cgd protected el_action_t
216 1.1 cgd /*ARGSUSED*/
217 1.8 lukem vi_next_space_word(EditLine *el, int c)
218 1.8 lukem {
219 1.1 cgd
220 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
221 1.8 lukem return (CC_ERROR);
222 1.1 cgd
223 1.8 lukem el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
224 1.8 lukem el->el_line.lastchar,
225 1.8 lukem el->el_state.argument,
226 1.8 lukem cv__isword);
227 1.8 lukem
228 1.8 lukem if (el->el_map.type == MAP_VI)
229 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
230 1.8 lukem cv_delfini(el);
231 1.8 lukem return (CC_REFRESH);
232 1.8 lukem }
233 1.8 lukem return (CC_CURSOR);
234 1.1 cgd }
235 1.1 cgd
236 1.8 lukem
237 1.7 simonb /* vi_next_word():
238 1.1 cgd * Vi move to the next word
239 1.1 cgd * [w]
240 1.1 cgd */
241 1.1 cgd protected el_action_t
242 1.1 cgd /*ARGSUSED*/
243 1.8 lukem vi_next_word(EditLine *el, int c)
244 1.8 lukem {
245 1.1 cgd
246 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
247 1.8 lukem return (CC_ERROR);
248 1.1 cgd
249 1.8 lukem el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
250 1.8 lukem el->el_line.lastchar,
251 1.8 lukem el->el_state.argument,
252 1.8 lukem ce__isword);
253 1.8 lukem
254 1.8 lukem if (el->el_map.type == MAP_VI)
255 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
256 1.8 lukem cv_delfini(el);
257 1.8 lukem return (CC_REFRESH);
258 1.8 lukem }
259 1.8 lukem return (CC_CURSOR);
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd
263 1.7 simonb /* vi_change_case():
264 1.1 cgd * Vi change case of character under the cursor and advance one character
265 1.1 cgd * [~]
266 1.1 cgd */
267 1.1 cgd protected el_action_t
268 1.8 lukem vi_change_case(EditLine *el, int c)
269 1.8 lukem {
270 1.8 lukem
271 1.8 lukem if (el->el_line.cursor < el->el_line.lastchar) {
272 1.8 lukem c = *el->el_line.cursor;
273 1.8 lukem if (isupper(c))
274 1.8 lukem *el->el_line.cursor++ = tolower(c);
275 1.8 lukem else if (islower(c))
276 1.8 lukem *el->el_line.cursor++ = toupper(c);
277 1.8 lukem else
278 1.8 lukem el->el_line.cursor++;
279 1.8 lukem re_fastaddc(el);
280 1.8 lukem return (CC_NORM);
281 1.8 lukem }
282 1.8 lukem return (CC_ERROR);
283 1.1 cgd }
284 1.1 cgd
285 1.1 cgd
286 1.7 simonb /* vi_change_meta():
287 1.1 cgd * Vi change prefix command
288 1.1 cgd * [c]
289 1.1 cgd */
290 1.1 cgd protected el_action_t
291 1.1 cgd /*ARGSUSED*/
292 1.8 lukem vi_change_meta(EditLine *el, int c)
293 1.8 lukem {
294 1.8 lukem
295 1.8 lukem /*
296 1.8 lukem * Delete with insert == change: first we delete and then we leave in
297 1.8 lukem * insert mode.
298 1.8 lukem */
299 1.8 lukem return (cv_action(el, DELETE | INSERT));
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd
303 1.7 simonb /* vi_insert_at_bol():
304 1.1 cgd * Vi enter insert mode at the beginning of line
305 1.1 cgd * [I]
306 1.1 cgd */
307 1.1 cgd protected el_action_t
308 1.1 cgd /*ARGSUSED*/
309 1.8 lukem vi_insert_at_bol(EditLine *el, int c)
310 1.1 cgd {
311 1.1 cgd
312 1.8 lukem el->el_line.cursor = el->el_line.buffer;
313 1.8 lukem el->el_chared.c_vcmd.ins = el->el_line.cursor;
314 1.1 cgd
315 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
316 1.8 lukem el->el_chared.c_undo.action = DELETE;
317 1.8 lukem
318 1.8 lukem el->el_map.current = el->el_map.key;
319 1.8 lukem return (CC_CURSOR);
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd
323 1.7 simonb /* vi_replace_char():
324 1.1 cgd * Vi replace character under the cursor with the next character typed
325 1.1 cgd * [r]
326 1.1 cgd */
327 1.1 cgd protected el_action_t
328 1.1 cgd /*ARGSUSED*/
329 1.8 lukem vi_replace_char(EditLine *el, int c)
330 1.8 lukem {
331 1.8 lukem
332 1.8 lukem el->el_map.current = el->el_map.key;
333 1.8 lukem el->el_state.inputmode = MODE_REPLACE_1;
334 1.8 lukem el->el_chared.c_undo.action = CHANGE;
335 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
336 1.8 lukem el->el_chared.c_undo.isize = 0;
337 1.8 lukem el->el_chared.c_undo.dsize = 0;
338 1.8 lukem return (CC_NORM);
339 1.1 cgd }
340 1.1 cgd
341 1.1 cgd
342 1.7 simonb /* vi_replace_mode():
343 1.1 cgd * Vi enter replace mode
344 1.1 cgd * [R]
345 1.1 cgd */
346 1.1 cgd protected el_action_t
347 1.1 cgd /*ARGSUSED*/
348 1.8 lukem vi_replace_mode(EditLine *el, int c)
349 1.8 lukem {
350 1.8 lukem
351 1.8 lukem el->el_map.current = el->el_map.key;
352 1.8 lukem el->el_state.inputmode = MODE_REPLACE;
353 1.8 lukem el->el_chared.c_undo.action = CHANGE;
354 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
355 1.8 lukem el->el_chared.c_undo.isize = 0;
356 1.8 lukem el->el_chared.c_undo.dsize = 0;
357 1.8 lukem return (CC_NORM);
358 1.1 cgd }
359 1.1 cgd
360 1.1 cgd
361 1.7 simonb /* vi_substitute_char():
362 1.1 cgd * Vi replace character under the cursor and enter insert mode
363 1.1 cgd * [r]
364 1.1 cgd */
365 1.1 cgd protected el_action_t
366 1.1 cgd /*ARGSUSED*/
367 1.8 lukem vi_substitute_char(EditLine *el, int c)
368 1.8 lukem {
369 1.8 lukem
370 1.8 lukem c_delafter(el, el->el_state.argument);
371 1.8 lukem el->el_map.current = el->el_map.key;
372 1.8 lukem return (CC_REFRESH);
373 1.1 cgd }
374 1.1 cgd
375 1.1 cgd
376 1.7 simonb /* vi_substitute_line():
377 1.1 cgd * Vi substitute entire line
378 1.1 cgd * [S]
379 1.1 cgd */
380 1.1 cgd protected el_action_t
381 1.1 cgd /*ARGSUSED*/
382 1.8 lukem vi_substitute_line(EditLine *el, int c)
383 1.8 lukem {
384 1.8 lukem
385 1.8 lukem (void) em_kill_line(el, 0);
386 1.8 lukem el->el_map.current = el->el_map.key;
387 1.8 lukem return (CC_REFRESH);
388 1.1 cgd }
389 1.1 cgd
390 1.1 cgd
391 1.7 simonb /* vi_change_to_eol():
392 1.1 cgd * Vi change to end of line
393 1.1 cgd * [C]
394 1.1 cgd */
395 1.1 cgd protected el_action_t
396 1.1 cgd /*ARGSUSED*/
397 1.8 lukem vi_change_to_eol(EditLine *el, int c)
398 1.8 lukem {
399 1.8 lukem
400 1.8 lukem (void) ed_kill_line(el, 0);
401 1.8 lukem el->el_map.current = el->el_map.key;
402 1.8 lukem return (CC_REFRESH);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd
406 1.1 cgd /* vi_insert():
407 1.1 cgd * Vi enter insert mode
408 1.1 cgd * [i]
409 1.1 cgd */
410 1.1 cgd protected el_action_t
411 1.1 cgd /*ARGSUSED*/
412 1.8 lukem vi_insert(EditLine *el, int c)
413 1.1 cgd {
414 1.1 cgd
415 1.8 lukem el->el_map.current = el->el_map.key;
416 1.1 cgd
417 1.8 lukem el->el_chared.c_vcmd.ins = el->el_line.cursor;
418 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
419 1.8 lukem el->el_chared.c_undo.action = DELETE;
420 1.8 lukem
421 1.8 lukem return (CC_NORM);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd
425 1.1 cgd /* vi_add():
426 1.7 simonb * Vi enter insert mode after the cursor
427 1.1 cgd * [a]
428 1.1 cgd */
429 1.1 cgd protected el_action_t
430 1.1 cgd /*ARGSUSED*/
431 1.8 lukem vi_add(EditLine *el, int c)
432 1.8 lukem {
433 1.8 lukem int ret;
434 1.8 lukem
435 1.8 lukem el->el_map.current = el->el_map.key;
436 1.8 lukem if (el->el_line.cursor < el->el_line.lastchar) {
437 1.8 lukem el->el_line.cursor++;
438 1.8 lukem if (el->el_line.cursor > el->el_line.lastchar)
439 1.8 lukem el->el_line.cursor = el->el_line.lastchar;
440 1.8 lukem ret = CC_CURSOR;
441 1.8 lukem } else
442 1.8 lukem ret = CC_NORM;
443 1.8 lukem
444 1.8 lukem el->el_chared.c_vcmd.ins = el->el_line.cursor;
445 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
446 1.8 lukem el->el_chared.c_undo.action = DELETE;
447 1.1 cgd
448 1.8 lukem return (ret);
449 1.1 cgd }
450 1.1 cgd
451 1.1 cgd
452 1.1 cgd /* vi_add_at_eol():
453 1.1 cgd * Vi enter insert mode at end of line
454 1.1 cgd * [A]
455 1.1 cgd */
456 1.1 cgd protected el_action_t
457 1.1 cgd /*ARGSUSED*/
458 1.8 lukem vi_add_at_eol(EditLine *el, int c)
459 1.8 lukem {
460 1.8 lukem
461 1.8 lukem el->el_map.current = el->el_map.key;
462 1.8 lukem el->el_line.cursor = el->el_line.lastchar;
463 1.8 lukem
464 1.8 lukem /* Mark where insertion begins */
465 1.8 lukem el->el_chared.c_vcmd.ins = el->el_line.lastchar;
466 1.8 lukem el->el_chared.c_undo.ptr = el->el_line.lastchar;
467 1.8 lukem el->el_chared.c_undo.action = DELETE;
468 1.8 lukem return (CC_CURSOR);
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd
472 1.1 cgd /* vi_delete_meta():
473 1.7 simonb * Vi delete prefix command
474 1.1 cgd * [d]
475 1.1 cgd */
476 1.1 cgd protected el_action_t
477 1.1 cgd /*ARGSUSED*/
478 1.8 lukem vi_delete_meta(EditLine *el, int c)
479 1.1 cgd {
480 1.8 lukem
481 1.8 lukem return (cv_action(el, DELETE));
482 1.1 cgd }
483 1.1 cgd
484 1.1 cgd
485 1.1 cgd /* vi_end_word():
486 1.7 simonb * Vi move to the end of the current space delimited word
487 1.7 simonb * [E]
488 1.1 cgd */
489 1.1 cgd protected el_action_t
490 1.1 cgd /*ARGSUSED*/
491 1.8 lukem vi_end_word(EditLine *el, int c)
492 1.8 lukem {
493 1.8 lukem
494 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
495 1.8 lukem return (CC_ERROR);
496 1.1 cgd
497 1.8 lukem el->el_line.cursor = cv__endword(el->el_line.cursor,
498 1.8 lukem el->el_line.lastchar, el->el_state.argument);
499 1.8 lukem
500 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
501 1.8 lukem el->el_line.cursor++;
502 1.8 lukem cv_delfini(el);
503 1.8 lukem return (CC_REFRESH);
504 1.8 lukem }
505 1.8 lukem return (CC_CURSOR);
506 1.1 cgd }
507 1.1 cgd
508 1.1 cgd
509 1.1 cgd /* vi_to_end_word():
510 1.1 cgd * Vi move to the end of the current word
511 1.1 cgd * [e]
512 1.1 cgd */
513 1.1 cgd protected el_action_t
514 1.1 cgd /*ARGSUSED*/
515 1.8 lukem vi_to_end_word(EditLine *el, int c)
516 1.8 lukem {
517 1.8 lukem
518 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
519 1.8 lukem return (CC_ERROR);
520 1.8 lukem
521 1.8 lukem el->el_line.cursor = cv__endword(el->el_line.cursor,
522 1.8 lukem el->el_line.lastchar, el->el_state.argument);
523 1.1 cgd
524 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
525 1.8 lukem el->el_line.cursor++;
526 1.8 lukem cv_delfini(el);
527 1.8 lukem return (CC_REFRESH);
528 1.8 lukem }
529 1.8 lukem return (CC_CURSOR);
530 1.1 cgd }
531 1.1 cgd
532 1.1 cgd
533 1.1 cgd /* vi_undo():
534 1.1 cgd * Vi undo last change
535 1.1 cgd * [u]
536 1.1 cgd */
537 1.1 cgd protected el_action_t
538 1.1 cgd /*ARGSUSED*/
539 1.8 lukem vi_undo(EditLine *el, int c)
540 1.8 lukem {
541 1.8 lukem char *cp, *kp;
542 1.8 lukem char temp;
543 1.8 lukem int i, size;
544 1.8 lukem c_undo_t *un = &el->el_chared.c_undo;
545 1.1 cgd
546 1.1 cgd #ifdef DEBUG_UNDO
547 1.8 lukem (void) fprintf(el->el_errfile, "Undo: %x \"%s\" +%d -%d\n",
548 1.8 lukem un->action, un->buf, un->isize, un->dsize);
549 1.1 cgd #endif
550 1.8 lukem switch (un->action) {
551 1.8 lukem case DELETE:
552 1.8 lukem if (un->dsize == 0)
553 1.8 lukem return (CC_NORM);
554 1.8 lukem
555 1.8 lukem (void) memcpy(un->buf, un->ptr, un->dsize);
556 1.8 lukem for (cp = un->ptr; cp <= el->el_line.lastchar; cp++)
557 1.8 lukem *cp = cp[un->dsize];
558 1.8 lukem
559 1.8 lukem el->el_line.lastchar -= un->dsize;
560 1.8 lukem el->el_line.cursor = un->ptr;
561 1.8 lukem
562 1.8 lukem un->action = INSERT;
563 1.8 lukem un->isize = un->dsize;
564 1.8 lukem un->dsize = 0;
565 1.8 lukem break;
566 1.8 lukem
567 1.8 lukem case DELETE | INSERT:
568 1.8 lukem size = un->isize - un->dsize;
569 1.8 lukem if (size > 0)
570 1.8 lukem i = un->dsize;
571 1.8 lukem else
572 1.8 lukem i = un->isize;
573 1.8 lukem cp = un->ptr;
574 1.8 lukem kp = un->buf;
575 1.8 lukem while (i-- > 0) {
576 1.8 lukem temp = *kp;
577 1.8 lukem *kp++ = *cp;
578 1.8 lukem *cp++ = temp;
579 1.8 lukem }
580 1.8 lukem if (size > 0) {
581 1.8 lukem el->el_line.cursor = cp;
582 1.8 lukem c_insert(el, size);
583 1.8 lukem while (size-- > 0 && cp < el->el_line.lastchar) {
584 1.8 lukem temp = *kp;
585 1.8 lukem *kp++ = *cp;
586 1.8 lukem *cp++ = temp;
587 1.8 lukem }
588 1.8 lukem } else if (size < 0) {
589 1.8 lukem size = -size;
590 1.8 lukem for (; cp <= el->el_line.lastchar; cp++) {
591 1.8 lukem *kp++ = *cp;
592 1.8 lukem *cp = cp[size];
593 1.8 lukem }
594 1.8 lukem el->el_line.lastchar -= size;
595 1.8 lukem }
596 1.8 lukem el->el_line.cursor = un->ptr;
597 1.8 lukem i = un->dsize;
598 1.8 lukem un->dsize = un->isize;
599 1.8 lukem un->isize = i;
600 1.8 lukem break;
601 1.8 lukem
602 1.8 lukem case INSERT:
603 1.8 lukem if (un->isize == 0)
604 1.8 lukem return (CC_NORM);
605 1.8 lukem
606 1.8 lukem el->el_line.cursor = un->ptr;
607 1.8 lukem c_insert(el, (int) un->isize);
608 1.8 lukem (void) memcpy(un->ptr, un->buf, un->isize);
609 1.8 lukem un->action = DELETE;
610 1.8 lukem un->dsize = un->isize;
611 1.8 lukem un->isize = 0;
612 1.8 lukem break;
613 1.8 lukem
614 1.8 lukem case CHANGE:
615 1.8 lukem if (un->isize == 0)
616 1.8 lukem return (CC_NORM);
617 1.8 lukem
618 1.8 lukem el->el_line.cursor = un->ptr;
619 1.8 lukem size = (int) (el->el_line.cursor - el->el_line.lastchar);
620 1.8 lukem if (size < un->isize)
621 1.8 lukem size = un->isize;
622 1.8 lukem cp = un->ptr;
623 1.8 lukem kp = un->buf;
624 1.8 lukem for (i = 0; i < size; i++) {
625 1.8 lukem temp = *kp;
626 1.8 lukem *kp++ = *cp;
627 1.8 lukem *cp++ = temp;
628 1.8 lukem }
629 1.8 lukem un->dsize = 0;
630 1.8 lukem break;
631 1.1 cgd
632 1.8 lukem default:
633 1.8 lukem return (CC_ERROR);
634 1.1 cgd }
635 1.1 cgd
636 1.8 lukem return (CC_REFRESH);
637 1.1 cgd }
638 1.1 cgd
639 1.1 cgd
640 1.1 cgd /* vi_command_mode():
641 1.1 cgd * Vi enter command mode (use alternative key bindings)
642 1.1 cgd * [<ESC>]
643 1.1 cgd */
644 1.1 cgd protected el_action_t
645 1.1 cgd /*ARGSUSED*/
646 1.8 lukem vi_command_mode(EditLine *el, int c)
647 1.8 lukem {
648 1.8 lukem int size;
649 1.8 lukem
650 1.8 lukem /* [Esc] cancels pending action */
651 1.8 lukem el->el_chared.c_vcmd.ins = 0;
652 1.8 lukem el->el_chared.c_vcmd.action = NOP;
653 1.8 lukem el->el_chared.c_vcmd.pos = 0;
654 1.8 lukem
655 1.8 lukem el->el_state.doingarg = 0;
656 1.8 lukem size = el->el_chared.c_undo.ptr - el->el_line.cursor;
657 1.8 lukem if (size < 0)
658 1.8 lukem size = -size;
659 1.8 lukem if (el->el_chared.c_undo.action == (INSERT | DELETE) ||
660 1.8 lukem el->el_chared.c_undo.action == DELETE)
661 1.8 lukem el->el_chared.c_undo.dsize = size;
662 1.8 lukem else
663 1.8 lukem el->el_chared.c_undo.isize = size;
664 1.1 cgd
665 1.8 lukem el->el_state.inputmode = MODE_INSERT;
666 1.8 lukem el->el_map.current = el->el_map.alt;
667 1.1 cgd #ifdef VI_MOVE
668 1.8 lukem if (el->el_line.cursor > el->el_line.buffer)
669 1.8 lukem el->el_line.cursor--;
670 1.1 cgd #endif
671 1.8 lukem return (CC_CURSOR);
672 1.1 cgd }
673 1.1 cgd
674 1.8 lukem
675 1.1 cgd /* vi_zero():
676 1.7 simonb * Vi move to the beginning of line
677 1.1 cgd * [0]
678 1.1 cgd */
679 1.1 cgd protected el_action_t
680 1.8 lukem vi_zero(EditLine *el, int c)
681 1.8 lukem {
682 1.8 lukem
683 1.8 lukem if (el->el_state.doingarg) {
684 1.8 lukem if (el->el_state.argument > 1000000)
685 1.8 lukem return (CC_ERROR);
686 1.8 lukem el->el_state.argument =
687 1.8 lukem (el->el_state.argument * 10) + (c - '0');
688 1.8 lukem return (CC_ARGHACK);
689 1.8 lukem } else {
690 1.8 lukem el->el_line.cursor = el->el_line.buffer;
691 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
692 1.8 lukem cv_delfini(el);
693 1.8 lukem return (CC_REFRESH);
694 1.8 lukem }
695 1.8 lukem return (CC_CURSOR);
696 1.8 lukem }
697 1.1 cgd }
698 1.1 cgd
699 1.1 cgd
700 1.1 cgd /* vi_delete_prev_char():
701 1.7 simonb * Vi move to previous character (backspace)
702 1.1 cgd * [^H]
703 1.7 simonb */
704 1.1 cgd protected el_action_t
705 1.1 cgd /*ARGSUSED*/
706 1.8 lukem vi_delete_prev_char(EditLine *el, int c)
707 1.8 lukem {
708 1.1 cgd
709 1.8 lukem if (el->el_chared.c_vcmd.ins == 0)
710 1.8 lukem return (CC_ERROR);
711 1.1 cgd
712 1.8 lukem if (el->el_chared.c_vcmd.ins >
713 1.8 lukem el->el_line.cursor - el->el_state.argument)
714 1.8 lukem return (CC_ERROR);
715 1.8 lukem
716 1.8 lukem c_delbefore(el, el->el_state.argument);
717 1.8 lukem el->el_line.cursor -= el->el_state.argument;
718 1.8 lukem
719 1.8 lukem return (CC_REFRESH);
720 1.8 lukem }
721 1.1 cgd
722 1.1 cgd
723 1.1 cgd /* vi_list_or_eof():
724 1.1 cgd * Vi list choices for completion or indicate end of file if empty line
725 1.1 cgd * [^D]
726 1.1 cgd */
727 1.1 cgd protected el_action_t
728 1.1 cgd /*ARGSUSED*/
729 1.8 lukem vi_list_or_eof(EditLine *el, int c)
730 1.1 cgd {
731 1.8 lukem
732 1.1 cgd #ifdef notyet
733 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar &&
734 1.8 lukem el->el_line.cursor == el->el_line.buffer) {
735 1.1 cgd #endif
736 1.8 lukem term_overwrite(el, STReof, 4); /* then do a EOF */
737 1.8 lukem term__flush();
738 1.8 lukem return (CC_EOF);
739 1.1 cgd #ifdef notyet
740 1.8 lukem } else {
741 1.8 lukem re_goto_bottom(el);
742 1.8 lukem *el->el_line.lastchar = '\0'; /* just in case */
743 1.8 lukem return (CC_LIST_CHOICES);
744 1.8 lukem }
745 1.1 cgd #endif
746 1.1 cgd }
747 1.1 cgd
748 1.1 cgd
749 1.1 cgd /* vi_kill_line_prev():
750 1.7 simonb * Vi cut from beginning of line to cursor
751 1.1 cgd * [^U]
752 1.1 cgd */
753 1.1 cgd protected el_action_t
754 1.1 cgd /*ARGSUSED*/
755 1.8 lukem vi_kill_line_prev(EditLine *el, int c)
756 1.8 lukem {
757 1.8 lukem char *kp, *cp;
758 1.8 lukem
759 1.8 lukem cp = el->el_line.buffer;
760 1.8 lukem kp = el->el_chared.c_kill.buf;
761 1.8 lukem while (cp < el->el_line.cursor)
762 1.8 lukem *kp++ = *cp++; /* copy it */
763 1.8 lukem el->el_chared.c_kill.last = kp;
764 1.8 lukem c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
765 1.8 lukem el->el_line.cursor = el->el_line.buffer; /* zap! */
766 1.8 lukem return (CC_REFRESH);
767 1.1 cgd }
768 1.1 cgd
769 1.1 cgd
770 1.1 cgd /* vi_search_prev():
771 1.1 cgd * Vi search history previous
772 1.1 cgd * [?]
773 1.1 cgd */
774 1.1 cgd protected el_action_t
775 1.1 cgd /*ARGSUSED*/
776 1.8 lukem vi_search_prev(EditLine *el, int c)
777 1.1 cgd {
778 1.8 lukem
779 1.8 lukem return (cv_search(el, ED_SEARCH_PREV_HISTORY));
780 1.1 cgd }
781 1.1 cgd
782 1.1 cgd
783 1.1 cgd /* vi_search_next():
784 1.1 cgd * Vi search history next
785 1.1 cgd * [/]
786 1.1 cgd */
787 1.1 cgd protected el_action_t
788 1.1 cgd /*ARGSUSED*/
789 1.8 lukem vi_search_next(EditLine *el, int c)
790 1.1 cgd {
791 1.8 lukem
792 1.8 lukem return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
793 1.1 cgd }
794 1.1 cgd
795 1.1 cgd
796 1.1 cgd /* vi_repeat_search_next():
797 1.1 cgd * Vi repeat current search in the same search direction
798 1.1 cgd * [n]
799 1.1 cgd */
800 1.1 cgd protected el_action_t
801 1.1 cgd /*ARGSUSED*/
802 1.8 lukem vi_repeat_search_next(EditLine *el, int c)
803 1.8 lukem {
804 1.8 lukem
805 1.8 lukem if (el->el_search.patlen == 0)
806 1.8 lukem return (CC_ERROR);
807 1.8 lukem else
808 1.8 lukem return (cv_repeat_srch(el, el->el_search.patdir));
809 1.1 cgd }
810 1.1 cgd
811 1.1 cgd
812 1.1 cgd /* vi_repeat_search_prev():
813 1.1 cgd * Vi repeat current search in the opposite search direction
814 1.1 cgd * [N]
815 1.1 cgd */
816 1.1 cgd /*ARGSUSED*/
817 1.1 cgd protected el_action_t
818 1.8 lukem vi_repeat_search_prev(EditLine *el, int c)
819 1.8 lukem {
820 1.8 lukem
821 1.8 lukem if (el->el_search.patlen == 0)
822 1.8 lukem return (CC_ERROR);
823 1.8 lukem else
824 1.8 lukem return (cv_repeat_srch(el,
825 1.8 lukem el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
826 1.8 lukem ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
827 1.1 cgd }
828 1.1 cgd
829 1.1 cgd
830 1.1 cgd /* vi_next_char():
831 1.1 cgd * Vi move to the character specified next
832 1.1 cgd * [f]
833 1.1 cgd */
834 1.1 cgd protected el_action_t
835 1.1 cgd /*ARGSUSED*/
836 1.8 lukem vi_next_char(EditLine *el, int c)
837 1.1 cgd {
838 1.8 lukem char ch;
839 1.1 cgd
840 1.8 lukem if (el_getc(el, &ch) != 1)
841 1.8 lukem return (ed_end_of_file(el, 0));
842 1.1 cgd
843 1.8 lukem el->el_search.chadir = CHAR_FWD;
844 1.8 lukem el->el_search.chacha = ch;
845 1.1 cgd
846 1.8 lukem return (cv_csearch_fwd(el, ch, el->el_state.argument, 0));
847 1.1 cgd
848 1.1 cgd }
849 1.1 cgd
850 1.1 cgd
851 1.1 cgd /* vi_prev_char():
852 1.1 cgd * Vi move to the character specified previous
853 1.1 cgd * [F]
854 1.1 cgd */
855 1.1 cgd protected el_action_t
856 1.1 cgd /*ARGSUSED*/
857 1.8 lukem vi_prev_char(EditLine *el, int c)
858 1.1 cgd {
859 1.8 lukem char ch;
860 1.1 cgd
861 1.8 lukem if (el_getc(el, &ch) != 1)
862 1.8 lukem return (ed_end_of_file(el, 0));
863 1.1 cgd
864 1.8 lukem el->el_search.chadir = CHAR_BACK;
865 1.8 lukem el->el_search.chacha = ch;
866 1.1 cgd
867 1.8 lukem return (cv_csearch_back(el, ch, el->el_state.argument, 0));
868 1.1 cgd }
869 1.1 cgd
870 1.1 cgd
871 1.1 cgd /* vi_to_next_char():
872 1.1 cgd * Vi move up to the character specified next
873 1.1 cgd * [t]
874 1.1 cgd */
875 1.1 cgd protected el_action_t
876 1.1 cgd /*ARGSUSED*/
877 1.8 lukem vi_to_next_char(EditLine *el, int c)
878 1.1 cgd {
879 1.8 lukem char ch;
880 1.1 cgd
881 1.8 lukem if (el_getc(el, &ch) != 1)
882 1.8 lukem return (ed_end_of_file(el, 0));
883 1.1 cgd
884 1.8 lukem return (cv_csearch_fwd(el, ch, el->el_state.argument, 1));
885 1.1 cgd
886 1.1 cgd }
887 1.1 cgd
888 1.1 cgd
889 1.1 cgd /* vi_to_prev_char():
890 1.1 cgd * Vi move up to the character specified previous
891 1.1 cgd * [T]
892 1.1 cgd */
893 1.1 cgd protected el_action_t
894 1.1 cgd /*ARGSUSED*/
895 1.8 lukem vi_to_prev_char(EditLine *el, int c)
896 1.8 lukem {
897 1.8 lukem char ch;
898 1.1 cgd
899 1.8 lukem if (el_getc(el, &ch) != 1)
900 1.8 lukem return (ed_end_of_file(el, 0));
901 1.8 lukem
902 1.8 lukem return (cv_csearch_back(el, ch, el->el_state.argument, 1));
903 1.1 cgd }
904 1.1 cgd
905 1.1 cgd
906 1.1 cgd /* vi_repeat_next_char():
907 1.1 cgd * Vi repeat current character search in the same search direction
908 1.1 cgd * [;]
909 1.1 cgd */
910 1.1 cgd protected el_action_t
911 1.1 cgd /*ARGSUSED*/
912 1.8 lukem vi_repeat_next_char(EditLine *el, int c)
913 1.8 lukem {
914 1.8 lukem
915 1.8 lukem if (el->el_search.chacha == 0)
916 1.8 lukem return (CC_ERROR);
917 1.8 lukem
918 1.8 lukem return (el->el_search.chadir == CHAR_FWD
919 1.8 lukem ? cv_csearch_fwd(el, el->el_search.chacha,
920 1.8 lukem el->el_state.argument, 0)
921 1.8 lukem : cv_csearch_back(el, el->el_search.chacha,
922 1.8 lukem el->el_state.argument, 0));
923 1.1 cgd }
924 1.1 cgd
925 1.1 cgd
926 1.1 cgd /* vi_repeat_prev_char():
927 1.1 cgd * Vi repeat current character search in the opposite search direction
928 1.1 cgd * [,]
929 1.1 cgd */
930 1.1 cgd protected el_action_t
931 1.1 cgd /*ARGSUSED*/
932 1.8 lukem vi_repeat_prev_char(EditLine *el, int c)
933 1.8 lukem {
934 1.8 lukem
935 1.8 lukem if (el->el_search.chacha == 0)
936 1.8 lukem return (CC_ERROR);
937 1.8 lukem
938 1.8 lukem return el->el_search.chadir == CHAR_BACK ?
939 1.8 lukem cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
940 1.8 lukem cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
941 1.1 cgd }
942