1 /* $NetBSD: common.c,v 1.48 2018/02/26 17:36:14 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.48 2018/02/26 17:36:14 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
367 ed_quoted_insert(EditLine *el, wint_t c __attribute__((__unused__)))
373 num = el_wgetc(el, &ch);
376 return ed_insert(el, ch);
378 return ed_end_of_file(el, 0);
383 * Adds to argument or enters a digit
385 libedit_private 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 libedit_private 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 libedit_private el_action_t
439 ed_unassigned(EditLine *el __attribute__((__unused__)),
440 wint_t c __attribute__((__unused__)))
448 * Input characters that have no effect
449 * [^C ^O ^Q ^S ^Z ^\ ^]] [^C ^O ^Q ^S ^\]
451 libedit_private el_action_t
453 ed_ignore(EditLine *el __attribute__((__unused__)),
454 wint_t c __attribute__((__unused__)))
465 libedit_private el_action_t
467 ed_newline(EditLine *el, wint_t c __attribute__((__unused__)))
471 *el->el_line.lastchar++ = '\n';
472 *el->el_line.lastchar = '\0';
477 /* ed_delete_prev_char():
478 * Delete the character to the left of the cursor
481 libedit_private el_action_t
483 ed_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
486 if (el->el_line.cursor <= el->el_line.buffer)
489 c_delbefore(el, el->el_state.argument);
490 el->el_line.cursor -= el->el_state.argument;
491 if (el->el_line.cursor < el->el_line.buffer)
492 el->el_line.cursor = el->el_line.buffer;
497 /* ed_clear_screen():
498 * Clear screen leaving current line at the top
501 libedit_private el_action_t
503 ed_clear_screen(EditLine *el, wint_t c __attribute__((__unused__)))
506 terminal_clear_screen(el); /* clear the whole real screen */
507 re_clear_display(el); /* reset everything */
513 * Redisplay everything
516 libedit_private el_action_t
518 ed_redisplay(EditLine *el __attribute__((__unused__)),
519 wint_t c __attribute__((__unused__)))
527 * Erase current line and start from scratch
530 libedit_private el_action_t
532 ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
540 /* ed_sequence_lead_in():
541 * First character in a bound sequence
542 * Placeholder for external keys
544 libedit_private el_action_t
546 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
547 wint_t c __attribute__((__unused__)))
554 /* ed_prev_history():
555 * Move to the previous history line
558 libedit_private el_action_t
560 ed_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
563 int sv_event = el->el_history.eventno;
565 el->el_chared.c_undo.len = -1;
566 *el->el_line.lastchar = '\0'; /* just in case */
568 if (el->el_history.eventno == 0) { /* save the current buffer
570 (void) wcsncpy(el->el_history.buf, el->el_line.buffer,
572 el->el_history.last = el->el_history.buf +
573 (el->el_line.lastchar - el->el_line.buffer);
575 el->el_history.eventno += el->el_state.argument;
577 if (hist_get(el) == CC_ERROR) {
578 if (el->el_map.type == MAP_VI) {
579 el->el_history.eventno = sv_event;
582 /* el->el_history.eventno was fixed by first call */
586 return CC_REFRESH_BEEP;
591 /* ed_next_history():
592 * Move to the next history line
595 libedit_private el_action_t
597 ed_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
599 el_action_t beep = CC_REFRESH, rval;
601 el->el_chared.c_undo.len = -1;
602 *el->el_line.lastchar = '\0'; /* just in case */
604 el->el_history.eventno -= el->el_state.argument;
606 if (el->el_history.eventno < 0) {
607 el->el_history.eventno = 0;
608 beep = CC_REFRESH_BEEP;
611 if (rval == CC_REFRESH)
618 /* ed_search_prev_history():
619 * Search previous in history for a line matching the current
620 * next search history [M-P] [K]
622 libedit_private el_action_t
624 ed_search_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
630 el->el_chared.c_vcmd.action = NOP;
631 el->el_chared.c_undo.len = -1;
632 *el->el_line.lastchar = '\0'; /* just in case */
633 if (el->el_history.eventno < 0) {
635 (void) fprintf(el->el_errfile,
636 "e_prev_search_hist(): eventno < 0;\n");
638 el->el_history.eventno = 0;
641 if (el->el_history.eventno == 0) {
642 (void) wcsncpy(el->el_history.buf, el->el_line.buffer,
644 el->el_history.last = el->el_history.buf +
645 (el->el_line.lastchar - el->el_line.buffer);
647 if (el->el_history.ref == NULL)
654 c_setpat(el); /* Set search pattern !! */
656 for (h = 1; h <= el->el_history.eventno; h++)
661 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
663 if ((wcsncmp(hp, el->el_line.buffer, (size_t)
664 (el->el_line.lastchar - el->el_line.buffer)) ||
665 hp[el->el_line.lastchar - el->el_line.buffer]) &&
676 (void) fprintf(el->el_errfile, "not found\n");
680 el->el_history.eventno = h;
686 /* ed_search_next_history():
687 * Search next in history for a line matching the current
690 libedit_private el_action_t
692 ed_search_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
698 el->el_chared.c_vcmd.action = NOP;
699 el->el_chared.c_undo.len = -1;
700 *el->el_line.lastchar = '\0'; /* just in case */
702 if (el->el_history.eventno == 0)
705 if (el->el_history.ref == NULL)
712 c_setpat(el); /* Set search pattern !! */
714 for (h = 1; h < el->el_history.eventno && hp; h++) {
716 (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
718 if ((wcsncmp(hp, el->el_line.buffer, (size_t)
719 (el->el_line.lastchar - el->el_line.buffer)) ||
720 hp[el->el_line.lastchar - el->el_line.buffer]) &&
726 if (!found) { /* is it the current history number? */
727 if (!c_hmatch(el, el->el_history.buf)) {
729 (void) fprintf(el->el_errfile, "not found\n");
734 el->el_history.eventno = found;
744 libedit_private el_action_t
746 ed_prev_line(EditLine *el, wint_t c __attribute__((__unused__)))
749 int nchars = c_hpos(el);
752 * Move to the line requested
754 if (*(ptr = el->el_line.cursor) == '\n')
757 for (; ptr >= el->el_line.buffer; ptr--)
758 if (*ptr == '\n' && --el->el_state.argument <= 0)
761 if (el->el_state.argument > 0)
765 * Move to the beginning of the line
767 for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
771 * Move to the character requested
774 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
778 el->el_line.cursor = ptr;
787 libedit_private el_action_t
789 ed_next_line(EditLine *el, wint_t c __attribute__((__unused__)))
792 int nchars = c_hpos(el);
795 * Move to the line requested
797 for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
798 if (*ptr == '\n' && --el->el_state.argument <= 0)
801 if (el->el_state.argument > 0)
805 * Move to the character requested
808 nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
812 el->el_line.cursor = ptr;
818 * Editline extended command
821 libedit_private el_action_t
823 ed_command(EditLine *el, wint_t c __attribute__((__unused__)))
825 wchar_t tmpbuf[EL_BUFSIZ];
828 tmplen = c_gets(el, tmpbuf, L"\n: ");
829 terminal__putc(el, '\n');
831 if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
834 el->el_map.current = el->el_map.key;
835 re_clear_display(el);