1 /* $NetBSD: common.c,v 1.40 2016/03/02 19:24:20 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.40 2016/03/02 19:24:20 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * common.c: Common Editor functions
58 * Indicate end of file
63 ed_end_of_file(EditLine *el, wint_t c __attribute__((__unused__)))
67 *el->el_line.lastchar = '\0';
73 * Add character to the line
74 * Insert a character [bound to all insert keys]
77 ed_insert(EditLine *el, wint_t c)
79 int count = el->el_state.argument;
84 if (el->el_line.lastchar + el->el_state.argument >=
86 /* end of buffer space, try to allocate more */
87 if (!ch_enlargebufs(el, (size_t) count))
88 return CC_ERROR; /* error allocating more */
92 if (el->el_state.inputmode == MODE_INSERT
93 || el->el_line.cursor >= el->el_line.lastchar)
96 *el->el_line.cursor++ = (Char)c;
97 re_fastaddc(el); /* fast refresh for one char. */
99 if (el->el_state.inputmode != MODE_REPLACE_1)
100 c_insert(el, el->el_state.argument);
102 while (count-- && el->el_line.cursor < el->el_line.lastchar)
103 *el->el_line.cursor++ = (Char)c;
107 if (el->el_state.inputmode == MODE_REPLACE_1)
108 return vi_command_mode(el, 0);
114 /* ed_delete_prev_word():
115 * Delete from beginning of current word to cursor
118 protected el_action_t
120 ed_delete_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
124 if (el->el_line.cursor == el->el_line.buffer)
127 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
128 el->el_state.argument, ce__isword);
130 for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
132 el->el_chared.c_kill.last = kp;
134 c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
135 el->el_line.cursor = cp;
136 if (el->el_line.cursor < el->el_line.buffer)
137 el->el_line.cursor = el->el_line.buffer; /* bounds check */
142 /* ed_delete_next_char():
143 * Delete character under cursor
146 protected el_action_t
148 ed_delete_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
151 #define EL el->el_line
152 (void) fprintf(el->el_errfile,
153 "\nD(b: %p(" FSTR ") c: %p(" FSTR ") last: %p(" FSTR
154 ") limit: %p(" FSTR ")\n",
155 EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
156 EL.lastchar, EL.limit, EL.limit);
158 if (el->el_line.cursor == el->el_line.lastchar) {
159 /* if I'm at the end */
160 if (el->el_map.type == MAP_VI) {
161 if (el->el_line.cursor == el->el_line.buffer) {
162 /* if I'm also at the beginning */
167 terminal_writec(el, c);
172 el->el_line.cursor--;
180 c_delafter(el, el->el_state.argument); /* delete after dot */
181 if (el->el_map.type == MAP_VI &&
182 el->el_line.cursor >= el->el_line.lastchar &&
183 el->el_line.cursor > el->el_line.buffer)
185 el->el_line.cursor = el->el_line.lastchar - 1;
191 * Cut to the end of line
194 protected el_action_t
196 ed_kill_line(EditLine *el, wint_t c __attribute__((__unused__)))
200 cp = el->el_line.cursor;
201 kp = el->el_chared.c_kill.buf;
202 while (cp < el->el_line.lastchar)
203 *kp++ = *cp++; /* copy it */
204 el->el_chared.c_kill.last = kp;
205 /* zap! -- delete to end */
206 el->el_line.lastchar = el->el_line.cursor;
212 * Move cursor to the end of line
215 protected el_action_t
217 ed_move_to_end(EditLine *el, wint_t c __attribute__((__unused__)))
220 el->el_line.cursor = el->el_line.lastchar;
221 if (el->el_map.type == MAP_VI) {
222 if (el->el_chared.c_vcmd.action != NOP) {
227 el->el_line.cursor--;
235 * Move cursor to the beginning of line
238 protected el_action_t
240 ed_move_to_beg(EditLine *el, wint_t c __attribute__((__unused__)))
243 el->el_line.cursor = el->el_line.buffer;
245 if (el->el_map.type == MAP_VI) {
246 /* We want FIRST non space character */
247 while (Isspace(*el->el_line.cursor))
248 el->el_line.cursor++;
249 if (el->el_chared.c_vcmd.action != NOP) {
258 /* ed_transpose_chars():
259 * Exchange the character to the left of the cursor with the one under it
262 protected el_action_t
263 ed_transpose_chars(EditLine *el, wint_t c)
266 if (el->el_line.cursor < el->el_line.lastchar) {
267 if (el->el_line.lastchar <= &el->el_line.buffer[1])
270 el->el_line.cursor++;
272 if (el->el_line.cursor > &el->el_line.buffer[1]) {
273 /* must have at least two chars entered */
274 c = el->el_line.cursor[-2];
275 el->el_line.cursor[-2] = el->el_line.cursor[-1];
276 el->el_line.cursor[-1] = (Char)c;
284 * Move to the right one character
287 protected el_action_t
289 ed_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
291 Char *lim = el->el_line.lastchar;
293 if (el->el_line.cursor >= lim ||
294 (el->el_line.cursor == lim - 1 &&
295 el->el_map.type == MAP_VI &&
296 el->el_chared.c_vcmd.action == NOP))
299 el->el_line.cursor += el->el_state.argument;
300 if (el->el_line.cursor > lim)
301 el->el_line.cursor = lim;
303 if (el->el_map.type == MAP_VI)
304 if (el->el_chared.c_vcmd.action != NOP) {
313 * Move to the beginning of the current word
316 protected el_action_t
318 ed_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
321 if (el->el_line.cursor == el->el_line.buffer)
324 el->el_line.cursor = c__prev_word(el->el_line.cursor,
326 el->el_state.argument,
329 if (el->el_map.type == MAP_VI)
330 if (el->el_chared.c_vcmd.action != NOP) {
339 * Move to the left one character
342 protected el_action_t
344 ed_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
347 if (el->el_line.cursor > el->el_line.buffer) {
348 el->el_line.cursor -= el->el_state.argument;
349 if (el->el_line.cursor < el->el_line.buffer)
350 el->el_line.cursor = el->el_line.buffer;
352 if (el->el_map.type == MAP_VI)
353 if (el->el_chared.c_vcmd.action != NOP) {
363 /* ed_quoted_insert():
364 * Add the next character typed verbatim
367 protected el_action_t
368 ed_quoted_insert(EditLine *el, wint_t c)
373 num = el_wgetc(el, &c);
376 return ed_insert(el, c);
378 return ed_end_of_file(el, 0);
383 * Adds to argument or enters a digit
385 protected el_action_t
386 ed_digit(EditLine *el, wint_t c)
392 if (el->el_state.doingarg) {
393 /* if doing an arg, add this in... */
394 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
395 el->el_state.argument = c - '0';
397 if (el->el_state.argument > 1000000)
399 el->el_state.argument =
400 (el->el_state.argument * 10) + (c - '0');
405 return ed_insert(el, c);
409 /* ed_argument_digit():
410 * Digit that starts argument
413 protected el_action_t
414 ed_argument_digit(EditLine *el, wint_t c)
420 if (el->el_state.doingarg) {
421 if (el->el_state.argument > 1000000)
423 el->el_state.argument = (el->el_state.argument * 10) +
425 } else { /* else starting an argument */
426 el->el_state.argument = c - '0';
427 el->el_state.doingarg = 1;
434 * Indicates unbound character
435 * Bound to keys that are not assigned
437 protected el_action_t
439 ed_unassigned(EditLine *el __attribute__((__unused__)),
440 wint_t c __attribute__((__unused__)))
452 * Tty interrupt character
455 protected el_action_t
457 ed_tty_sigint(EditLine *el __attribute__((__unused__)),
458 wint_t c __attribute__((__unused__)))
466 * Tty delayed suspend character
469 protected el_action_t
471 ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
472 wint_t c __attribute__((__unused__)))
479 /* ed_tty_flush_output():
480 * Tty flush output characters
483 protected el_action_t
485 ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
486 wint_t c __attribute__((__unused__)))
497 protected el_action_t
499 ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
500 wint_t c __attribute__((__unused__)))
508 * Tty suspend character
511 protected el_action_t
513 ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
514 wint_t c __attribute__((__unused__)))
521 /* ed_tty_stop_output():
522 * Tty disallow output characters
525 protected el_action_t
527 ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
528 wint_t c __attribute__((__unused__)))
535 /* ed_tty_start_output():
536 * Tty allow output characters
539 protected el_action_t
541 ed_tty_start_output(EditLine *el __attribute__((__unused__)),
542 wint_t c __attribute__((__unused__)))
553 protected el_action_t
555 ed_newline(EditLine *el, wint_t c __attribute__((__unused__)))
559 *el->el_line.lastchar++ = '\n';
560 *el->el_line.lastchar = '\0';
565 /* ed_delete_prev_char():
566 * Delete the character to the left of the cursor
569 protected el_action_t
571 ed_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
574 if (el->el_line.cursor <= el->el_line.buffer)
577 c_delbefore(el, el->el_state.argument);
578 el->el_line.cursor -= el->el_state.argument;
579 if (el->el_line.cursor < el->el_line.buffer)
580 el->el_line.cursor = el->el_line.buffer;
585 /* ed_clear_screen():
586 * Clear screen leaving current line at the top
589 protected el_action_t
591 ed_clear_screen(EditLine *el, wint_t c __attribute__((__unused__)))
594 terminal_clear_screen(el); /* clear the whole real screen */
595 re_clear_display(el); /* reset everything */
601 * Redisplay everything
604 protected el_action_t
606 ed_redisplay(EditLine *el __attribute__((__unused__)),
607 wint_t c __attribute__((__unused__)))
615 * Erase current line and start from scratch
618 protected el_action_t
620 ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
628 /* ed_sequence_lead_in():
629 * First character in a bound sequence
630 * Placeholder for external keys
632 protected el_action_t
634 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
635 wint_t c __attribute__((__unused__)))
642 /* ed_prev_history():
643 * Move to the previous history line
646 protected el_action_t
648 ed_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
651 int sv_event = el->el_history.eventno;
653 el->el_chared.c_undo.len = -1;
654 *el->el_line.lastchar = '\0'; /* just in case */
656 if (el->el_history.eventno == 0) { /* save the current buffer
658 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
660 el->el_history.last = el->el_history.buf +
661 (el->el_line.lastchar - el->el_line.buffer);
663 el->el_history.eventno += el->el_state.argument;
665 if (hist_get(el) == CC_ERROR) {
666 if (el->el_map.type == MAP_VI) {
667 el->el_history.eventno = sv_event;
670 /* el->el_history.eventno was fixed by first call */
674 return CC_REFRESH_BEEP;
679 /* ed_next_history():
680 * Move to the next history line
683 protected el_action_t
685 ed_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
687 el_action_t beep = CC_REFRESH, rval;
689 el->el_chared.c_undo.len = -1;
690 *el->el_line.lastchar = '\0'; /* just in case */
692 el->el_history.eventno -= el->el_state.argument;
694 if (el->el_history.eventno < 0) {
695 el->el_history.eventno = 0;
696 beep = CC_REFRESH_BEEP;
699 if (rval == CC_REFRESH)
706 /* ed_search_prev_history():
707 * Search previous in history for a line matching the current
708 * next search history [M-P] [K]
710 protected el_action_t
712 ed_search_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
718 el->el_chared.c_vcmd.action = NOP;
719 el->el_chared.c_undo.len = -1;
720 *el->el_line.lastchar = '\0'; /* just in case */
721 if (el->el_history.eventno < 0) {
723 (void) fprintf(el->el_errfile,
724 "e_prev_search_hist(): eventno < 0;\n");
726 el->el_history.eventno = 0;
729 if (el->el_history.eventno == 0) {
730 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
732 el->el_history.last = el->el_history.buf +
733 (el->el_line.lastchar - el->el_line.buffer);
735 if (el->el_history.ref == NULL)
742 c_setpat(el); /* Set search pattern !! */
744 for (h = 1; h <= el->el_history.eventno; h++)
749 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
751 if ((Strncmp(hp, el->el_line.buffer, (size_t)
752 (el->el_line.lastchar - el->el_line.buffer)) ||
753 hp[el->el_line.lastchar - el->el_line.buffer]) &&
764 (void) fprintf(el->el_errfile, "not found\n");
768 el->el_history.eventno = h;
774 /* ed_search_next_history():
775 * Search next in history for a line matching the current
778 protected el_action_t
780 ed_search_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
786 el->el_chared.c_vcmd.action = NOP;
787 el->el_chared.c_undo.len = -1;
788 *el->el_line.lastchar = '\0'; /* just in case */
790 if (el->el_history.eventno == 0)
793 if (el->el_history.ref == NULL)
800 c_setpat(el); /* Set search pattern !! */
802 for (h = 1; h < el->el_history.eventno && hp; h++) {
804 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
806 if ((Strncmp(hp, el->el_line.buffer, (size_t)
807 (el->el_line.lastchar - el->el_line.buffer)) ||
808 hp[el->el_line.lastchar - el->el_line.buffer]) &&
814 if (!found) { /* is it the current history number? */
815 if (!c_hmatch(el, el->el_history.buf)) {
817 (void) fprintf(el->el_errfile, "not found\n");
822 el->el_history.eventno = found;
832 protected el_action_t
834 ed_prev_line(EditLine *el, wint_t c __attribute__((__unused__)))
837 int nchars = c_hpos(el);
840 * Move to the line requested
842 if (*(ptr = el->el_line.cursor) == '\n')
845 for (; ptr >= el->el_line.buffer; ptr--)
846 if (*ptr == '\n' && --el->el_state.argument <= 0)
849 if (el->el_state.argument > 0)
853 * Move to the beginning of the line
855 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
859 * Move to the character requested
862 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
866 el->el_line.cursor = ptr;
875 protected el_action_t
877 ed_next_line(EditLine *el, wint_t c __attribute__((__unused__)))
880 int nchars = c_hpos(el);
883 * Move to the line requested
885 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
886 if (*ptr == '\n' && --el->el_state.argument <= 0)
889 if (el->el_state.argument > 0)
893 * Move to the character requested
896 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
900 el->el_line.cursor = ptr;
906 * Editline extended command
909 protected el_action_t
911 ed_command(EditLine *el, wint_t c __attribute__((__unused__)))
913 Char tmpbuf[EL_BUFSIZ];
916 tmplen = c_gets(el, tmpbuf, STR("\n: "));
917 terminal__putc(el, '\n');
919 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
922 el->el_map.current = el->el_map.key;
923 re_clear_display(el);