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