Annotation of src/lib/libedit/chared.c, Revision 1.11
1.11 ! christos 1: /* $NetBSD: chared.c,v 1.10 2000/11/11 22:18:57 christos Exp $ */
1.2 lukem 2:
1.1 cgd 3: /*-
4: * Copyright (c) 1992, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Christos Zoulas of Cornell University.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
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. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
1.3 christos 39: #include <sys/cdefs.h>
1.1 cgd 40: #if !defined(lint) && !defined(SCCSID)
1.2 lukem 41: #if 0
1.1 cgd 42: static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
1.2 lukem 43: #else
1.11 ! christos 44: __RCSID("$NetBSD: chared.c,v 1.10 2000/11/11 22:18:57 christos Exp $");
1.2 lukem 45: #endif
1.1 cgd 46: #endif /* not lint && not SCCSID */
47:
1.7 simonb 48: /*
1.1 cgd 49: * chared.c: Character editor utilities
50: */
51: #include "sys.h"
52:
53: #include <stdlib.h>
54: #include "el.h"
55:
56: /* cv_undo():
57: * Handle state for the vi undo command
58: */
59: protected void
1.9 lukem 60: cv_undo(EditLine *el,int action, size_t size, char *ptr)
61: {
62: c_undo_t *vu = &el->el_chared.c_undo;
63: vu->action = action;
64: vu->ptr = ptr;
65: vu->isize = size;
66: (void) memcpy(vu->buf, vu->ptr, size);
1.1 cgd 67: #ifdef DEBUG_UNDO
1.9 lukem 68: (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
69: vu->ptr, vu->isize, vu->dsize);
1.1 cgd 70: #endif
71: }
72:
73:
1.7 simonb 74: /* c_insert():
1.1 cgd 75: * Insert num characters
76: */
77: protected void
1.9 lukem 78: c_insert(EditLine *el, int num)
79: {
80: char *cp;
81:
82: if (el->el_line.lastchar + num >= el->el_line.limit)
83: return; /* can't go past end of buffer */
84:
85: if (el->el_line.cursor < el->el_line.lastchar) {
86: /* if I must move chars */
87: for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
88: cp[num] = *cp;
89: }
90: el->el_line.lastchar += num;
91: }
1.1 cgd 92:
93:
94: /* c_delafter():
95: * Delete num characters after the cursor
96: */
97: protected void
1.9 lukem 98: c_delafter(EditLine *el, int num)
1.1 cgd 99: {
100:
1.9 lukem 101: if (el->el_line.cursor + num > el->el_line.lastchar)
102: num = el->el_line.lastchar - el->el_line.cursor;
1.1 cgd 103:
1.9 lukem 104: if (num > 0) {
105: char *cp;
1.1 cgd 106:
1.9 lukem 107: if (el->el_map.current != el->el_map.emacs)
108: cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
1.1 cgd 109:
1.9 lukem 110: for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
111: *cp = cp[num];
1.1 cgd 112:
1.9 lukem 113: el->el_line.lastchar -= num;
114: }
1.1 cgd 115: }
116:
117:
118: /* c_delbefore():
119: * Delete num characters before the cursor
120: */
121: protected void
1.9 lukem 122: c_delbefore(EditLine *el, int num)
1.1 cgd 123: {
124:
1.9 lukem 125: if (el->el_line.cursor - num < el->el_line.buffer)
126: num = el->el_line.cursor - el->el_line.buffer;
1.1 cgd 127:
1.9 lukem 128: if (num > 0) {
129: char *cp;
1.1 cgd 130:
1.9 lukem 131: if (el->el_map.current != el->el_map.emacs)
132: cv_undo(el, INSERT, (size_t)num,
133: el->el_line.cursor - num);
1.1 cgd 134:
1.9 lukem 135: for (cp = el->el_line.cursor - num;
136: cp <= el->el_line.lastchar;
137: cp++)
138: *cp = cp[num];
1.1 cgd 139:
1.9 lukem 140: el->el_line.lastchar -= num;
141: }
1.1 cgd 142: }
143:
144:
145: /* ce__isword():
146: * Return if p is part of a word according to emacs
147: */
148: protected int
1.9 lukem 149: ce__isword(int p)
1.1 cgd 150: {
1.9 lukem 151: return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
1.1 cgd 152: }
153:
154:
155: /* cv__isword():
156: * Return if p is part of a word according to vi
157: */
158: protected int
1.9 lukem 159: cv__isword(int p)
1.1 cgd 160: {
1.9 lukem 161: return (!isspace(p));
1.1 cgd 162: }
163:
164:
165: /* c__prev_word():
166: * Find the previous word
167: */
168: protected char *
1.9 lukem 169: c__prev_word(char *p, char *low, int n, int (*wtest)(int))
170: {
171: p--;
172:
173: while (n--) {
174: while ((p >= low) && !(*wtest)((unsigned char) *p))
175: p--;
176: while ((p >= low) && (*wtest)((unsigned char) *p))
177: p--;
178: }
179:
180: /* cp now points to one character before the word */
181: p++;
182: if (p < low)
183: p = low;
184: /* cp now points where we want it */
185: return (p);
1.1 cgd 186: }
187:
188:
189: /* c__next_word():
190: * Find the next word
191: */
192: protected char *
1.9 lukem 193: c__next_word(char *p, char *high, int n, int (*wtest)(int))
194: {
195: while (n--) {
196: while ((p < high) && !(*wtest)((unsigned char) *p))
197: p++;
198: while ((p < high) && (*wtest)((unsigned char) *p))
199: p++;
200: }
201: if (p > high)
202: p = high;
203: /* p now points where we want it */
204: return (p);
1.1 cgd 205: }
206:
207: /* cv_next_word():
208: * Find the next word vi style
209: */
210: protected char *
1.9 lukem 211: cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
212: {
213: int test;
214:
215: while (n--) {
216: test = (*wtest)((unsigned char) *p);
217: while ((p < high) && (*wtest)((unsigned char) *p) == test)
218: p++;
219: /*
220: * vi historically deletes with cw only the word preserving the
221: * trailing whitespace! This is not what 'w' does..
222: */
223: if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
224: while ((p < high) && isspace((unsigned char) *p))
225: p++;
226: }
1.1 cgd 227:
1.9 lukem 228: /* p now points where we want it */
229: if (p > high)
230: return (high);
231: else
232: return (p);
1.1 cgd 233: }
234:
235:
236: /* cv_prev_word():
237: * Find the previous word vi style
238: */
239: protected char *
1.9 lukem 240: cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
1.1 cgd 241: {
1.9 lukem 242: int test;
1.1 cgd 243:
1.9 lukem 244: while (n--) {
1.1 cgd 245: p--;
1.9 lukem 246: /*
247: * vi historically deletes with cb only the word preserving the
248: * leading whitespace! This is not what 'b' does..
249: */
250: if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
251: while ((p > low) && isspace((unsigned char) *p))
252: p--;
253: test = (*wtest)((unsigned char) *p);
254: while ((p >= low) && (*wtest)((unsigned char) *p) == test)
255: p--;
1.1 cgd 256: p++;
1.9 lukem 257: while (isspace((unsigned char) *p))
258: p++;
259: }
1.1 cgd 260:
1.9 lukem 261: /* p now points where we want it */
262: if (p < low)
263: return (low);
264: else
265: return (p);
1.1 cgd 266: }
267:
268:
269: #ifdef notdef
270: /* c__number():
271: * Ignore character p points to, return number appearing after that.
272: * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
273: * Return p pointing to last char used.
274: */
275: protected char *
1.9 lukem 276: c__number(
277: char *p, /* character position */
278: int *num, /* Return value */
279: int dval) /* dval is the number to subtract from like $-3 */
280: {
281: int i;
282: int sign = 1;
283:
284: if (*++p == '^') {
285: *num = 1;
286: return (p);
287: }
288: if (*p == '$') {
289: if (*++p != '-') {
290: *num = 0x7fffffff; /* Handle $ */
291: return (--p);
292: }
293: sign = -1; /* Handle $- */
294: ++p;
295: }
296: for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
297: continue;
298: *num = (sign < 0 ? dval - i : i);
299: return (--p);
1.1 cgd 300: }
301: #endif
302:
303: /* cv_delfini():
304: * Finish vi delete action
305: */
306: protected void
1.9 lukem 307: cv_delfini(EditLine *el)
1.1 cgd 308: {
1.9 lukem 309: int size;
310: int oaction;
1.1 cgd 311:
1.9 lukem 312: if (el->el_chared.c_vcmd.action & INSERT)
313: el->el_map.current = el->el_map.key;
1.1 cgd 314:
1.9 lukem 315: oaction = el->el_chared.c_vcmd.action;
316: el->el_chared.c_vcmd.action = NOP;
317:
318: if (el->el_chared.c_vcmd.pos == 0)
319: return;
320:
321:
322: if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
323: size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
324: c_delbefore(el, size);
325: el->el_line.cursor = el->el_chared.c_vcmd.pos;
326: re_refresh_cursor(el);
327: } else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
328: size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
329: c_delafter(el, size);
330: } else {
331: size = 1;
332: c_delafter(el, size);
333: }
334: switch (oaction) {
335: case DELETE|INSERT:
336: el->el_chared.c_undo.action = DELETE|INSERT;
337: break;
338: case DELETE:
339: el->el_chared.c_undo.action = INSERT;
340: break;
341: case NOP:
342: case INSERT:
343: default:
1.10 christos 344: EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction));
1.9 lukem 345: break;
346: }
1.7 simonb 347:
1.1 cgd 348:
1.9 lukem 349: el->el_chared.c_undo.ptr = el->el_line.cursor;
350: el->el_chared.c_undo.dsize = size;
1.1 cgd 351: }
352:
353:
354: #ifdef notdef
355: /* ce__endword():
356: * Go to the end of this word according to emacs
357: */
358: protected char *
1.9 lukem 359: ce__endword(char *p, char *high, int n)
360: {
361: p++;
1.1 cgd 362:
1.9 lukem 363: while (n--) {
364: while ((p < high) && isspace((unsigned char) *p))
365: p++;
366: while ((p < high) && !isspace((unsigned char) *p))
367: p++;
368: }
369:
370: p--;
371: return (p);
1.1 cgd 372: }
373: #endif
374:
375:
376: /* cv__endword():
377: * Go to the end of this word according to vi
378: */
379: protected char *
1.9 lukem 380: cv__endword(char *p, char *high, int n)
381: {
382: p++;
1.1 cgd 383:
1.9 lukem 384: while (n--) {
385: while ((p < high) && isspace((unsigned char) *p))
386: p++;
387:
388: if (isalnum((unsigned char) *p))
389: while ((p < high) && isalnum((unsigned char) *p))
390: p++;
391: else
392: while ((p < high) && !(isspace((unsigned char) *p) ||
393: isalnum((unsigned char) *p)))
394: p++;
395: }
396: p--;
397: return (p);
1.1 cgd 398: }
399:
400: /* ch_init():
401: * Initialize the character editor
402: */
403: protected int
1.9 lukem 404: ch_init(EditLine *el)
1.1 cgd 405: {
1.11 ! christos 406: el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
! 407: if (el->el_line.buffer == NULL)
! 408: return (-1);
! 409:
1.9 lukem 410: (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
411: el->el_line.cursor = el->el_line.buffer;
412: el->el_line.lastchar = el->el_line.buffer;
413: el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
414:
1.11 ! christos 415: el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
! 416: if (el->el_chared.c_undo.buf == NULL)
! 417: return (-1);
1.9 lukem 418: (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
419: el->el_chared.c_undo.action = NOP;
420: el->el_chared.c_undo.isize = 0;
421: el->el_chared.c_undo.dsize = 0;
422: el->el_chared.c_undo.ptr = el->el_line.buffer;
423:
424: el->el_chared.c_vcmd.action = NOP;
425: el->el_chared.c_vcmd.pos = el->el_line.buffer;
426: el->el_chared.c_vcmd.ins = el->el_line.buffer;
427:
428: el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
1.11 ! christos 429: if (el->el_chared.c_kill.buf == NULL)
! 430: return (-1);
1.9 lukem 431: (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
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:
435: el->el_map.current = el->el_map.key;
436:
437: el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
438: el->el_state.doingarg = 0;
439: el->el_state.metanext = 0;
440: el->el_state.argument = 1;
441: el->el_state.lastcmd = ED_UNASSIGNED;
442:
443: el->el_chared.c_macro.nline = NULL;
444: el->el_chared.c_macro.level = -1;
445: el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
1.11 ! christos 446: sizeof(char *));
! 447: if (el->el_chared.c_macro.macro == NULL)
! 448: return (-1);
1.9 lukem 449: return (0);
1.1 cgd 450: }
451:
452: /* ch_reset():
453: * Reset the character editor
454: */
455: protected void
1.9 lukem 456: ch_reset(EditLine *el)
1.1 cgd 457: {
1.9 lukem 458: el->el_line.cursor = el->el_line.buffer;
459: el->el_line.lastchar = el->el_line.buffer;
1.1 cgd 460:
1.9 lukem 461: el->el_chared.c_undo.action = NOP;
462: el->el_chared.c_undo.isize = 0;
463: el->el_chared.c_undo.dsize = 0;
464: el->el_chared.c_undo.ptr = el->el_line.buffer;
1.1 cgd 465:
1.9 lukem 466: el->el_chared.c_vcmd.action = NOP;
467: el->el_chared.c_vcmd.pos = el->el_line.buffer;
468: el->el_chared.c_vcmd.ins = el->el_line.buffer;
1.1 cgd 469:
1.9 lukem 470: el->el_chared.c_kill.mark = el->el_line.buffer;
1.1 cgd 471:
1.9 lukem 472: el->el_map.current = el->el_map.key;
1.1 cgd 473:
1.9 lukem 474: el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
475: el->el_state.doingarg = 0;
476: el->el_state.metanext = 0;
477: el->el_state.argument = 1;
478: el->el_state.lastcmd = ED_UNASSIGNED;
1.1 cgd 479:
1.9 lukem 480: el->el_chared.c_macro.level = -1;
1.1 cgd 481:
1.9 lukem 482: el->el_history.eventno = 0;
1.1 cgd 483: }
484:
485:
486: /* ch_end():
487: * Free the data structures used by the editor
488: */
489: protected void
1.9 lukem 490: ch_end(EditLine *el)
1.1 cgd 491: {
1.9 lukem 492: el_free((ptr_t) el->el_line.buffer);
493: el->el_line.buffer = NULL;
494: el->el_line.limit = NULL;
495: el_free((ptr_t) el->el_chared.c_undo.buf);
496: el->el_chared.c_undo.buf = NULL;
497: el_free((ptr_t) el->el_chared.c_kill.buf);
498: el->el_chared.c_kill.buf = NULL;
499: el_free((ptr_t) el->el_chared.c_macro.macro);
500: el->el_chared.c_macro.macro = NULL;
501: ch_reset(el);
1.1 cgd 502: }
503:
504:
505: /* el_insertstr():
506: * Insert string at cursorI
507: */
508: public int
1.9 lukem 509: el_insertstr(EditLine *el, const char *s)
510: {
511: int len;
512:
513: if ((len = strlen(s)) == 0)
514: return (-1);
515: if (el->el_line.lastchar + len >= el->el_line.limit)
516: return (-1);
517:
518: c_insert(el, len);
519: while (*s)
520: *el->el_line.cursor++ = *s++;
521: return (0);
1.1 cgd 522: }
523:
524:
525: /* el_deletestr():
526: * Delete num characters before the cursor
527: */
528: public void
1.9 lukem 529: el_deletestr(EditLine *el, int n)
530: {
531: if (n <= 0)
532: return;
533:
534: if (el->el_line.cursor < &el->el_line.buffer[n])
535: return;
536:
537: c_delbefore(el, n); /* delete before dot */
538: el->el_line.cursor -= n;
539: if (el->el_line.cursor < el->el_line.buffer)
540: el->el_line.cursor = el->el_line.buffer;
1.1 cgd 541: }
542:
543: /* c_gets():
544: * Get a string
545: */
546: protected int
1.9 lukem 547: c_gets(EditLine *el, char *buf)
548: {
549: char ch;
550: int len = 0;
1.1 cgd 551:
1.9 lukem 552: for (ch = 0; ch == 0;) {
553: if (el_getc(el, &ch) != 1)
554: return (ed_end_of_file(el, 0));
555: switch (ch) {
556: case 0010: /* Delete and backspace */
557: case 0177:
558: if (len > 1) {
559: *el->el_line.cursor-- = '\0';
560: el->el_line.lastchar = el->el_line.cursor;
561: buf[len--] = '\0';
562: } else {
563: el->el_line.buffer[0] = '\0';
564: el->el_line.lastchar = el->el_line.buffer;
565: el->el_line.cursor = el->el_line.buffer;
566: return (CC_REFRESH);
567: }
568: re_refresh(el);
569: ch = 0;
570: break;
571:
572: case 0033: /* ESC */
573: case '\r': /* Newline */
574: case '\n':
575: break;
576:
577: default:
578: if (len >= EL_BUFSIZ)
579: term_beep(el);
580: else {
581: buf[len++] = ch;
582: *el->el_line.cursor++ = ch;
583: el->el_line.lastchar = el->el_line.cursor;
584: }
585: re_refresh(el);
586: ch = 0;
587: break;
588: }
589: }
590: buf[len] = ch;
591: return (len);
1.1 cgd 592: }
593:
594:
595: /* c_hpos():
596: * Return the current horizontal position of the cursor
597: */
598: protected int
1.9 lukem 599: c_hpos(EditLine *el)
1.1 cgd 600: {
1.9 lukem 601: char *ptr;
1.1 cgd 602:
1.9 lukem 603: /*
604: * Find how many characters till the beginning of this line.
605: */
606: if (el->el_line.cursor == el->el_line.buffer)
607: return (0);
608: else {
609: for (ptr = el->el_line.cursor - 1;
610: ptr >= el->el_line.buffer && *ptr != '\n';
611: ptr--)
612: continue;
613: return (el->el_line.cursor - ptr - 1);
614: }
1.1 cgd 615: }
CVSweb <webmaster@jp.NetBSD.org>