1 /* $NetBSD: chared.c,v 1.40 2014/06/18 18:12:28 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.40 2014/06/18 18:12:28 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * chared.c: Character editor utilities
52 private void ch__clearmacro (EditLine *);
54 /* value to leave unused in line buffer */
58 * Handle state for the vi undo command
63 c_undo_t *vu = &el->el_chared.c_undo;
64 c_redo_t *r = &el->el_chared.c_redo;
67 /* Save entire line for undo */
68 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
69 vu->len = (ssize_t)size;
70 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
71 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
73 /* save command info for redo */
74 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
75 r->action = el->el_chared.c_vcmd.action;
77 r->cmd = el->el_state.thiscmd;
78 r->ch = el->el_state.thisch;
82 * Save yank/delete data for paste
85 cv_yank(EditLine *el, const Char *ptr, int size)
87 c_kill_t *k = &el->el_chared.c_kill;
89 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
90 k->last = k->buf + size;
95 * Insert num characters
98 c_insert(EditLine *el, int num)
102 if (el->el_line.lastchar + num >= el->el_line.limit) {
103 if (!ch_enlargebufs(el, (size_t)num))
104 return; /* can't go past end of buffer */
107 if (el->el_line.cursor < el->el_line.lastchar) {
108 /* if I must move chars */
109 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
112 el->el_line.lastchar += num;
117 * Delete num characters after the cursor
120 c_delafter(EditLine *el, int num)
123 if (el->el_line.cursor + num > el->el_line.lastchar)
124 num = (int)(el->el_line.lastchar - el->el_line.cursor);
126 if (el->el_map.current != el->el_map.emacs) {
128 cv_yank(el, el->el_line.cursor, num);
134 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
137 el->el_line.lastchar -= num;
143 * Delete the character after the cursor, do not yank
146 c_delafter1(EditLine *el)
150 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
153 el->el_line.lastchar--;
158 * Delete num characters before the cursor
161 c_delbefore(EditLine *el, int num)
164 if (el->el_line.cursor - num < el->el_line.buffer)
165 num = (int)(el->el_line.cursor - el->el_line.buffer);
167 if (el->el_map.current != el->el_map.emacs) {
169 cv_yank(el, el->el_line.cursor - num, num);
175 for (cp = el->el_line.cursor - num;
176 cp <= el->el_line.lastchar;
180 el->el_line.lastchar -= num;
186 * Delete the character before the cursor, do not yank
189 c_delbefore1(EditLine *el)
193 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
196 el->el_line.lastchar--;
201 * Return if p is part of a word according to emacs
206 return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
211 * Return if p is part of a word according to vi
216 if (Isalnum(p) || p == '_')
225 * Return if p is part of a big word according to vi
235 * Find the previous word
238 c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
243 while ((p >= low) && !(*wtest)(*p))
245 while ((p >= low) && (*wtest)(*p))
249 /* cp now points to one character before the word */
253 /* cp now points where we want it */
262 c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
265 while ((p < high) && !(*wtest)(*p))
267 while ((p < high) && (*wtest)(*p))
272 /* p now points where we want it */
277 * Find the next word vi style
280 cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
286 while ((p < high) && (*wtest)(*p) == test)
289 * vi historically deletes with cw only the word preserving the
290 * trailing whitespace! This is not what 'w' does..
292 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
293 while ((p < high) && Isspace(*p))
297 /* p now points where we want it */
306 * Find the previous word vi style
309 cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
315 while ((p > low) && Isspace(*p))
318 while ((p >= low) && (*wtest)(*p) == test)
323 /* p now points where we want it */
332 * Finish vi delete action
335 cv_delfini(EditLine *el)
338 int action = el->el_chared.c_vcmd.action;
341 el->el_map.current = el->el_map.key;
343 if (el->el_chared.c_vcmd.pos == 0)
347 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
350 el->el_line.cursor = el->el_chared.c_vcmd.pos;
353 cv_yank(el, el->el_line.cursor, size);
355 cv_yank(el, el->el_line.cursor + size, -size);
358 c_delafter(el, size);
359 re_refresh_cursor(el);
361 c_delbefore(el, -size);
362 el->el_line.cursor += size;
365 el->el_chared.c_vcmd.action = NOP;
370 * Go to the end of this word according to vi
373 cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
380 while ((p < high) && Isspace(*p))
384 while ((p < high) && (*wtest)(*p) == test)
392 * Initialize the character editor
395 ch_init(EditLine *el)
397 c_macro_t *ma = &el->el_chared.c_macro;
399 el->el_line.buffer = el_malloc(EL_BUFSIZ *
400 sizeof(*el->el_line.buffer));
401 if (el->el_line.buffer == NULL)
404 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
405 sizeof(*el->el_line.buffer));
406 el->el_line.cursor = el->el_line.buffer;
407 el->el_line.lastchar = el->el_line.buffer;
408 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
410 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
411 sizeof(*el->el_chared.c_undo.buf));
412 if (el->el_chared.c_undo.buf == NULL)
414 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
415 sizeof(*el->el_chared.c_undo.buf));
416 el->el_chared.c_undo.len = -1;
417 el->el_chared.c_undo.cursor = 0;
418 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
419 sizeof(*el->el_chared.c_redo.buf));
420 if (el->el_chared.c_redo.buf == NULL)
422 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
423 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
424 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
426 el->el_chared.c_vcmd.action = NOP;
427 el->el_chared.c_vcmd.pos = el->el_line.buffer;
429 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
430 sizeof(*el->el_chared.c_kill.buf));
431 if (el->el_chared.c_kill.buf == NULL)
433 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
434 sizeof(*el->el_chared.c_kill.buf));
435 el->el_chared.c_kill.mark = el->el_line.buffer;
436 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
437 el->el_chared.c_resizefun = NULL;
438 el->el_chared.c_resizearg = NULL;
439 el->el_chared.c_aliasfun = NULL;
440 el->el_chared.c_aliasarg = NULL;
442 el->el_map.current = el->el_map.key;
444 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
445 el->el_state.doingarg = 0;
446 el->el_state.metanext = 0;
447 el->el_state.argument = 1;
448 el->el_state.lastcmd = ED_UNASSIGNED;
452 ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
453 if (ma->macro == NULL)
459 * Reset the character editor
462 ch_reset(EditLine *el, int mclear)
464 el->el_line.cursor = el->el_line.buffer;
465 el->el_line.lastchar = el->el_line.buffer;
467 el->el_chared.c_undo.len = -1;
468 el->el_chared.c_undo.cursor = 0;
470 el->el_chared.c_vcmd.action = NOP;
471 el->el_chared.c_vcmd.pos = el->el_line.buffer;
473 el->el_chared.c_kill.mark = el->el_line.buffer;
475 el->el_map.current = el->el_map.key;
477 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
478 el->el_state.doingarg = 0;
479 el->el_state.metanext = 0;
480 el->el_state.argument = 1;
481 el->el_state.lastcmd = ED_UNASSIGNED;
483 el->el_history.eventno = 0;
490 ch__clearmacro(EditLine *el)
492 c_macro_t *ma = &el->el_chared.c_macro;
493 while (ma->level >= 0)
494 el_free(ma->macro[ma->level--]);
498 * Enlarge line buffer to be able to hold twice as much characters.
499 * Returns 1 if successful, 0 if not.
502 ch_enlargebufs(EditLine *el, size_t addlen)
505 Char *newbuffer, *oldbuf, *oldkbuf;
507 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
510 * If newly required length is longer than current buffer, we need
511 * to make the buffer big enough to hold both old and new stuff.
514 while(newsz - sz < addlen)
519 * Reallocate line buffer.
521 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
525 /* zero the newly added memory, leave old data in */
526 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
528 oldbuf = el->el_line.buffer;
530 el->el_line.buffer = newbuffer;
531 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
532 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
533 /* don't set new size until all buffers are enlarged */
534 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
537 * Reallocate kill buffer.
539 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
544 /* zero the newly added memory, leave old data in */
545 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
547 oldkbuf = el->el_chared.c_kill.buf;
549 el->el_chared.c_kill.buf = newbuffer;
550 el->el_chared.c_kill.last = newbuffer +
551 (el->el_chared.c_kill.last - oldkbuf);
552 el->el_chared.c_kill.mark = el->el_line.buffer +
553 (el->el_chared.c_kill.mark - oldbuf);
556 * Reallocate undo buffer.
558 newbuffer = el_realloc(el->el_chared.c_undo.buf,
559 newsz * sizeof(*newbuffer));
563 /* zero the newly added memory, leave old data in */
564 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
565 el->el_chared.c_undo.buf = newbuffer;
567 newbuffer = el_realloc(el->el_chared.c_redo.buf,
568 newsz * sizeof(*newbuffer));
571 el->el_chared.c_redo.pos = newbuffer +
572 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
573 el->el_chared.c_redo.lim = newbuffer +
574 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
575 el->el_chared.c_redo.buf = newbuffer;
577 if (!hist_enlargebuf(el, sz, newsz))
580 /* Safe to set enlarged buffer size */
581 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
582 if (el->el_chared.c_resizefun)
583 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
588 * Free the data structures used by the editor
593 el_free(el->el_line.buffer);
594 el->el_line.buffer = NULL;
595 el->el_line.limit = NULL;
596 el_free(el->el_chared.c_undo.buf);
597 el->el_chared.c_undo.buf = NULL;
598 el_free(el->el_chared.c_redo.buf);
599 el->el_chared.c_redo.buf = NULL;
600 el->el_chared.c_redo.pos = NULL;
601 el->el_chared.c_redo.lim = NULL;
602 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
603 el_free(el->el_chared.c_kill.buf);
604 el->el_chared.c_kill.buf = NULL;
606 el_free(el->el_chared.c_macro.macro);
607 el->el_chared.c_macro.macro = NULL;
612 * Insert string at cursorI
615 FUN(el,insertstr)(EditLine *el, const Char *s)
619 if (s == NULL || (len = Strlen(s)) == 0)
621 if (el->el_line.lastchar + len >= el->el_line.limit) {
622 if (!ch_enlargebufs(el, len))
626 c_insert(el, (int)len);
628 *el->el_line.cursor++ = *s++;
634 * Delete num characters before the cursor
637 el_deletestr(EditLine *el, int n)
642 if (el->el_line.cursor < &el->el_line.buffer[n])
645 c_delbefore(el, n); /* delete before dot */
646 el->el_line.cursor -= n;
647 if (el->el_line.cursor < el->el_line.buffer)
648 el->el_line.cursor = el->el_line.buffer;
652 * Move the cursor to the left or the right of the current position
655 el_cursor(EditLine *el, int n)
660 el->el_line.cursor += n;
662 if (el->el_line.cursor < el->el_line.buffer)
663 el->el_line.cursor = el->el_line.buffer;
664 if (el->el_line.cursor > el->el_line.lastchar)
665 el->el_line.cursor = el->el_line.lastchar;
667 return (int)(el->el_line.cursor - el->el_line.buffer);
674 c_gets(EditLine *el, Char *buf, const Char *prompt)
678 Char *cp = el->el_line.buffer;
681 len = (ssize_t)Strlen(prompt);
682 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
688 el->el_line.cursor = cp;
690 el->el_line.lastchar = cp + 1;
693 if (FUN(el,getc)(el, &ch) != 1) {
694 ed_end_of_file(el, 0);
701 case 0010: /* Delete and backspace */
711 case '\r': /* Newline */
717 if (len >= (ssize_t)(EL_BUFSIZ - 16))
728 el->el_line.buffer[0] = '\0';
729 el->el_line.lastchar = el->el_line.buffer;
730 el->el_line.cursor = el->el_line.buffer;
736 * Return the current horizontal position of the cursor
744 * Find how many characters till the beginning of this line.
746 if (el->el_line.cursor == el->el_line.buffer)
749 for (ptr = el->el_line.cursor - 1;
750 ptr >= el->el_line.buffer && *ptr != '\n';
753 return (int)(el->el_line.cursor - ptr - 1);
758 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
760 el->el_chared.c_resizefun = f;
761 el->el_chared.c_resizearg = a;
766 ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
768 el->el_chared.c_aliasfun = f;
769 el->el_chared.c_aliasarg = a;