common.c revision 1.29 1 1.29 christos /* $NetBSD: common.c,v 1.29 2012/03/24 20:08:43 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.16 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.11 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.2 lukem #if 0
38 1.1 cgd static char sccsid[] = "@(#)common.c 8.1 (Berkeley) 6/4/93";
39 1.2 lukem #else
40 1.29 christos __RCSID("$NetBSD: common.c,v 1.29 2012/03/24 20:08:43 christos Exp $");
41 1.2 lukem #endif
42 1.1 cgd #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * common.c: Common Editor functions
46 1.1 cgd */
47 1.1 cgd #include "el.h"
48 1.1 cgd
49 1.8 simonb /* ed_end_of_file():
50 1.1 cgd * Indicate end of file
51 1.1 cgd * [^D]
52 1.1 cgd */
53 1.1 cgd protected el_action_t
54 1.1 cgd /*ARGSUSED*/
55 1.24 christos ed_end_of_file(EditLine *el, Int c __attribute__((__unused__)))
56 1.9 lukem {
57 1.9 lukem
58 1.9 lukem re_goto_bottom(el);
59 1.9 lukem *el->el_line.lastchar = '\0';
60 1.26 christos return CC_EOF;
61 1.1 cgd }
62 1.1 cgd
63 1.1 cgd
64 1.8 simonb /* ed_insert():
65 1.1 cgd * Add character to the line
66 1.1 cgd * Insert a character [bound to all insert keys]
67 1.1 cgd */
68 1.1 cgd protected el_action_t
69 1.24 christos ed_insert(EditLine *el, Int c)
70 1.9 lukem {
71 1.13 christos int count = el->el_state.argument;
72 1.1 cgd
73 1.9 lukem if (c == '\0')
74 1.26 christos return CC_ERROR;
75 1.1 cgd
76 1.9 lukem if (el->el_line.lastchar + el->el_state.argument >=
77 1.10 jdolecek el->el_line.limit) {
78 1.10 jdolecek /* end of buffer space, try to allocate more */
79 1.13 christos if (!ch_enlargebufs(el, (size_t) count))
80 1.10 jdolecek return CC_ERROR; /* error allocating more */
81 1.10 jdolecek }
82 1.9 lukem
83 1.13 christos if (count == 1) {
84 1.12 christos if (el->el_state.inputmode == MODE_INSERT
85 1.12 christos || el->el_line.cursor >= el->el_line.lastchar)
86 1.12 christos c_insert(el, 1);
87 1.9 lukem
88 1.9 lukem *el->el_line.cursor++ = c;
89 1.9 lukem re_fastaddc(el); /* fast refresh for one char. */
90 1.9 lukem } else {
91 1.12 christos if (el->el_state.inputmode != MODE_REPLACE_1)
92 1.12 christos c_insert(el, el->el_state.argument);
93 1.9 lukem
94 1.13 christos while (count-- && el->el_line.cursor < el->el_line.lastchar)
95 1.9 lukem *el->el_line.cursor++ = c;
96 1.9 lukem re_refresh(el);
97 1.9 lukem }
98 1.1 cgd
99 1.9 lukem if (el->el_state.inputmode == MODE_REPLACE_1)
100 1.12 christos return vi_command_mode(el, 0);
101 1.1 cgd
102 1.26 christos return CC_NORM;
103 1.1 cgd }
104 1.1 cgd
105 1.1 cgd
106 1.8 simonb /* ed_delete_prev_word():
107 1.1 cgd * Delete from beginning of current word to cursor
108 1.1 cgd * [M-^?] [^W]
109 1.1 cgd */
110 1.1 cgd protected el_action_t
111 1.1 cgd /*ARGSUSED*/
112 1.24 christos ed_delete_prev_word(EditLine *el, Int c __attribute__((__unused__)))
113 1.9 lukem {
114 1.24 christos Char *cp, *p, *kp;
115 1.9 lukem
116 1.9 lukem if (el->el_line.cursor == el->el_line.buffer)
117 1.26 christos return CC_ERROR;
118 1.9 lukem
119 1.9 lukem cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
120 1.9 lukem el->el_state.argument, ce__isword);
121 1.9 lukem
122 1.9 lukem for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
123 1.9 lukem *kp++ = *p;
124 1.9 lukem el->el_chared.c_kill.last = kp;
125 1.9 lukem
126 1.22 christos c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
127 1.9 lukem el->el_line.cursor = cp;
128 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
129 1.9 lukem el->el_line.cursor = el->el_line.buffer; /* bounds check */
130 1.26 christos return CC_REFRESH;
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd
134 1.8 simonb /* ed_delete_next_char():
135 1.1 cgd * Delete character under cursor
136 1.1 cgd * [^D] [x]
137 1.1 cgd */
138 1.1 cgd protected el_action_t
139 1.1 cgd /*ARGSUSED*/
140 1.28 christos ed_delete_next_char(EditLine *el, Int c __attribute__((__unused__)))
141 1.1 cgd {
142 1.27 christos #ifdef DEBUG_EDIT
143 1.9 lukem #define EL el->el_line
144 1.6 christos (void) fprintf(el->el_errlfile,
145 1.8 simonb "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
146 1.6 christos EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
147 1.6 christos EL.lastchar, EL.limit, EL.limit);
148 1.1 cgd #endif
149 1.9 lukem if (el->el_line.cursor == el->el_line.lastchar) {
150 1.9 lukem /* if I'm at the end */
151 1.9 lukem if (el->el_map.type == MAP_VI) {
152 1.9 lukem if (el->el_line.cursor == el->el_line.buffer) {
153 1.9 lukem /* if I'm also at the beginning */
154 1.1 cgd #ifdef KSHVI
155 1.26 christos return CC_ERROR;
156 1.1 cgd #else
157 1.19 christos /* then do an EOF */
158 1.25 christos terminal_writec(el, c);
159 1.26 christos return CC_EOF;
160 1.1 cgd #endif
161 1.9 lukem } else {
162 1.1 cgd #ifdef KSHVI
163 1.9 lukem el->el_line.cursor--;
164 1.1 cgd #else
165 1.26 christos return CC_ERROR;
166 1.1 cgd #endif
167 1.9 lukem }
168 1.29 christos } else
169 1.26 christos return CC_ERROR;
170 1.1 cgd }
171 1.9 lukem c_delafter(el, el->el_state.argument); /* delete after dot */
172 1.29 christos if (el->el_map.type == MAP_VI &&
173 1.29 christos el->el_line.cursor >= el->el_line.lastchar &&
174 1.9 lukem el->el_line.cursor > el->el_line.buffer)
175 1.9 lukem /* bounds check */
176 1.9 lukem el->el_line.cursor = el->el_line.lastchar - 1;
177 1.26 christos return CC_REFRESH;
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd
181 1.8 simonb /* ed_kill_line():
182 1.1 cgd * Cut to the end of line
183 1.1 cgd * [^K] [^K]
184 1.1 cgd */
185 1.1 cgd protected el_action_t
186 1.1 cgd /*ARGSUSED*/
187 1.24 christos ed_kill_line(EditLine *el, Int c __attribute__((__unused__)))
188 1.9 lukem {
189 1.24 christos Char *kp, *cp;
190 1.9 lukem
191 1.9 lukem cp = el->el_line.cursor;
192 1.9 lukem kp = el->el_chared.c_kill.buf;
193 1.9 lukem while (cp < el->el_line.lastchar)
194 1.9 lukem *kp++ = *cp++; /* copy it */
195 1.9 lukem el->el_chared.c_kill.last = kp;
196 1.9 lukem /* zap! -- delete to end */
197 1.9 lukem el->el_line.lastchar = el->el_line.cursor;
198 1.26 christos return CC_REFRESH;
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd
202 1.8 simonb /* ed_move_to_end():
203 1.1 cgd * Move cursor to the end of line
204 1.1 cgd * [^E] [^E]
205 1.1 cgd */
206 1.1 cgd protected el_action_t
207 1.1 cgd /*ARGSUSED*/
208 1.24 christos ed_move_to_end(EditLine *el, Int c __attribute__((__unused__)))
209 1.1 cgd {
210 1.9 lukem
211 1.9 lukem el->el_line.cursor = el->el_line.lastchar;
212 1.9 lukem if (el->el_map.type == MAP_VI) {
213 1.13 christos if (el->el_chared.c_vcmd.action != NOP) {
214 1.9 lukem cv_delfini(el);
215 1.26 christos return CC_REFRESH;
216 1.9 lukem }
217 1.21 aymeric #ifdef VI_MOVE
218 1.21 aymeric el->el_line.cursor--;
219 1.21 aymeric #endif
220 1.1 cgd }
221 1.26 christos return CC_CURSOR;
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd
225 1.8 simonb /* ed_move_to_beg():
226 1.1 cgd * Move cursor to the beginning of line
227 1.1 cgd * [^A] [^A]
228 1.1 cgd */
229 1.1 cgd protected el_action_t
230 1.1 cgd /*ARGSUSED*/
231 1.24 christos ed_move_to_beg(EditLine *el, Int c __attribute__((__unused__)))
232 1.9 lukem {
233 1.9 lukem
234 1.9 lukem el->el_line.cursor = el->el_line.buffer;
235 1.9 lukem
236 1.9 lukem if (el->el_map.type == MAP_VI) {
237 1.9 lukem /* We want FIRST non space character */
238 1.24 christos while (Isspace(*el->el_line.cursor))
239 1.9 lukem el->el_line.cursor++;
240 1.13 christos if (el->el_chared.c_vcmd.action != NOP) {
241 1.9 lukem cv_delfini(el);
242 1.26 christos return CC_REFRESH;
243 1.9 lukem }
244 1.1 cgd }
245 1.26 christos return CC_CURSOR;
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd
249 1.8 simonb /* ed_transpose_chars():
250 1.1 cgd * Exchange the character to the left of the cursor with the one under it
251 1.1 cgd * [^T] [^T]
252 1.1 cgd */
253 1.1 cgd protected el_action_t
254 1.24 christos ed_transpose_chars(EditLine *el, Int c)
255 1.9 lukem {
256 1.9 lukem
257 1.9 lukem if (el->el_line.cursor < el->el_line.lastchar) {
258 1.9 lukem if (el->el_line.lastchar <= &el->el_line.buffer[1])
259 1.26 christos return CC_ERROR;
260 1.9 lukem else
261 1.9 lukem el->el_line.cursor++;
262 1.9 lukem }
263 1.9 lukem if (el->el_line.cursor > &el->el_line.buffer[1]) {
264 1.9 lukem /* must have at least two chars entered */
265 1.9 lukem c = el->el_line.cursor[-2];
266 1.9 lukem el->el_line.cursor[-2] = el->el_line.cursor[-1];
267 1.9 lukem el->el_line.cursor[-1] = c;
268 1.26 christos return CC_REFRESH;
269 1.9 lukem } else
270 1.26 christos return CC_ERROR;
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd
274 1.8 simonb /* ed_next_char():
275 1.1 cgd * Move to the right one character
276 1.1 cgd * [^F] [^F]
277 1.1 cgd */
278 1.1 cgd protected el_action_t
279 1.1 cgd /*ARGSUSED*/
280 1.24 christos ed_next_char(EditLine *el, Int c __attribute__((__unused__)))
281 1.1 cgd {
282 1.24 christos Char *lim = el->el_line.lastchar;
283 1.1 cgd
284 1.13 christos if (el->el_line.cursor >= lim ||
285 1.13 christos (el->el_line.cursor == lim - 1 &&
286 1.13 christos el->el_map.type == MAP_VI &&
287 1.13 christos el->el_chared.c_vcmd.action == NOP))
288 1.26 christos return CC_ERROR;
289 1.1 cgd
290 1.9 lukem el->el_line.cursor += el->el_state.argument;
291 1.13 christos if (el->el_line.cursor > lim)
292 1.13 christos el->el_line.cursor = lim;
293 1.1 cgd
294 1.9 lukem if (el->el_map.type == MAP_VI)
295 1.13 christos if (el->el_chared.c_vcmd.action != NOP) {
296 1.9 lukem cv_delfini(el);
297 1.26 christos return CC_REFRESH;
298 1.9 lukem }
299 1.26 christos return CC_CURSOR;
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd
303 1.8 simonb /* ed_prev_word():
304 1.1 cgd * Move to the beginning of the current word
305 1.1 cgd * [M-b] [b]
306 1.1 cgd */
307 1.1 cgd protected el_action_t
308 1.1 cgd /*ARGSUSED*/
309 1.24 christos ed_prev_word(EditLine *el, Int c __attribute__((__unused__)))
310 1.1 cgd {
311 1.1 cgd
312 1.9 lukem if (el->el_line.cursor == el->el_line.buffer)
313 1.26 christos return CC_ERROR;
314 1.1 cgd
315 1.9 lukem el->el_line.cursor = c__prev_word(el->el_line.cursor,
316 1.9 lukem el->el_line.buffer,
317 1.9 lukem el->el_state.argument,
318 1.9 lukem ce__isword);
319 1.1 cgd
320 1.9 lukem if (el->el_map.type == MAP_VI)
321 1.13 christos if (el->el_chared.c_vcmd.action != NOP) {
322 1.9 lukem cv_delfini(el);
323 1.26 christos return CC_REFRESH;
324 1.9 lukem }
325 1.26 christos return CC_CURSOR;
326 1.1 cgd }
327 1.1 cgd
328 1.1 cgd
329 1.8 simonb /* ed_prev_char():
330 1.1 cgd * Move to the left one character
331 1.1 cgd * [^B] [^B]
332 1.1 cgd */
333 1.1 cgd protected el_action_t
334 1.1 cgd /*ARGSUSED*/
335 1.24 christos ed_prev_char(EditLine *el, Int c __attribute__((__unused__)))
336 1.1 cgd {
337 1.1 cgd
338 1.9 lukem if (el->el_line.cursor > el->el_line.buffer) {
339 1.9 lukem el->el_line.cursor -= el->el_state.argument;
340 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
341 1.9 lukem el->el_line.cursor = el->el_line.buffer;
342 1.9 lukem
343 1.9 lukem if (el->el_map.type == MAP_VI)
344 1.13 christos if (el->el_chared.c_vcmd.action != NOP) {
345 1.9 lukem cv_delfini(el);
346 1.26 christos return CC_REFRESH;
347 1.9 lukem }
348 1.26 christos return CC_CURSOR;
349 1.9 lukem } else
350 1.26 christos return CC_ERROR;
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd
354 1.8 simonb /* ed_quoted_insert():
355 1.1 cgd * Add the next character typed verbatim
356 1.1 cgd * [^V] [^V]
357 1.1 cgd */
358 1.1 cgd protected el_action_t
359 1.24 christos ed_quoted_insert(EditLine *el, Int c)
360 1.9 lukem {
361 1.9 lukem int num;
362 1.24 christos Char tc;
363 1.9 lukem
364 1.9 lukem tty_quotemode(el);
365 1.24 christos num = FUN(el,getc)(el, &tc);
366 1.24 christos c = tc;
367 1.9 lukem tty_noquotemode(el);
368 1.9 lukem if (num == 1)
369 1.26 christos return ed_insert(el, c);
370 1.9 lukem else
371 1.26 christos return ed_end_of_file(el, 0);
372 1.1 cgd }
373 1.1 cgd
374 1.1 cgd
375 1.8 simonb /* ed_digit():
376 1.1 cgd * Adds to argument or enters a digit
377 1.1 cgd */
378 1.1 cgd protected el_action_t
379 1.24 christos ed_digit(EditLine *el, Int c)
380 1.9 lukem {
381 1.9 lukem
382 1.24 christos if (!Isdigit(c))
383 1.26 christos return CC_ERROR;
384 1.9 lukem
385 1.9 lukem if (el->el_state.doingarg) {
386 1.9 lukem /* if doing an arg, add this in... */
387 1.9 lukem if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
388 1.9 lukem el->el_state.argument = c - '0';
389 1.9 lukem else {
390 1.9 lukem if (el->el_state.argument > 1000000)
391 1.26 christos return CC_ERROR;
392 1.9 lukem el->el_state.argument =
393 1.9 lukem (el->el_state.argument * 10) + (c - '0');
394 1.9 lukem }
395 1.26 christos return CC_ARGHACK;
396 1.12 christos }
397 1.9 lukem
398 1.12 christos return ed_insert(el, c);
399 1.1 cgd }
400 1.1 cgd
401 1.1 cgd
402 1.8 simonb /* ed_argument_digit():
403 1.1 cgd * Digit that starts argument
404 1.1 cgd * For ESC-n
405 1.1 cgd */
406 1.1 cgd protected el_action_t
407 1.24 christos ed_argument_digit(EditLine *el, Int c)
408 1.9 lukem {
409 1.9 lukem
410 1.24 christos if (!Isdigit(c))
411 1.26 christos return CC_ERROR;
412 1.9 lukem
413 1.9 lukem if (el->el_state.doingarg) {
414 1.9 lukem if (el->el_state.argument > 1000000)
415 1.26 christos return CC_ERROR;
416 1.9 lukem el->el_state.argument = (el->el_state.argument * 10) +
417 1.9 lukem (c - '0');
418 1.9 lukem } else { /* else starting an argument */
419 1.9 lukem el->el_state.argument = c - '0';
420 1.9 lukem el->el_state.doingarg = 1;
421 1.9 lukem }
422 1.26 christos return CC_ARGHACK;
423 1.1 cgd }
424 1.1 cgd
425 1.1 cgd
426 1.8 simonb /* ed_unassigned():
427 1.1 cgd * Indicates unbound character
428 1.1 cgd * Bound to keys that are not assigned
429 1.1 cgd */
430 1.1 cgd protected el_action_t
431 1.1 cgd /*ARGSUSED*/
432 1.28 christos ed_unassigned(EditLine *el __attribute__((__unused__)),
433 1.28 christos Int c __attribute__((__unused__)))
434 1.9 lukem {
435 1.9 lukem
436 1.26 christos return CC_ERROR;
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd
440 1.1 cgd /**
441 1.1 cgd ** TTY key handling.
442 1.1 cgd **/
443 1.1 cgd
444 1.8 simonb /* ed_tty_sigint():
445 1.1 cgd * Tty interrupt character
446 1.1 cgd * [^C]
447 1.1 cgd */
448 1.1 cgd protected el_action_t
449 1.1 cgd /*ARGSUSED*/
450 1.15 christos ed_tty_sigint(EditLine *el __attribute__((__unused__)),
451 1.24 christos Int c __attribute__((__unused__)))
452 1.8 simonb {
453 1.9 lukem
454 1.26 christos return CC_NORM;
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd
458 1.8 simonb /* ed_tty_dsusp():
459 1.1 cgd * Tty delayed suspend character
460 1.1 cgd * [^Y]
461 1.1 cgd */
462 1.1 cgd protected el_action_t
463 1.1 cgd /*ARGSUSED*/
464 1.15 christos ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
465 1.24 christos Int c __attribute__((__unused__)))
466 1.1 cgd {
467 1.9 lukem
468 1.26 christos return CC_NORM;
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd
472 1.8 simonb /* ed_tty_flush_output():
473 1.1 cgd * Tty flush output characters
474 1.1 cgd * [^O]
475 1.1 cgd */
476 1.1 cgd protected el_action_t
477 1.1 cgd /*ARGSUSED*/
478 1.15 christos ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
479 1.24 christos Int c __attribute__((__unused__)))
480 1.1 cgd {
481 1.9 lukem
482 1.26 christos return CC_NORM;
483 1.1 cgd }
484 1.1 cgd
485 1.1 cgd
486 1.8 simonb /* ed_tty_sigquit():
487 1.1 cgd * Tty quit character
488 1.1 cgd * [^\]
489 1.1 cgd */
490 1.1 cgd protected el_action_t
491 1.1 cgd /*ARGSUSED*/
492 1.15 christos ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
493 1.24 christos Int c __attribute__((__unused__)))
494 1.1 cgd {
495 1.9 lukem
496 1.26 christos return CC_NORM;
497 1.1 cgd }
498 1.1 cgd
499 1.1 cgd
500 1.8 simonb /* ed_tty_sigtstp():
501 1.1 cgd * Tty suspend character
502 1.1 cgd * [^Z]
503 1.1 cgd */
504 1.1 cgd protected el_action_t
505 1.1 cgd /*ARGSUSED*/
506 1.15 christos ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
507 1.24 christos Int c __attribute__((__unused__)))
508 1.1 cgd {
509 1.9 lukem
510 1.26 christos return CC_NORM;
511 1.1 cgd }
512 1.1 cgd
513 1.1 cgd
514 1.8 simonb /* ed_tty_stop_output():
515 1.1 cgd * Tty disallow output characters
516 1.1 cgd * [^S]
517 1.1 cgd */
518 1.1 cgd protected el_action_t
519 1.1 cgd /*ARGSUSED*/
520 1.15 christos ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
521 1.24 christos Int c __attribute__((__unused__)))
522 1.1 cgd {
523 1.9 lukem
524 1.26 christos return CC_NORM;
525 1.1 cgd }
526 1.1 cgd
527 1.1 cgd
528 1.8 simonb /* ed_tty_start_output():
529 1.1 cgd * Tty allow output characters
530 1.1 cgd * [^Q]
531 1.1 cgd */
532 1.1 cgd protected el_action_t
533 1.1 cgd /*ARGSUSED*/
534 1.15 christos ed_tty_start_output(EditLine *el __attribute__((__unused__)),
535 1.24 christos Int c __attribute__((__unused__)))
536 1.1 cgd {
537 1.9 lukem
538 1.26 christos return CC_NORM;
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd
542 1.8 simonb /* ed_newline():
543 1.1 cgd * Execute command
544 1.1 cgd * [^J]
545 1.1 cgd */
546 1.1 cgd protected el_action_t
547 1.1 cgd /*ARGSUSED*/
548 1.24 christos ed_newline(EditLine *el, Int c __attribute__((__unused__)))
549 1.9 lukem {
550 1.9 lukem
551 1.9 lukem re_goto_bottom(el);
552 1.9 lukem *el->el_line.lastchar++ = '\n';
553 1.9 lukem *el->el_line.lastchar = '\0';
554 1.26 christos return CC_NEWLINE;
555 1.1 cgd }
556 1.1 cgd
557 1.1 cgd
558 1.8 simonb /* ed_delete_prev_char():
559 1.1 cgd * Delete the character to the left of the cursor
560 1.1 cgd * [^?]
561 1.1 cgd */
562 1.1 cgd protected el_action_t
563 1.1 cgd /*ARGSUSED*/
564 1.24 christos ed_delete_prev_char(EditLine *el, Int c __attribute__((__unused__)))
565 1.9 lukem {
566 1.9 lukem
567 1.9 lukem if (el->el_line.cursor <= el->el_line.buffer)
568 1.26 christos return CC_ERROR;
569 1.9 lukem
570 1.9 lukem c_delbefore(el, el->el_state.argument);
571 1.9 lukem el->el_line.cursor -= el->el_state.argument;
572 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
573 1.9 lukem el->el_line.cursor = el->el_line.buffer;
574 1.26 christos return CC_REFRESH;
575 1.1 cgd }
576 1.1 cgd
577 1.1 cgd
578 1.8 simonb /* ed_clear_screen():
579 1.1 cgd * Clear screen leaving current line at the top
580 1.1 cgd * [^L]
581 1.1 cgd */
582 1.1 cgd protected el_action_t
583 1.1 cgd /*ARGSUSED*/
584 1.24 christos ed_clear_screen(EditLine *el, Int c __attribute__((__unused__)))
585 1.9 lukem {
586 1.9 lukem
587 1.25 christos terminal_clear_screen(el); /* clear the whole real screen */
588 1.9 lukem re_clear_display(el); /* reset everything */
589 1.26 christos return CC_REFRESH;
590 1.1 cgd }
591 1.1 cgd
592 1.1 cgd
593 1.8 simonb /* ed_redisplay():
594 1.1 cgd * Redisplay everything
595 1.1 cgd * ^R
596 1.1 cgd */
597 1.1 cgd protected el_action_t
598 1.1 cgd /*ARGSUSED*/
599 1.15 christos ed_redisplay(EditLine *el __attribute__((__unused__)),
600 1.24 christos Int c __attribute__((__unused__)))
601 1.1 cgd {
602 1.9 lukem
603 1.26 christos return CC_REDISPLAY;
604 1.1 cgd }
605 1.1 cgd
606 1.1 cgd
607 1.8 simonb /* ed_start_over():
608 1.1 cgd * Erase current line and start from scratch
609 1.1 cgd * [^G]
610 1.1 cgd */
611 1.1 cgd protected el_action_t
612 1.1 cgd /*ARGSUSED*/
613 1.24 christos ed_start_over(EditLine *el, Int c __attribute__((__unused__)))
614 1.1 cgd {
615 1.9 lukem
616 1.17 christos ch_reset(el, 0);
617 1.26 christos return CC_REFRESH;
618 1.1 cgd }
619 1.1 cgd
620 1.1 cgd
621 1.8 simonb /* ed_sequence_lead_in():
622 1.1 cgd * First character in a bound sequence
623 1.1 cgd * Placeholder for external keys
624 1.1 cgd */
625 1.1 cgd protected el_action_t
626 1.1 cgd /*ARGSUSED*/
627 1.15 christos ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
628 1.24 christos Int c __attribute__((__unused__)))
629 1.1 cgd {
630 1.9 lukem
631 1.26 christos return CC_NORM;
632 1.1 cgd }
633 1.1 cgd
634 1.1 cgd
635 1.8 simonb /* ed_prev_history():
636 1.1 cgd * Move to the previous history line
637 1.1 cgd * [^P] [k]
638 1.1 cgd */
639 1.1 cgd protected el_action_t
640 1.1 cgd /*ARGSUSED*/
641 1.24 christos ed_prev_history(EditLine *el, Int c __attribute__((__unused__)))
642 1.9 lukem {
643 1.9 lukem char beep = 0;
644 1.13 christos int sv_event = el->el_history.eventno;
645 1.9 lukem
646 1.12 christos el->el_chared.c_undo.len = -1;
647 1.9 lukem *el->el_line.lastchar = '\0'; /* just in case */
648 1.9 lukem
649 1.9 lukem if (el->el_history.eventno == 0) { /* save the current buffer
650 1.9 lukem * away */
651 1.24 christos (void) Strncpy(el->el_history.buf, el->el_line.buffer,
652 1.9 lukem EL_BUFSIZ);
653 1.9 lukem el->el_history.last = el->el_history.buf +
654 1.9 lukem (el->el_line.lastchar - el->el_line.buffer);
655 1.9 lukem }
656 1.9 lukem el->el_history.eventno += el->el_state.argument;
657 1.9 lukem
658 1.9 lukem if (hist_get(el) == CC_ERROR) {
659 1.13 christos if (el->el_map.type == MAP_VI) {
660 1.13 christos el->el_history.eventno = sv_event;
661 1.24 christos
662 1.13 christos }
663 1.9 lukem beep = 1;
664 1.9 lukem /* el->el_history.eventno was fixed by first call */
665 1.9 lukem (void) hist_get(el);
666 1.9 lukem }
667 1.9 lukem if (beep)
668 1.13 christos return CC_REFRESH_BEEP;
669 1.13 christos return CC_REFRESH;
670 1.1 cgd }
671 1.1 cgd
672 1.1 cgd
673 1.8 simonb /* ed_next_history():
674 1.1 cgd * Move to the next history line
675 1.1 cgd * [^N] [j]
676 1.1 cgd */
677 1.1 cgd protected el_action_t
678 1.1 cgd /*ARGSUSED*/
679 1.24 christos ed_next_history(EditLine *el, Int c __attribute__((__unused__)))
680 1.1 cgd {
681 1.13 christos el_action_t beep = CC_REFRESH, rval;
682 1.1 cgd
683 1.12 christos el->el_chared.c_undo.len = -1;
684 1.9 lukem *el->el_line.lastchar = '\0'; /* just in case */
685 1.1 cgd
686 1.9 lukem el->el_history.eventno -= el->el_state.argument;
687 1.1 cgd
688 1.9 lukem if (el->el_history.eventno < 0) {
689 1.9 lukem el->el_history.eventno = 0;
690 1.13 christos beep = CC_REFRESH_BEEP;
691 1.9 lukem }
692 1.13 christos rval = hist_get(el);
693 1.13 christos if (rval == CC_REFRESH)
694 1.13 christos return beep;
695 1.13 christos return rval;
696 1.13 christos
697 1.1 cgd }
698 1.1 cgd
699 1.1 cgd
700 1.8 simonb /* ed_search_prev_history():
701 1.1 cgd * Search previous in history for a line matching the current
702 1.1 cgd * next search history [M-P] [K]
703 1.1 cgd */
704 1.1 cgd protected el_action_t
705 1.1 cgd /*ARGSUSED*/
706 1.24 christos ed_search_prev_history(EditLine *el, Int c __attribute__((__unused__)))
707 1.9 lukem {
708 1.24 christos const Char *hp;
709 1.9 lukem int h;
710 1.9 lukem bool_t found = 0;
711 1.9 lukem
712 1.9 lukem el->el_chared.c_vcmd.action = NOP;
713 1.12 christos el->el_chared.c_undo.len = -1;
714 1.9 lukem *el->el_line.lastchar = '\0'; /* just in case */
715 1.9 lukem if (el->el_history.eventno < 0) {
716 1.1 cgd #ifdef DEBUG_EDIT
717 1.9 lukem (void) fprintf(el->el_errfile,
718 1.9 lukem "e_prev_search_hist(): eventno < 0;\n");
719 1.1 cgd #endif
720 1.9 lukem el->el_history.eventno = 0;
721 1.26 christos return CC_ERROR;
722 1.9 lukem }
723 1.9 lukem if (el->el_history.eventno == 0) {
724 1.24 christos (void) Strncpy(el->el_history.buf, el->el_line.buffer,
725 1.9 lukem EL_BUFSIZ);
726 1.9 lukem el->el_history.last = el->el_history.buf +
727 1.9 lukem (el->el_line.lastchar - el->el_line.buffer);
728 1.9 lukem }
729 1.9 lukem if (el->el_history.ref == NULL)
730 1.26 christos return CC_ERROR;
731 1.1 cgd
732 1.9 lukem hp = HIST_FIRST(el);
733 1.9 lukem if (hp == NULL)
734 1.26 christos return CC_ERROR;
735 1.1 cgd
736 1.9 lukem c_setpat(el); /* Set search pattern !! */
737 1.1 cgd
738 1.9 lukem for (h = 1; h <= el->el_history.eventno; h++)
739 1.9 lukem hp = HIST_NEXT(el);
740 1.1 cgd
741 1.9 lukem while (hp != NULL) {
742 1.1 cgd #ifdef SDEBUG
743 1.9 lukem (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
744 1.1 cgd #endif
745 1.24 christos if ((Strncmp(hp, el->el_line.buffer, (size_t)
746 1.9 lukem (el->el_line.lastchar - el->el_line.buffer)) ||
747 1.9 lukem hp[el->el_line.lastchar - el->el_line.buffer]) &&
748 1.9 lukem c_hmatch(el, hp)) {
749 1.9 lukem found++;
750 1.9 lukem break;
751 1.9 lukem }
752 1.9 lukem h++;
753 1.9 lukem hp = HIST_NEXT(el);
754 1.9 lukem }
755 1.1 cgd
756 1.9 lukem if (!found) {
757 1.1 cgd #ifdef SDEBUG
758 1.9 lukem (void) fprintf(el->el_errfile, "not found\n");
759 1.1 cgd #endif
760 1.26 christos return CC_ERROR;
761 1.9 lukem }
762 1.9 lukem el->el_history.eventno = h;
763 1.1 cgd
764 1.26 christos return hist_get(el);
765 1.1 cgd }
766 1.1 cgd
767 1.1 cgd
768 1.8 simonb /* ed_search_next_history():
769 1.1 cgd * Search next in history for a line matching the current
770 1.1 cgd * [M-N] [J]
771 1.1 cgd */
772 1.1 cgd protected el_action_t
773 1.1 cgd /*ARGSUSED*/
774 1.24 christos ed_search_next_history(EditLine *el, Int c __attribute__((__unused__)))
775 1.1 cgd {
776 1.24 christos const Char *hp;
777 1.9 lukem int h;
778 1.9 lukem bool_t found = 0;
779 1.1 cgd
780 1.9 lukem el->el_chared.c_vcmd.action = NOP;
781 1.12 christos el->el_chared.c_undo.len = -1;
782 1.9 lukem *el->el_line.lastchar = '\0'; /* just in case */
783 1.1 cgd
784 1.9 lukem if (el->el_history.eventno == 0)
785 1.26 christos return CC_ERROR;
786 1.1 cgd
787 1.9 lukem if (el->el_history.ref == NULL)
788 1.26 christos return CC_ERROR;
789 1.1 cgd
790 1.9 lukem hp = HIST_FIRST(el);
791 1.9 lukem if (hp == NULL)
792 1.26 christos return CC_ERROR;
793 1.1 cgd
794 1.9 lukem c_setpat(el); /* Set search pattern !! */
795 1.1 cgd
796 1.9 lukem for (h = 1; h < el->el_history.eventno && hp; h++) {
797 1.1 cgd #ifdef SDEBUG
798 1.9 lukem (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
799 1.1 cgd #endif
800 1.24 christos if ((Strncmp(hp, el->el_line.buffer, (size_t)
801 1.9 lukem (el->el_line.lastchar - el->el_line.buffer)) ||
802 1.9 lukem hp[el->el_line.lastchar - el->el_line.buffer]) &&
803 1.9 lukem c_hmatch(el, hp))
804 1.9 lukem found = h;
805 1.9 lukem hp = HIST_NEXT(el);
806 1.9 lukem }
807 1.1 cgd
808 1.9 lukem if (!found) { /* is it the current history number? */
809 1.9 lukem if (!c_hmatch(el, el->el_history.buf)) {
810 1.1 cgd #ifdef SDEBUG
811 1.9 lukem (void) fprintf(el->el_errfile, "not found\n");
812 1.1 cgd #endif
813 1.26 christos return CC_ERROR;
814 1.9 lukem }
815 1.1 cgd }
816 1.9 lukem el->el_history.eventno = found;
817 1.1 cgd
818 1.26 christos return hist_get(el);
819 1.1 cgd }
820 1.1 cgd
821 1.1 cgd
822 1.1 cgd /* ed_prev_line():
823 1.1 cgd * Move up one line
824 1.1 cgd * Could be [k] [^p]
825 1.1 cgd */
826 1.1 cgd protected el_action_t
827 1.1 cgd /*ARGSUSED*/
828 1.24 christos ed_prev_line(EditLine *el, Int c __attribute__((__unused__)))
829 1.9 lukem {
830 1.24 christos Char *ptr;
831 1.9 lukem int nchars = c_hpos(el);
832 1.8 simonb
833 1.9 lukem /*
834 1.9 lukem * Move to the line requested
835 1.9 lukem */
836 1.9 lukem if (*(ptr = el->el_line.cursor) == '\n')
837 1.9 lukem ptr--;
838 1.9 lukem
839 1.9 lukem for (; ptr >= el->el_line.buffer; ptr--)
840 1.9 lukem if (*ptr == '\n' && --el->el_state.argument <= 0)
841 1.9 lukem break;
842 1.9 lukem
843 1.9 lukem if (el->el_state.argument > 0)
844 1.26 christos return CC_ERROR;
845 1.9 lukem
846 1.9 lukem /*
847 1.9 lukem * Move to the beginning of the line
848 1.9 lukem */
849 1.9 lukem for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
850 1.9 lukem continue;
851 1.9 lukem
852 1.9 lukem /*
853 1.9 lukem * Move to the character requested
854 1.9 lukem */
855 1.9 lukem for (ptr++;
856 1.9 lukem nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
857 1.9 lukem ptr++)
858 1.9 lukem continue;
859 1.9 lukem
860 1.9 lukem el->el_line.cursor = ptr;
861 1.26 christos return CC_CURSOR;
862 1.1 cgd }
863 1.1 cgd
864 1.1 cgd
865 1.1 cgd /* ed_next_line():
866 1.1 cgd * Move down one line
867 1.1 cgd * Could be [j] [^n]
868 1.1 cgd */
869 1.1 cgd protected el_action_t
870 1.1 cgd /*ARGSUSED*/
871 1.24 christos ed_next_line(EditLine *el, Int c __attribute__((__unused__)))
872 1.9 lukem {
873 1.24 christos Char *ptr;
874 1.9 lukem int nchars = c_hpos(el);
875 1.8 simonb
876 1.9 lukem /*
877 1.9 lukem * Move to the line requested
878 1.9 lukem */
879 1.9 lukem for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
880 1.9 lukem if (*ptr == '\n' && --el->el_state.argument <= 0)
881 1.9 lukem break;
882 1.9 lukem
883 1.9 lukem if (el->el_state.argument > 0)
884 1.26 christos return CC_ERROR;
885 1.9 lukem
886 1.9 lukem /*
887 1.9 lukem * Move to the character requested
888 1.9 lukem */
889 1.9 lukem for (ptr++;
890 1.9 lukem nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
891 1.9 lukem ptr++)
892 1.9 lukem continue;
893 1.9 lukem
894 1.9 lukem el->el_line.cursor = ptr;
895 1.26 christos return CC_CURSOR;
896 1.1 cgd }
897 1.1 cgd
898 1.1 cgd
899 1.8 simonb /* ed_command():
900 1.1 cgd * Editline extended command
901 1.1 cgd * [M-X] [:]
902 1.1 cgd */
903 1.1 cgd protected el_action_t
904 1.1 cgd /*ARGSUSED*/
905 1.24 christos ed_command(EditLine *el, Int c __attribute__((__unused__)))
906 1.9 lukem {
907 1.24 christos Char tmpbuf[EL_BUFSIZ];
908 1.9 lukem int tmplen;
909 1.9 lukem
910 1.24 christos tmplen = c_gets(el, tmpbuf, STR("\n: "));
911 1.25 christos terminal__putc(el, '\n');
912 1.9 lukem
913 1.14 christos if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
914 1.25 christos terminal_beep(el);
915 1.9 lukem
916 1.14 christos el->el_map.current = el->el_map.key;
917 1.14 christos re_clear_display(el);
918 1.14 christos return CC_REFRESH;
919 1.1 cgd }
920