home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / mark.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-04-19  |  32.9 KB  |  1,375 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * mark.c: functions for setting marks and jumping to them
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. /*
  17.  * This file contains routines to maintain and manipulate marks.
  18.  */
  19.  
  20. /*
  21.  * If a named file mark's lnum is non-zero, it is valid.
  22.  * If a named file mark's fnum is non-zero, it is for an existing buffer,
  23.  * otherwise it is from .viminfo and namedfm[n].fname is the file name.
  24.  * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
  25.  * viminfo).
  26.  */
  27. #define EXTRA_MARKS 10                    /* marks 0-9 */
  28. static xfmark_T namedfm[NMARKS + EXTRA_MARKS];        /* marks with file nr */
  29.  
  30. static void fname2fnum __ARGS((xfmark_T *fm));
  31. static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
  32. static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
  33. static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
  34. #ifdef FEAT_JUMPLIST
  35. static void cleanup_jumplist __ARGS((void));
  36. #endif
  37. #ifdef FEAT_VIMINFO
  38. static void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));
  39. #endif
  40.  
  41. /*
  42.  * Set named mark 'c' at current cursor position.
  43.  * Returns OK on success, FAIL if bad name given.
  44.  */
  45.     int
  46. setmark(c)
  47.     int        c;
  48. {
  49.     int        i;
  50.  
  51.     /* Check for a special key (may cause islower() to crash). */
  52.     if (c < 0)
  53.     return FAIL;
  54.  
  55.     if (c == '\'' || c == '`')
  56.     {
  57.     setpcmark();
  58.     /* keep it even when the cursor doesn't move */
  59.     curwin->w_prev_pcmark = curwin->w_pcmark;
  60.     return OK;
  61.     }
  62.  
  63. #ifndef EBCDIC
  64.     if (c > 'z')        /* some islower() and isupper() cannot handle
  65.                 characters above 127 */
  66.     return FAIL;
  67. #endif
  68.     if (islower(c))
  69.     {
  70.     i = c - 'a';
  71.     curbuf->b_namedm[i] = curwin->w_cursor;
  72.     return OK;
  73.     }
  74.     if (isupper(c))
  75.     {
  76.     i = c - 'A';
  77.     namedfm[i].fmark.mark = curwin->w_cursor;
  78.     namedfm[i].fmark.fnum = curbuf->b_fnum;
  79.     vim_free(namedfm[i].fname);
  80.     namedfm[i].fname = NULL;
  81.     return OK;
  82.     }
  83.     return FAIL;
  84. }
  85.  
  86. /*
  87.  * Set the previous context mark to the current position and add it to the
  88.  * jump list.
  89.  */
  90.     void
  91. setpcmark()
  92. {
  93. #ifdef FEAT_JUMPLIST
  94.     int        i;
  95.     xfmark_T    *fm;
  96. #endif
  97. #ifdef JUMPLIST_ROTATE
  98.     xfmark_T    tempmark;
  99. #endif
  100.  
  101.     /* for :global the mark is set only once */
  102.     if (global_busy || listcmd_busy)
  103.     return;
  104.  
  105.     curwin->w_prev_pcmark = curwin->w_pcmark;
  106.     curwin->w_pcmark = curwin->w_cursor;
  107.  
  108. #ifdef FEAT_JUMPLIST
  109. # ifdef JUMPLIST_ROTATE
  110.     /*
  111.      * If last used entry is not at the top, put it at the top by rotating
  112.      * the stack until it is (the newer entries will be at the bottom).
  113.      * Keep one entry (the last used one) at the top.
  114.      */
  115.     if (curwin->w_jumplistidx < curwin->w_jumplistlen)
  116.     ++curwin->w_jumplistidx;
  117.     while (curwin->w_jumplistidx < curwin->w_jumplistlen)
  118.     {
  119.     tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
  120.     for (i = curwin->w_jumplistlen - 1; i > 0; --i)
  121.         curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
  122.     curwin->w_jumplist[0] = tempmark;
  123.     ++curwin->w_jumplistidx;
  124.     }
  125. # endif
  126.  
  127.     /* If jumplist is full: remove oldest entry */
  128.     if (++curwin->w_jumplistlen > JUMPLISTSIZE)
  129.     {
  130.     curwin->w_jumplistlen = JUMPLISTSIZE;
  131.     vim_free(curwin->w_jumplist[0].fname);
  132.     for (i = 1; i < JUMPLISTSIZE; ++i)
  133.         curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
  134.     }
  135.     curwin->w_jumplistidx = curwin->w_jumplistlen;
  136.     fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
  137.  
  138.     fm->fmark.mark = curwin->w_pcmark;
  139.     fm->fmark.fnum = curbuf->b_fnum;
  140.     fm->fname = NULL;
  141. #endif
  142. }
  143.  
  144. /*
  145.  * To change context, call setpcmark(), then move the current position to
  146.  * where ever, then call checkpcmark().  This ensures that the previous
  147.  * context will only be changed if the cursor moved to a different line.
  148.  * If pcmark was deleted (with "dG") the previous mark is restored.
  149.  */
  150.     void
  151. checkpcmark()
  152. {
  153.     if (curwin->w_prev_pcmark.lnum != 0
  154.         && (equal(curwin->w_pcmark, curwin->w_cursor)
  155.         || curwin->w_pcmark.lnum == 0))
  156.     {
  157.     curwin->w_pcmark = curwin->w_prev_pcmark;
  158.     curwin->w_prev_pcmark.lnum = 0;        /* Show it has been checked */
  159.     }
  160. }
  161.  
  162. #if defined(FEAT_JUMPLIST) || defined(PROTO)
  163. /*
  164.  * move "count" positions in the jump list (count may be negative)
  165.  */
  166.     pos_T *
  167. movemark(count)
  168.     int count;
  169. {
  170.     pos_T    *pos;
  171.     xfmark_T    *jmp;
  172.  
  173.     cleanup_jumplist();
  174.  
  175.     if (curwin->w_jumplistlen == 0)        /* nothing to jump to */
  176.     return (pos_T *)NULL;
  177.  
  178.     for (;;)
  179.     {
  180.     if (curwin->w_jumplistidx + count < 0
  181.         || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
  182.         return (pos_T *)NULL;
  183.  
  184.     /*
  185.      * if first CTRL-O or CTRL-I command after a jump, add cursor position
  186.      * to list.  Careful: If there are duplicates (CTRL-O immidiately after
  187.      * starting Vim on a file), another entry may have been removed.
  188.      */
  189.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  190.     {
  191.         setpcmark();
  192.         --curwin->w_jumplistidx;    /* skip the new entry */
  193.         if (curwin->w_jumplistidx + count < 0)
  194.         return (pos_T *)NULL;
  195.     }
  196.  
  197.     curwin->w_jumplistidx += count;
  198.  
  199.     jmp = curwin->w_jumplist + curwin->w_jumplistidx;
  200.     if (jmp->fmark.fnum == 0)
  201.         fname2fnum(jmp);
  202.     if (jmp->fmark.fnum != curbuf->b_fnum)
  203.     {
  204.         /* jump to other file */
  205.         if (buflist_findnr(jmp->fmark.fnum) == NULL)
  206.         {                         /* Skip this one .. */
  207.         count += count < 0 ? -1 : 1;
  208.         continue;
  209.         }
  210.         if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
  211.                                 0, FALSE) == FAIL)
  212.         return (pos_T *)NULL;
  213.         /* Set lnum again, autocommands my have changed it */
  214.         curwin->w_cursor = jmp->fmark.mark;
  215.         pos = (pos_T *)-1;
  216.     }
  217.     else
  218.         pos = &(jmp->fmark.mark);
  219.     return pos;
  220.     }
  221. }
  222. #endif
  223.  
  224. /*
  225.  * Find mark "c".
  226.  * Returns:
  227.  * - pointer to pos_T if found.  lnum is 0 when mark not set, -1 when mark is
  228.  *   in another file which can't be gotten. (caller needs to check lnum!)
  229.  * - NULL if there is no mark called 'c'.
  230.  * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
  231.  */
  232.     pos_T *
  233. getmark(c, changefile)
  234.     int        c;
  235.     int        changefile;        /* allowed to edit another file */
  236. {
  237.     pos_T        *posp;
  238. #ifdef FEAT_VISUAL
  239.     pos_T        *startp, *endp;
  240. #endif
  241.     static pos_T    pos_copy;
  242.  
  243.     posp = NULL;
  244.  
  245.     /* Check for special key, can't be a mark name and might cause islower()
  246.      * to crash. */
  247.     if (c < 0)
  248.     return posp;
  249. #ifndef EBCDIC
  250.     if (c > '~')            /* check for islower()/isupper() */
  251.     ;
  252.     else
  253. #endif
  254.     if (c == '\'' || c == '`')    /* previous context mark */
  255.     {
  256.     pos_copy = curwin->w_pcmark;    /* need to make a copy because */
  257.     posp = &pos_copy;        /*   w_pcmark may be changed soon */
  258.     }
  259.     else if (c == '"')            /* to pos when leaving buffer */
  260.     posp = &(curbuf->b_last_cursor);
  261.     else if (c == '^')            /* to where Insert mode stopped */
  262.     posp = &(curbuf->b_last_insert);
  263.     else if (c == '.')            /* to where last change was made */
  264.     posp = &(curbuf->b_last_change);
  265.     else if (c == '[')            /* to start of previous operator */
  266.     posp = &(curbuf->b_op_start);
  267.     else if (c == ']')            /* to end of previous operator */
  268.     posp = &(curbuf->b_op_end);
  269.     else if (c == '{' || c == '}')    /* to previous/next paragraph */
  270.     {
  271.     pos_T    pos;
  272.     oparg_T    oa;
  273.  
  274.     pos = curwin->w_cursor;
  275.     if (findpar(&oa, c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
  276.     {
  277.         pos_copy = curwin->w_cursor;
  278.         posp = &pos_copy;
  279.     }
  280.     curwin->w_cursor = pos;
  281.     }
  282.     else if (c == '(' || c == ')')    /* to previous/next sentence */
  283.     {
  284.     pos_T    pos;
  285.  
  286.     pos = curwin->w_cursor;
  287.     if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
  288.     {
  289.         pos_copy = curwin->w_cursor;
  290.         posp = &pos_copy;
  291.     }
  292.     curwin->w_cursor = pos;
  293.     }
  294. #ifdef FEAT_VISUAL
  295.     else if (c == '<' || c == '>')    /* start/end of visual area */
  296.     {
  297.     startp = &curbuf->b_visual_start;
  298.     endp = &curbuf->b_visual_end;
  299.     if ((c == '<') == lt(*startp, *endp))
  300.         posp = startp;
  301.     else
  302.         posp = endp;
  303.     /*
  304.      * For Visual line mode, set mark at begin or end of line
  305.      */
  306.     if (curbuf->b_visual_mode == 'V')
  307.     {
  308.         pos_copy = *posp;
  309.         posp = &pos_copy;
  310.         if (c == '<')
  311.         pos_copy.col = 0;
  312.         else
  313.         pos_copy.col = MAXCOL;
  314. #ifdef FEAT_VIRTUALEDIT
  315.         pos_copy.coladd = 0;
  316. #endif
  317.     }
  318.     }
  319. #endif
  320.     else if (ASCII_ISLOWER(c))        /* normal named mark */
  321.     {
  322.     posp = &(curbuf->b_namedm[c - 'a']);
  323.     }
  324.     else if (ASCII_ISUPPER(c) || vim_isdigit(c))    /* named file mark */
  325.     {
  326.     if (vim_isdigit(c))
  327.         c = c - '0' + NMARKS;
  328.     else
  329.         c -= 'A';
  330.     posp = &(namedfm[c].fmark.mark);
  331.  
  332.     if (namedfm[c].fmark.fnum == 0)
  333.         fname2fnum(&namedfm[c]);
  334.     if (namedfm[c].fmark.fnum != curbuf->b_fnum)
  335.     {
  336.         posp = &pos_copy;
  337.  
  338.         /* mark is in another file */
  339.         if (namedfm[c].fmark.mark.lnum != 0
  340.                        && changefile && namedfm[c].fmark.fnum)
  341.         {
  342.         if (buflist_getfile(namedfm[c].fmark.fnum,
  343.                       (linenr_T)1, GETF_SETMARK, FALSE) == OK)
  344.         {
  345.             /* Set the lnum now, autocommands could have changed it */
  346.             curwin->w_cursor = namedfm[c].fmark.mark;
  347.             return (pos_T *)-1;
  348.         }
  349.         pos_copy.lnum = -1;    /* can't get file */
  350.         }
  351.         else
  352.         pos_copy.lnum = 0;    /* mark exists, but is not valid in
  353.                        current buffer */
  354.     }
  355.     }
  356.  
  357.     return posp;
  358. }
  359.  
  360. /*
  361.  * Search for the next named mark in the current file.
  362.  *
  363.  * Returns pointer to pos_T of the next mark or NULL if no mark is found.
  364.  */
  365.     pos_T *
  366. getnextmark(startpos, dir, begin_line)
  367.     pos_T    *startpos;    /* where to start */
  368.     int        dir;    /* direction for search */
  369.     int        begin_line;
  370. {
  371.     int        i;
  372.     pos_T    *result = NULL;
  373.     pos_T    pos;
  374.  
  375.     pos = *startpos;
  376.  
  377.     /* When searching backward and leaving the cursor on the first non-blank,
  378.      * position must be in a previous line.
  379.      * When searching forward and leaving the cursor on the first non-blank,
  380.      * position must be in a next line. */
  381.     if (dir == BACKWARD && begin_line)
  382.     pos.col = 0;
  383.     else if (dir == FORWARD && begin_line)
  384.     pos.col = MAXCOL;
  385.  
  386.     for (i = 0; i < NMARKS; i++)
  387.     {
  388.     if (curbuf->b_namedm[i].lnum > 0)
  389.     {
  390.         if (dir == FORWARD)
  391.         {
  392.         if ((result == NULL || lt(curbuf->b_namedm[i], *result))
  393.             && lt(pos, curbuf->b_namedm[i]))
  394.             result = &curbuf->b_namedm[i];
  395.         }
  396.         else
  397.         {
  398.         if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
  399.             && lt(curbuf->b_namedm[i], pos))
  400.             result = &curbuf->b_namedm[i];
  401.         }
  402.     }
  403.     }
  404.  
  405.     return result;
  406. }
  407.  
  408. /*
  409.  * For an xtended filemark: set the fnum from the fname.
  410.  * This is used for marks obtained from the .viminfo file.  It's postponed
  411.  * until the mark is used to avoid a long startup delay.
  412.  */
  413.     static void
  414. fname2fnum(fm)
  415.     xfmark_T    *fm;
  416. {
  417.     char_u    *p;
  418.  
  419.     if (fm->fname != NULL)
  420.     {
  421.     /*
  422.      * First expand "~/" in the file name to the home directory.
  423.      * Try to shorten the file name.
  424.      */
  425.     expand_env(fm->fname, NameBuff, MAXPATHL);
  426.     mch_dirname(IObuff, IOSIZE);
  427.     p = shorten_fname(NameBuff, IObuff);
  428.  
  429.     /* buflist_new() will call fmarks_check_names() */
  430.     (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
  431.     }
  432. }
  433.  
  434. /*
  435.  * Check all file marks for a name that matches the file name in buf.
  436.  * May replace the name with an fnum.
  437.  * Used for marks that come from the .viminfo file.
  438.  */
  439.     void
  440. fmarks_check_names(buf)
  441.     buf_T    *buf;
  442. {
  443.     char_u    *name;
  444.     int        i;
  445. #ifdef FEAT_JUMPLIST
  446.     win_T    *wp;
  447. #endif
  448.  
  449.     if (buf->b_ffname == NULL)
  450.     return;
  451.  
  452.     name = home_replace_save(buf, buf->b_ffname);
  453.     if (name == NULL)
  454.     return;
  455.  
  456.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  457.     fmarks_check_one(&namedfm[i], name, buf);
  458.  
  459. #ifdef FEAT_JUMPLIST
  460.     FOR_ALL_WINDOWS(wp)
  461.     {
  462.     for (i = 0; i < wp->w_jumplistlen; ++i)
  463.         fmarks_check_one(&wp->w_jumplist[i], name, buf);
  464.     }
  465. #endif
  466.  
  467.     vim_free(name);
  468. }
  469.  
  470.     static void
  471. fmarks_check_one(fm, name, buf)
  472.     xfmark_T    *fm;
  473.     char_u    *name;
  474.     buf_T    *buf;
  475. {
  476.     if (fm->fmark.fnum == 0
  477.         && fm->fname != NULL
  478.         && fnamecmp(name, fm->fname) == 0)
  479.     {
  480.     fm->fmark.fnum = buf->b_fnum;
  481.     vim_free(fm->fname);
  482.     fm->fname = NULL;
  483.     }
  484. }
  485.  
  486. /*
  487.  * Check a if a position from a mark is valid.
  488.  * Give and error message and return FAIL if not.
  489.  */
  490.     int
  491. check_mark(pos)
  492.     pos_T    *pos;
  493. {
  494.     if (pos == NULL)
  495.     {
  496.     EMSG(_(e_umark));
  497.     return FAIL;
  498.     }
  499.     if (pos->lnum <= 0)
  500.     {
  501.     /* lnum is negative if mark is in another file can can't get that
  502.      * file, error message already give then. */
  503.     if (pos->lnum == 0)
  504.         EMSG(_(e_marknotset));
  505.     return FAIL;
  506.     }
  507.     if (pos->lnum > curbuf->b_ml.ml_line_count)
  508.     {
  509.     EMSG(_(e_markinval));
  510.     return FAIL;
  511.     }
  512.     return OK;
  513. }
  514.  
  515. /*
  516.  * clrallmarks() - clear all marks in the buffer 'buf'
  517.  *
  518.  * Used mainly when trashing the entire buffer during ":e" type commands
  519.  */
  520.     void
  521. clrallmarks(buf)
  522.     buf_T    *buf;
  523. {
  524.     static int        i = -1;
  525.  
  526.     if (i == -1)    /* first call ever: initialize */
  527.     for (i = 0; i < NMARKS + 1; i++)
  528.     {
  529.         namedfm[i].fmark.mark.lnum = 0;
  530.         namedfm[i].fname = NULL;
  531.     }
  532.  
  533.     for (i = 0; i < NMARKS; i++)
  534.     buf->b_namedm[i].lnum = 0;
  535.     buf->b_op_start.lnum = 0;        /* start/end op mark cleared */
  536.     buf->b_op_end.lnum = 0;
  537.     buf->b_last_cursor.lnum = 1;    /* '" mark cleared */
  538.     buf->b_last_cursor.col = 0;
  539. #ifdef FEAT_VIRTUALEDIT
  540.     buf->b_last_cursor.coladd = 0;
  541. #endif
  542.     buf->b_last_insert.lnum = 0;    /* '^ mark cleared */
  543.     buf->b_last_change.lnum = 0;    /* '. mark cleared */
  544. }
  545.  
  546. /*
  547.  * Get name of file from a filemark.
  548.  * When it's in the current buffer, return the text at the mark.
  549.  * Returns an allocated string.
  550.  */
  551.     char_u *
  552. fm_getname(fmark, lead_len)
  553.     fmark_T    *fmark;
  554.     int        lead_len;
  555. {
  556.     if (fmark->fnum == curbuf->b_fnum)            /* current buffer */
  557.     return mark_line(&(fmark->mark), lead_len);
  558.     return buflist_nr2name(fmark->fnum, FALSE, TRUE);
  559. }
  560.  
  561. /*
  562.  * Return the line at mark "mp".  Truncate to fit in window.
  563.  * The returned string has been allocated.
  564.  */
  565.     static char_u *
  566. mark_line(mp, lead_len)
  567.     pos_T    *mp;
  568.     int        lead_len;
  569. {
  570.     char_u    *s, *p;
  571.     int        len;
  572.  
  573.     if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
  574.     return vim_strsave((char_u *)"-invalid-");
  575.     s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
  576.     if (s == NULL)
  577.     return NULL;
  578.     /* Truncate the line to fit it in the window */
  579.     len = 0;
  580.     for (p = s; *p != NUL; ++p)
  581.     {
  582.     len += ptr2cells(p);
  583.     if (len >= Columns - lead_len)
  584.         break;
  585. #ifdef FEAT_MBYTE
  586.     if (has_mbyte)
  587.         p += (*mb_ptr2len_check)(p) - 1;
  588. #endif
  589.     }
  590.     *p = NUL;
  591.     return s;
  592. }
  593.  
  594. /*
  595.  * print the marks
  596.  */
  597.     void
  598. do_marks(eap)
  599.     exarg_T    *eap;
  600. {
  601.     char_u    *arg = eap->arg;
  602.     int        i;
  603.     char_u    *name;
  604.  
  605.     if (arg != NULL && *arg == NUL)
  606.     arg = NULL;
  607.  
  608.     show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
  609.     for (i = 0; i < NMARKS; ++i)
  610.     show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
  611.     for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
  612.     {
  613.     if (namedfm[i].fmark.fnum != 0)
  614.         name = fm_getname(&namedfm[i].fmark, 15);
  615.     else
  616.         name = namedfm[i].fname;
  617.     if (name != NULL)
  618.     {
  619.         show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
  620.             arg, &namedfm[i].fmark.mark, name,
  621.             namedfm[i].fmark.fnum == curbuf->b_fnum);
  622.         if (namedfm[i].fmark.fnum != 0)
  623.         vim_free(name);
  624.     }
  625.     }
  626.     show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
  627.     show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
  628.     show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
  629.     show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
  630.     show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
  631. #ifdef FEAT_VISUAL
  632.     show_one_mark('<', arg, &curbuf->b_visual_start, NULL, TRUE);
  633.     show_one_mark('>', arg, &curbuf->b_visual_end, NULL, TRUE);
  634. #endif
  635.     show_one_mark(-1, arg, NULL, NULL, FALSE);
  636. }
  637.  
  638.     static void
  639. show_one_mark(c, arg, p, name, current)
  640.     int        c;
  641.     char_u    *arg;
  642.     pos_T    *p;
  643.     char_u    *name;
  644.     int        current;    /* in current file */
  645. {
  646.     static int    did_title = FALSE;
  647.     int        mustfree = FALSE;
  648.  
  649.     if (c == -1)                /* finish up */
  650.     {
  651.     if (did_title)
  652.         did_title = FALSE;
  653.     else
  654.     {
  655.         if (arg == NULL)
  656.         MSG(_("No marks set"));
  657.         else
  658.         EMSG2(_("E283: No marks matching \"%s\""), arg);
  659.     }
  660.     }
  661.     /* don't output anything if 'q' typed at --more-- prompt */
  662.     else if (!got_int
  663.         && (arg == NULL || vim_strchr(arg, c) != NULL)
  664.         && p->lnum != 0)
  665.     {
  666.     if (!did_title)
  667.     {
  668.         /* Highlight title */
  669.         MSG_PUTS_TITLE(_("\nmark line  col file/text"));
  670.         did_title = TRUE;
  671.     }
  672.     msg_putchar('\n');
  673.     if (!got_int)
  674.     {
  675.         sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
  676.         msg_outtrans(IObuff);
  677.         if (name == NULL && current)
  678.         {
  679.         name = mark_line(p, 15);
  680.         mustfree = TRUE;
  681.         }
  682.         if (name != NULL)
  683.         {
  684.         msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
  685.         if (mustfree)
  686.             vim_free(name);
  687.         }
  688.     }
  689.     out_flush();            /* show one line at a time */
  690.     }
  691. }
  692.  
  693. #if defined(FEAT_JUMPLIST) || defined(PROTO)
  694. /*
  695.  * print the jumplist
  696.  */
  697. /*ARGSUSED*/
  698.     void
  699. ex_jumps(eap)
  700.     exarg_T    *eap;
  701. {
  702.     int        i;
  703.     char_u    *name;
  704.  
  705.     cleanup_jumplist();
  706.     /* Highlight title */
  707.     MSG_PUTS_TITLE(_("\n jump line  col file/text"));
  708.     for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
  709.     {
  710.     if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
  711.     {
  712.         if (curwin->w_jumplist[i].fmark.fnum == 0)
  713.         fname2fnum(&curwin->w_jumplist[i]);
  714.         name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
  715.         if (name == NULL)        /* file name not available */
  716.         continue;
  717.  
  718.         msg_putchar('\n');
  719.         if (got_int)
  720.         break;
  721.         sprintf((char *)IObuff, "%c %2d %5ld %4d ",
  722.         i == curwin->w_jumplistidx ? '>' : ' ',
  723.         i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
  724.                       : curwin->w_jumplistidx - i,
  725.         curwin->w_jumplist[i].fmark.mark.lnum,
  726.         curwin->w_jumplist[i].fmark.mark.col);
  727.         msg_outtrans(IObuff);
  728.         msg_outtrans_attr(name,
  729.                 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
  730.                             ? hl_attr(HLF_D) : 0);
  731.         vim_free(name);
  732.         ui_breakcheck();
  733.     }
  734.     out_flush();
  735.     }
  736.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  737.     MSG_PUTS("\n>");
  738. }
  739. #endif
  740.  
  741. #define one_adjust(add) \
  742.     { \
  743.     lp = add; \
  744.     if (*lp >= line1 && *lp <= line2) \
  745.     { \
  746.         if (amount == MAXLNUM) \
  747.         *lp = 0; \
  748.         else \
  749.         *lp += amount; \
  750.     } \
  751.     else if (amount_after && *lp > line2) \
  752.         *lp += amount_after; \
  753.     }
  754.  
  755. /* don't delete the line, just put at first deleted line */
  756. #define one_adjust_nodel(add) \
  757.     { \
  758.     lp = add; \
  759.     if (*lp >= line1 && *lp <= line2) \
  760.     { \
  761.         if (amount == MAXLNUM) \
  762.         *lp = line1; \
  763.         else \
  764.         *lp += amount; \
  765.     } \
  766.     else if (amount_after && *lp > line2) \
  767.         *lp += amount_after; \
  768.     }
  769.  
  770. /*
  771.  * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
  772.  * Must be called before changed_*(), appended_lines() or deleted_lines().
  773.  * May be called before or after changing the text.
  774.  * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
  775.  * within this range are made invalid.
  776.  * If 'amount_after' is non-zero adjust marks after line2.
  777.  * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
  778.  * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
  779.  *                   or: mark_adjust(56, 55, MAXLNUM, 2);
  780.  */
  781.     void
  782. mark_adjust(line1, line2, amount, amount_after)
  783.     linenr_T    line1;
  784.     linenr_T    line2;
  785.     long    amount;
  786.     long    amount_after;
  787. {
  788.     int        i;
  789.     int        fnum = curbuf->b_fnum;
  790.     linenr_T    *lp;
  791.     win_T    *win;
  792.  
  793.     if (line2 < line1 && amount_after == 0L)        /* nothing to do */
  794.     return;
  795.  
  796.     /* named marks, lower case and upper case */
  797.     for (i = 0; i < NMARKS; i++)
  798.     {
  799.     one_adjust(&(curbuf->b_namedm[i].lnum));
  800.     if (namedfm[i].fmark.fnum == fnum)
  801.         one_adjust(&(namedfm[i].fmark.mark.lnum));
  802.     }
  803.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
  804.     {
  805.     if (namedfm[i].fmark.fnum == fnum)
  806.         one_adjust(&(namedfm[i].fmark.mark.lnum));
  807.     }
  808.  
  809.     /* previous context mark */
  810.     one_adjust(&(curwin->w_pcmark.lnum));
  811.  
  812.     /* previous pcmark */
  813.     one_adjust(&(curwin->w_prev_pcmark.lnum));
  814.  
  815.     /* last Insert position */
  816.     one_adjust(&(curbuf->b_last_insert.lnum));
  817.  
  818.     /* last change position */
  819.     one_adjust(&(curbuf->b_last_change.lnum));
  820.  
  821. #ifdef FEAT_VISUAL
  822.     /* Visual area */
  823.     one_adjust_nodel(&(curbuf->b_visual_start.lnum));
  824.     one_adjust_nodel(&(curbuf->b_visual_end.lnum));
  825. #endif
  826.  
  827. #ifdef FEAT_QUICKFIX
  828.     /* quickfix marks */
  829.     qf_mark_adjust(line1, line2, amount, amount_after);
  830. #endif
  831.  
  832.     /*
  833.      * Adjust items in all windows related to the current buffer.
  834.      */
  835.     FOR_ALL_WINDOWS(win)
  836.     {
  837. #ifdef FEAT_JUMPLIST
  838.     /* Marks in the jumplist.  When deleting lines, this may create
  839.      * duplicate marks in the jumplist, they will be removed later. */
  840.     for (i = 0; i < win->w_jumplistlen; ++i)
  841.         if (win->w_jumplist[i].fmark.fnum == fnum)
  842.         one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
  843. #endif
  844.  
  845.     if (win->w_buffer == curbuf)
  846.     {
  847.         /* marks in the tag stack */
  848.         for (i = 0; i < win->w_tagstacklen; i++)
  849.         if (win->w_tagstack[i].fmark.fnum == fnum)
  850.             one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
  851.  
  852. #ifdef FEAT_VISUAL
  853.         /* the displayed Visual area */
  854.         if (win->w_old_cursor_lnum != 0)
  855.         {
  856.         one_adjust_nodel(&(win->w_old_cursor_lnum));
  857.         one_adjust_nodel(&(win->w_old_visual_lnum));
  858.         }
  859. #endif
  860.  
  861.         /* topline and cursor position for windows with the same buffer
  862.          * other than the current window */
  863.         if (win != curwin)
  864.         {
  865.         if (win->w_topline >= line1 && win->w_topline <= line2)
  866.         {
  867.             if (amount == MAXLNUM)        /* topline is deleted */
  868.             {
  869.             if (line1 <= 1)
  870.                 win->w_topline = 1;
  871.             else
  872.                 win->w_topline = line1 - 1;
  873.             }
  874.             else        /* keep topline on the same line */
  875.             win->w_topline += amount;
  876. #ifdef FEAT_DIFF
  877.             win->w_topfill = 0;
  878. #endif
  879.         }
  880.         else if (amount_after && win->w_topline > line2)
  881.         {
  882.             win->w_topline += amount_after;
  883. #ifdef FEAT_DIFF
  884.             win->w_topfill = 0;
  885. #endif
  886.         }
  887.         if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
  888.         {
  889.             if (amount == MAXLNUM) /* line with cursor is deleted */
  890.             {
  891.             if (line1 <= 1)
  892.                 win->w_cursor.lnum = 1;
  893.             else
  894.                 win->w_cursor.lnum = line1 - 1;
  895.             win->w_cursor.col = 0;
  896.             }
  897.             else        /* keep cursor on the same line */
  898.             win->w_cursor.lnum += amount;
  899.         }
  900.         else if (amount_after && win->w_cursor.lnum > line2)
  901.             win->w_cursor.lnum += amount_after;
  902.         }
  903.  
  904. #ifdef FEAT_FOLDING
  905.         /* adjust folds */
  906.         foldMarkAdjust(win, line1, line2, amount, amount_after);
  907. #endif
  908.     }
  909.     }
  910.  
  911. #ifdef FEAT_DIFF
  912.     /* adjust diffs */
  913.     diff_mark_adjust(line1, line2, amount, amount_after);
  914. #endif
  915.  
  916. #ifdef FEAT_SIGNS
  917.     sign_mark_adjust(line1, line2, amount, amount_after);
  918. #endif
  919. }
  920.  
  921. #ifdef FEAT_JUMPLIST
  922. /*
  923.  * When deleting lines, this may create duplicate marks in the
  924.  * jumplist. They will be removed here for the current window.
  925.  */
  926.     static void
  927. cleanup_jumplist()
  928. {
  929.     int        i;
  930.     int        from, to;
  931.  
  932.     to = 0;
  933.     for (from = 0; from < curwin->w_jumplistlen; ++from)
  934.     {
  935.     if (curwin->w_jumplistidx == from)
  936.         curwin->w_jumplistidx = to;
  937.     for (i = from + 1; i < curwin->w_jumplistlen; ++i)
  938.         if (curwin->w_jumplist[i].fmark.fnum
  939.                     == curwin->w_jumplist[from].fmark.fnum
  940.             && curwin->w_jumplist[from].fmark.fnum != 0
  941.             && curwin->w_jumplist[i].fmark.mark.lnum
  942.                   == curwin->w_jumplist[from].fmark.mark.lnum)
  943.         break;
  944.     if (i >= curwin->w_jumplistlen)        /* no duplicate */
  945.         curwin->w_jumplist[to++] = curwin->w_jumplist[from];
  946.     else
  947.         vim_free(curwin->w_jumplist[from].fname);
  948.     }
  949.     if (curwin->w_jumplistidx == curwin->w_jumplistlen)
  950.     curwin->w_jumplistidx = to;
  951.     curwin->w_jumplistlen = to;
  952. }
  953.  
  954. # if defined(FEAT_WINDOWS) || defined(PROTO)
  955. /*
  956.  * Copy the jumplist from window "from" to window "to".
  957.  */
  958.     void
  959. copy_jumplist(from, to)
  960.     win_T    *from;
  961.     win_T    *to;
  962. {
  963.     int        i;
  964.  
  965.     for (i = 0; i < from->w_jumplistlen; ++i)
  966.     {
  967.     to->w_jumplist[i] = from->w_jumplist[i];
  968.     if (from->w_jumplist[i].fname != NULL)
  969.         to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
  970.     }
  971.     to->w_jumplistlen = from->w_jumplistlen;
  972.     to->w_jumplistidx = from->w_jumplistidx;
  973. }
  974.  
  975. /*
  976.  * Free items in the jumplist of window "wp".
  977.  */
  978.     void
  979. free_jumplist(wp)
  980.     win_T    *wp;
  981. {
  982.     int        i;
  983.  
  984.     for (i = 0; i < wp->w_jumplistlen; ++i)
  985.     vim_free(wp->w_jumplist[i].fname);
  986. }
  987. # endif
  988. #endif /* FEAT_JUMPLIST */
  989.  
  990.     void
  991. set_last_cursor(win)
  992.     win_T    *win;
  993. {
  994.     win->w_buffer->b_last_cursor = win->w_cursor;
  995. }
  996.  
  997. #if defined(FEAT_VIMINFO) || defined(PROTO)
  998.     int
  999. read_viminfo_filemark(virp, force)
  1000.     vir_T    *virp;
  1001.     int        force;
  1002. {
  1003.     char_u    *str;
  1004.     xfmark_T    *fm;
  1005.     int        i;
  1006.  
  1007.     /* We only get here if line[0] == '\'' or '-'.
  1008.      * Illegal mark names are ignored (for future expansion). */
  1009.     str = virp->vir_line + 1;
  1010.     if (
  1011. #ifndef EBCDIC
  1012.         *str <= 127 &&
  1013. #endif
  1014.         ((*virp->vir_line == '\'' && (isdigit(*str) || isupper(*str)))
  1015.          || (*virp->vir_line == '-' && *str == '\'')))
  1016.     {
  1017.     if (*str == '\'')
  1018.     {
  1019. #ifdef FEAT_JUMPLIST
  1020.         /* If the jumplist isn't full insert fmark as oldest entry */
  1021.         if (curwin->w_jumplistlen == JUMPLISTSIZE)
  1022.         fm = NULL;
  1023.         else
  1024.         {
  1025.         for (i = curwin->w_jumplistlen; i > 0; --i)
  1026.             curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
  1027.         ++curwin->w_jumplistidx;
  1028.         ++curwin->w_jumplistlen;
  1029.         fm = &curwin->w_jumplist[0];
  1030.         fm->fmark.mark.lnum = 0;
  1031.         fm->fname = NULL;
  1032.         }
  1033. #else
  1034.         fm = NULL;
  1035. #endif
  1036.     }
  1037.     else if (isdigit(*str))
  1038.         fm = &namedfm[*str - '0' + NMARKS];
  1039.     else
  1040.         fm = &namedfm[*str - 'A'];
  1041.     if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
  1042.     {
  1043.         str = skipwhite(str + 1);
  1044.         fm->fmark.mark.lnum = getdigits(&str);
  1045.         str = skipwhite(str);
  1046.         fm->fmark.mark.col = getdigits(&str);
  1047. #ifdef FEAT_VIRTUALEDIT
  1048.         fm->fmark.mark.coladd = 0;
  1049. #endif
  1050.         fm->fmark.fnum = 0;
  1051.         str = skipwhite(str);
  1052.         vim_free(fm->fname);
  1053.         fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
  1054.                                        FALSE);
  1055.     }
  1056.     }
  1057.     return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
  1058. }
  1059.  
  1060.     void
  1061. write_viminfo_filemarks(fp)
  1062.     FILE    *fp;
  1063. {
  1064.     int        i;
  1065.     char_u    *name;
  1066.     buf_T    *buf;
  1067.     xfmark_T    *fm;
  1068.  
  1069.     if (get_viminfo_parameter('f') == 0)
  1070.     return;
  1071.  
  1072.     fprintf(fp, _("\n# File marks:\n"));
  1073.  
  1074.     /*
  1075.      * Find a mark that is the same file and position as the cursor.
  1076.      * That one, or else the last one is deleted.
  1077.      * Move '0 to '1, '1 to '2, etc. until the matching one or '9
  1078.      * Set '0 mark to current cursor position.
  1079.      */
  1080.     if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
  1081.     {
  1082.     name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
  1083.     for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
  1084.         if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
  1085.             && (namedfm[i].fname == NULL
  1086.                 ? namedfm[i].fmark.fnum == curbuf->b_fnum
  1087.                 : (name != NULL
  1088.                     && STRCMP(name, namedfm[i].fname) == 0)))
  1089.         break;
  1090.     vim_free(name);
  1091.  
  1092.     vim_free(namedfm[i].fname);
  1093.     for ( ; i > NMARKS; --i)
  1094.         namedfm[i] = namedfm[i - 1];
  1095.     namedfm[NMARKS].fmark.mark = curwin->w_cursor;
  1096.     namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
  1097.     namedfm[NMARKS].fname = NULL;
  1098.     }
  1099.  
  1100.     /* Write the filemarks '0 - '9 and 'A - 'Z */
  1101.     for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
  1102.     write_one_filemark(fp, &namedfm[i], '\'',
  1103.                      i < NMARKS ? i + 'A' : i - NMARKS + '0');
  1104.  
  1105. #ifdef FEAT_JUMPLIST
  1106.     /* Write the jumplist with -' */
  1107.     fprintf(fp, _("\n# Jumplist (newest first):\n"));
  1108.     setpcmark();    /* add current cursor position */
  1109.     cleanup_jumplist();
  1110.     for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
  1111.                        fm >= &curwin->w_jumplist[0]; --fm)
  1112.     {
  1113.     if (fm->fmark.fnum == 0
  1114.         || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
  1115.             && !removable(buf->b_ffname)))
  1116.         write_one_filemark(fp, fm, '-', '\'');
  1117.     }
  1118. #endif
  1119. }
  1120.  
  1121.     static void
  1122. write_one_filemark(fp, fm, c1, c2)
  1123.     FILE    *fp;
  1124.     xfmark_T    *fm;
  1125.     int        c1;
  1126.     int        c2;
  1127. {
  1128.     char_u    *name;
  1129.  
  1130.     if (fm->fmark.mark.lnum == 0)    /* not set */
  1131.     return;
  1132.  
  1133.     if (fm->fmark.fnum != 0)        /* there is a buffer */
  1134.     name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
  1135.     else
  1136.     name = fm->fname;        /* use name from .viminfo */
  1137.     if (name == NULL || *name == NUL)
  1138.     return;
  1139.  
  1140.     fprintf(fp, "%c%c  %ld  %ld  ", c1, c2, (long)fm->fmark.mark.lnum,
  1141.                             (long)fm->fmark.mark.col);
  1142.     viminfo_writestring(fp, name);
  1143.     if (fm->fmark.fnum != 0)
  1144.     vim_free(name);
  1145. }
  1146.  
  1147. /*
  1148.  * Return TRUE if "name" is on removable media (depending on 'viminfo').
  1149.  */
  1150.     int
  1151. removable(name)
  1152.     char_u  *name;
  1153. {
  1154.     char_u  *p;
  1155.     char_u  part[51];
  1156.     int        retval = FALSE;
  1157.  
  1158.     name = home_replace_save(NULL, name);
  1159.     if (name != NULL)
  1160.     {
  1161.     for (p = p_viminfo; *p; )
  1162.     {
  1163.         copy_option_part(&p, part, 51, ", ");
  1164.         if (part[0] == 'r'
  1165.             && MB_STRNICMP(part + 1, name, STRLEN(part + 1)) == 0)
  1166.         {
  1167.         retval = TRUE;
  1168.         break;
  1169.         }
  1170.     }
  1171.     vim_free(name);
  1172.     }
  1173.     return retval;
  1174. }
  1175.  
  1176. static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
  1177.  
  1178. /*
  1179.  * Write all the named marks for all buffers.
  1180.  * Return the number of buffers for which marks have been written.
  1181.  */
  1182.     int
  1183. write_viminfo_marks(fp_out)
  1184.     FILE    *fp_out;
  1185. {
  1186.     int        count;
  1187.     buf_T    *buf;
  1188.     int        is_mark_set;
  1189.     int        i;
  1190. #ifdef FEAT_WINDOWS
  1191.     win_T    *win;
  1192.  
  1193.     /*
  1194.      * Set b_last_cursor for the all buffers that have a window.
  1195.      */
  1196.     for (win = firstwin; win != NULL; win = win->w_next)
  1197.     set_last_cursor(win);
  1198. #else
  1199.     set_last_cursor(curwin);
  1200. #endif
  1201.  
  1202.     fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
  1203.     count = 0;
  1204.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1205.     {
  1206.     /*
  1207.      * Only write something if buffer has been loaded and at least one
  1208.      * mark is set.
  1209.      */
  1210.     if (buf->b_marks_read)
  1211.     {
  1212.         if (buf->b_last_cursor.lnum != 0)
  1213.         is_mark_set = TRUE;
  1214.         else
  1215.         {
  1216.         is_mark_set = FALSE;
  1217.         for (i = 0; i < NMARKS; i++)
  1218.             if (buf->b_namedm[i].lnum != 0)
  1219.             {
  1220.             is_mark_set = TRUE;
  1221.             break;
  1222.             }
  1223.         }
  1224.         if (is_mark_set && buf->b_ffname != NULL
  1225.               && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
  1226.         {
  1227.         home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
  1228.         fprintf(fp_out, "\n> ");
  1229.         viminfo_writestring(fp_out, IObuff);
  1230.         write_one_mark(fp_out, '"', &buf->b_last_cursor);
  1231.         write_one_mark(fp_out, '^', &buf->b_last_insert);
  1232.         write_one_mark(fp_out, '.', &buf->b_last_change);
  1233.         for (i = 0; i < NMARKS; i++)
  1234.             write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
  1235.         count++;
  1236.         }
  1237.     }
  1238.     }
  1239.  
  1240.     return count;
  1241. }
  1242.  
  1243.     static void
  1244. write_one_mark(fp_out, c, pos)
  1245.     FILE    *fp_out;
  1246.     int        c;
  1247.     pos_T    *pos;
  1248. {
  1249.     if (pos->lnum != 0)
  1250.     fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
  1251. }
  1252.  
  1253. /*
  1254.  * Handle marks in the viminfo file:
  1255.  * fp_out == NULL   read marks for current buffer only
  1256.  * fp_out != NULL   copy marks for buffers not in buffer list
  1257.  */
  1258.     void
  1259. copy_viminfo_marks(virp, fp_out, count, eof)
  1260.     vir_T    *virp;
  1261.     FILE    *fp_out;
  1262.     int        count;
  1263.     int        eof;
  1264. {
  1265.     char_u    *line = virp->vir_line;
  1266.     buf_T    *buf;
  1267.     int        num_marked_files;
  1268.     int        load_marks;
  1269.     int        copy_marks_out;
  1270.     char_u    *str;
  1271.     int        i;
  1272.     char_u    *p;
  1273.     char_u    *name_buf;
  1274.     pos_T    pos;
  1275.  
  1276.     if ((name_buf = alloc(LSIZE)) == NULL)
  1277.     return;
  1278.     num_marked_files = get_viminfo_parameter('\'');
  1279.     while (!eof && (count < num_marked_files || fp_out == NULL))
  1280.     {
  1281.     if (line[0] != '>')
  1282.     {
  1283.         if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
  1284.         {
  1285.         if (viminfo_error("E576: ", _("Missing '>'"), line))
  1286.             break;    /* too many errors, return now */
  1287.         }
  1288.         eof = vim_fgets(line, LSIZE, virp->vir_fd);
  1289.         continue;        /* Skip this dud line */
  1290.     }
  1291.  
  1292.     /*
  1293.      * Handle long line and translate escaped characters.
  1294.      * Find file name, set str to start.
  1295.      * Ignore leading and trailing white space.
  1296.      */
  1297.     str = skipwhite(line + 1);
  1298.     str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
  1299.     if (str == NULL)
  1300.         continue;
  1301.     p = str + STRLEN(str);
  1302.     while (p != str && (*p == NUL || vim_isspace(*p)))
  1303.         p--;
  1304.     if (*p)
  1305.         p++;
  1306.     *p = NUL;
  1307.  
  1308.     /*
  1309.      * If fp_out == NULL, load marks for current buffer.
  1310.      * If fp_out != NULL, copy marks for buffers not in buflist.
  1311.      */
  1312.     load_marks = copy_marks_out = FALSE;
  1313.     if (fp_out == NULL)
  1314.     {
  1315.         if (curbuf->b_ffname != NULL)
  1316.         {
  1317.         home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
  1318.         if (fnamecmp(str, name_buf) == 0)
  1319.             load_marks = TRUE;
  1320.         }
  1321.     }
  1322.     else /* fp_out != NULL */
  1323.     {
  1324.         /* This is slow if there are many buffers!! */
  1325.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1326.         if (buf->b_ffname != NULL)
  1327.         {
  1328.             home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
  1329.             if (fnamecmp(str, name_buf) == 0)
  1330.             break;
  1331.         }
  1332.  
  1333.         /*
  1334.          * copy marks if the buffer has not been loaded
  1335.          */
  1336.         if (buf == NULL || !buf->b_marks_read)
  1337.         {
  1338.         copy_marks_out = TRUE;
  1339.         fputs("\n> ", fp_out);
  1340.         viminfo_writestring(fp_out, str);
  1341.         count++;
  1342.         }
  1343.     }
  1344.     vim_free(str);
  1345.  
  1346. #ifdef FEAT_VIRTUALEDIT
  1347.     pos.coladd = 0;
  1348. #endif
  1349.     while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
  1350.     {
  1351.         if (load_marks)
  1352.         {
  1353.         if (line[1] != NUL)
  1354.         {
  1355.             sscanf((char *)line + 2, "%ld %u", &pos.lnum, &pos.col);
  1356.             switch (line[1])
  1357.             {
  1358.             case '"': curbuf->b_last_cursor = pos; break;
  1359.             case '^': curbuf->b_last_insert = pos; break;
  1360.             case '.': curbuf->b_last_change = pos; break;
  1361.             default:  if ((i = line[1] - 'a') >= 0 && i < NMARKS)
  1362.                       curbuf->b_namedm[i] = pos;
  1363.             }
  1364.         }
  1365.         }
  1366.         else if (copy_marks_out)
  1367.         fputs((char *)line, fp_out);
  1368.     }
  1369.     if (load_marks)
  1370.         break;
  1371.     }
  1372.     vim_free(name_buf);
  1373. }
  1374. #endif /* FEAT_VIMINFO */
  1375.