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 */
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
--;
171 c_delafter(el
, el
->el_state
.argument
); /* delete after dot */
172 if (el
->el_map
.type
== MAP_VI
&&
173 el
->el_line
.cursor
>= el
->el_line
.lastchar
&&
174 el
->el_line
.cursor
> el
->el_line
.buffer
)
176 el
->el_line
.cursor
= el
->el_line
.lastchar
- 1;
182 * Cut to the end of line
185 protected el_action_t
187 ed_kill_line(EditLine
*el
, Int c
__attribute__((__unused__
)))
191 cp
= el
->el_line
.cursor
;
192 kp
= el
->el_chared
.c_kill
.buf
;
193 while (cp
< el
->el_line
.lastchar
)
194 *kp
++ = *cp
++; /* copy it */
195 el
->el_chared
.c_kill
.last
= kp
;
196 /* zap! -- delete to end */
197 el
->el_line
.lastchar
= el
->el_line
.cursor
;
203 * Move cursor to the end of line
206 protected el_action_t
208 ed_move_to_end(EditLine
*el
, Int c
__attribute__((__unused__
)))
211 el
->el_line
.cursor
= el
->el_line
.lastchar
;
212 if (el
->el_map
.type
== MAP_VI
) {
213 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
218 el
->el_line
.cursor
--;
226 * Move cursor to the beginning of line
229 protected el_action_t
231 ed_move_to_beg(EditLine
*el
, Int c
__attribute__((__unused__
)))
234 el
->el_line
.cursor
= el
->el_line
.buffer
;
236 if (el
->el_map
.type
== MAP_VI
) {
237 /* We want FIRST non space character */
238 while (Isspace(*el
->el_line
.cursor
))
239 el
->el_line
.cursor
++;
240 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
249 /* ed_transpose_chars():
250 * Exchange the character to the left of the cursor with the one under it
253 protected el_action_t
254 ed_transpose_chars(EditLine
*el
, Int c
)
257 if (el
->el_line
.cursor
< el
->el_line
.lastchar
) {
258 if (el
->el_line
.lastchar
<= &el
->el_line
.buffer
[1])
261 el
->el_line
.cursor
++;
263 if (el
->el_line
.cursor
> &el
->el_line
.buffer
[1]) {
264 /* must have at least two chars entered */
265 c
= el
->el_line
.cursor
[-2];
266 el
->el_line
.cursor
[-2] = el
->el_line
.cursor
[-1];
267 el
->el_line
.cursor
[-1] = c
;
275 * Move to the right one character
278 protected el_action_t
280 ed_next_char(EditLine
*el
, Int c
__attribute__((__unused__
)))
282 Char
*lim
= el
->el_line
.lastchar
;
284 if (el
->el_line
.cursor
>= lim
||
285 (el
->el_line
.cursor
== lim
- 1 &&
286 el
->el_map
.type
== MAP_VI
&&
287 el
->el_chared
.c_vcmd
.action
== NOP
))
290 el
->el_line
.cursor
+= el
->el_state
.argument
;
291 if (el
->el_line
.cursor
> lim
)
292 el
->el_line
.cursor
= lim
;
294 if (el
->el_map
.type
== MAP_VI
)
295 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
304 * Move to the beginning of the current word
307 protected el_action_t
309 ed_prev_word(EditLine
*el
, Int c
__attribute__((__unused__
)))
312 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
315 el
->el_line
.cursor
= c__prev_word(el
->el_line
.cursor
,
317 el
->el_state
.argument
,
320 if (el
->el_map
.type
== MAP_VI
)
321 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
330 * Move to the left one character
333 protected el_action_t
335 ed_prev_char(EditLine
*el
, Int c
__attribute__((__unused__
)))
338 if (el
->el_line
.cursor
> el
->el_line
.buffer
) {
339 el
->el_line
.cursor
-= el
->el_state
.argument
;
340 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
341 el
->el_line
.cursor
= el
->el_line
.buffer
;
343 if (el
->el_map
.type
== MAP_VI
)
344 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
354 /* ed_quoted_insert():
355 * Add the next character typed verbatim
358 protected el_action_t
359 ed_quoted_insert(EditLine
*el
, Int c
)
365 num
= FUN(el
,getc
)(el
, &tc
);
369 return ed_insert(el
, c
);
371 return ed_end_of_file(el
, 0);
376 * Adds to argument or enters a digit
378 protected el_action_t
379 ed_digit(EditLine
*el
, Int c
)
385 if (el
->el_state
.doingarg
) {
386 /* if doing an arg, add this in... */
387 if (el
->el_state
.lastcmd
== EM_UNIVERSAL_ARGUMENT
)
388 el
->el_state
.argument
= c
- '0';
390 if (el
->el_state
.argument
> 1000000)
392 el
->el_state
.argument
=
393 (el
->el_state
.argument
* 10) + (c
- '0');
398 return ed_insert(el
, c
);
402 /* ed_argument_digit():
403 * Digit that starts argument
406 protected el_action_t
407 ed_argument_digit(EditLine
*el
, Int c
)
413 if (el
->el_state
.doingarg
) {
414 if (el
->el_state
.argument
> 1000000)
416 el
->el_state
.argument
= (el
->el_state
.argument
* 10) +
418 } else { /* else starting an argument */
419 el
->el_state
.argument
= c
- '0';
420 el
->el_state
.doingarg
= 1;
427 * Indicates unbound character
428 * Bound to keys that are not assigned
430 protected el_action_t
432 ed_unassigned(EditLine
*el
__attribute__((__unused__
)),
433 Int c
__attribute__((__unused__
)))
445 * Tty interrupt character
448 protected el_action_t
450 ed_tty_sigint(EditLine
*el
__attribute__((__unused__
)),
451 Int c
__attribute__((__unused__
)))
459 * Tty delayed suspend character
462 protected el_action_t
464 ed_tty_dsusp(EditLine
*el
__attribute__((__unused__
)),
465 Int c
__attribute__((__unused__
)))
472 /* ed_tty_flush_output():
473 * Tty flush output characters
476 protected el_action_t
478 ed_tty_flush_output(EditLine
*el
__attribute__((__unused__
)),
479 Int c
__attribute__((__unused__
)))
490 protected el_action_t
492 ed_tty_sigquit(EditLine
*el
__attribute__((__unused__
)),
493 Int c
__attribute__((__unused__
)))
501 * Tty suspend character
504 protected el_action_t
506 ed_tty_sigtstp(EditLine
*el
__attribute__((__unused__
)),
507 Int c
__attribute__((__unused__
)))
514 /* ed_tty_stop_output():
515 * Tty disallow output characters
518 protected el_action_t
520 ed_tty_stop_output(EditLine
*el
__attribute__((__unused__
)),
521 Int c
__attribute__((__unused__
)))
528 /* ed_tty_start_output():
529 * Tty allow output characters
532 protected el_action_t
534 ed_tty_start_output(EditLine
*el
__attribute__((__unused__
)),
535 Int c
__attribute__((__unused__
)))
546 protected el_action_t
548 ed_newline(EditLine
*el
, Int c
__attribute__((__unused__
)))
552 *el
->el_line
.lastchar
++ = '\n';
553 *el
->el_line
.lastchar
= '\0';
558 /* ed_delete_prev_char():
559 * Delete the character to the left of the cursor
562 protected el_action_t
564 ed_delete_prev_char(EditLine
*el
, Int c
__attribute__((__unused__
)))
567 if (el
->el_line
.cursor
<= el
->el_line
.buffer
)
570 c_delbefore(el
, el
->el_state
.argument
);
571 el
->el_line
.cursor
-= el
->el_state
.argument
;
572 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
573 el
->el_line
.cursor
= el
->el_line
.buffer
;
578 /* ed_clear_screen():
579 * Clear screen leaving current line at the top
582 protected el_action_t
584 ed_clear_screen(EditLine
*el
, Int c
__attribute__((__unused__
)))
587 terminal_clear_screen(el
); /* clear the whole real screen */
588 re_clear_display(el
); /* reset everything */
594 * Redisplay everything
597 protected el_action_t
599 ed_redisplay(EditLine
*el
__attribute__((__unused__
)),
600 Int c
__attribute__((__unused__
)))
608 * Erase current line and start from scratch
611 protected el_action_t
613 ed_start_over(EditLine
*el
, Int c
__attribute__((__unused__
)))
621 /* ed_sequence_lead_in():
622 * First character in a bound sequence
623 * Placeholder for external keys
625 protected el_action_t
627 ed_sequence_lead_in(EditLine
*el
__attribute__((__unused__
)),
628 Int c
__attribute__((__unused__
)))
635 /* ed_prev_history():
636 * Move to the previous history line
639 protected el_action_t
641 ed_prev_history(EditLine
*el
, Int c
__attribute__((__unused__
)))
644 int sv_event
= el
->el_history
.eventno
;
646 el
->el_chared
.c_undo
.len
= -1;
647 *el
->el_line
.lastchar
= '\0'; /* just in case */
649 if (el
->el_history
.eventno
== 0) { /* save the current buffer
651 (void) Strncpy(el
->el_history
.buf
, el
->el_line
.buffer
,
653 el
->el_history
.last
= el
->el_history
.buf
+
654 (el
->el_line
.lastchar
- el
->el_line
.buffer
);
656 el
->el_history
.eventno
+= el
->el_state
.argument
;
658 if (hist_get(el
) == CC_ERROR
) {
659 if (el
->el_map
.type
== MAP_VI
) {
660 el
->el_history
.eventno
= sv_event
;
664 /* el->el_history.eventno was fixed by first call */
668 return CC_REFRESH_BEEP
;
673 /* ed_next_history():
674 * Move to the next history line
677 protected el_action_t
679 ed_next_history(EditLine
*el
, Int c
__attribute__((__unused__
)))
681 el_action_t beep
= CC_REFRESH
, rval
;
683 el
->el_chared
.c_undo
.len
= -1;
684 *el
->el_line
.lastchar
= '\0'; /* just in case */
686 el
->el_history
.eventno
-= el
->el_state
.argument
;
688 if (el
->el_history
.eventno
< 0) {
689 el
->el_history
.eventno
= 0;
690 beep
= CC_REFRESH_BEEP
;
693 if (rval
== CC_REFRESH
)
700 /* ed_search_prev_history():
701 * Search previous in history for a line matching the current
702 * next search history [M-P] [K]
704 protected el_action_t
706 ed_search_prev_history(EditLine
*el
, Int c
__attribute__((__unused__
)))
712 el
->el_chared
.c_vcmd
.action
= NOP
;
713 el
->el_chared
.c_undo
.len
= -1;
714 *el
->el_line
.lastchar
= '\0'; /* just in case */
715 if (el
->el_history
.eventno
< 0) {
717 (void) fprintf(el
->el_errfile
,
718 "e_prev_search_hist(): eventno < 0;\n");
720 el
->el_history
.eventno
= 0;
723 if (el
->el_history
.eventno
== 0) {
724 (void) Strncpy(el
->el_history
.buf
, el
->el_line
.buffer
,
726 el
->el_history
.last
= el
->el_history
.buf
+
727 (el
->el_line
.lastchar
- el
->el_line
.buffer
);
729 if (el
->el_history
.ref
== NULL
)
736 c_setpat(el
); /* Set search pattern !! */
738 for (h
= 1; h
<= el
->el_history
.eventno
; h
++)
743 (void) fprintf(el
->el_errfile
, "Comparing with \"%s\"\n", hp
);
745 if ((Strncmp(hp
, el
->el_line
.buffer
, (size_t)
746 (el
->el_line
.lastchar
- el
->el_line
.buffer
)) ||
747 hp
[el
->el_line
.lastchar
- el
->el_line
.buffer
]) &&
758 (void) fprintf(el
->el_errfile
, "not found\n");
762 el
->el_history
.eventno
= h
;
768 /* ed_search_next_history():
769 * Search next in history for a line matching the current
772 protected el_action_t
774 ed_search_next_history(EditLine
*el
, Int c
__attribute__((__unused__
)))
780 el
->el_chared
.c_vcmd
.action
= NOP
;
781 el
->el_chared
.c_undo
.len
= -1;
782 *el
->el_line
.lastchar
= '\0'; /* just in case */
784 if (el
->el_history
.eventno
== 0)
787 if (el
->el_history
.ref
== NULL
)
794 c_setpat(el
); /* Set search pattern !! */
796 for (h
= 1; h
< el
->el_history
.eventno
&& hp
; h
++) {
798 (void) fprintf(el
->el_errfile
, "Comparing with \"%s\"\n", hp
);
800 if ((Strncmp(hp
, el
->el_line
.buffer
, (size_t)
801 (el
->el_line
.lastchar
- el
->el_line
.buffer
)) ||
802 hp
[el
->el_line
.lastchar
- el
->el_line
.buffer
]) &&
808 if (!found
) { /* is it the current history number? */
809 if (!c_hmatch(el
, el
->el_history
.buf
)) {
811 (void) fprintf(el
->el_errfile
, "not found\n");
816 el
->el_history
.eventno
= found
;
826 protected el_action_t
828 ed_prev_line(EditLine
*el
, Int c
__attribute__((__unused__
)))
831 int nchars
= c_hpos(el
);
834 * Move to the line requested
836 if (*(ptr
= el
->el_line
.cursor
) == '\n')
839 for (; ptr
>= el
->el_line
.buffer
; ptr
--)
840 if (*ptr
== '\n' && --el
->el_state
.argument
<= 0)
843 if (el
->el_state
.argument
> 0)
847 * Move to the beginning of the line
849 for (ptr
--; ptr
>= el
->el_line
.buffer
&& *ptr
!= '\n'; ptr
--)
853 * Move to the character requested
856 nchars
-- > 0 && ptr
< el
->el_line
.lastchar
&& *ptr
!= '\n';
860 el
->el_line
.cursor
= ptr
;
869 protected el_action_t
871 ed_next_line(EditLine
*el
, Int c
__attribute__((__unused__
)))
874 int nchars
= c_hpos(el
);
877 * Move to the line requested
879 for (ptr
= el
->el_line
.cursor
; ptr
< el
->el_line
.lastchar
; ptr
++)
880 if (*ptr
== '\n' && --el
->el_state
.argument
<= 0)
883 if (el
->el_state
.argument
> 0)
887 * Move to the character requested
890 nchars
-- > 0 && ptr
< el
->el_line
.lastchar
&& *ptr
!= '\n';
894 el
->el_line
.cursor
= ptr
;
900 * Editline extended command
903 protected el_action_t
905 ed_command(EditLine
*el
, Int c
__attribute__((__unused__
)))
907 Char tmpbuf
[EL_BUFSIZ
];
910 tmplen
= c_gets(el
, tmpbuf
, STR("\n: "));
911 terminal__putc(el
, '\n');
913 if (tmplen
< 0 || (tmpbuf
[tmplen
] = 0, parse_line(el
, tmpbuf
)) == -1)
916 el
->el_map
.current
= el
->el_map
.key
;
917 re_clear_display(el
);