1 /* $NetBSD: chared.c,v 1.63 2022/10/30 19:11:31 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.63 2022/10/30 19:11:31 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * chared.c: Character editor utilities
55 /* value to leave unused in line buffer */
59 * Handle state for the vi undo command
64 c_undo_t *vu = &el->el_chared.c_undo;
65 c_redo_t *r = &el->el_chared.c_redo;
68 /* Save entire line for undo */
69 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
70 vu->len = (ssize_t)size;
71 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
72 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
74 /* save command info for redo */
75 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
76 r->action = el->el_chared.c_vcmd.action;
78 r->cmd = el->el_state.thiscmd;
79 r->ch = el->el_state.thisch;
83 * Save yank/delete data for paste
86 cv_yank(EditLine *el, const wchar_t *ptr, int size)
88 c_kill_t *k = &el->el_chared.c_kill;
90 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
91 k->last = k->buf + size;
96 * Insert num characters
99 c_insert(EditLine *el, int num)
103 if (el->el_line.lastchar + num >= el->el_line.limit) {
104 if (!ch_enlargebufs(el, (size_t)num))
105 return; /* can't go past end of buffer */
108 if (el->el_line.cursor < el->el_line.lastchar) {
109 /* if I must move chars */
110 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
113 el->el_line.lastchar += num;
118 * Delete num characters after the cursor
121 c_delafter(EditLine *el, int num)
124 if (el->el_line.cursor + num > el->el_line.lastchar)
125 num = (int)(el->el_line.lastchar - el->el_line.cursor);
127 if (el->el_map.current != el->el_map.emacs) {
129 cv_yank(el, el->el_line.cursor, num);
135 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
138 el->el_line.lastchar -= num;
144 * Delete the character after the cursor, do not yank
147 c_delafter1(EditLine *el)
151 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
154 el->el_line.lastchar--;
159 * Delete num characters before the cursor
162 c_delbefore(EditLine *el, int num)
165 if (el->el_line.cursor - num < el->el_line.buffer)
166 num = (int)(el->el_line.cursor - el->el_line.buffer);
168 if (el->el_map.current != el->el_map.emacs) {
170 cv_yank(el, el->el_line.cursor - num, num);
176 for (cp = el->el_line.cursor - num;
177 &cp[num] <= el->el_line.lastchar;
181 el->el_line.lastchar -= num;
187 * Delete the character before the cursor, do not yank
190 c_delbefore1(EditLine *el)
194 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
197 el->el_line.lastchar--;
202 * Return if p is part of a word according to emacs
207 return iswalnum(p) || wcschr(L"*?_-.[]~=", p) != NULL;
212 * Return if p is part of a word according to vi
217 if (iswalnum(p) || p == L'_')
226 * Return if p is part of a big word according to vi
236 * Find the previous word
238 libedit_private wchar_t *
239 c__prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
244 while ((p >= low) && !(*wtest)(*p))
246 while ((p >= low) && (*wtest)(*p))
250 /* cp now points to one character before the word */
254 /* cp now points where we want it */
262 libedit_private wchar_t *
263 c__next_word(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
266 while ((p < high) && !(*wtest)(*p))
268 while ((p < high) && (*wtest)(*p))
273 /* p now points where we want it */
278 * Find the next word vi style
280 libedit_private wchar_t *
281 cv_next_word(EditLine *el, wchar_t *p, wchar_t *high, int n,
282 int (*wtest)(wint_t))
288 while ((p < high) && (*wtest)(*p) == test)
291 * vi historically deletes with cw only the word preserving the
292 * trailing whitespace! This is not what 'w' does..
294 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
295 while ((p < high) && iswspace(*p))
299 /* p now points where we want it */
308 * Find the previous word vi style
310 libedit_private wchar_t *
311 cv_prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
317 while ((p > low) && iswspace(*p))
320 while ((p >= low) && (*wtest)(*p) == test)
325 /* p now points where we want it */
334 * Finish vi delete action
337 cv_delfini(EditLine *el)
340 int action = el->el_chared.c_vcmd.action;
343 el->el_map.current = el->el_map.key;
345 if (el->el_chared.c_vcmd.pos == 0)
349 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
352 el->el_line.cursor = el->el_chared.c_vcmd.pos;
355 cv_yank(el, el->el_line.cursor, size);
357 cv_yank(el, el->el_line.cursor + size, -size);
360 c_delafter(el, size);
361 re_refresh_cursor(el);
363 c_delbefore(el, -size);
364 el->el_line.cursor += size;
367 el->el_chared.c_vcmd.action = NOP;
372 * Go to the end of this word according to vi
374 libedit_private wchar_t *
375 cv__endword(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
382 while ((p < high) && iswspace(*p))
386 while ((p < high) && (*wtest)(*p) == test)
394 * Initialize the character editor
397 ch_init(EditLine *el)
399 el->el_line.buffer = el_calloc(EL_BUFSIZ,
400 sizeof(*el->el_line.buffer));
401 if (el->el_line.buffer == NULL)
404 el->el_line.cursor = el->el_line.buffer;
405 el->el_line.lastchar = el->el_line.buffer;
406 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
408 el->el_chared.c_undo.buf = el_calloc(EL_BUFSIZ,
409 sizeof(*el->el_chared.c_undo.buf));
410 if (el->el_chared.c_undo.buf == NULL)
412 el->el_chared.c_undo.len = -1;
413 el->el_chared.c_undo.cursor = 0;
414 el->el_chared.c_redo.buf = el_calloc(EL_BUFSIZ,
415 sizeof(*el->el_chared.c_redo.buf));
416 if (el->el_chared.c_redo.buf == NULL)
418 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
419 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
420 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
422 el->el_chared.c_vcmd.action = NOP;
423 el->el_chared.c_vcmd.pos = el->el_line.buffer;
425 el->el_chared.c_kill.buf = el_calloc(EL_BUFSIZ,
426 sizeof(*el->el_chared.c_kill.buf));
427 if (el->el_chared.c_kill.buf == NULL)
429 el->el_chared.c_kill.mark = el->el_line.buffer;
430 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
431 el->el_chared.c_resizefun = NULL;
432 el->el_chared.c_resizearg = NULL;
433 el->el_chared.c_aliasfun = NULL;
434 el->el_chared.c_aliasarg = NULL;
436 el->el_map.current = el->el_map.key;
438 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
439 el->el_state.doingarg = 0;
440 el->el_state.metanext = 0;
441 el->el_state.argument = 1;
442 el->el_state.lastcmd = ED_UNASSIGNED;
451 * Reset the character editor
454 ch_reset(EditLine *el)
456 el->el_line.cursor = el->el_line.buffer;
457 el->el_line.lastchar = el->el_line.buffer;
459 el->el_chared.c_undo.len = -1;
460 el->el_chared.c_undo.cursor = 0;
462 el->el_chared.c_vcmd.action = NOP;
463 el->el_chared.c_vcmd.pos = el->el_line.buffer;
465 el->el_chared.c_kill.mark = el->el_line.buffer;
467 el->el_map.current = el->el_map.key;
469 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
470 el->el_state.doingarg = 0;
471 el->el_state.metanext = 0;
472 el->el_state.argument = 1;
473 el->el_state.lastcmd = ED_UNASSIGNED;
475 el->el_history.eventno = 0;
479 * Enlarge line buffer to be able to hold twice as much characters.
480 * Returns 1 if successful, 0 if not.
483 ch_enlargebufs(EditLine *el, size_t addlen)
486 wchar_t *newbuffer, *oldbuf, *oldkbuf;
488 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
491 * If newly required length is longer than current buffer, we need
492 * to make the buffer big enough to hold both old and new stuff.
495 while(newsz - sz < addlen)
500 * Reallocate line buffer.
502 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
506 /* zero the newly added memory, leave old data in */
507 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
509 oldbuf = el->el_line.buffer;
511 el->el_line.buffer = newbuffer;
512 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
513 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
514 /* don't set new size until all buffers are enlarged */
515 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
518 * Reallocate kill buffer.
520 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
525 /* zero the newly added memory, leave old data in */
526 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
528 oldkbuf = el->el_chared.c_kill.buf;
530 el->el_chared.c_kill.buf = newbuffer;
531 el->el_chared.c_kill.last = newbuffer +
532 (el->el_chared.c_kill.last - oldkbuf);
533 el->el_chared.c_kill.mark = el->el_line.buffer +
534 (el->el_chared.c_kill.mark - oldbuf);
537 * Reallocate undo buffer.
539 newbuffer = el_realloc(el->el_chared.c_undo.buf,
540 newsz * sizeof(*newbuffer));
544 /* zero the newly added memory, leave old data in */
545 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
546 el->el_chared.c_undo.buf = newbuffer;
548 newbuffer = el_realloc(el->el_chared.c_redo.buf,
549 newsz * sizeof(*newbuffer));
552 el->el_chared.c_redo.pos = newbuffer +
553 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
554 el->el_chared.c_redo.lim = newbuffer +
555 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
556 el->el_chared.c_redo.buf = newbuffer;
558 if (!hist_enlargebuf(el, sz, newsz))
561 /* Safe to set enlarged buffer size */
562 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
563 if (el->el_chared.c_resizefun)
564 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
569 * Free the data structures used by the editor
574 el_free(el->el_line.buffer);
575 el->el_line.buffer = NULL;
576 el->el_line.limit = NULL;
577 el_free(el->el_chared.c_undo.buf);
578 el->el_chared.c_undo.buf = NULL;
579 el_free(el->el_chared.c_redo.buf);
580 el->el_chared.c_redo.buf = NULL;
581 el->el_chared.c_redo.pos = NULL;
582 el->el_chared.c_redo.lim = NULL;
583 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
584 el_free(el->el_chared.c_kill.buf);
585 el->el_chared.c_kill.buf = NULL;
591 * Insert string at cursor
594 el_winsertstr(EditLine *el, const wchar_t *s)
598 if (s == NULL || (len = wcslen(s)) == 0)
600 if (el->el_line.lastchar + len >= el->el_line.limit) {
601 if (!ch_enlargebufs(el, len))
605 c_insert(el, (int)len);
607 *el->el_line.cursor++ = *s++;
613 * Delete num characters before the cursor
616 el_deletestr(EditLine *el, int n)
621 if (el->el_line.cursor < &el->el_line.buffer[n])
624 c_delbefore(el, n); /* delete before dot */
625 el->el_line.cursor -= n;
626 if (el->el_line.cursor < el->el_line.buffer)
627 el->el_line.cursor = el->el_line.buffer;
631 * Delete characters between start and end
634 el_deletestr1(EditLine *el, int start, int end)
636 size_t line_length, len;
642 line_length = (size_t)(el->el_line.lastchar - el->el_line.buffer);
644 if (start >= (int)line_length || end >= (int)line_length)
647 len = (size_t)(end - start);
648 if (len > line_length - (size_t)end)
649 len = line_length - (size_t)end;
651 p1 = el->el_line.buffer + start;
652 p2 = el->el_line.buffer + end;
653 for (size_t i = 0; i < len; i++) {
655 el->el_line.lastchar--;
658 if (el->el_line.cursor < el->el_line.buffer)
659 el->el_line.cursor = el->el_line.buffer;
665 * Replace the contents of the line with the provided string
668 el_wreplacestr(EditLine *el, const wchar_t *s)
673 if (s == NULL || (len = wcslen(s)) == 0)
676 if (el->el_line.buffer + len >= el->el_line.limit) {
677 if (!ch_enlargebufs(el, len))
681 p = el->el_line.buffer;
682 for (size_t i = 0; i < len; i++)
685 el->el_line.buffer[len] = '\0';
686 el->el_line.lastchar = el->el_line.buffer + len;
687 if (el->el_line.cursor > el->el_line.lastchar)
688 el->el_line.cursor = el->el_line.lastchar;
694 * Move the cursor to the left or the right of the current position
697 el_cursor(EditLine *el, int n)
702 el->el_line.cursor += n;
704 if (el->el_line.cursor < el->el_line.buffer)
705 el->el_line.cursor = el->el_line.buffer;
706 if (el->el_line.cursor > el->el_line.lastchar)
707 el->el_line.cursor = el->el_line.lastchar;
709 return (int)(el->el_line.cursor - el->el_line.buffer);
716 c_gets(EditLine *el, wchar_t *buf, const wchar_t *prompt)
719 wchar_t *cp = el->el_line.buffer, ch;
722 len = (ssize_t)wcslen(prompt);
723 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
729 el->el_line.cursor = cp;
731 el->el_line.lastchar = cp + 1;
734 if (el_wgetc(el, &ch) != 1) {
735 ed_end_of_file(el, 0);
742 case L'\b': /* Delete and backspace */
753 case L'\r': /* Newline */
759 if (len >= (ssize_t)(EL_BUFSIZ - 16))
770 el->el_line.buffer[0] = '\0';
771 el->el_line.lastchar = el->el_line.buffer;
772 el->el_line.cursor = el->el_line.buffer;
778 * Return the current horizontal position of the cursor
786 * Find how many characters till the beginning of this line.
788 if (el->el_line.cursor == el->el_line.buffer)
791 for (ptr = el->el_line.cursor - 1;
792 ptr >= el->el_line.buffer && *ptr != '\n';
795 return (int)(el->el_line.cursor - ptr - 1);
800 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
802 el->el_chared.c_resizefun = f;
803 el->el_chared.c_resizearg = a;
808 ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
810 el->el_chared.c_aliasfun = f;
811 el->el_chared.c_aliasarg = a;