2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)chared.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: chared.c,v 1.23 2005/06/01 11:37:52 lukem Exp $
34 * $DragonFly: src/lib/libedit/chared.c,v 1.7 2005/11/13 11:58:30 corecode Exp $
40 * chared.c: Character editor utilities
45 /* value to leave unused in line buffer */
49 * Handle state for the vi undo command
54 c_undo_t *vu = &el->el_chared.c_undo;
55 c_redo_t *r = &el->el_chared.c_redo;
58 /* Save entire line for undo */
59 size = el->el_line.lastchar - el->el_line.buffer;
61 vu->cursor = el->el_line.cursor - el->el_line.buffer;
62 memcpy(vu->buf, el->el_line.buffer, size);
64 /* save command info for redo */
65 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
66 r->action = el->el_chared.c_vcmd.action;
68 r->cmd = el->el_state.thiscmd;
69 r->ch = el->el_state.thisch;
73 * Save yank/delete data for paste
76 cv_yank(EditLine *el, const char *ptr, int size)
78 c_kill_t *k = &el->el_chared.c_kill;
80 memcpy(k->buf, ptr, size +0u);
81 k->last = k->buf + size;
86 * Insert num characters
89 c_insert(EditLine *el, int num)
93 if (el->el_line.lastchar + num >= el->el_line.limit) {
94 if (!ch_enlargebufs(el, num +0u))
95 return; /* can't go past end of buffer */
98 if (el->el_line.cursor < el->el_line.lastchar) {
99 /* if I must move chars */
100 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
103 el->el_line.lastchar += num;
108 * Delete num characters after the cursor
111 c_delafter(EditLine *el, int num)
114 if (el->el_line.cursor + num > el->el_line.lastchar)
115 num = el->el_line.lastchar - el->el_line.cursor;
117 if (el->el_map.current != el->el_map.emacs) {
119 cv_yank(el, el->el_line.cursor, num);
125 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
128 el->el_line.lastchar -= num;
134 * Delete the character after the cursor, do not yank
137 c_delafter1(EditLine *el)
141 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
144 el->el_line.lastchar--;
149 * Delete num characters before the cursor
152 c_delbefore(EditLine *el, int num)
155 if (el->el_line.cursor - num < el->el_line.buffer)
156 num = el->el_line.cursor - el->el_line.buffer;
158 if (el->el_map.current != el->el_map.emacs) {
160 cv_yank(el, el->el_line.cursor - num, num);
166 for (cp = el->el_line.cursor - num;
167 cp <= el->el_line.lastchar;
171 el->el_line.lastchar -= num;
177 * Delete the character before the cursor, do not yank
180 c_delbefore1(EditLine *el)
184 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
187 el->el_line.lastchar--;
192 * Return if p is part of a word according to emacs
197 return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
202 * Return if p is part of a word according to vi
207 if (isalnum(p) || p == '_')
216 * Return if p is part of a big word according to vi
221 return (!isspace(p));
226 * Find the previous word
229 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
234 while ((p >= low) && !(*wtest)((unsigned char) *p))
236 while ((p >= low) && (*wtest)((unsigned char) *p))
240 /* cp now points to one character before the word */
244 /* cp now points where we want it */
253 c__next_word(char *p, char *high, int n, int (*wtest)(int))
256 while ((p < high) && !(*wtest)((unsigned char) *p))
258 while ((p < high) && (*wtest)((unsigned char) *p))
263 /* p now points where we want it */
268 * Find the next word vi style
271 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
276 test = (*wtest)((unsigned char) *p);
277 while ((p < high) && (*wtest)((unsigned char) *p) == test)
280 * vi historically deletes with cw only the word preserving the
281 * trailing whitespace! This is not what 'w' does..
283 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
284 while ((p < high) && isspace((unsigned char) *p))
288 /* p now points where we want it */
297 * Find the previous word vi style
300 cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
306 while ((p > low) && isspace((unsigned char) *p))
308 test = (*wtest)((unsigned char) *p);
309 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
314 /* p now points where we want it */
324 * Ignore character p points to, return number appearing after that.
325 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
326 * Return p pointing to last char used.
330 char *p, /* character position */
331 int *num, /* Return value */
332 int dval) /* dval is the number to subtract from like $-3 */
343 *num = 0x7fffffff; /* Handle $ */
346 sign = -1; /* Handle $- */
349 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
351 *num = (sign < 0 ? dval - i : i);
357 * Finish vi delete action
360 cv_delfini(EditLine *el)
363 int action = el->el_chared.c_vcmd.action;
366 el->el_map.current = el->el_map.key;
368 if (el->el_chared.c_vcmd.pos == 0)
372 size = el->el_line.cursor - el->el_chared.c_vcmd.pos;
375 el->el_line.cursor = el->el_chared.c_vcmd.pos;
378 cv_yank(el, el->el_line.cursor, size);
380 cv_yank(el, el->el_line.cursor + size, -size);
383 c_delafter(el, size);
384 re_refresh_cursor(el);
386 c_delbefore(el, -size);
387 el->el_line.cursor += size;
390 el->el_chared.c_vcmd.action = NOP;
396 * Go to the end of this word according to emacs
399 ce__endword(char *p, char *high, int n)
404 while ((p < high) && isspace((unsigned char) *p))
406 while ((p < high) && !isspace((unsigned char) *p))
417 * Go to the end of this word according to vi
420 cv__endword(char *p, char *high, int n, int (*wtest)(int))
427 while ((p < high) && isspace((unsigned char) *p))
430 test = (*wtest)((unsigned char) *p);
431 while ((p < high) && (*wtest)((unsigned char) *p) == test)
439 * Initialize the character editor
442 ch_init(EditLine *el)
444 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
445 if (el->el_line.buffer == NULL)
448 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
449 el->el_line.cursor = el->el_line.buffer;
450 el->el_line.lastchar = el->el_line.buffer;
451 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
453 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
454 if (el->el_chared.c_undo.buf == NULL)
456 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
457 el->el_chared.c_undo.len = -1;
458 el->el_chared.c_undo.cursor = 0;
459 el->el_chared.c_redo.buf = (char *) el_malloc(EL_BUFSIZ);
460 if (el->el_chared.c_redo.buf == NULL)
462 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
463 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
464 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
466 el->el_chared.c_vcmd.action = NOP;
467 el->el_chared.c_vcmd.pos = el->el_line.buffer;
469 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
470 if (el->el_chared.c_kill.buf == NULL)
472 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
473 el->el_chared.c_kill.mark = el->el_line.buffer;
474 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
476 el->el_map.current = el->el_map.key;
478 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
479 el->el_state.doingarg = 0;
480 el->el_state.metanext = 0;
481 el->el_state.argument = 1;
482 el->el_state.lastcmd = ED_UNASSIGNED;
484 el->el_chared.c_macro.level = -1;
485 el->el_chared.c_macro.offset = 0;
486 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
488 if (el->el_chared.c_macro.macro == NULL)
494 * Reset the character editor
497 ch_reset(EditLine *el)
499 el->el_line.cursor = el->el_line.buffer;
500 el->el_line.lastchar = el->el_line.buffer;
502 el->el_chared.c_undo.len = -1;
503 el->el_chared.c_undo.cursor = 0;
505 el->el_chared.c_vcmd.action = NOP;
506 el->el_chared.c_vcmd.pos = el->el_line.buffer;
508 el->el_chared.c_kill.mark = el->el_line.buffer;
510 el->el_map.current = el->el_map.key;
512 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
513 el->el_state.doingarg = 0;
514 el->el_state.metanext = 0;
515 el->el_state.argument = 1;
516 el->el_state.lastcmd = ED_UNASSIGNED;
518 el->el_chared.c_macro.level = -1;
520 el->el_history.eventno = 0;
524 * Enlarge line buffer to be able to hold twice as much characters.
525 * Returns 1 if successful, 0 if not.
528 ch_enlargebufs(el, addlen)
533 char *newbuffer, *oldbuf, *oldkbuf;
535 sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
538 * If newly required length is longer than current buffer, we need
539 * to make the buffer big enough to hold both old and new stuff.
542 while(newsz - sz < addlen)
547 * Reallocate line buffer.
549 newbuffer = el_realloc(el->el_line.buffer, newsz);
553 /* zero the newly added memory, leave old data in */
554 (void) memset(&newbuffer[sz], 0, newsz - sz);
556 oldbuf = el->el_line.buffer;
558 el->el_line.buffer = newbuffer;
559 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
560 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
561 /* don't set new size until all buffers are enlarged */
562 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
565 * Reallocate kill buffer.
567 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
571 /* zero the newly added memory, leave old data in */
572 (void) memset(&newbuffer[sz], 0, newsz - sz);
574 oldkbuf = el->el_chared.c_kill.buf;
576 el->el_chared.c_kill.buf = newbuffer;
577 el->el_chared.c_kill.last = newbuffer +
578 (el->el_chared.c_kill.last - oldkbuf);
579 el->el_chared.c_kill.mark = el->el_line.buffer +
580 (el->el_chared.c_kill.mark - oldbuf);
583 * Reallocate undo buffer.
585 newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
589 /* zero the newly added memory, leave old data in */
590 (void) memset(&newbuffer[sz], 0, newsz - sz);
591 el->el_chared.c_undo.buf = newbuffer;
593 newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz);
596 el->el_chared.c_redo.pos = newbuffer +
597 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
598 el->el_chared.c_redo.lim = newbuffer +
599 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
600 el->el_chared.c_redo.buf = newbuffer;
602 if (!hist_enlargebuf(el, sz, newsz))
605 /* Safe to set enlarged buffer size */
606 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
611 * Free the data structures used by the editor
616 el_free((ptr_t) el->el_line.buffer);
617 el->el_line.buffer = NULL;
618 el->el_line.limit = NULL;
619 el_free((ptr_t) el->el_chared.c_undo.buf);
620 el->el_chared.c_undo.buf = NULL;
621 el_free((ptr_t) el->el_chared.c_redo.buf);
622 el->el_chared.c_redo.buf = NULL;
623 el->el_chared.c_redo.pos = NULL;
624 el->el_chared.c_redo.lim = NULL;
625 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
626 el_free((ptr_t) el->el_chared.c_kill.buf);
627 el->el_chared.c_kill.buf = NULL;
628 el_free((ptr_t) el->el_chared.c_macro.macro);
629 el->el_chared.c_macro.macro = NULL;
635 * Insert string at cursorI
638 el_insertstr(EditLine *el, const char *s)
642 if ((len = strlen(s)) == 0)
644 if (el->el_line.lastchar + len >= el->el_line.limit) {
645 if (!ch_enlargebufs(el, len))
649 c_insert(el, (int)len);
651 *el->el_line.cursor++ = *s++;
657 * Delete num characters before the cursor
660 el_deletestr(EditLine *el, int n)
665 if (el->el_line.cursor < &el->el_line.buffer[n])
668 c_delbefore(el, n); /* delete before dot */
669 el->el_line.cursor -= n;
670 if (el->el_line.cursor < el->el_line.buffer)
671 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;
685 len = strlen(prompt);
686 memcpy(cp, prompt, len + 0u);
692 el->el_line.cursor = cp;
694 el->el_line.lastchar = cp + 1;
697 if (el_getc(el, &ch) != 1) {
698 ed_end_of_file(el, 0);
705 case 0010: /* Delete and backspace */
715 case '\r': /* Newline */
721 if (len >= EL_BUFSIZ - 16)
732 el->el_line.buffer[0] = '\0';
733 el->el_line.lastchar = el->el_line.buffer;
734 el->el_line.cursor = el->el_line.buffer;
740 * Return the current horizontal position of the cursor
748 * Find how many characters till the beginning of this line.
750 if (el->el_line.cursor == el->el_line.buffer)
753 for (ptr = el->el_line.cursor - 1;
754 ptr >= el->el_line.buffer && *ptr != '\n';
757 return (el->el_line.cursor - ptr - 1);