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