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 / cmdline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-08  |  127.3 KB  |  5,583 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.  * cmdline.c: functions for reading in the command line and executing it
  11.  */
  12.  
  13. #include "vim.h"
  14. #include "globals.h"
  15. #include "proto.h"
  16. #include "option.h"
  17. #include "cmdtab.h"
  18. #include "ops.h"            /* included because we call functions in ops.c */
  19. #ifdef HAVE_FCNTL_H
  20. # include <fcntl.h>            /* for chdir() */
  21. #endif
  22.  
  23. /*
  24.  * variables shared between getcmdline() and redrawcmdline()
  25.  */
  26. static char_u    *cmdbuff;        /* pointer to command line buffer */
  27. static int         cmdbufflen;    /* length of cmdbuff */
  28. static int         cmdlen;        /* number of chars on command line */
  29. static int         cmdpos;        /* current cursor position */
  30. static int         cmdspos;        /* cursor column on screen */
  31. static int         cmdfirstc;     /* ':', '/' or '?' */
  32.  
  33. /*
  34.  * Typing mode on the command line.  Shared by getcmdline() and
  35.  * put_on_cmdline().
  36.  */
  37. static int        overstrike = FALSE;    /* typing mode */
  38.  
  39. static int            quitmore = 0;
  40. static int          cmd_numfiles = -1;      /* number of files found by
  41.                                                     filename completion */
  42. /*
  43.  * There are two history tables:
  44.  * 0: colon commands
  45.  * 1: search commands
  46.  */
  47. static char_u        **(history[2]) = {NULL, NULL};    /* history tables */
  48. static int            hisidx[2] = {-1, -1};            /* last entered entry */
  49. static int            hislen = 0;         /* actual lengt of history tables */
  50.  
  51. #ifdef RIGHTLEFT
  52. static int            cmd_hkmap = 0;        /* Hebrew mapping during command line */
  53. #endif
  54.  
  55. static void        init_history __ARGS((void));
  56.  
  57. static int        is_in_history __ARGS((int, char_u *, int));
  58. static void        putcmdline __ARGS((int));
  59. static void        redrawcmd __ARGS((void));
  60. static void        cursorcmd __ARGS((void));
  61. static int        ccheck_abbr __ARGS((int));
  62. static char_u    *do_one_cmd __ARGS((char_u **, int *, int));
  63. static int        buf_write_all __ARGS((BUF *));
  64. static int        do_write __ARGS((char_u *, linenr_t, linenr_t, int, int));
  65. static char_u    *getargcmd __ARGS((char_u **));
  66. static void        backslash_halve __ARGS((char_u *p, int expand_wildcards));
  67. static void        do_make __ARGS((char_u *));
  68. static int        do_arglist __ARGS((char_u *));
  69. static int        is_backslash __ARGS((char_u *str));
  70. static int        check_readonly __ARGS((int));
  71. static int        check_changed __ARGS((BUF *, int, int, int));
  72. static int        check_changed_any __ARGS((void));
  73. static int        check_more __ARGS((int, int));
  74. static void        vim_strncpy __ARGS((char_u *, char_u *, int));
  75. static int        nextwild __ARGS((int));
  76. static int        showmatches __ARGS((char_u *));
  77. static linenr_t get_address __ARGS((char_u **));
  78. static void        set_expand_context __ARGS((int, char_u *));
  79. static char_u    *set_one_cmd_context __ARGS((int, char_u *));
  80. static int        ExpandFromContext __ARGS((char_u *, int *, char_u ***, int, int));
  81. static int        ExpandCommands __ARGS((regexp *, int *, char_u ***));
  82.  
  83. /*
  84.  * init_history() - initialize the command line history
  85.  */
  86.     static void
  87. init_history()
  88. {
  89.     int        newlen;            /* new length of history table */
  90.     char_u    **temp;
  91.     register int i;
  92.     int        j;
  93.     int        type;
  94.  
  95.     /*
  96.      * If size of history table changed, reallocate it
  97.      */
  98.     newlen = (int)p_hi;
  99.     if (newlen != hislen)                        /* history length changed */
  100.     {
  101.         for (type = 0; type <= 1; ++type)        /* adjust both history tables */
  102.         {
  103.             if (newlen)
  104.                 temp = (char_u **)lalloc((long_u)(newlen * sizeof(char_u *)),
  105.                                     TRUE);
  106.             else
  107.                 temp = NULL;
  108.             if (newlen == 0 || temp != NULL)
  109.             {
  110.                 if (hisidx[type] < 0)            /* there are no entries yet */
  111.                 {
  112.                     for (i = 0; i < newlen; ++i)
  113.                         temp[i] = NULL;
  114.                 }
  115.                 else if (newlen > hislen)        /* array becomes bigger */
  116.                 {
  117.                     for (i = 0; i <= hisidx[type]; ++i)
  118.                         temp[i] = history[type][i];
  119.                     j = i;
  120.                     for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
  121.                         temp[i] = NULL;
  122.                     for ( ; j < hislen; ++i, ++j)
  123.                         temp[i] = history[type][j];
  124.                 }
  125.                 else                            /* array becomes smaller or 0 */
  126.                 {
  127.                     j = hisidx[type];
  128.                     for (i = newlen - 1; ; --i)
  129.                     {
  130.                         if (i >= 0)                /* copy newest entries */
  131.                             temp[i] = history[type][j];
  132.                         else                    /* remove older entries */
  133.                             vim_free(history[type][j]);
  134.                         if (--j < 0)
  135.                             j = hislen - 1;
  136.                         if (j == hisidx[type])
  137.                             break;
  138.                     }
  139.                     hisidx[type] = newlen - 1;
  140.                 }
  141.                 vim_free(history[type]);
  142.                 history[type] = temp;
  143.             }
  144.         }
  145.         hislen = newlen;
  146.     }
  147. }
  148.  
  149. /*
  150.  * check if command line 'str' is already in history
  151.  * 'type' is 0 for ':' commands, '1' for search commands
  152.  * if 'move_to_front' is TRUE, matching entry is moved to end of history
  153.  */
  154.     static int
  155. is_in_history(type, str, move_to_front)
  156.     int        type;
  157.     char_u    *str;
  158.     int        move_to_front;        /* Move the entry to the front if it exists */
  159. {
  160.     int        i;
  161.     int        last_i = -1;
  162.  
  163.     if (hisidx[type] < 0)
  164.         return FALSE;
  165.     i = hisidx[type];
  166.     do
  167.     {
  168.         if (history[type][i] == NULL)
  169.             return FALSE;
  170.         if (STRCMP(str, history[type][i]) == 0)
  171.         {
  172.             if (!move_to_front)
  173.                 return TRUE;
  174.             last_i = i;
  175.             break;
  176.         }
  177.         if (--i < 0)
  178.             i = hislen - 1;
  179.     } while (i != hisidx[type]);
  180.  
  181.     if (last_i >= 0)
  182.     {
  183.         str = history[type][i];
  184.         while (i != hisidx[type])
  185.         {
  186.             if (++i >= hislen)
  187.                 i = 0;
  188.             history[type][last_i] = history[type][i];
  189.             last_i = i;
  190.         }
  191.         history[type][i] = str;
  192.         return TRUE;
  193.     }
  194.     return FALSE;
  195. }
  196.  
  197. /*
  198.  * Add the given string to the given history.  If the string is already in the
  199.  * history then it is moved to the front.  histype may be 0 for the ':'
  200.  * history, or 1 for the '/' history.
  201.  */
  202.     void
  203. add_to_history(histype, new_entry)
  204.     int        histype;
  205.     char_u    *new_entry;
  206. {
  207.     if (hislen != 0 && !is_in_history(histype, new_entry, TRUE))
  208.     {
  209.         if (++hisidx[histype] == hislen)
  210.             hisidx[histype] = 0;
  211.         vim_free(history[histype][hisidx[histype]]);
  212.         history[histype][hisidx[histype]] = strsave(new_entry);
  213.     }
  214. }
  215.  
  216.  
  217. /*
  218.  * getcmdline() - accept a command line starting with ':', '/', or '?'
  219.  *
  220.  * The line is collected in cmdbuff, which is reallocated to fit the command
  221.  * line.
  222.  *
  223.  * Return pointer to allocated string if there is a commandline, NULL
  224.  * otherwise.
  225.  */
  226.  
  227.     char_u *
  228. getcmdline(firstc, count)
  229.     int            firstc;     /* either ':', '/', or '?' */
  230.     long        count;        /* only used for incremental search */
  231. {
  232.     register int         c;
  233. #ifdef DIGRAPHS
  234.              int        cc;
  235. #endif
  236.     register int        i;
  237.              int        j;
  238.              char_u        *p;
  239.              int        hiscnt;                /* current history line in use */
  240.              char_u        *lookfor = NULL;    /* string to match */
  241.              int        gotesc = FALSE;        /* TRUE when <ESC> just typed */
  242.              int        do_abbr;            /* when TRUE check for abbr. */
  243.              int        histype;            /* history type to be used */
  244.              FPOS        old_cursor;
  245.              colnr_t    old_curswant;
  246.              int        did_incsearch = FALSE;
  247.              int        incsearch_postponed = FALSE;
  248.              int        save_msg_scroll = msg_scroll;
  249.              int        some_key_typed = FALSE;    /* one of the keys was typed */
  250. #ifdef USE_MOUSE
  251.              /* mouse drag and release events are ignored, unless they are
  252.               * preceded with a mouse down event */
  253.              int        ignore_drag_release = TRUE;
  254. #endif
  255.  
  256.     overstrike = FALSE;                        /* always start in insert mode */
  257.     old_cursor = curwin->w_cursor;            /* needs to be restored later */
  258.     old_curswant = curwin->w_curswant;
  259. /*
  260.  * set some variables for redrawcmd()
  261.  */
  262.     cmdfirstc = firstc;
  263.     alloc_cmdbuff(0);                    /* allocate initial cmdbuff */
  264.     if (cmdbuff == NULL)
  265.         return NULL;                    /* out of memory */
  266.     cmdlen = cmdpos = 0;
  267.     cmdspos = 1;
  268.     State = CMDLINE;
  269. #ifdef USE_MOUSE
  270.     setmouse();
  271. #endif
  272.     gotocmdline(TRUE);
  273.     msg_outchar(firstc);
  274.     /*
  275.      * Avoid scrolling when called by a recursive do_cmdline(), e.g. when doing
  276.      * ":@0" when register 0 doesn't contain a CR.
  277.      */
  278.     msg_scroll = FALSE;
  279.  
  280.     init_history();
  281.     hiscnt = hislen;            /* set hiscnt to impossible history value */
  282.     histype = (firstc == ':' ? 0 : 1);
  283.  
  284. #ifdef DIGRAPHS
  285.     do_digraph(-1);                /* init digraph typahead */
  286. #endif
  287.  
  288.     /* collect the command string, handling editing keys */
  289.     for (;;)
  290.     {
  291.         cursorcmd();            /* set the cursor on the right spot */
  292.         c = vgetc();
  293.         if (KeyTyped)
  294.         {
  295.             some_key_typed = TRUE;
  296. #ifdef RIGHTLEFT
  297.             if (cmd_hkmap)
  298.                 c = hkmap(c);
  299. #endif
  300.         }
  301.  
  302.         /*
  303.          * Ignore got_int when CTRL-C was typed here.
  304.          * Don't ignore it in :global, we really need to break then.
  305.          */
  306.         if (c == Ctrl('C') && !global_busy)
  307.             got_int = FALSE;
  308.  
  309.             /* free old command line when finished moving around in the
  310.              * history list */
  311.         if (lookfor && c != K_S_DOWN && c != K_S_UP &&
  312.                 c != K_DOWN && c != K_UP &&
  313.                 c != K_PAGEDOWN && c != K_PAGEUP &&
  314.                 c != K_KPAGEDOWN && c != K_KPAGEUP &&
  315.                 (cmd_numfiles > 0 || (c != Ctrl('P') && c != Ctrl('N'))))
  316.         {
  317.             vim_free(lookfor);
  318.             lookfor = NULL;
  319.         }
  320.  
  321.         /*
  322.          * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
  323.          */
  324.         if (c != p_wc && c == K_S_TAB)
  325.             c = Ctrl('P');
  326.  
  327.             /* free expanded names when finished walking through matches */
  328.         if (cmd_numfiles != -1 && !(c == p_wc && KeyTyped) && c != Ctrl('N') &&
  329.                         c != Ctrl('P') && c != Ctrl('A') && c != Ctrl('L'))
  330.             (void)ExpandOne(NULL, NULL, 0, WILD_FREE);
  331.  
  332. #ifdef DIGRAPHS
  333.         c = do_digraph(c);
  334. #endif
  335.  
  336.         if (c == '\n' || c == '\r' || (c == ESC && (!KeyTyped || 
  337.                                          vim_strchr(p_cpo, CPO_ESC) != NULL)))
  338.         {
  339.             gotesc = FALSE;        /* Might have typed ESC previously, don't
  340.                                    truncate the cmdline now. */
  341.             if (ccheck_abbr(c + ABBR_OFF))
  342.                 goto cmdline_changed;
  343.             outchar('\r');        /* show that we got the return */
  344.             screen_cur_col = 0;
  345.             flushbuf();
  346.             break;
  347.         }
  348.  
  349.             /* hitting <ESC> twice means: abandon command line */
  350.             /* wildcard expansion is only done when the key is really typed,
  351.              * not when it comes from a macro */
  352.         if (c == p_wc && !gotesc && KeyTyped)
  353.         {
  354.             if (cmd_numfiles > 0)    /* typed p_wc twice */
  355.                 i = nextwild(WILD_NEXT);
  356.             else                    /* typed p_wc first time */
  357.                 i = nextwild(WILD_EXPAND_KEEP);
  358.             if (c == ESC)
  359.                 gotesc = TRUE;
  360.             if (i)
  361.                 goto cmdline_changed;
  362.         }
  363.         gotesc = FALSE;
  364.  
  365.         if (c == NUL || c == K_ZERO)        /* NUL is stored as NL */
  366.             c = NL;
  367.  
  368.         do_abbr = TRUE;            /* default: check for abbreviation */
  369.         switch (c)
  370.         {
  371.         case K_BS:
  372.         case Ctrl('H'):
  373.         case K_DEL:
  374.         case Ctrl('W'):
  375.                 /*
  376.                  * delete current character is the same as backspace on next
  377.                  * character, except at end of line
  378.                  */
  379.                 if (c == K_DEL && cmdpos != cmdlen)
  380.                     ++cmdpos;
  381.                 if (cmdpos > 0)
  382.                 {
  383.                     j = cmdpos;
  384.                     if (c == Ctrl('W'))
  385.                     {
  386.                         while (cmdpos && vim_isspace(cmdbuff[cmdpos - 1]))
  387.                             --cmdpos;
  388.                         i = iswordchar(cmdbuff[cmdpos - 1]);
  389.                         while (cmdpos && !vim_isspace(cmdbuff[cmdpos - 1]) &&
  390.                                          iswordchar(cmdbuff[cmdpos - 1]) == i)
  391.                             --cmdpos;
  392.                     }
  393.                     else
  394.                         --cmdpos;
  395.                     cmdlen -= j - cmdpos;
  396.                     i = cmdpos;
  397.                     while (i < cmdlen)
  398.                         cmdbuff[i++] = cmdbuff[j++];
  399.                     redrawcmd();
  400.                 }
  401.                 else if (cmdlen == 0 && c != Ctrl('W'))
  402.                 {
  403.                     vim_free(cmdbuff);        /* no commandline to return */
  404.                     cmdbuff = NULL;
  405.                     msg_pos(-1, 0);
  406.                     msg_outchar(' ');    /* delete ':' */
  407.                     redraw_cmdline = TRUE;
  408.                     goto returncmd;     /* back to cmd mode */
  409.                 }
  410.                 goto cmdline_changed;
  411.  
  412.         case K_INS:
  413.                 overstrike = !overstrike;
  414.                 /* should change shape of cursor */
  415.                 goto cmdline_not_changed;
  416.  
  417. /*        case '@':    only in very old vi */
  418.         case Ctrl('U'):
  419.                 cmdpos = 0;
  420.                 cmdlen = 0;
  421.                 cmdspos = 1;
  422.                 redrawcmd();
  423.                 goto cmdline_changed;
  424.  
  425.         case ESC:        /* get here if p_wc != ESC or when ESC typed twice */
  426.         case Ctrl('C'):
  427.                 gotesc = TRUE;        /* will free cmdbuff after putting it in
  428.                                         history */
  429.                 goto returncmd;     /* back to cmd mode */
  430.  
  431.         case Ctrl('R'):                /* insert register */
  432.                 putcmdline('"');
  433.                 ++no_mapping;
  434.                   c = vgetc();
  435.                 --no_mapping;
  436.                 if (c != ESC)        /* use ESC to cancel inserting register */
  437.                     cmdline_paste(c);
  438.                 redrawcmd();
  439.                 goto cmdline_changed;
  440.  
  441.         case Ctrl('D'):
  442.             {
  443.                 if (showmatches(cmdbuff) == FAIL)
  444.                     break;        /* Use ^D as normal char instead */
  445.  
  446.                 redrawcmd();
  447.                 continue;        /* don't do incremental search now */
  448.             }
  449.  
  450.         case K_RIGHT:
  451.         case K_S_RIGHT:
  452.                 do
  453.                 {
  454.                         if (cmdpos >= cmdlen)
  455.                                 break;
  456.                         cmdspos += charsize(cmdbuff[cmdpos]);
  457.                         ++cmdpos;
  458.                 }
  459.                 while (c == K_S_RIGHT && cmdbuff[cmdpos] != ' ');
  460.                 goto cmdline_not_changed;
  461.  
  462.         case K_LEFT:
  463.         case K_S_LEFT:
  464.                 do
  465.                 {
  466.                         if (cmdpos <= 0)
  467.                                 break;
  468.                         --cmdpos;
  469.                         cmdspos -= charsize(cmdbuff[cmdpos]);
  470.                 }
  471.                 while (c == K_S_LEFT && cmdbuff[cmdpos - 1] != ' ');
  472.                 goto cmdline_not_changed;
  473.  
  474. #ifdef USE_MOUSE
  475.         case K_MIDDLEDRAG:
  476.         case K_MIDDLERELEASE:
  477.         case K_IGNORE:
  478.                 goto cmdline_not_changed;    /* Ignore mouse */
  479.  
  480.         case K_MIDDLEMOUSE:
  481. # ifdef USE_GUI
  482.                 /* When GUI is active, also paste when 'mouse' is empty */
  483.                 if (!gui.in_use)
  484. # endif
  485.                     if (!mouse_has(MOUSE_COMMAND))
  486.                         goto cmdline_not_changed;    /* Ignore mouse */
  487. # ifdef USE_GUI
  488.                 if (gui.in_use && yankbuffer == 0)
  489.                     cmdline_paste('*');
  490.                 else
  491. # endif
  492.                     cmdline_paste(yankbuffer);
  493.                 redrawcmd();
  494.                 goto cmdline_changed;
  495.  
  496.         case K_LEFTDRAG:
  497.         case K_LEFTRELEASE:
  498.         case K_RIGHTDRAG:
  499.         case K_RIGHTRELEASE:
  500.                 if (ignore_drag_release)
  501.                     goto cmdline_not_changed;
  502.                 /* FALLTHROUGH */
  503.         case K_LEFTMOUSE:
  504.         case K_RIGHTMOUSE:
  505.                 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
  506.                     ignore_drag_release = TRUE;
  507.                 else
  508.                     ignore_drag_release = FALSE;
  509. # ifdef USE_GUI
  510.                 /* When GUI is active, also move when 'mouse' is empty */
  511.                 if (!gui.in_use)
  512. # endif
  513.                     if (!mouse_has(MOUSE_COMMAND))
  514.                         goto cmdline_not_changed;    /* Ignore mouse */
  515.                 cmdspos = 1;
  516.                 for (cmdpos = 0; cmdpos < cmdlen; ++cmdpos)
  517.                 {
  518.                     i = charsize(cmdbuff[cmdpos]);
  519.                     if (mouse_row <= cmdline_row + cmdspos / Columns &&
  520.                                         mouse_col < cmdspos % Columns + i)
  521.                         break;
  522.                     cmdspos += i;
  523.                 }
  524.                 goto cmdline_not_changed;
  525. #endif    /* USE_MOUSE */
  526.  
  527. #ifdef USE_GUI
  528.         case K_SCROLLBAR:
  529.                 if (!msg_scrolled)
  530.                 {
  531.                     gui_do_scroll();
  532.                     redrawcmd();
  533.                 }
  534.                 goto cmdline_not_changed;
  535.  
  536.         case K_HORIZ_SCROLLBAR:
  537.                 if (!msg_scrolled)
  538.                 {
  539.                     gui_do_horiz_scroll();
  540.                     redrawcmd();
  541.                 }
  542.                 goto cmdline_not_changed;
  543. #endif
  544.  
  545.         case Ctrl('B'):        /* begin of command line */
  546.         case K_HOME:
  547.         case K_KHOME:
  548.                 cmdpos = 0;
  549.                 cmdspos = 1;
  550.                 goto cmdline_not_changed;
  551.  
  552.         case Ctrl('E'):        /* end of command line */
  553.         case K_END:
  554.         case K_KEND:
  555.                 cmdpos = cmdlen;
  556.                 cmdbuff[cmdlen] = NUL;
  557.                 cmdspos = strsize(cmdbuff) + 1;
  558.                 goto cmdline_not_changed;
  559.  
  560.         case Ctrl('A'):        /* all matches */
  561.                 if (!nextwild(WILD_ALL))
  562.                     break;
  563.                 goto cmdline_changed;
  564.  
  565.         case Ctrl('L'):        /* longest common part */
  566.                 if (!nextwild(WILD_LONGEST))
  567.                     break;
  568.                 goto cmdline_changed;
  569.  
  570.         case Ctrl('N'):        /* next match */
  571.         case Ctrl('P'):        /* previous match */
  572.                 if (cmd_numfiles > 0)
  573.                 {
  574.                     if (!nextwild((c == Ctrl('P')) ? WILD_PREV : WILD_NEXT))
  575.                         break;
  576.                     goto cmdline_changed;
  577.                 }
  578.  
  579.         case K_UP:
  580.         case K_DOWN:
  581.         case K_S_UP:
  582.         case K_S_DOWN:
  583.         case K_PAGEUP:
  584.         case K_KPAGEUP:
  585.         case K_PAGEDOWN:
  586.         case K_KPAGEDOWN:
  587.                 if (hislen == 0)        /* no history */
  588.                     goto cmdline_not_changed;
  589.  
  590.                 i = hiscnt;
  591.             
  592.                 /* save current command string so it can be restored later */
  593.                 cmdbuff[cmdpos] = NUL;
  594.                 if (lookfor == NULL && (lookfor = strsave(cmdbuff)) == NULL)
  595.                     goto cmdline_not_changed;
  596.  
  597.                 j = STRLEN(lookfor);
  598.                 for (;;)
  599.                 {
  600.                         /* one step backwards */
  601.                     if (c == K_UP || c == K_S_UP || c == Ctrl('P') ||
  602.                             c == K_PAGEUP || c == K_KPAGEUP)
  603.                     {
  604.                         if (hiscnt == hislen)    /* first time */
  605.                             hiscnt = hisidx[histype];
  606.                         else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
  607.                             hiscnt = hislen - 1;
  608.                         else if (hiscnt != hisidx[histype] + 1)
  609.                             --hiscnt;
  610.                         else                    /* at top of list */
  611.                         {
  612.                             hiscnt = i;
  613.                             break;
  614.                         }
  615.                     }
  616.                     else    /* one step forwards */
  617.                     {
  618.                                     /* on last entry, clear the line */
  619.                         if (hiscnt == hisidx[histype])
  620.                         {
  621.                             hiscnt = hislen;
  622.                             break;
  623.                         }
  624.                                     /* not on a history line, nothing to do */
  625.                         if (hiscnt == hislen)
  626.                             break;
  627.                         if (hiscnt == hislen - 1)    /* wrap around */
  628.                             hiscnt = 0;
  629.                         else
  630.                             ++hiscnt;
  631.                     }
  632.                     if (hiscnt < 0 || history[histype][hiscnt] == NULL)
  633.                     {
  634.                         hiscnt = i;
  635.                         break;
  636.                     }
  637.                     if ((c != K_UP && c != K_DOWN) || hiscnt == i ||
  638.                             STRNCMP(history[histype][hiscnt],
  639.                                                     lookfor, (size_t)j) == 0)
  640.                         break;
  641.                 }
  642.  
  643.                 if (hiscnt != i)        /* jumped to other entry */
  644.                 {
  645.                     vim_free(cmdbuff);
  646.                     if (hiscnt == hislen)
  647.                         p = lookfor;    /* back to the old one */
  648.                     else
  649.                         p = history[histype][hiscnt];
  650.  
  651.                     alloc_cmdbuff((int)STRLEN(p));
  652.                     if (cmdbuff == NULL)
  653.                         goto returncmd;
  654.                     STRCPY(cmdbuff, p);
  655.  
  656.                     cmdpos = cmdlen = STRLEN(cmdbuff);
  657.                     redrawcmd();
  658.                     goto cmdline_changed;
  659.                 }
  660.                 beep_flush();
  661.                 goto cmdline_not_changed;
  662.  
  663.         case Ctrl('V'):
  664.         case Ctrl('Q'):
  665. #ifdef USE_MOUSE
  666.                 ignore_drag_release = TRUE;
  667. #endif
  668.                 putcmdline('^');
  669.                 c = get_literal();            /* get next (two) character(s) */
  670.                 do_abbr = FALSE;            /* don't do abbreviation now */
  671.                 break;
  672.  
  673. #ifdef DIGRAPHS
  674.         case Ctrl('K'):
  675. #ifdef USE_MOUSE
  676.                 ignore_drag_release = TRUE;
  677. #endif
  678.                 putcmdline('?');
  679.                 ++no_mapping;
  680.                 ++allow_keys;
  681.                   c = vgetc();
  682.                 --no_mapping;
  683.                 --allow_keys;
  684.                 if (c != ESC)                /* ESC cancels CTRL-K */
  685.                 {
  686.                     if (IS_SPECIAL(c))            /* insert special key code */
  687.                         break;
  688.                     if (charsize(c) == 1)
  689.                         putcmdline(c);
  690.                     ++no_mapping;
  691.                     ++allow_keys;
  692.                     cc = vgetc();
  693.                     --no_mapping;
  694.                     --allow_keys;
  695.                     if (cc != ESC)            /* ESC cancels CTRL-K */
  696.                     {
  697.                         c = getdigraph(c, cc, TRUE);
  698.                         break;
  699.                     }
  700.                 }
  701.                 redrawcmd();
  702.                 goto cmdline_not_changed;
  703. #endif /* DIGRAPHS */
  704.  
  705. #ifdef RIGHTLEFT
  706.         case Ctrl('_'):        /* CTRL-_: switch language mode */
  707.                 cmd_hkmap = !cmd_hkmap;
  708.                 goto cmdline_not_changed;
  709. #endif
  710.  
  711.         default:
  712.                 /*
  713.                  * Normal character with no special meaning.  Just set mod_mask
  714.                  * to 0x0 so that typing Shift-Space in the GUI doesn't enter
  715.                  * the string <S-Space>.  This should only happen after ^V.
  716.                  */
  717.                 if (!IS_SPECIAL(c))
  718.                     mod_mask = 0x0;
  719.                 break;
  720.         }
  721.  
  722.         /* we come here if we have a normal character */
  723.  
  724.         if (do_abbr && (IS_SPECIAL(c) || !iswordchar(c)) && ccheck_abbr(c))
  725.             goto cmdline_changed;
  726.  
  727.         /*
  728.          * put the character in the command line
  729.          */
  730.         if (IS_SPECIAL(c) || mod_mask != 0x0)
  731.             put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
  732.         else
  733.         {
  734.             IObuff[0] = c;
  735.             put_on_cmdline(IObuff, 1, TRUE);
  736.         }
  737.         goto cmdline_changed;
  738.  
  739. /*
  740.  * This part implements incremental searches for "/" and "?"
  741.  * Jump to cmdline_not_changed when a character has been read but the command
  742.  * line did not change. Then we only search and redraw if something changed in
  743.  * the past.
  744.  * Jump to cmdline_changed when the command line did change.
  745.  * (Sorry for the goto's, I know it is ugly).
  746.  */
  747. cmdline_not_changed:
  748.         if (!incsearch_postponed)
  749.             continue;
  750.  
  751. cmdline_changed:
  752.         if (p_is && (firstc == '/' || firstc == '?'))
  753.         {
  754.                 /* if there is a character waiting, search and redraw later */
  755.             if (char_avail())
  756.             {
  757.                 incsearch_postponed = TRUE;
  758.                 continue;
  759.             }
  760.             incsearch_postponed = FALSE;
  761.             curwin->w_cursor = old_cursor;    /* start at old position */
  762.  
  763.                 /* If there is no command line, don't do anything */
  764.             if (cmdlen == 0)
  765.                 i = 0;
  766.             else
  767.             {
  768.                 cmdbuff[cmdlen] = NUL;
  769.                 emsg_off = TRUE;    /* So it doesn't beep if bad expr */
  770.                 i = do_search(firstc, cmdbuff, count,
  771.                                       SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF);
  772.                 emsg_off = FALSE;
  773.             }
  774.             if (i)
  775.             {
  776.                 highlight_match = TRUE;            /* highlight position */
  777.                 cursupdate();
  778.             }
  779.             else
  780.             {
  781.                 highlight_match = FALSE;            /* don't highlight */
  782.                 /* vim_beep(); */ /* even beeps when invalid expr, e.g. "[" */
  783.             }
  784.             updateScreen(NOT_VALID);
  785.             redrawcmdline();
  786.             did_incsearch = TRUE;
  787.         }
  788.     }
  789.  
  790. returncmd:
  791.     if (did_incsearch)
  792.     {
  793.         curwin->w_cursor = old_cursor;
  794.         curwin->w_curswant = old_curswant;
  795.         highlight_match = FALSE;
  796.         redraw_later(NOT_VALID);
  797.     }
  798.     if (cmdbuff != NULL)
  799.     {
  800.         /*
  801.          * Put line in history buffer (":" only when it was typed).
  802.          */
  803.         cmdbuff[cmdlen] = NUL;
  804.         if (cmdlen != 0 && (some_key_typed || firstc != ':'))
  805.         {
  806.             add_to_history(histype, cmdbuff);
  807.             if (firstc == ':')
  808.             {
  809.                 vim_free(new_last_cmdline);
  810.                 new_last_cmdline = strsave(cmdbuff);
  811.             }
  812.         }
  813.  
  814.         if (gotesc)            /* abandon command line */
  815.         {
  816.             vim_free(cmdbuff);
  817.             cmdbuff = NULL;
  818.             MSG("");
  819.             redraw_cmdline = TRUE;
  820.         }
  821.     }
  822.  
  823.     /*
  824.      * If the screen was shifted up, redraw the whole screen (later).
  825.      * If the line is too long, clear it, so ruler and shown command do
  826.      * not get printed in the middle of it.
  827.      */
  828.     msg_check();
  829.     msg_scroll = save_msg_scroll;
  830.     State = NORMAL;
  831. #ifdef USE_MOUSE
  832.     setmouse();
  833. #endif
  834.     return cmdbuff;
  835. }
  836.  
  837. /*
  838.  * Put the given string, of the given length, onto the command line.
  839.  * If len is -1, then STRLEN() is used to calculate the length.
  840.  * If 'redraw' is TRUE then the new part of the command line, and the remaining
  841.  * part will be redrawn, otherwise it will not.  If this function is called
  842.  * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
  843.  * called afterwards.
  844.  */
  845.     int
  846. put_on_cmdline(str, len, redraw)
  847.     char_u    *str;
  848.     int        len;
  849.     int        redraw;
  850. {
  851.     int        i;
  852.  
  853.     if (len < 0)
  854.         len = STRLEN(str);
  855.  
  856.     /* Check if cmdbuff needs to be longer */
  857.     if (cmdlen + len + 1 >= cmdbufflen)
  858.         i = realloc_cmdbuff(cmdlen + len);
  859.     else
  860.         i = OK;
  861.     if (i == OK)
  862.     {
  863.         if (!overstrike)
  864.         {
  865.             vim_memmove(cmdbuff + cmdpos + len, cmdbuff + cmdpos,
  866.                                                    (size_t)(cmdlen - cmdpos));
  867.             cmdlen += len;
  868.         }
  869.         else if (cmdpos + len > cmdlen)
  870.             cmdlen = cmdpos + len;
  871.         vim_memmove(cmdbuff + cmdpos, str, (size_t)len);
  872.         if (redraw)
  873.             msg_outtrans_len(cmdbuff + cmdpos, cmdlen - cmdpos);
  874.         cmdpos += len;
  875.         while (len--)
  876.             cmdspos += charsize(str[len]);
  877.     }
  878.     if (redraw)
  879.         msg_check();
  880.     return i;
  881. }
  882.  
  883.     void
  884. alloc_cmdbuff(len)
  885.     int        len;
  886. {
  887.     /*
  888.      * give some extra space to avoid having to allocate all the time
  889.      */
  890.     if (len < 80)
  891.         len = 100;
  892.     else
  893.         len += 20;
  894.  
  895.     cmdbuff = alloc(len);        /* caller should check for out of memory */
  896.     cmdbufflen = len;
  897. }
  898.  
  899. /*
  900.  * Re-allocate the command line to length len + something extra.
  901.  * return FAIL for failure, OK otherwise
  902.  */
  903.     int
  904. realloc_cmdbuff(len)
  905.     int        len;
  906. {
  907.     char_u        *p;
  908.  
  909.     p = cmdbuff;
  910.     alloc_cmdbuff(len);                /* will get some more */
  911.     if (cmdbuff == NULL)            /* out of memory */
  912.     {
  913.         cmdbuff = p;                /* keep the old one */
  914.         return FAIL;
  915.     }
  916.     vim_memmove(cmdbuff, p, (size_t)cmdlen);
  917.     vim_free(p);
  918.     return OK;
  919. }
  920.  
  921. /*
  922.  * put a character on the command line.
  923.  * Used for CTRL-V and CTRL-K
  924.  */
  925.     static void
  926. putcmdline(c)
  927.     int        c;
  928. {
  929.     char_u    buf[1];
  930.  
  931.     buf[0] = c;
  932.     msg_outtrans_len(buf, 1);
  933.     msg_outtrans_len(cmdbuff + cmdpos, cmdlen - cmdpos);
  934.     cursorcmd();
  935. }
  936.  
  937. /*
  938.  * this fuction is called when the screen size changes and with incremental
  939.  * search
  940.  */
  941.     void
  942. redrawcmdline()
  943. {
  944.     msg_scrolled = 0;
  945.     need_wait_return = FALSE;
  946.     compute_cmdrow();
  947.     redrawcmd();
  948.     cursorcmd();
  949. }
  950.  
  951.     void
  952. compute_cmdrow()
  953. {
  954.     cmdline_row = lastwin->w_winpos + lastwin->w_height +
  955.                                         lastwin->w_status_height;
  956. }
  957.  
  958. /*
  959.  * Redraw what is currently on the command line.
  960.  */
  961.     static void
  962. redrawcmd()
  963. {
  964.     register int    i;
  965.  
  966.     msg_start();
  967.     msg_outchar(cmdfirstc);
  968.     msg_outtrans_len(cmdbuff, cmdlen);
  969.     msg_clr_eos();
  970.  
  971.     cmdspos = 1;
  972.     for (i = 0; i < cmdlen && i < cmdpos; ++i)
  973.         cmdspos += charsize(cmdbuff[i]);
  974.     /*
  975.      * An emsg() before may have set msg_scroll and need_sleep. These are used
  976.      * in normal mode, in cmdline mode we can reset them now.
  977.      */
  978.     msg_scroll = FALSE;            /* next message overwrites cmdline */
  979. #ifdef SLEEP_IN_EMSG
  980.     need_sleep = FALSE;            /* don't sleep */
  981. #endif
  982. }
  983.  
  984.     static void
  985. cursorcmd()
  986. {
  987.     msg_pos(cmdline_row + (cmdspos / (int)Columns), cmdspos % (int)Columns);
  988.     windgoto(msg_row, msg_col);
  989. }
  990.  
  991. /*
  992.  * Check the word in front of the cursor for an abbreviation.
  993.  * Called when the non-id character "c" has been entered.
  994.  * When an abbreviation is recognized it is removed from the text with
  995.  * backspaces and the replacement string is inserted, followed by "c".
  996.  */
  997.     static int
  998. ccheck_abbr(c)
  999.     int c;
  1000. {
  1001.     if (p_paste || no_abbr)            /* no abbreviations or in paste mode */
  1002.         return FALSE;
  1003.     
  1004.     return check_abbr(c, cmdbuff, cmdpos, 0);
  1005. }
  1006.  
  1007. /*
  1008.  * do_cmdline(): execute an Ex command line
  1009.  *
  1010.  * 1. If no line given, get one.
  1011.  * 2. Split up in parts separated with '|'.
  1012.  *
  1013.  * This function may be called recursively!
  1014.  * 
  1015.  * If 'sourcing' is TRUE, the command will be included in the error message.
  1016.  * If 'repeating' is TRUE, there is no wait_return() and friends.
  1017.  *
  1018.  * return FAIL if commandline could not be executed, OK otherwise
  1019.  */
  1020.     int
  1021. do_cmdline(cmdline, sourcing, repeating)
  1022.     char_u        *cmdline;
  1023.     int            sourcing;
  1024.     int            repeating;
  1025. {
  1026.     int            cmdlinelen;
  1027.     char_u        *nextcomm;
  1028.     static int    recursive = 0;            /* recursive depth */
  1029.     int            got_cmdline = FALSE;    /* TRUE when cmdline was typed */
  1030.     int            msg_didout_before_start;
  1031.  
  1032. /*
  1033.  * 1. If no line given: Get a line in cmdbuff.
  1034.  *    If a line is given: Copy it into cmdbuff.
  1035.  *    After this we don't use cmdbuff but cmdline, because of recursiveness
  1036.  */
  1037.     if (cmdline == NULL)
  1038.     {
  1039.         if ((cmdline = getcmdline(':', 1L)) == NULL)
  1040.         {
  1041.                 /* don't call wait_return for aborted command line */
  1042.             need_wait_return = FALSE;
  1043.             return FAIL;
  1044.         }
  1045.         got_cmdline = TRUE;
  1046.     }
  1047.     else
  1048.     {
  1049.         /* Make a copy of the command so we can mess with it. */
  1050.         alloc_cmdbuff((int)STRLEN(cmdline));
  1051.         if (cmdbuff == NULL)
  1052.             return FAIL;
  1053.         STRCPY(cmdbuff, cmdline);
  1054.         cmdline = cmdbuff;
  1055.     }
  1056.     cmdlinelen = cmdbufflen;        /* we need to copy it for recursiveness */
  1057.  
  1058. /*
  1059.  * All output from the commands is put below each other, without waiting for a
  1060.  * return. Don't do this when executing commands from a script or when being
  1061.  * called recursive (e.g. for ":e +command file").
  1062.  */
  1063.     msg_didout_before_start = msg_didout;
  1064.     if (!repeating && !recursive)
  1065.     {
  1066.         msg_didany = FALSE;        /* no output yet */
  1067.         msg_start();
  1068.         msg_scroll = TRUE;        /* put messages below each other */
  1069. #ifdef SLEEP_IN_EMSG
  1070.         ++dont_sleep;            /* don't sleep in emsg() */
  1071. #endif
  1072.         ++no_wait_return;        /* dont wait for return until finished */
  1073.         ++RedrawingDisabled;
  1074.     }
  1075.  
  1076. /*
  1077.  * 2. Loop for each '|' separated command.
  1078.  *    do_one_cmd will set nextcomm to NULL if there is no trailing '|'.
  1079.  *    cmdline and cmdlinelen may change, e.g. for '%' and '#' expansion.
  1080.  */
  1081.     ++recursive;
  1082.     for (;;)
  1083.     {
  1084.         nextcomm = do_one_cmd(&cmdline, &cmdlinelen, sourcing);
  1085.         if (nextcomm == NULL)
  1086.             break;
  1087.         STRCPY(cmdline, nextcomm);
  1088.     }
  1089.     --recursive;
  1090.     vim_free(cmdline);
  1091.  
  1092. /*
  1093.  * If there was too much output to fit on the command line, ask the user to
  1094.  * hit return before redrawing the screen. With the ":global" command we do
  1095.  * this only once after the command is finished.
  1096.  */
  1097.     if (!repeating && !recursive)
  1098.     {
  1099.         --RedrawingDisabled;
  1100. #ifdef SLEEP_IN_EMSG
  1101.         --dont_sleep;
  1102. #endif
  1103.         --no_wait_return;
  1104.         msg_scroll = FALSE;
  1105.         if (need_wait_return || (msg_check() && !dont_wait_return))
  1106.         {
  1107.             /*
  1108.              * The msg_start() above clears msg_didout. The wait_return we do
  1109.              * here should not overwrite the command that may be shown before
  1110.              * doing that.
  1111.              */
  1112.             msg_didout = msg_didout_before_start;
  1113.             wait_return(FALSE);
  1114.         }
  1115.     }
  1116.  
  1117. /*
  1118.  * If the command was typed, remember it for register :
  1119.  * Do this AFTER executing the command to make :@: work.
  1120.  */
  1121.     if (got_cmdline && new_last_cmdline != NULL)
  1122.     {
  1123.         vim_free(last_cmdline);
  1124.         last_cmdline = new_last_cmdline;
  1125.         new_last_cmdline = NULL;
  1126.     }
  1127.     return OK;
  1128. }
  1129.  
  1130. static char *(make_cmd_chars[6]) =
  1131. {    " \164\145a",
  1132.     "\207\171\204\170\060\175\171\174\173\117\032",
  1133.     " c\157\146\146e\145",
  1134.     "\200\174\165\161\203\165\060\171\176\203\165\202\204\060\163\177\171\176\060\204\177\060\202\205\176\060\175\161\173\165\032",
  1135.     " \164o\141\163t",
  1136.     "\136\137\122\137\124\151\060\165\210\200\165\163\204\203\060\204\170\165\060\143\200\161\176\171\203\170\060\171\176\201\205\171\203\171\204\171\177\176\061\032"
  1137. };
  1138.  
  1139. /*
  1140.  * Execute one Ex command.
  1141.  *
  1142.  * If 'sourcing' is TRUE, the command will be included in the error message.
  1143.  *
  1144.  * 2. skip comment lines and leading space
  1145.  * 3. parse range
  1146.  * 4. parse command
  1147.  * 5. parse arguments
  1148.  * 6. switch on command name
  1149.  *
  1150.  * This function may be called recursively!
  1151.  */
  1152.     static char_u *
  1153. do_one_cmd(cmdlinep, cmdlinelenp, sourcing)
  1154.     char_u        **cmdlinep;
  1155.     int            *cmdlinelenp;
  1156.     int            sourcing;
  1157. {
  1158.     char_u                *p;
  1159.     char_u                *q;
  1160.     char_u                *s;
  1161.     char_u                *cmd, *arg;
  1162.     char_u                *do_ecmd_cmd = NULL;    /* +command for do_ecmd() */
  1163.     linenr_t             do_ecmd_lnum = 0;        /* lnum file for do_ecmd() */
  1164.     int                 i = 0;                    /* init to shut up gcc */
  1165.     int                    len;
  1166.     int                    cmdidx;
  1167.     long                argt;
  1168.     register linenr_t    lnum;
  1169.     long                n = 0;                    /* init to shut up gcc */
  1170.     linenr_t             line1 = 1, line2 = 1;    /* the command range */
  1171.     int                    addr_count;                /* number of address specs */
  1172.     int                    forceit = FALSE;        /* '!' after command */
  1173.     FPOS                pos;
  1174.     int                    append = FALSE;            /* write with append */
  1175.     int                    usefilter = FALSE;        /* no read/write but filter */
  1176.     int                    regname = 0;            /* register name flag */
  1177.     char_u                *nextcomm = NULL;        /* no next command yet */
  1178.     int                    amount = 0;                /* for ":>"; init for gcc */
  1179.     char_u                *errormsg = NULL;        /* error message */
  1180.     WIN                    *old_curwin = NULL;        /* init for GCC */
  1181.     static int            if_level = 0;            /* depth in :if */
  1182.  
  1183.         /* when not editing the last file :q has to be typed twice */
  1184.     if (quitmore)
  1185.         --quitmore;
  1186.     did_emsg = FALSE;        /* will be set to TRUE when emsg() used, in which
  1187.                              * case we set nextcomm to NULL to cancel the
  1188.                              * whole command line */
  1189. /*
  1190.  * 2. skip comment lines and leading space and colons
  1191.  */
  1192.     for (cmd = *cmdlinep; vim_strchr((char_u *)" \t:", *cmd) != NULL; cmd++)
  1193.         ;
  1194.  
  1195.     if (*cmd == '"' || *cmd == NUL)    /* ignore comment and empty lines */
  1196.         goto doend;
  1197.  
  1198. /*
  1199.  * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
  1200.  *
  1201.  * where 'addr' is:
  1202.  *
  1203.  * %          (entire file)
  1204.  * $  [+-NUM]
  1205.  * 'x [+-NUM] (where x denotes a currently defined mark)
  1206.  * .  [+-NUM]
  1207.  * [+-NUM]..
  1208.  * NUM
  1209.  *
  1210.  * The cmd pointer is updated to point to the first character following the
  1211.  * range spec. If an initial address is found, but no second, the upper bound
  1212.  * is equal to the lower.
  1213.  */
  1214.  
  1215.     addr_count = 0;
  1216.     if (if_level)
  1217.         goto skip_address;
  1218.  
  1219.     --cmd;
  1220.     do
  1221.     {
  1222.         line1 = line2;
  1223.         line2 = curwin->w_cursor.lnum;    /* default is current line number */
  1224.         cmd = skipwhite(cmd + 1);        /* skip ',' or ';' and following ' ' */
  1225.         lnum = get_address(&cmd);
  1226.         if (cmd == NULL)                /* error detected */
  1227.             goto doend;
  1228.         if (lnum == MAXLNUM)
  1229.         {
  1230.             if (*cmd == '%')            /* '%' - all lines */
  1231.             {
  1232.                 ++cmd;
  1233.                 line1 = 1;
  1234.                 line2 = curbuf->b_ml.ml_line_count;
  1235.                 ++addr_count;
  1236.             }
  1237.             else if (*cmd == '*')        /* '*' - visual area */
  1238.             {
  1239.                 FPOS        *fp;
  1240.  
  1241.                 ++cmd;
  1242.                 fp = getmark('<', FALSE);
  1243.                 if (check_mark(fp) == FAIL)
  1244.                     goto doend;
  1245.                 line1 = fp->lnum;
  1246.                 fp = getmark('>', FALSE);
  1247.                 if (check_mark(fp) == FAIL)
  1248.                     goto doend;
  1249.                 line2 = fp->lnum;
  1250.                 ++addr_count;
  1251.             }
  1252.         }
  1253.         else
  1254.             line2 = lnum;
  1255.         addr_count++;
  1256.  
  1257.         if (*cmd == ';')
  1258.         {
  1259.             if (line2 == 0)
  1260.                 curwin->w_cursor.lnum = 1;
  1261.             else
  1262.                 curwin->w_cursor.lnum = line2;
  1263.         }
  1264.     } while (*cmd == ',' || *cmd == ';');
  1265.  
  1266.     /* One address given: set start and end lines */
  1267.     if (addr_count == 1)
  1268.     {
  1269.         line1 = line2;
  1270.             /* ... but only implicit: really no address given */
  1271.         if (lnum == MAXLNUM)
  1272.             addr_count = 0;
  1273.     }
  1274. skip_address:
  1275.  
  1276. /*
  1277.  * 4. parse command
  1278.  */
  1279.  
  1280.     /*
  1281.      * Skip ':' and any white space
  1282.      */
  1283.     cmd = skipwhite(cmd);
  1284.     if (*cmd == ':')
  1285.         cmd = skipwhite(cmd + 1);
  1286.  
  1287.     /*
  1288.      * If we got a line, but no command, then go to the line.
  1289.      * If we find a '|' or '\n' we set nextcomm.
  1290.      */
  1291.     if (*cmd == NUL || *cmd == '"' ||
  1292.             ((*cmd == '|' || *cmd == '\n') &&
  1293.                     (nextcomm = cmd + 1) != NULL))        /* just an assignment */
  1294.     {
  1295.         /*
  1296.          * strange vi behaviour:
  1297.          * ":3"            jumps to line 3
  1298.          * ":3|..."        prints line 3
  1299.          * ":|"            prints current line
  1300.          */
  1301.         if (if_level)                /* skip this if inside :if */
  1302.             goto doend;
  1303.         if (*cmd == '|')
  1304.         {
  1305.             cmdidx = CMD_print;
  1306.             goto cmdswitch;            /* UGLY goto */
  1307.         }
  1308.         if (addr_count != 0)
  1309.         {
  1310.             if (line2 == 0)
  1311.                 curwin->w_cursor.lnum = 1;
  1312.             else if (line2 > curbuf->b_ml.ml_line_count)
  1313.                 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  1314.             else
  1315.                 curwin->w_cursor.lnum = line2;
  1316.             beginline(MAYBE);
  1317.             /* This causes problems for ":234", since displaying is disabled
  1318.              * here */
  1319.             /* cursupdate(); */
  1320.         }
  1321.         goto doend;
  1322.     }
  1323.  
  1324.     /*
  1325.      * Isolate the command and search for it in the command table.
  1326.      * Exeptions:
  1327.      * - the 'k' command can directly be followed by any character.
  1328.      * - the 's' command can be followed directly by 'c', 'g' or 'r'
  1329.      *        but :sre[wind] is another command.
  1330.      */
  1331.     if (*cmd == 'k')
  1332.     {
  1333.         cmdidx = CMD_k;
  1334.         p = cmd + 1;
  1335.     }
  1336.     else if (*cmd == 's' && vim_strchr((char_u *)"cgr", cmd[1]) != NULL &&
  1337.                                           STRNCMP("sre", cmd, (size_t)3) != 0)
  1338.     {
  1339.         cmdidx = CMD_substitute;
  1340.         p = cmd + 1;
  1341.     }
  1342.     else
  1343.     {
  1344.         p = cmd;
  1345.         while (isalpha(*p))
  1346.             ++p;
  1347.             /* check for non-alpha command */
  1348.         if (p == cmd && vim_strchr((char_u *)"@!=><&~#", *p) != NULL)
  1349.             ++p;
  1350.         i = (int)(p - cmd);
  1351.  
  1352.         for (cmdidx = 0; cmdidx < CMD_SIZE; ++cmdidx)
  1353.             if (STRNCMP(cmdnames[cmdidx].cmd_name, (char *)cmd, (size_t)i) == 0)
  1354.                 break;
  1355.         if (i == 0 || cmdidx == CMD_SIZE)
  1356.         {
  1357.             if (if_level == 0)
  1358.             {
  1359.                 STRCPY(IObuff, "Not an editor command");
  1360.                 if (!sourcing)
  1361.                 {
  1362.                     STRCAT(IObuff, ": ");
  1363.                     STRNCAT(IObuff, *cmdlinep, 40);
  1364.                 }
  1365.                 errormsg = IObuff;
  1366.             }
  1367.             goto doend;
  1368.         }
  1369.     }
  1370.  
  1371. /*
  1372.  * Handle the future ":if" command.
  1373.  * For version 4 everything between ":if" and ":endif" is ignored.
  1374.  */
  1375.     if (cmdidx == CMD_if)
  1376.         ++if_level;
  1377.     if (if_level)
  1378.     {
  1379.         if (cmdidx == CMD_endif)
  1380.             --if_level;
  1381.         goto doend;
  1382.     }
  1383.  
  1384.     if (*p == '!')                    /* forced commands */
  1385.     {
  1386.         ++p;
  1387.         forceit = TRUE;
  1388.     }
  1389.     else
  1390.         forceit = FALSE;
  1391.  
  1392. /*
  1393.  * 5. parse arguments
  1394.  */
  1395.     argt = cmdnames[cmdidx].cmd_argt;
  1396.  
  1397.     if (!(argt & RANGE) && addr_count)        /* no range allowed */
  1398.     {
  1399.         errormsg = e_norange;
  1400.         goto doend;
  1401.     }
  1402.  
  1403.     if (!(argt & BANG) && forceit)            /* no <!> allowed */
  1404.     {
  1405.         errormsg = e_nobang;
  1406.         goto doend;
  1407.     }
  1408.  
  1409. /*
  1410.  * If the range is backwards, ask for confirmation and, if given, swap
  1411.  * line1 & line2 so it's forwards again.
  1412.  * When global command is busy, don't ask, will fail below.
  1413.  */
  1414.     if (!global_busy && line1 > line2)
  1415.     {
  1416.         if (sourcing)
  1417.         {
  1418.             errormsg = (char_u *)"Backwards range given";
  1419.             goto doend;
  1420.         }
  1421.         else if (ask_yesno((char_u *)"Backwards range given, OK to swap", FALSE) != 'y')
  1422.             goto doend;
  1423.         lnum = line1;
  1424.         line1 = line2;
  1425.         line2 = lnum;
  1426.     }
  1427.     /*
  1428.      * don't complain about the range if it is not used
  1429.      * (could happen if line_count is accidently set to 0)
  1430.      */
  1431.     if (line1 < 0 || line2 < 0  || line1 > line2 || ((argt & RANGE) &&
  1432.                     !(argt & NOTADR) && line2 > curbuf->b_ml.ml_line_count))
  1433.     {
  1434.         errormsg = e_invrange;
  1435.         goto doend;
  1436.     }
  1437.  
  1438.     if ((argt & NOTADR) && addr_count == 0)        /* default is 1, not cursor */
  1439.         line2 = 1;
  1440.  
  1441.     if (!(argt & ZEROR))            /* zero in range not allowed */
  1442.     {
  1443.         if (line1 == 0)
  1444.             line1 = 1;
  1445.         if (line2 == 0)
  1446.             line2 = 1;
  1447.     }
  1448.  
  1449.     /*
  1450.      * for the :make command we insert the 'makeprg' option here,
  1451.      * so things like % get expanded
  1452.      */
  1453.     if (cmdidx == CMD_make)
  1454.     {
  1455.         alloc_cmdbuff((int)(STRLEN(p_mp) + STRLEN(p) + 2));
  1456.         if (cmdbuff == NULL)        /* out of memory */
  1457.             goto doend;
  1458.         /*
  1459.          * Check for special command characters and echo them.
  1460.          */
  1461.         for (i = 0; i < 6; i += 2)
  1462.             if (!STRCMP(make_cmd_chars[i], p))
  1463.                 for (s = (char_u *)(make_cmd_chars[i + 1]); *s; ++s)
  1464.                     msg_outchar(*s - 16);
  1465.         STRCPY(cmdbuff, p_mp);
  1466.         STRCAT(cmdbuff, " ");
  1467.         STRCAT(cmdbuff, p);
  1468.             /* 'cmd' is not set here, because it is not used at CMD_make */
  1469.         vim_free(*cmdlinep);
  1470.         *cmdlinep = cmdbuff;
  1471.         *cmdlinelenp = cmdbufflen;
  1472.         p = cmdbuff;
  1473.     }
  1474.  
  1475.     /*
  1476.      * Skip to start of argument.
  1477.      * Don't do this for the ":!" command, because ":!! -l" needs the space.
  1478.      */
  1479.     if (cmdidx == CMD_bang)
  1480.         arg = p;
  1481.     else
  1482.         arg = skipwhite(p);
  1483.  
  1484.     if (cmdidx == CMD_write)
  1485.     {
  1486.         if (*arg == '>')                        /* append */
  1487.         {
  1488.             if (*++arg != '>')                /* typed wrong */
  1489.             {
  1490.                 errormsg = (char_u *)"Use w or w>>";
  1491.                 goto doend;
  1492.             }
  1493.             arg = skipwhite(arg + 1);
  1494.             append = TRUE;
  1495.         }
  1496.         else if (*arg == '!')                    /* :w !filter */
  1497.         {
  1498.             ++arg;
  1499.             usefilter = TRUE;
  1500.         }
  1501.     }
  1502.  
  1503.     if (cmdidx == CMD_read)
  1504.     {
  1505.         if (forceit)
  1506.         {
  1507.             usefilter = TRUE;                    /* :r! filter if forceit */
  1508.             forceit = FALSE;
  1509.         }
  1510.         else if (*arg == '!')                    /* :r !filter */
  1511.         {
  1512.             ++arg;
  1513.             usefilter = TRUE;
  1514.         }
  1515.     }
  1516.  
  1517.     if (cmdidx == CMD_lshift || cmdidx == CMD_rshift)
  1518.     {
  1519.         amount = 1;
  1520.         while (*arg == *cmd)        /* count number of '>' or '<' */
  1521.         {
  1522.             ++arg;
  1523.             ++amount;
  1524.         }
  1525.         arg = skipwhite(arg);
  1526.     }
  1527.  
  1528.     /*
  1529.      * Check for "+command" argument, before checking for next command.
  1530.      * Don't do this for ":read !cmd" and ":write !cmd".
  1531.      */
  1532.     if ((argt & EDITCMD) && !usefilter)
  1533.         do_ecmd_cmd = getargcmd(&arg);
  1534.  
  1535.     /*
  1536.      * Check for '|' to separate commands and '"' to start comments.
  1537.      * Don't do this for ":read !cmd" and ":write !cmd".
  1538.      */
  1539.     if ((argt & TRLBAR) && !usefilter)
  1540.     {
  1541.         for (p = arg; *p; ++p)
  1542.         {
  1543.             if (*p == Ctrl('V'))
  1544.             {
  1545.                 if (argt & (USECTRLV | XFILE)) 
  1546.                     ++p;                /* skip CTRL-V and next char */
  1547.                 else
  1548.                     STRCPY(p, p + 1);    /* remove CTRL-V and skip next char */
  1549.                 if (*p == NUL)            /* stop at NUL after CTRL-V */
  1550.                     break;
  1551.             }
  1552.             else if ((*p == '"' && !(argt & NOTRLCOM)) ||
  1553.                                                       *p == '|' || *p == '\n')
  1554.             {
  1555.                 /*
  1556.                  * We remove the '\' before the '|', unless USECTRLV is used
  1557.                  * AND 'b' is present in 'cpoptions'.
  1558.                  */
  1559.                 if ((vim_strchr(p_cpo, CPO_BAR) == NULL ||
  1560.                                        !(argt & USECTRLV)) && *(p - 1) == '\\')
  1561.                 {
  1562.                     STRCPY(p - 1, p);    /* remove the backslash */
  1563.                     --p;
  1564.                 }
  1565.                 else
  1566.                 {
  1567.                     if (*p == '|' || *p == '\n')
  1568.                         nextcomm = p + 1;
  1569.                     *p = NUL;
  1570.                     break;
  1571.                 }
  1572.             }
  1573.         }
  1574.         if (!(argt & NOTRLCOM))            /* remove trailing spaces */
  1575.             del_trailing_spaces(arg);
  1576.     }
  1577.  
  1578.     /*
  1579.      * Check for <newline> to end a shell command.
  1580.      * Also do this for ":read !cmd" and ":write !cmd".
  1581.      */
  1582.     else if (cmdidx == CMD_bang || usefilter)
  1583.     {
  1584.         for (p = arg; *p; ++p)
  1585.         {
  1586.             if (*p == '\\' && p[1])
  1587.                 ++p;
  1588.             else if (*p == '\n')
  1589.             {
  1590.                 nextcomm = p + 1;
  1591.                 *p = NUL;
  1592.                 break;
  1593.             }
  1594.         }
  1595.     }
  1596.  
  1597.     if ((argt & DFLALL) && addr_count == 0)
  1598.     {
  1599.         line1 = 1;
  1600.         line2 = curbuf->b_ml.ml_line_count;
  1601.     }
  1602.  
  1603.         /* accept numbered register only when no count allowed (:put) */
  1604.     if ((argt & REGSTR) && *arg != NUL && is_yank_buffer(*arg, FALSE) &&
  1605.                                            !((argt & COUNT) && isdigit(*arg)))
  1606.     {
  1607.         regname = *arg;
  1608.         arg = skipwhite(arg + 1);
  1609.     }
  1610.  
  1611.     if ((argt & COUNT) && isdigit(*arg))
  1612.     {
  1613.         n = getdigits(&arg);
  1614.         arg = skipwhite(arg);
  1615.         if (n <= 0)
  1616.         {
  1617.             errormsg = e_zerocount;
  1618.             goto doend;
  1619.         }
  1620.         if (argt & NOTADR)        /* e.g. :buffer 2, :sleep 3 */
  1621.         {
  1622.             line2 = n;
  1623.             if (addr_count == 0)
  1624.                 addr_count = 1;
  1625.         }
  1626.         else
  1627.         {
  1628.             line1 = line2;
  1629.             line2 += n - 1;
  1630.             ++addr_count;
  1631.             /*
  1632.              * Be vi compatible: no error message for out of range.
  1633.              */
  1634.             if (line2 > curbuf->b_ml.ml_line_count)
  1635.                 line2 = curbuf->b_ml.ml_line_count;
  1636.         }
  1637.     }
  1638.                                                 /* no arguments allowed */
  1639.     if (!(argt & EXTRA) && *arg != NUL &&
  1640.                                     vim_strchr((char_u *)"|\"", *arg) == NULL)
  1641.     {
  1642.         errormsg = e_trailing;
  1643.         goto doend;
  1644.     }
  1645.  
  1646.     if ((argt & NEEDARG) && *arg == NUL)
  1647.     {
  1648.         errormsg = e_argreq;
  1649.         goto doend;
  1650.     }
  1651.  
  1652.     /*
  1653.      * change '%'          to curbuf->b_filename
  1654.      *           '#'          to curwin->w_altfile
  1655.      *          '<cword>' to word under the cursor
  1656.      *          '<cWORD>' to WORD under the cursor
  1657.      *          '<cfile>' to path name under the cursor
  1658.      *          '<afile>' to file name for autocommand
  1659.      */
  1660.     if (argt & XFILE)
  1661.     {
  1662.         char_u        *buf = NULL;
  1663.         int            expand_wildcards;        /* need to expand wildcards */
  1664.         int            spec_idx;
  1665.         static char *(spec_str[]) =
  1666.                     {
  1667.                         "%",
  1668. #define SPEC_PERC    0
  1669.                         "#",
  1670. #define SPEC_HASH    1
  1671.                         "<cword>",            /* cursor word */
  1672. #define SPEC_CWORD    2
  1673.                         "<cWORD>",            /* cursor WORD */
  1674. #define SPEC_CCWORD    3
  1675.                         "<cfile>",            /* cursor path name */
  1676. #define SPEC_CFILE    4
  1677. #ifdef AUTOCMD
  1678.                         "<afile>"            /* autocommand file name */
  1679. # define SPEC_AFILE    5
  1680. #endif
  1681.                     };
  1682. #define SPEC_COUNT    (sizeof(spec_str) / sizeof(char *))
  1683.  
  1684.         /*
  1685.          * Decide to expand wildcards *before* replacing '%', '#', etc.  If
  1686.          * the file name contains a wildcard it should not cause expanding.
  1687.          * (it will be expanded anyway if there is a wildcard before replacing).
  1688.          */
  1689.         expand_wildcards = mch_has_wildcard(arg);
  1690.         for (p = arg; *p; ++p)
  1691.         {
  1692.             /*
  1693.              * Quick check if this cannot be the start of a special string.
  1694.              */
  1695.             if (vim_strchr((char_u *)"%#<", *p) == NULL)
  1696.                 continue;
  1697.  
  1698.             /*
  1699.              * Check if there is something to do.
  1700.              */
  1701.             for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx)
  1702.             {
  1703.                 n = strlen(spec_str[spec_idx]);
  1704.                 if (STRNCMP(p, spec_str[spec_idx], n) == 0)
  1705.                     break;
  1706.             }
  1707.             if (spec_idx == SPEC_COUNT)        /* no match */
  1708.                 continue;
  1709.  
  1710.             /*
  1711.              * Skip when preceded with a backslash "\%" and "\#".
  1712.              * Note: In "\\%" the % is also not recognized!
  1713.              */
  1714.             if (*(p - 1) == '\\')
  1715.             {
  1716.                 --p;
  1717.                 STRCPY(p, p + 1);            /* remove escaped char */
  1718.                 continue;
  1719.             }
  1720.  
  1721.             /*
  1722.              * word or WORD under cursor
  1723.              */
  1724.             if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
  1725.             {
  1726.                 len = find_ident_under_cursor(&q, spec_idx == SPEC_CWORD ?
  1727.                                       (FIND_IDENT|FIND_STRING) : FIND_STRING);
  1728.                 if (len == 0)
  1729.                     goto doend;
  1730.             }
  1731.  
  1732.             /*
  1733.              * '#': Alternate file name
  1734.              * '%': Current file name
  1735.              *      File name under the cursor
  1736.              *      File name for autocommand
  1737.              *  and following modifiers
  1738.              */
  1739.             else
  1740.             {
  1741.                 switch (spec_idx)
  1742.                 {
  1743.                     case SPEC_PERC:             /* '%': current file */
  1744.                                 if (curbuf->b_filename == NULL)
  1745.                                 {
  1746.                                     errormsg = (char_u *)"No file name to substitute for '%'";
  1747.                                     goto doend;
  1748.                                 }
  1749.                                 q = curbuf->b_xfilename;
  1750.                                 break;
  1751.  
  1752.                     case SPEC_HASH:            /* '#' or "#99": alternate file */
  1753.                                 q = p + 1;
  1754.                                 i = (int)getdigits(&q);
  1755.                                 n = q - p;        /* length of what we expand */
  1756.  
  1757.                                 if (buflist_name_nr(i, &q, &do_ecmd_lnum) ==
  1758.                                                                          FAIL)
  1759.                                 {
  1760.                                     errormsg = (char_u *)"no alternate filename to substitute for '#'";
  1761.                                     goto doend;
  1762.                                 }
  1763.                                 break;
  1764.  
  1765.                     case SPEC_CFILE:            /* file name under cursor */
  1766.                                 q = file_name_at_cursor(FNAME_MESS|FNAME_HYP);
  1767.                                 if (q == NULL)
  1768.                                     goto doend;
  1769.                                 buf = q;
  1770.                                 break;
  1771.  
  1772. #ifdef AUTOCMD
  1773.                     case SPEC_AFILE:            /* file name for autocommand */
  1774.                                 q = autocmd_fname;
  1775.                                 if (q == NULL)
  1776.                                 {
  1777.                                     errormsg = (char_u *)"no autocommand filename to substitute for \"<afile>\"";
  1778.                                     goto doend;
  1779.                                 }
  1780.                                 break;
  1781. #endif
  1782.                 }
  1783.  
  1784.                 len = STRLEN(q);        /* length of new string */
  1785.                 if (p[n] == '<')        /* remove the file name extension */
  1786.                 {
  1787.                     ++n;
  1788.                     if ((s = vim_strrchr(q, '.')) != NULL && s >= gettail(q))
  1789.                         len = s - q;
  1790.                 }
  1791.                 else
  1792.                 {
  1793.                     char_u        *tail;
  1794.  
  1795.                     /* ":p" - full path/filename */
  1796.                     if (p[n] == ':' && p[n + 1] == 'p')
  1797.                     {
  1798.                         n += 2;
  1799.                         s = FullName_save(q);
  1800.                         vim_free(buf);        /* free any allocated file name */
  1801.                         if (s == NULL)
  1802.                             goto doend;
  1803.                         q = s;
  1804.                         len = STRLEN(q);
  1805.                         buf = q;
  1806.                     }
  1807.  
  1808.                     tail = gettail(q);
  1809.  
  1810.                     /* ":h" - head, remove "/filename"  */
  1811.                     /* ":h" can be repeated */
  1812.                     /* Don't remove the first "/" or "c:\" */
  1813.                     while (p[n] == ':' && p[n + 1] == 'h')
  1814.                     {
  1815.                         n += 2;
  1816.                         s = get_past_head(q);
  1817.                         while (tail > s && ispathsep(tail[-1]))
  1818.                             --tail;
  1819.                         len = tail - q;
  1820.                         while (tail > s && !ispathsep(tail[-1]))
  1821.                             --tail;
  1822.                     }
  1823.  
  1824.                     /* ":t" - tail, just the basename */
  1825.                     if (p[n] == ':' && p[n + 1] == 't')
  1826.                     {
  1827.                         n += 2;
  1828.                         len -= tail - q;
  1829.                         q = tail;
  1830.                     }
  1831.  
  1832.                     /* ":e" - extension */
  1833.                     /* ":e" can be repeated */
  1834.                     /* ":r" - root, without extension */
  1835.                     /* ":r" can be repeated */
  1836.                     while (p[n] == ':' &&
  1837.                                      (p[n + 1] == 'e' || p[n + 1] == 'r'))
  1838.                     {
  1839.                         /* find a '.' in the tail:
  1840.                          * - for second :e: before the current fname
  1841.                          * - otherwise: The last '.'
  1842.                          */
  1843.                         if (p[n + 1] == 'e' && q > tail)
  1844.                             s = q - 2;
  1845.                         else
  1846.                             s = q + len - 1;
  1847.                         for ( ; s > tail; --s)
  1848.                             if (s[0] == '.')
  1849.                                 break;
  1850.                         if (p[n + 1] == 'e')            /* :e */
  1851.                         {
  1852.                             if (s > tail)
  1853.                             {
  1854.                                 len += q - (s + 1);
  1855.                                 q = s + 1;
  1856.                             }
  1857.                             else if (q <= tail)
  1858.                                 len = 0;
  1859.                         }
  1860.                         else                            /* :r */
  1861.                         {
  1862.                             if (s > tail)        /* remove one extension */
  1863.                                 len = s - q;
  1864.                         }
  1865.                         n += 2;
  1866.                     }
  1867.                 }
  1868.  
  1869.                 /* TODO - ":s/pat/foo/" - substitute */
  1870.                 /* if (p[n] == ':' && p[n + 1] == 's') */
  1871.             }
  1872.  
  1873.             /*
  1874.              * The new command line is build in cmdbuff[].
  1875.              * First allocate it.
  1876.              */
  1877.             i = STRLEN(*cmdlinep) + len + 3;
  1878.             if (nextcomm)
  1879.                 i += STRLEN(nextcomm);            /* add space for next command */
  1880.             alloc_cmdbuff(i);
  1881.             if (cmdbuff == NULL)                /* out of memory! */
  1882.                 goto doend;
  1883.  
  1884.             i = p - *cmdlinep;            /* length of part before c */
  1885.             vim_memmove(cmdbuff, *cmdlinep, (size_t)i);
  1886.             vim_memmove(cmdbuff + i, q, (size_t)len);    /* append the string */
  1887.             i += len;                     /* remember the end of the string */
  1888.             STRCPY(cmdbuff + i, p + n);    /* append what is after '#' or '%' */
  1889.             p = cmdbuff + i - 1;        /* remember where to continue */
  1890.             vim_free(buf);                /* free any allocated string */
  1891.  
  1892.             if (nextcomm)                /* append next command */
  1893.             {
  1894.                 i = STRLEN(cmdbuff) + 1;
  1895.                 STRCPY(cmdbuff + i, nextcomm);
  1896.                 nextcomm = cmdbuff + i;
  1897.             }
  1898.             cmd = cmdbuff + (cmd - *cmdlinep);
  1899.             arg = cmdbuff + (arg - *cmdlinep);
  1900.             vim_free(*cmdlinep);
  1901.             *cmdlinep = cmdbuff;
  1902.             *cmdlinelenp = cmdbufflen;
  1903.         }
  1904.  
  1905.         /*
  1906.          * One file argument: expand wildcards.
  1907.          * Don't do this with ":r !command" or ":w !command".
  1908.          */
  1909.         if ((argt & NOSPC) && !usefilter)
  1910.         {
  1911. #if defined(UNIX)
  1912.             /*
  1913.              * Only for Unix we check for more than one file name.
  1914.              * For other systems spaces are considered to be part
  1915.              * of the file name.
  1916.              * Only check here if there is no wildcard, otherwise ExpandOne
  1917.              * will check for errors. This allows ":e `ls ve*.c`" on Unix.
  1918.              */
  1919.             if (!expand_wildcards)
  1920.                 for (p = arg; *p; ++p)
  1921.                 {
  1922.                                 /* skip escaped characters */
  1923.                     if (p[1] && (*p == '\\' || *p == Ctrl('V')))
  1924.                         ++p;
  1925.                     else if (vim_iswhite(*p))
  1926.                     {
  1927.                         errormsg = (char_u *)"Only one file name allowed";
  1928.                         goto doend;
  1929.                     }
  1930.                 }
  1931. #endif
  1932.             /*
  1933.              * halve the number of backslashes (this is vi compatible)
  1934.              */
  1935.             backslash_halve(arg, expand_wildcards);
  1936.  
  1937.             if (expand_wildcards)
  1938.             {
  1939.                 if ((p = ExpandOne(arg, NULL, WILD_LIST_NOTFOUND,
  1940.                                                    WILD_EXPAND_FREE)) == NULL)
  1941.                     goto doend;
  1942.                 n = arg - *cmdlinep;
  1943.                 i = STRLEN(p) + n;
  1944.                 if (nextcomm)
  1945.                     i += STRLEN(nextcomm);
  1946.                 alloc_cmdbuff(i);
  1947.                 if (cmdbuff != NULL)
  1948.                 {
  1949.                     STRNCPY(cmdbuff, *cmdlinep, n);
  1950.                     STRCPY(cmdbuff + n, p);
  1951.                     if (nextcomm)                /* append next command */
  1952.                     {
  1953.                         i = STRLEN(cmdbuff) + 1;
  1954.                         STRCPY(cmdbuff + i, nextcomm);
  1955.                         nextcomm = cmdbuff + i;
  1956.                     }
  1957.                     cmd = cmdbuff + (cmd - *cmdlinep);
  1958.                     arg = cmdbuff + n;
  1959.                     vim_free(*cmdlinep);
  1960.                     *cmdlinep = cmdbuff;
  1961.                     *cmdlinelenp = cmdbufflen;
  1962.                 }
  1963.                 vim_free(p);
  1964.             }
  1965.         }
  1966.     }
  1967.  
  1968.     /*
  1969.      * Accept buffer name.  Cannot be used at the same time with a buffer
  1970.      * number.
  1971.      */
  1972.     if ((argt & BUFNAME) && *arg && addr_count == 0)
  1973.     {
  1974.         /*
  1975.          * :bdelete and :bunload take several arguments, separated by spaces:
  1976.          * find next space (skipping over escaped characters).
  1977.          * The others take one argument: ignore trailing spaces.
  1978.          */
  1979.         if (cmdidx == CMD_bdelete || cmdidx == CMD_bunload)
  1980.             p = skiptowhite_esc(arg);
  1981.         else
  1982.         {
  1983.             p = arg + STRLEN(arg);
  1984.             while (p > arg && vim_iswhite(p[-1]))
  1985.                 --p;
  1986.         }
  1987.         line2 = buflist_findpat(arg, p);
  1988.         if (line2 < 0)            /* failed */
  1989.             goto doend;
  1990.         addr_count = 1;
  1991.         arg = skipwhite(p);
  1992.     }
  1993.  
  1994. /*
  1995.  * 6. switch on command name
  1996.  *    arg        points to the argument of the command
  1997.  *    nextcomm    points to the next command (if any)
  1998.  *      cmd        points to the name of the command (except for :make)
  1999.  *      cmdidx    is the index for the command
  2000.  *      forceit    is TRUE if ! present
  2001.  *      addr_count is the number of addresses given
  2002.  *      line1        is the first line number
  2003.  *      line2        is the second line number or count
  2004.  *      do_ecmd_cmd    is +command argument to be used in edited file
  2005.  *      do_ecmd_lnum  is the line number in edited file
  2006.  *      append    is TRUE with ":w >>file" command
  2007.  *      usefilter is TRUE with ":w !command" and ":r!command"
  2008.  *      amount    is number of '>' or '<' for shift command
  2009.  */
  2010. cmdswitch:
  2011.     switch (cmdidx)
  2012.     {
  2013.         /*
  2014.          * quit current window, quit Vim if closed the last window
  2015.          */
  2016.         case CMD_quit:
  2017.                         /* if more files or windows we won't exit */
  2018.                 if (check_more(FALSE, forceit) == OK && only_one_window())
  2019.                     exiting = TRUE;
  2020.                 if (check_changed(curbuf, FALSE, FALSE, forceit) ||
  2021.                             check_more(TRUE, forceit) == FAIL ||
  2022.                        (only_one_window() && !forceit && check_changed_any()))
  2023.                 {
  2024.                     exiting = FALSE;
  2025.                     settmode(1);
  2026.                     break;
  2027.                 }
  2028.                 if (only_one_window())    /* quit last window */
  2029.                     getout(0);
  2030.                 close_window(curwin, TRUE);    /* may free buffer */
  2031.                 break;
  2032.  
  2033.         /*
  2034.          * try to quit all windows
  2035.          */
  2036.         case CMD_qall:
  2037.                 exiting = TRUE;
  2038.                 if (forceit || !check_changed_any())
  2039.                     getout(0);
  2040.                 exiting = FALSE;
  2041.                 settmode(1);
  2042.                 break;
  2043.  
  2044.         /*
  2045.          * close current window, unless it is the last one
  2046.          */
  2047.         case CMD_close:
  2048.                 close_window(curwin, FALSE);    /* don't free buffer */
  2049.                 break;
  2050.  
  2051.         /*
  2052.          * close all but current window, unless it is the last one
  2053.          */
  2054.         case CMD_only:
  2055.                 close_others(TRUE);
  2056.                 break;
  2057.  
  2058.         case CMD_stop:
  2059.         case CMD_suspend:
  2060. #ifdef WIN32
  2061.                 /*
  2062.                  * Check if external commands are allowed now.
  2063.                  */
  2064.                 if (can_end_termcap_mode(TRUE) == FALSE)
  2065.                     break;
  2066. #endif
  2067.                 if (!forceit)
  2068.                     autowrite_all();
  2069.                 windgoto((int)Rows - 1, 0);
  2070.                 outchar('\n');
  2071.                 flushbuf();
  2072.                 stoptermcap();
  2073.                 mch_restore_title(3);    /* restore window titles */
  2074.                 mch_suspend();            /* call machine specific function */
  2075.                 maketitle();
  2076.                 starttermcap();
  2077.                 scroll_start();            /* scroll screen before redrawing */
  2078.                 must_redraw = CLEAR;
  2079.                 set_winsize(0, 0, FALSE); /* May have resized window -- webb */
  2080.                 break;
  2081.  
  2082.         case CMD_exit:
  2083.         case CMD_xit:
  2084.         case CMD_wq:
  2085.                             /* if more files or windows we won't exit */
  2086.                 if (check_more(FALSE, forceit) == OK && only_one_window())
  2087.                     exiting = TRUE;
  2088.                 if (((cmdidx == CMD_wq || curbuf->b_changed) &&
  2089.                        do_write(arg, line1, line2, FALSE, forceit) == FAIL) ||
  2090.                                           check_more(TRUE, forceit) == FAIL || 
  2091.                        (only_one_window() && !forceit && check_changed_any()))
  2092.                 {
  2093.                     exiting = FALSE;
  2094.                     settmode(1);
  2095.                     break;
  2096.                 }
  2097.                 if (only_one_window())    /* quit last window, exit Vim */
  2098.                     getout(0);
  2099.                 close_window(curwin, TRUE);    /* quit current window, may free buffer */
  2100.                 break;
  2101.  
  2102.         case CMD_xall:        /* write all changed files and exit */
  2103.         case CMD_wqall:        /* write all changed files and quit */
  2104.                 exiting = TRUE;
  2105.                 /* FALLTHROUGH */
  2106.  
  2107.         case CMD_wall:        /* write all changed files */
  2108.                 {
  2109.                     BUF        *buf;
  2110.                     int        error = 0;
  2111.  
  2112.                     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  2113.                     {
  2114.                         if (buf->b_changed)
  2115.                         {
  2116.                             if (buf->b_filename == NULL)
  2117.                             {
  2118.                                 emsg(e_noname);
  2119.                                 ++error;
  2120.                             }
  2121.                             else if (!forceit && buf->b_p_ro)
  2122.                             {
  2123.                                 EMSG2("\"%s\" is readonly, use ! to write anyway", buf->b_xfilename);
  2124.                                 ++error;
  2125.                             }
  2126.                             else
  2127.                             {
  2128.                                 if (buf_write_all(buf) == FAIL)
  2129.                                     ++error;
  2130. #ifdef AUTOCMD
  2131.                                 /* an autocommand may have deleted the buffer */
  2132.                                 if (!buf_valid(buf))
  2133.                                     buf = firstbuf;
  2134. #endif
  2135.                             }
  2136.                         }
  2137.                     }
  2138.                     if (exiting)
  2139.                     {
  2140.                         if (!error)
  2141.                             getout(0);            /* exit Vim */
  2142.                         exiting = FALSE;
  2143.                         settmode(1);
  2144.                     }
  2145.                 }
  2146.                 break;
  2147.  
  2148.         case CMD_preserve:                    /* put everything in .swp file */
  2149.                 ml_preserve(curbuf, TRUE);
  2150.                 break;
  2151.  
  2152.         case CMD_recover:                    /* recover file */
  2153.                 recoverymode = TRUE;
  2154.                 if (!check_changed(curbuf, FALSE, TRUE, forceit) &&
  2155.                             (*arg == NUL || setfname(arg, NULL, TRUE) == OK))
  2156.                     ml_recover();
  2157.                 recoverymode = FALSE;
  2158.                 break;
  2159.  
  2160.         case CMD_args:        
  2161.                     /*
  2162.                      * ":args file": handle like :next
  2163.                      */
  2164.                 if (*arg != NUL && *arg != '|' && *arg != '\n')
  2165.                     goto do_next;
  2166.  
  2167.                 if (arg_count == 0)                /* no file name list */
  2168.                 {
  2169.                     if (check_fname() == OK)    /* check for no file name */
  2170.                         smsg((char_u *)"[%s]", curbuf->b_filename);
  2171.                     break;
  2172.                 }
  2173.                 /*
  2174.                  * Overwrite the command, in most cases there is no scrolling
  2175.                  * required and no wait_return().
  2176.                  */
  2177.                 gotocmdline(TRUE);
  2178.                 for (i = 0; i < arg_count; ++i)
  2179.                 {
  2180.                     if (i == curwin->w_arg_idx)
  2181.                         msg_outchar('[');
  2182.                     msg_outtrans(arg_files[i]);
  2183.                     if (i == curwin->w_arg_idx)
  2184.                         msg_outchar(']');
  2185.                     msg_outchar(' ');
  2186.                 }
  2187.                 break;
  2188.  
  2189.         case CMD_wnext:
  2190.         case CMD_wNext:
  2191.         case CMD_wprevious:
  2192.                 if (cmd[1] == 'n')
  2193.                     i = curwin->w_arg_idx + (int)line2;
  2194.                 else
  2195.                     i = curwin->w_arg_idx - (int)line2;
  2196.                 line1 = 1;
  2197.                 line2 = curbuf->b_ml.ml_line_count;
  2198.                 if (do_write(arg, line1, line2, FALSE, forceit) == FAIL)
  2199.                     break;
  2200.                 goto donextfile;
  2201.  
  2202.         case CMD_next:
  2203.         case CMD_snext:
  2204. do_next:
  2205.                     /*
  2206.                      * check for changed buffer now, if this fails the
  2207.                      * argument list is not redefined.
  2208.                      */
  2209.                 if (!(p_hid || cmdidx == CMD_snext) &&
  2210.                                 check_changed(curbuf, TRUE, FALSE, forceit))
  2211.                     break;
  2212.  
  2213.                 if (*arg != NUL)                /* redefine file list */
  2214.                 {
  2215.                     if (do_arglist(arg) == FAIL)
  2216.                         break;
  2217.                     i = 0;
  2218.                 }
  2219.                 else
  2220.                     i = curwin->w_arg_idx + (int)line2;
  2221.  
  2222. donextfile:        if (i < 0 || i >= arg_count)
  2223.                 {
  2224.                     if (arg_count <= 1)
  2225.                         EMSG("There is only one file to edit");
  2226.                     else if (i < 0)
  2227.                         EMSG("Cannot go before first file");
  2228.                     else
  2229.                         EMSG("Cannot go beyond last file");
  2230.                     break;
  2231.                 }
  2232.                 setpcmark();
  2233.                 if (*cmd == 's')        /* split window first */
  2234.                 {
  2235.                     if (win_split(0, FALSE) == FAIL)
  2236.                         break;
  2237.                 }
  2238.                 else
  2239.                 {
  2240.                     register int other;
  2241.  
  2242.                     /*
  2243.                      * if 'hidden' set, only check for changed file when
  2244.                      * re-editing the same buffer
  2245.                      */
  2246.                     other = TRUE;
  2247.                     if (p_hid)
  2248.                         other = otherfile(fix_fname(arg_files[i]));
  2249.                     if ((!p_hid || !other) &&
  2250.                                  check_changed(curbuf, TRUE, !other, forceit))
  2251.                     break;
  2252.                 }
  2253.                 curwin->w_arg_idx = i;
  2254.                 if (i == arg_count - 1)
  2255.                     arg_had_last = TRUE;
  2256.                 (void)do_ecmd(0, arg_files[curwin->w_arg_idx],
  2257.                                NULL, do_ecmd_cmd, do_ecmd_lnum,
  2258.                                (p_hid ? ECMD_HIDE : 0) +
  2259.                                                 (forceit ? ECMD_FORCEIT : 0));
  2260.                 break;
  2261.  
  2262.         case CMD_previous:
  2263.         case CMD_sprevious:
  2264.         case CMD_Next:
  2265.         case CMD_sNext:
  2266.                 i = curwin->w_arg_idx - (int)line2;
  2267.                 goto donextfile;
  2268.  
  2269.         case CMD_rewind:
  2270.         case CMD_srewind:
  2271.                 i = 0;
  2272.                 goto donextfile;
  2273.  
  2274.         case CMD_last:
  2275.         case CMD_slast:
  2276.                 i = arg_count - 1;
  2277.                 goto donextfile;
  2278.  
  2279.         case CMD_argument:
  2280.         case CMD_sargument:
  2281.                 if (addr_count)
  2282.                     i = line2 - 1;
  2283.                 else
  2284.                     i = curwin->w_arg_idx;
  2285.                 goto donextfile;
  2286.  
  2287.         case CMD_all:
  2288.         case CMD_sall:
  2289.                 if (addr_count == 0)
  2290.                     line2 = 9999;
  2291.                 do_arg_all((int)line2);    /* open a window for each argument */
  2292.                 break;
  2293.  
  2294.         case CMD_buffer:            /* :[N]buffer [N]     to buffer N */
  2295.         case CMD_sbuffer:            /* :[N]sbuffer [N]     to buffer N */
  2296.                 if (*arg)
  2297.                 {
  2298.                     errormsg = e_trailing;
  2299.                     break;
  2300.                 }
  2301.                 if (addr_count == 0)        /* default is current buffer */
  2302.                     (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2303.                                                 DOBUF_CURRENT, FORWARD, 0, 0);
  2304.                 else
  2305.                     (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2306.                                          DOBUF_FIRST, FORWARD, (int)line2, 0);
  2307.                 break;
  2308.  
  2309.         case CMD_bmodified:            /* :[N]bmod    [N]      to next modified buffer */
  2310.         case CMD_sbmodified:        /* :[N]sbmod [N]  to next modified buffer */
  2311.                 (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2312.                                            DOBUF_MOD, FORWARD, (int)line2, 0);
  2313.                 break;
  2314.  
  2315.         case CMD_bnext:                /* :[N]bnext [N]     to next buffer */
  2316.         case CMD_sbnext:            /* :[N]sbnext [N]     to next buffer */
  2317.                 (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2318.                                        DOBUF_CURRENT, FORWARD, (int)line2, 0);
  2319.                 break;
  2320.  
  2321.         case CMD_bNext:                /* :[N]bNext [N]     to previous buffer */
  2322.         case CMD_bprevious:            /* :[N]bprevious [N] to previous buffer */
  2323.         case CMD_sbNext:            /* :[N]sbNext [N]      to previous buffer */
  2324.         case CMD_sbprevious:        /* :[N]sbprevious [N] to previous buffer */
  2325.                 (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2326.                                       DOBUF_CURRENT, BACKWARD, (int)line2, 0);
  2327.                 break;
  2328.  
  2329.         case CMD_brewind:            /* :brewind             to first buffer */
  2330.         case CMD_sbrewind:            /* :sbrewind         to first buffer */
  2331.                 (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2332.                                                   DOBUF_FIRST, FORWARD, 0, 0);
  2333.                 break;
  2334.  
  2335.         case CMD_blast:                /* :blast             to last buffer */
  2336.         case CMD_sblast:            /* :sblast             to last buffer */
  2337.                 (void)do_buffer(*cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
  2338.                                                    DOBUF_LAST, FORWARD, 0, 0);
  2339.                 break;
  2340.  
  2341.         case CMD_bunload:        /* :[N]bunload[!] [N] [bufname] unload buffer */
  2342.         case CMD_bdelete:        /* :[N]bdelete[!] [N] [bufname] delete buffer */
  2343.                 errormsg = do_bufdel(
  2344.                             cmdidx == CMD_bdelete ? DOBUF_DEL : DOBUF_UNLOAD,
  2345.                             arg, addr_count, (int)line1, (int)line2, forceit);
  2346.                 break;
  2347.  
  2348.         case CMD_unhide:
  2349.         case CMD_sunhide:    /* open a window for loaded buffers */
  2350.                 if (addr_count == 0)
  2351.                     line2 = 9999;
  2352.                 (void)do_buffer_all((int)line2, FALSE);
  2353.                 break;
  2354.  
  2355.         case CMD_ball:
  2356.         case CMD_sball:        /* open a window for every buffer */
  2357.                 if (addr_count == 0)
  2358.                     line2 = 9999;
  2359.                 (void)do_buffer_all((int)line2, TRUE);
  2360.                 break;
  2361.  
  2362.         case CMD_buffers:
  2363.         case CMD_files:
  2364.         case CMD_ls:
  2365.                 buflist_list();
  2366.                 break;
  2367.  
  2368.         case CMD_write:
  2369.                 if (usefilter)        /* input lines to shell command */
  2370.                     do_bang(1, line1, line2, FALSE, arg, TRUE, FALSE);
  2371.                 else
  2372.                     (void)do_write(arg, line1, line2, append, forceit);
  2373.                 break;
  2374.  
  2375.             /*
  2376.              * set screen mode
  2377.              * if no argument given, just get the screen size and redraw
  2378.              */
  2379.         case CMD_mode:
  2380.                 if (*arg == NUL || mch_screenmode(arg) != FAIL)
  2381.                     set_winsize(0, 0, FALSE);
  2382.                 break;
  2383.  
  2384.                 /*
  2385.                  * set, increment or decrement current window height
  2386.                  */
  2387.         case CMD_resize:
  2388.                 n = atol((char *)arg);
  2389.                 if (*arg == '-' || *arg == '+')
  2390.                     win_setheight(curwin->w_height + (int)n);
  2391.                 else
  2392.                 {
  2393.                     if (n == 0)        /* default is very high */
  2394.                         n = 9999;
  2395.                     win_setheight((int)n);
  2396.                 }
  2397.                 break;
  2398.  
  2399.                 /*
  2400.                  * :sview [+command] file    split window with new file, ro
  2401.                  * :split [[+command] file]  split window with current or new file
  2402.                  * :new [[+command] file]    split window with no or new file
  2403.                  */
  2404.         case CMD_sview:
  2405.         case CMD_split:
  2406.         case CMD_new:
  2407.                 old_curwin = curwin;
  2408.                 if (win_split(addr_count ? (int)line2 : 0, FALSE) == FAIL)
  2409.                     break;
  2410.                 /*FALLTHROUGH*/
  2411.  
  2412.         case CMD_edit:
  2413.         case CMD_ex:
  2414.         case CMD_visual:
  2415.         case CMD_view:
  2416.                 if ((cmdidx == CMD_new) && *arg == NUL)
  2417.                 {
  2418.                     setpcmark();
  2419.                     (void)do_ecmd(0, NULL, NULL, do_ecmd_cmd, (linenr_t)1,
  2420.                                   ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0));
  2421.                 }
  2422.                 else if (cmdidx != CMD_split || *arg != NUL)
  2423.                 {
  2424.                     n = readonlymode;
  2425.                     if (cmdidx == CMD_view || cmdidx == CMD_sview)
  2426.                         readonlymode = TRUE;
  2427.                     setpcmark();
  2428.                     (void)do_ecmd(0, arg, NULL, do_ecmd_cmd, do_ecmd_lnum,
  2429.                                                      (p_hid ? ECMD_HIDE : 0) +
  2430.                                                 (forceit ? ECMD_FORCEIT : 0));
  2431.                     readonlymode = n;
  2432.                 }
  2433.                 else
  2434.                     updateScreen(NOT_VALID);
  2435.                     /* if ":split file" worked, set alternate filename in
  2436.                      * old window to new file */
  2437.                 if ((cmdidx == CMD_new || cmdidx == CMD_split) &&
  2438.                                 *arg != NUL && curwin != old_curwin &&
  2439.                                 win_valid(old_curwin) &&
  2440.                                 old_curwin->w_buffer != curbuf)
  2441.                     old_curwin->w_alt_fnum = curbuf->b_fnum;
  2442.                 break;
  2443.  
  2444. #ifdef USE_GUI
  2445.         /*
  2446.          * Change from the terminal version to the GUI version.  File names may
  2447.          * be given to redefine the args list -- webb
  2448.          */
  2449.         case CMD_gvim:
  2450.         case CMD_gui:
  2451.                 if (arg[0] == '-' && arg[1] == 'f' &&
  2452.                                        (arg[2] == NUL || vim_iswhite(arg[2])))
  2453.                 {
  2454.                     gui.dofork = FALSE;
  2455.                     arg = skipwhite(arg + 2);
  2456.                 }
  2457.                 else
  2458.                     gui.dofork = TRUE;
  2459.                 if (!gui.in_use)
  2460.                     gui_start();
  2461.                 if (*arg != NUL && *arg != '|' && *arg != '\n')
  2462.                     goto do_next;
  2463.                 break;
  2464. #endif
  2465.  
  2466.         case CMD_file:
  2467.                 do_file(arg, forceit);
  2468.                 break;
  2469.  
  2470.         case CMD_swapname:
  2471.                 if (curbuf->b_ml.ml_mfp == NULL ||
  2472.                                 (p = curbuf->b_ml.ml_mfp->mf_fname) == NULL)
  2473.                     MSG("No swap file");
  2474.                 else
  2475.                     msg(p);
  2476.                 break;
  2477.  
  2478. #if 0
  2479.         case CMD_mfstat:        /* print memfile statistics, for debugging */
  2480.                 mf_statistics();
  2481.                 break;
  2482. #endif
  2483.  
  2484.         case CMD_read:
  2485.                 if (usefilter)                    /* :r!cmd */
  2486.                 {    
  2487.                     do_bang(1, line1, line2, FALSE, arg, FALSE, TRUE);
  2488.                     break;
  2489.                 }
  2490.                 if (u_save(line2, (linenr_t)(line2 + 1)) == FAIL)
  2491.                     break;
  2492.                 if (*arg == NUL)
  2493.                 {
  2494.                     if (check_fname() == FAIL)    /* check for no file name */
  2495.                         break;
  2496.                     i = readfile(curbuf->b_filename, curbuf->b_sfilename,
  2497.                                     line2, FALSE, (linenr_t)0, MAXLNUM, FALSE);
  2498.                 }
  2499.                 else
  2500.                 {
  2501.                     if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
  2502.                         setaltfname(arg, arg, (linenr_t)1);
  2503.                     i = readfile(arg, NULL,
  2504.                                     line2, FALSE, (linenr_t)0, MAXLNUM, FALSE);
  2505.                 }
  2506.                 if (i == FAIL)
  2507.                 {
  2508.                     emsg2(e_notopen, arg);
  2509.                     break;
  2510.                 }
  2511.                 
  2512.                 updateScreen(NOT_VALID);
  2513.                 break;
  2514.  
  2515.         case CMD_cd:
  2516.         case CMD_chdir:
  2517. #ifdef UNIX
  2518.                 /*
  2519.                  * for UNIX ":cd" means: go to home directory
  2520.                  */
  2521.                 if (*arg == NUL)     /* use NameBuff for home directory name */
  2522.                 {
  2523.                     expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
  2524.                     arg = NameBuff;
  2525.                 }
  2526. #endif
  2527.                 if (*arg != NUL)
  2528.                 {
  2529.                     if (!did_cd)
  2530.                     {
  2531.                         BUF        *buf;
  2532.  
  2533.                             /* use full path from now on for names of files
  2534.                              * being edited and swap files */
  2535.                         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  2536.                         {
  2537.                             buf->b_xfilename = buf->b_filename;
  2538.                             mf_fullname(buf->b_ml.ml_mfp);
  2539.                         }
  2540.                         status_redraw_all();
  2541.                     }
  2542.                     did_cd = TRUE;
  2543.                     if (vim_chdir((char *)arg))
  2544.                         emsg(e_failed);
  2545.                     break;
  2546.                 }
  2547.                 /*FALLTHROUGH*/
  2548.  
  2549.         case CMD_pwd:
  2550.                 if (mch_dirname(NameBuff, MAXPATHL) == OK)
  2551.                     msg(NameBuff);
  2552.                 else
  2553.                     emsg(e_unknown);
  2554.                 break;
  2555.  
  2556.         case CMD_equal:
  2557.                 smsg((char_u *)"line %ld", (long)line2);
  2558.                 break;
  2559.  
  2560.         case CMD_list:
  2561.                 i = curwin->w_p_list;
  2562.                 curwin->w_p_list = 1;
  2563.         case CMD_number:                /* :nu */
  2564.         case CMD_pound:                    /* :# */
  2565.         case CMD_print:                    /* :p */
  2566.                 for ( ;!got_int; mch_breakcheck())
  2567.                 {
  2568.                     print_line(line1,
  2569.                                (cmdidx == CMD_number || cmdidx == CMD_pound));
  2570.                     if (++line1 > line2)
  2571.                         break;
  2572.                     flushbuf();            /* show one line at a time */
  2573.                 }
  2574.                 setpcmark();
  2575.                 curwin->w_cursor.lnum = line2;    /* put cursor at last line */
  2576.  
  2577.                 if (cmdidx == CMD_list)
  2578.                     curwin->w_p_list = i;
  2579.  
  2580.                 break;
  2581.  
  2582.         case CMD_shell:
  2583.                 do_shell(NULL);
  2584.                 break;
  2585.  
  2586.         case CMD_sleep:
  2587.                 n = curwin->w_winpos + curwin->w_row - msg_scrolled;
  2588.                 if (n >= 0)
  2589.                 {
  2590.                     windgoto((int)n, curwin->w_col);
  2591.                     flushbuf();
  2592.                 }
  2593.                 mch_delay(line2 * 1000L, TRUE);
  2594.                 break;
  2595.  
  2596.         case CMD_stag:
  2597.                 postponed_split = TRUE;
  2598.                 /*FALLTHROUGH*/
  2599.         case CMD_tag:
  2600.                 do_tag(arg, 0, addr_count ? (int)line2 : 1, forceit);
  2601.                 break;
  2602.  
  2603.         case CMD_pop:
  2604.                 do_tag((char_u *)"", 1, addr_count ? (int)line2 : 1, forceit);
  2605.                 break;
  2606.  
  2607.         case CMD_tags:
  2608.                 do_tags();
  2609.                 break;
  2610.  
  2611.         case CMD_marks:
  2612.                 do_marks(arg);
  2613.                 break;
  2614.  
  2615.         case CMD_jumps:
  2616.                 do_jumps();
  2617.                 break;
  2618.  
  2619.         case CMD_ascii:
  2620.                 do_ascii();
  2621.                 break;
  2622.  
  2623.         case CMD_checkpath:
  2624.                 find_pattern_in_path(NULL, 0, FALSE, FALSE, CHECK_PATH, 1L,
  2625.                                       forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
  2626.                                             (linenr_t)1, (linenr_t)MAXLNUM);
  2627.                 break;
  2628.  
  2629.         case CMD_digraphs:
  2630. #ifdef DIGRAPHS
  2631.                 if (*arg)
  2632.                     putdigraph(arg);
  2633.                 else
  2634.                     listdigraphs();
  2635. #else
  2636.                 EMSG("No digraphs in this version");
  2637. #endif /* DIGRAPHS */
  2638.                 break;
  2639.  
  2640.         case CMD_set:
  2641.                 (void)do_set(arg);
  2642.                 break;
  2643.  
  2644.         case CMD_fixdel:
  2645.                 do_fixdel();
  2646.                 break;
  2647.  
  2648. #ifdef AUTOCMD
  2649.         case CMD_autocmd:
  2650.                 /*
  2651.                  * Disallow auto commands from .exrc and .vimrc in current
  2652.                  * directory for security reasons.
  2653.                  */
  2654.                 if (secure)
  2655.                 {
  2656.                     secure = 2;
  2657.                     errormsg = e_curdir;
  2658.                 }
  2659.                 else
  2660.                     do_autocmd(arg, forceit);    /* handle the auto commands */
  2661.                 break;
  2662.  
  2663.         case CMD_doautocmd:
  2664.                 do_doautocmd(arg);        /* apply the automatic commands */
  2665.                 do_modelines();
  2666.                 break;
  2667. #endif
  2668.  
  2669.         case CMD_abbreviate:
  2670.         case CMD_cabbrev:
  2671.         case CMD_iabbrev:
  2672.         case CMD_cnoreabbrev:
  2673.         case CMD_inoreabbrev:
  2674.         case CMD_noreabbrev:
  2675.         case CMD_unabbreviate:
  2676.         case CMD_cunabbrev:
  2677.         case CMD_iunabbrev:
  2678.                 i = ABBREV;
  2679.                 goto doabbr;        /* almost the same as mapping */
  2680.  
  2681.         case CMD_nmap:
  2682.         case CMD_vmap:
  2683.         case CMD_cmap:
  2684.         case CMD_imap:
  2685.         case CMD_map:
  2686.         case CMD_nnoremap:
  2687.         case CMD_vnoremap:
  2688.         case CMD_cnoremap:
  2689.         case CMD_inoremap:
  2690.         case CMD_noremap:
  2691.                 /*
  2692.                  * If we are sourcing .exrc or .vimrc in current directory we
  2693.                  * print the mappings for security reasons.
  2694.                  */
  2695.                 if (secure)
  2696.                 {
  2697.                     secure = 2;
  2698.                     msg_outtrans(cmd);
  2699.                     msg_outchar('\n');
  2700.                 }
  2701.         case CMD_nunmap:
  2702.         case CMD_vunmap:
  2703.         case CMD_cunmap:
  2704.         case CMD_iunmap:
  2705.         case CMD_unmap:
  2706.                 i = 0;
  2707. doabbr:
  2708.                 if (*cmd == 'c')            /* cmap, cunmap, cnoremap, etc. */
  2709.                 {
  2710.                     i += CMDLINE;
  2711.                     ++cmd;
  2712.                 }
  2713.                 else if (*cmd == 'i')        /* imap, iunmap, inoremap, etc. */
  2714.                 {
  2715.                     i += INSERT;
  2716.                     ++cmd;
  2717.                 }
  2718.                                             /* nmap, nunmap, nnoremap */
  2719.                 else if (*cmd == 'n' && *(cmd + 1) != 'o')
  2720.                 {
  2721.                     i += NORMAL;
  2722.                     ++cmd;
  2723.                 }
  2724.                 else if (*cmd == 'v')        /* vmap, vunmap, vnoremap */
  2725.                 {
  2726.                     i += VISUAL;
  2727.                     ++cmd;
  2728.                 }
  2729.                 else if (forceit || i)        /* map!, unmap!, noremap!, abbrev */
  2730.                     i += INSERT + CMDLINE;
  2731.                 else                        /* map, unmap, noremap */
  2732.                     i += NORMAL + VISUAL;
  2733.                 switch (do_map((*cmd == 'n') ? 2 : (*cmd == 'u'), arg, i))
  2734.                 {
  2735.                     case 1: emsg(e_invarg);
  2736.                             break;
  2737.                     case 2: emsg(e_nomap);
  2738.                             break;
  2739.                     case 3: emsg(e_ambmap);
  2740.                             break;
  2741.                 }
  2742.                 break;
  2743.  
  2744.         case CMD_mapclear:
  2745.         case CMD_imapclear:
  2746.         case CMD_nmapclear:
  2747.         case CMD_vmapclear:
  2748.         case CMD_cmapclear:
  2749.                 map_clear(*cmd, forceit, FALSE);
  2750.                 break;
  2751.  
  2752.         case CMD_abclear:
  2753.         case CMD_iabclear:
  2754.         case CMD_cabclear:
  2755.                 map_clear(*cmd, FALSE, TRUE);
  2756.                 break;
  2757.  
  2758. #ifdef USE_GUI
  2759.         case CMD_menu:        case CMD_noremenu:        case CMD_unmenu:
  2760.         case CMD_nmenu:        case CMD_nnoremenu:        case CMD_nunmenu:
  2761.         case CMD_vmenu:        case CMD_vnoremenu:        case CMD_vunmenu:
  2762.         case CMD_imenu:        case CMD_inoremenu:        case CMD_iunmenu:
  2763.         case CMD_cmenu:        case CMD_cnoremenu:        case CMD_cunmenu:
  2764.                 gui_do_menu(cmd, arg, forceit);
  2765.                 break;
  2766. #endif /* USE_GUI */
  2767.  
  2768.         case CMD_display:
  2769.         case CMD_registers:
  2770.                 do_dis(arg);        /* display buffer contents */
  2771.                 break;
  2772.  
  2773.         case CMD_help:
  2774.                 do_help(arg);
  2775.                 break;
  2776.  
  2777.         case CMD_version:
  2778.                 do_version(arg);
  2779.                 break;
  2780.  
  2781.         case CMD_winsize:                    /* obsolete command */
  2782.                 line1 = getdigits(&arg);
  2783.                 arg = skipwhite(arg);
  2784.                 line2 = getdigits(&arg);
  2785.                 set_winsize((int)line1, (int)line2, TRUE);
  2786.                 break;
  2787.  
  2788.         case CMD_delete:
  2789.         case CMD_yank:
  2790.         case CMD_rshift:
  2791.         case CMD_lshift:
  2792.                 yankbuffer = regname;
  2793.                 curbuf->b_op_start.lnum = line1;
  2794.                 curbuf->b_op_end.lnum = line2;
  2795.                 op_line_count = line2 - line1 + 1;
  2796.                 op_motion_type = MLINE;
  2797.                 if (cmdidx != CMD_yank)        /* set cursor position for undo */
  2798.                 {
  2799.                     setpcmark();
  2800.                     curwin->w_cursor.lnum = line1;
  2801.                     beginline(MAYBE);
  2802.                 }
  2803.                 switch (cmdidx)
  2804.                 {
  2805.                 case CMD_delete:
  2806.                     do_delete();
  2807.                     break;
  2808.                 case CMD_yank:
  2809.                     (void)do_yank(FALSE, TRUE);
  2810.                     break;
  2811. #ifdef RIGHTLEFT
  2812.                 case CMD_rshift:
  2813.                     do_shift(curwin->w_p_rl ? LSHIFT : RSHIFT, FALSE, amount);
  2814.                     break;
  2815.                 case CMD_lshift:
  2816.                     do_shift(curwin->w_p_rl ? RSHIFT : LSHIFT, FALSE, amount);
  2817.                     break;
  2818. #else
  2819.                 case CMD_rshift:
  2820.                     do_shift(RSHIFT, FALSE, amount);
  2821.                     break;
  2822.                 case CMD_lshift:
  2823.                     do_shift(LSHIFT, FALSE, amount);
  2824.                     break;
  2825. #endif
  2826.                 }
  2827.                 break;
  2828.  
  2829.         case CMD_put:
  2830.                 yankbuffer = regname;
  2831.                 /*
  2832.                  * ":0put" works like ":1put!".
  2833.                  */
  2834.                 if (line2 == 0)
  2835.                 {
  2836.                     line2 = 1;
  2837.                     forceit = TRUE;
  2838.                 }
  2839.                 curwin->w_cursor.lnum = line2;
  2840.                 do_put(forceit ? BACKWARD : FORWARD, -1L, FALSE);
  2841.                 break;
  2842.  
  2843.         case CMD_t:
  2844.         case CMD_copy:
  2845.         case CMD_move:
  2846.                 n = get_address(&arg);
  2847.                 if (arg == NULL)            /* error detected */
  2848.                 {
  2849.                     nextcomm = NULL;
  2850.                     break;
  2851.                 }
  2852.                 /*
  2853.                  * move or copy lines from 'line1'-'line2' to below line 'n'
  2854.                  */
  2855.                 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
  2856.                 {
  2857.                     emsg(e_invaddr);
  2858.                     break;
  2859.                 }
  2860.  
  2861.                 if (cmdidx == CMD_move)
  2862.                 {
  2863.                     if (do_move(line1, line2, n) == FAIL)
  2864.                         break;
  2865.                 }
  2866.                 else
  2867.                     do_copy(line1, line2, n);
  2868.                 u_clearline();
  2869.                 beginline(MAYBE);
  2870.                 updateScreen(NOT_VALID);
  2871.                 break;
  2872.  
  2873.         case CMD_and:            /* :& */
  2874.         case CMD_tilde:            /* :~ */
  2875.         case CMD_substitute:    /* :s */
  2876.                 do_sub(line1, line2, arg, &nextcomm,
  2877.                             cmdidx == CMD_substitute ? 0 :
  2878.                             cmdidx == CMD_and ? 1 : 2);
  2879.                 break;
  2880.  
  2881.         case CMD_join:
  2882.                 curwin->w_cursor.lnum = line1;
  2883.                 if (line1 == line2)
  2884.                 {
  2885.                     if (addr_count >= 2)    /* :2,2join does nothing */
  2886.                         break;
  2887.                     if (line2 == curbuf->b_ml.ml_line_count)
  2888.                     {
  2889.                         beep_flush();
  2890.                         break;
  2891.                     }
  2892.                     ++line2;
  2893.                 }
  2894.                 do_do_join(line2 - line1 + 1, !forceit, FALSE);
  2895.                 beginline(TRUE);
  2896.                 break;
  2897.  
  2898.         case CMD_global:
  2899.                 if (forceit)
  2900.                     *cmd = 'v';
  2901.         case CMD_vglobal:
  2902.                 do_glob(*cmd, line1, line2, arg);
  2903.                 break;
  2904.  
  2905.         case CMD_at:                /* :[addr]@r */
  2906.                 curwin->w_cursor.lnum = line2;
  2907.                                     /* put the register in mapbuf */
  2908.                 if (do_execbuf(*arg, TRUE,
  2909.                               vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL)
  2910.                     beep_flush();
  2911.                 else
  2912.                                     /* execute from the mapbuf */
  2913.                     while (vpeekc() == ':')
  2914.                     {
  2915.                         (void)vgetc();
  2916.                         (void)do_cmdline((char_u *)NULL, TRUE, TRUE);
  2917.                     }
  2918.                 break;
  2919.  
  2920.         case CMD_bang:
  2921.                 do_bang(addr_count, line1, line2, forceit, arg, TRUE, TRUE);
  2922.                 break;
  2923.  
  2924.         case CMD_undo:
  2925.                 u_undo(1);
  2926.                 break;
  2927.  
  2928.         case CMD_redo:
  2929.                 u_redo(1);
  2930.                 break;
  2931.  
  2932.         case CMD_source:
  2933.                 if (forceit)                    /* :so! read vi commands */
  2934.                     (void)openscript(arg);
  2935.                                                 /* :so read ex commands */
  2936.                 else if (do_source(arg, FALSE) == FAIL)
  2937.                     emsg2(e_notopen, arg);
  2938.                 break;
  2939.  
  2940. #ifdef VIMINFO
  2941.         case CMD_rviminfo:
  2942.                 p = p_viminfo;
  2943.                 if (*p_viminfo == NUL)
  2944.                     p_viminfo = (char_u *)"'100";
  2945.                 if (read_viminfo(arg, TRUE, TRUE, forceit) == FAIL)
  2946.                     EMSG("Cannot open viminfo file for reading");
  2947.                 p_viminfo = p;
  2948.                 break;
  2949.  
  2950.         case CMD_wviminfo:
  2951.                 p = p_viminfo;
  2952.                 if (*p_viminfo == NUL)
  2953.                     p_viminfo = (char_u *)"'100";
  2954.                 write_viminfo(arg, forceit);
  2955.                 p_viminfo = p;
  2956.                 break;
  2957. #endif /* VIMINFO */
  2958.  
  2959.         case CMD_mkvimrc:
  2960.                 if (*arg == NUL)
  2961.                     arg = (char_u *)VIMRC_FILE;
  2962.                 /*FALLTHROUGH*/
  2963.  
  2964.         case CMD_mkexrc:
  2965.                 {
  2966.                     FILE    *fd;
  2967.  
  2968.                     if (*arg == NUL)
  2969.                         arg = (char_u *)EXRC_FILE;
  2970. #ifdef UNIX
  2971.                     /* with Unix it is possible to open a directory */
  2972.                     if (mch_isdir(arg))
  2973.                     {
  2974.                         EMSG2("\"%s\" is a directory", arg);
  2975.                         break;
  2976.                     }
  2977. #endif
  2978.                     if (!forceit && vim_fexists(arg))
  2979.                     {
  2980.                         EMSG2("\"%s\" exists (use ! to override)", arg);
  2981.                         break;
  2982.                     }
  2983.  
  2984.                     if ((fd = fopen((char *)arg, WRITEBIN)) == NULL)
  2985.                     {
  2986.                         EMSG2("Cannot open \"%s\" for writing", arg);
  2987.                         break;
  2988.                     }
  2989.  
  2990.                     /* Write the version command for :mkvimrc */
  2991.                     if (cmdidx == CMD_mkvimrc)
  2992.                     {
  2993. #ifdef USE_CRNL
  2994.                         fprintf(fd, "version 4.0\r\n");
  2995. #else
  2996.                         fprintf(fd, "version 4.0\n");
  2997. #endif
  2998.                     }
  2999.  
  3000.                     if (makemap(fd) == FAIL || makeset(fd) == FAIL ||
  3001.                                                                    fclose(fd))
  3002.                         emsg(e_write);
  3003.                     break;
  3004.                 }
  3005.  
  3006.         case CMD_cc:
  3007.                     qf_jump(0, addr_count ? (int)line2 : 0, forceit);
  3008.                     break;
  3009.  
  3010.         case CMD_cfile:
  3011.                     if (*arg != NUL)
  3012.                     {
  3013.                         /*
  3014.                          * Great trick: Insert 'ef=' before arg.
  3015.                          * Always ok, because "cf " must be there.
  3016.                          */
  3017.                         arg -= 3;
  3018.                         arg[0] = 'e';
  3019.                         arg[1] = 'f';
  3020.                         arg[2] = '=';
  3021.                         (void)do_set(arg);
  3022.                     }
  3023.                     if (qf_init() == OK)
  3024.                         qf_jump(0, 0, forceit);        /* display first error */
  3025.                     break;
  3026.  
  3027.         case CMD_clist:
  3028.                     qf_list(forceit);
  3029.                     break;
  3030.  
  3031.         case CMD_cnext:
  3032.                     qf_jump(FORWARD, addr_count ? (int)line2 : 1, forceit);
  3033.                     break;
  3034.  
  3035.         case CMD_cNext:
  3036.         case CMD_cprevious:
  3037.                     qf_jump(BACKWARD, addr_count ? (int)line2 : 1, forceit);
  3038.                     break;
  3039.  
  3040.         case CMD_cquit:
  3041.                     getout(1);        /* this does not always pass on the exit
  3042.                                        code to the Manx compiler. why? */
  3043.  
  3044.         case CMD_mark:
  3045.         case CMD_k:
  3046.                     pos = curwin->w_cursor;            /* save curwin->w_cursor */
  3047.                     curwin->w_cursor.lnum = line2;
  3048.                     beginline(MAYBE);
  3049.                     (void)setmark(*arg);            /* set mark */
  3050.                     curwin->w_cursor = pos;            /* restore curwin->w_cursor */
  3051.                     break;
  3052.  
  3053.         case CMD_center:
  3054.         case CMD_right:
  3055.         case CMD_left:
  3056.                     do_align(line1, line2, atoi((char *)arg),
  3057.                             cmdidx == CMD_center ? 0 : cmdidx == CMD_right ? 1 : -1);
  3058.                     break;
  3059.  
  3060.         case CMD_retab:
  3061.                 n = getdigits(&arg);
  3062.                 do_retab(line1, line2, (int)n, forceit);
  3063.                 u_clearline();
  3064.                 updateScreen(NOT_VALID);
  3065.                 break;
  3066.  
  3067.         case CMD_make:
  3068.                 do_make(arg);
  3069.                 break;
  3070.  
  3071.                 /*
  3072.                  * :normal[!] {commands} - execute normal mode commands
  3073.                  * Mostly used for ":autocmd".
  3074.                  */
  3075.         case CMD_normal:
  3076.                 /*
  3077.                  * Stuff the argument into the typeahead buffer.
  3078.                  * Execute normal() until there is no more typeahead than
  3079.                  * there was before this command.
  3080.                  */
  3081.                 len = typelen;
  3082.                 ins_typebuf(arg, forceit ? -1 : 0, 0, TRUE);
  3083.                 while ((!stuff_empty() ||
  3084.                              (!typebuf_typed() && typelen > len)) && !got_int)
  3085.                 {
  3086.                     adjust_cursor();    /* put cursor on an existing line */
  3087.                     cursupdate();        /* update cursor position */
  3088.                     normal();     /* get and execute a normal mode command */
  3089.                 }
  3090.                 break;
  3091.  
  3092.         case CMD_isearch:
  3093.         case CMD_dsearch:
  3094.                 i = ACTION_SHOW;
  3095.                 goto find_pat;
  3096.  
  3097.         case CMD_ilist:
  3098.         case CMD_dlist:
  3099.                 i = ACTION_SHOW_ALL;
  3100.                 goto find_pat;
  3101.  
  3102.         case CMD_ijump:
  3103.         case CMD_djump:
  3104.                 i = ACTION_GOTO;
  3105.                 goto find_pat;
  3106.  
  3107.         case CMD_isplit:
  3108.         case CMD_dsplit:
  3109.                 i = ACTION_SPLIT;
  3110. find_pat:
  3111.                 {
  3112.                     int        whole = TRUE;
  3113.  
  3114.                     n = 1;
  3115.                     if (isdigit(*arg))        /* get count */
  3116.                     {
  3117.                         n = getdigits(&arg);
  3118.                         arg = skipwhite(arg);
  3119.                     }
  3120.                     if (*arg == '/')    /* Match regexp, not just whole words */
  3121.                     {
  3122.                         whole = FALSE;
  3123.                         ++arg;
  3124.                         for (p = arg; *p && *p != '/'; p++)
  3125.                             if (*p == '\\' && p[1] != NUL)
  3126.                                 p++;
  3127.                         if (*p)
  3128.                         {
  3129.                             *p++ = NUL;
  3130.                             p = skipwhite(p);
  3131.  
  3132.                             /* Check for trailing illegal characters */
  3133.                             if (*p && vim_strchr((char_u *)"|\"\n", *p) == NULL)
  3134.                                 errormsg = e_trailing;
  3135.                             else
  3136.                                 nextcomm = p;
  3137.                         }
  3138.                     }
  3139.                     find_pattern_in_path(arg, (int)STRLEN(arg), whole, !forceit,
  3140.                         *cmd == 'd' ?  FIND_DEFINE : FIND_ANY,
  3141.                         n, i, line1, line2);
  3142.                 }
  3143.                 break;
  3144.  
  3145.         default:
  3146.                     /* Normal illegal commands have already been handled */
  3147.                 errormsg = (char_u *)"Sorry, this command is not implemented";
  3148.     }
  3149.  
  3150.  
  3151. doend:
  3152.     if (errormsg != NULL)
  3153.     {
  3154.         emsg(errormsg);
  3155.         if (sourcing)
  3156.         {
  3157.             MSG_OUTSTR(": ");
  3158.             msg_outtrans(*cmdlinep);
  3159.         }
  3160.     }
  3161.     if (did_emsg)
  3162.         nextcomm = NULL;                /* cancel nextcomm at an error */
  3163.     if (nextcomm && *nextcomm == NUL)        /* not really a next command */
  3164.         nextcomm = NULL;
  3165.     return nextcomm;
  3166. }
  3167.  
  3168. /*
  3169.  * If 'autowrite' option set, try to write the file.
  3170.  *
  3171.  * return FAIL for failure, OK otherwise
  3172.  */
  3173.     int
  3174. autowrite(buf, forceit)
  3175.     BUF        *buf;
  3176.     int        forceit;
  3177. {
  3178.     if (!p_aw || (!forceit && buf->b_p_ro) || buf->b_filename == NULL)
  3179.         return FAIL;
  3180.     return buf_write_all(buf);
  3181. }
  3182.  
  3183. /*
  3184.  * flush all buffers, except the ones that are readonly
  3185.  */
  3186.     void
  3187. autowrite_all()
  3188. {
  3189.     BUF        *buf;
  3190.  
  3191.     if (!p_aw)
  3192.         return;
  3193.     for (buf = firstbuf; buf; buf = buf->b_next)
  3194.         if (buf->b_changed && !buf->b_p_ro)
  3195.         {
  3196.             (void)buf_write_all(buf);
  3197. #ifdef AUTOCMD
  3198.             /* an autocommand may have deleted the buffer */
  3199.             if (!buf_valid(buf))
  3200.                 buf = firstbuf;
  3201. #endif
  3202.         }
  3203. }
  3204.  
  3205. /*
  3206.  * flush the contents of a buffer, unless it has no file name
  3207.  *
  3208.  * return FAIL for failure, OK otherwise
  3209.  */
  3210.     static int
  3211. buf_write_all(buf)
  3212.     BUF        *buf;
  3213. {
  3214.     int        retval;
  3215. #ifdef AUTOCMD
  3216.     BUF        *old_curbuf = curbuf;
  3217. #endif
  3218.  
  3219.     retval = (buf_write(buf, buf->b_filename, buf->b_sfilename,
  3220.                                          (linenr_t)1, buf->b_ml.ml_line_count,
  3221.                                                   FALSE, FALSE, TRUE, FALSE));
  3222. #ifdef AUTOCMD
  3223.     if (curbuf != old_curbuf)
  3224.         MSG("Warning: Entered other buffer unexpectedly (check autocommands)");
  3225. #endif
  3226.     return retval;
  3227. }
  3228.  
  3229. /*
  3230.  * write current buffer to file 'fname'
  3231.  * if 'append' is TRUE, append to the file
  3232.  *
  3233.  * if *fname == NUL write to current file
  3234.  * if b_notedited is TRUE, check for overwriting current file
  3235.  *
  3236.  * return FAIL for failure, OK otherwise
  3237.  */
  3238.     static int
  3239. do_write(fname, line1, line2, append, forceit)
  3240.     char_u        *fname;
  3241.     linenr_t    line1, line2;
  3242.     int            append;
  3243.     int            forceit;
  3244. {
  3245.     int        other;
  3246.     char_u    *sfname = NULL;                /* init to shut up gcc */
  3247.  
  3248.     if (*fname == NUL)
  3249.         other = FALSE;
  3250.     else
  3251.     {
  3252.         sfname = fname;
  3253.         fname = fix_fname(fname);
  3254.         other = otherfile(fname);
  3255.     }
  3256.  
  3257.     /*
  3258.      * if we have a new file name put it in the list of alternate file names
  3259.      */
  3260.     if (other && vim_strchr(p_cpo, CPO_ALTWRITE) != NULL)
  3261.         setaltfname(fname, sfname, (linenr_t)1);
  3262.  
  3263.     /*
  3264.      * writing to the current file is not allowed in readonly mode
  3265.      * and need a file name
  3266.      */
  3267.     if (!other && (check_readonly(forceit) || check_fname() == FAIL))
  3268.         return FAIL;
  3269.  
  3270.     if (!other)
  3271.     {
  3272.         fname = curbuf->b_filename;
  3273.         sfname = curbuf->b_sfilename;
  3274.         /*
  3275.          * Not writing the whole file is only allowed with '!'.
  3276.          */
  3277.         if ((line1 != 1 || line2 != curbuf->b_ml.ml_line_count) &&
  3278.                                                  !forceit && !append && !p_wa)
  3279.         {
  3280.             EMSG("Use ! to write partial buffer");
  3281.             return FAIL;
  3282.         }
  3283.     }
  3284.  
  3285.     /*
  3286.      * write to other file or b_notedited set or not writing the whole file:
  3287.      * overwriting only allowed with '!'
  3288.      */
  3289.     if ((other || curbuf->b_notedited) && !forceit &&
  3290.                                        !append && !p_wa && vim_fexists(fname))
  3291.     {                                /* don't overwrite existing file */
  3292. #ifdef UNIX
  3293.             /* with UNIX it is possible to open a directory */
  3294.         if (mch_isdir(fname))
  3295.             EMSG2("\"%s\" is a directory", fname);
  3296.         else
  3297. #endif
  3298.             emsg(e_exists);
  3299.         return FAIL;
  3300.     }
  3301.     return (buf_write(curbuf, fname, sfname, line1, line2,
  3302.                                                 append, forceit, TRUE, FALSE));
  3303. }
  3304.  
  3305. /*
  3306.  * start editing a new file
  3307.  *
  3308.  *     fnum: file number; if zero use fname/sfname
  3309.  *    fname: the file name
  3310.  *                - full path if sfname used,
  3311.  *                - any file name if sfname is NULL
  3312.  *                - empty string to re-edit with the same file name (but may be
  3313.  *                    in a different directory)
  3314.  *                - NULL to start an empty buffer
  3315.  *   sfname: the short file name (or NULL)
  3316.  *  command: the command to be executed after loading the file
  3317.  *  newlnum: put cursor on this line number (if possible)
  3318.  *    flags:
  3319.  *         ECMD_HIDE: if TRUE don't free the current buffer
  3320.  *     ECMD_SET_HELP: set b_help flag of (new) buffer before opening file
  3321.  *       ECMD_OLDBUF: use existing buffer if it exists
  3322.  *      ECMD_FORCEIT: ! used for Ex command
  3323.  *
  3324.  * return FAIL for failure, OK otherwise
  3325.  */
  3326.     int
  3327. do_ecmd(fnum, fname, sfname, command, newlnum, flags)
  3328.     int            fnum;
  3329.     char_u        *fname;
  3330.     char_u        *sfname;
  3331.     char_u        *command;
  3332.     linenr_t    newlnum;
  3333.     int            flags;
  3334. {
  3335.     int            other_file;                /* TRUE if editing another file */
  3336.     int            oldbuf;                    /* TRUE if using existing buffer */
  3337. #ifdef AUTOCMD
  3338.     int            auto_buf = FALSE;        /* TRUE if autocommands brought us
  3339.                                            into the buffer unexpectedly */
  3340. #endif
  3341.     BUF            *buf;
  3342.  
  3343.     if (fnum != 0)
  3344.     {
  3345.         if (fnum == curbuf->b_fnum)        /* file is already being edited */
  3346.             return OK;                    /* nothing to do */
  3347.         other_file = TRUE;
  3348.     }
  3349.     else
  3350.     {
  3351.             /* if no short name given, use fname for short name */
  3352.         if (sfname == NULL)
  3353.             sfname = fname;
  3354. #ifdef USE_FNAME_CASE
  3355. # ifdef USE_LONG_FNAME
  3356.         if (USE_LONG_FNAME)
  3357. # endif
  3358.             fname_case(sfname);            /* set correct case for short filename */
  3359. #endif
  3360.  
  3361.         if (fname == NULL)
  3362.             other_file = TRUE;
  3363.                                             /* there is no file name */
  3364.         else if (*fname == NUL && curbuf->b_filename == NULL)
  3365.             other_file = FALSE;
  3366.         else
  3367.         {
  3368.             if (*fname == NUL)                /* re-edit with same file name */
  3369.             {
  3370.                 fname = curbuf->b_filename;
  3371.                 sfname = curbuf->b_sfilename;
  3372.             }
  3373.             fname = fix_fname(fname);        /* may expand to full path name */
  3374.             other_file = otherfile(fname);
  3375.         }
  3376.     }
  3377. /*
  3378.  * if the file was changed we may not be allowed to abandon it
  3379.  * - if we are going to re-edit the same file
  3380.  * - or if we are the only window on this file and if ECMD_HIDE is FALSE
  3381.  */
  3382.     if (((!other_file && !(flags & ECMD_OLDBUF)) ||
  3383.             (curbuf->b_nwindows == 1 && !(flags & ECMD_HIDE))) &&
  3384.             check_changed(curbuf, FALSE, !other_file, (flags & ECMD_FORCEIT)))
  3385.     {
  3386.         if (fnum == 0 && other_file && fname != NULL)
  3387.             setaltfname(fname, sfname, (linenr_t)1);
  3388.         return FAIL;
  3389.     }
  3390.  
  3391. /*
  3392.  * End Visual mode before switching to another buffer, so the text can be
  3393.  * copied into the GUI selection buffer.
  3394.  */
  3395.     if (VIsual_active)
  3396.         end_visual_mode();
  3397.  
  3398. /*
  3399.  * If we are starting to edit another file, open a (new) buffer.
  3400.  * Otherwise we re-use the current buffer.
  3401.  */
  3402.     if (other_file)
  3403.     {
  3404.         curwin->w_alt_fnum = curbuf->b_fnum;
  3405.         buflist_altlnum();
  3406.  
  3407.         if (fnum)
  3408.             buf = buflist_findnr(fnum);
  3409.         else
  3410.             buf = buflist_new(fname, sfname, 1L, TRUE);
  3411.         if (buf == NULL)
  3412.             return FAIL;
  3413.         if (buf->b_ml.ml_mfp == NULL)        /* no memfile yet */
  3414.         {
  3415.             oldbuf = FALSE;
  3416.             buf->b_nwindows = 0;
  3417.         }
  3418.         else                                /* existing memfile */
  3419.         {
  3420.             oldbuf = TRUE;
  3421.             buf_check_timestamp(buf);
  3422.         }
  3423.  
  3424.         /*
  3425.          * Make the (new) buffer the one used by the current window.
  3426.          * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE.
  3427.          * If the current buffer was empty and has no file name, curbuf
  3428.          * is returned by buflist_new().
  3429.          */
  3430.         if (buf != curbuf)
  3431.         {
  3432. #ifdef AUTOCMD
  3433.             BUF        *old_curbuf;
  3434.             char_u    *new_name = NULL;
  3435.  
  3436.             /*
  3437.              * Be careful: The autocommands may delete any buffer and change
  3438.              * the current buffer.
  3439.              * - If the buffer we are going to edit is deleted, give up.
  3440.              * - If we ended up in the new buffer already, need to skip a few
  3441.              *   things, set auto_buf.
  3442.              */
  3443.             old_curbuf = curbuf;
  3444.             if (buf->b_xfilename != NULL)
  3445.                 new_name = strsave(buf->b_xfilename);
  3446.             apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE);
  3447.             if (!buf_valid(buf))        /* new buffer has been deleted */
  3448.             {
  3449.                 EMSG2("Autocommands unexpectedly deleted new buffer %s",
  3450.                         new_name == NULL ? (char_u *)"" : new_name);
  3451.                 vim_free(new_name);
  3452.                 return FAIL;
  3453.             }
  3454.             vim_free(new_name);
  3455.             if (buf == curbuf)            /* already in new buffer */
  3456.                 auto_buf = TRUE;
  3457.             else
  3458.             {
  3459.                 if (curbuf == old_curbuf)
  3460. #endif
  3461.                     buf_copy_options(curbuf, buf, TRUE, FALSE);
  3462.                 close_buffer(curwin, curbuf, !(flags & ECMD_HIDE), FALSE);
  3463.                 curwin->w_buffer = buf;
  3464.                 curbuf = buf;
  3465.                 ++curbuf->b_nwindows;
  3466. #ifdef AUTOCMD
  3467.             }
  3468. #endif
  3469.         }
  3470.         else
  3471.             ++curbuf->b_nwindows;
  3472.  
  3473.         curwin->w_pcmark.lnum = 1;
  3474.         curwin->w_pcmark.col = 0;
  3475.     }
  3476.     else
  3477.     {
  3478.         if (check_fname() == FAIL)
  3479.             return FAIL;
  3480.         oldbuf = (flags & ECMD_OLDBUF);
  3481.     }
  3482.  
  3483. /*
  3484.  * If we get here we are sure to start editing
  3485.  */
  3486.         /* don't redraw until the cursor is in the right line */
  3487.     ++RedrawingDisabled;
  3488.     if (flags & ECMD_SET_HELP)
  3489.         curbuf->b_help = TRUE;
  3490.  
  3491. /*
  3492.  * other_file    oldbuf
  3493.  *    FALSE        FALSE        re-edit same file, buffer is re-used
  3494.  *    FALSE        TRUE        re-edit same file, nothing changes
  3495.  *  TRUE        FALSE        start editing new file, new buffer
  3496.  *  TRUE        TRUE        start editing in existing buffer (nothing to do)
  3497.  */
  3498.     if (!other_file && !oldbuf)            /* re-use the buffer */
  3499.     {
  3500.         if (newlnum == 0)
  3501.             newlnum = curwin->w_cursor.lnum;
  3502.         buf_freeall(curbuf);            /* free all things for buffer */
  3503.         buf_clear(curbuf);
  3504.         curbuf->b_op_start.lnum = 0;    /* clear '[ and '] marks */
  3505.         curbuf->b_op_end.lnum = 0;
  3506.     }
  3507.  
  3508.     /*
  3509.      * Reset cursor position, could be used by autocommands.
  3510.      */
  3511.     adjust_cursor();
  3512.  
  3513.     /*
  3514.      * Check if we are editing the w_arg_idx file in the argument list.
  3515.      */
  3516.     check_arg_idx();
  3517.  
  3518. #ifdef AUTOCMD
  3519.     if (!auto_buf)
  3520. #endif
  3521.     {
  3522.         /*
  3523.          * Set cursor and init window before reading the file and executing
  3524.          * autocommands.  This allows for the autocommands to position the
  3525.          * cursor.
  3526.          */
  3527.         win_init(curwin);
  3528.  
  3529.         /*
  3530.          * Careful: open_buffer() and apply_autocmds() may change the current
  3531.          * buffer and window.
  3532.          */
  3533.         if (!oldbuf)                        /* need to read the file */
  3534.             (void)open_buffer();
  3535. #ifdef AUTOCMD
  3536.         else
  3537.             apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE);
  3538.         check_arg_idx();
  3539. #endif
  3540.         maketitle();
  3541.     }
  3542.  
  3543.     if (command == NULL)
  3544.     {
  3545.         if (newlnum)
  3546.         {
  3547.             curwin->w_cursor.lnum = newlnum;
  3548.             check_cursor();
  3549.             beginline(MAYBE);
  3550.         }
  3551.         else
  3552.             beginline(TRUE);
  3553.     }
  3554.  
  3555.     /*
  3556.      * Did not read the file, need to show some info about the file.
  3557.      * Do this after setting the cursor.
  3558.      */
  3559.     if (oldbuf
  3560. #ifdef AUTOCMD
  3561.                 && !auto_buf
  3562. #endif
  3563.                             )
  3564.         fileinfo(did_cd, TRUE, FALSE);
  3565.  
  3566.     if (command != NULL)
  3567.         do_cmdline(command, TRUE, FALSE);
  3568.     --RedrawingDisabled;
  3569.     if (!skip_redraw)
  3570.         updateScreen(CURSUPD);            /* redraw now */
  3571.  
  3572.     if (p_im)
  3573.         need_start_insertmode = TRUE;
  3574.     return OK;
  3575. }
  3576.  
  3577. /*
  3578.  * get + command from ex argument
  3579.  */
  3580.     static char_u *
  3581. getargcmd(argp)
  3582.     char_u **argp;
  3583. {
  3584.     char_u *arg = *argp;
  3585.     char_u *command = NULL;
  3586.  
  3587.     if (*arg == '+')        /* +[command] */
  3588.     {
  3589.         ++arg;
  3590.         if (vim_isspace(*arg))
  3591.             command = (char_u *)"$";
  3592.         else
  3593.         {
  3594.             /*
  3595.              * should check for "\ " (but vi has a bug that prevents it to work)
  3596.              */
  3597.             command = arg;
  3598.             arg = skiptowhite(command);
  3599.             if (*arg)
  3600.                 *arg++ = NUL;    /* terminate command with NUL */
  3601.         }
  3602.         
  3603.         arg = skipwhite(arg);    /* skip over spaces */
  3604.         *argp = arg;
  3605.     }
  3606.     return command;
  3607. }
  3608.  
  3609. /*
  3610.  * Halve the number of backslashes in a file name argument.
  3611.  * For MS-DOS we only do this if the character after the backslash
  3612.  * is not a normal file character.
  3613.  * For Unix, when wildcards are going to be expanded, don't remove
  3614.  * backslashes before special characters.
  3615.  */
  3616.     static void
  3617. backslash_halve(p, expand_wildcards)
  3618.     char_u    *p;
  3619.     int        expand_wildcards;        /* going to expand wildcards later */
  3620. {
  3621.     for ( ; *p; ++p)
  3622.         if (is_backslash(p)
  3623. #if defined(UNIX) || defined(OS2)
  3624.                 && !(expand_wildcards &&
  3625.                         vim_strchr((char_u *)" *?[{`$\\", p[1]))
  3626. #endif
  3627.                                                )
  3628.             STRCPY(p, p + 1);
  3629. }
  3630.  
  3631.     static void
  3632. do_make(arg)
  3633.     char_u *arg;
  3634. {
  3635.     if (*p_ef == NUL)
  3636.     {
  3637.         EMSG("errorfile option not set");
  3638.         return;
  3639.     }
  3640.  
  3641.     autowrite_all();
  3642.     vim_remove(p_ef);
  3643.  
  3644.     /*
  3645.      * If 'shellpipe' empty: don't redirect to 'errorfile'.
  3646.      */
  3647.     if (*p_sp == NUL)
  3648.         sprintf((char *)IObuff, "%s%s%s", p_shq, arg, p_shq);
  3649.     else
  3650.         sprintf((char *)IObuff, "%s%s%s %s %s", p_shq, arg, p_shq, p_sp, p_ef);
  3651.     /*
  3652.      * Output a newline if there's something else than the :make command that
  3653.      * was typed (in which case the cursor is in column 0).
  3654.      */
  3655.     if (msg_col != 0)
  3656.         msg_outchar('\n');
  3657.     MSG_OUTSTR(":!");
  3658.     msg_outtrans(IObuff);                /* show what we are doing */
  3659.     do_shell(IObuff);
  3660.  
  3661. #ifdef AMIGA
  3662.     flushbuf();
  3663.                 /* read window status report and redraw before message */
  3664.     (void)char_avail();
  3665. #endif
  3666.  
  3667.     if (qf_init() == OK)
  3668.         qf_jump(0, 0, FALSE);            /* display first error */
  3669.  
  3670.     vim_remove(p_ef);
  3671. }
  3672.  
  3673. /* 
  3674.  * Redefine the argument list to 'str'.
  3675.  *
  3676.  * Return FAIL for failure, OK otherwise.
  3677.  */
  3678.     static int
  3679. do_arglist(str)
  3680.     char_u *str;
  3681. {
  3682.     int        new_count = 0;
  3683.     char_u    **new_files = NULL;
  3684.     int        exp_count;
  3685.     char_u    **exp_files;
  3686.     char_u    **t;
  3687.     char_u    *p;
  3688.     int        inquote;
  3689.     int        i;
  3690.  
  3691.     while (*str)
  3692.     {
  3693.         /*
  3694.          * create a new entry in new_files[]
  3695.          */
  3696.         t = (char_u **)lalloc((long_u)(sizeof(char_u *) * (new_count + 1)), TRUE);
  3697.         if (t != NULL)
  3698.             for (i = new_count; --i >= 0; )
  3699.                 t[i] = new_files[i];
  3700.         vim_free(new_files);
  3701.         if (t == NULL)
  3702.             return FAIL;
  3703.         new_files = t;
  3704.         new_files[new_count++] = str;
  3705.  
  3706.         /*
  3707.          * isolate one argument, taking quotes
  3708.          */
  3709.         inquote = FALSE;
  3710.         for (p = str; *str; ++str)
  3711.         {
  3712.             /*
  3713.              * for MSDOS et.al. a backslash is part of a file name.
  3714.              * Only skip ", space and tab.
  3715.              */
  3716.             if (is_backslash(str))
  3717.                 *p++ = *++str;
  3718.             else
  3719.             {
  3720.                 if (!inquote && vim_isspace(*str))
  3721.                     break;
  3722.                 if (*str == '"')
  3723.                     inquote ^= TRUE;
  3724.                 else
  3725.                     *p++ = *str;
  3726.             }
  3727.         }
  3728.         str = skipwhite(str);
  3729.         *p = NUL;
  3730.     }
  3731.     
  3732.     i = ExpandWildCards(new_count, new_files, &exp_count,
  3733.                                                 &exp_files, FALSE, TRUE);
  3734.     vim_free(new_files);
  3735.     if (i == FAIL)
  3736.         return FAIL;
  3737.     if (exp_count == 0)
  3738.     {
  3739.         emsg(e_nomatch);
  3740.         return FAIL;
  3741.     }
  3742.     if (arg_exp)                /* arg_files[] has been allocated, free it */
  3743.         FreeWild(arg_count, arg_files);
  3744.     else
  3745.         arg_exp = TRUE;
  3746.     arg_files = exp_files;
  3747.     arg_count = exp_count;
  3748.     arg_had_last = FALSE;
  3749.  
  3750.     /*
  3751.      * put all file names in the buffer list
  3752.      */
  3753.     for (i = 0; i < arg_count; ++i)
  3754.         (void)buflist_add(arg_files[i]);
  3755.  
  3756.     return OK;
  3757. }
  3758.  
  3759. /*
  3760.  * Return TRUE if "str" starts with a backslash that should be removed.
  3761.  * For MS-DOS, WIN32 and OS/2 this is only done when the character after the
  3762.  * backslash is not a normal file name character.
  3763.  * Although '$' is a valid filename character, we remove the backslash before
  3764.  * it, to be able to disginguish between a filename that starts with '$' and
  3765.  * the name of an environment variable.
  3766.  */
  3767.     static int
  3768. is_backslash(str)
  3769.     char_u    *str;
  3770. {
  3771. #ifdef BACKSLASH_IN_FILENAME
  3772.     return (str[0] == '\\' && (str[1] == '$' ||
  3773.             (str[1] != NUL && str[1] != '*' && str[1] != '?'
  3774.                         && !(isfilechar(str[1]) && str[1] != '\\'))));
  3775. #else
  3776.     return (str[0] == '\\' && str[1] != NUL);
  3777. #endif
  3778. }
  3779.  
  3780. /*
  3781.  * Check if we are editing the w_arg_idx file in the argument list.
  3782.  */
  3783.     void
  3784. check_arg_idx()
  3785. {
  3786.     int        t;
  3787.  
  3788.     if (arg_count > 1 && (curbuf->b_filename == NULL ||
  3789.                           curwin->w_arg_idx >= arg_count ||
  3790.                 (t = fullpathcmp(arg_files[curwin->w_arg_idx],
  3791.                            curbuf->b_filename)) == FPC_DIFF || t == FPC_DIFFX))
  3792.         curwin->w_arg_idx_invalid = TRUE;
  3793.     else
  3794.         curwin->w_arg_idx_invalid = FALSE;
  3795. }
  3796.  
  3797.     void
  3798. gotocmdline(clr)
  3799.     int                clr;
  3800. {
  3801.     msg_start();
  3802.     if (clr)                /* clear the bottom line(s) */
  3803.         msg_clr_eos();        /* will reset clear_cmdline */
  3804.     windgoto(cmdline_row, 0);
  3805. }
  3806.  
  3807.     static int
  3808. check_readonly(forceit)
  3809.     int        forceit;
  3810. {
  3811.     if (!forceit && curbuf->b_p_ro)
  3812.     {
  3813.         emsg(e_readonly);
  3814.         return TRUE;
  3815.     }
  3816.     return FALSE;
  3817. }
  3818.  
  3819. /*
  3820.  * return TRUE if buffer was changed and cannot be abandoned.
  3821.  */
  3822.     static int
  3823. check_changed(buf, checkaw, mult_win, forceit)
  3824.     BUF        *buf;
  3825.     int        checkaw;        /* do autowrite if buffer was changed */
  3826.     int        mult_win;        /* check also when several windows for the buffer */
  3827.     int        forceit;
  3828. {
  3829.     if (    !forceit &&
  3830.             buf->b_changed && (mult_win || buf->b_nwindows <= 1) &&
  3831.             (!checkaw || autowrite(buf, forceit) == FAIL))
  3832.     {
  3833.         emsg(e_nowrtmsg);
  3834.         return TRUE;
  3835.     }
  3836.     return FALSE;
  3837. }
  3838.  
  3839. /*
  3840.  * return TRUE if any buffer was changed and cannot be abandoned.
  3841.  * That changed buffer becomes the current buffer.
  3842.  */
  3843.     static int
  3844. check_changed_any()
  3845. {
  3846.     BUF        *buf;
  3847.     int        save;
  3848.  
  3849.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  3850.     {
  3851.         if (buf->b_changed)
  3852.         {
  3853.             /* There must be a wait_return for this message, do_buffer
  3854.              * will cause a redraw */
  3855.             exiting = FALSE;
  3856.             if (EMSG2("No write since last change for buffer \"%s\"",
  3857.                           buf->b_xfilename == NULL ? (char_u *)"No File" :
  3858.                                                         buf->b_xfilename))
  3859.             {
  3860.                 save = no_wait_return;
  3861.                 no_wait_return = FALSE;
  3862.                 wait_return(FALSE);
  3863.                 no_wait_return = save;
  3864.             }
  3865.             (void)do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
  3866.             return TRUE;
  3867.         }
  3868.     }
  3869.     return FALSE;
  3870. }
  3871.  
  3872. /*
  3873.  * return FAIL if there is no filename, OK if there is one
  3874.  * give error message for FAIL
  3875.  */
  3876.     int
  3877. check_fname()
  3878. {
  3879.     if (curbuf->b_filename == NULL)
  3880.     {
  3881.         emsg(e_noname);
  3882.         return FAIL;
  3883.     }
  3884.     return OK;
  3885. }
  3886.  
  3887. /*
  3888.  * - if there are more files to edit
  3889.  * - and this is the last window
  3890.  * - and forceit not used
  3891.  * - and not repeated twice on a row
  3892.  *      return FAIL and give error message if 'message' TRUE
  3893.  * return OK otherwise
  3894.  */
  3895.     static int
  3896. check_more(message, forceit)
  3897.     int message;            /* when FALSE check only, no messages */
  3898.     int forceit;
  3899. {
  3900.     if (!forceit && only_one_window() && arg_count > 1 && !arg_had_last &&
  3901.                                     quitmore == 0)
  3902.     {
  3903.         if (message)
  3904.         {
  3905.             EMSGN("%ld more files to edit", arg_count - curwin->w_arg_idx - 1);
  3906.             quitmore = 2;            /* next try to quit is allowed */
  3907.         }
  3908.         return FAIL;
  3909.     }
  3910.     return OK;
  3911. }
  3912.  
  3913. /*
  3914.  * try to abandon current file and edit a new or existing file
  3915.  * 'fnum' is the number of the file, if zero use fname/sfname
  3916.  *
  3917.  * return 1 for "normal" error, 2 for "not written" error, 0 for success
  3918.  * -1 for succesfully opening another file
  3919.  * 'lnum' is the line number for the cursor in the new file (if non-zero).
  3920.  */
  3921.     int
  3922. getfile(fnum, fname, sfname, setpm, lnum, forceit)
  3923.     int            fnum;
  3924.     char_u        *fname;
  3925.     char_u        *sfname;
  3926.     int            setpm;
  3927.     linenr_t    lnum;
  3928.     int            forceit;
  3929. {
  3930.     int other;
  3931.  
  3932.     if (fnum == 0)
  3933.     {
  3934.         fname_expand(&fname, &sfname);    /* make fname full path, set sfname */
  3935.         other = otherfile(fname);
  3936.     }
  3937.     else
  3938.         other = (fnum != curbuf->b_fnum);
  3939.  
  3940.     if (other)
  3941.         ++no_wait_return;            /* don't wait for autowrite message */
  3942.     if (other && !forceit && curbuf->b_nwindows == 1 &&
  3943.             !p_hid && curbuf->b_changed && autowrite(curbuf, forceit) == FAIL)
  3944.     {
  3945.         if (other)
  3946.             --no_wait_return;
  3947.         emsg(e_nowrtmsg);
  3948.         return 2;        /* file has been changed */
  3949.     }
  3950.     if (other)
  3951.         --no_wait_return;
  3952.     if (setpm)
  3953.         setpcmark();
  3954.     if (!other)
  3955.     {
  3956.         if (lnum != 0)
  3957.             curwin->w_cursor.lnum = lnum;
  3958.         check_cursor();
  3959.         beginline(MAYBE);
  3960.  
  3961.         return 0;        /* it's in the same file */
  3962.     }
  3963.     if (do_ecmd(fnum, fname, sfname, NULL, lnum,
  3964.                 (p_hid ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0)) == OK)
  3965.         return -1;        /* opened another file */
  3966.     return 1;            /* error encountered */
  3967. }
  3968.  
  3969. /*
  3970.  * vim_strncpy()
  3971.  *
  3972.  * This is here because strncpy() does not guarantee successful results when
  3973.  * the to and from strings overlap.  It is only currently called from nextwild()
  3974.  * which copies part of the command line to another part of the command line.
  3975.  * This produced garbage when expanding files etc in the middle of the command
  3976.  * line (on my terminal, anyway) -- webb.
  3977.  */
  3978.     static void
  3979. vim_strncpy(to, from, len)
  3980.     char_u *to;
  3981.     char_u *from;
  3982.     int len;
  3983. {
  3984.     int i;
  3985.  
  3986.     if (to <= from)
  3987.     {
  3988.         while (len-- && *from)
  3989.             *to++ = *from++;
  3990.         if (len >= 0)
  3991.             *to = *from;    /* Copy NUL */
  3992.     }
  3993.     else
  3994.     {
  3995.         for (i = 0; i < len; i++)
  3996.         {
  3997.             to++;
  3998.             if (*from++ == NUL)
  3999.             {
  4000.                 i++;
  4001.                 break;
  4002.             }
  4003.         }
  4004.         for (; i > 0; i--)
  4005.             *--to = *--from;
  4006.     }
  4007. }
  4008.  
  4009. /*
  4010.  * Return FALSE if this is not an appropriate context in which to do
  4011.  * completion of anything, & TRUE if it is (even if there are no matches).
  4012.  * For the caller, this means that the character is just passed through like a
  4013.  * normal character (instead of being expanded).  This allows :s/^I^D etc.
  4014.  */
  4015.     static int
  4016. nextwild(type)
  4017.     int        type;
  4018. {
  4019.     int        i;
  4020.     char_u    *p1;
  4021.     char_u    *p2;
  4022.     int        oldlen;
  4023.     int        difflen;
  4024.     int        v;
  4025.  
  4026.     if (cmd_numfiles == -1)
  4027.         set_expand_context(cmdfirstc, cmdbuff);
  4028.     if (expand_context == EXPAND_UNSUCCESSFUL)
  4029.     {
  4030.         beep_flush();
  4031.         return OK;    /* Something illegal on command line */
  4032.     }
  4033.     if (expand_context == EXPAND_NOTHING)
  4034.     {
  4035.         /* Caller can use the character as a normal char instead */
  4036.         return FAIL;
  4037.     }
  4038.     expand_interactively = TRUE;
  4039.  
  4040.     MSG_OUTSTR("...");        /* show that we are busy */
  4041.     flushbuf();
  4042.  
  4043.     i = expand_pattern - cmdbuff;
  4044.     oldlen = cmdpos - i;
  4045.  
  4046.     if (type == WILD_NEXT || type == WILD_PREV)
  4047.     {
  4048.         /*
  4049.          * Get next/previous match for a previous expanded pattern.
  4050.          */
  4051.         p2 = ExpandOne(NULL, NULL, 0, type);
  4052.     }
  4053.     else
  4054.     {
  4055.         /*
  4056.          * Translate string into pattern and expand it.
  4057.          */
  4058.         if ((p1 = addstar(&cmdbuff[i], oldlen)) == NULL)
  4059.             p2 = NULL;
  4060.         else
  4061.         {
  4062.             p2 = ExpandOne(p1, strnsave(&cmdbuff[i], oldlen),
  4063.                                                      WILD_HOME_REPLACE, type);
  4064.             vim_free(p1);
  4065.         }
  4066.     }
  4067.  
  4068.     if (p2 != NULL)
  4069.     {
  4070.         if (cmdlen + (difflen = STRLEN(p2) - oldlen) > cmdbufflen - 4)
  4071.             v = realloc_cmdbuff(cmdlen + difflen);
  4072.         else
  4073.             v = OK;
  4074.         if (v == OK)
  4075.         {
  4076.             vim_strncpy(&cmdbuff[cmdpos + difflen], &cmdbuff[cmdpos],
  4077.                     cmdlen - cmdpos);
  4078.             STRNCPY(&cmdbuff[i], p2, STRLEN(p2));
  4079.             cmdlen += difflen;
  4080.             cmdpos += difflen;
  4081.         }
  4082.         vim_free(p2);
  4083.     }
  4084.  
  4085.     redrawcmd();
  4086.     if (cmd_numfiles <= 0 && p2 == NULL)
  4087.         beep_flush();
  4088.     else if (cmd_numfiles == 1)
  4089.         (void)ExpandOne(NULL, NULL, 0, WILD_FREE);    /* free expanded pattern */
  4090.  
  4091.     expand_interactively = FALSE;            /* reset for next call */
  4092.     return OK;
  4093. }
  4094.  
  4095. #define MAXSUFLEN 30        /* maximum length of a file suffix */
  4096.  
  4097. /*
  4098.  * Do wildcard expansion on the string 'str'.
  4099.  * Return a pointer to alloced memory containing the new string.
  4100.  * Return NULL for failure.
  4101.  *
  4102.  * mode = WILD_FREE:        just free previously expanded matches
  4103.  * mode = WILD_EXPAND_FREE:    normal expansion, do not keep matches
  4104.  * mode = WILD_EXPAND_KEEP:    normal expansion, keep matches
  4105.  * mode = WILD_NEXT:        use next match in multiple match, wrap to first
  4106.  * mode = WILD_PREV:        use previous match in multiple match, wrap to first
  4107.  * mode = WILD_ALL:            return all matches concatenated
  4108.  * mode = WILD_LONGEST:        return longest matched part
  4109.  *
  4110.  * options = WILD_LIST_NOTFOUND:    list entries without a match
  4111.  * options = WILD_HOME_REPLACE:        do home_replace() for buffer names
  4112.  */
  4113.     char_u *
  4114. ExpandOne(str, orig, options, mode)
  4115.     char_u    *str;
  4116.     char_u    *orig;            /* original string which is expanded */
  4117.     int        options;
  4118.     int        mode;
  4119. {
  4120.     char_u        *ss = NULL;
  4121.     static char_u **cmd_files = NULL;    /* list of input files */
  4122.     static int    findex;
  4123.     static char_u *orig_save = NULL;    /* kept value of orig */
  4124.     int            i, j;
  4125.     int            non_suf_match;            /* number without matching suffix */
  4126.     long_u        len;
  4127.     char_u        *setsuf;
  4128.     int            fnamelen, setsuflen;
  4129.     char_u        suf_buf[MAXSUFLEN];
  4130.     char_u        *p;
  4131.  
  4132. /*
  4133.  * first handle the case of using an old match
  4134.  */
  4135.     if (mode == WILD_NEXT || mode == WILD_PREV)
  4136.     {
  4137.         if (cmd_numfiles > 0)
  4138.         {
  4139.             if (mode == WILD_PREV)
  4140.             {
  4141.                 if (findex == -1)
  4142.                     findex = cmd_numfiles;
  4143.                 --findex;
  4144.             }
  4145.             else    /* mode == WILD_NEXT */
  4146.                 ++findex;
  4147.  
  4148.             /*
  4149.              * When wrapping around, return the original string, set findex to
  4150.              * -1.
  4151.              */
  4152.             if (findex < 0)
  4153.             {
  4154.                 if (orig_save == NULL)
  4155.                     findex = cmd_numfiles - 1;
  4156.                 else
  4157.                     findex = -1;
  4158.             }
  4159.             if (findex >= cmd_numfiles)
  4160.             {
  4161.                 if (orig_save == NULL)
  4162.                     findex = 0;
  4163.                 else
  4164.                     findex = -1;
  4165.             }
  4166.             if (findex == -1)
  4167.                 return strsave(orig_save);
  4168.             return strsave(cmd_files[findex]);
  4169.         }
  4170.         else
  4171.             return NULL;
  4172.     }
  4173.  
  4174. /* free old names */
  4175.     if (cmd_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
  4176.     {
  4177.         FreeWild(cmd_numfiles, cmd_files);
  4178.         cmd_numfiles = -1;
  4179.         vim_free(orig_save);
  4180.         orig_save = NULL;
  4181.     }
  4182.     findex = 0;
  4183.  
  4184.     if (mode == WILD_FREE)        /* only release file name */
  4185.         return NULL;
  4186.  
  4187.     if (cmd_numfiles == -1)
  4188.     {
  4189.         vim_free(orig_save);
  4190.         orig_save = orig;
  4191.         if (ExpandFromContext(str, &cmd_numfiles, &cmd_files, FALSE,
  4192.                                                              options) == FAIL)
  4193.             /* error: do nothing */;
  4194.         else if (cmd_numfiles == 0)
  4195.         {
  4196.             if (!expand_interactively)
  4197.                 emsg(e_nomatch);
  4198.         }
  4199.         else
  4200.         {
  4201.             /*
  4202.              * May change home directory back to "~"
  4203.              */
  4204.             if (options & WILD_HOME_REPLACE)
  4205.                 tilde_replace(str, cmd_numfiles, cmd_files);
  4206.  
  4207.             /*
  4208.              * Insert backslashes into a file name before a space, \, %, # and
  4209.              * wildmatch characters, except '~'.
  4210.              */
  4211.             if (expand_interactively &&
  4212.                     (expand_context == EXPAND_FILES ||
  4213.                      expand_context == EXPAND_BUFFERS ||
  4214.                      expand_context == EXPAND_DIRECTORIES))
  4215.             {
  4216.                 for (i = 0; i < cmd_numfiles; ++i)
  4217.                 {
  4218.                     p = strsave_escaped(cmd_files[i],
  4219. #ifdef BACKSLASH_IN_FILENAME
  4220.                                                     (char_u *)" *?[{`$%#");
  4221. #else
  4222.                                                     (char_u *)" *?[{`$\\%#");
  4223. #endif
  4224.                     if (p != NULL)
  4225.                     {
  4226.                         vim_free(cmd_files[i]);
  4227.                         cmd_files[i] = p;
  4228.                     }
  4229.                 }
  4230.             }
  4231.  
  4232.             if (mode != WILD_ALL && mode != WILD_LONGEST)
  4233.             {
  4234.                 non_suf_match = 1;
  4235.                 if (cmd_numfiles > 1)    /* more than one match; check suffix */
  4236.                 {
  4237.                     non_suf_match = 0;
  4238.                     for (i = 0; i < cmd_numfiles; ++i)
  4239.                     {
  4240.                         fnamelen = STRLEN(cmd_files[i]);
  4241.                         setsuflen = 0;
  4242.                         for (setsuf = p_su; *setsuf; )
  4243.                         {
  4244.                             setsuflen = copy_option_part(&setsuf, suf_buf,
  4245.                                                               MAXSUFLEN, ".,");
  4246.                             if (fnamelen >= setsuflen && STRNCMP(suf_buf,
  4247.                                           cmd_files[i] + fnamelen - setsuflen,
  4248.                                                       (size_t)setsuflen) == 0)
  4249.                                 break;
  4250.                             setsuflen = 0;
  4251.                         }
  4252.                         if (setsuflen)        /* suffix matched: ignore file */
  4253.                             continue;
  4254.                         /*
  4255.                          * Move the name without matching suffix to the front
  4256.                          * of the list.  This makes CTRL-N work nice.
  4257.                          */
  4258.                         p = cmd_files[i];
  4259.                         for (j = i; j > non_suf_match; --j)
  4260.                             cmd_files[j] = cmd_files[j - 1];
  4261.                         cmd_files[non_suf_match++] = p;
  4262.                     }
  4263.                 }
  4264.                 if (non_suf_match != 1)
  4265.                 {
  4266.                     /* Can we ever get here unless it's while expanding
  4267.                      * interactively?  If not, we can get rid of this all
  4268.                      * together. Don't really want to wait for this message
  4269.                      * (and possibly have to hit return to continue!).
  4270.                      */
  4271.                     if (!expand_interactively)
  4272.                         emsg(e_toomany);
  4273.                     else
  4274.                         beep_flush();
  4275.                 }
  4276.                 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
  4277.                     ss = strsave(cmd_files[0]);
  4278.             }
  4279.         }
  4280.     }
  4281.  
  4282.     /* Find longest common part */
  4283.     if (mode == WILD_LONGEST && cmd_numfiles > 0)
  4284.     {
  4285.         for (len = 0; cmd_files[0][len]; ++len)
  4286.         {
  4287.             for (i = 0; i < cmd_numfiles; ++i)
  4288.             {
  4289. #ifdef CASE_INSENSITIVE_FILENAME
  4290.                 if ((expand_context == EXPAND_DIRECTORIES ||
  4291.                                              expand_context == EXPAND_FILES ||
  4292.                                           expand_context == EXPAND_BUFFERS) &&
  4293.                      toupper(cmd_files[i][len]) != toupper(cmd_files[0][len]))
  4294.                     break;
  4295.                 else
  4296. #endif
  4297.                 if (cmd_files[i][len] != cmd_files[0][len])
  4298.                     break;
  4299.             }
  4300.             if (i < cmd_numfiles)
  4301.             {
  4302.                 vim_beep();
  4303.                 break;
  4304.             }
  4305.         }
  4306.         ss = alloc((unsigned)len + 1);
  4307.         if (ss)
  4308.         {
  4309.             STRNCPY(ss, cmd_files[0], len);
  4310.             ss[len] = NUL;
  4311.         }
  4312.         findex = -1;                        /* next p_wc gets first one */
  4313.     }
  4314.  
  4315.     /* Concatenate all matching names */
  4316.     if (mode == WILD_ALL && cmd_numfiles > 0)
  4317.     {
  4318.         len = 0;
  4319.         for (i = 0; i < cmd_numfiles; ++i)
  4320.             len += STRLEN(cmd_files[i]) + 1;
  4321.         ss = lalloc(len, TRUE);
  4322.         if (ss)
  4323.         {
  4324.             *ss = NUL;
  4325.             for (i = 0; i < cmd_numfiles; ++i)
  4326.             {
  4327.                 STRCAT(ss, cmd_files[i]);
  4328.                 if (i != cmd_numfiles - 1)
  4329.                     STRCAT(ss, " ");
  4330.             }
  4331.         }
  4332.     }
  4333.  
  4334.     if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
  4335.     {
  4336.         FreeWild(cmd_numfiles, cmd_files);
  4337.         cmd_numfiles = -1;
  4338.     }
  4339.     return ss;
  4340. }
  4341.  
  4342. /*
  4343.  * For each file name in files[num_files]:
  4344.  * If 'orig_pat' starts with "~/", replace the home directory with "~".
  4345.  */
  4346.     void
  4347. tilde_replace(orig_pat, num_files, files)
  4348.     char_u    *orig_pat;
  4349.     int        num_files;
  4350.     char_u    **files;
  4351. {
  4352.     int        i;
  4353.     char_u    *p;
  4354.  
  4355.     if (orig_pat[0] == '~' && ispathsep(orig_pat[1]))
  4356.     {
  4357.         for (i = 0; i < num_files; ++i)
  4358.         {
  4359.             p = home_replace_save(NULL, files[i]);
  4360.             if (p != NULL)
  4361.             {
  4362.                 vim_free(files[i]);
  4363.                 files[i] = p;
  4364.             }
  4365.         }
  4366.     }
  4367. }
  4368.  
  4369. /*
  4370.  * show all matches for completion on the command line
  4371.  */
  4372.     static int
  4373. showmatches(buff)
  4374.     char_u *buff;
  4375. {
  4376.     char_u        *file_str;
  4377.     int            num_files;
  4378.     char_u        **files_found;
  4379.     int            i, j, k;
  4380.     int            maxlen;
  4381.     int            lines;
  4382.     int            columns;
  4383.     char_u        *p;
  4384.     int            lastlen;
  4385.  
  4386.     set_expand_context(cmdfirstc, cmdbuff);
  4387.     if (expand_context == EXPAND_UNSUCCESSFUL)
  4388.     {
  4389.         beep_flush();
  4390.         return OK;    /* Something illegal on command line */
  4391.     }
  4392.     if (expand_context == EXPAND_NOTHING)
  4393.     {
  4394.         /* Caller can use the character as a normal char instead */
  4395.         return FAIL;
  4396.     }
  4397.     expand_interactively = TRUE;
  4398.  
  4399.     /* add star to file name, or convert to regexp if not expanding files! */
  4400.     file_str = addstar(expand_pattern, (int)(buff + cmdpos - expand_pattern));
  4401.     if (file_str == NULL)
  4402.     {
  4403.         expand_interactively = FALSE;
  4404.         return OK;
  4405.     }
  4406.  
  4407.     msg_didany = FALSE;                    /* lines_left will be set */
  4408.     msg_start();                        /* prepare for paging */
  4409.     msg_outchar('\n');
  4410.     flushbuf();
  4411.     cmdline_row = msg_row;
  4412.     msg_didany = FALSE;                    /* lines_left will be set again */
  4413.     msg_start();                        /* prepare for paging */
  4414.  
  4415.     /* find all files that match the description */
  4416.     if (ExpandFromContext(file_str, &num_files, &files_found, FALSE, 0) == FAIL)
  4417.     {
  4418.         num_files = 0;
  4419.         files_found = (char_u **)"";
  4420.     }
  4421.  
  4422.     /* find the length of the longest file name */
  4423.     maxlen = 0;
  4424.     for (i = 0; i < num_files; ++i)
  4425.     {
  4426.         if (expand_context == EXPAND_FILES || expand_context == EXPAND_BUFFERS)
  4427.         {
  4428.             home_replace(NULL, files_found[i], NameBuff, MAXPATHL);
  4429.             j = strsize(NameBuff);
  4430.         }
  4431.         else
  4432.             j = strsize(files_found[i]);
  4433.         if (j > maxlen)
  4434.             maxlen = j;
  4435.     }
  4436.  
  4437.     /* compute the number of columns and lines for the listing */
  4438.     maxlen += 2;    /* two spaces between file names */
  4439.     columns = ((int)Columns + 2) / maxlen;
  4440.     if (columns < 1)
  4441.         columns = 1;
  4442.     lines = (num_files + columns - 1) / columns;
  4443.  
  4444.     (void)set_highlight('d');    /* find out highlight mode for directories */
  4445.  
  4446.     /* list the files line by line */
  4447.     for (i = 0; i < lines; ++i)
  4448.     {
  4449.         lastlen = 999;
  4450.         for (k = i; k < num_files; k += lines)
  4451.         {
  4452.             for (j = maxlen - lastlen; --j >= 0; )
  4453.                 msg_outchar(' ');
  4454.             if (expand_context == EXPAND_FILES ||
  4455.                                              expand_context == EXPAND_BUFFERS)
  4456.             {
  4457.                         /* highlight directories */
  4458.                 j = (mch_isdir(files_found[k]));
  4459.                 home_replace(NULL, files_found[k], NameBuff, MAXPATHL);
  4460.                 p = NameBuff;
  4461.             }
  4462.             else
  4463.             {
  4464.                 j = FALSE;
  4465.                 p = files_found[k];
  4466.             }
  4467.             if (j)
  4468.                 start_highlight();
  4469.             lastlen = msg_outtrans(p);
  4470.             if (j)
  4471.                 stop_highlight();
  4472.         }
  4473.         msg_outchar('\n');
  4474.         flushbuf();                    /* show one line at a time */
  4475.         if (got_int)
  4476.         {
  4477.             if (!global_busy)
  4478.                 got_int = FALSE;
  4479.             break;
  4480.         }
  4481.     }
  4482.     vim_free(file_str);
  4483.     FreeWild(num_files, files_found);
  4484.  
  4485. /*
  4486.  * we redraw the command below the lines that we have just listed
  4487.  * This is a bit tricky, but it saves a lot of screen updating.
  4488.  */
  4489.     cmdline_row = msg_row;        /* will put it back later */
  4490.  
  4491.     expand_interactively = FALSE;
  4492.     return OK;
  4493. }
  4494.  
  4495. /*
  4496.  * Prepare a string for expansion.
  4497.  * When expanding file names:  The string will be used with ExpandWildCards().
  4498.  * Copy the file name into allocated memory and add a '*' at the end.
  4499.  * When expanding other names:  The string will be used with regcomp().  Copy
  4500.  * the name into allocated memory and add ".*" at the end.
  4501.  */
  4502.     char_u *
  4503. addstar(fname, len)
  4504.     char_u    *fname;
  4505.     int        len;
  4506. {
  4507.     char_u    *retval;
  4508.     int        i, j;
  4509.     int        new_len;
  4510.     char_u    *tail;
  4511.  
  4512.     if (expand_interactively && expand_context != EXPAND_FILES &&
  4513.                                          expand_context != EXPAND_DIRECTORIES)
  4514.     {
  4515.         /*
  4516.          * Matching will be done internally (on something other than files).
  4517.          * So we convert the file-matching-type wildcards into our kind for
  4518.          * use with vim_regcomp().  First work out how long it will be:
  4519.          */
  4520.  
  4521.         /* for help tags the translation is done in find_help_tags() */
  4522.         if (expand_context == EXPAND_HELP)
  4523.             retval = strnsave(fname, len);
  4524.         else
  4525.         {
  4526.             new_len = len + 2;            /* +2 for '^' at start, NUL at end */
  4527.             for (i = 0; i < len; i++)
  4528.             {
  4529.                 if (fname[i] == '*' || fname[i] == '~')
  4530.                     new_len++;            /* '*' needs to be replaced by ".*"
  4531.                                            '~' needs to be replaced by "\~" */
  4532.  
  4533.                 /* Buffer names are like file names.  "." should be literal */
  4534.                 if (expand_context == EXPAND_BUFFERS && fname[i] == '.')
  4535.                     new_len++;            /* "." becomes "\." */
  4536.             }
  4537.             retval = alloc(new_len);
  4538.             if (retval != NULL)
  4539.             {
  4540.                 retval[0] = '^';
  4541.                 j = 1;
  4542.                 for (i = 0; i < len; i++, j++)
  4543.                 {
  4544.                     if (fname[i] == '\\' && ++i == len)    /* skip backslash */
  4545.                         break;
  4546.  
  4547.                     switch (fname[i])
  4548.                     {
  4549.                         case '*':    retval[j++] = '.';
  4550.                                     break;
  4551.                         case '~':    retval[j++] = '\\';
  4552.                                     break;
  4553.                         case '?':    retval[j] = '.';
  4554.                                     continue;
  4555.                         case '.':    if (expand_context == EXPAND_BUFFERS)
  4556.                                         retval[j++] = '\\';
  4557.                                     break;
  4558.                     }
  4559.                     retval[j] = fname[i];
  4560.                 }
  4561.                 retval[j] = NUL;
  4562.             }
  4563.         }
  4564.     }
  4565.     else
  4566.     {
  4567.         retval = alloc(len + 4);
  4568.         if (retval != NULL)
  4569.         {
  4570.             STRNCPY(retval, fname, len);
  4571.             retval[len] = NUL;
  4572.             backslash_halve(retval, TRUE);        /* remove some backslashes */
  4573.             len = STRLEN(retval);
  4574.  
  4575.             /*
  4576.              * Don't add a star to ~, ~user, $var or `cmd`.
  4577.              * ~ would be at the start of the tail.
  4578.              * $ could be anywhere in the tail.
  4579.              * ` could be anywhere in the file name.
  4580.              */
  4581.             tail = gettail(retval);
  4582.             if (*tail != '~' && vim_strchr(tail, '$') == NULL
  4583.                                            && vim_strchr(retval, '`') == NULL)
  4584.             {
  4585. #ifdef MSDOS
  4586.                 /*
  4587.                  * if there is no dot in the file name, add "*.*" instead of
  4588.                  * "*".
  4589.                  */
  4590.                 for (i = len - 1; i >= 0; --i)
  4591.                     if (vim_strchr((char_u *)".\\/:", retval[i]) != NULL)
  4592.                         break;
  4593.                 if (i < 0 || retval[i] != '.')
  4594.                 {
  4595.                     retval[len++] = '*';
  4596.                     retval[len++] = '.';
  4597.                 }
  4598. #endif
  4599.                 retval[len++] = '*';
  4600.             }
  4601.             retval[len] = NUL;
  4602.         }
  4603.     }
  4604.     return retval;
  4605. }
  4606.  
  4607. /*
  4608.  * do_source: read the file "fname" and execute its lines as EX commands
  4609.  *
  4610.  * This function may be called recursively!
  4611.  *
  4612.  * return FAIL if file could not be opened, OK otherwise
  4613.  */
  4614.     int
  4615. do_source(fname, check_other)
  4616.     register char_u *fname;
  4617.     int                check_other;        /* check for .vimrc and _vimrc */
  4618. {
  4619.     register FILE    *fp;
  4620.     register int    len;
  4621. #ifdef USE_CRNL
  4622.     int                has_cr;
  4623.     int                textmode = -1;    /* -1 = unknown, 0 = NL, 1 = CR-NL */
  4624.     int                error = FALSE;
  4625. #endif
  4626.                                     /* use NameBuff for expanded name */
  4627.     expand_env(fname, NameBuff, MAXPATHL);
  4628.     fp = fopen((char *)NameBuff, READBIN);
  4629.     if (fp == NULL && check_other)
  4630.     {
  4631.         /*
  4632.          * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa
  4633.          * (if applicable)
  4634.          */
  4635.         len = STRLEN(NameBuff);
  4636.         if (((len > 6 && ispathsep(NameBuff[len - 7])) || len == 6) &&
  4637.                      (NameBuff[len - 6] == '.' || NameBuff[len - 6] == '_') &&
  4638.                                   (STRCMP(&NameBuff[len - 5], "vimrc") == 0))
  4639.         {
  4640.             if (NameBuff[len - 6] == '_')
  4641.                 NameBuff[len - 6] = '.';
  4642.             else
  4643.                 NameBuff[len - 6] = '_';
  4644.             fp = fopen((char *)NameBuff, READBIN);
  4645.         }
  4646.     }
  4647.  
  4648.     if (fp == NULL)
  4649.         return FAIL;
  4650.  
  4651. #ifdef USE_CRNL
  4652.         /* no automatic textmode: Set default to CR-NL */
  4653.     if (!p_ta)
  4654.         textmode = 1;
  4655. #endif
  4656.     sourcing_name = fname;
  4657.     sourcing_lnum = 1;
  4658. #ifdef SLEEP_IN_EMSG
  4659.     ++dont_sleep;            /* don't call sleep() in emsg() */
  4660. #endif
  4661.     len = 0;
  4662.     while (fgets((char *)IObuff + len, IOSIZE - len, fp) != NULL && !got_int)
  4663.     {
  4664.         len = STRLEN(IObuff) - 1;
  4665. #ifdef USE_CRNL
  4666.         /* Ignore a trailing CTRL-Z, when in textmode */
  4667.         if (len == 0 && textmode == 1 && IObuff[0] == Ctrl('Z'))
  4668.             break;
  4669. #endif
  4670.         if (len >= 0 && IObuff[len] == '\n')    /* remove trailing newline */
  4671.         {
  4672. #ifdef USE_CRNL
  4673.             has_cr = (len > 0 && IObuff[len - 1] == '\r');
  4674.             if (textmode == -1)
  4675.             {
  4676.                 if (has_cr)
  4677.                     textmode = 1;
  4678.                 else
  4679.                     textmode = 0;
  4680.             }
  4681.  
  4682.             if (textmode)
  4683.             {
  4684.                 if (has_cr)         /* remove trailing CR */
  4685.                     --len;
  4686.                 else        /* lines like ":map xx yy^M" will have failed */
  4687.                 {
  4688.                     if (!error)
  4689.                         EMSG("Warning: Wrong line separator, ^M may be missing");
  4690.                     error = TRUE;
  4691.                     textmode = 0;
  4692.                 }
  4693.             }
  4694. #endif
  4695.                 /* escaped newline, read more */
  4696.             if (len > 0 && len < IOSIZE && IObuff[len - 1] == Ctrl('V'))
  4697.             {
  4698.                 IObuff[len - 1] = '\n';        /* remove CTRL-V */
  4699.                 ++sourcing_lnum;
  4700.                 continue;
  4701.             }
  4702.             IObuff[len] = NUL;
  4703.         }
  4704.             /* check for ^C here, so recursive :so will be broken */
  4705.         mch_breakcheck();
  4706.         do_cmdline(IObuff, TRUE, TRUE);
  4707.         len = 0;
  4708.         ++sourcing_lnum;
  4709.     }
  4710.     fclose(fp);
  4711.     if (got_int)
  4712.         emsg(e_interr);
  4713. #ifdef SLEEP_IN_EMSG
  4714.     --dont_sleep;
  4715. #endif
  4716.     sourcing_name = NULL;
  4717.     sourcing_lnum = 0;
  4718.     return OK;
  4719. }
  4720.  
  4721. /*
  4722.  * get a single EX address
  4723.  * 
  4724.  * Set ptr to the next character after the part that was interpreted.
  4725.  * Set ptr to NULL when an error is encountered.
  4726.  *
  4727.  * Return MAXLNUM when no Ex address was found.
  4728.  */
  4729.     static linenr_t
  4730. get_address(ptr)
  4731.     char_u        **ptr;
  4732. {
  4733.     int            c;
  4734.     int            i;
  4735.     long        n;
  4736.     char_u      *cmd;
  4737.     FPOS        pos;
  4738.     FPOS        *fp;
  4739.     linenr_t    lnum;
  4740.  
  4741.     cmd = skipwhite(*ptr);
  4742.     lnum = MAXLNUM;
  4743.     do
  4744.     {
  4745.         switch (*cmd)
  4746.         {
  4747.             case '.':                         /* '.' - Cursor position */
  4748.                         ++cmd;
  4749.                         lnum = curwin->w_cursor.lnum;
  4750.                         break;
  4751.  
  4752.             case '$':                         /* '$' - last line */
  4753.                         ++cmd;
  4754.                         lnum = curbuf->b_ml.ml_line_count;
  4755.                         break;
  4756.  
  4757.             case '\'':                         /* ''' - mark */
  4758.                         if (*++cmd == NUL || (check_mark(
  4759.                                         fp = getmark(*cmd++, FALSE)) == FAIL))
  4760.                             goto error;
  4761.                         lnum = fp->lnum;
  4762.                         break;
  4763.  
  4764.             case '/':
  4765.             case '?':                        /* '/' or '?' - search */
  4766.                         c = *cmd++;
  4767.                         pos = curwin->w_cursor;        /* save curwin->w_cursor */
  4768.                         /*
  4769.                          * When '/' or '?' follows another address, start from
  4770.                          * there.
  4771.                          */
  4772.                         if (lnum != MAXLNUM)
  4773.                             curwin->w_cursor.lnum = lnum;
  4774.                         /*
  4775.                          * Start a forward search at the end of the line.
  4776.                          * Start a backward search at the start of the line.
  4777.                          * This makes sure we never match in the current line,
  4778.                          * and can match anywhere in the next/previous line.
  4779.                          */
  4780.                         if (c == '/')
  4781.                             curwin->w_cursor.col = MAXCOL;
  4782.                         else
  4783.                             curwin->w_cursor.col = 0;
  4784.                         searchcmdlen = 0;
  4785.                         if (!do_search(c, cmd, 1L,
  4786.                                       SEARCH_HIS + SEARCH_MSG + SEARCH_START))
  4787.                         {
  4788.                             cmd = NULL;
  4789.                             curwin->w_cursor = pos;
  4790.                             goto error;
  4791.                         }
  4792.                         lnum = curwin->w_cursor.lnum;
  4793.                         curwin->w_cursor = pos;
  4794.                                             /* adjust command string pointer */
  4795.                         cmd += searchcmdlen;
  4796.                         break;
  4797.  
  4798.             case '\\':                /* "\?", "\/" or "\&", repeat search */
  4799.                         ++cmd;
  4800.                         if (*cmd == '&')
  4801.                             i = RE_SUBST;
  4802.                         else if (*cmd == '?' || *cmd == '/')
  4803.                             i = RE_SEARCH;
  4804.                         else
  4805.                         {
  4806.                             emsg(e_backslash);
  4807.                             cmd = NULL;
  4808.                             goto error;
  4809.                         }
  4810.  
  4811.                         /*
  4812.                          * When search follows another address, start from
  4813.                          * there.
  4814.                          */
  4815.                         if (lnum != MAXLNUM)
  4816.                             pos.lnum = lnum;
  4817.                         else
  4818.                             pos.lnum = curwin->w_cursor.lnum;
  4819.  
  4820.                         /*
  4821.                          * Start the search just like for the above do_search().
  4822.                          */
  4823.                         if (*cmd != '?')
  4824.                             pos.col = MAXCOL;
  4825.                         else        
  4826.                             pos.col = 0;
  4827.                         if (searchit(&pos, *cmd == '?' ? BACKWARD : FORWARD,
  4828.                                                              (char_u *)"", 1L,
  4829.                                           SEARCH_MSG + SEARCH_START, i) == OK)
  4830.                             lnum = pos.lnum;
  4831.                         else
  4832.                         {
  4833.                             cmd = NULL;
  4834.                             goto error;
  4835.                         }
  4836.                         ++cmd;
  4837.                         break;
  4838.  
  4839.             default:
  4840.                         if (isdigit(*cmd))        /* absolute line number */
  4841.                             lnum = getdigits(&cmd);
  4842.         }
  4843.         
  4844.         for (;;)
  4845.         {
  4846.             cmd = skipwhite(cmd);
  4847.             if (*cmd != '-' && *cmd != '+' && !isdigit(*cmd))
  4848.                 break;
  4849.  
  4850.             if (lnum == MAXLNUM)
  4851.                 lnum = curwin->w_cursor.lnum;    /* "+1" is same as ".+1" */
  4852.             if (isdigit(*cmd))
  4853.                 i = '+';                /* "number" is same as "+number" */
  4854.             else
  4855.                 i = *cmd++;
  4856.             if (!isdigit(*cmd))            /* '+' is '+1', but '+0' is not '+1' */
  4857.                 n = 1;
  4858.             else 
  4859.                 n = getdigits(&cmd);
  4860.             if (i == '-')
  4861.                 lnum -= n;
  4862.             else
  4863.                 lnum += n;
  4864.         }
  4865.     } while (*cmd == '/' || *cmd == '?');
  4866.  
  4867. error:
  4868.     *ptr = cmd;
  4869.     return lnum;
  4870. }
  4871.  
  4872.  
  4873. /*
  4874.  * Must parse the command line so far to work out what context we are in.
  4875.  * Completion can then be done based on that context.
  4876.  * This routine sets two global variables:
  4877.  *    char_u *expand_pattern    The start of the pattern to be expanded within
  4878.  *                                the command line (ends at the cursor).
  4879.  *    int expand_context        The type of thing to expand.  Will be one of:
  4880.  *
  4881.  *    EXPAND_UNSUCCESSFUL        Used sometimes when there is something illegal on
  4882.  *                            the command line, like an unknown command.  Caller
  4883.  *                            should beep.
  4884.  *    EXPAND_NOTHING            Unrecognised context for completion, use char like
  4885.  *                            a normal char, rather than for completion.  eg
  4886.  *                            :s/^I/
  4887.  *    EXPAND_COMMANDS            Cursor is still touching the command, so complete
  4888.  *                            it.
  4889.  *    EXPAND_BUFFERS            Complete file names for :buf and :sbuf commands.
  4890.  *    EXPAND_FILES            After command with XFILE set, or after setting
  4891.  *                              with P_EXPAND set.  eg :e ^I, :w>>^I
  4892.  *    EXPAND_DIRECTORIES        In some cases this is used instead of the latter
  4893.  *                              when we know only directories are of interest.  eg
  4894.  *                              :set dir=^I
  4895.  *    EXPAND_SETTINGS            Complete variable names.  eg :set d^I
  4896.  *    EXPAND_BOOL_SETTINGS    Complete boolean variables only,  eg :set no^I
  4897.  *    EXPAND_TAGS                Complete tags from the files in p_tags.  eg :ta a^I
  4898.  *    EXPAND_HELP                Complete tags from the file 'helpfile'/vim_tags
  4899.  *    EXPAND_EVENTS            Complete event names
  4900.  *
  4901.  * -- webb.
  4902.  */
  4903.     static void
  4904. set_expand_context(firstc, buff)
  4905.     int            firstc;     /* either ':', '/', or '?' */
  4906.     char_u        *buff;         /* buffer for command string */
  4907. {
  4908.     char_u        *nextcomm;
  4909.     char_u        old_char;
  4910.  
  4911.     old_char = cmdbuff[cmdpos];
  4912.     cmdbuff[cmdpos] = NUL;
  4913.     nextcomm = buff;
  4914.     while (nextcomm != NULL)
  4915.         nextcomm = set_one_cmd_context(firstc, nextcomm);
  4916.     cmdbuff[cmdpos] = old_char;
  4917. }
  4918.  
  4919. /*
  4920.  * This is all pretty much copied from do_one_cmd(), with all the extra stuff
  4921.  * we don't need/want deleted.  Maybe this could be done better if we didn't
  4922.  * repeat all this stuff.  The only problem is that they may not stay perfectly
  4923.  * compatible with each other, but then the command line syntax probably won't
  4924.  * change that much -- webb.
  4925.  */
  4926.     static char_u *
  4927. set_one_cmd_context(firstc, buff)
  4928.     int            firstc;     /* either ':', '/', or '?' */
  4929.     char_u        *buff;         /* buffer for command string */
  4930. {
  4931.     char_u                *p;
  4932.     char_u                *cmd, *arg;
  4933.     int                 i;
  4934.     int                    cmdidx;
  4935.     long                argt;
  4936.     char_u                delim;
  4937.     int                    forceit = FALSE;
  4938.     int                    usefilter = FALSE;    /* filter instead of file name */
  4939.  
  4940.     expand_pattern = buff;
  4941.     if (firstc != ':')
  4942.     {
  4943.         expand_context = EXPAND_NOTHING;
  4944.         return NULL;
  4945.     }
  4946.     expand_context = EXPAND_COMMANDS;    /* Default until we get past command */
  4947.  
  4948. /*
  4949.  * 2. skip comment lines and leading space, colons or bars
  4950.  */
  4951.     for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
  4952.         ;
  4953.     expand_pattern = cmd;
  4954.  
  4955.     if (*cmd == NUL)
  4956.         return NULL;
  4957.     if (*cmd == '"')        /* ignore comment lines */
  4958.     {
  4959.         expand_context = EXPAND_NOTHING;
  4960.         return NULL;
  4961.     }
  4962.  
  4963. /*
  4964.  * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
  4965.  */
  4966.     /* 
  4967.      * Backslashed delimiters after / or ? will be skipped, and commands will
  4968.      * not be expanded between /'s and ?'s or after "'". -- webb
  4969.      */
  4970.     while (*cmd != NUL && (vim_isspace(*cmd) || isdigit(*cmd) ||
  4971.                             vim_strchr((char_u *)".$%'/?-+,;", *cmd) != NULL))
  4972.     {
  4973.         if (*cmd == '\'')
  4974.         {
  4975.             if (*++cmd == NUL)
  4976.                 expand_context = EXPAND_NOTHING;
  4977.         }
  4978.         else if (*cmd == '/' || *cmd == '?')
  4979.         {
  4980.             delim = *cmd++;
  4981.             while (*cmd != NUL && *cmd != delim)
  4982.                 if (*cmd++ == '\\' && *cmd != NUL)
  4983.                     ++cmd;
  4984.             if (*cmd == NUL)
  4985.                 expand_context = EXPAND_NOTHING;
  4986.         }
  4987.         if (*cmd != NUL)
  4988.             ++cmd;
  4989.     }
  4990.  
  4991. /*
  4992.  * 4. parse command
  4993.  */
  4994.  
  4995.     cmd = skipwhite(cmd);
  4996.     expand_pattern = cmd;
  4997.     if (*cmd == NUL)
  4998.         return NULL;
  4999.     if (*cmd == '"')
  5000.     {
  5001.         expand_context = EXPAND_NOTHING;
  5002.         return NULL;
  5003.     }
  5004.  
  5005.     if (*cmd == '|' || *cmd == '\n')
  5006.         return cmd + 1;                    /* There's another command */
  5007.  
  5008.     /*
  5009.      * Isolate the command and search for it in the command table.
  5010.      * Exeptions:
  5011.      * - the 'k' command can directly be followed by any character.
  5012.      * - the 's' command can be followed directly by 'c', 'g' or 'r'
  5013.      */
  5014.     if (*cmd == 'k')
  5015.     {
  5016.         cmdidx = CMD_k;
  5017.         p = cmd + 1;
  5018.     }
  5019.     else
  5020.     {
  5021.         p = cmd;
  5022.         while (isalpha(*p) || *p == '*')    /* Allow * wild card */
  5023.             ++p;
  5024.             /* check for non-alpha command */
  5025.         if (p == cmd && vim_strchr((char_u *)"@!=><&~#", *p) != NULL)
  5026.             ++p;
  5027.         i = (int)(p - cmd);
  5028.  
  5029.         if (i == 0)
  5030.         {
  5031.             expand_context = EXPAND_UNSUCCESSFUL;
  5032.             return NULL;
  5033.         }
  5034.         for (cmdidx = 0; cmdidx < CMD_SIZE; ++cmdidx)
  5035.             if (STRNCMP(cmdnames[cmdidx].cmd_name, cmd, (size_t)i) == 0)
  5036.                 break;
  5037.     }
  5038.  
  5039.     /*
  5040.      * If the cursor is touching the command, and it ends in an alphabetic
  5041.      * character, complete the command name.
  5042.      */
  5043.     if (p == cmdbuff + cmdpos && isalpha(p[-1]))
  5044.         return NULL;
  5045.  
  5046.     if (cmdidx == CMD_SIZE)
  5047.     {
  5048.         if (*cmd == 's' && vim_strchr((char_u *)"cgr", cmd[1]) != NULL)
  5049.         {
  5050.             cmdidx = CMD_substitute;
  5051.             p = cmd + 1;
  5052.         }
  5053.         else
  5054.         {
  5055.             /* Not still touching the command and it was an illegal command */
  5056.             expand_context = EXPAND_UNSUCCESSFUL;
  5057.             return NULL;
  5058.         }
  5059.     }
  5060.  
  5061.     expand_context = EXPAND_NOTHING; /* Default now that we're past command */
  5062.  
  5063.     if (*p == '!')                    /* forced commands */
  5064.     {
  5065.         forceit = TRUE;
  5066.         ++p;
  5067.     }
  5068.  
  5069. /*
  5070.  * 5. parse arguments
  5071.  */
  5072.     argt = cmdnames[cmdidx].cmd_argt;
  5073.  
  5074.     arg = skipwhite(p);
  5075.  
  5076.     if (cmdidx == CMD_write)
  5077.     {
  5078.         if (*arg == '>')                        /* append */
  5079.         {
  5080.             if (*++arg == '>')                /* It should be */
  5081.                 ++arg;
  5082.             arg = skipwhite(arg);
  5083.         }
  5084.         else if (*arg == '!')                    /* :w !filter */
  5085.         {
  5086.             ++arg;
  5087.             usefilter = TRUE;
  5088.         }
  5089.     }
  5090.  
  5091.     if (cmdidx == CMD_read)
  5092.     {
  5093.         usefilter = forceit;                    /* :r! filter if forced */
  5094.         if (*arg == '!')                        /* :r !filter */
  5095.         {
  5096.             ++arg;
  5097.             usefilter = TRUE;
  5098.         }
  5099.     }
  5100.  
  5101.     if (cmdidx == CMD_lshift || cmdidx == CMD_rshift)
  5102.     {
  5103.         while (*arg == *cmd)        /* allow any number of '>' or '<' */
  5104.             ++arg;
  5105.         arg = skipwhite(arg);
  5106.     }
  5107.  
  5108.     /* Does command allow "+command"? */
  5109.     if ((argt & EDITCMD) && !usefilter && *arg == '+')
  5110.     {
  5111.         /* Check if we're in the +command */
  5112.         p = arg + 1;
  5113.         arg = skiptowhite(arg);
  5114.  
  5115.         /* Still touching the command after '+'? */
  5116.         if (arg >= cmdbuff + cmdpos)
  5117.             return p;
  5118.  
  5119.         /* Skip space after +command to get to the real argument */
  5120.         arg = skipwhite(arg);
  5121.     }
  5122.  
  5123.     /*
  5124.      * Check for '|' to separate commands and '"' to start comments.
  5125.      * Don't do this for ":read !cmd" and ":write !cmd".
  5126.      */
  5127.     if ((argt & TRLBAR) && !usefilter)
  5128.     {
  5129.         p = arg;
  5130.         while (*p)
  5131.         {
  5132.             if (*p == Ctrl('V'))
  5133.             {
  5134.                 if (p[1] != NUL)
  5135.                     ++p;
  5136.             }
  5137.             else if ((*p == '"' && !(argt & NOTRLCOM)) || *p == '|' || *p == '\n')
  5138.             {
  5139.                 if (*(p - 1) != '\\')
  5140.                 {
  5141.                     if (*p == '|' || *p == '\n')
  5142.                         return p + 1;
  5143.                     return NULL;    /* It's a comment */
  5144.                 }
  5145.             }
  5146.             ++p;
  5147.         }
  5148.     }
  5149.  
  5150.                                                 /* no arguments allowed */
  5151.     if (!(argt & EXTRA) && *arg != NUL &&
  5152.                                     vim_strchr((char_u *)"|\"", *arg) == NULL)
  5153.         return NULL;
  5154.  
  5155.     /* Find start of last argument (argument just before cursor): */
  5156.     p = cmdbuff + cmdpos;
  5157.     while (p != arg && *p != ' ' && *p != TAB)
  5158.         p--;
  5159.     if (*p == ' ' || *p == TAB)
  5160.         p++;
  5161.     expand_pattern = p;
  5162.  
  5163.     if (argt & XFILE)
  5164.     {
  5165.         int in_quote = FALSE;
  5166.         char_u *bow = NULL;        /* Beginning of word */
  5167.  
  5168.         /*
  5169.          * Allow spaces within back-quotes to count as part of the argument
  5170.          * being expanded.
  5171.          * Never accept '<' or '>' inside a file name.
  5172.          */
  5173.         expand_pattern = skipwhite(arg);
  5174.         for (p = expand_pattern; *p; ++p)
  5175.         {
  5176.             if (*p == '\\' && p[1])
  5177.                 ++p;
  5178.             else if ((vim_iswhite(*p)
  5179. #ifdef SPACE_IN_FILENAME
  5180.                     && (!(argt & NOSPC) || usefilter)
  5181. #endif
  5182.                      ) || *p == '<' || *p == '>')
  5183.             {
  5184.                 if (p[1] == '&')        /* skip ">&" */
  5185.                     ++p;
  5186.                 if (p[1] == '!')        /* skip ">&!" */
  5187.                     ++p;
  5188.                 if (in_quote)
  5189.                     bow = p + 1;
  5190.                 else
  5191.                     expand_pattern = p + 1;
  5192.             }
  5193.             else if (*p == '`')
  5194.             {
  5195.                 if (!in_quote)
  5196.                 {
  5197.                     expand_pattern = p;
  5198.                     bow = p + 1;
  5199.                 }
  5200.                 in_quote = !in_quote;
  5201.             }
  5202.         }
  5203.  
  5204.         /*
  5205.          * If we are still inside the quotes, and we passed a space, just
  5206.          * expand from there.
  5207.          */
  5208.         if (bow != NULL && in_quote)
  5209.             expand_pattern = bow;
  5210.         expand_context = EXPAND_FILES;
  5211.     }
  5212.  
  5213. /*
  5214.  * 6. switch on command name
  5215.  */
  5216.     switch (cmdidx)
  5217.     {
  5218.         case CMD_cd:
  5219.         case CMD_chdir:
  5220.             expand_context = EXPAND_DIRECTORIES;
  5221.             break;
  5222.         case CMD_global:
  5223.         case CMD_vglobal:
  5224.             delim = *arg;             /* get the delimiter */
  5225.             if (delim)
  5226.                 ++arg;                /* skip delimiter if there is one */
  5227.  
  5228.             while (arg[0] != NUL && arg[0] != delim)
  5229.             {
  5230.                 if (arg[0] == '\\' && arg[1] != NUL)
  5231.                     ++arg;
  5232.                 ++arg;
  5233.             }
  5234.             if (arg[0] != NUL)
  5235.                 return arg + 1;
  5236.             break;
  5237.         case CMD_and:
  5238.         case CMD_substitute:
  5239.             delim = *arg;
  5240.             if (delim)
  5241.                 ++arg;
  5242.             for (i = 0; i < 2; i++)
  5243.             {
  5244.                 while (arg[0] != NUL && arg[0] != delim)
  5245.                 {
  5246.                     if (arg[0] == '\\' && arg[1] != NUL)
  5247.                         ++arg;
  5248.                     ++arg;
  5249.                 }
  5250.                 if (arg[0] != NUL)        /* skip delimiter */
  5251.                     ++arg;
  5252.             }
  5253.             while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
  5254.                 ++arg;
  5255.             if (arg[0] != NUL)
  5256.                 return arg;
  5257.             break;
  5258.         case CMD_isearch:
  5259.         case CMD_dsearch:
  5260.         case CMD_ilist:
  5261.         case CMD_dlist:
  5262.         case CMD_ijump:
  5263.         case CMD_djump:
  5264.         case CMD_isplit:
  5265.         case CMD_dsplit:
  5266.             arg = skipwhite(skipdigits(arg));        /* skip count */
  5267.             if (*arg == '/')    /* Match regexp, not just whole words */
  5268.             {
  5269.                 for (++arg; *arg && *arg != '/'; arg++)
  5270.                     if (*arg == '\\' && arg[1] != NUL)
  5271.                         arg++;
  5272.                 if (*arg)
  5273.                 {
  5274.                     arg = skipwhite(arg + 1);
  5275.  
  5276.                     /* Check for trailing illegal characters */
  5277.                     if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
  5278.                         expand_context = EXPAND_NOTHING;
  5279.                     else
  5280.                         return arg;
  5281.                 }
  5282.             }
  5283.             break;
  5284. #ifdef AUTOCMD
  5285.         case CMD_autocmd:
  5286.             return set_context_in_autocmd(arg, FALSE);
  5287.  
  5288.         case CMD_doautocmd:
  5289.             return set_context_in_autocmd(arg, TRUE);
  5290. #endif
  5291.         case CMD_set:
  5292.             set_context_in_set_cmd(arg);
  5293.             break;
  5294.         case CMD_stag:
  5295.         case CMD_tag:
  5296.             expand_context = EXPAND_TAGS;
  5297.             expand_pattern = arg;
  5298.             break;
  5299.         case CMD_help:
  5300.             expand_context = EXPAND_HELP;
  5301.             expand_pattern = arg;
  5302.             break;
  5303.         case CMD_bdelete:
  5304.         case CMD_bunload:
  5305.             while ((expand_pattern = vim_strchr(arg, ' ')) != NULL)
  5306.                 arg = expand_pattern + 1;
  5307.         case CMD_buffer:
  5308.         case CMD_sbuffer:
  5309.             expand_context = EXPAND_BUFFERS;
  5310.             expand_pattern = arg;
  5311.             break;
  5312. #ifdef USE_GUI
  5313.         case CMD_menu:        case CMD_noremenu:        case CMD_unmenu:
  5314.         case CMD_nmenu:        case CMD_nnoremenu:        case CMD_nunmenu:
  5315.         case CMD_vmenu:        case CMD_vnoremenu:        case CMD_vunmenu:
  5316.         case CMD_imenu:        case CMD_inoremenu:        case CMD_iunmenu:
  5317.         case CMD_cmenu:        case CMD_cnoremenu:        case CMD_cunmenu:
  5318.             return gui_set_context_in_menu_cmd(cmd, arg, forceit);
  5319.             break;
  5320. #endif
  5321.         default:
  5322.             break;
  5323.     }
  5324.     return NULL;
  5325. }
  5326.  
  5327. /*
  5328.  * Do the expansion based on the global variables expand_context and
  5329.  * expand_pattern -- webb.
  5330.  */
  5331.     static int
  5332. ExpandFromContext(pat, num_file, file, files_only, options)
  5333.     char_u    *pat;
  5334.     int        *num_file;
  5335.     char_u    ***file;
  5336.     int        files_only;
  5337.     int        options;
  5338. {
  5339.     regexp    *prog;
  5340.     int        ret;
  5341.     int        i;
  5342.     int        count;
  5343.  
  5344.     if (!expand_interactively || expand_context == EXPAND_FILES)
  5345.         return ExpandWildCards(1, &pat, num_file, file, files_only,
  5346.                                               (options & WILD_LIST_NOTFOUND));
  5347.     else if (expand_context == EXPAND_DIRECTORIES)
  5348.     {
  5349.         if (ExpandWildCards(1, &pat, num_file, file, files_only,
  5350.                                       (options & WILD_LIST_NOTFOUND)) == FAIL)
  5351.             return FAIL;
  5352.         count = 0;
  5353.         for (i = 0; i < *num_file; i++)
  5354.             if (mch_isdir((*file)[i]))
  5355.                 (*file)[count++] = (*file)[i];
  5356.             else
  5357.                 vim_free((*file)[i]);
  5358.         if (count == 0)
  5359.         {
  5360.             vim_free(*file);
  5361.             *file = (char_u **)"";
  5362.             *num_file = -1;
  5363.             return FAIL;
  5364.         }
  5365.         *num_file = count;
  5366.         return OK;
  5367.     }
  5368.     *file = (char_u **)"";
  5369.     *num_file = 0;
  5370.     if (expand_context == EXPAND_OLD_SETTING)
  5371.         return ExpandOldSetting(num_file, file);
  5372.  
  5373.     if (expand_context == EXPAND_HELP)
  5374.         return find_help_tags(pat, num_file, file);
  5375.  
  5376.     set_reg_ic(pat);        /* set reg_ic according to p_ic, p_scs and pat */
  5377. #ifdef AUTOCMD
  5378.     if (expand_context == EXPAND_EVENTS)
  5379.         reg_ic = TRUE;        /* always ignore case for events */
  5380. #endif
  5381.     reg_magic = p_magic;
  5382.  
  5383.     if (expand_context == EXPAND_BUFFERS)
  5384.         return ExpandBufnames(pat, num_file, file, options);
  5385.  
  5386.     prog = vim_regcomp(pat);
  5387.     if (prog == NULL)
  5388.         return FAIL;
  5389.  
  5390.     if (expand_context == EXPAND_COMMANDS)
  5391.         ret = ExpandCommands(prog, num_file, file);
  5392.     else if (expand_context == EXPAND_SETTINGS ||
  5393.                                        expand_context == EXPAND_BOOL_SETTINGS)
  5394.         ret = ExpandSettings(prog, num_file, file);
  5395.     else if (expand_context == EXPAND_TAGS)
  5396.         ret = find_tags(NULL, prog, num_file, file, FALSE, FALSE);
  5397. #ifdef AUTOCMD
  5398.     else if (expand_context == EXPAND_EVENTS)
  5399.         ret = ExpandEvents(prog, num_file, file);
  5400. #endif
  5401. #ifdef USE_GUI
  5402.     else if (expand_context == EXPAND_MENUS)
  5403.         ret = gui_ExpandMenuNames(prog, num_file, file);
  5404. #endif
  5405.     else
  5406.         ret = FAIL;
  5407.  
  5408.     vim_free(prog);
  5409.     return ret;
  5410. }
  5411.  
  5412.     static int
  5413. ExpandCommands(prog, num_file, file)
  5414.     regexp        *prog;
  5415.     int            *num_file;
  5416.     char_u        ***file;
  5417. {
  5418.     int        cmdidx;
  5419.     int        count;
  5420.     int        round;
  5421.  
  5422.     /*
  5423.      * round == 1: Count the matches.
  5424.      * round == 2: Save the matches into the array.
  5425.      */
  5426.     for (round = 1; round <= 2; ++round)
  5427.     {
  5428.         count = 0;
  5429.         for (cmdidx = 0; cmdidx < CMD_SIZE; cmdidx++)
  5430.             if (vim_regexec(prog, cmdnames[cmdidx].cmd_name, TRUE))
  5431.             {
  5432.                 if (round == 1)
  5433.                     count++;
  5434.                 else
  5435.                     (*file)[count++] = strsave(cmdnames[cmdidx].cmd_name);
  5436.             }
  5437.         if (round == 1)
  5438.         {
  5439.             *num_file = count;
  5440.             if (count == 0 || (*file = (char_u **)
  5441.                          alloc((unsigned)(count * sizeof(char_u *)))) == NULL)
  5442.                 return FAIL;
  5443.         }
  5444.     }
  5445.     return OK;
  5446. }
  5447.  
  5448. #ifdef VIMINFO
  5449. static char_u **viminfo_history[2] = {NULL, NULL};
  5450. static int        viminfo_hisidx[2] = {0, 0};
  5451. static int        viminfo_hislen = 0;
  5452. static int        viminfo_add_at_front = FALSE;
  5453.  
  5454.     void
  5455. prepare_viminfo_history(len)
  5456.     int len;
  5457. {
  5458.     int i;
  5459.     int num;
  5460.     int    type;
  5461.  
  5462.     init_history();
  5463.     viminfo_add_at_front = (len != 0);
  5464.     if (len > hislen)
  5465.         len = hislen;
  5466.  
  5467.     for (type = 0; type <= 1; ++type)
  5468.     {
  5469.         /* If there are more spaces available than we request, then fill them
  5470.          * up */
  5471.         for (i = 0, num = 0; i < hislen; i++)
  5472.             if (history[type][i] == NULL)
  5473.                 num++;
  5474.         if (num > len)
  5475.             len = num;
  5476.         viminfo_hisidx[type] = 0;
  5477.         if (len <= 0)
  5478.             viminfo_history[type] = NULL;
  5479.         else
  5480.             viminfo_history[type] = (char_u **)lalloc(len * sizeof(char_u *),
  5481.                                                                        FALSE);
  5482.     }
  5483.     viminfo_hislen = len;
  5484.     if (viminfo_history[0] == NULL || viminfo_history[1] == NULL)
  5485.         viminfo_hislen = 0;
  5486. }
  5487.  
  5488.     int
  5489. read_viminfo_history(line, fp)
  5490.     char_u    *line;
  5491.     FILE    *fp;
  5492. {
  5493.     int        type;
  5494.  
  5495.     type = (line[0] == ':' ? 0 : 1);
  5496.     if (viminfo_hisidx[type] != viminfo_hislen)
  5497.     {
  5498.         viminfo_readstring(line);
  5499.         if (!is_in_history(type, line + 1, viminfo_add_at_front))
  5500.             viminfo_history[type][viminfo_hisidx[type]++] = strsave(line + 1);
  5501.     }
  5502.     return vim_fgets(line, LSIZE, fp);
  5503. }
  5504.  
  5505.     void
  5506. finish_viminfo_history()
  5507. {
  5508.     int idx;
  5509.     int i;
  5510.     int    type;
  5511.  
  5512.     for (type = 0; type <= 1; ++type)
  5513.     {
  5514.         if (history[type] == NULL)
  5515.             return;
  5516.         idx = hisidx[type] + viminfo_hisidx[type];
  5517.         if (idx >= hislen)
  5518.             idx -= hislen;
  5519.         if (viminfo_add_at_front)
  5520.             hisidx[type] = idx;
  5521.         else
  5522.         {
  5523.             if (hisidx[type] == -1)
  5524.                 hisidx[type] = hislen - 1;
  5525.             do
  5526.             {
  5527.                 if (history[type][idx] != NULL)
  5528.                     break;
  5529.                 if (++idx == hislen)
  5530.                     idx = 0;
  5531.             } while (idx != hisidx[type]);
  5532.             if (idx != hisidx[type] && --idx < 0)
  5533.                 idx = hislen - 1;
  5534.         }
  5535.         for (i = 0; i < viminfo_hisidx[type]; i++)
  5536.         {
  5537.             history[type][idx] = viminfo_history[type][i];
  5538.             if (--idx < 0)
  5539.                 idx = hislen - 1;
  5540.         }
  5541.         vim_free(viminfo_history[type]);
  5542.         viminfo_history[type] = NULL;
  5543.     }
  5544. }
  5545.  
  5546.     void
  5547. write_viminfo_history(fp)
  5548.     FILE    *fp;
  5549. {
  5550.     int        i;
  5551.     int        type;
  5552.     int        num_saved;
  5553.  
  5554.     init_history();
  5555.     if (hislen == 0)
  5556.         return;
  5557.     for (type = 0; type <= 1; ++type)
  5558.     {
  5559.         num_saved = get_viminfo_parameter(type == 0 ? ':' : '/');
  5560.         if (num_saved == 0)
  5561.             continue;
  5562.         if (num_saved < 0)    /* Use default */
  5563.             num_saved = hislen;
  5564.         fprintf(fp, "\n# %s History (newest to oldest):\n",
  5565.                             type == 0 ? "Command Line" : "Search String");
  5566.         if (num_saved > hislen)
  5567.             num_saved = hislen;
  5568.         i = hisidx[type];
  5569.         if (i >= 0)
  5570.             while (num_saved--)
  5571.             {
  5572.                 if (history[type][i] != NULL)
  5573.                 {
  5574.                     putc(type == 0 ? ':' : '?', fp);
  5575.                     viminfo_writestring(fp, history[type][i]);
  5576.                 }
  5577.                 if (--i < 0)
  5578.                     i = hislen - 1;
  5579.             }
  5580.     }
  5581. }
  5582. #endif /* VIMINFO */
  5583.