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.5 2004/10/25 19:38:45 drhodus Exp $
41 * chared.c: Character editor utilities
49 * Handle state for the vi undo command
52 cv_undo(el, action, size, ptr)
57 c_undo_t *vu = &el->el_chared.c_undo;
61 (void) memcpy(vu->buf, vu->ptr, size);
63 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
64 vu->ptr, vu->isize, vu->dsize);
70 * Insert num characters
79 if (el->el_line.lastchar + num >= el->el_line.limit)
80 return; /* can't go past end of buffer */
82 if (el->el_line.cursor < el->el_line.lastchar) {
83 /* if I must move chars */
84 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
87 el->el_line.lastchar += num;
92 * Delete num characters after the cursor
100 if (el->el_line.cursor + num > el->el_line.lastchar)
101 num = el->el_line.lastchar - el->el_line.cursor;
106 if (el->el_map.current != el->el_map.emacs)
107 cv_undo(el, INSERT, num, el->el_line.cursor);
109 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
112 el->el_line.lastchar -= num;
118 * Delete num characters before the cursor
126 if (el->el_line.cursor - num < el->el_line.buffer)
127 num = el->el_line.cursor - el->el_line.buffer;
132 if (el->el_map.current != el->el_map.emacs)
133 cv_undo(el, INSERT, num, el->el_line.cursor - num);
135 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
138 el->el_line.lastchar -= num;
144 * Return if p is part of a word according to emacs
150 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
155 * Return type of word for p according to vi
161 if (isspace((unsigned char) p))
163 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
170 * Return if p is part of a space-delimited word (!isspace)
176 return !isspace((unsigned char) p);
181 * Find the previous word
184 c__prev_word(p, low, n, wtest)
192 while ((p >= low) && !(*wtest)((unsigned char) *p))
194 while ((p >= low) && (*wtest)((unsigned char) *p))
198 /* cp now points to one character before the word */
202 /* cp now points where we want it */
211 c__next_word(p, high, n, wtest)
217 while ((p < high) && !(*wtest)((unsigned char) *p))
219 while ((p < high) && (*wtest)((unsigned char) *p))
224 /* p now points where we want it */
229 * Find the next word vi style
232 cv_next_word(el, p, high, n, wtest)
241 test = (*wtest)((unsigned char) *p);
242 while ((p < high) && (*wtest)((unsigned char) *p) == test)
245 * vi historically deletes with cw only the word preserving the
246 * trailing whitespace! This is not what 'w' does..
248 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
249 while ((p < high) && isspace((unsigned char) *p))
253 /* p now points where we want it */
262 * Find the previous word vi style
265 cv_prev_word(el, p, low, n, wtest)
276 * vi historically deletes with cb only the word preserving the
277 * leading whitespace! This is not what 'b' does..
279 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
280 while ((p > low) && isspace((unsigned char) *p))
282 test = (*wtest)((unsigned char) *p);
283 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
286 while (isspace((unsigned char) *p))
290 /* p now points where we want it */
300 * Ignore character p points to, return number appearing after that.
301 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
302 * Return p pointing to last char used.
305 c__number(p, num, dval)
306 char *p; /* character position */
307 int *num; /* Return value */
308 int dval; /* dval is the number to subtract from like $-3 */
319 *num = 0x7fffffff; /* Handle $ */
322 sign = -1; /* Handle $- */
325 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
327 *num = (sign < 0 ? dval - i : i);
333 * Finish vi delete action
342 if (el->el_chared.c_vcmd.action & INSERT)
343 el->el_map.current = el->el_map.key;
345 oaction = el->el_chared.c_vcmd.action;
346 el->el_chared.c_vcmd.action = NOP;
348 if (el->el_chared.c_vcmd.pos == 0)
352 if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
353 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
354 c_delbefore(el, size);
355 el->el_line.cursor = el->el_chared.c_vcmd.pos;
356 re_refresh_cursor(el);
358 else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
359 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
360 c_delafter(el, size);
364 c_delafter(el, size);
368 el->el_chared.c_undo.action = DELETE|INSERT;
371 el->el_chared.c_undo.action = INSERT;
381 el->el_chared.c_undo.ptr = el->el_line.cursor;
382 el->el_chared.c_undo.dsize = size;
388 * Go to the end of this word according to emacs
391 ce__endword(p, high, n)
398 while ((p < high) && isspace((unsigned char) *p))
400 while ((p < high) && !isspace((unsigned char) *p))
411 * Go to the end of this word according to vi
414 cv__endword(p, high, n)
421 while ((p < high) && isspace((unsigned char) *p))
424 if (isalnum((unsigned char) *p))
425 while ((p < high) && isalnum((unsigned char) *p))
428 while ((p < high) && !(isspace((unsigned char) *p) ||
429 isalnum((unsigned char) *p)))
437 * Initialize the character editor
443 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
444 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
445 el->el_line.cursor = el->el_line.buffer;
446 el->el_line.lastchar = el->el_line.buffer;
447 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
449 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
450 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
451 el->el_chared.c_undo.action = NOP;
452 el->el_chared.c_undo.isize = 0;
453 el->el_chared.c_undo.dsize = 0;
454 el->el_chared.c_undo.ptr = el->el_line.buffer;
456 el->el_chared.c_vcmd.action = NOP;
457 el->el_chared.c_vcmd.pos = el->el_line.buffer;
458 el->el_chared.c_vcmd.ins = el->el_line.buffer;
460 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
461 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
462 el->el_chared.c_kill.mark = el->el_line.buffer;
463 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
465 el->el_map.current = el->el_map.key;
467 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
468 el->el_state.doingarg = 0;
469 el->el_state.metanext = 0;
470 el->el_state.argument = 1;
471 el->el_state.lastcmd = ED_UNASSIGNED;
473 el->el_chared.c_macro.nline = NULL;
474 el->el_chared.c_macro.level = -1;
475 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
481 * Reset the character editor
487 el->el_line.cursor = el->el_line.buffer;
488 el->el_line.lastchar = el->el_line.buffer;
490 el->el_chared.c_undo.action = NOP;
491 el->el_chared.c_undo.isize = 0;
492 el->el_chared.c_undo.dsize = 0;
493 el->el_chared.c_undo.ptr = el->el_line.buffer;
495 el->el_chared.c_vcmd.action = NOP;
496 el->el_chared.c_vcmd.pos = el->el_line.buffer;
497 el->el_chared.c_vcmd.ins = el->el_line.buffer;
499 el->el_chared.c_kill.mark = el->el_line.buffer;
501 el->el_map.current = el->el_map.key;
503 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
504 el->el_state.doingarg = 0;
505 el->el_state.metanext = 0;
506 el->el_state.argument = 1;
507 el->el_state.lastcmd = ED_UNASSIGNED;
509 el->el_chared.c_macro.level = -1;
511 el->el_history.eventno = 0;
516 * Free the data structures used by the editor
522 el_free((ptr_t) el->el_line.buffer);
523 el->el_line.buffer = NULL;
524 el->el_line.limit = NULL;
525 el_free((ptr_t) el->el_chared.c_undo.buf);
526 el->el_chared.c_undo.buf = NULL;
527 el_free((ptr_t) el->el_chared.c_kill.buf);
528 el->el_chared.c_kill.buf = NULL;
529 el_free((ptr_t) el->el_chared.c_macro.macro);
530 el->el_chared.c_macro.macro = NULL;
536 * Insert string at cursorI
545 if ((len = strlen(s)) == 0)
547 if (el->el_line.lastchar + len >= el->el_line.limit)
552 *el->el_line.cursor++ = *s++;
558 * Delete num characters before the cursor
568 if (el->el_line.cursor < &el->el_line.buffer[n])
571 c_delbefore(el, n); /* delete before dot */
572 el->el_line.cursor -= n;
573 if (el->el_line.cursor < el->el_line.buffer)
574 el->el_line.cursor = el->el_line.buffer;
588 for (ch = 0; ch == 0;) {
589 if (el_getc(el, &ch) != 1)
590 return ed_end_of_file(el, 0);
592 case '\010': /* Delete and backspace */
595 *el->el_line.cursor-- = '\0';
596 el->el_line.lastchar = el->el_line.cursor;
600 el->el_line.buffer[0] = '\0';
601 el->el_line.lastchar = el->el_line.buffer;
602 el->el_line.cursor = el->el_line.buffer;
609 case '\033': /* ESC */
610 case '\r': /* Newline */
615 if (len >= EL_BUFSIZ)
619 *el->el_line.cursor++ = ch;
620 el->el_line.lastchar = el->el_line.cursor;
633 * Return the current horizontal position of the cursor
642 * Find how many characters till the beginning of this line.
644 if (el->el_line.cursor == el->el_line.buffer)
647 for (ptr = el->el_line.cursor - 1;
648 ptr >= el->el_line.buffer && *ptr != '\n';
651 return el->el_line.cursor - ptr - 1;