[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.15

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

CVSweb <webmaster@jp.NetBSD.org>