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.25 2005/08/08 01:41:30 christos Exp $
34 * $DragonFly: src/lib/libedit/chared.c,v 1.9 2008/09/30 16:57:05 swildner Exp $
40 * chared.c: Character editor utilities
45 private void ch__clearmacro(EditLine
*);
47 /* value to leave unused in line buffer */
51 * Handle state for the vi undo command
56 c_undo_t
*vu
= &el
->el_chared
.c_undo
;
57 c_redo_t
*r
= &el
->el_chared
.c_redo
;
60 /* Save entire line for undo */
61 size
= el
->el_line
.lastchar
- el
->el_line
.buffer
;
63 vu
->cursor
= el
->el_line
.cursor
- el
->el_line
.buffer
;
64 memcpy(vu
->buf
, el
->el_line
.buffer
, size
);
66 /* save command info for redo */
67 r
->count
= el
->el_state
.doingarg
? el
->el_state
.argument
: 0;
68 r
->action
= el
->el_chared
.c_vcmd
.action
;
70 r
->cmd
= el
->el_state
.thiscmd
;
71 r
->ch
= el
->el_state
.thisch
;
75 * Save yank/delete data for paste
78 cv_yank(EditLine
*el
, const char *ptr
, int size
)
80 c_kill_t
*k
= &el
->el_chared
.c_kill
;
82 memcpy(k
->buf
, ptr
, size
+0u);
83 k
->last
= k
->buf
+ size
;
88 * Insert num characters
91 c_insert(EditLine
*el
, int num
)
95 if (el
->el_line
.lastchar
+ num
>= el
->el_line
.limit
) {
96 if (!ch_enlargebufs(el
, num
+0u))
97 return; /* can't go past end of buffer */
100 if (el
->el_line
.cursor
< el
->el_line
.lastchar
) {
101 /* if I must move chars */
102 for (cp
= el
->el_line
.lastchar
; cp
>= el
->el_line
.cursor
; cp
--)
105 el
->el_line
.lastchar
+= num
;
110 * Delete num characters after the cursor
113 c_delafter(EditLine
*el
, int num
)
116 if (el
->el_line
.cursor
+ num
> el
->el_line
.lastchar
)
117 num
= el
->el_line
.lastchar
- el
->el_line
.cursor
;
119 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
121 cv_yank(el
, el
->el_line
.cursor
, num
);
127 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
130 el
->el_line
.lastchar
-= num
;
136 * Delete the character after the cursor, do not yank
139 c_delafter1(EditLine
*el
)
143 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
146 el
->el_line
.lastchar
--;
151 * Delete num characters before the cursor
154 c_delbefore(EditLine
*el
, int num
)
157 if (el
->el_line
.cursor
- num
< el
->el_line
.buffer
)
158 num
= el
->el_line
.cursor
- el
->el_line
.buffer
;
160 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
162 cv_yank(el
, el
->el_line
.cursor
- num
, num
);
168 for (cp
= el
->el_line
.cursor
- num
;
169 cp
<= el
->el_line
.lastchar
;
173 el
->el_line
.lastchar
-= num
;
179 * Delete the character before the cursor, do not yank
182 c_delbefore1(EditLine
*el
)
186 for (cp
= el
->el_line
.cursor
- 1; cp
<= el
->el_line
.lastchar
; cp
++)
189 el
->el_line
.lastchar
--;
194 * Return if p is part of a word according to emacs
199 return (isalnum(p
) || strchr("*?_-.[]~=", p
) != NULL
);
204 * Return if p is part of a word according to vi
209 if (isalnum(p
) || p
== '_')
218 * Return if p is part of a big word according to vi
223 return (!isspace(p
));
228 * Find the previous word
231 c__prev_word(char *p
, char *low
, int n
, int (*wtest
)(int))
236 while ((p
>= low
) && !(*wtest
)((unsigned char) *p
))
238 while ((p
>= low
) && (*wtest
)((unsigned char) *p
))
242 /* cp now points to one character before the word */
246 /* cp now points where we want it */
255 c__next_word(char *p
, char *high
, int n
, int (*wtest
)(int))
258 while ((p
< high
) && !(*wtest
)((unsigned char) *p
))
260 while ((p
< high
) && (*wtest
)((unsigned char) *p
))
265 /* p now points where we want it */
270 * Find the next word vi style
273 cv_next_word(EditLine
*el
, char *p
, char *high
, int n
, int (*wtest
)(int))
278 test
= (*wtest
)((unsigned char) *p
);
279 while ((p
< high
) && (*wtest
)((unsigned char) *p
) == test
)
282 * vi historically deletes with cw only the word preserving the
283 * trailing whitespace! This is not what 'w' does..
285 if (n
|| el
->el_chared
.c_vcmd
.action
!= (DELETE
|INSERT
))
286 while ((p
< high
) && isspace((unsigned char) *p
))
290 /* p now points where we want it */
299 * Find the previous word vi style
302 cv_prev_word(char *p
, char *low
, int n
, int (*wtest
)(int))
308 while ((p
> low
) && isspace((unsigned char) *p
))
310 test
= (*wtest
)((unsigned char) *p
);
311 while ((p
>= low
) && (*wtest
)((unsigned char) *p
) == test
)
316 /* p now points where we want it */
326 * Ignore character p points to, return number appearing after that.
327 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
328 * Return p pointing to last char used.
332 char *p
, /* character position */
333 int *num
, /* Return value */
334 int dval
) /* dval is the number to subtract from like $-3 */
345 *num
= 0x7fffffff; /* Handle $ */
348 sign
= -1; /* Handle $- */
351 for (i
= 0; isdigit((unsigned char) *p
); i
= 10 * i
+ *p
++ - '0')
353 *num
= (sign
< 0 ? dval
- i
: i
);
359 * Finish vi delete action
362 cv_delfini(EditLine
*el
)
365 int action
= el
->el_chared
.c_vcmd
.action
;
368 el
->el_map
.current
= el
->el_map
.key
;
370 if (el
->el_chared
.c_vcmd
.pos
== 0)
374 size
= el
->el_line
.cursor
- el
->el_chared
.c_vcmd
.pos
;
377 el
->el_line
.cursor
= el
->el_chared
.c_vcmd
.pos
;
380 cv_yank(el
, el
->el_line
.cursor
, size
);
382 cv_yank(el
, el
->el_line
.cursor
+ size
, -size
);
385 c_delafter(el
, size
);
386 re_refresh_cursor(el
);
388 c_delbefore(el
, -size
);
389 el
->el_line
.cursor
+= size
;
392 el
->el_chared
.c_vcmd
.action
= NOP
;
398 * Go to the end of this word according to emacs
401 ce__endword(char *p
, char *high
, int n
)
406 while ((p
< high
) && isspace((unsigned char) *p
))
408 while ((p
< high
) && !isspace((unsigned char) *p
))
419 * Go to the end of this word according to vi
422 cv__endword(char *p
, char *high
, int n
, int (*wtest
)(int))
429 while ((p
< high
) && isspace((unsigned char) *p
))
432 test
= (*wtest
)((unsigned char) *p
);
433 while ((p
< high
) && (*wtest
)((unsigned char) *p
) == test
)
441 * Initialize the character editor
444 ch_init(EditLine
*el
)
446 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
448 el
->el_line
.buffer
= (char *) el_malloc(EL_BUFSIZ
);
449 if (el
->el_line
.buffer
== NULL
)
452 (void) memset(el
->el_line
.buffer
, 0, EL_BUFSIZ
);
453 el
->el_line
.cursor
= el
->el_line
.buffer
;
454 el
->el_line
.lastchar
= el
->el_line
.buffer
;
455 el
->el_line
.limit
= &el
->el_line
.buffer
[EL_BUFSIZ
- EL_LEAVE
];
457 el
->el_chared
.c_undo
.buf
= (char *) el_malloc(EL_BUFSIZ
);
458 if (el
->el_chared
.c_undo
.buf
== NULL
)
460 (void) memset(el
->el_chared
.c_undo
.buf
, 0, EL_BUFSIZ
);
461 el
->el_chared
.c_undo
.len
= -1;
462 el
->el_chared
.c_undo
.cursor
= 0;
463 el
->el_chared
.c_redo
.buf
= (char *) el_malloc(EL_BUFSIZ
);
464 if (el
->el_chared
.c_redo
.buf
== NULL
)
466 el
->el_chared
.c_redo
.pos
= el
->el_chared
.c_redo
.buf
;
467 el
->el_chared
.c_redo
.lim
= el
->el_chared
.c_redo
.buf
+ EL_BUFSIZ
;
468 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
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
.buf
= (char *) el_malloc(EL_BUFSIZ
);
474 if (el
->el_chared
.c_kill
.buf
== NULL
)
476 (void) memset(el
->el_chared
.c_kill
.buf
, 0, EL_BUFSIZ
);
477 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
478 el
->el_chared
.c_kill
.last
= el
->el_chared
.c_kill
.buf
;
480 el
->el_map
.current
= el
->el_map
.key
;
482 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
483 el
->el_state
.doingarg
= 0;
484 el
->el_state
.metanext
= 0;
485 el
->el_state
.argument
= 1;
486 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
490 ma
->macro
= (char **) el_malloc(EL_MAXMACRO
* sizeof(char *));
491 if (ma
->macro
== NULL
)
497 * Reset the character editor
500 ch_reset(EditLine
*el
, int mclear
)
502 el
->el_line
.cursor
= el
->el_line
.buffer
;
503 el
->el_line
.lastchar
= el
->el_line
.buffer
;
505 el
->el_chared
.c_undo
.len
= -1;
506 el
->el_chared
.c_undo
.cursor
= 0;
508 el
->el_chared
.c_vcmd
.action
= NOP
;
509 el
->el_chared
.c_vcmd
.pos
= el
->el_line
.buffer
;
511 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
513 el
->el_map
.current
= el
->el_map
.key
;
515 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
516 el
->el_state
.doingarg
= 0;
517 el
->el_state
.metanext
= 0;
518 el
->el_state
.argument
= 1;
519 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
521 el
->el_history
.eventno
= 0;
528 ch__clearmacro(EditLine
*el
)
530 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
531 while (ma
->level
>= 0)
532 el_free((ptr_t
)ma
->macro
[ma
->level
--]);
536 * Enlarge line buffer to be able to hold twice as much characters.
537 * Returns 1 if successful, 0 if not.
540 ch_enlargebufs(EditLine
*el
, size_t addlen
)
543 char *newbuffer
, *oldbuf
, *oldkbuf
;
545 sz
= el
->el_line
.limit
- el
->el_line
.buffer
+ EL_LEAVE
;
548 * If newly required length is longer than current buffer, we need
549 * to make the buffer big enough to hold both old and new stuff.
552 while(newsz
- sz
< addlen
)
557 * Reallocate line buffer.
559 newbuffer
= el_realloc(el
->el_line
.buffer
, newsz
);
563 /* zero the newly added memory, leave old data in */
564 (void) memset(&newbuffer
[sz
], 0, newsz
- sz
);
566 oldbuf
= el
->el_line
.buffer
;
568 el
->el_line
.buffer
= newbuffer
;
569 el
->el_line
.cursor
= newbuffer
+ (el
->el_line
.cursor
- oldbuf
);
570 el
->el_line
.lastchar
= newbuffer
+ (el
->el_line
.lastchar
- oldbuf
);
571 /* don't set new size until all buffers are enlarged */
572 el
->el_line
.limit
= &newbuffer
[sz
- EL_LEAVE
];
575 * Reallocate kill buffer.
577 newbuffer
= el_realloc(el
->el_chared
.c_kill
.buf
, newsz
);
581 /* zero the newly added memory, leave old data in */
582 (void) memset(&newbuffer
[sz
], 0, newsz
- sz
);
584 oldkbuf
= el
->el_chared
.c_kill
.buf
;
586 el
->el_chared
.c_kill
.buf
= newbuffer
;
587 el
->el_chared
.c_kill
.last
= newbuffer
+
588 (el
->el_chared
.c_kill
.last
- oldkbuf
);
589 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
+
590 (el
->el_chared
.c_kill
.mark
- oldbuf
);
593 * Reallocate undo buffer.
595 newbuffer
= el_realloc(el
->el_chared
.c_undo
.buf
, newsz
);
599 /* zero the newly added memory, leave old data in */
600 (void) memset(&newbuffer
[sz
], 0, newsz
- sz
);
601 el
->el_chared
.c_undo
.buf
= newbuffer
;
603 newbuffer
= el_realloc(el
->el_chared
.c_redo
.buf
, newsz
);
606 el
->el_chared
.c_redo
.pos
= newbuffer
+
607 (el
->el_chared
.c_redo
.pos
- el
->el_chared
.c_redo
.buf
);
608 el
->el_chared
.c_redo
.lim
= newbuffer
+
609 (el
->el_chared
.c_redo
.lim
- el
->el_chared
.c_redo
.buf
);
610 el
->el_chared
.c_redo
.buf
= newbuffer
;
612 if (!hist_enlargebuf(el
, sz
, newsz
))
615 /* Safe to set enlarged buffer size */
616 el
->el_line
.limit
= &el
->el_line
.buffer
[newsz
- EL_LEAVE
];
621 * Free the data structures used by the editor
626 el_free((ptr_t
) el
->el_line
.buffer
);
627 el
->el_line
.buffer
= NULL
;
628 el
->el_line
.limit
= NULL
;
629 el_free((ptr_t
) el
->el_chared
.c_undo
.buf
);
630 el
->el_chared
.c_undo
.buf
= NULL
;
631 el_free((ptr_t
) el
->el_chared
.c_redo
.buf
);
632 el
->el_chared
.c_redo
.buf
= NULL
;
633 el
->el_chared
.c_redo
.pos
= NULL
;
634 el
->el_chared
.c_redo
.lim
= NULL
;
635 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
636 el_free((ptr_t
) el
->el_chared
.c_kill
.buf
);
637 el
->el_chared
.c_kill
.buf
= NULL
;
639 el_free((ptr_t
) el
->el_chared
.c_macro
.macro
);
640 el
->el_chared
.c_macro
.macro
= NULL
;
645 * Insert string at cursorI
648 el_insertstr(EditLine
*el
, const char *s
)
652 if ((len
= strlen(s
)) == 0)
654 if (el
->el_line
.lastchar
+ len
>= el
->el_line
.limit
) {
655 if (!ch_enlargebufs(el
, len
))
659 c_insert(el
, (int)len
);
661 *el
->el_line
.cursor
++ = *s
++;
667 * Delete num characters before the cursor
670 el_deletestr(EditLine
*el
, int n
)
675 if (el
->el_line
.cursor
< &el
->el_line
.buffer
[n
])
678 c_delbefore(el
, n
); /* delete before dot */
679 el
->el_line
.cursor
-= n
;
680 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
681 el
->el_line
.cursor
= el
->el_line
.buffer
;
688 c_gets(EditLine
*el
, char *buf
, const char *prompt
)
692 char *cp
= el
->el_line
.buffer
;
695 len
= strlen(prompt
);
696 memcpy(cp
, prompt
, len
+ 0u);
702 el
->el_line
.cursor
= cp
;
704 el
->el_line
.lastchar
= cp
+ 1;
707 if (el_getc(el
, &ch
) != 1) {
708 ed_end_of_file(el
, 0);
715 case 0010: /* Delete and backspace */
725 case '\r': /* Newline */
731 if (len
>= EL_BUFSIZ
- 16)
742 el
->el_line
.buffer
[0] = '\0';
743 el
->el_line
.lastchar
= el
->el_line
.buffer
;
744 el
->el_line
.cursor
= el
->el_line
.buffer
;
750 * Return the current horizontal position of the cursor
758 * Find how many characters till the beginning of this line.
760 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
763 for (ptr
= el
->el_line
.cursor
- 1;
764 ptr
>= el
->el_line
.buffer
&& *ptr
!= '\n';
767 return (el
->el_line
.cursor
- ptr
- 1);