vi.c revision 1.10 1 1.10 christos /* $NetBSD: vi.c,v 1.10 2002/10/27 21:41:50 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.10 christos __RCSID("$NetBSD: vi.c,v 1.10 2002/10/27 21:41:50 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.1 cgd
63 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
64 1.10 christos /* 'dd' and (possibly) friends */
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.10 christos cv_undo(el, el->el_line.lastchar - el->el_line.buffer,
69 1.10 christos el->el_line.buffer );
70 1.8 lukem el->el_line.lastchar = el->el_line.buffer;
71 1.8 lukem el->el_line.cursor = el->el_line.buffer;
72 1.8 lukem if (c & INSERT)
73 1.8 lukem el->el_map.current = el->el_map.key;
74 1.7 simonb
75 1.8 lukem return (CC_REFRESH);
76 1.1 cgd }
77 1.8 lukem el->el_chared.c_vcmd.pos = el->el_line.cursor;
78 1.8 lukem el->el_chared.c_vcmd.action = c;
79 1.8 lukem return (CC_ARGHACK);
80 1.1 cgd
81 1.1 cgd #ifdef notdef
82 1.8 lukem /*
83 1.8 lukem * I don't think that this is needed. But we keep it for now
84 1.8 lukem */
85 1.8 lukem else
86 1.8 lukem if (el_chared.c_vcmd.action == NOP) {
87 1.8 lukem el->el_chared.c_vcmd.pos = el->el_line.cursor;
88 1.8 lukem el->el_chared.c_vcmd.action = c;
89 1.8 lukem return (CC_ARGHACK);
90 1.8 lukem } else {
91 1.8 lukem el->el_chared.c_vcmd.action = 0;
92 1.8 lukem el->el_chared.c_vcmd.pos = 0;
93 1.8 lukem return (CC_ERROR);
94 1.8 lukem }
95 1.1 cgd #endif
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd /* cv_paste():
99 1.1 cgd * Paste previous deletion before or after the cursor
100 1.1 cgd */
101 1.3 christos private el_action_t
102 1.8 lukem cv_paste(EditLine *el, int c)
103 1.1 cgd {
104 1.8 lukem char *ptr;
105 1.8 lukem c_undo_t *un = &el->el_chared.c_undo;
106 1.8 lukem
107 1.1 cgd #ifdef DEBUG_PASTE
108 1.10 christos (void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n",
109 1.10 christos (int)un->paste_len, un->paste ? un->paste : "<null>" );
110 1.1 cgd #endif
111 1.10 christos if (!un->paste || !un->paste_len)
112 1.8 lukem return (CC_ERROR);
113 1.1 cgd
114 1.10 christos cv_undo(el, 0, 0);
115 1.10 christos
116 1.8 lukem if (!c && el->el_line.cursor < el->el_line.lastchar)
117 1.8 lukem el->el_line.cursor++;
118 1.8 lukem ptr = el->el_line.cursor;
119 1.8 lukem
120 1.10 christos c_insert(el, (int) un->paste_len);
121 1.10 christos if (el->el_line.cursor + un->paste_len > el->el_line.lastchar)
122 1.8 lukem return (CC_ERROR);
123 1.10 christos (void) memcpy(ptr, un->paste, un->paste_len);
124 1.8 lukem return (CC_REFRESH);
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd
128 1.7 simonb /* vi_paste_next():
129 1.1 cgd * Vi paste previous deletion to the right of the cursor
130 1.1 cgd * [p]
131 1.1 cgd */
132 1.1 cgd protected el_action_t
133 1.1 cgd /*ARGSUSED*/
134 1.8 lukem vi_paste_next(EditLine *el, int c)
135 1.1 cgd {
136 1.8 lukem
137 1.8 lukem return (cv_paste(el, 0));
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd
141 1.7 simonb /* vi_paste_prev():
142 1.1 cgd * Vi paste previous deletion to the left of the cursor
143 1.1 cgd * [P]
144 1.1 cgd */
145 1.1 cgd protected el_action_t
146 1.1 cgd /*ARGSUSED*/
147 1.8 lukem vi_paste_prev(EditLine *el, int c)
148 1.1 cgd {
149 1.8 lukem
150 1.8 lukem return (cv_paste(el, 1));
151 1.1 cgd }
152 1.1 cgd
153 1.1 cgd
154 1.10 christos /* vi_prev_big_word():
155 1.1 cgd * Vi move to the previous space delimited word
156 1.1 cgd * [B]
157 1.1 cgd */
158 1.1 cgd protected el_action_t
159 1.1 cgd /*ARGSUSED*/
160 1.10 christos vi_prev_big_word(EditLine *el, int c)
161 1.8 lukem {
162 1.8 lukem
163 1.8 lukem if (el->el_line.cursor == el->el_line.buffer)
164 1.8 lukem return (CC_ERROR);
165 1.1 cgd
166 1.10 christos el->el_line.cursor = cv_prev_word(el->el_line.cursor,
167 1.8 lukem el->el_line.buffer,
168 1.8 lukem el->el_state.argument,
169 1.10 christos cv__isWord);
170 1.8 lukem
171 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
172 1.8 lukem cv_delfini(el);
173 1.8 lukem return (CC_REFRESH);
174 1.8 lukem }
175 1.8 lukem return (CC_CURSOR);
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd
179 1.7 simonb /* vi_prev_word():
180 1.1 cgd * Vi move to the previous word
181 1.10 christos * [b]
182 1.1 cgd */
183 1.1 cgd protected el_action_t
184 1.1 cgd /*ARGSUSED*/
185 1.8 lukem vi_prev_word(EditLine *el, int c)
186 1.8 lukem {
187 1.1 cgd
188 1.8 lukem if (el->el_line.cursor == el->el_line.buffer)
189 1.8 lukem return (CC_ERROR);
190 1.8 lukem
191 1.10 christos el->el_line.cursor = cv_prev_word(el->el_line.cursor,
192 1.8 lukem el->el_line.buffer,
193 1.8 lukem el->el_state.argument,
194 1.10 christos cv__isword);
195 1.8 lukem
196 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
197 1.8 lukem cv_delfini(el);
198 1.8 lukem return (CC_REFRESH);
199 1.8 lukem }
200 1.8 lukem return (CC_CURSOR);
201 1.1 cgd }
202 1.1 cgd
203 1.1 cgd
204 1.10 christos /* vi_next_big_word():
205 1.1 cgd * Vi move to the next space delimited word
206 1.1 cgd * [W]
207 1.1 cgd */
208 1.1 cgd protected el_action_t
209 1.1 cgd /*ARGSUSED*/
210 1.10 christos vi_next_big_word(EditLine *el, int c)
211 1.8 lukem {
212 1.1 cgd
213 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
214 1.8 lukem return (CC_ERROR);
215 1.1 cgd
216 1.8 lukem el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
217 1.8 lukem el->el_line.lastchar,
218 1.8 lukem el->el_state.argument,
219 1.10 christos cv__isWord);
220 1.8 lukem
221 1.8 lukem if (el->el_map.type == MAP_VI)
222 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
223 1.8 lukem cv_delfini(el);
224 1.8 lukem return (CC_REFRESH);
225 1.8 lukem }
226 1.8 lukem return (CC_CURSOR);
227 1.1 cgd }
228 1.1 cgd
229 1.8 lukem
230 1.7 simonb /* vi_next_word():
231 1.1 cgd * Vi move to the next word
232 1.1 cgd * [w]
233 1.1 cgd */
234 1.1 cgd protected el_action_t
235 1.1 cgd /*ARGSUSED*/
236 1.8 lukem vi_next_word(EditLine *el, int c)
237 1.8 lukem {
238 1.1 cgd
239 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
240 1.8 lukem return (CC_ERROR);
241 1.1 cgd
242 1.8 lukem el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
243 1.8 lukem el->el_line.lastchar,
244 1.8 lukem el->el_state.argument,
245 1.10 christos cv__isword);
246 1.8 lukem
247 1.8 lukem if (el->el_map.type == MAP_VI)
248 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
249 1.8 lukem cv_delfini(el);
250 1.8 lukem return (CC_REFRESH);
251 1.8 lukem }
252 1.8 lukem return (CC_CURSOR);
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd
256 1.7 simonb /* vi_change_case():
257 1.1 cgd * Vi change case of character under the cursor and advance one character
258 1.1 cgd * [~]
259 1.1 cgd */
260 1.1 cgd protected el_action_t
261 1.8 lukem vi_change_case(EditLine *el, int c)
262 1.8 lukem {
263 1.10 christos int i;
264 1.10 christos
265 1.10 christos if (el->el_line.cursor >= el->el_line.lastchar)
266 1.10 christos return (CC_ERROR);
267 1.10 christos cv_undo(el, 0, 0);
268 1.10 christos for (i = 0; i < el->el_state.argument; i++) {
269 1.10 christos if (el->el_line.cursor >= el->el_line.lastchar)
270 1.10 christos break;
271 1.8 lukem
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 }
281 1.10 christos return (CC_NORM);
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd
285 1.7 simonb /* vi_change_meta():
286 1.1 cgd * Vi change prefix command
287 1.1 cgd * [c]
288 1.1 cgd */
289 1.1 cgd protected el_action_t
290 1.1 cgd /*ARGSUSED*/
291 1.8 lukem vi_change_meta(EditLine *el, int c)
292 1.8 lukem {
293 1.8 lukem
294 1.8 lukem /*
295 1.8 lukem * Delete with insert == change: first we delete and then we leave in
296 1.8 lukem * insert mode.
297 1.8 lukem */
298 1.8 lukem return (cv_action(el, DELETE | INSERT));
299 1.1 cgd }
300 1.1 cgd
301 1.1 cgd
302 1.7 simonb /* vi_insert_at_bol():
303 1.1 cgd * Vi enter insert mode at the beginning of line
304 1.1 cgd * [I]
305 1.1 cgd */
306 1.1 cgd protected el_action_t
307 1.1 cgd /*ARGSUSED*/
308 1.8 lukem vi_insert_at_bol(EditLine *el, int c)
309 1.1 cgd {
310 1.1 cgd
311 1.8 lukem el->el_line.cursor = el->el_line.buffer;
312 1.10 christos cv_undo(el, 0, 0);
313 1.8 lukem el->el_map.current = el->el_map.key;
314 1.8 lukem return (CC_CURSOR);
315 1.1 cgd }
316 1.1 cgd
317 1.1 cgd
318 1.7 simonb /* vi_replace_char():
319 1.1 cgd * Vi replace character under the cursor with the next character typed
320 1.1 cgd * [r]
321 1.1 cgd */
322 1.1 cgd protected el_action_t
323 1.1 cgd /*ARGSUSED*/
324 1.8 lukem vi_replace_char(EditLine *el, int c)
325 1.8 lukem {
326 1.8 lukem
327 1.10 christos if (el->el_state.argument > el->el_line.lastchar - el->el_line.cursor)
328 1.10 christos return CC_ERROR;
329 1.10 christos
330 1.8 lukem el->el_map.current = el->el_map.key;
331 1.8 lukem el->el_state.inputmode = MODE_REPLACE_1;
332 1.10 christos cv_undo(el, 0, 0);
333 1.10 christos return (CC_ARGHACK);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd
337 1.7 simonb /* vi_replace_mode():
338 1.1 cgd * Vi enter replace mode
339 1.1 cgd * [R]
340 1.1 cgd */
341 1.1 cgd protected el_action_t
342 1.1 cgd /*ARGSUSED*/
343 1.8 lukem vi_replace_mode(EditLine *el, int c)
344 1.8 lukem {
345 1.8 lukem
346 1.8 lukem el->el_map.current = el->el_map.key;
347 1.8 lukem el->el_state.inputmode = MODE_REPLACE;
348 1.10 christos cv_undo(el, 0, 0);
349 1.8 lukem return (CC_NORM);
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd
353 1.7 simonb /* vi_substitute_char():
354 1.1 cgd * Vi replace character under the cursor and enter insert mode
355 1.10 christos * [s]
356 1.1 cgd */
357 1.1 cgd protected el_action_t
358 1.1 cgd /*ARGSUSED*/
359 1.8 lukem vi_substitute_char(EditLine *el, int c)
360 1.8 lukem {
361 1.8 lukem
362 1.8 lukem c_delafter(el, el->el_state.argument);
363 1.8 lukem el->el_map.current = el->el_map.key;
364 1.8 lukem return (CC_REFRESH);
365 1.1 cgd }
366 1.1 cgd
367 1.1 cgd
368 1.7 simonb /* vi_substitute_line():
369 1.1 cgd * Vi substitute entire line
370 1.1 cgd * [S]
371 1.1 cgd */
372 1.1 cgd protected el_action_t
373 1.1 cgd /*ARGSUSED*/
374 1.8 lukem vi_substitute_line(EditLine *el, int c)
375 1.8 lukem {
376 1.8 lukem
377 1.10 christos cv_undo(el, el->el_line.lastchar - el->el_line.buffer,
378 1.10 christos el->el_line.buffer);
379 1.8 lukem (void) em_kill_line(el, 0);
380 1.8 lukem el->el_map.current = el->el_map.key;
381 1.8 lukem return (CC_REFRESH);
382 1.1 cgd }
383 1.1 cgd
384 1.1 cgd
385 1.7 simonb /* vi_change_to_eol():
386 1.1 cgd * Vi change to end of line
387 1.1 cgd * [C]
388 1.1 cgd */
389 1.1 cgd protected el_action_t
390 1.1 cgd /*ARGSUSED*/
391 1.8 lukem vi_change_to_eol(EditLine *el, int c)
392 1.8 lukem {
393 1.8 lukem
394 1.10 christos cv_undo(el, el->el_line.lastchar - el->el_line.cursor,
395 1.10 christos el->el_line.cursor);
396 1.8 lukem (void) ed_kill_line(el, 0);
397 1.8 lukem el->el_map.current = el->el_map.key;
398 1.8 lukem return (CC_REFRESH);
399 1.1 cgd }
400 1.1 cgd
401 1.1 cgd
402 1.1 cgd /* vi_insert():
403 1.1 cgd * Vi enter insert mode
404 1.1 cgd * [i]
405 1.1 cgd */
406 1.1 cgd protected el_action_t
407 1.1 cgd /*ARGSUSED*/
408 1.8 lukem vi_insert(EditLine *el, int c)
409 1.1 cgd {
410 1.1 cgd
411 1.8 lukem el->el_map.current = el->el_map.key;
412 1.10 christos cv_undo(el, 0, 0);
413 1.8 lukem return (CC_NORM);
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd
417 1.1 cgd /* vi_add():
418 1.7 simonb * Vi enter insert mode after the cursor
419 1.1 cgd * [a]
420 1.1 cgd */
421 1.1 cgd protected el_action_t
422 1.1 cgd /*ARGSUSED*/
423 1.8 lukem vi_add(EditLine *el, int c)
424 1.8 lukem {
425 1.8 lukem int ret;
426 1.8 lukem
427 1.8 lukem el->el_map.current = el->el_map.key;
428 1.8 lukem if (el->el_line.cursor < el->el_line.lastchar) {
429 1.8 lukem el->el_line.cursor++;
430 1.8 lukem if (el->el_line.cursor > el->el_line.lastchar)
431 1.8 lukem el->el_line.cursor = el->el_line.lastchar;
432 1.8 lukem ret = CC_CURSOR;
433 1.8 lukem } else
434 1.8 lukem ret = CC_NORM;
435 1.8 lukem
436 1.10 christos cv_undo(el, 0, 0);
437 1.1 cgd
438 1.8 lukem return (ret);
439 1.1 cgd }
440 1.1 cgd
441 1.1 cgd
442 1.1 cgd /* vi_add_at_eol():
443 1.1 cgd * Vi enter insert mode at end of line
444 1.1 cgd * [A]
445 1.1 cgd */
446 1.1 cgd protected el_action_t
447 1.1 cgd /*ARGSUSED*/
448 1.8 lukem vi_add_at_eol(EditLine *el, int c)
449 1.8 lukem {
450 1.8 lukem
451 1.8 lukem el->el_map.current = el->el_map.key;
452 1.8 lukem el->el_line.cursor = el->el_line.lastchar;
453 1.10 christos cv_undo(el, 0, 0);
454 1.8 lukem return (CC_CURSOR);
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd
458 1.1 cgd /* vi_delete_meta():
459 1.7 simonb * Vi delete prefix command
460 1.1 cgd * [d]
461 1.1 cgd */
462 1.1 cgd protected el_action_t
463 1.1 cgd /*ARGSUSED*/
464 1.8 lukem vi_delete_meta(EditLine *el, int c)
465 1.1 cgd {
466 1.8 lukem
467 1.8 lukem return (cv_action(el, DELETE));
468 1.1 cgd }
469 1.1 cgd
470 1.1 cgd
471 1.10 christos /* vi_end_big_word():
472 1.7 simonb * Vi move to the end of the current space delimited word
473 1.7 simonb * [E]
474 1.1 cgd */
475 1.1 cgd protected el_action_t
476 1.1 cgd /*ARGSUSED*/
477 1.10 christos vi_end_big_word(EditLine *el, int c)
478 1.8 lukem {
479 1.8 lukem
480 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
481 1.8 lukem return (CC_ERROR);
482 1.1 cgd
483 1.8 lukem el->el_line.cursor = cv__endword(el->el_line.cursor,
484 1.10 christos el->el_line.lastchar, el->el_state.argument, cv__isWord);
485 1.8 lukem
486 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
487 1.8 lukem el->el_line.cursor++;
488 1.8 lukem cv_delfini(el);
489 1.8 lukem return (CC_REFRESH);
490 1.8 lukem }
491 1.8 lukem return (CC_CURSOR);
492 1.1 cgd }
493 1.1 cgd
494 1.1 cgd
495 1.10 christos /* vi_end_word():
496 1.1 cgd * Vi move to the end of the current word
497 1.1 cgd * [e]
498 1.1 cgd */
499 1.1 cgd protected el_action_t
500 1.1 cgd /*ARGSUSED*/
501 1.10 christos vi_end_word(EditLine *el, int c)
502 1.8 lukem {
503 1.8 lukem
504 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar)
505 1.8 lukem return (CC_ERROR);
506 1.8 lukem
507 1.8 lukem el->el_line.cursor = cv__endword(el->el_line.cursor,
508 1.10 christos el->el_line.lastchar, el->el_state.argument, cv__isword);
509 1.1 cgd
510 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
511 1.8 lukem el->el_line.cursor++;
512 1.8 lukem cv_delfini(el);
513 1.8 lukem return (CC_REFRESH);
514 1.8 lukem }
515 1.8 lukem return (CC_CURSOR);
516 1.1 cgd }
517 1.1 cgd
518 1.1 cgd
519 1.1 cgd /* vi_undo():
520 1.1 cgd * Vi undo last change
521 1.1 cgd * [u]
522 1.1 cgd */
523 1.1 cgd protected el_action_t
524 1.1 cgd /*ARGSUSED*/
525 1.8 lukem vi_undo(EditLine *el, int c)
526 1.8 lukem {
527 1.10 christos c_undo_t un = el->el_chared.c_undo;
528 1.1 cgd
529 1.10 christos if (un.len == -1)
530 1.10 christos return CC_ERROR;
531 1.1 cgd
532 1.10 christos /* switch line buffer and undo buffer */
533 1.10 christos el->el_chared.c_undo.buf = el->el_line.buffer;
534 1.10 christos el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
535 1.10 christos el->el_chared.c_undo.cursor = el->el_line.cursor - el->el_line.buffer;
536 1.10 christos el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
537 1.10 christos el->el_line.buffer = un.buf;
538 1.10 christos el->el_line.cursor = un.buf + un.cursor;
539 1.10 christos el->el_line.lastchar = un.buf + un.len;
540 1.1 cgd
541 1.8 lukem return (CC_REFRESH);
542 1.1 cgd }
543 1.1 cgd
544 1.1 cgd
545 1.1 cgd /* vi_command_mode():
546 1.1 cgd * Vi enter command mode (use alternative key bindings)
547 1.1 cgd * [<ESC>]
548 1.1 cgd */
549 1.1 cgd protected el_action_t
550 1.1 cgd /*ARGSUSED*/
551 1.8 lukem vi_command_mode(EditLine *el, int c)
552 1.8 lukem {
553 1.8 lukem
554 1.8 lukem /* [Esc] cancels pending action */
555 1.8 lukem el->el_chared.c_vcmd.action = NOP;
556 1.8 lukem el->el_chared.c_vcmd.pos = 0;
557 1.8 lukem
558 1.8 lukem el->el_state.doingarg = 0;
559 1.1 cgd
560 1.8 lukem el->el_state.inputmode = MODE_INSERT;
561 1.8 lukem el->el_map.current = el->el_map.alt;
562 1.1 cgd #ifdef VI_MOVE
563 1.8 lukem if (el->el_line.cursor > el->el_line.buffer)
564 1.8 lukem el->el_line.cursor--;
565 1.1 cgd #endif
566 1.8 lukem return (CC_CURSOR);
567 1.1 cgd }
568 1.1 cgd
569 1.8 lukem
570 1.1 cgd /* vi_zero():
571 1.7 simonb * Vi move to the beginning of line
572 1.1 cgd * [0]
573 1.1 cgd */
574 1.1 cgd protected el_action_t
575 1.8 lukem vi_zero(EditLine *el, int c)
576 1.8 lukem {
577 1.8 lukem
578 1.8 lukem if (el->el_state.doingarg) {
579 1.8 lukem if (el->el_state.argument > 1000000)
580 1.8 lukem return (CC_ERROR);
581 1.8 lukem el->el_state.argument =
582 1.8 lukem (el->el_state.argument * 10) + (c - '0');
583 1.8 lukem return (CC_ARGHACK);
584 1.8 lukem } else {
585 1.8 lukem el->el_line.cursor = el->el_line.buffer;
586 1.8 lukem if (el->el_chared.c_vcmd.action & DELETE) {
587 1.8 lukem cv_delfini(el);
588 1.8 lukem return (CC_REFRESH);
589 1.8 lukem }
590 1.8 lukem return (CC_CURSOR);
591 1.8 lukem }
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd
595 1.1 cgd /* vi_delete_prev_char():
596 1.7 simonb * Vi move to previous character (backspace)
597 1.10 christos * [^H] in insert mode only
598 1.7 simonb */
599 1.1 cgd protected el_action_t
600 1.1 cgd /*ARGSUSED*/
601 1.8 lukem vi_delete_prev_char(EditLine *el, int c)
602 1.8 lukem {
603 1.10 christos char *cp;
604 1.1 cgd
605 1.10 christos cp = el->el_line.cursor;
606 1.10 christos if (cp <= el->el_line.buffer)
607 1.8 lukem return (CC_ERROR);
608 1.8 lukem
609 1.10 christos /* do the delete here so we dont mess up the undo and paste buffers */
610 1.10 christos el->el_line.cursor = --cp;
611 1.10 christos for (; cp < el->el_line.lastchar; cp++)
612 1.10 christos cp[0] = cp[1];
613 1.10 christos el->el_line.lastchar = cp - 1;
614 1.8 lukem
615 1.8 lukem return (CC_REFRESH);
616 1.8 lukem }
617 1.1 cgd
618 1.1 cgd
619 1.1 cgd /* vi_list_or_eof():
620 1.1 cgd * Vi list choices for completion or indicate end of file if empty line
621 1.1 cgd * [^D]
622 1.1 cgd */
623 1.1 cgd protected el_action_t
624 1.1 cgd /*ARGSUSED*/
625 1.8 lukem vi_list_or_eof(EditLine *el, int c)
626 1.1 cgd {
627 1.8 lukem
628 1.1 cgd #ifdef notyet
629 1.8 lukem if (el->el_line.cursor == el->el_line.lastchar &&
630 1.8 lukem el->el_line.cursor == el->el_line.buffer) {
631 1.1 cgd #endif
632 1.8 lukem term_overwrite(el, STReof, 4); /* then do a EOF */
633 1.8 lukem term__flush();
634 1.8 lukem return (CC_EOF);
635 1.1 cgd #ifdef notyet
636 1.8 lukem } else {
637 1.8 lukem re_goto_bottom(el);
638 1.8 lukem *el->el_line.lastchar = '\0'; /* just in case */
639 1.8 lukem return (CC_LIST_CHOICES);
640 1.8 lukem }
641 1.1 cgd #endif
642 1.1 cgd }
643 1.1 cgd
644 1.1 cgd
645 1.1 cgd /* vi_kill_line_prev():
646 1.7 simonb * Vi cut from beginning of line to cursor
647 1.1 cgd * [^U]
648 1.1 cgd */
649 1.1 cgd protected el_action_t
650 1.1 cgd /*ARGSUSED*/
651 1.8 lukem vi_kill_line_prev(EditLine *el, int c)
652 1.8 lukem {
653 1.8 lukem char *kp, *cp;
654 1.8 lukem
655 1.8 lukem cp = el->el_line.buffer;
656 1.8 lukem kp = el->el_chared.c_kill.buf;
657 1.8 lukem while (cp < el->el_line.cursor)
658 1.8 lukem *kp++ = *cp++; /* copy it */
659 1.8 lukem el->el_chared.c_kill.last = kp;
660 1.8 lukem c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
661 1.8 lukem el->el_line.cursor = el->el_line.buffer; /* zap! */
662 1.8 lukem return (CC_REFRESH);
663 1.1 cgd }
664 1.1 cgd
665 1.1 cgd
666 1.1 cgd /* vi_search_prev():
667 1.1 cgd * Vi search history previous
668 1.1 cgd * [?]
669 1.1 cgd */
670 1.1 cgd protected el_action_t
671 1.1 cgd /*ARGSUSED*/
672 1.8 lukem vi_search_prev(EditLine *el, int c)
673 1.1 cgd {
674 1.8 lukem
675 1.8 lukem return (cv_search(el, ED_SEARCH_PREV_HISTORY));
676 1.1 cgd }
677 1.1 cgd
678 1.1 cgd
679 1.1 cgd /* vi_search_next():
680 1.1 cgd * Vi search history next
681 1.1 cgd * [/]
682 1.1 cgd */
683 1.1 cgd protected el_action_t
684 1.1 cgd /*ARGSUSED*/
685 1.8 lukem vi_search_next(EditLine *el, int c)
686 1.1 cgd {
687 1.8 lukem
688 1.8 lukem return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
689 1.1 cgd }
690 1.1 cgd
691 1.1 cgd
692 1.1 cgd /* vi_repeat_search_next():
693 1.1 cgd * Vi repeat current search in the same search direction
694 1.1 cgd * [n]
695 1.1 cgd */
696 1.1 cgd protected el_action_t
697 1.1 cgd /*ARGSUSED*/
698 1.8 lukem vi_repeat_search_next(EditLine *el, int c)
699 1.8 lukem {
700 1.8 lukem
701 1.8 lukem if (el->el_search.patlen == 0)
702 1.8 lukem return (CC_ERROR);
703 1.8 lukem else
704 1.8 lukem return (cv_repeat_srch(el, el->el_search.patdir));
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd
708 1.1 cgd /* vi_repeat_search_prev():
709 1.1 cgd * Vi repeat current search in the opposite search direction
710 1.1 cgd * [N]
711 1.1 cgd */
712 1.1 cgd /*ARGSUSED*/
713 1.1 cgd protected el_action_t
714 1.8 lukem vi_repeat_search_prev(EditLine *el, int c)
715 1.8 lukem {
716 1.8 lukem
717 1.8 lukem if (el->el_search.patlen == 0)
718 1.8 lukem return (CC_ERROR);
719 1.8 lukem else
720 1.8 lukem return (cv_repeat_srch(el,
721 1.8 lukem el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
722 1.8 lukem ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
723 1.1 cgd }
724 1.1 cgd
725 1.1 cgd
726 1.1 cgd /* vi_next_char():
727 1.1 cgd * Vi move to the character specified next
728 1.1 cgd * [f]
729 1.1 cgd */
730 1.1 cgd protected el_action_t
731 1.1 cgd /*ARGSUSED*/
732 1.8 lukem vi_next_char(EditLine *el, int c)
733 1.1 cgd {
734 1.8 lukem char ch;
735 1.1 cgd
736 1.8 lukem if (el_getc(el, &ch) != 1)
737 1.8 lukem return (ed_end_of_file(el, 0));
738 1.1 cgd
739 1.8 lukem el->el_search.chadir = CHAR_FWD;
740 1.8 lukem el->el_search.chacha = ch;
741 1.1 cgd
742 1.8 lukem return (cv_csearch_fwd(el, ch, el->el_state.argument, 0));
743 1.1 cgd
744 1.1 cgd }
745 1.1 cgd
746 1.1 cgd
747 1.1 cgd /* vi_prev_char():
748 1.1 cgd * Vi move to the character specified previous
749 1.1 cgd * [F]
750 1.1 cgd */
751 1.1 cgd protected el_action_t
752 1.1 cgd /*ARGSUSED*/
753 1.8 lukem vi_prev_char(EditLine *el, int c)
754 1.1 cgd {
755 1.8 lukem char ch;
756 1.1 cgd
757 1.8 lukem if (el_getc(el, &ch) != 1)
758 1.8 lukem return (ed_end_of_file(el, 0));
759 1.1 cgd
760 1.8 lukem el->el_search.chadir = CHAR_BACK;
761 1.8 lukem el->el_search.chacha = ch;
762 1.1 cgd
763 1.8 lukem return (cv_csearch_back(el, ch, el->el_state.argument, 0));
764 1.1 cgd }
765 1.1 cgd
766 1.1 cgd
767 1.1 cgd /* vi_to_next_char():
768 1.1 cgd * Vi move up to the character specified next
769 1.1 cgd * [t]
770 1.1 cgd */
771 1.1 cgd protected el_action_t
772 1.1 cgd /*ARGSUSED*/
773 1.8 lukem vi_to_next_char(EditLine *el, int c)
774 1.1 cgd {
775 1.8 lukem char ch;
776 1.1 cgd
777 1.8 lukem if (el_getc(el, &ch) != 1)
778 1.8 lukem return (ed_end_of_file(el, 0));
779 1.1 cgd
780 1.8 lukem return (cv_csearch_fwd(el, ch, el->el_state.argument, 1));
781 1.1 cgd
782 1.1 cgd }
783 1.1 cgd
784 1.1 cgd
785 1.1 cgd /* vi_to_prev_char():
786 1.1 cgd * Vi move up to the character specified previous
787 1.1 cgd * [T]
788 1.1 cgd */
789 1.1 cgd protected el_action_t
790 1.1 cgd /*ARGSUSED*/
791 1.8 lukem vi_to_prev_char(EditLine *el, int c)
792 1.8 lukem {
793 1.8 lukem char ch;
794 1.1 cgd
795 1.8 lukem if (el_getc(el, &ch) != 1)
796 1.8 lukem return (ed_end_of_file(el, 0));
797 1.8 lukem
798 1.8 lukem return (cv_csearch_back(el, ch, el->el_state.argument, 1));
799 1.1 cgd }
800 1.1 cgd
801 1.1 cgd
802 1.1 cgd /* vi_repeat_next_char():
803 1.1 cgd * Vi repeat current character search in the same search direction
804 1.1 cgd * [;]
805 1.1 cgd */
806 1.1 cgd protected el_action_t
807 1.1 cgd /*ARGSUSED*/
808 1.8 lukem vi_repeat_next_char(EditLine *el, int c)
809 1.8 lukem {
810 1.8 lukem
811 1.8 lukem if (el->el_search.chacha == 0)
812 1.8 lukem return (CC_ERROR);
813 1.8 lukem
814 1.8 lukem return (el->el_search.chadir == CHAR_FWD
815 1.8 lukem ? cv_csearch_fwd(el, el->el_search.chacha,
816 1.8 lukem el->el_state.argument, 0)
817 1.8 lukem : cv_csearch_back(el, el->el_search.chacha,
818 1.8 lukem el->el_state.argument, 0));
819 1.1 cgd }
820 1.1 cgd
821 1.1 cgd
822 1.1 cgd /* vi_repeat_prev_char():
823 1.1 cgd * Vi repeat current character search in the opposite search direction
824 1.1 cgd * [,]
825 1.1 cgd */
826 1.1 cgd protected el_action_t
827 1.1 cgd /*ARGSUSED*/
828 1.8 lukem vi_repeat_prev_char(EditLine *el, int c)
829 1.8 lukem {
830 1.8 lukem
831 1.8 lukem if (el->el_search.chacha == 0)
832 1.8 lukem return (CC_ERROR);
833 1.8 lukem
834 1.8 lukem return el->el_search.chadir == CHAR_BACK ?
835 1.8 lukem cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
836 1.8 lukem cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
837 1.1 cgd }
838