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.19 2006/03/06 21:11:56 christos Exp $
34 * $DragonFly: src/lib/libedit/common.c,v 1.6 2007/05/05 00:27:39 pavalos 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)
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 */
153 term_writechar(el, c);
158 el->el_line.cursor--;
164 if (el->el_line.cursor != el->el_line.buffer)
165 el->el_line.cursor--;
170 c_delafter(el, el->el_state.argument); /* delete after dot */
171 if (el->el_line.cursor >= el->el_line.lastchar &&
172 el->el_line.cursor > el->el_line.buffer)
174 el->el_line.cursor = el->el_line.lastchar - 1;
180 * Cut to the end of line
183 protected el_action_t
185 ed_kill_line(EditLine *el, int c __attribute__((__unused__)))
189 cp = el->el_line.cursor;
190 kp = el->el_chared.c_kill.buf;
191 while (cp < el->el_line.lastchar)
192 *kp++ = *cp++; /* copy it */
193 el->el_chared.c_kill.last = kp;
194 /* zap! -- delete to end */
195 el->el_line.lastchar = el->el_line.cursor;
201 * Move cursor to the end of line
204 protected el_action_t
206 ed_move_to_end(EditLine *el, int c __attribute__((__unused__)))
209 el->el_line.cursor = el->el_line.lastchar;
210 if (el->el_map.type == MAP_VI) {
212 el->el_line.cursor--;
214 if (el->el_chared.c_vcmd.action != NOP) {
224 * Move cursor to the beginning of line
227 protected el_action_t
229 ed_move_to_beg(EditLine *el, int c __attribute__((__unused__)))
232 el->el_line.cursor = el->el_line.buffer;
234 if (el->el_map.type == MAP_VI) {
235 /* We want FIRST non space character */
236 while (isspace((unsigned char) *el->el_line.cursor))
237 el->el_line.cursor++;
238 if (el->el_chared.c_vcmd.action != NOP) {
247 /* ed_transpose_chars():
248 * Exchange the character to the left of the cursor with the one under it
251 protected el_action_t
252 ed_transpose_chars(EditLine *el, int c)
255 if (el->el_line.cursor < el->el_line.lastchar) {
256 if (el->el_line.lastchar <= &el->el_line.buffer[1])
259 el->el_line.cursor++;
261 if (el->el_line.cursor > &el->el_line.buffer[1]) {
262 /* must have at least two chars entered */
263 c = el->el_line.cursor[-2];
264 el->el_line.cursor[-2] = el->el_line.cursor[-1];
265 el->el_line.cursor[-1] = c;
273 * Move to the right one character
276 protected el_action_t
278 ed_next_char(EditLine *el, int c __attribute__((__unused__)))
280 char *lim = el->el_line.lastchar;
282 if (el->el_line.cursor >= lim ||
283 (el->el_line.cursor == lim - 1 &&
284 el->el_map.type == MAP_VI &&
285 el->el_chared.c_vcmd.action == NOP))
288 el->el_line.cursor += el->el_state.argument;
289 if (el->el_line.cursor > lim)
290 el->el_line.cursor = lim;
292 if (el->el_map.type == MAP_VI)
293 if (el->el_chared.c_vcmd.action != NOP) {
302 * Move to the beginning of the current word
305 protected el_action_t
307 ed_prev_word(EditLine *el, int c __attribute__((__unused__)))
310 if (el->el_line.cursor == el->el_line.buffer)
313 el->el_line.cursor = c__prev_word(el->el_line.cursor,
315 el->el_state.argument,
318 if (el->el_map.type == MAP_VI)
319 if (el->el_chared.c_vcmd.action != NOP) {
328 * Move to the left one character
331 protected el_action_t
333 ed_prev_char(EditLine *el, int c __attribute__((__unused__)))
336 if (el->el_line.cursor > el->el_line.buffer) {
337 el->el_line.cursor -= el->el_state.argument;
338 if (el->el_line.cursor < el->el_line.buffer)
339 el->el_line.cursor = el->el_line.buffer;
341 if (el->el_map.type == MAP_VI)
342 if (el->el_chared.c_vcmd.action != NOP) {
352 /* ed_quoted_insert():
353 * Add the next character typed verbatim
356 protected el_action_t
357 ed_quoted_insert(EditLine *el, int c)
363 num = el_getc(el, &tc);
364 c = (unsigned char) tc;
367 return (ed_insert(el, c));
369 return (ed_end_of_file(el, 0));
374 * Adds to argument or enters a digit
376 protected el_action_t
377 ed_digit(EditLine *el, int c)
383 if (el->el_state.doingarg) {
384 /* if doing an arg, add this in... */
385 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
386 el->el_state.argument = c - '0';
388 if (el->el_state.argument > 1000000)
390 el->el_state.argument =
391 (el->el_state.argument * 10) + (c - '0');
396 return ed_insert(el, c);
400 /* ed_argument_digit():
401 * Digit that starts argument
404 protected el_action_t
405 ed_argument_digit(EditLine *el, int c)
411 if (el->el_state.doingarg) {
412 if (el->el_state.argument > 1000000)
414 el->el_state.argument = (el->el_state.argument * 10) +
416 } else { /* else starting an argument */
417 el->el_state.argument = c - '0';
418 el->el_state.doingarg = 1;
425 * Indicates unbound character
426 * Bound to keys that are not assigned
428 protected el_action_t
430 ed_unassigned(EditLine *el, int c __attribute__((__unused__)))
442 * Tty interrupt character
445 protected el_action_t
447 ed_tty_sigint(EditLine *el __attribute__((__unused__)),
448 int c __attribute__((__unused__)))
456 * Tty delayed suspend character
459 protected el_action_t
461 ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
462 int c __attribute__((__unused__)))
469 /* ed_tty_flush_output():
470 * Tty flush output characters
473 protected el_action_t
475 ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
476 int c __attribute__((__unused__)))
487 protected el_action_t
489 ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
490 int c __attribute__((__unused__)))
498 * Tty suspend character
501 protected el_action_t
503 ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
504 int c __attribute__((__unused__)))
511 /* ed_tty_stop_output():
512 * Tty disallow output characters
515 protected el_action_t
517 ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
518 int c __attribute__((__unused__)))
525 /* ed_tty_start_output():
526 * Tty allow output characters
529 protected el_action_t
531 ed_tty_start_output(EditLine *el __attribute__((__unused__)),
532 int c __attribute__((__unused__)))
543 protected el_action_t
545 ed_newline(EditLine *el, int c __attribute__((__unused__)))
549 *el->el_line.lastchar++ = '\n';
550 *el->el_line.lastchar = '\0';
555 /* ed_delete_prev_char():
556 * Delete the character to the left of the cursor
559 protected el_action_t
561 ed_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
564 if (el->el_line.cursor <= el->el_line.buffer)
567 c_delbefore(el, el->el_state.argument);
568 el->el_line.cursor -= el->el_state.argument;
569 if (el->el_line.cursor < el->el_line.buffer)
570 el->el_line.cursor = el->el_line.buffer;
575 /* ed_clear_screen():
576 * Clear screen leaving current line at the top
579 protected el_action_t
581 ed_clear_screen(EditLine *el, int c __attribute__((__unused__)))
584 term_clear_screen(el); /* clear the whole real screen */
585 re_clear_display(el); /* reset everything */
591 * Redisplay everything
594 protected el_action_t
596 ed_redisplay(EditLine *el __attribute__((__unused__)),
597 int c __attribute__((__unused__)))
600 return (CC_REDISPLAY);
605 * Erase current line and start from scratch
608 protected el_action_t
610 ed_start_over(EditLine *el, int c __attribute__((__unused__)))
618 /* ed_sequence_lead_in():
619 * First character in a bound sequence
620 * Placeholder for external keys
622 protected el_action_t
624 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
625 int c __attribute__((__unused__)))
632 /* ed_prev_history():
633 * Move to the previous history line
636 protected el_action_t
638 ed_prev_history(EditLine *el, int c __attribute__((__unused__)))
641 int sv_event = el->el_history.eventno;
643 el->el_chared.c_undo.len = -1;
644 *el->el_line.lastchar = '\0'; /* just in case */
646 if (el->el_history.eventno == 0) { /* save the current buffer
648 (void) strncpy(el->el_history.buf, el->el_line.buffer,
650 el->el_history.last = el->el_history.buf +
651 (el->el_line.lastchar - el->el_line.buffer);
653 el->el_history.eventno += el->el_state.argument;
655 if (hist_get(el) == CC_ERROR) {
656 if (el->el_map.type == MAP_VI) {
657 el->el_history.eventno = sv_event;
661 /* el->el_history.eventno was fixed by first call */
665 return CC_REFRESH_BEEP;
670 /* ed_next_history():
671 * Move to the next history line
674 protected el_action_t
676 ed_next_history(EditLine *el, int c __attribute__((__unused__)))
678 el_action_t beep = CC_REFRESH, rval;
680 el->el_chared.c_undo.len = -1;
681 *el->el_line.lastchar = '\0'; /* just in case */
683 el->el_history.eventno -= el->el_state.argument;
685 if (el->el_history.eventno < 0) {
686 el->el_history.eventno = 0;
687 beep = CC_REFRESH_BEEP;
690 if (rval == CC_REFRESH)
697 /* ed_search_prev_history():
698 * Search previous in history for a line matching the current
699 * next search history [M-P] [K]
701 protected el_action_t
703 ed_search_prev_history(EditLine *el, int c __attribute__((__unused__)))
709 el->el_chared.c_vcmd.action = NOP;
710 el->el_chared.c_undo.len = -1;
711 *el->el_line.lastchar = '\0'; /* just in case */
712 if (el->el_history.eventno < 0) {
714 (void) fprintf(el->el_errfile,
715 "e_prev_search_hist(): eventno < 0;\n");
717 el->el_history.eventno = 0;
720 if (el->el_history.eventno == 0) {
721 (void) strncpy(el->el_history.buf, el->el_line.buffer,
723 el->el_history.last = el->el_history.buf +
724 (el->el_line.lastchar - el->el_line.buffer);
726 if (el->el_history.ref == NULL)
733 c_setpat(el); /* Set search pattern !! */
735 for (h = 1; h <= el->el_history.eventno; h++)
740 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
742 if ((strncmp(hp, el->el_line.buffer, (size_t)
743 (el->el_line.lastchar - el->el_line.buffer)) ||
744 hp[el->el_line.lastchar - el->el_line.buffer]) &&
755 (void) fprintf(el->el_errfile, "not found\n");
759 el->el_history.eventno = h;
761 return (hist_get(el));
765 /* ed_search_next_history():
766 * Search next in history for a line matching the current
769 protected el_action_t
771 ed_search_next_history(EditLine *el, int c __attribute__((__unused__)))
777 el->el_chared.c_vcmd.action = NOP;
778 el->el_chared.c_undo.len = -1;
779 *el->el_line.lastchar = '\0'; /* just in case */
781 if (el->el_history.eventno == 0)
784 if (el->el_history.ref == NULL)
791 c_setpat(el); /* Set search pattern !! */
793 for (h = 1; h < el->el_history.eventno && hp; h++) {
795 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
797 if ((strncmp(hp, el->el_line.buffer, (size_t)
798 (el->el_line.lastchar - el->el_line.buffer)) ||
799 hp[el->el_line.lastchar - el->el_line.buffer]) &&
805 if (!found) { /* is it the current history number? */
806 if (!c_hmatch(el, el->el_history.buf)) {
808 (void) fprintf(el->el_errfile, "not found\n");
813 el->el_history.eventno = found;
815 return (hist_get(el));
823 protected el_action_t
825 ed_prev_line(EditLine *el, int c __attribute__((__unused__)))
828 int nchars = c_hpos(el);
831 * Move to the line requested
833 if (*(ptr = el->el_line.cursor) == '\n')
836 for (; ptr >= el->el_line.buffer; ptr--)
837 if (*ptr == '\n' && --el->el_state.argument <= 0)
840 if (el->el_state.argument > 0)
844 * Move to the beginning of the line
846 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
850 * Move to the character requested
853 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
857 el->el_line.cursor = ptr;
866 protected el_action_t
868 ed_next_line(EditLine *el, int c __attribute__((__unused__)))
871 int nchars = c_hpos(el);
874 * Move to the line requested
876 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
877 if (*ptr == '\n' && --el->el_state.argument <= 0)
880 if (el->el_state.argument > 0)
884 * Move to the character requested
887 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
891 el->el_line.cursor = ptr;
897 * Editline extended command
900 protected el_action_t
902 ed_command(EditLine *el, int c __attribute__((__unused__)))
904 char tmpbuf[EL_BUFSIZ];
907 tmplen = c_gets(el, tmpbuf, "\n: ");
910 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
913 el->el_map.current = el->el_map.key;
914 re_clear_display(el);