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.18 2005/08/08 14:05:37 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 __unused)
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 */
155 term_overwrite(el, STReof, 4);
162 el->el_line.cursor--;
168 if (el->el_line.cursor != el->el_line.buffer)
169 el->el_line.cursor--;
174 c_delafter(el, el->el_state.argument); /* delete after dot */
175 if (el->el_line.cursor >= el->el_line.lastchar &&
176 el->el_line.cursor > el->el_line.buffer)
178 el->el_line.cursor = el->el_line.lastchar - 1;
184 * Cut to the end of line
187 protected el_action_t
189 ed_kill_line(EditLine *el, int c __unused)
193 cp = el->el_line.cursor;
194 kp = el->el_chared.c_kill.buf;
195 while (cp < el->el_line.lastchar)
196 *kp++ = *cp++; /* copy it */
197 el->el_chared.c_kill.last = kp;
198 /* zap! -- delete to end */
199 el->el_line.lastchar = el->el_line.cursor;
205 * Move cursor to the end of line
208 protected el_action_t
210 ed_move_to_end(EditLine *el, int c __unused)
213 el->el_line.cursor = el->el_line.lastchar;
214 if (el->el_map.type == MAP_VI) {
216 el->el_line.cursor--;
218 if (el->el_chared.c_vcmd.action != NOP) {
228 * Move cursor to the beginning of line
231 protected el_action_t
233 ed_move_to_beg(EditLine *el, int c __unused)
236 el->el_line.cursor = el->el_line.buffer;
238 if (el->el_map.type == MAP_VI) {
239 /* We want FIRST non space character */
240 while (isspace((unsigned char) *el->el_line.cursor))
241 el->el_line.cursor++;
242 if (el->el_chared.c_vcmd.action != NOP) {
251 /* ed_transpose_chars():
252 * Exchange the character to the left of the cursor with the one under it
255 protected el_action_t
256 ed_transpose_chars(EditLine *el, int c)
259 if (el->el_line.cursor < el->el_line.lastchar) {
260 if (el->el_line.lastchar <= &el->el_line.buffer[1])
263 el->el_line.cursor++;
265 if (el->el_line.cursor > &el->el_line.buffer[1]) {
266 /* must have at least two chars entered */
267 c = el->el_line.cursor[-2];
268 el->el_line.cursor[-2] = el->el_line.cursor[-1];
269 el->el_line.cursor[-1] = c;
277 * Move to the right one character
280 protected el_action_t
282 ed_next_char(EditLine *el, int c __unused)
284 char *lim = el->el_line.lastchar;
286 if (el->el_line.cursor >= lim ||
287 (el->el_line.cursor == lim - 1 &&
288 el->el_map.type == MAP_VI &&
289 el->el_chared.c_vcmd.action == NOP))
292 el->el_line.cursor += el->el_state.argument;
293 if (el->el_line.cursor > lim)
294 el->el_line.cursor = lim;
296 if (el->el_map.type == MAP_VI)
297 if (el->el_chared.c_vcmd.action != NOP) {
306 * Move to the beginning of the current word
309 protected el_action_t
311 ed_prev_word(EditLine *el, int c __unused)
314 if (el->el_line.cursor == el->el_line.buffer)
317 el->el_line.cursor = c__prev_word(el->el_line.cursor,
319 el->el_state.argument,
322 if (el->el_map.type == MAP_VI)
323 if (el->el_chared.c_vcmd.action != NOP) {
332 * Move to the left one character
335 protected el_action_t
337 ed_prev_char(EditLine *el, int c __unused)
340 if (el->el_line.cursor > el->el_line.buffer) {
341 el->el_line.cursor -= el->el_state.argument;
342 if (el->el_line.cursor < el->el_line.buffer)
343 el->el_line.cursor = el->el_line.buffer;
345 if (el->el_map.type == MAP_VI)
346 if (el->el_chared.c_vcmd.action != NOP) {
356 /* ed_quoted_insert():
357 * Add the next character typed verbatim
360 protected el_action_t
361 ed_quoted_insert(EditLine *el, int c)
367 num = el_getc(el, &tc);
368 c = (unsigned char) tc;
371 return (ed_insert(el, c));
373 return (ed_end_of_file(el, 0));
378 * Adds to argument or enters a digit
380 protected el_action_t
381 ed_digit(EditLine *el, int c)
384 if (!isdigit((unsigned char) c))
387 if (el->el_state.doingarg) {
388 /* if doing an arg, add this in... */
389 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
390 el->el_state.argument = c - '0';
392 if (el->el_state.argument > 1000000)
394 el->el_state.argument =
395 (el->el_state.argument * 10) + (c - '0');
400 return ed_insert(el, c);
404 /* ed_argument_digit():
405 * Digit that starts argument
408 protected el_action_t
409 ed_argument_digit(EditLine *el, int c)
412 if (!isdigit((unsigned char) c))
415 if (el->el_state.doingarg) {
416 if (el->el_state.argument > 1000000)
418 el->el_state.argument = (el->el_state.argument * 10) +
420 } else { /* else starting an argument */
421 el->el_state.argument = c - '0';
422 el->el_state.doingarg = 1;
429 * Indicates unbound character
430 * Bound to keys that are not assigned
432 protected el_action_t
434 ed_unassigned(EditLine *el, int c __unused)
446 * Tty interrupt character
449 protected el_action_t
451 ed_tty_sigint(EditLine *el __unused,
460 * Tty delayed suspend character
463 protected el_action_t
465 ed_tty_dsusp(EditLine *el __unused,
473 /* ed_tty_flush_output():
474 * Tty flush output characters
477 protected el_action_t
479 ed_tty_flush_output(EditLine *el __unused,
491 protected el_action_t
493 ed_tty_sigquit(EditLine *el __unused,
502 * Tty suspend character
505 protected el_action_t
507 ed_tty_sigtstp(EditLine *el __unused,
515 /* ed_tty_stop_output():
516 * Tty disallow output characters
519 protected el_action_t
521 ed_tty_stop_output(EditLine *el __unused,
529 /* ed_tty_start_output():
530 * Tty allow output characters
533 protected el_action_t
535 ed_tty_start_output(EditLine *el __unused,
547 protected el_action_t
549 ed_newline(EditLine *el, int c __unused)
553 *el->el_line.lastchar++ = '\n';
554 *el->el_line.lastchar = '\0';
559 /* ed_delete_prev_char():
560 * Delete the character to the left of the cursor
563 protected el_action_t
565 ed_delete_prev_char(EditLine *el, int c __unused)
568 if (el->el_line.cursor <= el->el_line.buffer)
571 c_delbefore(el, el->el_state.argument);
572 el->el_line.cursor -= el->el_state.argument;
573 if (el->el_line.cursor < el->el_line.buffer)
574 el->el_line.cursor = el->el_line.buffer;
579 /* ed_clear_screen():
580 * Clear screen leaving current line at the top
583 protected el_action_t
585 ed_clear_screen(EditLine *el, int c __unused)
588 term_clear_screen(el); /* clear the whole real screen */
589 re_clear_display(el); /* reset everything */
595 * Redisplay everything
598 protected el_action_t
600 ed_redisplay(EditLine *el __unused,
604 return (CC_REDISPLAY);
609 * Erase current line and start from scratch
612 protected el_action_t
614 ed_start_over(EditLine *el, int c __unused)
622 /* ed_sequence_lead_in():
623 * First character in a bound sequence
624 * Placeholder for external keys
626 protected el_action_t
628 ed_sequence_lead_in(EditLine *el __unused,
636 /* ed_prev_history():
637 * Move to the previous history line
640 protected el_action_t
642 ed_prev_history(EditLine *el, int c __unused)
645 int sv_event = el->el_history.eventno;
647 el->el_chared.c_undo.len = -1;
648 *el->el_line.lastchar = '\0'; /* just in case */
650 if (el->el_history.eventno == 0) { /* save the current buffer
652 (void) strncpy(el->el_history.buf, el->el_line.buffer,
654 el->el_history.last = el->el_history.buf +
655 (el->el_line.lastchar - el->el_line.buffer);
657 el->el_history.eventno += el->el_state.argument;
659 if (hist_get(el) == CC_ERROR) {
660 if (el->el_map.type == MAP_VI) {
661 el->el_history.eventno = sv_event;
665 /* el->el_history.eventno was fixed by first call */
669 return CC_REFRESH_BEEP;
674 /* ed_next_history():
675 * Move to the next history line
678 protected el_action_t
680 ed_next_history(EditLine *el, int c __unused)
682 el_action_t beep = CC_REFRESH, rval;
684 el->el_chared.c_undo.len = -1;
685 *el->el_line.lastchar = '\0'; /* just in case */
687 el->el_history.eventno -= el->el_state.argument;
689 if (el->el_history.eventno < 0) {
690 el->el_history.eventno = 0;
691 beep = CC_REFRESH_BEEP;
694 if (rval == CC_REFRESH)
701 /* ed_search_prev_history():
702 * Search previous in history for a line matching the current
703 * next search history [M-P] [K]
705 protected el_action_t
707 ed_search_prev_history(EditLine *el, int c __unused)
713 el->el_chared.c_vcmd.action = NOP;
714 el->el_chared.c_undo.len = -1;
715 *el->el_line.lastchar = '\0'; /* just in case */
716 if (el->el_history.eventno < 0) {
718 (void) fprintf(el->el_errfile,
719 "e_prev_search_hist(): eventno < 0;\n");
721 el->el_history.eventno = 0;
724 if (el->el_history.eventno == 0) {
725 (void) strncpy(el->el_history.buf, el->el_line.buffer,
727 el->el_history.last = el->el_history.buf +
728 (el->el_line.lastchar - el->el_line.buffer);
730 if (el->el_history.ref == NULL)
737 c_setpat(el); /* Set search pattern !! */
739 for (h = 1; h <= el->el_history.eventno; h++)
744 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
746 if ((strncmp(hp, el->el_line.buffer, (size_t)
747 (el->el_line.lastchar - el->el_line.buffer)) ||
748 hp[el->el_line.lastchar - el->el_line.buffer]) &&
759 (void) fprintf(el->el_errfile, "not found\n");
763 el->el_history.eventno = h;
765 return (hist_get(el));
769 /* ed_search_next_history():
770 * Search next in history for a line matching the current
773 protected el_action_t
775 ed_search_next_history(EditLine *el, int c __unused)
781 el->el_chared.c_vcmd.action = NOP;
782 el->el_chared.c_undo.len = -1;
783 *el->el_line.lastchar = '\0'; /* just in case */
785 if (el->el_history.eventno == 0)
788 if (el->el_history.ref == NULL)
795 c_setpat(el); /* Set search pattern !! */
797 for (h = 1; h < el->el_history.eventno && hp; h++) {
799 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
801 if ((strncmp(hp, el->el_line.buffer, (size_t)
802 (el->el_line.lastchar - el->el_line.buffer)) ||
803 hp[el->el_line.lastchar - el->el_line.buffer]) &&
809 if (!found) { /* is it the current history number? */
810 if (!c_hmatch(el, el->el_history.buf)) {
812 (void) fprintf(el->el_errfile, "not found\n");
817 el->el_history.eventno = found;
819 return (hist_get(el));
827 protected el_action_t
829 ed_prev_line(EditLine *el, int c __unused)
832 int nchars = c_hpos(el);
835 * Move to the line requested
837 if (*(ptr = el->el_line.cursor) == '\n')
840 for (; ptr >= el->el_line.buffer; ptr--)
841 if (*ptr == '\n' && --el->el_state.argument <= 0)
844 if (el->el_state.argument > 0)
848 * Move to the beginning of the line
850 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
854 * Move to the character requested
857 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
861 el->el_line.cursor = ptr;
870 protected el_action_t
872 ed_next_line(EditLine *el, int c __unused)
875 int nchars = c_hpos(el);
878 * Move to the line requested
880 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
881 if (*ptr == '\n' && --el->el_state.argument <= 0)
884 if (el->el_state.argument > 0)
888 * Move to the character requested
891 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
895 el->el_line.cursor = ptr;
901 * Editline extended command
904 protected el_action_t
906 ed_command(EditLine *el, int c __unused)
908 char tmpbuf[EL_BUFSIZ];
911 tmplen = c_gets(el, tmpbuf, "\n: ");
914 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
917 el->el_map.current = el->el_map.key;
918 re_clear_display(el);