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 * $NetBSD: common.c,v 1.19 2006/03/06 21:11:56 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)common.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * common.c: Common Editor functions
48 * Indicate end of file
53 ed_end_of_file(EditLine *el, int c __unused)
57 *el->el_line.lastchar = '\0';
63 * Add character to the line
64 * Insert a character [bound to all insert keys]
67 ed_insert(EditLine *el, int c)
69 int count = el->el_state.argument;
74 if (el->el_line.lastchar + el->el_state.argument >=
76 /* end of buffer space, try to allocate more */
77 if (!ch_enlargebufs(el, (size_t) count))
78 return CC_ERROR; /* error allocating more */
82 if (el->el_state.inputmode == MODE_INSERT
83 || el->el_line.cursor >= el->el_line.lastchar)
86 *el->el_line.cursor++ = c;
87 re_fastaddc(el); /* fast refresh for one char. */
89 if (el->el_state.inputmode != MODE_REPLACE_1)
90 c_insert(el, el->el_state.argument);
92 while (count-- && el->el_line.cursor < el->el_line.lastchar)
93 *el->el_line.cursor++ = c;
97 if (el->el_state.inputmode == MODE_REPLACE_1)
98 return vi_command_mode(el, 0);
104 /* ed_delete_prev_word():
105 * Delete from beginning of current word to cursor
108 protected el_action_t
110 ed_delete_prev_word(EditLine *el, int c __unused)
114 if (el->el_line.cursor == el->el_line.buffer)
117 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
118 el->el_state.argument, ce__isword);
120 for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
122 el->el_chared.c_kill.last = kp;
124 c_delbefore(el, el->el_line.cursor - cp); /* delete before dot */
125 el->el_line.cursor = cp;
126 if (el->el_line.cursor < el->el_line.buffer)
127 el->el_line.cursor = el->el_line.buffer; /* bounds check */
132 /* ed_delete_next_char():
133 * Delete character under cursor
136 protected el_action_t
138 ed_delete_next_char(EditLine *el, int c)
140 #ifdef notdef /* XXX */
141 #define EL el->el_line
142 (void) fprintf(el->el_errlfile,
143 "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
144 EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
145 EL.lastchar, EL.limit, EL.limit);
147 if (el->el_line.cursor == el->el_line.lastchar) {
148 /* if I'm at the end */
149 if (el->el_map.type == MAP_VI) {
150 if (el->el_line.cursor == el->el_line.buffer) {
151 /* if I'm also at the beginning */
156 term_writechar(el, c);
161 el->el_line.cursor--;
167 if (el->el_line.cursor != el->el_line.buffer)
168 el->el_line.cursor--;
173 c_delafter(el, el->el_state.argument); /* delete after dot */
174 if (el->el_line.cursor >= el->el_line.lastchar &&
175 el->el_line.cursor > el->el_line.buffer)
177 el->el_line.cursor = el->el_line.lastchar - 1;
183 * Cut to the end of line
186 protected el_action_t
188 ed_kill_line(EditLine *el, int c __unused)
192 cp = el->el_line.cursor;
193 kp = el->el_chared.c_kill.buf;
194 while (cp < el->el_line.lastchar)
195 *kp++ = *cp++; /* copy it */
196 el->el_chared.c_kill.last = kp;
197 /* zap! -- delete to end */
198 el->el_line.lastchar = el->el_line.cursor;
204 * Move cursor to the end of line
207 protected el_action_t
209 ed_move_to_end(EditLine *el, int c __unused)
212 el->el_line.cursor = el->el_line.lastchar;
213 if (el->el_map.type == MAP_VI) {
215 el->el_line.cursor--;
217 if (el->el_chared.c_vcmd.action != NOP) {
227 * Move cursor to the beginning of line
230 protected el_action_t
232 ed_move_to_beg(EditLine *el, int c __unused)
235 el->el_line.cursor = el->el_line.buffer;
237 if (el->el_map.type == MAP_VI) {
238 /* We want FIRST non space character */
239 while (isspace((unsigned char) *el->el_line.cursor))
240 el->el_line.cursor++;
241 if (el->el_chared.c_vcmd.action != NOP) {
250 /* ed_transpose_chars():
251 * Exchange the character to the left of the cursor with the one under it
254 protected el_action_t
255 ed_transpose_chars(EditLine *el, int c)
258 if (el->el_line.cursor < el->el_line.lastchar) {
259 if (el->el_line.lastchar <= &el->el_line.buffer[1])
262 el->el_line.cursor++;
264 if (el->el_line.cursor > &el->el_line.buffer[1]) {
265 /* must have at least two chars entered */
266 c = el->el_line.cursor[-2];
267 el->el_line.cursor[-2] = el->el_line.cursor[-1];
268 el->el_line.cursor[-1] = c;
276 * Move to the right one character
279 protected el_action_t
281 ed_next_char(EditLine *el, int c __unused)
283 char *lim = el->el_line.lastchar;
285 if (el->el_line.cursor >= lim ||
286 (el->el_line.cursor == lim - 1 &&
287 el->el_map.type == MAP_VI &&
288 el->el_chared.c_vcmd.action == NOP))
291 el->el_line.cursor += el->el_state.argument;
292 if (el->el_line.cursor > lim)
293 el->el_line.cursor = lim;
295 if (el->el_map.type == MAP_VI)
296 if (el->el_chared.c_vcmd.action != NOP) {
305 * Move to the beginning of the current word
308 protected el_action_t
310 ed_prev_word(EditLine *el, int c __unused)
313 if (el->el_line.cursor == el->el_line.buffer)
316 el->el_line.cursor = c__prev_word(el->el_line.cursor,
318 el->el_state.argument,
321 if (el->el_map.type == MAP_VI)
322 if (el->el_chared.c_vcmd.action != NOP) {
331 * Move to the left one character
334 protected el_action_t
336 ed_prev_char(EditLine *el, int c __unused)
339 if (el->el_line.cursor > el->el_line.buffer) {
340 el->el_line.cursor -= el->el_state.argument;
341 if (el->el_line.cursor < el->el_line.buffer)
342 el->el_line.cursor = el->el_line.buffer;
344 if (el->el_map.type == MAP_VI)
345 if (el->el_chared.c_vcmd.action != NOP) {
355 /* ed_quoted_insert():
356 * Add the next character typed verbatim
359 protected el_action_t
360 ed_quoted_insert(EditLine *el, int c)
366 num = el_getc(el, &tc);
367 c = (unsigned char) tc;
370 return (ed_insert(el, c));
372 return (ed_end_of_file(el, 0));
377 * Adds to argument or enters a digit
379 protected el_action_t
380 ed_digit(EditLine *el, int c)
383 if (!isdigit((unsigned char) c))
386 if (el->el_state.doingarg) {
387 /* if doing an arg, add this in... */
388 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
389 el->el_state.argument = c - '0';
391 if (el->el_state.argument > 1000000)
393 el->el_state.argument =
394 (el->el_state.argument * 10) + (c - '0');
399 return ed_insert(el, c);
403 /* ed_argument_digit():
404 * Digit that starts argument
407 protected el_action_t
408 ed_argument_digit(EditLine *el, int c)
411 if (!isdigit((unsigned char) c))
414 if (el->el_state.doingarg) {
415 if (el->el_state.argument > 1000000)
417 el->el_state.argument = (el->el_state.argument * 10) +
419 } else { /* else starting an argument */
420 el->el_state.argument = c - '0';
421 el->el_state.doingarg = 1;
428 * Indicates unbound character
429 * Bound to keys that are not assigned
431 protected el_action_t
433 ed_unassigned(EditLine *el, int c __unused)
445 * Tty interrupt character
448 protected el_action_t
450 ed_tty_sigint(EditLine *el __unused,
459 * Tty delayed suspend character
462 protected el_action_t
464 ed_tty_dsusp(EditLine *el __unused,
472 /* ed_tty_flush_output():
473 * Tty flush output characters
476 protected el_action_t
478 ed_tty_flush_output(EditLine *el __unused,
490 protected el_action_t
492 ed_tty_sigquit(EditLine *el __unused,
501 * Tty suspend character
504 protected el_action_t
506 ed_tty_sigtstp(EditLine *el __unused,
514 /* ed_tty_stop_output():
515 * Tty disallow output characters
518 protected el_action_t
520 ed_tty_stop_output(EditLine *el __unused,
528 /* ed_tty_start_output():
529 * Tty allow output characters
532 protected el_action_t
534 ed_tty_start_output(EditLine *el __unused,
546 protected el_action_t
548 ed_newline(EditLine *el, int c __unused)
552 *el->el_line.lastchar++ = '\n';
553 *el->el_line.lastchar = '\0';
558 /* ed_delete_prev_char():
559 * Delete the character to the left of the cursor
562 protected el_action_t
564 ed_delete_prev_char(EditLine *el, int c __unused)
567 if (el->el_line.cursor <= el->el_line.buffer)
570 c_delbefore(el, el->el_state.argument);
571 el->el_line.cursor -= el->el_state.argument;
572 if (el->el_line.cursor < el->el_line.buffer)
573 el->el_line.cursor = el->el_line.buffer;
578 /* ed_clear_screen():
579 * Clear screen leaving current line at the top
582 protected el_action_t
584 ed_clear_screen(EditLine *el, int c __unused)
587 term_clear_screen(el); /* clear the whole real screen */
588 re_clear_display(el); /* reset everything */
594 * Redisplay everything
597 protected el_action_t
599 ed_redisplay(EditLine *el __unused,
603 return (CC_REDISPLAY);
608 * Erase current line and start from scratch
611 protected el_action_t
613 ed_start_over(EditLine *el, int c __unused)
621 /* ed_sequence_lead_in():
622 * First character in a bound sequence
623 * Placeholder for external keys
625 protected el_action_t
627 ed_sequence_lead_in(EditLine *el __unused,
635 /* ed_prev_history():
636 * Move to the previous history line
639 protected el_action_t
641 ed_prev_history(EditLine *el, int c __unused)
644 int sv_event = el->el_history.eventno;
646 el->el_chared.c_undo.len = -1;
647 *el->el_line.lastchar = '\0'; /* just in case */
649 if (el->el_history.eventno == 0) { /* save the current buffer
651 (void) strncpy(el->el_history.buf, el->el_line.buffer,
653 el->el_history.last = el->el_history.buf +
654 (el->el_line.lastchar - el->el_line.buffer);
656 el->el_history.eventno += el->el_state.argument;
658 if (hist_get(el) == CC_ERROR) {
659 if (el->el_map.type == MAP_VI) {
660 el->el_history.eventno = sv_event;
664 /* el->el_history.eventno was fixed by first call */
668 return CC_REFRESH_BEEP;
673 /* ed_next_history():
674 * Move to the next history line
677 protected el_action_t
679 ed_next_history(EditLine *el, int c __unused)
681 el_action_t beep = CC_REFRESH, rval;
683 el->el_chared.c_undo.len = -1;
684 *el->el_line.lastchar = '\0'; /* just in case */
686 el->el_history.eventno -= el->el_state.argument;
688 if (el->el_history.eventno < 0) {
689 el->el_history.eventno = 0;
690 beep = CC_REFRESH_BEEP;
693 if (rval == CC_REFRESH)
700 /* ed_search_prev_history():
701 * Search previous in history for a line matching the current
702 * next search history [M-P] [K]
704 protected el_action_t
706 ed_search_prev_history(EditLine *el, int c __unused)
712 el->el_chared.c_vcmd.action = NOP;
713 el->el_chared.c_undo.len = -1;
714 *el->el_line.lastchar = '\0'; /* just in case */
715 if (el->el_history.eventno < 0) {
717 (void) fprintf(el->el_errfile,
718 "e_prev_search_hist(): eventno < 0;\n");
720 el->el_history.eventno = 0;
723 if (el->el_history.eventno == 0) {
724 (void) strncpy(el->el_history.buf, el->el_line.buffer,
726 el->el_history.last = el->el_history.buf +
727 (el->el_line.lastchar - el->el_line.buffer);
729 if (el->el_history.ref == NULL)
736 c_setpat(el); /* Set search pattern !! */
738 for (h = 1; h <= el->el_history.eventno; h++)
743 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
745 if ((strncmp(hp, el->el_line.buffer, (size_t)
746 (el->el_line.lastchar - el->el_line.buffer)) ||
747 hp[el->el_line.lastchar - el->el_line.buffer]) &&
758 (void) fprintf(el->el_errfile, "not found\n");
762 el->el_history.eventno = h;
764 return (hist_get(el));
768 /* ed_search_next_history():
769 * Search next in history for a line matching the current
772 protected el_action_t
774 ed_search_next_history(EditLine *el, int c __unused)
780 el->el_chared.c_vcmd.action = NOP;
781 el->el_chared.c_undo.len = -1;
782 *el->el_line.lastchar = '\0'; /* just in case */
784 if (el->el_history.eventno == 0)
787 if (el->el_history.ref == NULL)
794 c_setpat(el); /* Set search pattern !! */
796 for (h = 1; h < el->el_history.eventno && hp; h++) {
798 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
800 if ((strncmp(hp, el->el_line.buffer, (size_t)
801 (el->el_line.lastchar - el->el_line.buffer)) ||
802 hp[el->el_line.lastchar - el->el_line.buffer]) &&
808 if (!found) { /* is it the current history number? */
809 if (!c_hmatch(el, el->el_history.buf)) {
811 (void) fprintf(el->el_errfile, "not found\n");
816 el->el_history.eventno = found;
818 return (hist_get(el));
826 protected el_action_t
828 ed_prev_line(EditLine *el, int c __unused)
831 int nchars = c_hpos(el);
834 * Move to the line requested
836 if (*(ptr = el->el_line.cursor) == '\n')
839 for (; ptr >= el->el_line.buffer; ptr--)
840 if (*ptr == '\n' && --el->el_state.argument <= 0)
843 if (el->el_state.argument > 0)
847 * Move to the beginning of the line
849 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
853 * Move to the character requested
856 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
860 el->el_line.cursor = ptr;
869 protected el_action_t
871 ed_next_line(EditLine *el, int c __unused)
874 int nchars = c_hpos(el);
877 * Move to the line requested
879 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
880 if (*ptr == '\n' && --el->el_state.argument <= 0)
883 if (el->el_state.argument > 0)
887 * Move to the character requested
890 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
894 el->el_line.cursor = ptr;
900 * Editline extended command
903 protected el_action_t
905 ed_command(EditLine *el, int c __unused)
907 char tmpbuf[EL_BUFSIZ];
910 tmplen = c_gets(el, tmpbuf, "\n: ");
913 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
916 el->el_map.current = el->el_map.key;
917 re_clear_display(el);