1 /* $NetBSD: common.c,v 1.29 2012/03/24 20:08:43 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)common.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: common.c,v 1.29 2012/03/24 20:08:43 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * common.c: Common Editor functions
52 * Indicate end of file
57 ed_end_of_file(EditLine *el, Int c __attribute__((__unused__)))
61 *el->el_line.lastchar = '\0';
67 * Add character to the line
68 * Insert a character [bound to all insert keys]
71 ed_insert(EditLine *el, Int c)
73 int count = el->el_state.argument;
78 if (el->el_line.lastchar + el->el_state.argument >=
80 /* end of buffer space, try to allocate more */
81 if (!ch_enlargebufs(el, (size_t) count))
82 return CC_ERROR; /* error allocating more */
86 if (el->el_state.inputmode == MODE_INSERT
87 || el->el_line.cursor >= el->el_line.lastchar)
90 *el->el_line.cursor++ = c;
91 re_fastaddc(el); /* fast refresh for one char. */
93 if (el->el_state.inputmode != MODE_REPLACE_1)
94 c_insert(el, el->el_state.argument);
96 while (count-- && el->el_line.cursor < el->el_line.lastchar)
97 *el->el_line.cursor++ = c;
101 if (el->el_state.inputmode == MODE_REPLACE_1)
102 return vi_command_mode(el, 0);
108 /* ed_delete_prev_word():
109 * Delete from beginning of current word to cursor
112 protected el_action_t
114 ed_delete_prev_word(EditLine *el, Int c __attribute__((__unused__)))
118 if (el->el_line.cursor == el->el_line.buffer)
121 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
122 el->el_state.argument, ce__isword);
124 for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
126 el->el_chared.c_kill.last = kp;
128 c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
129 el->el_line.cursor = cp;
130 if (el->el_line.cursor < el->el_line.buffer)
131 el->el_line.cursor = el->el_line.buffer; /* bounds check */
136 /* ed_delete_next_char():
137 * Delete character under cursor
140 protected el_action_t
142 ed_delete_next_char(EditLine *el, Int c __attribute__((__unused__)))
145 #define EL el->el_line
146 (void) fprintf(el->el_errlfile,
147 "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
148 EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
149 EL.lastchar, EL.limit, EL.limit);
151 if (el->el_line.cursor == el->el_line.lastchar) {
152 /* if I'm at the end */
153 if (el->el_map.type == MAP_VI) {
154 if (el->el_line.cursor == el->el_line.buffer) {
155 /* if I'm also at the beginning */
160 terminal_writec(el, c);
165 el->el_line.cursor--;
173 c_delafter(el, el->el_state.argument); /* delete after dot */
174 if (el->el_map.type == MAP_VI &&
175 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 __attribute__((__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 __attribute__((__unused__)))
213 el->el_line.cursor = el->el_line.lastchar;
214 if (el->el_map.type == MAP_VI) {
215 if (el->el_chared.c_vcmd.action != NOP) {
220 el->el_line.cursor--;
228 * Move cursor to the beginning of line
231 protected el_action_t
233 ed_move_to_beg(EditLine *el, Int c __attribute__((__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(*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 __attribute__((__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 __attribute__((__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 __attribute__((__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 = FUN(el,getc)(el, &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)
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)
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 __attribute__((__unused__)),
435 Int c __attribute__((__unused__)))
447 * Tty interrupt character
450 protected el_action_t
452 ed_tty_sigint(EditLine *el __attribute__((__unused__)),
453 Int c __attribute__((__unused__)))
461 * Tty delayed suspend character
464 protected el_action_t
466 ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
467 Int c __attribute__((__unused__)))
474 /* ed_tty_flush_output():
475 * Tty flush output characters
478 protected el_action_t
480 ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
481 Int c __attribute__((__unused__)))
492 protected el_action_t
494 ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
495 Int c __attribute__((__unused__)))
503 * Tty suspend character
506 protected el_action_t
508 ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
509 Int c __attribute__((__unused__)))
516 /* ed_tty_stop_output():
517 * Tty disallow output characters
520 protected el_action_t
522 ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
523 Int c __attribute__((__unused__)))
530 /* ed_tty_start_output():
531 * Tty allow output characters
534 protected el_action_t
536 ed_tty_start_output(EditLine *el __attribute__((__unused__)),
537 Int c __attribute__((__unused__)))
548 protected el_action_t
550 ed_newline(EditLine *el, Int c __attribute__((__unused__)))
554 *el->el_line.lastchar++ = '\n';
555 *el->el_line.lastchar = '\0';
560 /* ed_delete_prev_char():
561 * Delete the character to the left of the cursor
564 protected el_action_t
566 ed_delete_prev_char(EditLine *el, Int c __attribute__((__unused__)))
569 if (el->el_line.cursor <= el->el_line.buffer)
572 c_delbefore(el, el->el_state.argument);
573 el->el_line.cursor -= el->el_state.argument;
574 if (el->el_line.cursor < el->el_line.buffer)
575 el->el_line.cursor = el->el_line.buffer;
580 /* ed_clear_screen():
581 * Clear screen leaving current line at the top
584 protected el_action_t
586 ed_clear_screen(EditLine *el, Int c __attribute__((__unused__)))
589 terminal_clear_screen(el); /* clear the whole real screen */
590 re_clear_display(el); /* reset everything */
596 * Redisplay everything
599 protected el_action_t
601 ed_redisplay(EditLine *el __attribute__((__unused__)),
602 Int c __attribute__((__unused__)))
610 * Erase current line and start from scratch
613 protected el_action_t
615 ed_start_over(EditLine *el, Int c __attribute__((__unused__)))
623 /* ed_sequence_lead_in():
624 * First character in a bound sequence
625 * Placeholder for external keys
627 protected el_action_t
629 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
630 Int c __attribute__((__unused__)))
637 /* ed_prev_history():
638 * Move to the previous history line
641 protected el_action_t
643 ed_prev_history(EditLine *el, Int c __attribute__((__unused__)))
646 int sv_event = el->el_history.eventno;
648 el->el_chared.c_undo.len = -1;
649 *el->el_line.lastchar = '\0'; /* just in case */
651 if (el->el_history.eventno == 0) { /* save the current buffer
653 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
655 el->el_history.last = el->el_history.buf +
656 (el->el_line.lastchar - el->el_line.buffer);
658 el->el_history.eventno += el->el_state.argument;
660 if (hist_get(el) == CC_ERROR) {
661 if (el->el_map.type == MAP_VI) {
662 el->el_history.eventno = sv_event;
666 /* el->el_history.eventno was fixed by first call */
670 return CC_REFRESH_BEEP;
675 /* ed_next_history():
676 * Move to the next history line
679 protected el_action_t
681 ed_next_history(EditLine *el, Int c __attribute__((__unused__)))
683 el_action_t beep = CC_REFRESH, rval;
685 el->el_chared.c_undo.len = -1;
686 *el->el_line.lastchar = '\0'; /* just in case */
688 el->el_history.eventno -= el->el_state.argument;
690 if (el->el_history.eventno < 0) {
691 el->el_history.eventno = 0;
692 beep = CC_REFRESH_BEEP;
695 if (rval == CC_REFRESH)
702 /* ed_search_prev_history():
703 * Search previous in history for a line matching the current
704 * next search history [M-P] [K]
706 protected el_action_t
708 ed_search_prev_history(EditLine *el, Int c __attribute__((__unused__)))
714 el->el_chared.c_vcmd.action = NOP;
715 el->el_chared.c_undo.len = -1;
716 *el->el_line.lastchar = '\0'; /* just in case */
717 if (el->el_history.eventno < 0) {
719 (void) fprintf(el->el_errfile,
720 "e_prev_search_hist(): eventno < 0;\n");
722 el->el_history.eventno = 0;
725 if (el->el_history.eventno == 0) {
726 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
728 el->el_history.last = el->el_history.buf +
729 (el->el_line.lastchar - el->el_line.buffer);
731 if (el->el_history.ref == NULL)
738 c_setpat(el); /* Set search pattern !! */
740 for (h = 1; h <= el->el_history.eventno; h++)
745 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
747 if ((Strncmp(hp, el->el_line.buffer, (size_t)
748 (el->el_line.lastchar - el->el_line.buffer)) ||
749 hp[el->el_line.lastchar - el->el_line.buffer]) &&
760 (void) fprintf(el->el_errfile, "not found\n");
764 el->el_history.eventno = h;
770 /* ed_search_next_history():
771 * Search next in history for a line matching the current
774 protected el_action_t
776 ed_search_next_history(EditLine *el, Int c __attribute__((__unused__)))
782 el->el_chared.c_vcmd.action = NOP;
783 el->el_chared.c_undo.len = -1;
784 *el->el_line.lastchar = '\0'; /* just in case */
786 if (el->el_history.eventno == 0)
789 if (el->el_history.ref == NULL)
796 c_setpat(el); /* Set search pattern !! */
798 for (h = 1; h < el->el_history.eventno && hp; h++) {
800 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
802 if ((Strncmp(hp, el->el_line.buffer, (size_t)
803 (el->el_line.lastchar - el->el_line.buffer)) ||
804 hp[el->el_line.lastchar - el->el_line.buffer]) &&
810 if (!found) { /* is it the current history number? */
811 if (!c_hmatch(el, el->el_history.buf)) {
813 (void) fprintf(el->el_errfile, "not found\n");
818 el->el_history.eventno = found;
828 protected el_action_t
830 ed_prev_line(EditLine *el, Int c __attribute__((__unused__)))
833 int nchars = c_hpos(el);
836 * Move to the line requested
838 if (*(ptr = el->el_line.cursor) == '\n')
841 for (; ptr >= el->el_line.buffer; ptr--)
842 if (*ptr == '\n' && --el->el_state.argument <= 0)
845 if (el->el_state.argument > 0)
849 * Move to the beginning of the line
851 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
855 * Move to the character requested
858 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
862 el->el_line.cursor = ptr;
871 protected el_action_t
873 ed_next_line(EditLine *el, Int c __attribute__((__unused__)))
876 int nchars = c_hpos(el);
879 * Move to the line requested
881 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
882 if (*ptr == '\n' && --el->el_state.argument <= 0)
885 if (el->el_state.argument > 0)
889 * Move to the character requested
892 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
896 el->el_line.cursor = ptr;
902 * Editline extended command
905 protected el_action_t
907 ed_command(EditLine *el, Int c __attribute__((__unused__)))
909 Char tmpbuf[EL_BUFSIZ];
912 tmplen = c_gets(el, tmpbuf, STR("\n: "));
913 terminal__putc(el, '\n');
915 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
918 el->el_map.current = el->el_map.key;
919 re_clear_display(el);