root/src/noitedit/chared.c

Revision bafab25395cb398ddf5eae42145f0d0bfc00ec22, 15.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

import of libedit... prepare to hack

  • Property mode set to 100644
Line 
1 /*      $NetBSD: chared.c,v 1.14 2001/05/17 01:02:17 christos Exp $     */
2
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
39 #include "compat.h"
40 /*
41  * chared.c: Character editor utilities
42  */
43 #include "sys.h"
44
45 #include <stdlib.h>
46 #include "el.h"
47
48 /* value to leave unused in line buffer */
49 #define EL_LEAVE        2
50
51 /* cv_undo():
52  *      Handle state for the vi undo command
53  */
54 protected void
55 cv_undo(EditLine *el,int action, size_t size, char *ptr)
56 {
57         c_undo_t *vu = &el->el_chared.c_undo;
58         vu->action = action;
59         vu->ptr    = ptr;
60         vu->isize  = size;
61         (void) memcpy(vu->buf, vu->ptr, size);
62 #ifdef DEBUG_UNDO
63         (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
64                vu->ptr, vu->isize, vu->dsize);
65 #endif
66 }
67
68
69 /* c_insert():
70  *      Insert num characters
71  */
72 protected void
73 c_insert(EditLine *el, int num)
74 {
75         char *cp;
76
77         if (el->el_line.lastchar + num >= el->el_line.limit)
78                 return;                 /* can't go past end of buffer */
79
80         if (el->el_line.cursor < el->el_line.lastchar) {
81                 /* if I must move chars */
82                 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
83                         cp[num] = *cp;
84         }
85         el->el_line.lastchar += num;
86 }
87
88
89 /* c_delafter():
90  *      Delete num characters after the cursor
91  */
92 protected void
93 c_delafter(EditLine *el, int num)
94 {
95
96         if (el->el_line.cursor + num > el->el_line.lastchar)
97                 num = el->el_line.lastchar - el->el_line.cursor;
98
99         if (num > 0) {
100                 char *cp;
101
102                 if (el->el_map.current != el->el_map.emacs)
103                         cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
104
105                 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
106                         *cp = cp[num];
107
108                 el->el_line.lastchar -= num;
109         }
110 }
111
112
113 /* c_delbefore():
114  *      Delete num characters before the cursor
115  */
116 protected void
117 c_delbefore(EditLine *el, int num)
118 {
119
120         if (el->el_line.cursor - num < el->el_line.buffer)
121                 num = el->el_line.cursor - el->el_line.buffer;
122
123         if (num > 0) {
124                 char *cp;
125
126                 if (el->el_map.current != el->el_map.emacs)
127                         cv_undo(el, INSERT, (size_t)num,
128                             el->el_line.cursor - num);
129
130                 for (cp = el->el_line.cursor - num;
131                     cp <= el->el_line.lastchar;
132                     cp++)
133                         *cp = cp[num];
134
135                 el->el_line.lastchar -= num;
136         }
137 }
138
139
140 /* ce__isword():
141  *      Return if p is part of a word according to emacs
142  */
143 protected int
144 ce__isword(int p)
145 {
146         return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
147 }
148
149
150 /* cv__isword():
151  *      Return if p is part of a word according to vi
152  */
153 protected int
154 cv__isword(int p)
155 {
156         return (!isspace(p));
157 }
158
159
160 /* c__prev_word():
161  *      Find the previous word
162  */
163 protected char *
164 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
165 {
166         p--;
167
168         while (n--) {
169                 while ((p >= low) && !(*wtest)((unsigned char) *p))
170                         p--;
171                 while ((p >= low) && (*wtest)((unsigned char) *p))
172                         p--;
173         }
174
175         /* cp now points to one character before the word */
176         p++;
177         if (p < low)
178                 p = low;
179         /* cp now points where we want it */
180         return (p);
181 }
182
183
184 /* c__next_word():
185  *      Find the next word
186  */
187 protected char *
188 c__next_word(char *p, char *high, int n, int (*wtest)(int))
189 {
190         while (n--) {
191                 while ((p < high) && !(*wtest)((unsigned char) *p))
192                         p++;
193                 while ((p < high) && (*wtest)((unsigned char) *p))
194                         p++;
195         }
196         if (p > high)
197                 p = high;
198         /* p now points where we want it */
199         return (p);
200 }
201
202 /* cv_next_word():
203  *      Find the next word vi style
204  */
205 protected char *
206 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
207 {
208         int test;
209
210         while (n--) {
211                 test = (*wtest)((unsigned char) *p);
212                 while ((p < high) && (*wtest)((unsigned char) *p) == test)
213                         p++;
214                 /*
215                  * vi historically deletes with cw only the word preserving the
216                  * trailing whitespace! This is not what 'w' does..
217                  */
218                 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
219                         while ((p < high) && isspace((unsigned char) *p))
220                                 p++;
221         }
222
223         /* p now points where we want it */
224         if (p > high)
225                 return (high);
226         else
227                 return (p);
228 }
229
230
231 /* cv_prev_word():
232  *      Find the previous word vi style
233  */
234 protected char *
235 cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
236 {
237         int test;
238
239         while (n--) {
240                 p--;
241                 /*
242                  * vi historically deletes with cb only the word preserving the
243                  * leading whitespace! This is not what 'b' does..
244                  */
245                 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
246                         while ((p > low) && isspace((unsigned char) *p))
247                                 p--;
248                 test = (*wtest)((unsigned char) *p);
249                 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
250                         p--;
251                 p++;
252                 while (isspace((unsigned char) *p))
253                         p++;
254         }
255
256         /* p now points where we want it */
257         if (p < low)
258                 return (low);
259         else
260                 return (p);
261 }
262
263
264 #ifdef notdef
265 /* c__number():
266  *      Ignore character p points to, return number appearing after that.
267  *      A '$' by itself means a big number; "$-" is for negative; '^' means 1.
268  *      Return p pointing to last char used.
269  */
270 protected char *
271 c__number(
272     char *p,    /* character position */
273     int *num,   /* Return value */
274     int dval)   /* dval is the number to subtract from like $-3 */
275 {
276         int i;
277         int sign = 1;
278
279         if (*++p == '^') {
280                 *num = 1;
281                 return (p);
282         }
283         if (*p == '$') {
284                 if (*++p != '-') {
285                         *num = 0x7fffffff;      /* Handle $ */
286                         return (--p);
287                 }
288                 sign = -1;                      /* Handle $- */
289                 ++p;
290         }
291         for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
292                 continue;
293         *num = (sign < 0 ? dval - i : i);
294         return (--p);
295 }
296 #endif
297
298 /* cv_delfini():
299  *      Finish vi delete action
300  */
301 protected void
302 cv_delfini(EditLine *el)
303 {
304         int size;
305         int oaction;
306
307         if (el->el_chared.c_vcmd.action & INSERT)
308                 el->el_map.current = el->el_map.key;
309
310         oaction = el->el_chared.c_vcmd.action;
311         el->el_chared.c_vcmd.action = NOP;
312
313         if (el->el_chared.c_vcmd.pos == 0)
314                 return;
315
316
317         if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
318                 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
319                 c_delbefore(el, size);
320                 el->el_line.cursor = el->el_chared.c_vcmd.pos;
321                 re_refresh_cursor(el);
322         } else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
323                 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
324                 c_delafter(el, size);
325         } else {
326                 size = 1;
327                 c_delafter(el, size);
328         }
329         switch (oaction) {
330         case DELETE|INSERT:
331                 el->el_chared.c_undo.action = DELETE|INSERT;
332                 break;
333         case DELETE:
334                 el->el_chared.c_undo.action = INSERT;
335                 break;
336         case NOP:
337         case INSERT:
338         default:
339                 EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction));
340                 break;
341         }
342
343
344         el->el_chared.c_undo.ptr = el->el_line.cursor;
345         el->el_chared.c_undo.dsize = size;
346 }
347
348
349 #ifdef notdef
350 /* ce__endword():
351  *      Go to the end of this word according to emacs
352  */
353 protected char *
354 ce__endword(char *p, char *high, int n)
355 {
356         p++;
357
358         while (n--) {
359                 while ((p < high) && isspace((unsigned char) *p))
360                         p++;
361                 while ((p < high) && !isspace((unsigned char) *p))
362                         p++;
363         }
364
365         p--;
366         return (p);
367 }
368 #endif
369
370
371 /* cv__endword():
372  *      Go to the end of this word according to vi
373  */
374 protected char *
375 cv__endword(char *p, char *high, int n)
376 {
377         p++;
378
379         while (n--) {
380                 while ((p < high) && isspace((unsigned char) *p))
381                         p++;
382
383                 if (isalnum((unsigned char) *p))
384                         while ((p < high) && isalnum((unsigned char) *p))
385                                 p++;
386                 else
387                         while ((p < high) && !(isspace((unsigned char) *p) ||
388                             isalnum((unsigned char) *p)))
389                                 p++;
390         }
391         p--;
392         return (p);
393 }
394
395 /* ch_init():
396  *      Initialize the character editor
397  */
398 protected int
399 ch_init(EditLine *el)
400 {
401         el->el_line.buffer              = (char *) el_malloc(EL_BUFSIZ);
402         if (el->el_line.buffer == NULL)
403                 return (-1);
404
405         (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
406         el->el_line.cursor              = el->el_line.buffer;
407         el->el_line.lastchar            = el->el_line.buffer;
408         el->el_line.limit               = &el->el_line.buffer[EL_BUFSIZ - 2];
409
410         el->el_chared.c_undo.buf        = (char *) el_malloc(EL_BUFSIZ);
411         if (el->el_chared.c_undo.buf == NULL)
412                 return (-1);
413         (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
414         el->el_chared.c_undo.action     = NOP;
415         el->el_chared.c_undo.isize      = 0;
416         el->el_chared.c_undo.dsize      = 0;
417         el->el_chared.c_undo.ptr        = el->el_line.buffer;
418
419         el->el_chared.c_vcmd.action     = NOP;
420         el->el_chared.c_vcmd.pos        = el->el_line.buffer;
421         el->el_chared.c_vcmd.ins        = el->el_line.buffer;
422
423         el->el_chared.c_kill.buf        = (char *) el_malloc(EL_BUFSIZ);
424         if (el->el_chared.c_kill.buf == NULL)
425                 return (-1);
426         (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
427         el->el_chared.c_kill.mark       = el->el_line.buffer;
428         el->el_chared.c_kill.last       = el->el_chared.c_kill.buf;
429
430         el->el_map.current              = el->el_map.key;
431
432         el->el_state.inputmode          = MODE_INSERT; /* XXX: save a default */
433         el->el_state.doingarg           = 0;
434         el->el_state.metanext           = 0;
435         el->el_state.argument           = 1;
436         el->el_state.lastcmd            = ED_UNASSIGNED;
437
438         el->el_chared.c_macro.nline     = NULL;
439         el->el_chared.c_macro.level     = -1;
440         el->el_chared.c_macro.macro     = (char **) el_malloc(EL_MAXMACRO *
441             sizeof(char *));
442         if (el->el_chared.c_macro.macro == NULL)
443                 return (-1);
444         return (0);
445 }
446
447 /* ch_reset():
448  *      Reset the character editor
449  */
450 protected void
451 ch_reset(EditLine *el)
452 {
453         el->el_line.cursor              = el->el_line.buffer;
454         el->el_line.lastchar            = el->el_line.buffer;
455
456         el->el_chared.c_undo.action     = NOP;
457         el->el_chared.c_undo.isize      = 0;
458         el->el_chared.c_undo.dsize      = 0;
459         el->el_chared.c_undo.ptr        = el->el_line.buffer;
460
461         el->el_chared.c_vcmd.action     = NOP;
462         el->el_chared.c_vcmd.pos        = el->el_line.buffer;
463         el->el_chared.c_vcmd.ins        = el->el_line.buffer;
464
465         el->el_chared.c_kill.mark       = el->el_line.buffer;
466
467         el->el_map.current              = el->el_map.key;
468
469         el->el_state.inputmode          = MODE_INSERT; /* XXX: save a default */
470         el->el_state.doingarg           = 0;
471         el->el_state.metanext           = 0;
472         el->el_state.argument           = 1;
473         el->el_state.lastcmd            = ED_UNASSIGNED;
474
475         el->el_chared.c_macro.level     = -1;
476
477         el->el_history.eventno          = 0;
478 }
479
480 /* ch_enlargebufs():
481  *      Enlarge line buffer to be able to hold twice as much characters.
482  *      Returns 1 if successful, 0 if not.
483  */
484 protected int
485 ch_enlargebufs(el, addlen)
486         EditLine *el;
487         size_t addlen;
488 {
489         size_t sz, newsz;
490         char *newbuffer, *oldbuf, *oldkbuf;
491
492         sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
493         newsz = sz * 2;
494         /*
495          * If newly required length is longer than current buffer, we need
496          * to make the buffer big enough to hold both old and new stuff.
497          */
498         if (addlen > sz) {
499                 while(newsz - sz < addlen)
500                         newsz *= 2;
501         }
502
503         /*
504          * Reallocate line buffer.
505          */
506         newbuffer = el_realloc(el->el_line.buffer, newsz);
507         if (!newbuffer)
508                 return 0;
509
510         /* zero the newly added memory, leave old data in */
511         (void) memset(&newbuffer[sz], 0, newsz - sz);
512            
513         oldbuf = el->el_line.buffer;
514
515         el->el_line.buffer = newbuffer;
516         el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
517         el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
518         el->el_line.limit  = &newbuffer[newsz - EL_LEAVE];
519
520         /*
521          * Reallocate kill buffer.
522          */
523         newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
524         if (!newbuffer)
525                 return 0;
526
527         /* zero the newly added memory, leave old data in */
528         (void) memset(&newbuffer[sz], 0, newsz - sz);
529
530         oldkbuf = el->el_chared.c_kill.buf;
531
532         el->el_chared.c_kill.buf = newbuffer;
533         el->el_chared.c_kill.last = newbuffer +
534                                         (el->el_chared.c_kill.last - oldkbuf);
535         el->el_chared.c_kill.mark = el->el_line.buffer +
536                                         (el->el_chared.c_kill.mark - oldbuf);
537
538         /*
539          * Reallocate undo buffer.
540          */
541         newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
542         if (!newbuffer)
543                 return 0;
544
545         /* zero the newly added memory, leave old data in */
546         (void) memset(&newbuffer[sz], 0, newsz - sz);
547
548         el->el_chared.c_undo.ptr = el->el_line.buffer +
549                                     (el->el_chared.c_undo.ptr - oldbuf);
550         el->el_chared.c_undo.buf = newbuffer;
551        
552         if (!hist_enlargebuf(el, sz, newsz))
553                 return 0;
554
555         return 1;
556 }
557
558 /* ch_end():
559  *      Free the data structures used by the editor
560  */
561 protected void
562 ch_end(EditLine *el)
563 {
564         el_free((ptr_t) el->el_line.buffer);
565         el->el_line.buffer = NULL;
566         el->el_line.limit = NULL;
567         el_free((ptr_t) el->el_chared.c_undo.buf);
568         el->el_chared.c_undo.buf = NULL;
569         el_free((ptr_t) el->el_chared.c_kill.buf);
570         el->el_chared.c_kill.buf = NULL;
571         el_free((ptr_t) el->el_chared.c_macro.macro);
572         el->el_chared.c_macro.macro = NULL;
573         ch_reset(el);
574 }
575
576
577 /* el_insertstr():
578  *      Insert string at cursorI
579  */
580 public int
581 el_insertstr(EditLine *el, const char *s)
582 {
583         size_t len;
584
585         if ((len = strlen(s)) == 0)
586                 return (-1);
587         if (el->el_line.lastchar + len >= el->el_line.limit) {
588                 if (!ch_enlargebufs(el, len))
589                         return (-1);
590         }
591
592         c_insert(el, (int)len);
593         while (*s)
594                 *el->el_line.cursor++ = *s++;
595         return (0);
596 }
597
598
599 /* el_deletestr():
600  *      Delete num characters before the cursor
601  */
602 public void
603 el_deletestr(EditLine *el, int n)
604 {
605         if (n <= 0)
606                 return;
607
608         if (el->el_line.cursor < &el->el_line.buffer[n])
609                 return;
610
611         c_delbefore(el, n);             /* delete before dot */
612         el->el_line.cursor -= n;
613         if (el->el_line.cursor < el->el_line.buffer)
614                 el->el_line.cursor = el->el_line.buffer;
615 }
616
617 /* c_gets():
618  *      Get a string
619  */
620 protected int
621 c_gets(EditLine *el, char *buf)
622 {
623         char ch;
624         int len = 0;
625
626         for (ch = 0; ch == 0;) {
627                 if (el_getc(el, &ch) != 1)
628                         return (ed_end_of_file(el, 0));
629                 switch (ch) {
630                 case 0010:      /* Delete and backspace */
631                 case 0177:
632                         if (len > 1) {
633                                 *el->el_line.cursor-- = '\0';
634                                 el->el_line.lastchar = el->el_line.cursor;
635                                 buf[len--] = '\0';
636                         } else {
637                                 el->el_line.buffer[0] = '\0';
638                                 el->el_line.lastchar = el->el_line.buffer;
639                                 el->el_line.cursor = el->el_line.buffer;
640                                 return (CC_REFRESH);
641                         }
642                         re_refresh(el);
643                         ch = 0;
644                         break;
645
646                 case 0033:      /* ESC */
647                 case '\r':      /* Newline */
648                 case '\n':
649                         break;
650
651                 default:
652                         if (len >= EL_BUFSIZ)
653                                 term_beep(el);
654                         else {
655                                 buf[len++] = ch;
656                                 *el->el_line.cursor++ = ch;
657                                 el->el_line.lastchar = el->el_line.cursor;
658                         }
659                         re_refresh(el);
660                         ch = 0;
661                         break;
662                 }
663         }
664         buf[len] = ch;
665         return (len);
666 }
667
668
669 /* c_hpos():
670  *      Return the current horizontal position of the cursor
671  */
672 protected int
673 c_hpos(EditLine *el)
674 {
675         char *ptr;
676
677         /*
678          * Find how many characters till the beginning of this line.
679          */
680         if (el->el_line.cursor == el->el_line.buffer)
681                 return (0);
682         else {
683                 for (ptr = el->el_line.cursor - 1;
684                      ptr >= el->el_line.buffer && *ptr != '\n';
685                      ptr--)
686                         continue;
687                 return (el->el_line.cursor - ptr - 1);
688         }
689 }
Note: See TracBrowser for help on using the browser.