1 /* $NetBSD: chared.c,v 1.36 2011/10/23 17:37:55 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";
41 #endif /* not lint && not SCCSID */
44 * chared.c: Character editor utilities
49 private void ch__clearmacro (EditLine
*);
51 /* value to leave unused in line buffer */
55 * Handle state for the vi undo command
60 c_undo_t
*vu
= &el
->el_chared
.c_undo
;
61 c_redo_t
*r
= &el
->el_chared
.c_redo
;
64 /* Save entire line for undo */
65 size
= (size_t)(el
->el_line
.lastchar
- el
->el_line
.buffer
);
66 vu
->len
= (ssize_t
)size
;
67 vu
->cursor
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
68 (void)memcpy(vu
->buf
, el
->el_line
.buffer
, size
* sizeof(*vu
->buf
));
70 /* save command info for redo */
71 r
->count
= el
->el_state
.doingarg
? el
->el_state
.argument
: 0;
72 r
->action
= el
->el_chared
.c_vcmd
.action
;
74 r
->cmd
= el
->el_state
.thiscmd
;
75 r
->ch
= el
->el_state
.thisch
;
79 * Save yank/delete data for paste
82 cv_yank(EditLine
*el
, const Char
*ptr
, int size
)
84 c_kill_t
*k
= &el
->el_chared
.c_kill
;
86 (void)memcpy(k
->buf
, ptr
, (size_t)size
* sizeof(*k
->buf
));
87 k
->last
= k
->buf
+ size
;
92 * Insert num characters
95 c_insert(EditLine
*el
, int num
)
99 if (el
->el_line
.lastchar
+ num
>= el
->el_line
.limit
) {
100 if (!ch_enlargebufs(el
, (size_t)num
))
101 return; /* can't go past end of buffer */
104 if (el
->el_line
.cursor
< el
->el_line
.lastchar
) {
105 /* if I must move chars */
106 for (cp
= el
->el_line
.lastchar
; cp
>= el
->el_line
.cursor
; cp
--)
109 el
->el_line
.lastchar
+= num
;
114 * Delete num characters after the cursor
117 c_delafter(EditLine
*el
, int num
)
120 if (el
->el_line
.cursor
+ num
> el
->el_line
.lastchar
)
121 num
= (int)(el
->el_line
.lastchar
- el
->el_line
.cursor
);
123 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
125 cv_yank(el
, el
->el_line
.cursor
, num
);
131 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
134 el
->el_line
.lastchar
-= num
;
140 * Delete the character after the cursor, do not yank
143 c_delafter1(EditLine
*el
)
147 for (cp
= el
->el_line
.cursor
; cp
<= el
->el_line
.lastchar
; cp
++)
150 el
->el_line
.lastchar
--;
155 * Delete num characters before the cursor
158 c_delbefore(EditLine
*el
, int num
)
161 if (el
->el_line
.cursor
- num
< el
->el_line
.buffer
)
162 num
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
164 if (el
->el_map
.current
!= el
->el_map
.emacs
) {
166 cv_yank(el
, el
->el_line
.cursor
- num
, num
);
172 for (cp
= el
->el_line
.cursor
- num
;
173 cp
<= el
->el_line
.lastchar
;
177 el
->el_line
.lastchar
-= num
;
183 * Delete the character before the cursor, do not yank
186 c_delbefore1(EditLine
*el
)
190 for (cp
= el
->el_line
.cursor
- 1; cp
<= el
->el_line
.lastchar
; cp
++)
193 el
->el_line
.lastchar
--;
198 * Return if p is part of a word according to emacs
203 return Isalnum(p
) || Strchr(STR("*?_-.[]~="), p
) != NULL
;
208 * Return if p is part of a word according to vi
213 if (Isalnum(p
) || p
== '_')
222 * Return if p is part of a big word according to vi
232 * Find the previous word
235 c__prev_word(Char
*p
, Char
*low
, int n
, int (*wtest
)(Int
))
240 while ((p
>= low
) && !(*wtest
)(*p
))
242 while ((p
>= low
) && (*wtest
)(*p
))
246 /* cp now points to one character before the word */
250 /* cp now points where we want it */
259 c__next_word(Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
262 while ((p
< high
) && !(*wtest
)(*p
))
264 while ((p
< high
) && (*wtest
)(*p
))
269 /* p now points where we want it */
274 * Find the next word vi style
277 cv_next_word(EditLine
*el
, Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
283 while ((p
< high
) && (*wtest
)(*p
) == test
)
286 * vi historically deletes with cw only the word preserving the
287 * trailing whitespace! This is not what 'w' does..
289 if (n
|| el
->el_chared
.c_vcmd
.action
!= (DELETE
|INSERT
))
290 while ((p
< high
) && Isspace(*p
))
294 /* p now points where we want it */
303 * Find the previous word vi style
306 cv_prev_word(Char
*p
, Char
*low
, int n
, int (*wtest
)(Int
))
312 while ((p
> low
) && Isspace(*p
))
315 while ((p
>= low
) && (*wtest
)(*p
) == test
)
320 /* p now points where we want it */
329 * Finish vi delete action
332 cv_delfini(EditLine
*el
)
335 int action
= el
->el_chared
.c_vcmd
.action
;
338 el
->el_map
.current
= el
->el_map
.key
;
340 if (el
->el_chared
.c_vcmd
.pos
== 0)
344 size
= (int)(el
->el_line
.cursor
- el
->el_chared
.c_vcmd
.pos
);
347 el
->el_line
.cursor
= el
->el_chared
.c_vcmd
.pos
;
350 cv_yank(el
, el
->el_line
.cursor
, size
);
352 cv_yank(el
, el
->el_line
.cursor
+ size
, -size
);
355 c_delafter(el
, size
);
356 re_refresh_cursor(el
);
358 c_delbefore(el
, -size
);
359 el
->el_line
.cursor
+= size
;
362 el
->el_chared
.c_vcmd
.action
= NOP
;
367 * Go to the end of this word according to vi
370 cv__endword(Char
*p
, Char
*high
, int n
, int (*wtest
)(Int
))
377 while ((p
< high
) && Isspace(*p
))
381 while ((p
< high
) && (*wtest
)(*p
) == test
)
389 * Initialize the character editor
392 ch_init(EditLine
*el
)
394 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
396 el
->el_line
.buffer
= el_malloc(EL_BUFSIZ
*
397 sizeof(*el
->el_line
.buffer
));
398 if (el
->el_line
.buffer
== NULL
)
401 (void) memset(el
->el_line
.buffer
, 0, EL_BUFSIZ
*
402 sizeof(*el
->el_line
.buffer
));
403 el
->el_line
.cursor
= el
->el_line
.buffer
;
404 el
->el_line
.lastchar
= el
->el_line
.buffer
;
405 el
->el_line
.limit
= &el
->el_line
.buffer
[EL_BUFSIZ
- EL_LEAVE
];
407 el
->el_chared
.c_undo
.buf
= el_malloc(EL_BUFSIZ
*
408 sizeof(*el
->el_chared
.c_undo
.buf
));
409 if (el
->el_chared
.c_undo
.buf
== NULL
)
411 (void) memset(el
->el_chared
.c_undo
.buf
, 0, EL_BUFSIZ
*
412 sizeof(*el
->el_chared
.c_undo
.buf
));
413 el
->el_chared
.c_undo
.len
= -1;
414 el
->el_chared
.c_undo
.cursor
= 0;
415 el
->el_chared
.c_redo
.buf
= el_malloc(EL_BUFSIZ
*
416 sizeof(*el
->el_chared
.c_redo
.buf
));
417 if (el
->el_chared
.c_redo
.buf
== NULL
)
419 el
->el_chared
.c_redo
.pos
= el
->el_chared
.c_redo
.buf
;
420 el
->el_chared
.c_redo
.lim
= el
->el_chared
.c_redo
.buf
+ EL_BUFSIZ
;
421 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
423 el
->el_chared
.c_vcmd
.action
= NOP
;
424 el
->el_chared
.c_vcmd
.pos
= el
->el_line
.buffer
;
426 el
->el_chared
.c_kill
.buf
= el_malloc(EL_BUFSIZ
*
427 sizeof(*el
->el_chared
.c_kill
.buf
));
428 if (el
->el_chared
.c_kill
.buf
== NULL
)
430 (void) memset(el
->el_chared
.c_kill
.buf
, 0, EL_BUFSIZ
*
431 sizeof(*el
->el_chared
.c_kill
.buf
));
432 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
433 el
->el_chared
.c_kill
.last
= el
->el_chared
.c_kill
.buf
;
434 el
->el_chared
.c_resizefun
= NULL
;
435 el
->el_chared
.c_resizearg
= NULL
;
437 el
->el_map
.current
= el
->el_map
.key
;
439 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
440 el
->el_state
.doingarg
= 0;
441 el
->el_state
.metanext
= 0;
442 el
->el_state
.argument
= 1;
443 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
447 ma
->macro
= el_malloc(EL_MAXMACRO
* sizeof(*ma
->macro
));
448 if (ma
->macro
== NULL
)
454 * Reset the character editor
457 ch_reset(EditLine
*el
, int mclear
)
459 el
->el_line
.cursor
= el
->el_line
.buffer
;
460 el
->el_line
.lastchar
= el
->el_line
.buffer
;
462 el
->el_chared
.c_undo
.len
= -1;
463 el
->el_chared
.c_undo
.cursor
= 0;
465 el
->el_chared
.c_vcmd
.action
= NOP
;
466 el
->el_chared
.c_vcmd
.pos
= el
->el_line
.buffer
;
468 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
;
470 el
->el_map
.current
= el
->el_map
.key
;
472 el
->el_state
.inputmode
= MODE_INSERT
; /* XXX: save a default */
473 el
->el_state
.doingarg
= 0;
474 el
->el_state
.metanext
= 0;
475 el
->el_state
.argument
= 1;
476 el
->el_state
.lastcmd
= ED_UNASSIGNED
;
478 el
->el_history
.eventno
= 0;
485 ch__clearmacro(EditLine
*el
)
487 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
488 while (ma
->level
>= 0)
489 el_free(ma
->macro
[ma
->level
--]);
493 * Enlarge line buffer to be able to hold twice as much characters.
494 * Returns 1 if successful, 0 if not.
497 ch_enlargebufs(EditLine
*el
, size_t addlen
)
500 Char
*newbuffer
, *oldbuf
, *oldkbuf
;
502 sz
= (size_t)(el
->el_line
.limit
- el
->el_line
.buffer
+ EL_LEAVE
);
505 * If newly required length is longer than current buffer, we need
506 * to make the buffer big enough to hold both old and new stuff.
509 while(newsz
- sz
< addlen
)
514 * Reallocate line buffer.
516 newbuffer
= el_realloc(el
->el_line
.buffer
, newsz
* sizeof(*newbuffer
));
520 /* zero the newly added memory, leave old data in */
521 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
523 oldbuf
= el
->el_line
.buffer
;
525 el
->el_line
.buffer
= newbuffer
;
526 el
->el_line
.cursor
= newbuffer
+ (el
->el_line
.cursor
- oldbuf
);
527 el
->el_line
.lastchar
= newbuffer
+ (el
->el_line
.lastchar
- oldbuf
);
528 /* don't set new size until all buffers are enlarged */
529 el
->el_line
.limit
= &newbuffer
[sz
- EL_LEAVE
];
532 * Reallocate kill buffer.
534 newbuffer
= el_realloc(el
->el_chared
.c_kill
.buf
, newsz
*
539 /* zero the newly added memory, leave old data in */
540 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
542 oldkbuf
= el
->el_chared
.c_kill
.buf
;
544 el
->el_chared
.c_kill
.buf
= newbuffer
;
545 el
->el_chared
.c_kill
.last
= newbuffer
+
546 (el
->el_chared
.c_kill
.last
- oldkbuf
);
547 el
->el_chared
.c_kill
.mark
= el
->el_line
.buffer
+
548 (el
->el_chared
.c_kill
.mark
- oldbuf
);
551 * Reallocate undo buffer.
553 newbuffer
= el_realloc(el
->el_chared
.c_undo
.buf
,
554 newsz
* sizeof(*newbuffer
));
558 /* zero the newly added memory, leave old data in */
559 (void) memset(&newbuffer
[sz
], 0, (newsz
- sz
) * sizeof(*newbuffer
));
560 el
->el_chared
.c_undo
.buf
= newbuffer
;
562 newbuffer
= el_realloc(el
->el_chared
.c_redo
.buf
,
563 newsz
* sizeof(*newbuffer
));
566 el
->el_chared
.c_redo
.pos
= newbuffer
+
567 (el
->el_chared
.c_redo
.pos
- el
->el_chared
.c_redo
.buf
);
568 el
->el_chared
.c_redo
.lim
= newbuffer
+
569 (el
->el_chared
.c_redo
.lim
- el
->el_chared
.c_redo
.buf
);
570 el
->el_chared
.c_redo
.buf
= newbuffer
;
572 if (!hist_enlargebuf(el
, sz
, newsz
))
575 /* Safe to set enlarged buffer size */
576 el
->el_line
.limit
= &el
->el_line
.buffer
[newsz
- EL_LEAVE
];
577 if (el
->el_chared
.c_resizefun
)
578 (*el
->el_chared
.c_resizefun
)(el
, el
->el_chared
.c_resizearg
);
583 * Free the data structures used by the editor
588 el_free(el
->el_line
.buffer
);
589 el
->el_line
.buffer
= NULL
;
590 el
->el_line
.limit
= NULL
;
591 el_free(el
->el_chared
.c_undo
.buf
);
592 el
->el_chared
.c_undo
.buf
= NULL
;
593 el_free(el
->el_chared
.c_redo
.buf
);
594 el
->el_chared
.c_redo
.buf
= NULL
;
595 el
->el_chared
.c_redo
.pos
= NULL
;
596 el
->el_chared
.c_redo
.lim
= NULL
;
597 el
->el_chared
.c_redo
.cmd
= ED_UNASSIGNED
;
598 el_free(el
->el_chared
.c_kill
.buf
);
599 el
->el_chared
.c_kill
.buf
= NULL
;
601 el_free(el
->el_chared
.c_macro
.macro
);
602 el
->el_chared
.c_macro
.macro
= NULL
;
607 * Insert string at cursorI
610 FUN(el
,insertstr
)(EditLine
*el
, const Char
*s
)
614 if ((len
= Strlen(s
)) == 0)
616 if (el
->el_line
.lastchar
+ len
>= el
->el_line
.limit
) {
617 if (!ch_enlargebufs(el
, len
))
621 c_insert(el
, (int)len
);
623 *el
->el_line
.cursor
++ = *s
++;
629 * Delete num characters before the cursor
632 el_deletestr(EditLine
*el
, int n
)
637 if (el
->el_line
.cursor
< &el
->el_line
.buffer
[n
])
640 c_delbefore(el
, n
); /* delete before dot */
641 el
->el_line
.cursor
-= n
;
642 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
643 el
->el_line
.cursor
= el
->el_line
.buffer
;
650 c_gets(EditLine
*el
, Char
*buf
, const Char
*prompt
)
654 Char
*cp
= el
->el_line
.buffer
;
657 len
= (ssize_t
)Strlen(prompt
);
658 (void)memcpy(cp
, prompt
, (size_t)len
* sizeof(*cp
));
664 el
->el_line
.cursor
= cp
;
666 el
->el_line
.lastchar
= cp
+ 1;
669 if (FUN(el
,getc
)(el
, &ch
) != 1) {
670 ed_end_of_file(el
, 0);
677 case 0010: /* Delete and backspace */
687 case '\r': /* Newline */
693 if (len
>= (ssize_t
)(EL_BUFSIZ
- 16))
704 el
->el_line
.buffer
[0] = '\0';
705 el
->el_line
.lastchar
= el
->el_line
.buffer
;
706 el
->el_line
.cursor
= el
->el_line
.buffer
;
712 * Return the current horizontal position of the cursor
720 * Find how many characters till the beginning of this line.
722 if (el
->el_line
.cursor
== el
->el_line
.buffer
)
725 for (ptr
= el
->el_line
.cursor
- 1;
726 ptr
>= el
->el_line
.buffer
&& *ptr
!= '\n';
729 return (int)(el
->el_line
.cursor
- ptr
- 1);
734 ch_resizefun(EditLine
*el
, el_zfunc_t f
, void *a
)
736 el
->el_chared
.c_resizefun
= f
;
737 el
->el_chared
.c_resizearg
= a
;