1 /* $NetBSD: common.c,v 1.47 2016/05/22 19:44:26 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.47 2016/05/22 19:44:26 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * common.c: Common Editor functions
57 * Indicate end of file
60 libedit_private el_action_t
62 ed_end_of_file(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
66 *el
->el_line
.lastchar
= '\0';
72 * Add character to the line
73 * Insert a character [bound to all insert keys]
75 libedit_private el_action_t
76 ed_insert(EditLine
*el
, wint_t c
)
78 int count
= el
->el_state
.argument
;
83 if (el
->el_line
.lastchar
+ el
->el_state
.argument
>=
85 /* end of buffer space, try to allocate more */
86 if (!ch_enlargebufs(el
, (size_t) count
))
87 return CC_ERROR
; /* error allocating more */
91 if (el
->el_state
.inputmode
== MODE_INSERT
92 || el
->el_line
.cursor
>= el
->el_line
.lastchar
)
95 *el
->el_line
.cursor
++ = c
;
96 re_fastaddc(el
); /* fast refresh for one char. */
98 if (el
->el_state
.inputmode
!= MODE_REPLACE_1
)
99 c_insert(el
, el
->el_state
.argument
);
101 while (count
-- && el
->el_line
.cursor
< el
->el_line
.lastchar
)
102 *el
->el_line
.cursor
++ = c
;
106 if (el
->el_state
.inputmode
== MODE_REPLACE_1
)
107 return vi_command_mode(el
, 0);
113 /* ed_delete_prev_word():
114 * Delete from beginning of current word to cursor
117 libedit_private el_action_t
119 ed_delete_prev_word(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
121 wchar_t *cp
, *p
, *kp
;
123 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
126 cp
= c__prev_word(el
->el_line
.cursor
, el
->el_line
.buffer
,
127 el
->el_state
.argument
, ce__isword
);
129 for (p
= cp
, kp
= el
->el_chared
.c_kill
.buf
; p
< el
->el_line
.cursor
; p
++)
131 el
->el_chared
.c_kill
.last
= kp
;
133 c_delbefore(el
, (int)(el
->el_line
.cursor
- cp
));/* delete before dot */
134 el
->el_line
.cursor
= cp
;
135 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
136 el
->el_line
.cursor
= el
->el_line
.buffer
; /* bounds check */
141 /* ed_delete_next_char():
142 * Delete character under cursor
145 libedit_private el_action_t
147 ed_delete_next_char(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
150 #define EL el->el_line
151 (void) fprintf(el
->el_errfile
,
152 "\nD(b: %p(%ls) c: %p(%ls) last: %p(%ls) limit: %p(%ls)\n",
153 EL
.buffer
, EL
.buffer
, EL
.cursor
, EL
.cursor
, EL
.lastchar
,
154 EL
.lastchar
, EL
.limit
, EL
.limit
);
156 if (el
->el_line
.cursor
== el
->el_line
.lastchar
) {
157 /* if I'm at the end */
158 if (el
->el_map
.type
== MAP_VI
) {
159 if (el
->el_line
.cursor
== el
->el_line
.buffer
) {
160 /* if I'm also at the beginning */
165 terminal_writec(el
, c
);
170 el
->el_line
.cursor
--;
178 c_delafter(el
, el
->el_state
.argument
); /* delete after dot */
179 if (el
->el_map
.type
== MAP_VI
&&
180 el
->el_line
.cursor
>= el
->el_line
.lastchar
&&
181 el
->el_line
.cursor
> el
->el_line
.buffer
)
183 el
->el_line
.cursor
= el
->el_line
.lastchar
- 1;
189 * Cut to the end of line
192 libedit_private el_action_t
194 ed_kill_line(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
198 cp
= el
->el_line
.cursor
;
199 kp
= el
->el_chared
.c_kill
.buf
;
200 while (cp
< el
->el_line
.lastchar
)
201 *kp
++ = *cp
++; /* copy it */
202 el
->el_chared
.c_kill
.last
= kp
;
203 /* zap! -- delete to end */
204 el
->el_line
.lastchar
= el
->el_line
.cursor
;
210 * Move cursor to the end of line
213 libedit_private el_action_t
215 ed_move_to_end(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
218 el
->el_line
.cursor
= el
->el_line
.lastchar
;
219 if (el
->el_map
.type
== MAP_VI
) {
220 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
225 el
->el_line
.cursor
--;
233 * Move cursor to the beginning of line
236 libedit_private el_action_t
238 ed_move_to_beg(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
241 el
->el_line
.cursor
= el
->el_line
.buffer
;
243 if (el
->el_map
.type
== MAP_VI
) {
244 /* We want FIRST non space character */
245 while (iswspace(*el
->el_line
.cursor
))
246 el
->el_line
.cursor
++;
247 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
256 /* ed_transpose_chars():
257 * Exchange the character to the left of the cursor with the one under it
260 libedit_private el_action_t
261 ed_transpose_chars(EditLine
*el
, wint_t c
)
264 if (el
->el_line
.cursor
< el
->el_line
.lastchar
) {
265 if (el
->el_line
.lastchar
<= &el
->el_line
.buffer
[1])
268 el
->el_line
.cursor
++;
270 if (el
->el_line
.cursor
> &el
->el_line
.buffer
[1]) {
271 /* must have at least two chars entered */
272 c
= el
->el_line
.cursor
[-2];
273 el
->el_line
.cursor
[-2] = el
->el_line
.cursor
[-1];
274 el
->el_line
.cursor
[-1] = c
;
282 * Move to the right one character
285 libedit_private el_action_t
287 ed_next_char(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
289 wchar_t *lim
= el
->el_line
.lastchar
;
291 if (el
->el_line
.cursor
>= lim
||
292 (el
->el_line
.cursor
== lim
- 1 &&
293 el
->el_map
.type
== MAP_VI
&&
294 el
->el_chared
.c_vcmd
.action
== NOP
))
297 el
->el_line
.cursor
+= el
->el_state
.argument
;
298 if (el
->el_line
.cursor
> lim
)
299 el
->el_line
.cursor
= lim
;
301 if (el
->el_map
.type
== MAP_VI
)
302 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
311 * Move to the beginning of the current word
314 libedit_private el_action_t
316 ed_prev_word(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
319 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
322 el
->el_line
.cursor
= c__prev_word(el
->el_line
.cursor
,
324 el
->el_state
.argument
,
327 if (el
->el_map
.type
== MAP_VI
)
328 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
337 * Move to the left one character
340 libedit_private el_action_t
342 ed_prev_char(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
345 if (el
->el_line
.cursor
> el
->el_line
.buffer
) {
346 el
->el_line
.cursor
-= el
->el_state
.argument
;
347 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
348 el
->el_line
.cursor
= el
->el_line
.buffer
;
350 if (el
->el_map
.type
== MAP_VI
)
351 if (el
->el_chared
.c_vcmd
.action
!= NOP
) {
361 /* ed_quoted_insert():
362 * Add the next character typed verbatim
365 libedit_private el_action_t
366 ed_quoted_insert(EditLine
*el
, wint_t c
)
371 num
= el_wgetc(el
, &c
);
374 return ed_insert(el
, c
);
376 return ed_end_of_file(el
, 0);
381 * Adds to argument or enters a digit
383 libedit_private el_action_t
384 ed_digit(EditLine
*el
, wint_t c
)
390 if (el
->el_state
.doingarg
) {
391 /* if doing an arg, add this in... */
392 if (el
->el_state
.lastcmd
== EM_UNIVERSAL_ARGUMENT
)
393 el
->el_state
.argument
= c
- '0';
395 if (el
->el_state
.argument
> 1000000)
397 el
->el_state
.argument
=
398 (el
->el_state
.argument
* 10) + (c
- '0');
403 return ed_insert(el
, c
);
407 /* ed_argument_digit():
408 * Digit that starts argument
411 libedit_private el_action_t
412 ed_argument_digit(EditLine
*el
, wint_t c
)
418 if (el
->el_state
.doingarg
) {
419 if (el
->el_state
.argument
> 1000000)
421 el
->el_state
.argument
= (el
->el_state
.argument
* 10) +
423 } else { /* else starting an argument */
424 el
->el_state
.argument
= c
- '0';
425 el
->el_state
.doingarg
= 1;
432 * Indicates unbound character
433 * Bound to keys that are not assigned
435 libedit_private el_action_t
437 ed_unassigned(EditLine
*el
__attribute__((__unused__
)),
438 wint_t c
__attribute__((__unused__
)))
446 * Input characters that have no effect
447 * [^C ^O ^Q ^S ^Z ^\ ^]] [^C ^O ^Q ^S ^\]
449 libedit_private el_action_t
451 ed_ignore(EditLine
*el
__attribute__((__unused__
)),
452 wint_t c
__attribute__((__unused__
)))
463 libedit_private el_action_t
465 ed_newline(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
469 *el
->el_line
.lastchar
++ = '\n';
470 *el
->el_line
.lastchar
= '\0';
475 /* ed_delete_prev_char():
476 * Delete the character to the left of the cursor
479 libedit_private el_action_t
481 ed_delete_prev_char(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
484 if (el
->el_line
.cursor
<= el
->el_line
.buffer
)
487 c_delbefore(el
, el
->el_state
.argument
);
488 el
->el_line
.cursor
-= el
->el_state
.argument
;
489 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
490 el
->el_line
.cursor
= el
->el_line
.buffer
;
495 /* ed_clear_screen():
496 * Clear screen leaving current line at the top
499 libedit_private el_action_t
501 ed_clear_screen(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
504 terminal_clear_screen(el
); /* clear the whole real screen */
505 re_clear_display(el
); /* reset everything */
511 * Redisplay everything
514 libedit_private el_action_t
516 ed_redisplay(EditLine
*el
__attribute__((__unused__
)),
517 wint_t c
__attribute__((__unused__
)))
525 * Erase current line and start from scratch
528 libedit_private el_action_t
530 ed_start_over(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
538 /* ed_sequence_lead_in():
539 * First character in a bound sequence
540 * Placeholder for external keys
542 libedit_private el_action_t
544 ed_sequence_lead_in(EditLine
*el
__attribute__((__unused__
)),
545 wint_t c
__attribute__((__unused__
)))
552 /* ed_prev_history():
553 * Move to the previous history line
556 libedit_private el_action_t
558 ed_prev_history(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
561 int sv_event
= el
->el_history
.eventno
;
563 el
->el_chared
.c_undo
.len
= -1;
564 *el
->el_line
.lastchar
= '\0'; /* just in case */
566 if (el
->el_history
.eventno
== 0) { /* save the current buffer
568 (void) wcsncpy(el
->el_history
.buf
, el
->el_line
.buffer
,
570 el
->el_history
.last
= el
->el_history
.buf
+
571 (el
->el_line
.lastchar
- el
->el_line
.buffer
);
573 el
->el_history
.eventno
+= el
->el_state
.argument
;
575 if (hist_get(el
) == CC_ERROR
) {
576 if (el
->el_map
.type
== MAP_VI
) {
577 el
->el_history
.eventno
= sv_event
;
580 /* el->el_history.eventno was fixed by first call */
584 return CC_REFRESH_BEEP
;
589 /* ed_next_history():
590 * Move to the next history line
593 libedit_private el_action_t
595 ed_next_history(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
597 el_action_t beep
= CC_REFRESH
, rval
;
599 el
->el_chared
.c_undo
.len
= -1;
600 *el
->el_line
.lastchar
= '\0'; /* just in case */
602 el
->el_history
.eventno
-= el
->el_state
.argument
;
604 if (el
->el_history
.eventno
< 0) {
605 el
->el_history
.eventno
= 0;
606 beep
= CC_REFRESH_BEEP
;
609 if (rval
== CC_REFRESH
)
616 /* ed_search_prev_history():
617 * Search previous in history for a line matching the current
618 * next search history [M-P] [K]
620 libedit_private el_action_t
622 ed_search_prev_history(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
628 el
->el_chared
.c_vcmd
.action
= NOP
;
629 el
->el_chared
.c_undo
.len
= -1;
630 *el
->el_line
.lastchar
= '\0'; /* just in case */
631 if (el
->el_history
.eventno
< 0) {
633 (void) fprintf(el
->el_errfile
,
634 "e_prev_search_hist(): eventno < 0;\n");
636 el
->el_history
.eventno
= 0;
639 if (el
->el_history
.eventno
== 0) {
640 (void) wcsncpy(el
->el_history
.buf
, el
->el_line
.buffer
,
642 el
->el_history
.last
= el
->el_history
.buf
+
643 (el
->el_line
.lastchar
- el
->el_line
.buffer
);
645 if (el
->el_history
.ref
== NULL
)
652 c_setpat(el
); /* Set search pattern !! */
654 for (h
= 1; h
<= el
->el_history
.eventno
; h
++)
659 (void) fprintf(el
->el_errfile
, "Comparing with \"%s\"\n", hp
);
661 if ((wcsncmp(hp
, el
->el_line
.buffer
, (size_t)
662 (el
->el_line
.lastchar
- el
->el_line
.buffer
)) ||
663 hp
[el
->el_line
.lastchar
- el
->el_line
.buffer
]) &&
674 (void) fprintf(el
->el_errfile
, "not found\n");
678 el
->el_history
.eventno
= h
;
684 /* ed_search_next_history():
685 * Search next in history for a line matching the current
688 libedit_private el_action_t
690 ed_search_next_history(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
696 el
->el_chared
.c_vcmd
.action
= NOP
;
697 el
->el_chared
.c_undo
.len
= -1;
698 *el
->el_line
.lastchar
= '\0'; /* just in case */
700 if (el
->el_history
.eventno
== 0)
703 if (el
->el_history
.ref
== NULL
)
710 c_setpat(el
); /* Set search pattern !! */
712 for (h
= 1; h
< el
->el_history
.eventno
&& hp
; h
++) {
714 (void) fprintf(el
->el_errfile
, "Comparing with \"%s\"\n", hp
);
716 if ((wcsncmp(hp
, el
->el_line
.buffer
, (size_t)
717 (el
->el_line
.lastchar
- el
->el_line
.buffer
)) ||
718 hp
[el
->el_line
.lastchar
- el
->el_line
.buffer
]) &&
724 if (!found
) { /* is it the current history number? */
725 if (!c_hmatch(el
, el
->el_history
.buf
)) {
727 (void) fprintf(el
->el_errfile
, "not found\n");
732 el
->el_history
.eventno
= found
;
742 libedit_private el_action_t
744 ed_prev_line(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
747 int nchars
= c_hpos(el
);
750 * Move to the line requested
752 if (*(ptr
= el
->el_line
.cursor
) == '\n')
755 for (; ptr
>= el
->el_line
.buffer
; ptr
--)
756 if (*ptr
== '\n' && --el
->el_state
.argument
<= 0)
759 if (el
->el_state
.argument
> 0)
763 * Move to the beginning of the line
765 for (ptr
--; ptr
>= el
->el_line
.buffer
&& *ptr
!= '\n'; ptr
--)
769 * Move to the character requested
772 nchars
-- > 0 && ptr
< el
->el_line
.lastchar
&& *ptr
!= '\n';
776 el
->el_line
.cursor
= ptr
;
785 libedit_private el_action_t
787 ed_next_line(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
790 int nchars
= c_hpos(el
);
793 * Move to the line requested
795 for (ptr
= el
->el_line
.cursor
; ptr
< el
->el_line
.lastchar
; ptr
++)
796 if (*ptr
== '\n' && --el
->el_state
.argument
<= 0)
799 if (el
->el_state
.argument
> 0)
803 * Move to the character requested
806 nchars
-- > 0 && ptr
< el
->el_line
.lastchar
&& *ptr
!= '\n';
810 el
->el_line
.cursor
= ptr
;
816 * Editline extended command
819 libedit_private el_action_t
821 ed_command(EditLine
*el
, wint_t c
__attribute__((__unused__
)))
823 wchar_t tmpbuf
[EL_BUFSIZ
];
826 tmplen
= c_gets(el
, tmpbuf
, L
"\n: ");
827 terminal__putc(el
, '\n');
829 if (tmplen
< 0 || (tmpbuf
[tmplen
] = 0, parse_line(el
, tmpbuf
)) == -1)
832 el
->el_map
.current
= el
->el_map
.key
;
833 re_clear_display(el
);