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.23 2009/02/27 04:18:45 msaitoh 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, (int)(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--;
169 c_delafter(el, el->el_state.argument); /* delete after dot */
170 if (el->el_map.type == MAP_VI &&
171 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 __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 __unused)
209 el->el_line.cursor = el->el_line.lastchar;
210 if (el->el_map.type == MAP_VI) {
211 if (el->el_chared.c_vcmd.action != NOP) {
221 * Move cursor to the beginning of line
224 protected el_action_t
226 ed_move_to_beg(EditLine *el, int c __unused)
229 el->el_line.cursor = el->el_line.buffer;
231 if (el->el_map.type == MAP_VI) {
232 /* We want FIRST non space character */
233 while (isspace((unsigned char) *el->el_line.cursor))
234 el->el_line.cursor++;
235 if (el->el_chared.c_vcmd.action != NOP) {
244 /* ed_transpose_chars():
245 * Exchange the character to the left of the cursor with the one under it
248 protected el_action_t
249 ed_transpose_chars(EditLine *el, int c)
252 if (el->el_line.cursor < el->el_line.lastchar) {
253 if (el->el_line.lastchar <= &el->el_line.buffer[1])
256 el->el_line.cursor++;
258 if (el->el_line.cursor > &el->el_line.buffer[1]) {
259 /* must have at least two chars entered */
260 c = el->el_line.cursor[-2];
261 el->el_line.cursor[-2] = el->el_line.cursor[-1];
262 el->el_line.cursor[-1] = c;
270 * Move to the right one character
273 protected el_action_t
275 ed_next_char(EditLine *el, int c __unused)
277 char *lim = el->el_line.lastchar;
279 if (el->el_line.cursor >= lim ||
280 (el->el_line.cursor == lim - 1 &&
281 el->el_map.type == MAP_VI &&
282 el->el_chared.c_vcmd.action == NOP))
285 el->el_line.cursor += el->el_state.argument;
286 if (el->el_line.cursor > lim)
287 el->el_line.cursor = lim;
289 if (el->el_map.type == MAP_VI)
290 if (el->el_chared.c_vcmd.action != NOP) {
299 * Move to the beginning of the current word
302 protected el_action_t
304 ed_prev_word(EditLine *el, int c __unused)
307 if (el->el_line.cursor == el->el_line.buffer)
310 el->el_line.cursor = c__prev_word(el->el_line.cursor,
312 el->el_state.argument,
315 if (el->el_map.type == MAP_VI)
316 if (el->el_chared.c_vcmd.action != NOP) {
325 * Move to the left one character
328 protected el_action_t
330 ed_prev_char(EditLine *el, int c __unused)
333 if (el->el_line.cursor > el->el_line.buffer) {
334 el->el_line.cursor -= el->el_state.argument;
335 if (el->el_line.cursor < el->el_line.buffer)
336 el->el_line.cursor = el->el_line.buffer;
338 if (el->el_map.type == MAP_VI)
339 if (el->el_chared.c_vcmd.action != NOP) {
349 /* ed_quoted_insert():
350 * Add the next character typed verbatim
353 protected el_action_t
354 ed_quoted_insert(EditLine *el, int c)
360 num = el_getc(el, &tc);
361 c = (unsigned char) tc;
364 return (ed_insert(el, c));
366 return (ed_end_of_file(el, 0));
371 * Adds to argument or enters a digit
373 protected el_action_t
374 ed_digit(EditLine *el, int c)
377 if (!isdigit((unsigned char) c))
380 if (el->el_state.doingarg) {
381 /* if doing an arg, add this in... */
382 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
383 el->el_state.argument = c - '0';
385 if (el->el_state.argument > 1000000)
387 el->el_state.argument =
388 (el->el_state.argument * 10) + (c - '0');
393 return ed_insert(el, c);
397 /* ed_argument_digit():
398 * Digit that starts argument
401 protected el_action_t
402 ed_argument_digit(EditLine *el, int c)
405 if (!isdigit((unsigned char) c))
408 if (el->el_state.doingarg) {
409 if (el->el_state.argument > 1000000)
411 el->el_state.argument = (el->el_state.argument * 10) +
413 } else { /* else starting an argument */
414 el->el_state.argument = c - '0';
415 el->el_state.doingarg = 1;
422 * Indicates unbound character
423 * Bound to keys that are not assigned
425 protected el_action_t
427 ed_unassigned(EditLine *el, int c __unused)
439 * Tty interrupt character
442 protected el_action_t
444 ed_tty_sigint(EditLine *el __unused,
453 * Tty delayed suspend character
456 protected el_action_t
458 ed_tty_dsusp(EditLine *el __unused,
466 /* ed_tty_flush_output():
467 * Tty flush output characters
470 protected el_action_t
472 ed_tty_flush_output(EditLine *el __unused,
484 protected el_action_t
486 ed_tty_sigquit(EditLine *el __unused,
495 * Tty suspend character
498 protected el_action_t
500 ed_tty_sigtstp(EditLine *el __unused,
508 /* ed_tty_stop_output():
509 * Tty disallow output characters
512 protected el_action_t
514 ed_tty_stop_output(EditLine *el __unused,
522 /* ed_tty_start_output():
523 * Tty allow output characters
526 protected el_action_t
528 ed_tty_start_output(EditLine *el __unused,
540 protected el_action_t
542 ed_newline(EditLine *el, int c __unused)
546 *el->el_line.lastchar++ = '\n';
547 *el->el_line.lastchar = '\0';
552 /* ed_delete_prev_char():
553 * Delete the character to the left of the cursor
556 protected el_action_t
558 ed_delete_prev_char(EditLine *el, int c __unused)
561 if (el->el_line.cursor <= el->el_line.buffer)
564 c_delbefore(el, el->el_state.argument);
565 el->el_line.cursor -= el->el_state.argument;
566 if (el->el_line.cursor < el->el_line.buffer)
567 el->el_line.cursor = el->el_line.buffer;
572 /* ed_clear_screen():
573 * Clear screen leaving current line at the top
576 protected el_action_t
578 ed_clear_screen(EditLine *el, int c __unused)
581 term_clear_screen(el); /* clear the whole real screen */
582 re_clear_display(el); /* reset everything */
588 * Redisplay everything
591 protected el_action_t
593 ed_redisplay(EditLine *el __unused,
597 return (CC_REDISPLAY);
602 * Erase current line and start from scratch
605 protected el_action_t
607 ed_start_over(EditLine *el, int c __unused)
615 /* ed_sequence_lead_in():
616 * First character in a bound sequence
617 * Placeholder for external keys
619 protected el_action_t
621 ed_sequence_lead_in(EditLine *el __unused,
629 /* ed_prev_history():
630 * Move to the previous history line
633 protected el_action_t
635 ed_prev_history(EditLine *el, int c __unused)
638 int sv_event = el->el_history.eventno;
640 el->el_chared.c_undo.len = -1;
641 *el->el_line.lastchar = '\0'; /* just in case */
643 if (el->el_history.eventno == 0) { /* save the current buffer
645 (void) strncpy(el->el_history.buf, el->el_line.buffer,
647 el->el_history.last = el->el_history.buf +
648 (el->el_line.lastchar - el->el_line.buffer);
650 el->el_history.eventno += el->el_state.argument;
652 if (hist_get(el) == CC_ERROR) {
653 if (el->el_map.type == MAP_VI) {
654 el->el_history.eventno = sv_event;
658 /* el->el_history.eventno was fixed by first call */
662 return CC_REFRESH_BEEP;
667 /* ed_next_history():
668 * Move to the next history line
671 protected el_action_t
673 ed_next_history(EditLine *el, int c __unused)
675 el_action_t beep = CC_REFRESH, rval;
677 el->el_chared.c_undo.len = -1;
678 *el->el_line.lastchar = '\0'; /* just in case */
680 el->el_history.eventno -= el->el_state.argument;
682 if (el->el_history.eventno < 0) {
683 el->el_history.eventno = 0;
684 beep = CC_REFRESH_BEEP;
687 if (rval == CC_REFRESH)
694 /* ed_search_prev_history():
695 * Search previous in history for a line matching the current
696 * next search history [M-P] [K]
698 protected el_action_t
700 ed_search_prev_history(EditLine *el, int c __unused)
706 el->el_chared.c_vcmd.action = NOP;
707 el->el_chared.c_undo.len = -1;
708 *el->el_line.lastchar = '\0'; /* just in case */
709 if (el->el_history.eventno < 0) {
711 (void) fprintf(el->el_errfile,
712 "e_prev_search_hist(): eventno < 0;\n");
714 el->el_history.eventno = 0;
717 if (el->el_history.eventno == 0) {
718 (void) strncpy(el->el_history.buf, el->el_line.buffer,
720 el->el_history.last = el->el_history.buf +
721 (el->el_line.lastchar - el->el_line.buffer);
723 if (el->el_history.ref == NULL)
730 c_setpat(el); /* Set search pattern !! */
732 for (h = 1; h <= el->el_history.eventno; h++)
737 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
739 if ((strncmp(hp, el->el_line.buffer, (size_t)
740 (el->el_line.lastchar - el->el_line.buffer)) ||
741 hp[el->el_line.lastchar - el->el_line.buffer]) &&
752 (void) fprintf(el->el_errfile, "not found\n");
756 el->el_history.eventno = h;
758 return (hist_get(el));
762 /* ed_search_next_history():
763 * Search next in history for a line matching the current
766 protected el_action_t
768 ed_search_next_history(EditLine *el, int c __unused)
774 el->el_chared.c_vcmd.action = NOP;
775 el->el_chared.c_undo.len = -1;
776 *el->el_line.lastchar = '\0'; /* just in case */
778 if (el->el_history.eventno == 0)
781 if (el->el_history.ref == NULL)
788 c_setpat(el); /* Set search pattern !! */
790 for (h = 1; h < el->el_history.eventno && hp; h++) {
792 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
794 if ((strncmp(hp, el->el_line.buffer, (size_t)
795 (el->el_line.lastchar - el->el_line.buffer)) ||
796 hp[el->el_line.lastchar - el->el_line.buffer]) &&
802 if (!found) { /* is it the current history number? */
803 if (!c_hmatch(el, el->el_history.buf)) {
805 (void) fprintf(el->el_errfile, "not found\n");
810 el->el_history.eventno = found;
812 return (hist_get(el));
820 protected el_action_t
822 ed_prev_line(EditLine *el, int c __unused)
825 int nchars = c_hpos(el);
828 * Move to the line requested
830 if (*(ptr = el->el_line.cursor) == '\n')
833 for (; ptr >= el->el_line.buffer; ptr--)
834 if (*ptr == '\n' && --el->el_state.argument <= 0)
837 if (el->el_state.argument > 0)
841 * Move to the beginning of the line
843 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
847 * Move to the character requested
850 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
854 el->el_line.cursor = ptr;
863 protected el_action_t
865 ed_next_line(EditLine *el, int c __unused)
868 int nchars = c_hpos(el);
871 * Move to the line requested
873 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
874 if (*ptr == '\n' && --el->el_state.argument <= 0)
877 if (el->el_state.argument > 0)
881 * Move to the character requested
884 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
888 el->el_line.cursor = ptr;
894 * Editline extended command
897 protected el_action_t
899 ed_command(EditLine *el, int c __unused)
901 char tmpbuf[EL_BUFSIZ];
904 tmplen = c_gets(el, tmpbuf, "\n: ");
905 term__putc(el, '\n');
907 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
910 el->el_map.current = el->el_map.key;
911 re_clear_display(el);