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
40 * chared.c: Character editor utilities
48 * Handle state for the vi undo command
51 cv_undo(el, action, size, ptr)
56 c_undo_t *vu = &el->el_chared.c_undo;
60 (void) memcpy(vu->buf, vu->ptr, size);
62 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
63 vu->ptr, vu->isize, vu->dsize);
69 * Insert num characters
78 if (el->el_line.lastchar + num >= el->el_line.limit)
79 return; /* can't go past end of buffer */
81 if (el->el_line.cursor < el->el_line.lastchar) {
82 /* if I must move chars */
83 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
86 el->el_line.lastchar += num;
91 * Delete num characters after the cursor
99 if (el->el_line.cursor + num > el->el_line.lastchar)
100 num = el->el_line.lastchar - el->el_line.cursor;
105 if (el->el_map.current != el->el_map.emacs)
106 cv_undo(el, INSERT, num, el->el_line.cursor);
108 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
111 el->el_line.lastchar -= num;
117 * Delete num characters before the cursor
125 if (el->el_line.cursor - num < el->el_line.buffer)
126 num = el->el_line.cursor - el->el_line.buffer;
131 if (el->el_map.current != el->el_map.emacs)
132 cv_undo(el, INSERT, num, el->el_line.cursor - num);
134 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
137 el->el_line.lastchar -= num;
143 * Return if p is part of a word according to emacs
149 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
154 * Return type of word for p according to vi
160 if (isspace((unsigned char) p))
162 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
169 * Return if p is part of a space-delimited word (!isspace)
175 return !isspace((unsigned char) p);
180 * Find the previous word
183 c__prev_word(p, low, n, wtest)
184 register char *p, *low;
186 int (*wtest) __P((int));
191 while ((p >= low) && !(*wtest)((unsigned char) *p))
193 while ((p >= low) && (*wtest)((unsigned char) *p))
197 /* cp now points to one character before the word */
201 /* cp now points where we want it */
210 c__next_word(p, high, n, wtest)
211 register char *p, *high;
213 int (*wtest) __P((int));
216 while ((p < high) && !(*wtest)((unsigned char) *p))
218 while ((p < high) && (*wtest)((unsigned char) *p))
223 /* p now points where we want it */
228 * Find the next word vi style
231 cv_next_word(el, p, high, n, wtest)
233 register char *p, *high;
235 int (*wtest) __P((int));
240 test = (*wtest)((unsigned char) *p);
241 while ((p < high) && (*wtest)((unsigned char) *p) == test)
244 * vi historically deletes with cw only the word preserving the
245 * trailing whitespace! This is not what 'w' does..
247 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
248 while ((p < high) && isspace((unsigned char) *p))
252 /* p now points where we want it */
261 * Find the previous word vi style
264 cv_prev_word(el, p, low, n, wtest)
266 register char *p, *low;
268 int (*wtest) __P((int));
275 * vi historically deletes with cb only the word preserving the
276 * leading whitespace! This is not what 'b' does..
278 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
279 while ((p > low) && isspace((unsigned char) *p))
281 test = (*wtest)((unsigned char) *p);
282 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
285 while (isspace((unsigned char) *p))
289 /* p now points where we want it */
299 * Ignore character p points to, return number appearing after that.
300 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
301 * Return p pointing to last char used.
304 c__number(p, num, dval)
305 char *p; /* character position */
306 int *num; /* Return value */
307 int dval; /* dval is the number to subtract from like $-3 */
310 register int sign = 1;
318 *num = 0x7fffffff; /* Handle $ */
321 sign = -1; /* Handle $- */
324 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
326 *num = (sign < 0 ? dval - i : i);
332 * Finish vi delete action
341 if (el->el_chared.c_vcmd.action & INSERT)
342 el->el_map.current = el->el_map.key;
344 oaction = el->el_chared.c_vcmd.action;
345 el->el_chared.c_vcmd.action = NOP;
347 if (el->el_chared.c_vcmd.pos == 0)
351 if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
352 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
353 c_delbefore(el, size);
354 el->el_line.cursor = el->el_chared.c_vcmd.pos;
355 re_refresh_cursor(el);
357 else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
358 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
359 c_delafter(el, size);
363 c_delafter(el, size);
367 el->el_chared.c_undo.action = DELETE|INSERT;
370 el->el_chared.c_undo.action = INSERT;
380 el->el_chared.c_undo.ptr = el->el_line.cursor;
381 el->el_chared.c_undo.dsize = size;
387 * Go to the end of this word according to emacs
390 ce__endword(p, high, n)
397 while ((p < high) && isspace((unsigned char) *p))
399 while ((p < high) && !isspace((unsigned char) *p))
410 * Go to the end of this word according to vi
413 cv__endword(p, high, n)
420 while ((p < high) && isspace((unsigned char) *p))
423 if (isalnum((unsigned char) *p))
424 while ((p < high) && isalnum((unsigned char) *p))
427 while ((p < high) && !(isspace((unsigned char) *p) ||
428 isalnum((unsigned char) *p)))
436 * Initialize the character editor
442 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
443 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
444 el->el_line.cursor = el->el_line.buffer;
445 el->el_line.lastchar = el->el_line.buffer;
446 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
448 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
449 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
450 el->el_chared.c_undo.action = NOP;
451 el->el_chared.c_undo.isize = 0;
452 el->el_chared.c_undo.dsize = 0;
453 el->el_chared.c_undo.ptr = el->el_line.buffer;
455 el->el_chared.c_vcmd.action = NOP;
456 el->el_chared.c_vcmd.pos = el->el_line.buffer;
457 el->el_chared.c_vcmd.ins = el->el_line.buffer;
459 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
460 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
461 el->el_chared.c_kill.mark = el->el_line.buffer;
462 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
464 el->el_map.current = el->el_map.key;
466 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
467 el->el_state.doingarg = 0;
468 el->el_state.metanext = 0;
469 el->el_state.argument = 1;
470 el->el_state.lastcmd = ED_UNASSIGNED;
472 el->el_chared.c_macro.nline = NULL;
473 el->el_chared.c_macro.level = -1;
474 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
480 * Reset the character editor
486 el->el_line.cursor = el->el_line.buffer;
487 el->el_line.lastchar = el->el_line.buffer;
489 el->el_chared.c_undo.action = NOP;
490 el->el_chared.c_undo.isize = 0;
491 el->el_chared.c_undo.dsize = 0;
492 el->el_chared.c_undo.ptr = el->el_line.buffer;
494 el->el_chared.c_vcmd.action = NOP;
495 el->el_chared.c_vcmd.pos = el->el_line.buffer;
496 el->el_chared.c_vcmd.ins = el->el_line.buffer;
498 el->el_chared.c_kill.mark = el->el_line.buffer;
500 el->el_map.current = el->el_map.key;
502 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
503 el->el_state.doingarg = 0;
504 el->el_state.metanext = 0;
505 el->el_state.argument = 1;
506 el->el_state.lastcmd = ED_UNASSIGNED;
508 el->el_chared.c_macro.level = -1;
510 el->el_history.eventno = 0;
515 * Free the data structures used by the editor
521 el_free((ptr_t) el->el_line.buffer);
522 el->el_line.buffer = NULL;
523 el->el_line.limit = NULL;
524 el_free((ptr_t) el->el_chared.c_undo.buf);
525 el->el_chared.c_undo.buf = NULL;
526 el_free((ptr_t) el->el_chared.c_kill.buf);
527 el->el_chared.c_kill.buf = NULL;
528 el_free((ptr_t) el->el_chared.c_macro.macro);
529 el->el_chared.c_macro.macro = NULL;
535 * Insert string at cursorI
544 if ((len = strlen(s)) == 0)
546 if (el->el_line.lastchar + len >= el->el_line.limit)
551 *el->el_line.cursor++ = *s++;
557 * Delete num characters before the cursor
567 if (el->el_line.cursor < &el->el_line.buffer[n])
570 c_delbefore(el, n); /* delete before dot */
571 el->el_line.cursor -= n;
572 if (el->el_line.cursor < el->el_line.buffer)
573 el->el_line.cursor = el->el_line.buffer;
587 for (ch = 0; ch == 0;) {
588 if (el_getc(el, &ch) != 1)
589 return ed_end_of_file(el, 0);
591 case '\010': /* Delete and backspace */
594 *el->el_line.cursor-- = '\0';
595 el->el_line.lastchar = el->el_line.cursor;
599 el->el_line.buffer[0] = '\0';
600 el->el_line.lastchar = el->el_line.buffer;
601 el->el_line.cursor = el->el_line.buffer;
608 case '\033': /* ESC */
609 case '\r': /* Newline */
614 if (len >= EL_BUFSIZ)
618 *el->el_line.cursor++ = ch;
619 el->el_line.lastchar = el->el_line.cursor;
632 * Return the current horizontal position of the cursor
641 * Find how many characters till the beginning of this line.
643 if (el->el_line.cursor == el->el_line.buffer)
646 for (ptr = el->el_line.cursor - 1;
647 ptr >= el->el_line.buffer && *ptr != '\n';
650 return el->el_line.cursor - ptr - 1;