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
37 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
39 #endif /* not lint && not SCCSID */
42 * chared.c: Character editor utilities
50 * Handle state for the vi undo command
53 cv_undo(el, action, size, ptr)
58 c_undo_t *vu = &el->el_chared.c_undo;
62 (void) memcpy(vu->buf, vu->ptr, size);
64 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
65 vu->ptr, vu->isize, vu->dsize);
71 * Insert num characters
80 if (el->el_line.lastchar + num >= el->el_line.limit)
81 return; /* can't go past end of buffer */
83 if (el->el_line.cursor < el->el_line.lastchar) {
84 /* if I must move chars */
85 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
88 el->el_line.lastchar += num;
93 * Delete num characters after the cursor
101 if (el->el_line.cursor + num > el->el_line.lastchar)
102 num = el->el_line.lastchar - el->el_line.cursor;
107 if (el->el_map.current != el->el_map.emacs)
108 cv_undo(el, INSERT, num, el->el_line.cursor);
110 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
113 el->el_line.lastchar -= num;
119 * Delete num characters before the cursor
127 if (el->el_line.cursor - num < el->el_line.buffer)
128 num = el->el_line.cursor - el->el_line.buffer;
133 if (el->el_map.current != el->el_map.emacs)
134 cv_undo(el, INSERT, num, el->el_line.cursor - num);
136 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
139 el->el_line.lastchar -= num;
145 * Return if p is part of a word according to emacs
151 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
156 * Return type of word for p according to vi
162 if (isspace((unsigned char) p))
164 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
171 * Return if p is part of a space-delimited word (!isspace)
177 return !isspace((unsigned char) p);
182 * Find the previous word
185 c__prev_word(p, low, n, wtest)
186 register char *p, *low;
188 int (*wtest) __P((int));
193 while ((p >= low) && !(*wtest)((unsigned char) *p))
195 while ((p >= low) && (*wtest)((unsigned char) *p))
199 /* cp now points to one character before the word */
203 /* cp now points where we want it */
212 c__next_word(p, high, n, wtest)
213 register char *p, *high;
215 int (*wtest) __P((int));
218 while ((p < high) && !(*wtest)((unsigned char) *p))
220 while ((p < high) && (*wtest)((unsigned char) *p))
225 /* p now points where we want it */
230 * Find the next word vi style
233 cv_next_word(el, p, high, n, wtest)
235 register char *p, *high;
237 int (*wtest) __P((int));
242 test = (*wtest)((unsigned char) *p);
243 while ((p < high) && (*wtest)((unsigned char) *p) == test)
246 * vi historically deletes with cw only the word preserving the
247 * trailing whitespace! This is not what 'w' does..
249 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
250 while ((p < high) && isspace((unsigned char) *p))
254 /* p now points where we want it */
263 * Find the previous word vi style
266 cv_prev_word(el, p, low, n, wtest)
268 register char *p, *low;
270 int (*wtest) __P((int));
277 * vi historically deletes with cb only the word preserving the
278 * leading whitespace! This is not what 'b' does..
280 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
281 while ((p > low) && isspace((unsigned char) *p))
283 test = (*wtest)((unsigned char) *p);
284 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
287 while (isspace((unsigned char) *p))
291 /* p now points where we want it */
301 * Ignore character p points to, return number appearing after that.
302 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
303 * Return p pointing to last char used.
306 c__number(p, num, dval)
307 char *p; /* character position */
308 int *num; /* Return value */
309 int dval; /* dval is the number to subtract from like $-3 */
312 register int sign = 1;
320 *num = 0x7fffffff; /* Handle $ */
323 sign = -1; /* Handle $- */
326 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
328 *num = (sign < 0 ? dval - i : i);
334 * Finish vi delete action
343 if (el->el_chared.c_vcmd.action & INSERT)
344 el->el_map.current = el->el_map.key;
346 oaction = el->el_chared.c_vcmd.action;
347 el->el_chared.c_vcmd.action = NOP;
349 if (el->el_chared.c_vcmd.pos == 0)
353 if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
354 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
355 c_delbefore(el, size);
356 el->el_line.cursor = el->el_chared.c_vcmd.pos;
357 re_refresh_cursor(el);
359 else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
360 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
361 c_delafter(el, size);
365 c_delafter(el, size);
369 el->el_chared.c_undo.action = DELETE|INSERT;
372 el->el_chared.c_undo.action = INSERT;
382 el->el_chared.c_undo.ptr = el->el_line.cursor;
383 el->el_chared.c_undo.dsize = size;
389 * Go to the end of this word according to emacs
392 ce__endword(p, high, n)
399 while ((p < high) && isspace((unsigned char) *p))
401 while ((p < high) && !isspace((unsigned char) *p))
412 * Go to the end of this word according to vi
415 cv__endword(p, high, n)
422 while ((p < high) && isspace((unsigned char) *p))
425 if (isalnum((unsigned char) *p))
426 while ((p < high) && isalnum((unsigned char) *p))
429 while ((p < high) && !(isspace((unsigned char) *p) ||
430 isalnum((unsigned char) *p)))
438 * Initialize the character editor
444 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
445 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
446 el->el_line.cursor = el->el_line.buffer;
447 el->el_line.lastchar = el->el_line.buffer;
448 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
450 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
451 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
452 el->el_chared.c_undo.action = NOP;
453 el->el_chared.c_undo.isize = 0;
454 el->el_chared.c_undo.dsize = 0;
455 el->el_chared.c_undo.ptr = el->el_line.buffer;
457 el->el_chared.c_vcmd.action = NOP;
458 el->el_chared.c_vcmd.pos = el->el_line.buffer;
459 el->el_chared.c_vcmd.ins = el->el_line.buffer;
461 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
462 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
463 el->el_chared.c_kill.mark = el->el_line.buffer;
464 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
466 el->el_map.current = el->el_map.key;
468 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
469 el->el_state.doingarg = 0;
470 el->el_state.metanext = 0;
471 el->el_state.argument = 1;
472 el->el_state.lastcmd = ED_UNASSIGNED;
474 el->el_chared.c_macro.nline = NULL;
475 el->el_chared.c_macro.level = -1;
476 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
482 * Reset the character editor
488 el->el_line.cursor = el->el_line.buffer;
489 el->el_line.lastchar = el->el_line.buffer;
491 el->el_chared.c_undo.action = NOP;
492 el->el_chared.c_undo.isize = 0;
493 el->el_chared.c_undo.dsize = 0;
494 el->el_chared.c_undo.ptr = el->el_line.buffer;
496 el->el_chared.c_vcmd.action = NOP;
497 el->el_chared.c_vcmd.pos = el->el_line.buffer;
498 el->el_chared.c_vcmd.ins = el->el_line.buffer;
500 el->el_chared.c_kill.mark = el->el_line.buffer;
502 el->el_map.current = el->el_map.key;
504 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
505 el->el_state.doingarg = 0;
506 el->el_state.metanext = 0;
507 el->el_state.argument = 1;
508 el->el_state.lastcmd = ED_UNASSIGNED;
510 el->el_chared.c_macro.level = -1;
512 el->el_history.eventno = 0;
517 * Free the data structures used by the editor
523 el_free((ptr_t) el->el_line.buffer);
524 el->el_line.buffer = NULL;
525 el->el_line.limit = NULL;
526 el_free((ptr_t) el->el_chared.c_undo.buf);
527 el->el_chared.c_undo.buf = NULL;
528 el_free((ptr_t) el->el_chared.c_kill.buf);
529 el->el_chared.c_kill.buf = NULL;
530 el_free((ptr_t) el->el_chared.c_macro.macro);
531 el->el_chared.c_macro.macro = NULL;
537 * Insert string at cursorI
546 if ((len = strlen(s)) == 0)
548 if (el->el_line.lastchar + len >= el->el_line.limit)
553 *el->el_line.cursor++ = *s++;
559 * Delete num characters before the cursor
569 if (el->el_line.cursor < &el->el_line.buffer[n])
572 c_delbefore(el, n); /* delete before dot */
573 el->el_line.cursor -= n;
574 if (el->el_line.cursor < el->el_line.buffer)
575 el->el_line.cursor = el->el_line.buffer;
589 for (ch = 0; ch == 0;) {
590 if (el_getc(el, &ch) != 1)
591 return ed_end_of_file(el, 0);
593 case '\010': /* Delete and backspace */
596 *el->el_line.cursor-- = '\0';
597 el->el_line.lastchar = el->el_line.cursor;
601 el->el_line.buffer[0] = '\0';
602 el->el_line.lastchar = el->el_line.buffer;
603 el->el_line.cursor = el->el_line.buffer;
610 case '\033': /* ESC */
611 case '\r': /* Newline */
616 if (len >= EL_BUFSIZ)
620 *el->el_line.cursor++ = ch;
621 el->el_line.lastchar = el->el_line.cursor;
634 * Return the current horizontal position of the cursor
643 * Find how many characters till the beginning of this line.
645 if (el->el_line.cursor == el->el_line.buffer)
648 for (ptr = el->el_line.cursor - 1;
649 ptr >= el->el_line.buffer && *ptr != '\n';
652 return el->el_line.cursor - ptr - 1;