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 / amiga / vim46src.lha / vim-4.6 / src / mark.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-01  |  22.4 KB  |  966 lines

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