[BACK]Return to chared.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libedit

Annotation of src/lib/libedit/chared.c, Revision 1.12

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

CVSweb <webmaster@jp.NetBSD.org>