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