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