[BACK]Return to mark.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / vim

Annotation of src/usr.bin/vim/mark.c, Revision 1.1.1.1

1.1       downsj      1: /* $OpenBSD$   */
                      2: /* vi:set ts=4 sw=4:
                      3:  *
                      4:  * VIM - Vi IMproved       by Bram Moolenaar
                      5:  *
                      6:  * Do ":help uganda"  in Vim to read copying and usage conditions.
                      7:  * Do ":help credits" in Vim to see a list of people who contributed.
                      8:  */
                      9:
                     10: /*
                     11:  * mark.c: functions for setting marks and jumping to them
                     12:  */
                     13:
                     14: #include "vim.h"
                     15: #include "globals.h"
                     16: #include "proto.h"
                     17: #include "option.h"
                     18:
                     19: /*
                     20:  * This file contains routines to maintain and manipulate marks.
                     21:  */
                     22:
                     23: /*
                     24:  * If a named file mark's lnum is non-zero, it is valid.
                     25:  * If a named file mark's fnum is non-zero, it is for an existing buffer,
                     26:  * otherwise it is from .viminfo and namedfm_names[n] is the file name.
                     27:  * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
                     28:  * viminfo).
                     29:  */
                     30: #define EXTRA_MARKS    10                                  /* marks 0-9 */
                     31: static struct filemark namedfm[NMARKS + EXTRA_MARKS];  /* marks with file nr */
                     32: static char_u *namedfm_names[NMARKS + EXTRA_MARKS];        /* name for namedfm[] */
                     33:
                     34: static void show_one_mark __ARGS((int, char_u *, FPOS *, char_u *));
                     35: static void cleanup_jumplist __ARGS((void));
                     36: #ifdef VIMINFO
                     37: static int removable __ARGS((char_u *name));
                     38: #endif
                     39:
                     40: /*
                     41:  * setmark(c) - set named mark 'c' at current cursor position
                     42:  *
                     43:  * Returns OK on success, FAIL if no room for mark or bad name given.
                     44:  */
                     45:    int
                     46: setmark(c)
                     47:    int         c;
                     48: {
                     49:    int         i;
                     50:
                     51:    if (c > 'z')            /* some islower() and isupper() cannot handle
                     52:                                characters above 127 */
                     53:        return FAIL;
                     54:    if (islower(c))
                     55:    {
                     56:        i = c - 'a';
                     57:        curbuf->b_namedm[i] = curwin->w_cursor;
                     58:        return OK;
                     59:    }
                     60:    if (isupper(c))
                     61:    {
                     62:        i = c - 'A';
                     63:        namedfm[i].mark = curwin->w_cursor;
                     64:        namedfm[i].fnum = curbuf->b_fnum;
                     65:        return OK;
                     66:    }
                     67:    return FAIL;
                     68: }
                     69:
                     70: /*
                     71:  * setpcmark() - set the previous context mark to the current position
                     72:  *              and add it to the jump list
                     73:  */
                     74:    void
                     75: setpcmark()
                     76: {
                     77:    int i;
                     78: #ifdef ROTATE
                     79:    struct filemark tempmark;
                     80: #endif
                     81:
                     82:    /* for :global the mark is set only once */
                     83:    if (global_busy)
                     84:        return;
                     85:
                     86:    curwin->w_prev_pcmark = curwin->w_pcmark;
                     87:    curwin->w_pcmark = curwin->w_cursor;
                     88:
                     89: #ifdef ROTATE
                     90:    /*
                     91:     * If last used entry is not at the top, put it at the top by rotating
                     92:     * the stack until it is (the newer entries will be at the bottom).
                     93:     * Keep one entry (the last used one) at the top.
                     94:     */
                     95:    if (curwin->w_jumplistidx < curwin->w_jumplistlen)
                     96:        ++curwin->w_jumplistidx;
                     97:    while (curwin->w_jumplistidx < curwin->w_jumplistlen)
                     98:    {
                     99:        tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
                    100:        for (i = curwin->w_jumplistlen - 1; i > 0; --i)
                    101:            curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
                    102:        curwin->w_jumplist[0] = tempmark;
                    103:        ++curwin->w_jumplistidx;
                    104:    }
                    105: #endif
                    106:
                    107:    /* If jumplist is full: remove oldest entry */
                    108:    if (++curwin->w_jumplistlen > JUMPLISTSIZE)
                    109:    {
                    110:        curwin->w_jumplistlen = JUMPLISTSIZE;
                    111:        for (i = 1; i < JUMPLISTSIZE; ++i)
                    112:            curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
                    113:    }
                    114:    curwin->w_jumplistidx = curwin->w_jumplistlen - 1;
                    115:
                    116: #ifdef ARCHIE
                    117:    /* Workaround for a bug in gcc 2.4.5 R2 on the Archimedes
                    118:     * Should be fixed in 2.5.x.
                    119:     */
                    120:    curwin->w_jumplist[curwin->w_jumplistidx].mark.ptr = curwin->w_pcmark.ptr;
                    121:    curwin->w_jumplist[curwin->w_jumplistidx].mark.col = curwin->w_pcmark.col;
                    122: #else
                    123:    curwin->w_jumplist[curwin->w_jumplistidx].mark = curwin->w_pcmark;
                    124: #endif
                    125:    curwin->w_jumplist[curwin->w_jumplistidx].fnum = curbuf->b_fnum;
                    126:    ++curwin->w_jumplistidx;
                    127:
                    128:    /* remove any duplicates, from the new entry or from previous deletes */
                    129:    cleanup_jumplist();
                    130: }
                    131:
                    132: /*
                    133:  * checkpcmark() - To change context, call setpcmark(), then move the current
                    134:  *                position to where ever, then call checkpcmark().  This
                    135:  *                ensures that the previous context will only be changed if
                    136:  *                the cursor moved to a different line. -- webb.
                    137:  *                If pcmark was deleted (with "dG") the previous mark is
                    138:  *                restored.
                    139:  */
                    140:    void
                    141: checkpcmark()
                    142: {
                    143:    if (curwin->w_prev_pcmark.lnum != 0 &&
                    144:            (equal(curwin->w_pcmark, curwin->w_cursor) ||
                    145:            curwin->w_pcmark.lnum == 0))
                    146:    {
                    147:        curwin->w_pcmark = curwin->w_prev_pcmark;
                    148:        curwin->w_prev_pcmark.lnum = 0;         /* Show it has been checked */
                    149:    }
                    150: }
                    151:
                    152: /*
                    153:  * move "count" positions in the jump list (count may be negative)
                    154:  */
                    155:    FPOS *
                    156: movemark(count)
                    157:    int count;
                    158: {
                    159:    FPOS        *pos;
                    160:
                    161:    cleanup_jumplist();
                    162:
                    163:    if (curwin->w_jumplistlen == 0)         /* nothing to jump to */
                    164:        return (FPOS *)NULL;
                    165:
                    166:    if (curwin->w_jumplistidx + count < 0 ||
                    167:                        curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
                    168:        return (FPOS *)NULL;
                    169:
                    170:    /*
                    171:     * if first CTRL-O or CTRL-I command after a jump, add cursor position to
                    172:     * list.  Careful: If there are duplicates (CTRL-O immidiately after
                    173:     * starting Vim on a file), another entry may have been removed.
                    174:     */
                    175:    if (curwin->w_jumplistidx == curwin->w_jumplistlen)
                    176:    {
                    177:        setpcmark();
                    178:        --curwin->w_jumplistidx;        /* skip the new entry */
                    179:        if (curwin->w_jumplistidx + count < 0)
                    180:            return (FPOS *)NULL;
                    181:    }
                    182:
                    183:    curwin->w_jumplistidx += count;
                    184:                                                /* jump to other file */
                    185:    if (curwin->w_jumplist[curwin->w_jumplistidx].fnum != curbuf->b_fnum)
                    186:    {
                    187:        if (buflist_getfile(curwin->w_jumplist[curwin->w_jumplistidx].fnum,
                    188:                          curwin->w_jumplist[curwin->w_jumplistidx].mark.lnum,
                    189:                                                               0) == FAIL)
                    190:            return (FPOS *)NULL;
                    191:        curwin->w_cursor.col =
                    192:                           curwin->w_jumplist[curwin->w_jumplistidx].mark.col;
                    193:        pos = (FPOS *)-1;
                    194:    }
                    195:    else
                    196:        pos = &(curwin->w_jumplist[curwin->w_jumplistidx].mark);
                    197:    return pos;
                    198: }
                    199:
                    200: /*
                    201:  * getmark(c) - find mark for char 'c'
                    202:  *
                    203:  * Return pointer to FPOS if found (caller needs to check lnum!)
                    204:  *        NULL if there is no mark called 'c'.
                    205:  *        -1 if mark is in other file (only if changefile is TRUE)
                    206:  */
                    207:    FPOS *
                    208: getmark(c, changefile)
                    209:    int         c;
                    210:    int         changefile;
                    211: {
                    212:    FPOS            *posp;
                    213:    FPOS            *startp, *endp;
                    214:    static  FPOS    pos_copy;
                    215:    int             len;
                    216:    char_u          *p;
                    217:
                    218:    posp = NULL;
                    219:    if (c > '~')                        /* check for islower()/isupper() */
                    220:        ;
                    221:    else if (c == '\'' || c == '`')     /* previous context mark */
                    222:    {
                    223:        pos_copy = curwin->w_pcmark;    /* need to make a copy because */
                    224:        posp = &pos_copy;               /*   w_pcmark may be changed soon */
                    225:    }
                    226:    else if (c == '"')                  /* to pos when leaving buffer */
                    227:        posp = &(curbuf->b_last_cursor);
                    228:    else if (c == '[')                  /* to start of previous operator */
                    229:        posp = &(curbuf->b_op_start);
                    230:    else if (c == ']')                  /* to end of previous operator */
                    231:        posp = &(curbuf->b_op_end);
                    232:    else if (c == '<' || c == '>')      /* start/end of visual area */
                    233:    {
                    234:        if (VIsual_active)
                    235:            startp = &VIsual_save;
                    236:        else
                    237:            startp = &VIsual;
                    238:        endp = &VIsual_end;
                    239:        if ((c == '<') == lt(*startp, *endp))
                    240:            posp = startp;
                    241:        else
                    242:            posp = endp;
                    243:    }
                    244:    else if (islower(c))                /* normal named mark */
                    245:        posp = &(curbuf->b_namedm[c - 'a']);
                    246:    else if (isupper(c) || isdigit(c))  /* named file mark */
                    247:    {
                    248:        if (isdigit(c))
                    249:            c = c - '0' + NMARKS;
                    250:        else
                    251:            c -= 'A';
                    252:        posp = &(namedfm[c].mark);
                    253:
                    254:        if (namedfm[c].fnum == 0 && namedfm_names[c] != NULL)
                    255:        {
                    256:            /*
                    257:             * First expand "~/" in the file name to the home directory.
                    258:             * Try to find the shortname by comparing the fullname with the
                    259:             * current directory.
                    260:             */
                    261:            expand_env(namedfm_names[c], NameBuff, MAXPATHL);
                    262:            mch_dirname(IObuff, IOSIZE);
                    263:            len = STRLEN(IObuff);
                    264:            if (fnamencmp(IObuff, NameBuff, len) == 0)
                    265:            {
                    266:                p = NameBuff + len;
                    267:                if (ispathsep(*p))
                    268:                    ++p;
                    269:            }
                    270:            else
                    271:                p = NULL;
                    272:                                /* buflist_new will call fmarks_check_names() */
                    273:            (void)buflist_new(NameBuff, p, (linenr_t)1, FALSE);
                    274:        }
                    275:
                    276:        if (namedfm[c].fnum != curbuf->b_fnum)      /* mark is in other file */
                    277:        {
                    278:            if (namedfm[c].mark.lnum != 0 && changefile && namedfm[c].fnum)
                    279:            {
                    280:                if (buflist_getfile(namedfm[c].fnum,
                    281:                                    namedfm[c].mark.lnum, GETF_SETMARK) == OK)
                    282:                {
                    283:                    curwin->w_cursor.col = namedfm[c].mark.col;
                    284:                    return (FPOS *)-1;
                    285:                }
                    286:            }
                    287:            posp = &pos_copy;           /* mark exists, but is not valid in
                    288:                                            current buffer */
                    289:            pos_copy.lnum = 0;
                    290:        }
                    291:    }
                    292:    return posp;
                    293: }
                    294:
                    295: /*
                    296:  * Check all file marks for a name that matches the file name in buf.
                    297:  * May replace the name with an fnum.
                    298:  */
                    299:    void
                    300: fmarks_check_names(buf)
                    301:    BUF     *buf;
                    302: {
                    303:    char_u      *name;
                    304:    int         i;
                    305:
                    306:    if (buf->b_filename == NULL)
                    307:        return;
                    308:
                    309:    name = home_replace_save(buf, buf->b_filename);
                    310:    if (name == NULL)
                    311:        return;
                    312:
                    313:    for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
                    314:    {
                    315:        if (namedfm[i].fnum == 0 && namedfm_names[i] != NULL &&
                    316:                                        fnamecmp(name, namedfm_names[i]) == 0)
                    317:        {
                    318:            namedfm[i].fnum = buf->b_fnum;
                    319:            vim_free(namedfm_names[i]);
                    320:            namedfm_names[i] = NULL;
                    321:        }
                    322:    }
                    323:    vim_free(name);
                    324: }
                    325:
                    326: /*
                    327:  * Check a if a position from a mark is valid.
                    328:  * Give and error message and return FAIL if not.
                    329:  */
                    330:    int
                    331: check_mark(pos)
                    332:    FPOS    *pos;
                    333: {
                    334:    if (pos == NULL)
                    335:    {
                    336:        emsg(e_umark);
                    337:        return FAIL;
                    338:    }
                    339:    if (pos->lnum == 0)
                    340:    {
                    341:        emsg(e_marknotset);
                    342:        return FAIL;
                    343:    }
                    344:    if (pos->lnum > curbuf->b_ml.ml_line_count)
                    345:    {
                    346:        emsg(e_markinval);
                    347:        return FAIL;
                    348:    }
                    349:    return OK;
                    350: }
                    351:
                    352: /*
                    353:  * clrallmarks() - clear all marks in the buffer 'buf'
                    354:  *
                    355:  * Used mainly when trashing the entire buffer during ":e" type commands
                    356:  */
                    357:    void
                    358: clrallmarks(buf)
                    359:    BUF     *buf;
                    360: {
                    361:    static int          i = -1;
                    362:
                    363:    if (i == -1)        /* first call ever: initialize */
                    364:        for (i = 0; i < NMARKS + 1; i++)
                    365:        {
                    366:            namedfm[i].mark.lnum = 0;
                    367:            namedfm_names[i] = NULL;
                    368:        }
                    369:
                    370:    for (i = 0; i < NMARKS; i++)
                    371:        buf->b_namedm[i].lnum = 0;
                    372:    buf->b_op_start.lnum = 0;       /* start/end op mark cleared */
                    373:    buf->b_op_end.lnum = 0;
                    374: }
                    375:
                    376: /*
                    377:  * get name of file from a filemark
                    378:  * Careful: buflist_nr2name returns NameBuff.
                    379:  */
                    380:    char_u *
                    381: fm_getname(fmark)
                    382:    struct filemark *fmark;
                    383: {
                    384:    if (fmark->fnum != curbuf->b_fnum)              /* not current file */
                    385:        return buflist_nr2name(fmark->fnum, FALSE, TRUE);
                    386:    return (char_u *)"-current-";
                    387: }
                    388:
                    389: /*
                    390:  * print the marks
                    391:  */
                    392:    void
                    393: do_marks(arg)
                    394:    char_u      *arg;
                    395: {
                    396:    int         i;
                    397:    char_u      *name;
                    398:
                    399:    if (arg != NULL && *arg == NUL)
                    400:        arg = NULL;
                    401:
                    402:    show_one_mark('\'', arg, &curwin->w_pcmark, NULL);
                    403:    for (i = 0; i < NMARKS; ++i)
                    404:        show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL);
                    405:    for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
                    406:    {
                    407:        name = namedfm[i].fnum == 0 ? namedfm_names[i] :
                    408:                                                      fm_getname(&namedfm[i]);
                    409:        if (name != NULL)
                    410:            show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
                    411:                                                 arg, &namedfm[i].mark, name);
                    412:    }
                    413:    show_one_mark('"', arg, &curbuf->b_last_cursor, NULL);
                    414:    show_one_mark('[', arg, &curbuf->b_op_start, NULL);
                    415:    show_one_mark(']', arg, &curbuf->b_op_end, NULL);
                    416:    show_one_mark('<', arg, &VIsual, NULL);
                    417:    show_one_mark('>', arg, &VIsual_end, NULL);
                    418:    show_one_mark(-1, arg, NULL, NULL);
                    419: }
                    420:
                    421:    static void
                    422: show_one_mark(c, arg, p, name)
                    423:    int     c;
                    424:    char_u  *arg;
                    425:    FPOS    *p;
                    426:    char_u  *name;
                    427: {
                    428:    static int      did_title = FALSE;
                    429:
                    430:    if (c == -1)                            /* finish up */
                    431:    {
                    432:        if (did_title)
                    433:            did_title = FALSE;
                    434:        else
                    435:        {
                    436:            if (arg == NULL)
                    437:                MSG("No marks set");
                    438:            else
                    439:                EMSG2("No marks matching \"%s\"", arg);
                    440:        }
                    441:    }
                    442:    /* don't output anything if 'q' typed at --more-- prompt */
                    443:    else if (!got_int && (arg == NULL || vim_strchr(arg, c) != NULL) &&
                    444:                                                                 p->lnum != 0)
                    445:    {
                    446:        if (!did_title)
                    447:        {
                    448:            set_highlight('t');             /* Highlight title */
                    449:            start_highlight();
                    450:            MSG_OUTSTR("\nmark line  col file");
                    451:            stop_highlight();
                    452:            did_title = TRUE;
                    453:        }
                    454:        msg_outchar('\n');
                    455:        if (!got_int)
                    456:        {
                    457:            sprintf((char *)IObuff, " %c %5ld  %3d  ", c, p->lnum, p->col);
                    458:            if (name != NULL)
                    459:                STRCAT(IObuff, name);
                    460:            msg_outtrans(IObuff);
                    461:        }
                    462:        flushbuf();                 /* show one line at a time */
                    463:    }
                    464: }
                    465:
                    466: /*
                    467:  * print the jumplist
                    468:  */
                    469:    void
                    470: do_jumps()
                    471: {
                    472:    int         i;
                    473:    char_u      *name;
                    474:
                    475:    cleanup_jumplist();
                    476:    set_highlight('t');     /* Highlight title */
                    477:    start_highlight();
                    478:    MSG_OUTSTR("\n jump line  file");
                    479:    stop_highlight();
                    480:    for (i = 0; i < curwin->w_jumplistlen; ++i)
                    481:    {
                    482:        if (curwin->w_jumplist[i].mark.lnum != 0)
                    483:        {
                    484:            name = fm_getname(&curwin->w_jumplist[i]);
                    485:            if (name == NULL)       /* file name not available */
                    486:                continue;
                    487:
                    488:            msg_outchar('\n');
                    489:            sprintf((char *)IObuff, "%c %2d %5ld  %s",
                    490:                i == curwin->w_jumplistidx ? '>' : ' ',
                    491:                i + 1,
                    492:                curwin->w_jumplist[i].mark.lnum,
                    493:                name);
                    494:            msg_outtrans(IObuff);
                    495:        }
                    496:        flushbuf();
                    497:    }
                    498:    if (curwin->w_jumplistidx == curwin->w_jumplistlen)
                    499:        MSG_OUTSTR("\n>");
                    500: }
                    501:
                    502: /*
                    503:  * adjust marks between line1 and line2 (inclusive) to move 'amount' lines
                    504:  * If 'amount' is MAXLNUM the mark is made invalid.
                    505:  * If 'amount_after' is non-zero adjust marks after line 2.
                    506:  */
                    507:
                    508: #define one_adjust(add) \
                    509:    { \
                    510:        lp = add; \
                    511:        if (*lp >= line1 && *lp <= line2) \
                    512:        { \
                    513:            if (amount == MAXLNUM) \
                    514:                *lp = 0; \
                    515:            else \
                    516:                *lp += amount; \
                    517:        } \
                    518:        else if (amount_after && *lp > line2) \
                    519:            *lp += amount_after; \
                    520:    }
                    521:
                    522: /* don't delete the line, just put at first deleted line */
                    523: #define one_adjust_nodel(add) \
                    524:    { \
                    525:        lp = add; \
                    526:        if (*lp >= line1 && *lp <= line2) \
                    527:        { \
                    528:            if (amount == MAXLNUM) \
                    529:                *lp = line1; \
                    530:            else \
                    531:                *lp += amount; \
                    532:        } \
                    533:        else if (amount_after && *lp > line2) \
                    534:            *lp += amount_after; \
                    535:    }
                    536:
                    537:    void
                    538: mark_adjust(line1, line2, amount, amount_after)
                    539:    linenr_t    line1;
                    540:    linenr_t    line2;
                    541:    long        amount;
                    542:    long        amount_after;
                    543: {
                    544:    int         i;
                    545:    int         fnum = curbuf->b_fnum;
                    546:    linenr_t    *lp;
                    547:    WIN         *win;
                    548:
                    549:    if (line2 < line1 && amount_after == 0L)        /* nothing to do */
                    550:        return;
                    551:
                    552: /* named marks, lower case and upper case */
                    553:    for (i = 0; i < NMARKS; i++)
                    554:    {
                    555:        one_adjust(&(curbuf->b_namedm[i].lnum));
                    556:        if (namedfm[i].fnum == fnum)
                    557:            one_adjust(&(namedfm[i].mark.lnum));
                    558:    }
                    559:    for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
                    560:    {
                    561:        if (namedfm[i].fnum == fnum)
                    562:            one_adjust(&(namedfm[i].mark.lnum));
                    563:    }
                    564:
                    565: /* previous context mark */
                    566:    one_adjust(&(curwin->w_pcmark.lnum));
                    567:
                    568: /* previous pcmark */
                    569:    one_adjust(&(curwin->w_prev_pcmark.lnum));
                    570:
                    571: /* Visual area */
                    572:    one_adjust_nodel(&(VIsual.lnum));
                    573:    one_adjust_nodel(&(VIsual_end.lnum));
                    574:
                    575: /* marks in the tag stack */
                    576:    for (i = 0; i < curwin->w_tagstacklen; i++)
                    577:        if (curwin->w_tagstack[i].fmark.fnum == fnum)
                    578:            one_adjust_nodel(&(curwin->w_tagstack[i].fmark.mark.lnum));
                    579:
                    580: /* quickfix marks */
                    581:    qf_mark_adjust(line1, line2, amount, amount_after);
                    582:
                    583: /* jumplist marks */
                    584:    for (win = firstwin; win != NULL; win = win->w_next)
                    585:    {
                    586:        /*
                    587:         * When deleting lines, this may create duplicate marks in the
                    588:         * jumplist. They will be removed later.
                    589:         */
                    590:        for (i = 0; i < win->w_jumplistlen; ++i)
                    591:            if (win->w_jumplist[i].fnum == fnum)
                    592:                one_adjust_nodel(&(win->w_jumplist[i].mark.lnum));
                    593:        /*
                    594:         * also adjust the line at the top of the window and the cursor
                    595:         * position for windows with the same buffer.
                    596:         */
                    597:        if (win != curwin && win->w_buffer == curbuf)
                    598:        {
                    599:            if (win->w_topline >= line1 && win->w_topline <= line2)
                    600:            {
                    601:                if (amount == MAXLNUM)      /* topline is deleted */
                    602:                {
                    603:                    if (line1 <= 1)
                    604:                        win->w_topline = 1;
                    605:                    else
                    606:                        win->w_topline = line1 - 1;
                    607:                }
                    608:                else                    /* keep topline on the same line */
                    609:                    win->w_topline += amount;
                    610:            }
                    611:            else if (amount_after && win->w_topline > line2)
                    612:                win->w_topline += amount_after;
                    613:            if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
                    614:            {
                    615:                if (amount == MAXLNUM)      /* line with cursor is deleted */
                    616:                {
                    617:                    if (line1 <= 1)
                    618:                        win->w_cursor.lnum = 1;
                    619:                    else
                    620:                        win->w_cursor.lnum = line1 - 1;
                    621:                    win->w_cursor.col = 0;
                    622:                }
                    623:                else                    /* keep cursor on the same line */
                    624:                    win->w_cursor.lnum += amount;
                    625:            }
                    626:            else if (amount_after && win->w_cursor.lnum > line2)
                    627:                win->w_cursor.lnum += amount_after;
                    628:        }
                    629:    }
                    630: }
                    631:
                    632: /*
                    633:  * When deleting lines, this may create duplicate marks in the
                    634:  * jumplist. They will be removed here for the current window.
                    635:  */
                    636:    static void
                    637: cleanup_jumplist()
                    638: {
                    639:    int     i;
                    640:    int     from, to;
                    641:
                    642:    to = 0;
                    643:    for (from = 0; from < curwin->w_jumplistlen; ++from)
                    644:    {
                    645:        if (curwin->w_jumplistidx == from)
                    646:            curwin->w_jumplistidx = to;
                    647:        for (i = from + 1; i < curwin->w_jumplistlen; ++i)
                    648:            if (curwin->w_jumplist[i].fnum == curwin->w_jumplist[from].fnum &&
                    649:                curwin->w_jumplist[i].mark.lnum ==
                    650:                                           curwin->w_jumplist[from].mark.lnum)
                    651:                break;
                    652:        if (i >= curwin->w_jumplistlen)     /* no duplicate */
                    653:            curwin->w_jumplist[to++] = curwin->w_jumplist[from];
                    654:    }
                    655:    if (curwin->w_jumplistidx == curwin->w_jumplistlen)
                    656:        curwin->w_jumplistidx = to;
                    657:    curwin->w_jumplistlen = to;
                    658: }
                    659:
                    660:    void
                    661: set_last_cursor(win)
                    662:    WIN     *win;
                    663: {
                    664:    win->w_buffer->b_last_cursor = win->w_cursor;
                    665: }
                    666:
                    667: #ifdef VIMINFO
                    668:    int
                    669: read_viminfo_filemark(line, fp, force)
                    670:    char_u  *line;
                    671:    FILE    *fp;
                    672:    int     force;
                    673: {
                    674:    int     idx;
                    675:    char_u  *str;
                    676:
                    677:    /* We only get here (hopefully) if line[0] == '\'' */
                    678:    str = line + 1;
                    679:    if (*str > 127 || (!isdigit(*str) && !isupper(*str)))
                    680:        EMSG2("viminfo: Illegal file mark name in line %s", line);
                    681:    else
                    682:    {
                    683:        if (isdigit(*str))
                    684:            idx = *str - '0' + NMARKS;
                    685:        else
                    686:            idx = *str - 'A';
                    687:        if (namedfm[idx].mark.lnum == 0 || force)
                    688:        {
                    689:            str = skipwhite(str + 1);
                    690:            namedfm[idx].mark.lnum = getdigits(&str);
                    691:            str = skipwhite(str);
                    692:            namedfm[idx].mark.col = getdigits(&str);
                    693:            str = skipwhite(str);
                    694:            viminfo_readstring(line);
                    695:            namedfm_names[idx] = strsave(str);
                    696:        }
                    697:    }
                    698:    return vim_fgets(line, LSIZE, fp);
                    699: }
                    700:
                    701:    void
                    702: write_viminfo_filemarks(fp)
                    703:    FILE    *fp;
                    704: {
                    705:    int     i;
                    706:    char_u  *name;
                    707:
                    708:    if (get_viminfo_parameter('\'') == 0)
                    709:        return;
                    710:
                    711:    fprintf(fp, "\n# File marks:\n");
                    712:
                    713:    /*
                    714:     * Find a mark that is the same file and position as the cursor.
                    715:     * That one, or else the last one is deleted.
                    716:     * Move '0 to '1, '1 to '2, etc. until the matching one or '9
                    717:     * Set '0 mark to current cursor position.
                    718:     */
                    719:    if (curbuf->b_filename != NULL && !removable(curbuf->b_filename))
                    720:    {
                    721:        name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
                    722:        for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
                    723:            if (namedfm[i].mark.lnum == curwin->w_cursor.lnum &&
                    724:                            (namedfm_names[i] == NULL ?
                    725:                                           namedfm[i].fnum == curbuf->b_fnum :
                    726:                                          STRCMP(name, namedfm_names[i]) == 0))
                    727:                break;
                    728:
                    729:        vim_free(namedfm_names[i]);
                    730:        for ( ; i > NMARKS; --i)
                    731:        {
                    732:            namedfm[i] = namedfm[i - 1];
                    733:            namedfm_names[i] = namedfm_names[i - 1];
                    734:        }
                    735:        namedfm[NMARKS].mark = curwin->w_cursor;
                    736:        namedfm[NMARKS].fnum = curbuf->b_fnum;
                    737:        namedfm_names[NMARKS] = NULL;
                    738:    }
                    739:
                    740:    for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
                    741:    {
                    742:        if (namedfm[i].mark.lnum == 0)          /* not set */
                    743:            continue;
                    744:
                    745:        if (namedfm[i].fnum)                    /* there is a buffer */
                    746:            name = buflist_nr2name(namedfm[i].fnum, TRUE, FALSE);
                    747:        else
                    748:            name = namedfm_names[i];            /* use name from .viminfo */
                    749:        if (name == NULL)
                    750:            continue;
                    751:
                    752:        fprintf(fp, "'%c  %ld  %ld  %s\n",
                    753:                    i < NMARKS ? i + 'A' : i - NMARKS + '0',
                    754:                    (long)namedfm[i].mark.lnum,
                    755:                    (long)namedfm[i].mark.col,
                    756:                    name);
                    757:    }
                    758: }
                    759:
                    760: /*
                    761:  * Return TRUE if "name" is on removable media (depending on 'viminfo').
                    762:  */
                    763:    static int
                    764: removable(name)
                    765:    char_u  *name;
                    766: {
                    767:    char_u  *p;
                    768:    char_u  part[51];
                    769:    int     retval = FALSE;
                    770:
                    771:    name = home_replace_save(NULL, name);
                    772:    if (name != NULL)
                    773:    {
                    774:        for (p = p_viminfo; *p; )
                    775:        {
                    776:            copy_option_part(&p, part, 51, ", ");
                    777:            if (part[0] == 'r' && vim_strnicmp(part + 1, name,
                    778:                    (size_t)STRLEN(part + 1)) == 0)
                    779:            {
                    780:                retval = TRUE;
                    781:                break;
                    782:            }
                    783:        }
                    784:        vim_free(name);
                    785:    }
                    786:    return retval;
                    787: }
                    788:
                    789: /*
                    790:  * Write all the named marks for all buffers.
                    791:  * Return the number of buffers for which marks have been written.
                    792:  */
                    793:    int
                    794: write_viminfo_marks(fp_out)
                    795:    FILE    *fp_out;
                    796: {
                    797:    int     count;
                    798:    BUF     *buf;
                    799:    WIN     *win;
                    800:    int     is_mark_set;
                    801:    int     i;
                    802:
                    803:    /*
                    804:     * Set b_last_cursor for the all buffers that have a window.
                    805:     */
                    806:    for (win = firstwin; win != NULL; win = win->w_next)
                    807:        set_last_cursor(win);
                    808:
                    809:    fprintf(fp_out, "\n# History of marks within files (newest to oldest):\n");
                    810:    count = 0;
                    811:    for (buf = firstbuf; buf != NULL; buf = buf->b_next)
                    812:    {
                    813:        /*
                    814:         * Only write something if buffer has been loaded and at least one
                    815:         * mark is set.
                    816:         */
                    817:        if (buf->b_marks_read)
                    818:        {
                    819:            if (buf->b_last_cursor.lnum != 0)
                    820:                is_mark_set = TRUE;
                    821:            else
                    822:            {
                    823:                is_mark_set = FALSE;
                    824:                for (i = 0; i < NMARKS; i++)
                    825:                    if (buf->b_namedm[i].lnum != 0)
                    826:                    {
                    827:                        is_mark_set = TRUE;
                    828:                        break;
                    829:                    }
                    830:            }
                    831:            if (is_mark_set && buf->b_filename != NULL &&
                    832:                 buf->b_filename[0] != NUL && !removable(buf->b_filename))
                    833:            {
                    834:                home_replace(NULL, buf->b_filename, IObuff, IOSIZE);
                    835:                fprintf(fp_out, "\n> %s\n", (char *)IObuff);
                    836:                if (buf->b_last_cursor.lnum != 0)
                    837:                    fprintf(fp_out, "\t\"\t%ld\t%d\n",
                    838:                            buf->b_last_cursor.lnum, buf->b_last_cursor.col);
                    839:                for (i = 0; i < NMARKS; i++)
                    840:                    if (buf->b_namedm[i].lnum != 0)
                    841:                        fprintf(fp_out, "\t%c\t%ld\t%d\n", 'a' + i,
                    842:                                buf->b_namedm[i].lnum, buf->b_namedm[i].col);
                    843:                count++;
                    844:            }
                    845:        }
                    846:    }
                    847:
                    848:    return count;
                    849: }
                    850:
                    851: /*
                    852:  * Handle marks in the viminfo file:
                    853:  * fp_out == NULL  read marks for current buffer only
                    854:  * fp_out != NULL  copy marks for buffers not in buffer list
                    855:  */
                    856:    void
                    857: copy_viminfo_marks(line, fp_in, fp_out, count, eof)
                    858:    char_u      *line;
                    859:    FILE        *fp_in;
                    860:    FILE        *fp_out;
                    861:    int         count;
                    862:    int         eof;
                    863: {
                    864:    BUF         *buf;
                    865:    int         num_marked_files;
                    866:    char_u      save_char;
                    867:    int         load_marks;
                    868:    int         copy_marks_out;
                    869:    char_u      *str;
                    870:    int         i;
                    871:    char_u      *p;
                    872:
                    873:    num_marked_files = get_viminfo_parameter('\'');
                    874:    while (!eof && (count < num_marked_files || fp_out == NULL))
                    875:    {
                    876:        if (line[0] != '>')
                    877:        {
                    878:            if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
                    879:                EMSG2("viminfo: Illegal starting char in line %s", line);
                    880:            eof = vim_fgets(line, LSIZE, fp_in);
                    881:            continue;           /* Skip this dud line */
                    882:        }
                    883:
                    884:        /*
                    885:         * Find filename, set str to start.
                    886:         * Ignore leading and trailing white space.
                    887:         */
                    888:        str = skipwhite(line + 1);
                    889:        p = str + STRLEN(str);
                    890:        while (p != str && (*p == NUL || vim_isspace(*p)))
                    891:            p--;
                    892:        if (*p)
                    893:            p++;
                    894:        save_char = *p;
                    895:        *p = NUL;
                    896:
                    897:        /*
                    898:         * If fp_out == NULL, load marks for current buffer.
                    899:         * If fp_out != NULL, copy marks for buffers not in buflist.
                    900:         */
                    901:        load_marks = copy_marks_out = FALSE;
                    902:        if (fp_out == NULL)
                    903:        {
                    904:            if (curbuf->b_filename != NULL &&
                    905:                             fullpathcmp(str, curbuf->b_filename) == FPC_SAME)
                    906:                load_marks = TRUE;
                    907:        }
                    908:        else /* fp_out != NULL */
                    909:        {
                    910:            /* This is slow if there are many buffers!! */
                    911:            for (buf = firstbuf; buf != NULL; buf = buf->b_next)
                    912:                if (buf->b_filename != NULL &&
                    913:                                fullpathcmp(str, buf->b_filename) == FPC_SAME)
                    914:                    break;
                    915:
                    916:            /*
                    917:             * copy marks if the buffer has not been loaded
                    918:             */
                    919:            if (buf == NULL || !buf->b_marks_read)
                    920:            {
                    921:                copy_marks_out = TRUE;
                    922:                *p = save_char;
                    923:                fputs("\n", fp_out);
                    924:                fputs((char *)line, fp_out);
                    925:                count++;
                    926:            }
                    927:        }
                    928:        while (!(eof = vim_fgets(line, LSIZE, fp_in)) && line[0] == TAB)
                    929:        {
                    930:            if (load_marks)
                    931:            {
                    932:                if (line[1] == '"')
                    933:                    sscanf((char *)line + 2, "%ld %d",
                    934:                            &curbuf->b_last_cursor.lnum,
                    935:                            &curbuf->b_last_cursor.col);
                    936:                else if ((i = line[1] - 'a') >= 0 && i < NMARKS)
                    937:                    sscanf((char *)line + 2, "%ld %d",
                    938:                            &curbuf->b_namedm[i].lnum,
                    939:                            &curbuf->b_namedm[i].col);
                    940:            }
                    941:            else if (copy_marks_out)
                    942:                fputs((char *)line, fp_out);
                    943:        }
                    944:        if (load_marks)
                    945:            return;
                    946:    }
                    947: }
                    948: #endif /* VIMINFO */