1 /* $NetBSD: chared.c,v 1.49 2016/02/24 14:29:21 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[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: chared.c,v 1.49 2016/02/24 14:29:21 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * chared.c: Character editor utilities
56 private void ch__clearmacro (EditLine *);
58 /* value to leave unused in line buffer */
62 * Handle state for the vi undo command
67 c_undo_t *vu = &el->el_chared.c_undo;
68 c_redo_t *r = &el->el_chared.c_redo;
71 /* Save entire line for undo */
72 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
73 vu->len = (ssize_t)size;
74 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
75 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
77 /* save command info for redo */
78 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
79 r->action = el->el_chared.c_vcmd.action;
81 r->cmd = el->el_state.thiscmd;
82 r->ch = el->el_state.thisch;
86 * Save yank/delete data for paste
89 cv_yank(EditLine *el, const Char *ptr, int size)
91 c_kill_t *k = &el->el_chared.c_kill;
93 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
94 k->last = k->buf + size;
99 * Insert num characters
102 c_insert(EditLine *el, int num)
106 if (el->el_line.lastchar + num >= el->el_line.limit) {
107 if (!ch_enlargebufs(el, (size_t)num))
108 return; /* can't go past end of buffer */
111 if (el->el_line.cursor < el->el_line.lastchar) {
112 /* if I must move chars */
113 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
116 el->el_line.lastchar += num;
121 * Delete num characters after the cursor
124 c_delafter(EditLine *el, int num)
127 if (el->el_line.cursor + num > el->el_line.lastchar)
128 num = (int)(el->el_line.lastchar - el->el_line.cursor);
130 if (el->el_map.current != el->el_map.emacs) {
132 cv_yank(el, el->el_line.cursor, num);
138 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
141 el->el_line.lastchar -= num;
147 * Delete the character after the cursor, do not yank
150 c_delafter1(EditLine *el)
154 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
157 el->el_line.lastchar--;
162 * Delete num characters before the cursor
165 c_delbefore(EditLine *el, int num)
168 if (el->el_line.cursor - num < el->el_line.buffer)
169 num = (int)(el->el_line.cursor - el->el_line.buffer);
171 if (el->el_map.current != el->el_map.emacs) {
173 cv_yank(el, el->el_line.cursor - num, num);
179 for (cp = el->el_line.cursor - num;
180 cp <= el->el_line.lastchar;
184 el->el_line.lastchar -= num;
190 * Delete the character before the cursor, do not yank
193 c_delbefore1(EditLine *el)
197 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
200 el->el_line.lastchar--;
205 * Return if p is part of a word according to emacs
210 return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
215 * Return if p is part of a word according to vi
220 if (Isalnum(p) || p == '_')
229 * Return if p is part of a big word according to vi
239 * Find the previous word
242 c__prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
247 while ((p >= low) && !(*wtest)(*p))
249 while ((p >= low) && (*wtest)(*p))
253 /* cp now points to one character before the word */
257 /* cp now points where we want it */
266 c__next_word(Char *p, Char *high, int n, int (*wtest)(wint_t))
269 while ((p < high) && !(*wtest)(*p))
271 while ((p < high) && (*wtest)(*p))
276 /* p now points where we want it */
281 * Find the next word vi style
284 cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(wint_t))
290 while ((p < high) && (*wtest)(*p) == test)
293 * vi historically deletes with cw only the word preserving the
294 * trailing whitespace! This is not what 'w' does..
296 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
297 while ((p < high) && Isspace(*p))
301 /* p now points where we want it */
310 * Find the previous word vi style
313 cv_prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
319 while ((p > low) && Isspace(*p))
322 while ((p >= low) && (*wtest)(*p) == test)
327 /* p now points where we want it */
336 * Finish vi delete action
339 cv_delfini(EditLine *el)
342 int action = el->el_chared.c_vcmd.action;
345 el->el_map.current = el->el_map.key;
347 if (el->el_chared.c_vcmd.pos == 0)
351 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
354 el->el_line.cursor = el->el_chared.c_vcmd.pos;
357 cv_yank(el, el->el_line.cursor, size);
359 cv_yank(el, el->el_line.cursor + size, -size);
362 c_delafter(el, size);
363 re_refresh_cursor(el);
365 c_delbefore(el, -size);
366 el->el_line.cursor += size;
369 el->el_chared.c_vcmd.action = NOP;
374 * Go to the end of this word according to vi
377 cv__endword(Char *p, Char *high, int n, int (*wtest)(wint_t))
384 while ((p < high) && Isspace(*p))
388 while ((p < high) && (*wtest)(*p) == test)
396 * Initialize the character editor
399 ch_init(EditLine *el)
401 c_macro_t *ma = &el->el_chared.c_macro;
403 el->el_line.buffer = el_malloc(EL_BUFSIZ *
404 sizeof(*el->el_line.buffer));
405 if (el->el_line.buffer == NULL)
408 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
409 sizeof(*el->el_line.buffer));
410 el->el_line.cursor = el->el_line.buffer;
411 el->el_line.lastchar = el->el_line.buffer;
412 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
414 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
415 sizeof(*el->el_chared.c_undo.buf));
416 if (el->el_chared.c_undo.buf == NULL)
418 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
419 sizeof(*el->el_chared.c_undo.buf));
420 el->el_chared.c_undo.len = -1;
421 el->el_chared.c_undo.cursor = 0;
422 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
423 sizeof(*el->el_chared.c_redo.buf));
424 if (el->el_chared.c_redo.buf == NULL)
426 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
427 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
428 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
430 el->el_chared.c_vcmd.action = NOP;
431 el->el_chared.c_vcmd.pos = el->el_line.buffer;
433 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
434 sizeof(*el->el_chared.c_kill.buf));
435 if (el->el_chared.c_kill.buf == NULL)
437 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
438 sizeof(*el->el_chared.c_kill.buf));
439 el->el_chared.c_kill.mark = el->el_line.buffer;
440 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
441 el->el_chared.c_resizefun = NULL;
442 el->el_chared.c_resizearg = NULL;
443 el->el_chared.c_aliasfun = NULL;
444 el->el_chared.c_aliasarg = NULL;
446 el->el_map.current = el->el_map.key;
448 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
449 el->el_state.doingarg = 0;
450 el->el_state.metanext = 0;
451 el->el_state.argument = 1;
452 el->el_state.lastcmd = ED_UNASSIGNED;
456 ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
457 if (ma->macro == NULL)
463 * Reset the character editor
466 ch_reset(EditLine *el, int mclear)
468 el->el_line.cursor = el->el_line.buffer;
469 el->el_line.lastchar = el->el_line.buffer;
471 el->el_chared.c_undo.len = -1;
472 el->el_chared.c_undo.cursor = 0;
474 el->el_chared.c_vcmd.action = NOP;
475 el->el_chared.c_vcmd.pos = el->el_line.buffer;
477 el->el_chared.c_kill.mark = el->el_line.buffer;
479 el->el_map.current = el->el_map.key;
481 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
482 el->el_state.doingarg = 0;
483 el->el_state.metanext = 0;
484 el->el_state.argument = 1;
485 el->el_state.lastcmd = ED_UNASSIGNED;
487 el->el_history.eventno = 0;
494 ch__clearmacro(EditLine *el)
496 c_macro_t *ma = &el->el_chared.c_macro;
497 while (ma->level >= 0)
498 el_free(ma->macro[ma->level--]);
502 * Enlarge line buffer to be able to hold twice as much characters.
503 * Returns 1 if successful, 0 if not.
506 ch_enlargebufs(EditLine *el, size_t addlen)
509 Char *newbuffer, *oldbuf, *oldkbuf;
511 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
514 * If newly required length is longer than current buffer, we need
515 * to make the buffer big enough to hold both old and new stuff.
518 while(newsz - sz < addlen)
523 * Reallocate line buffer.
525 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
529 /* zero the newly added memory, leave old data in */
530 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
532 oldbuf = el->el_line.buffer;
534 el->el_line.buffer = newbuffer;
535 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
536 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
537 /* don't set new size until all buffers are enlarged */
538 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
541 * Reallocate kill buffer.
543 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
548 /* zero the newly added memory, leave old data in */
549 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
551 oldkbuf = el->el_chared.c_kill.buf;
553 el->el_chared.c_kill.buf = newbuffer;
554 el->el_chared.c_kill.last = newbuffer +
555 (el->el_chared.c_kill.last - oldkbuf);
556 el->el_chared.c_kill.mark = el->el_line.buffer +
557 (el->el_chared.c_kill.mark - oldbuf);
560 * Reallocate undo buffer.
562 newbuffer = el_realloc(el->el_chared.c_undo.buf,
563 newsz * sizeof(*newbuffer));
567 /* zero the newly added memory, leave old data in */
568 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
569 el->el_chared.c_undo.buf = newbuffer;
571 newbuffer = el_realloc(el->el_chared.c_redo.buf,
572 newsz * sizeof(*newbuffer));
575 el->el_chared.c_redo.pos = newbuffer +
576 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
577 el->el_chared.c_redo.lim = newbuffer +
578 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
579 el->el_chared.c_redo.buf = newbuffer;
581 if (!hist_enlargebuf(el, sz, newsz))
584 /* Safe to set enlarged buffer size */
585 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
586 if (el->el_chared.c_resizefun)
587 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
592 * Free the data structures used by the editor
597 el_free(el->el_line.buffer);
598 el->el_line.buffer = NULL;
599 el->el_line.limit = NULL;
600 el_free(el->el_chared.c_undo.buf);
601 el->el_chared.c_undo.buf = NULL;
602 el_free(el->el_chared.c_redo.buf);
603 el->el_chared.c_redo.buf = NULL;
604 el->el_chared.c_redo.pos = NULL;
605 el->el_chared.c_redo.lim = NULL;
606 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
607 el_free(el->el_chared.c_kill.buf);
608 el->el_chared.c_kill.buf = NULL;
610 el_free(el->el_chared.c_macro.macro);
611 el->el_chared.c_macro.macro = NULL;
616 * Insert string at cursorI
619 FUN(el,insertstr)(EditLine *el, const Char *s)
623 if (s == NULL || (len = Strlen(s)) == 0)
625 if (el->el_line.lastchar + len >= el->el_line.limit) {
626 if (!ch_enlargebufs(el, len))
630 c_insert(el, (int)len);
632 *el->el_line.cursor++ = *s++;
638 * Delete num characters before the cursor
641 el_deletestr(EditLine *el, int n)
646 if (el->el_line.cursor < &el->el_line.buffer[n])
649 c_delbefore(el, n); /* delete before dot */
650 el->el_line.cursor -= n;
651 if (el->el_line.cursor < el->el_line.buffer)
652 el->el_line.cursor = el->el_line.buffer;
656 * Move the cursor to the left or the right of the current position
659 el_cursor(EditLine *el, int n)
664 el->el_line.cursor += n;
666 if (el->el_line.cursor < el->el_line.buffer)
667 el->el_line.cursor = el->el_line.buffer;
668 if (el->el_line.cursor > el->el_line.lastchar)
669 el->el_line.cursor = el->el_line.lastchar;
671 return (int)(el->el_line.cursor - el->el_line.buffer);
678 c_gets(EditLine *el, Char *buf, const Char *prompt)
682 Char *cp = el->el_line.buffer, ch;
685 len = (ssize_t)Strlen(prompt);
686 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
692 el->el_line.cursor = cp;
694 el->el_line.lastchar = cp + 1;
697 if (el_wgetc(el, &wch) != 1) {
698 ed_end_of_file(el, 0);
706 case L'\b': /* Delete and backspace */
717 case L'\r': /* Newline */
723 if (len >= (ssize_t)(EL_BUFSIZ - 16))
734 el->el_line.buffer[0] = '\0';
735 el->el_line.lastchar = el->el_line.buffer;
736 el->el_line.cursor = el->el_line.buffer;
742 * Return the current horizontal position of the cursor
750 * Find how many characters till the beginning of this line.
752 if (el->el_line.cursor == el->el_line.buffer)
755 for (ptr = el->el_line.cursor - 1;
756 ptr >= el->el_line.buffer && *ptr != '\n';
759 return (int)(el->el_line.cursor - ptr - 1);
764 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
766 el->el_chared.c_resizefun = f;
767 el->el_chared.c_resizearg = a;
772 ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
774 el->el_chared.c_aliasfun = f;
775 el->el_chared.c_aliasarg = a;