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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)chared.c 8.1 (Berkeley) 6/4/93
37 * $DragonFly: src/lib/libedit/chared.c,v 1.6 2005/08/04 17:27:09 drhodus Exp $
41 * chared.c: Character editor utilities
49 * Handle state for the vi undo command
52 cv_undo(EditLine *el, int action, int size, char *ptr)
54 c_undo_t *vu = &el->el_chared.c_undo;
58 (void) memcpy(vu->buf, vu->ptr, size);
60 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
61 vu->ptr, vu->isize, vu->dsize);
67 * Insert num characters
70 c_insert(EditLine *el, int num)
74 if (el->el_line.lastchar + num >= el->el_line.limit)
75 return; /* can't go past end of buffer */
77 if (el->el_line.cursor < el->el_line.lastchar) {
78 /* if I must move chars */
79 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
82 el->el_line.lastchar += num;
87 * Delete num characters after the cursor
90 c_delafter(EditLine *el, int num)
93 if (el->el_line.cursor + num > el->el_line.lastchar)
94 num = el->el_line.lastchar - el->el_line.cursor;
99 if (el->el_map.current != el->el_map.emacs)
100 cv_undo(el, INSERT, num, el->el_line.cursor);
102 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
105 el->el_line.lastchar -= num;
111 * Delete num characters before the cursor
114 c_delbefore(EditLine *el, int num)
117 if (el->el_line.cursor - num < el->el_line.buffer)
118 num = el->el_line.cursor - el->el_line.buffer;
123 if (el->el_map.current != el->el_map.emacs)
124 cv_undo(el, INSERT, num, el->el_line.cursor - num);
126 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
129 el->el_line.lastchar -= num;
135 * Return if p is part of a word according to emacs
140 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
145 * Return type of word for p according to vi
150 if (isspace((unsigned char) p))
152 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
159 * Return if p is part of a space-delimited word (!isspace)
164 return !isspace((unsigned char) p);
169 * Find the previous word
172 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
177 while ((p >= low) && !(*wtest)((unsigned char) *p))
179 while ((p >= low) && (*wtest)((unsigned char) *p))
183 /* cp now points to one character before the word */
187 /* cp now points where we want it */
196 c__next_word(char *p, char *high, int n, int (*wtest)(int))
199 while ((p < high) && !(*wtest)((unsigned char) *p))
201 while ((p < high) && (*wtest)((unsigned char) *p))
206 /* p now points where we want it */
211 * Find the next word vi style
214 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
219 test = (*wtest)((unsigned char) *p);
220 while ((p < high) && (*wtest)((unsigned char) *p) == test)
223 * vi historically deletes with cw only the word preserving the
224 * trailing whitespace! This is not what 'w' does..
226 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
227 while ((p < high) && isspace((unsigned char) *p))
231 /* p now points where we want it */
240 * Find the previous word vi style
243 cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
250 * vi historically deletes with cb only the word preserving the
251 * leading whitespace! This is not what 'b' does..
253 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
254 while ((p > low) && isspace((unsigned char) *p))
256 test = (*wtest)((unsigned char) *p);
257 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
260 while (isspace((unsigned char) *p))
264 /* p now points where we want it */
274 * Ignore character p points to, return number appearing after that.
275 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
276 * Return p pointing to last char used.
280 char *p, /* character position */
281 int *num, /* Return value */
282 int dval, /* dval is the number to subtract from like $-3 */
294 *num = 0x7fffffff; /* Handle $ */
297 sign = -1; /* Handle $- */
300 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
302 *num = (sign < 0 ? dval - i : i);
308 * Finish vi delete action
311 cv_delfini(EditLine *el)
316 if (el->el_chared.c_vcmd.action & INSERT)
317 el->el_map.current = el->el_map.key;
319 oaction = el->el_chared.c_vcmd.action;
320 el->el_chared.c_vcmd.action = NOP;
322 if (el->el_chared.c_vcmd.pos == 0)
326 if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
327 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
328 c_delbefore(el, size);
329 el->el_line.cursor = el->el_chared.c_vcmd.pos;
330 re_refresh_cursor(el);
332 else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
333 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
334 c_delafter(el, size);
338 c_delafter(el, size);
342 el->el_chared.c_undo.action = DELETE|INSERT;
345 el->el_chared.c_undo.action = INSERT;
355 el->el_chared.c_undo.ptr = el->el_line.cursor;
356 el->el_chared.c_undo.dsize = size;
362 * Go to the end of this word according to emacs
365 ce__endword(char *p, char *high, int n)
370 while ((p < high) && isspace((unsigned char) *p))
372 while ((p < high) && !isspace((unsigned char) *p))
383 * Go to the end of this word according to vi
386 cv__endword(char *p, char *high, int n)
391 while ((p < high) && isspace((unsigned char) *p))
394 if (isalnum((unsigned char) *p))
395 while ((p < high) && isalnum((unsigned char) *p))
398 while ((p < high) && !(isspace((unsigned char) *p) ||
399 isalnum((unsigned char) *p)))
407 * Initialize the character editor
410 ch_init(EditLine *el)
412 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
413 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
414 el->el_line.cursor = el->el_line.buffer;
415 el->el_line.lastchar = el->el_line.buffer;
416 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
418 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
419 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
420 el->el_chared.c_undo.action = NOP;
421 el->el_chared.c_undo.isize = 0;
422 el->el_chared.c_undo.dsize = 0;
423 el->el_chared.c_undo.ptr = el->el_line.buffer;
425 el->el_chared.c_vcmd.action = NOP;
426 el->el_chared.c_vcmd.pos = el->el_line.buffer;
427 el->el_chared.c_vcmd.ins = el->el_line.buffer;
429 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
430 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
431 el->el_chared.c_kill.mark = el->el_line.buffer;
432 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
434 el->el_map.current = el->el_map.key;
436 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
437 el->el_state.doingarg = 0;
438 el->el_state.metanext = 0;
439 el->el_state.argument = 1;
440 el->el_state.lastcmd = ED_UNASSIGNED;
442 el->el_chared.c_macro.nline = NULL;
443 el->el_chared.c_macro.level = -1;
444 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
450 * Reset the character editor
453 ch_reset(EditLine *el)
455 el->el_line.cursor = el->el_line.buffer;
456 el->el_line.lastchar = el->el_line.buffer;
458 el->el_chared.c_undo.action = NOP;
459 el->el_chared.c_undo.isize = 0;
460 el->el_chared.c_undo.dsize = 0;
461 el->el_chared.c_undo.ptr = el->el_line.buffer;
463 el->el_chared.c_vcmd.action = NOP;
464 el->el_chared.c_vcmd.pos = el->el_line.buffer;
465 el->el_chared.c_vcmd.ins = el->el_line.buffer;
467 el->el_chared.c_kill.mark = el->el_line.buffer;
469 el->el_map.current = el->el_map.key;
471 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
472 el->el_state.doingarg = 0;
473 el->el_state.metanext = 0;
474 el->el_state.argument = 1;
475 el->el_state.lastcmd = ED_UNASSIGNED;
477 el->el_chared.c_macro.level = -1;
479 el->el_history.eventno = 0;
484 * Free the data structures used by the editor
489 el_free((ptr_t) el->el_line.buffer);
490 el->el_line.buffer = NULL;
491 el->el_line.limit = NULL;
492 el_free((ptr_t) el->el_chared.c_undo.buf);
493 el->el_chared.c_undo.buf = NULL;
494 el_free((ptr_t) el->el_chared.c_kill.buf);
495 el->el_chared.c_kill.buf = NULL;
496 el_free((ptr_t) el->el_chared.c_macro.macro);
497 el->el_chared.c_macro.macro = NULL;
503 * Insert string at cursorI
506 el_insertstr(EditLine *el, char *s)
510 if ((len = strlen(s)) == 0)
512 if (el->el_line.lastchar + len >= el->el_line.limit)
517 *el->el_line.cursor++ = *s++;
523 * Delete num characters before the cursor
526 el_deletestr(EditLine *el, int n)
531 if (el->el_line.cursor < &el->el_line.buffer[n])
534 c_delbefore(el, n); /* delete before dot */
535 el->el_line.cursor -= n;
536 if (el->el_line.cursor < el->el_line.buffer)
537 el->el_line.cursor = el->el_line.buffer;
544 c_gets(EditLine *el, char *buf)
549 for (ch = 0; ch == 0;) {
550 if (el_getc(el, &ch) != 1)
551 return ed_end_of_file(el, 0);
553 case '\010': /* Delete and backspace */
556 *el->el_line.cursor-- = '\0';
557 el->el_line.lastchar = el->el_line.cursor;
561 el->el_line.buffer[0] = '\0';
562 el->el_line.lastchar = el->el_line.buffer;
563 el->el_line.cursor = el->el_line.buffer;
570 case '\033': /* ESC */
571 case '\r': /* Newline */
576 if (len >= EL_BUFSIZ)
580 *el->el_line.cursor++ = ch;
581 el->el_line.lastchar = el->el_line.cursor;
594 * Return the current horizontal position of the cursor
602 * Find how many characters till the beginning of this line.
604 if (el->el_line.cursor == el->el_line.buffer)
607 for (ptr = el->el_line.cursor - 1;
608 ptr >= el->el_line.buffer && *ptr != '\n';
611 return el->el_line.cursor - ptr - 1;