2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)common.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: common.c,v 1.16 2003/08/07 16:44:30 agc Exp $
34 * $DragonFly: src/lib/libedit/common.c,v 1.5 2005/11/13 11:58:30 corecode Exp $
40 * common.c: Common Editor functions
45 * Indicate end of file
50 ed_end_of_file(EditLine *el, int c __attribute__((__unused__)))
54 *el->el_line.lastchar = '\0';
60 * Add character to the line
61 * Insert a character [bound to all insert keys]
64 ed_insert(EditLine *el, int c)
66 int count = el->el_state.argument;
71 if (el->el_line.lastchar + el->el_state.argument >=
73 /* end of buffer space, try to allocate more */
74 if (!ch_enlargebufs(el, (size_t) count))
75 return CC_ERROR; /* error allocating more */
79 if (el->el_state.inputmode == MODE_INSERT
80 || el->el_line.cursor >= el->el_line.lastchar)
83 *el->el_line.cursor++ = c;
84 re_fastaddc(el); /* fast refresh for one char. */
86 if (el->el_state.inputmode != MODE_REPLACE_1)
87 c_insert(el, el->el_state.argument);
89 while (count-- && el->el_line.cursor < el->el_line.lastchar)
90 *el->el_line.cursor++ = c;
94 if (el->el_state.inputmode == MODE_REPLACE_1)
95 return vi_command_mode(el, 0);
101 /* ed_delete_prev_word():
102 * Delete from beginning of current word to cursor
105 protected el_action_t
107 ed_delete_prev_word(EditLine *el, int c __attribute__((__unused__)))
111 if (el->el_line.cursor == el->el_line.buffer)
114 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
115 el->el_state.argument, ce__isword);
117 for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
119 el->el_chared.c_kill.last = kp;
121 c_delbefore(el, el->el_line.cursor - cp); /* delete before dot */
122 el->el_line.cursor = cp;
123 if (el->el_line.cursor < el->el_line.buffer)
124 el->el_line.cursor = el->el_line.buffer; /* bounds check */
129 /* ed_delete_next_char():
130 * Delete character under cursor
133 protected el_action_t
135 ed_delete_next_char(EditLine *el, int c __attribute__((__unused__)))
137 #ifdef notdef /* XXX */
138 #define EL el->el_line
139 (void) fprintf(el->el_errlfile,
140 "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
141 EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
142 EL.lastchar, EL.limit, EL.limit);
144 if (el->el_line.cursor == el->el_line.lastchar) {
145 /* if I'm at the end */
146 if (el->el_map.type == MAP_VI) {
147 if (el->el_line.cursor == el->el_line.buffer) {
148 /* if I'm also at the beginning */
152 term_overwrite(el, STReof, 4);
159 el->el_line.cursor--;
165 if (el->el_line.cursor != el->el_line.buffer)
166 el->el_line.cursor--;
171 c_delafter(el, el->el_state.argument); /* delete after dot */
172 if (el->el_line.cursor >= el->el_line.lastchar &&
173 el->el_line.cursor > el->el_line.buffer)
175 el->el_line.cursor = el->el_line.lastchar - 1;
181 * Cut to the end of line
184 protected el_action_t
186 ed_kill_line(EditLine *el, int c __attribute__((__unused__)))
190 cp = el->el_line.cursor;
191 kp = el->el_chared.c_kill.buf;
192 while (cp < el->el_line.lastchar)
193 *kp++ = *cp++; /* copy it */
194 el->el_chared.c_kill.last = kp;
195 /* zap! -- delete to end */
196 el->el_line.lastchar = el->el_line.cursor;
202 * Move cursor to the end of line
205 protected el_action_t
207 ed_move_to_end(EditLine *el, int c __attribute__((__unused__)))
210 el->el_line.cursor = el->el_line.lastchar;
211 if (el->el_map.type == MAP_VI) {
213 el->el_line.cursor--;
215 if (el->el_chared.c_vcmd.action != NOP) {
225 * Move cursor to the beginning of line
228 protected el_action_t
230 ed_move_to_beg(EditLine *el, int c __attribute__((__unused__)))
233 el->el_line.cursor = el->el_line.buffer;
235 if (el->el_map.type == MAP_VI) {
236 /* We want FIRST non space character */
237 while (isspace((unsigned char) *el->el_line.cursor))
238 el->el_line.cursor++;
239 if (el->el_chared.c_vcmd.action != NOP) {
248 /* ed_transpose_chars():
249 * Exchange the character to the left of the cursor with the one under it
252 protected el_action_t
253 ed_transpose_chars(EditLine *el, int c)
256 if (el->el_line.cursor < el->el_line.lastchar) {
257 if (el->el_line.lastchar <= &el->el_line.buffer[1])
260 el->el_line.cursor++;
262 if (el->el_line.cursor > &el->el_line.buffer[1]) {
263 /* must have at least two chars entered */
264 c = el->el_line.cursor[-2];
265 el->el_line.cursor[-2] = el->el_line.cursor[-1];
266 el->el_line.cursor[-1] = c;
274 * Move to the right one character
277 protected el_action_t
279 ed_next_char(EditLine *el, int c __attribute__((__unused__)))
281 char *lim = el->el_line.lastchar;
283 if (el->el_line.cursor >= lim ||
284 (el->el_line.cursor == lim - 1 &&
285 el->el_map.type == MAP_VI &&
286 el->el_chared.c_vcmd.action == NOP))
289 el->el_line.cursor += el->el_state.argument;
290 if (el->el_line.cursor > lim)
291 el->el_line.cursor = lim;
293 if (el->el_map.type == MAP_VI)
294 if (el->el_chared.c_vcmd.action != NOP) {
303 * Move to the beginning of the current word
306 protected el_action_t
308 ed_prev_word(EditLine *el, int c __attribute__((__unused__)))
311 if (el->el_line.cursor == el->el_line.buffer)
314 el->el_line.cursor = c__prev_word(el->el_line.cursor,
316 el->el_state.argument,
319 if (el->el_map.type == MAP_VI)
320 if (el->el_chared.c_vcmd.action != NOP) {
329 * Move to the left one character
332 protected el_action_t
334 ed_prev_char(EditLine *el, int c __attribute__((__unused__)))
337 if (el->el_line.cursor > el->el_line.buffer) {
338 el->el_line.cursor -= el->el_state.argument;
339 if (el->el_line.cursor < el->el_line.buffer)
340 el->el_line.cursor = el->el_line.buffer;
342 if (el->el_map.type == MAP_VI)
343 if (el->el_chared.c_vcmd.action != NOP) {
353 /* ed_quoted_insert():
354 * Add the next character typed verbatim
357 protected el_action_t
358 ed_quoted_insert(EditLine *el, int c)
364 num = el_getc(el, &tc);
365 c = (unsigned char) tc;
368 return (ed_insert(el, c));
370 return (ed_end_of_file(el, 0));
375 * Adds to argument or enters a digit
377 protected el_action_t
378 ed_digit(EditLine *el, int c)
384 if (el->el_state.doingarg) {
385 /* if doing an arg, add this in... */
386 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
387 el->el_state.argument = c - '0';
389 if (el->el_state.argument > 1000000)
391 el->el_state.argument =
392 (el->el_state.argument * 10) + (c - '0');
397 return ed_insert(el, c);
401 /* ed_argument_digit():
402 * Digit that starts argument
405 protected el_action_t
406 ed_argument_digit(EditLine *el, int c)
412 if (el->el_state.doingarg) {
413 if (el->el_state.argument > 1000000)
415 el->el_state.argument = (el->el_state.argument * 10) +
417 } else { /* else starting an argument */
418 el->el_state.argument = c - '0';
419 el->el_state.doingarg = 1;
426 * Indicates unbound character
427 * Bound to keys that are not assigned
429 protected el_action_t
431 ed_unassigned(EditLine *el, int c __attribute__((__unused__)))
443 * Tty interrupt character
446 protected el_action_t
448 ed_tty_sigint(EditLine *el __attribute__((__unused__)),
449 int c __attribute__((__unused__)))
457 * Tty delayed suspend character
460 protected el_action_t
462 ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
463 int c __attribute__((__unused__)))
470 /* ed_tty_flush_output():
471 * Tty flush output characters
474 protected el_action_t
476 ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
477 int c __attribute__((__unused__)))
488 protected el_action_t
490 ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
491 int c __attribute__((__unused__)))
499 * Tty suspend character
502 protected el_action_t
504 ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
505 int c __attribute__((__unused__)))
512 /* ed_tty_stop_output():
513 * Tty disallow output characters
516 protected el_action_t
518 ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
519 int c __attribute__((__unused__)))
526 /* ed_tty_start_output():
527 * Tty allow output characters
530 protected el_action_t
532 ed_tty_start_output(EditLine *el __attribute__((__unused__)),
533 int c __attribute__((__unused__)))
544 protected el_action_t
546 ed_newline(EditLine *el, int c __attribute__((__unused__)))
550 *el->el_line.lastchar++ = '\n';
551 *el->el_line.lastchar = '\0';
556 /* ed_delete_prev_char():
557 * Delete the character to the left of the cursor
560 protected el_action_t
562 ed_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
565 if (el->el_line.cursor <= el->el_line.buffer)
568 c_delbefore(el, el->el_state.argument);
569 el->el_line.cursor -= el->el_state.argument;
570 if (el->el_line.cursor < el->el_line.buffer)
571 el->el_line.cursor = el->el_line.buffer;
576 /* ed_clear_screen():
577 * Clear screen leaving current line at the top
580 protected el_action_t
582 ed_clear_screen(EditLine *el, int c __attribute__((__unused__)))
585 term_clear_screen(el); /* clear the whole real screen */
586 re_clear_display(el); /* reset everything */
592 * Redisplay everything
595 protected el_action_t
597 ed_redisplay(EditLine *el __attribute__((__unused__)),
598 int c __attribute__((__unused__)))
601 return (CC_REDISPLAY);
606 * Erase current line and start from scratch
609 protected el_action_t
611 ed_start_over(EditLine *el, int c __attribute__((__unused__)))
619 /* ed_sequence_lead_in():
620 * First character in a bound sequence
621 * Placeholder for external keys
623 protected el_action_t
625 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
626 int c __attribute__((__unused__)))
633 /* ed_prev_history():
634 * Move to the previous history line
637 protected el_action_t
639 ed_prev_history(EditLine *el, int c __attribute__((__unused__)))
642 int sv_event = el->el_history.eventno;
644 el->el_chared.c_undo.len = -1;
645 *el->el_line.lastchar = '\0'; /* just in case */
647 if (el->el_history.eventno == 0) { /* save the current buffer
649 (void) strncpy(el->el_history.buf, el->el_line.buffer,
651 el->el_history.last = el->el_history.buf +
652 (el->el_line.lastchar - el->el_line.buffer);
654 el->el_history.eventno += el->el_state.argument;
656 if (hist_get(el) == CC_ERROR) {
657 if (el->el_map.type == MAP_VI) {
658 el->el_history.eventno = sv_event;
662 /* el->el_history.eventno was fixed by first call */
666 return CC_REFRESH_BEEP;
671 /* ed_next_history():
672 * Move to the next history line
675 protected el_action_t
677 ed_next_history(EditLine *el, int c __attribute__((__unused__)))
679 el_action_t beep = CC_REFRESH, rval;
681 el->el_chared.c_undo.len = -1;
682 *el->el_line.lastchar = '\0'; /* just in case */
684 el->el_history.eventno -= el->el_state.argument;
686 if (el->el_history.eventno < 0) {
687 el->el_history.eventno = 0;
688 beep = CC_REFRESH_BEEP;
691 if (rval == CC_REFRESH)
698 /* ed_search_prev_history():
699 * Search previous in history for a line matching the current
700 * next search history [M-P] [K]
702 protected el_action_t
704 ed_search_prev_history(EditLine *el, int c __attribute__((__unused__)))
710 el->el_chared.c_vcmd.action = NOP;
711 el->el_chared.c_undo.len = -1;
712 *el->el_line.lastchar = '\0'; /* just in case */
713 if (el->el_history.eventno < 0) {
715 (void) fprintf(el->el_errfile,
716 "e_prev_search_hist(): eventno < 0;\n");
718 el->el_history.eventno = 0;
721 if (el->el_history.eventno == 0) {
722 (void) strncpy(el->el_history.buf, el->el_line.buffer,
724 el->el_history.last = el->el_history.buf +
725 (el->el_line.lastchar - el->el_line.buffer);
727 if (el->el_history.ref == NULL)
734 c_setpat(el); /* Set search pattern !! */
736 for (h = 1; h <= el->el_history.eventno; h++)
741 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
743 if ((strncmp(hp, el->el_line.buffer, (size_t)
744 (el->el_line.lastchar - el->el_line.buffer)) ||
745 hp[el->el_line.lastchar - el->el_line.buffer]) &&
756 (void) fprintf(el->el_errfile, "not found\n");
760 el->el_history.eventno = h;
762 return (hist_get(el));
766 /* ed_search_next_history():
767 * Search next in history for a line matching the current
770 protected el_action_t
772 ed_search_next_history(EditLine *el, int c __attribute__((__unused__)))
778 el->el_chared.c_vcmd.action = NOP;
779 el->el_chared.c_undo.len = -1;
780 *el->el_line.lastchar = '\0'; /* just in case */
782 if (el->el_history.eventno == 0)
785 if (el->el_history.ref == NULL)
792 c_setpat(el); /* Set search pattern !! */
794 for (h = 1; h < el->el_history.eventno && hp; h++) {
796 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
798 if ((strncmp(hp, el->el_line.buffer, (size_t)
799 (el->el_line.lastchar - el->el_line.buffer)) ||
800 hp[el->el_line.lastchar - el->el_line.buffer]) &&
806 if (!found) { /* is it the current history number? */
807 if (!c_hmatch(el, el->el_history.buf)) {
809 (void) fprintf(el->el_errfile, "not found\n");
814 el->el_history.eventno = found;
816 return (hist_get(el));
824 protected el_action_t
826 ed_prev_line(EditLine *el, int c __attribute__((__unused__)))
829 int nchars = c_hpos(el);
832 * Move to the line requested
834 if (*(ptr = el->el_line.cursor) == '\n')
837 for (; ptr >= el->el_line.buffer; ptr--)
838 if (*ptr == '\n' && --el->el_state.argument <= 0)
841 if (el->el_state.argument > 0)
845 * Move to the beginning of the line
847 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
851 * Move to the character requested
854 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
858 el->el_line.cursor = ptr;
867 protected el_action_t
869 ed_next_line(EditLine *el, int c __attribute__((__unused__)))
872 int nchars = c_hpos(el);
875 * Move to the line requested
877 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
878 if (*ptr == '\n' && --el->el_state.argument <= 0)
881 if (el->el_state.argument > 0)
885 * Move to the character requested
888 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
892 el->el_line.cursor = ptr;
898 * Editline extended command
901 protected el_action_t
903 ed_command(EditLine *el, int c __attribute__((__unused__)))
905 char tmpbuf[EL_BUFSIZ];
908 tmplen = c_gets(el, tmpbuf, "\n: ");
911 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
914 el->el_map.current = el->el_map.key;
915 re_clear_display(el);