1 /* $NetBSD: chared.c,v 1.27 2009/02/15 21:55:23 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.27 2009/02/15 21:55:23 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * chared.c: Character editor utilities
50 private void ch__clearmacro (EditLine
*);
52 /* value to leave unused in line buffer */
56 * Handle state for the vi undo command
61 c_undo_t
*vu
= &el
->el_chared
.c_undo
;
62 c_redo_t
*r
= &el
->el_chared
.c_redo
;
65 /* Save entire line for undo */
66 size
= el
->el_line
.lastchar
- el
->el_line
.buffer
;
68 vu
->cursor
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
69 (void)memcpy(vu
->buf
, el
->el_line
.buffer
, size
* sizeof(*vu
->buf
));
71 /* save command info for redo */
72 r
->count
= el
->el_state
.doingarg
? el
->el_state
.argument
: 0;
73 r
->action
= el
->el_chared
.c_vcmd
.action
;
75 r
->cmd
= el
->el_state
.thiscmd
;
76 r
->ch
= el
->el_state
.thisch
;
80 * Save yank/delete data for paste
83 cv_yank(EditLine
*el
, const Char
*ptr
, int size
)
85 c_kill_t
*k
= &el
->el_chared
.c_kill
;
87 (void)memcpy(k
->buf
, ptr
, size
* sizeof(*k
->buf
));
88 k
->last
= k
->buf
+ size
;
93 * Insert num characters
96 c_insert(EditLine
*el
, int num
)
100 if (el
->el_line
.lastchar
+ num
>= el
->el_line
.limit
) {
101 if (!ch_enlargebufs(el
, (size_t)num
))
102 return; /* can't go past end of buffer */
105 if (el
->el_line
.cursor
< el
->el_line
.lastchar
) {
106 /* if I must move chars */
107 for (cp
= el
->el_line
.lastchar
; cp
>= el
->el_line
.cursor
; cp
--)
110 el
->el_line
.lastchar
+= num
;
115 * Delete num characters after the cursor
118 c_delafter(EditLine
*el
, int num
)
121 if (el
->el_line
.cursor
+ num
> el
->el_line
.lastchar
)
122 num
= (int)(el
->el_line
.lastchar
- el
->el_line
.cursor
);
124 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
126 cv_yank(el
, el
->el_line
.cursor
, num
);
132 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
135 el
->el_line
.lastchar
-= num
;
141 * Delete the character after the cursor, do not yank
144 c_delafter1(EditLine
*el
)
148 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
151 el
->el_line
.lastchar
--;
156 * Delete num characters before the cursor
159 c_delbefore(EditLine
*el
, int num
)
162 if (el
->el_line
.cursor
- num
< el
->el_line
.buffer
)
163 num
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
165 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
167 cv_yank(el
, el
->el_line
.cursor
- num
, num
);
173 for (cp
= el
->el_line
.cursor
- num
;
174 cp
<= el
->el_line
.lastchar
;
178 el
->el_line
.lastchar
-= num
;
184 * Delete the character before the cursor, do not yank
187 c_delbefore1(EditLine
*el
)
191 for (cp
= el
->el_line
.cursor
- 1; cp
<= el
->el_line
.lastchar
; cp
++)
194 el
->el_line
.lastchar
--;
199 * Return if p is part of a word according to emacs
204 return (Isalnum(p
) || Strchr(STR("*?_-.[]~="), p
) != NULL
);
209 * Return if p is part of a word according to vi
214 if (Isalnum(p
) || p
== '_')
223 * Return if p is part of a big word according to vi
228 return (!Isspace(p
));
233 * Find the previous word
236 c__prev_word(Char
*p
, Char
*low
, int n
, int (*wtest
)(Int
))
241 while ((p
>= low
) && !(*wtest
)(*p
))
243 while ((p
>= low
) && (*wtest
)(*p
))
247 /* cp now points to one character before the word */
251 /* cp now points where we want it */
260 c__next_word(Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
263 while ((p
< high
) && !(*wtest
)(*p
))
265 while ((p
< high
) && (*wtest
)(*p
))
270 /* p now points where we want it */
275 * Find the next word vi style
278 cv_next_word(EditLine
*el
, Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
284 while ((p
< high
) && (*wtest
)(*p
) == test
)
287 * vi historically deletes with cw only the word preserving the
288 * trailing whitespace! This is not what 'w' does..
290 if (n
|| el
->el_chared
.c_vcmd
.action
!= (DELETE
|INSERT
))
291 while ((p
< high
) && Isspace(*p
))
295 /* p now points where we want it */
304 * Find the previous word vi style
307 cv_prev_word(Char
*p
, Char
*low
, int n
, int (*wtest
)(Int
))
313 while ((p
> low
) && Isspace(*p
))
316 while ((p
>= low
) && (*wtest
)(*p
) == test
)
321 /* p now points where we want it */
331 * Ignore character p points to, return number appearing after that.
332 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
333 * Return p pointing to last char used.
337 Char
*p
, /* character position */
338 int *num
, /* Return value */
339 int dval
) /* dval is the number to subtract from like $-3 */
350 *num
= 0x7fffffff; /* Handle $ */
353 sign
= -1; /* Handle $- */
356 /* XXX: this assumes ASCII compatible digits */
357 for (i
= 0; Isdigit(*p
); i
= 10 * i
+ *p
++ - '0')
359 *num
= (sign
< 0 ? dval
- i
: i
);
365 * Finish vi delete action
368 cv_delfini(EditLine
*el
)
371 int action
= el
->el_chared
.c_vcmd
.action
;
374 el
->el_map
.current
= el
->el_map
.key
;
376 if (el
->el_chared
.c_vcmd
.pos
== 0)
380 size
= (int)(el
->el_line
.cursor
- el
->el_chared
.c_vcmd
.pos
);
383 el
->el_line
.cursor
= el
->el_chared
.c_vcmd
.pos
;
386 cv_yank(el
, el
->el_line
.cursor
, size
);
388 cv_yank(el
, el
->el_line
.cursor
+ size
, -size
);
391 c_delafter(el
, size
);
392 re_refresh_cursor(el
);
394 c_delbefore(el
, -size
);
395 el
->el_line
.cursor
+= size
;
398 el
->el_chared
.c_vcmd
.action
= NOP
;
404 * Go to the end of this word according to emacs
407 ce__endword(Char
*p
, Char
*high
, int n
)
412 while ((p
< high
) && Isspace(*p
))
414 while ((p
< high
) && !Isspace(*p
))
425 * Go to the end of this word according to vi
428 cv__endword(Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
435 while ((p
< high
) && Isspace(*p
))
439 while ((p
< high
) && (*wtest
)(*p
) == test
)
447 * Initialize the character editor
450 ch_init(EditLine
*el
)
452 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
454 el
->el_line
.buffer
= el_malloc(EL_BUFSIZ
*
455 sizeof(*el
->el_line
.buffer
));
456 if (el
->el_line
.buffer
== NULL
)
459 (void) memset(el
->el_line
.buffer
, 0, EL_BUFSIZ
*
460 sizeof(*el
->el_line
.buffer
));
461 el
->el_line
.cursor
= el
->el_line
.buffer
;
462 el
->el_line
.lastchar
= el
->el_line
.buffer
;
463 el
->el_line
.limit
= &el
->el_line
.buffer
[EL_BUFSIZ
- EL_LEAVE
];
465 el
->el_chared
.c_undo
.buf
= el_malloc(EL_BUFSIZ
*
466 sizeof(*el
->el_chared
.c_undo
.buf
));
467 if (el
->el_chared
.c_undo
.buf
== NULL
)
469 (void) memset(el
->el_chared
.c_undo
.buf
, 0, EL_BUFSIZ
*
470 sizeof(*el
->el_chared
.c_undo
.buf
));
471 el
->el_chared
.c_undo
.len
= -1;
472 el
->el_chared
.c_undo
.cursor
= 0;
473 el
->el_chared
.c_redo
.buf
= el_malloc(EL_BUFSIZ
*
474 sizeof(*el
->el_chared
.c_redo
.buf
));
475 if (el
->el_chared
.c_redo
.buf
== NULL
)
477 el
->el_chared
.c_redo
.pos
= el
->el_chared
.c_redo
.buf
;
478 el
->el_chared
.c_redo
.lim
= el
->el_chared
.c_redo
.buf
+ EL_BUFSIZ
;
479 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
481 el
->el_chared
.c_vcmd
.action
= NOP
;
482 el
->el_chared
.c_vcmd
.pos
= el
->el_line
.buffer
;
484 el
->el_chared
.c_kill
.buf
= el_malloc(EL_BUFSIZ
*
485 sizeof(*el
->el_chared
.c_kill
.buf
));
486 if (el
->el_chared
.c_kill
.buf
== NULL
)
488 (void) memset(el
->el_chared
.c_kill
.buf
, 0, EL_BUFSIZ
*
489 sizeof(*el
->el_chared
.c_kill
.buf
));
490 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
491 el
->el_chared
.c_kill
.last
= el
->el_chared
.c_kill
.buf
;
493 el
->el_map
.current
= el
->el_map
.key
;
495 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
496 el
->el_state
.doingarg
= 0;
497 el
->el_state
.metanext
= 0;
498 el
->el_state
.argument
= 1;
499 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
503 ma
->macro
= el_malloc(EL_MAXMACRO
* sizeof(*ma
->macro
));
504 if (ma
->macro
== NULL
)
510 * Reset the character editor
513 ch_reset(EditLine
*el
, int mclear
)
515 el
->el_line
.cursor
= el
->el_line
.buffer
;
516 el
->el_line
.lastchar
= el
->el_line
.buffer
;
518 el
->el_chared
.c_undo
.len
= -1;
519 el
->el_chared
.c_undo
.cursor
= 0;
521 el
->el_chared
.c_vcmd
.action
= NOP
;
522 el
->el_chared
.c_vcmd
.pos
= el
->el_line
.buffer
;
524 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
526 el
->el_map
.current
= el
->el_map
.key
;
528 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
529 el
->el_state
.doingarg
= 0;
530 el
->el_state
.metanext
= 0;
531 el
->el_state
.argument
= 1;
532 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
534 el
->el_history
.eventno
= 0;
541 ch__clearmacro(EditLine
*el
)
543 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
544 while (ma
->level
>= 0)
545 el_free((ptr_t
)ma
->macro
[ma
->level
--]);
549 * Enlarge line buffer to be able to hold twice as much characters.
550 * Returns 1 if successful, 0 if not.
553 ch_enlargebufs(EditLine
*el
, size_t addlen
)
556 Char
*newbuffer
, *oldbuf
, *oldkbuf
;
558 sz
= el
->el_line
.limit
- el
->el_line
.buffer
+ EL_LEAVE
;
561 * If newly required length is longer than current buffer, we need
562 * to make the buffer big enough to hold both old and new stuff.
565 while(newsz
- sz
< addlen
)
570 * Reallocate line buffer.
572 newbuffer
= el_realloc(el
->el_line
.buffer
, newsz
* sizeof(*newbuffer
));
576 /* zero the newly added memory, leave old data in */
577 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
579 oldbuf
= el
->el_line
.buffer
;
581 el
->el_line
.buffer
= newbuffer
;
582 el
->el_line
.cursor
= newbuffer
+ (el
->el_line
.cursor
- oldbuf
);
583 el
->el_line
.lastchar
= newbuffer
+ (el
->el_line
.lastchar
- oldbuf
);
584 /* don't set new size until all buffers are enlarged */
585 el
->el_line
.limit
= &newbuffer
[sz
- EL_LEAVE
];
588 * Reallocate kill buffer.
590 newbuffer
= el_realloc(el
->el_chared
.c_kill
.buf
, newsz
* sizeof(*newbuffer
));
594 /* zero the newly added memory, leave old data in */
595 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
597 oldkbuf
= el
->el_chared
.c_kill
.buf
;
599 el
->el_chared
.c_kill
.buf
= newbuffer
;
600 el
->el_chared
.c_kill
.last
= newbuffer
+
601 (el
->el_chared
.c_kill
.last
- oldkbuf
);
602 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
+
603 (el
->el_chared
.c_kill
.mark
- oldbuf
);
606 * Reallocate undo buffer.
608 newbuffer
= el_realloc(el
->el_chared
.c_undo
.buf
,
609 newsz
* sizeof(*newbuffer
));
613 /* zero the newly added memory, leave old data in */
614 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
615 el
->el_chared
.c_undo
.buf
= newbuffer
;
617 newbuffer
= el_realloc(el
->el_chared
.c_redo
.buf
,
618 newsz
* sizeof(*newbuffer
));
621 el
->el_chared
.c_redo
.pos
= newbuffer
+
622 (el
->el_chared
.c_redo
.pos
- el
->el_chared
.c_redo
.buf
);
623 el
->el_chared
.c_redo
.lim
= newbuffer
+
624 (el
->el_chared
.c_redo
.lim
- el
->el_chared
.c_redo
.buf
);
625 el
->el_chared
.c_redo
.buf
= newbuffer
;
627 if (!hist_enlargebuf(el
, sz
, newsz
))
630 /* Safe to set enlarged buffer size */
631 el
->el_line
.limit
= &el
->el_line
.buffer
[newsz
- EL_LEAVE
];
636 * Free the data structures used by the editor
641 el_free((ptr_t
) el
->el_line
.buffer
);
642 el
->el_line
.buffer
= NULL
;
643 el
->el_line
.limit
= NULL
;
644 el_free((ptr_t
) el
->el_chared
.c_undo
.buf
);
645 el
->el_chared
.c_undo
.buf
= NULL
;
646 el_free((ptr_t
) el
->el_chared
.c_redo
.buf
);
647 el
->el_chared
.c_redo
.buf
= NULL
;
648 el
->el_chared
.c_redo
.pos
= NULL
;
649 el
->el_chared
.c_redo
.lim
= NULL
;
650 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
651 el_free((ptr_t
) el
->el_chared
.c_kill
.buf
);
652 el
->el_chared
.c_kill
.buf
= NULL
;
654 el_free((ptr_t
) el
->el_chared
.c_macro
.macro
);
655 el
->el_chared
.c_macro
.macro
= NULL
;
660 * Insert string at cursorI
663 FUN(el
,insertstr
)(EditLine
*el
, const Char
*s
)
667 if ((len
= Strlen(s
)) == 0)
669 if (el
->el_line
.lastchar
+ len
>= el
->el_line
.limit
) {
670 if (!ch_enlargebufs(el
, len
))
674 c_insert(el
, (int)len
);
676 *el
->el_line
.cursor
++ = *s
++;
682 * Delete num characters before the cursor
685 el_deletestr(EditLine
*el
, int n
)
690 if (el
->el_line
.cursor
< &el
->el_line
.buffer
[n
])
693 c_delbefore(el
, n
); /* delete before dot */
694 el
->el_line
.cursor
-= n
;
695 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
696 el
->el_line
.cursor
= el
->el_line
.buffer
;
703 c_gets(EditLine
*el
, Char
*buf
, const Char
*prompt
)
707 Char
*cp
= el
->el_line
.buffer
;
710 len
= Strlen(prompt
);
711 (void)memcpy(cp
, prompt
, len
* sizeof(*cp
));
717 el
->el_line
.cursor
= cp
;
719 el
->el_line
.lastchar
= cp
+ 1;
722 if (FUN(el
,getc
)(el
, &ch
) != 1) {
723 ed_end_of_file(el
, 0);
730 case 0010: /* Delete and backspace */
740 case '\r': /* Newline */
746 if (len
>= EL_BUFSIZ
- 16)
757 el
->el_line
.buffer
[0] = '\0';
758 el
->el_line
.lastchar
= el
->el_line
.buffer
;
759 el
->el_line
.cursor
= el
->el_line
.buffer
;
765 * Return the current horizontal position of the cursor
773 * Find how many characters till the beginning of this line.
775 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
778 for (ptr
= el
->el_line
.cursor
- 1;
779 ptr
>= el
->el_line
.buffer
&& *ptr
!= '\n';
782 return (int)(el
->el_line
.cursor
- ptr
- 1);