1 /* $NetBSD: common.c,v 1.28 2011/07/29 20:58:07 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.28 2011/07/29 20:58:07 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * common.c: Common Editor functions
50 * Indicate end of file
55 ed_end_of_file(EditLine *el, Int c __attribute__((__unused__)))
59 *el->el_line.lastchar = '\0';
65 * Add character to the line
66 * Insert a character [bound to all insert keys]
69 ed_insert(EditLine *el, Int c)
71 int count = el->el_state.argument;
76 if (el->el_line.lastchar + el->el_state.argument >=
78 /* end of buffer space, try to allocate more */
79 if (!ch_enlargebufs(el, (size_t) count))
80 return CC_ERROR; /* error allocating more */
84 if (el->el_state.inputmode == MODE_INSERT
85 || el->el_line.cursor >= el->el_line.lastchar)
88 *el->el_line.cursor++ = c;
89 re_fastaddc(el); /* fast refresh for one char. */
91 if (el->el_state.inputmode != MODE_REPLACE_1)
92 c_insert(el, el->el_state.argument);
94 while (count-- && el->el_line.cursor < el->el_line.lastchar)
95 *el->el_line.cursor++ = c;
99 if (el->el_state.inputmode == MODE_REPLACE_1)
100 return vi_command_mode(el, 0);
106 /* ed_delete_prev_word():
107 * Delete from beginning of current word to cursor
110 protected el_action_t
112 ed_delete_prev_word(EditLine *el, Int c __attribute__((__unused__)))
116 if (el->el_line.cursor == el->el_line.buffer)
119 cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
120 el->el_state.argument, ce__isword);
122 for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
124 el->el_chared.c_kill.last = kp;
126 c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
127 el->el_line.cursor = cp;
128 if (el->el_line.cursor < el->el_line.buffer)
129 el->el_line.cursor = el->el_line.buffer; /* bounds check */
134 /* ed_delete_next_char():
135 * Delete character under cursor
138 protected el_action_t
140 ed_delete_next_char(EditLine *el, Int c __attribute__((__unused__)))
143 #define EL el->el_line
144 (void) fprintf(el->el_errlfile,
145 "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
146 EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
147 EL.lastchar, EL.limit, EL.limit);
149 if (el->el_line.cursor == el->el_line.lastchar) {
150 /* if I'm at the end */
151 if (el->el_map.type == MAP_VI) {
152 if (el->el_line.cursor == el->el_line.buffer) {
153 /* if I'm also at the beginning */
158 terminal_writec(el, c);
163 el->el_line.cursor--;
169 if (el->el_line.cursor != el->el_line.buffer)
170 el->el_line.cursor--;
175 c_delafter(el, el->el_state.argument); /* delete after dot */
176 if (el->el_line.cursor >= el->el_line.lastchar &&
177 el->el_line.cursor > el->el_line.buffer)
179 el->el_line.cursor = el->el_line.lastchar - 1;
185 * Cut to the end of line
188 protected el_action_t
190 ed_kill_line(EditLine *el, Int c __attribute__((__unused__)))
194 cp = el->el_line.cursor;
195 kp = el->el_chared.c_kill.buf;
196 while (cp < el->el_line.lastchar)
197 *kp++ = *cp++; /* copy it */
198 el->el_chared.c_kill.last = kp;
199 /* zap! -- delete to end */
200 el->el_line.lastchar = el->el_line.cursor;
206 * Move cursor to the end of line
209 protected el_action_t
211 ed_move_to_end(EditLine *el, Int c __attribute__((__unused__)))
214 el->el_line.cursor = el->el_line.lastchar;
215 if (el->el_map.type == MAP_VI) {
216 if (el->el_chared.c_vcmd.action != NOP) {
221 el->el_line.cursor--;
229 * Move cursor to the beginning of line
232 protected el_action_t
234 ed_move_to_beg(EditLine *el, Int c __attribute__((__unused__)))
237 el->el_line.cursor = el->el_line.buffer;
239 if (el->el_map.type == MAP_VI) {
240 /* We want FIRST non space character */
241 while (Isspace(*el->el_line.cursor))
242 el->el_line.cursor++;
243 if (el->el_chared.c_vcmd.action != NOP) {
252 /* ed_transpose_chars():
253 * Exchange the character to the left of the cursor with the one under it
256 protected el_action_t
257 ed_transpose_chars(EditLine *el, Int c)
260 if (el->el_line.cursor < el->el_line.lastchar) {
261 if (el->el_line.lastchar <= &el->el_line.buffer[1])
264 el->el_line.cursor++;
266 if (el->el_line.cursor > &el->el_line.buffer[1]) {
267 /* must have at least two chars entered */
268 c = el->el_line.cursor[-2];
269 el->el_line.cursor[-2] = el->el_line.cursor[-1];
270 el->el_line.cursor[-1] = c;
278 * Move to the right one character
281 protected el_action_t
283 ed_next_char(EditLine *el, Int c __attribute__((__unused__)))
285 Char *lim = el->el_line.lastchar;
287 if (el->el_line.cursor >= lim ||
288 (el->el_line.cursor == lim - 1 &&
289 el->el_map.type == MAP_VI &&
290 el->el_chared.c_vcmd.action == NOP))
293 el->el_line.cursor += el->el_state.argument;
294 if (el->el_line.cursor > lim)
295 el->el_line.cursor = lim;
297 if (el->el_map.type == MAP_VI)
298 if (el->el_chared.c_vcmd.action != NOP) {
307 * Move to the beginning of the current word
310 protected el_action_t
312 ed_prev_word(EditLine *el, Int c __attribute__((__unused__)))
315 if (el->el_line.cursor == el->el_line.buffer)
318 el->el_line.cursor = c__prev_word(el->el_line.cursor,
320 el->el_state.argument,
323 if (el->el_map.type == MAP_VI)
324 if (el->el_chared.c_vcmd.action != NOP) {
333 * Move to the left one character
336 protected el_action_t
338 ed_prev_char(EditLine *el, Int c __attribute__((__unused__)))
341 if (el->el_line.cursor > el->el_line.buffer) {
342 el->el_line.cursor -= el->el_state.argument;
343 if (el->el_line.cursor < el->el_line.buffer)
344 el->el_line.cursor = el->el_line.buffer;
346 if (el->el_map.type == MAP_VI)
347 if (el->el_chared.c_vcmd.action != NOP) {
357 /* ed_quoted_insert():
358 * Add the next character typed verbatim
361 protected el_action_t
362 ed_quoted_insert(EditLine *el, Int c)
368 num = FUN(el,getc)(el, &tc);
372 return ed_insert(el, c);
374 return ed_end_of_file(el, 0);
379 * Adds to argument or enters a digit
381 protected el_action_t
382 ed_digit(EditLine *el, Int c)
388 if (el->el_state.doingarg) {
389 /* if doing an arg, add this in... */
390 if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
391 el->el_state.argument = c - '0';
393 if (el->el_state.argument > 1000000)
395 el->el_state.argument =
396 (el->el_state.argument * 10) + (c - '0');
401 return ed_insert(el, c);
405 /* ed_argument_digit():
406 * Digit that starts argument
409 protected el_action_t
410 ed_argument_digit(EditLine *el, Int c)
416 if (el->el_state.doingarg) {
417 if (el->el_state.argument > 1000000)
419 el->el_state.argument = (el->el_state.argument * 10) +
421 } else { /* else starting an argument */
422 el->el_state.argument = c - '0';
423 el->el_state.doingarg = 1;
430 * Indicates unbound character
431 * Bound to keys that are not assigned
433 protected el_action_t
435 ed_unassigned(EditLine *el __attribute__((__unused__)),
436 Int c __attribute__((__unused__)))
448 * Tty interrupt character
451 protected el_action_t
453 ed_tty_sigint(EditLine *el __attribute__((__unused__)),
454 Int c __attribute__((__unused__)))
462 * Tty delayed suspend character
465 protected el_action_t
467 ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
468 Int c __attribute__((__unused__)))
475 /* ed_tty_flush_output():
476 * Tty flush output characters
479 protected el_action_t
481 ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
482 Int c __attribute__((__unused__)))
493 protected el_action_t
495 ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
496 Int c __attribute__((__unused__)))
504 * Tty suspend character
507 protected el_action_t
509 ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
510 Int c __attribute__((__unused__)))
517 /* ed_tty_stop_output():
518 * Tty disallow output characters
521 protected el_action_t
523 ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
524 Int c __attribute__((__unused__)))
531 /* ed_tty_start_output():
532 * Tty allow output characters
535 protected el_action_t
537 ed_tty_start_output(EditLine *el __attribute__((__unused__)),
538 Int c __attribute__((__unused__)))
549 protected el_action_t
551 ed_newline(EditLine *el, Int c __attribute__((__unused__)))
555 *el->el_line.lastchar++ = '\n';
556 *el->el_line.lastchar = '\0';
561 /* ed_delete_prev_char():
562 * Delete the character to the left of the cursor
565 protected el_action_t
567 ed_delete_prev_char(EditLine *el, Int c __attribute__((__unused__)))
570 if (el->el_line.cursor <= el->el_line.buffer)
573 c_delbefore(el, el->el_state.argument);
574 el->el_line.cursor -= el->el_state.argument;
575 if (el->el_line.cursor < el->el_line.buffer)
576 el->el_line.cursor = el->el_line.buffer;
581 /* ed_clear_screen():
582 * Clear screen leaving current line at the top
585 protected el_action_t
587 ed_clear_screen(EditLine *el, Int c __attribute__((__unused__)))
590 terminal_clear_screen(el); /* clear the whole real screen */
591 re_clear_display(el); /* reset everything */
597 * Redisplay everything
600 protected el_action_t
602 ed_redisplay(EditLine *el __attribute__((__unused__)),
603 Int c __attribute__((__unused__)))
611 * Erase current line and start from scratch
614 protected el_action_t
616 ed_start_over(EditLine *el, Int c __attribute__((__unused__)))
624 /* ed_sequence_lead_in():
625 * First character in a bound sequence
626 * Placeholder for external keys
628 protected el_action_t
630 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
631 Int c __attribute__((__unused__)))
638 /* ed_prev_history():
639 * Move to the previous history line
642 protected el_action_t
644 ed_prev_history(EditLine *el, Int c __attribute__((__unused__)))
647 int sv_event = el->el_history.eventno;
649 el->el_chared.c_undo.len = -1;
650 *el->el_line.lastchar = '\0'; /* just in case */
652 if (el->el_history.eventno == 0) { /* save the current buffer
654 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
656 el->el_history.last = el->el_history.buf +
657 (el->el_line.lastchar - el->el_line.buffer);
659 el->el_history.eventno += el->el_state.argument;
661 if (hist_get(el) == CC_ERROR) {
662 if (el->el_map.type == MAP_VI) {
663 el->el_history.eventno = sv_event;
667 /* el->el_history.eventno was fixed by first call */
671 return CC_REFRESH_BEEP;
676 /* ed_next_history():
677 * Move to the next history line
680 protected el_action_t
682 ed_next_history(EditLine *el, Int c __attribute__((__unused__)))
684 el_action_t beep = CC_REFRESH, rval;
686 el->el_chared.c_undo.len = -1;
687 *el->el_line.lastchar = '\0'; /* just in case */
689 el->el_history.eventno -= el->el_state.argument;
691 if (el->el_history.eventno < 0) {
692 el->el_history.eventno = 0;
693 beep = CC_REFRESH_BEEP;
696 if (rval == CC_REFRESH)
703 /* ed_search_prev_history():
704 * Search previous in history for a line matching the current
705 * next search history [M-P] [K]
707 protected el_action_t
709 ed_search_prev_history(EditLine *el, Int c __attribute__((__unused__)))
715 el->el_chared.c_vcmd.action = NOP;
716 el->el_chared.c_undo.len = -1;
717 *el->el_line.lastchar = '\0'; /* just in case */
718 if (el->el_history.eventno < 0) {
720 (void) fprintf(el->el_errfile,
721 "e_prev_search_hist(): eventno < 0;\n");
723 el->el_history.eventno = 0;
726 if (el->el_history.eventno == 0) {
727 (void) Strncpy(el->el_history.buf, el->el_line.buffer,
729 el->el_history.last = el->el_history.buf +
730 (el->el_line.lastchar - el->el_line.buffer);
732 if (el->el_history.ref == NULL)
739 c_setpat(el); /* Set search pattern !! */
741 for (h = 1; h <= el->el_history.eventno; h++)
746 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
748 if ((Strncmp(hp, el->el_line.buffer, (size_t)
749 (el->el_line.lastchar - el->el_line.buffer)) ||
750 hp[el->el_line.lastchar - el->el_line.buffer]) &&
761 (void) fprintf(el->el_errfile, "not found\n");
765 el->el_history.eventno = h;
771 /* ed_search_next_history():
772 * Search next in history for a line matching the current
775 protected el_action_t
777 ed_search_next_history(EditLine *el, Int c __attribute__((__unused__)))
783 el->el_chared.c_vcmd.action = NOP;
784 el->el_chared.c_undo.len = -1;
785 *el->el_line.lastchar = '\0'; /* just in case */
787 if (el->el_history.eventno == 0)
790 if (el->el_history.ref == NULL)
797 c_setpat(el); /* Set search pattern !! */
799 for (h = 1; h < el->el_history.eventno && hp; h++) {
801 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
803 if ((Strncmp(hp, el->el_line.buffer, (size_t)
804 (el->el_line.lastchar - el->el_line.buffer)) ||
805 hp[el->el_line.lastchar - el->el_line.buffer]) &&
811 if (!found) { /* is it the current history number? */
812 if (!c_hmatch(el, el->el_history.buf)) {
814 (void) fprintf(el->el_errfile, "not found\n");
819 el->el_history.eventno = found;
829 protected el_action_t
831 ed_prev_line(EditLine *el, Int c __attribute__((__unused__)))
834 int nchars = c_hpos(el);
837 * Move to the line requested
839 if (*(ptr = el->el_line.cursor) == '\n')
842 for (; ptr >= el->el_line.buffer; ptr--)
843 if (*ptr == '\n' && --el->el_state.argument <= 0)
846 if (el->el_state.argument > 0)
850 * Move to the beginning of the line
852 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
856 * Move to the character requested
859 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
863 el->el_line.cursor = ptr;
872 protected el_action_t
874 ed_next_line(EditLine *el, Int c __attribute__((__unused__)))
877 int nchars = c_hpos(el);
880 * Move to the line requested
882 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
883 if (*ptr == '\n' && --el->el_state.argument <= 0)
886 if (el->el_state.argument > 0)
890 * Move to the character requested
893 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
897 el->el_line.cursor = ptr;
903 * Editline extended command
906 protected el_action_t
908 ed_command(EditLine *el, Int c __attribute__((__unused__)))
910 Char tmpbuf[EL_BUFSIZ];
913 tmplen = c_gets(el, tmpbuf, STR("\n: "));
914 terminal__putc(el, '\n');
916 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
919 el->el_map.current = el->el_map.key;
920 re_clear_display(el);