home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / ex_getln.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-29  |  117.5 KB  |  5,075 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * ex_getln.c: Functions for entering and editing an Ex command line.
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. /*
  17.  * Variables shared between getcmdline(), redrawcmdline() and others.
  18.  * These need to be saved when using CTRL-R |, that's why they are in a
  19.  * structure.
  20.  */
  21. struct cmdline_info
  22. {
  23.     char_u    *cmdbuff;    /* pointer to command line buffer */
  24.     int        cmdbufflen;    /* length of cmdbuff */
  25.     int        cmdlen;        /* number of chars in command line */
  26.     int        cmdpos;        /* current cursor position */
  27.     int        cmdspos;    /* cursor column on screen */
  28.     int        cmdfirstc;    /* ':', '/', '?', '=' or NUL */
  29.     int        cmdindent;    /* number of spaces before cmdline */
  30.     char_u    *cmdprompt;    /* message in front of cmdline */
  31.     int        cmdattr;    /* attributes for prompt */
  32.     int        overstrike;    /* Typing mode on the command line.  Shared by
  33.                    getcmdline() and put_on_cmdline(). */
  34. };
  35.  
  36. static struct cmdline_info ccline;    /* current cmdline_info */
  37.  
  38. static int    cmd_numfiles = -1;    /* number of files found by
  39.                             file name completion */
  40. static char_u    **cmd_files = NULL;    /* list of files */
  41. static int    cmd_showtail;        /* Only show path tail in lists ? */
  42.  
  43.  
  44. #ifdef FEAT_CMDHIST
  45. typedef struct hist_entry
  46. {
  47.     int        hisnum;        /* identifying number */
  48.     char_u    *hisstr;    /* actual entry */
  49. } histentry_T;
  50.  
  51. static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
  52. static int    hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1};  /* lastused entry */
  53. static int    hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
  54.             /* identifying (unique) number of newest history entry */
  55. static int    hislen = 0;        /* actual length of history tables */
  56.  
  57. static int    hist_char2type __ARGS((int c));
  58. static void    init_history __ARGS((void));
  59.  
  60. static int    in_history __ARGS((int, char_u *, int));
  61. # ifdef FEAT_EVAL
  62. static int    calc_hist_idx __ARGS((int histype, int num));
  63. # endif
  64. #endif
  65.  
  66. #ifdef FEAT_RIGHTLEFT
  67. static int    cmd_hkmap = 0;    /* Hebrew mapping during command line */
  68. #endif
  69.  
  70. #ifdef FEAT_FKMAP
  71. static int    cmd_fkmap = 0;    /* Farsi mapping during command line */
  72. #endif
  73.  
  74. static int    cmdline_charsize __ARGS((int idx));
  75. static void    set_cmdspos __ARGS((void));
  76. static void    set_cmdspos_cursor __ARGS((void));
  77. #ifdef FEAT_MBYTE
  78. static void    correct_cmdspos __ARGS((int idx, int cells));
  79. #endif
  80. static void    alloc_cmdbuff __ARGS((int len));
  81. static int    realloc_cmdbuff __ARGS((int len));
  82. static void    draw_cmdline __ARGS((int start, int len));
  83. #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
  84. static void    redrawcmd_preedit __ARGS((void));
  85. #endif
  86. #ifdef FEAT_WILDMENU
  87. static void    cmdline_del __ARGS((int from));
  88. #endif
  89. static void    redrawcmdprompt __ARGS((void));
  90. static void    cursorcmd __ARGS((void));
  91. static int    ccheck_abbr __ARGS((int));
  92. static int    nextwild __ARGS((expand_T *xp, int type, int options));
  93. static int    showmatches __ARGS((expand_T *xp, int wildmenu));
  94. static void    set_expand_context __ARGS((expand_T *xp));
  95. static int    ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
  96. static int    glob_in_path_prefix __ARGS((expand_T *xp));
  97. #ifdef FEAT_CMDL_COMPL
  98. static int    ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
  99. static int    ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
  100. #endif
  101.  
  102. #ifdef FEAT_CMDWIN
  103. static int    ex_window __ARGS((void));
  104. #endif
  105.  
  106. /*
  107.  * getcmdline() - accept a command line starting with firstc.
  108.  *
  109.  * firstc == ':'        get ":" command line.
  110.  * firstc == '/' or '?'        get search pattern
  111.  * firstc == '='        get expression
  112.  * firstc == '@'        get text for input() function
  113.  * firstc == '>'        get text for debug mode
  114.  * firstc == NUL        get text for :insert command
  115.  * firstc == -1            like NUL, and break on CTRL-C
  116.  *
  117.  * The line is collected in ccline.cmdbuff, which is reallocated to fit the
  118.  * command line.
  119.  *
  120.  * Careful: getcmdline() can be called recursively!
  121.  *
  122.  * Return pointer to allocated string if there is a commandline, NULL
  123.  * otherwise.
  124.  */
  125. /*ARGSUSED*/
  126.     char_u *
  127. getcmdline(firstc, count, indent)
  128.     int        firstc;
  129.     long    count;        /* only used for incremental search */
  130.     int        indent;        /* indent for inside conditionals */
  131. {
  132.     int        c;
  133.     int        i;
  134.     int        j;
  135.     int        gotesc = FALSE;        /* TRUE when <ESC> just typed */
  136.     int        do_abbr;        /* when TRUE check for abbr. */
  137. #ifdef FEAT_CMDHIST
  138.     char_u    *lookfor = NULL;    /* string to match */
  139.     int        hiscnt;            /* current history line in use */
  140.     int        histype;        /* history type to be used */
  141. #endif
  142. #ifdef FEAT_SEARCH_EXTRA
  143.     pos_T    old_cursor;
  144.     colnr_T    old_curswant;
  145.     colnr_T    old_leftcol;
  146.     linenr_T    old_topline;
  147. # ifdef FEAT_DIFF
  148.     int        old_topfill;
  149. # endif
  150.     linenr_T    old_botline;
  151.     int        did_incsearch = FALSE;
  152.     int        incsearch_postponed = FALSE;
  153. #endif
  154.     int        did_wild_list = FALSE;    /* did wild_list() recently */
  155.     int        wim_index = 0;        /* index in wim_flags[] */
  156.     int        res;
  157.     int        save_msg_scroll = msg_scroll;
  158.     int        save_State = State;    /* remember State when called */
  159.     int        some_key_typed = FALSE;    /* one of the keys was typed */
  160. #ifdef FEAT_MOUSE
  161.     /* mouse drag and release events are ignored, unless they are
  162.      * preceded with a mouse down event */
  163.     int        ignore_drag_release = TRUE;
  164. #endif
  165. #ifdef FEAT_EVAL
  166.     int        break_ctrl_c = FALSE;
  167. #endif
  168.     expand_T    xpc;
  169.     long    *b_im_ptr = NULL;
  170.  
  171. #ifdef FEAT_SNIFF
  172.     want_sniff_request = 0;
  173. #endif
  174. #ifdef FEAT_EVAL
  175.     if (firstc == -1)
  176.     {
  177.     firstc = NUL;
  178.     break_ctrl_c = TRUE;
  179.     }
  180. #endif
  181. #ifdef FEAT_RIGHTLEFT
  182.     /* start without Hebrew mapping for a command line */
  183.     if (firstc == ':' || firstc == '=' || firstc == '>')
  184.     cmd_hkmap = 0;
  185. #endif
  186.  
  187.     ccline.overstrike = FALSE;            /* always start in insert mode */
  188. #ifdef FEAT_SEARCH_EXTRA
  189.     old_cursor = curwin->w_cursor;        /* needs to be restored later */
  190.     old_curswant = curwin->w_curswant;
  191.     old_leftcol = curwin->w_leftcol;
  192.     old_topline = curwin->w_topline;
  193. # ifdef FEAT_DIFF
  194.     old_topfill = curwin->w_topfill;
  195. # endif
  196.     old_botline = curwin->w_botline;
  197. #endif
  198.  
  199.     /*
  200.      * set some variables for redrawcmd()
  201.      */
  202.     ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
  203.     ccline.cmdindent = indent;
  204.     alloc_cmdbuff(exmode_active ? 250 : 0); /* alloc initial ccline.cmdbuff */
  205.     if (ccline.cmdbuff == NULL)
  206.     return NULL;                /* out of memory */
  207.     ccline.cmdlen = ccline.cmdpos = 0;
  208.     ccline.cmdbuff[0] = NUL;
  209.  
  210. #ifdef FEAT_RIGHTLEFT
  211.     if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
  212.                       && (firstc == '/' || firstc == '?'))
  213.     cmdmsg_rl = TRUE;
  214.     else
  215.     cmdmsg_rl = FALSE;
  216. #endif
  217.  
  218.     redir_off = TRUE;        /* don't redirect the typed command */
  219.     if (!cmd_silent)
  220.     {
  221.     i = msg_scrolled;
  222.     msg_scrolled = 0;        /* avoid wait_return message */
  223.     gotocmdline(TRUE);
  224.     msg_scrolled += i;
  225.     redrawcmdprompt();        /* draw prompt or indent */
  226.     set_cmdspos();
  227.     }
  228.     xpc.xp_context = EXPAND_NOTHING;
  229.     xpc.xp_backslash = XP_BS_NONE;
  230.  
  231.     /*
  232.      * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
  233.      * doing ":@0" when register 0 doesn't contain a CR.
  234.      */
  235.     msg_scroll = FALSE;
  236.  
  237.     State = CMDLINE;
  238.  
  239.     if (firstc == '/' || firstc == '?' || firstc == '@')
  240.     {
  241.     /* Use ":lmap" mappings for search pattern and input(). */
  242.     if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
  243.         b_im_ptr = &curbuf->b_p_iminsert;
  244.     else
  245.         b_im_ptr = &curbuf->b_p_imsearch;
  246.     if (*b_im_ptr == B_IMODE_LMAP)
  247.         State |= LANGMAP;
  248. #ifdef USE_IM_CONTROL
  249.     im_set_active(*b_im_ptr == B_IMODE_IM);
  250. #endif
  251.     }
  252. #ifdef USE_IM_CONTROL
  253.     else if (p_imcmdline)
  254.     im_set_active(TRUE);
  255. #endif
  256.  
  257. #ifdef FEAT_MOUSE
  258.     setmouse();
  259. #endif
  260. #ifdef CURSOR_SHAPE
  261.     ui_cursor_shape();        /* may show different cursor shape */
  262. #endif
  263.  
  264. #ifdef FEAT_CMDHIST
  265.     init_history();
  266.     hiscnt = hislen;        /* set hiscnt to impossible history value */
  267.     histype = hist_char2type(firstc);
  268. #endif
  269.  
  270. #ifdef FEAT_DIGRAPHS
  271.     do_digraph(-1);        /* init digraph typahead */
  272. #endif
  273.  
  274.     /*
  275.      * Collect the command string, handling editing keys.
  276.      */
  277.     for (;;)
  278.     {
  279. #ifdef USE_ON_FLY_SCROLL
  280.     dont_scroll = FALSE;    /* allow scrolling here */
  281. #endif
  282.     quit_more = FALSE;    /* reset after CTRL-D which had a more-prompt */
  283.  
  284.     cursorcmd();        /* set the cursor on the right spot */
  285.     c = safe_vgetc();
  286.     if (KeyTyped)
  287.     {
  288.         some_key_typed = TRUE;
  289. #ifdef FEAT_RIGHTLEFT
  290.         if (cmd_hkmap)
  291.         c = hkmap(c);
  292. # ifdef FEAT_FKMAP
  293.         if (cmd_fkmap)
  294.         c = cmdl_fkmap(c);
  295. # endif
  296.         if (cmdmsg_rl && !KeyStuffed)
  297.         {
  298.         /* Invert horizontal movements and operations.  Only when
  299.          * typed by the user directly, not when the result of a
  300.          * mapping. */
  301.         switch (c)
  302.         {
  303.             case K_RIGHT:   c = K_LEFT; break;
  304.             case K_S_RIGHT: c = K_S_LEFT; break;
  305.             case K_C_RIGHT: c = K_C_LEFT; break;
  306.             case K_LEFT:    c = K_RIGHT; break;
  307.             case K_S_LEFT:  c = K_S_RIGHT; break;
  308.             case K_C_LEFT:  c = K_C_RIGHT; break;
  309.         }
  310.         }
  311. #endif
  312.     }
  313.  
  314.     /*
  315.      * Ignore got_int when CTRL-C was typed here.
  316.      * Don't ignore it in :global, we really need to break then, e.g., for
  317.      * ":g/pat/normal /pat" (without the <CR>).
  318.      * Don't ignore it for the input() function.
  319.      */
  320.     if ((c == Ctrl_C
  321. #ifdef UNIX
  322.         || c == intr_char
  323. #endif
  324.                 )
  325. #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
  326.         && firstc != '@'
  327. #endif
  328. #ifdef FEAT_EVAL
  329.         && !break_ctrl_c
  330. #endif
  331.         && !global_busy)
  332.         got_int = FALSE;
  333.  
  334. #ifdef FEAT_CMDHIST
  335.     /* free old command line when finished moving around in the history
  336.      * list */
  337.     if (lookfor != NULL
  338.         && c != K_S_DOWN && c != K_S_UP && c != K_DOWN && c != K_UP
  339.         && c != K_PAGEDOWN && c != K_PAGEUP
  340.         && c != K_KPAGEDOWN && c != K_KPAGEUP
  341.         && c != K_LEFT && c != K_RIGHT
  342.         && (cmd_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
  343.     {
  344.         vim_free(lookfor);
  345.         lookfor = NULL;
  346.     }
  347. #endif
  348.  
  349.     /*
  350.      * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
  351.      */
  352.     if (c != p_wc && c == K_S_TAB && cmd_numfiles != -1)
  353.         c = Ctrl_P;
  354.  
  355. #ifdef FEAT_WILDMENU
  356.     /* Special translations for 'wildmenu' */
  357.     if (did_wild_list && p_wmnu)
  358.     {
  359.         if (c == K_LEFT)
  360.         c = Ctrl_P;
  361.         else if (c == K_RIGHT)
  362.         c = Ctrl_N;
  363.     }
  364.     /* Hitting CR after "emenu Name.": complete submenu */
  365.     if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
  366.         && ccline.cmdpos > 1
  367.         && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
  368.         && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
  369.         && (c == '\n' || c == '\r' || c == K_KENTER))
  370.         c = K_DOWN;
  371. #endif
  372.  
  373.     /* free expanded names when finished walking through matches */
  374.     if (cmd_numfiles != -1
  375.         && !(c == p_wc && KeyTyped) && c != p_wcm
  376.         && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
  377.         && c != Ctrl_L)
  378.     {
  379.         (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
  380.         did_wild_list = FALSE;
  381. #ifdef FEAT_WILDMENU
  382.         if (!p_wmnu || (c != K_UP && c != K_DOWN))
  383. #endif
  384.         xpc.xp_context = EXPAND_NOTHING;
  385.         wim_index = 0;
  386. #ifdef FEAT_WILDMENU
  387.         if (p_wmnu && wild_menu_showing != 0)
  388.         {
  389.         int skt = KeyTyped;
  390.  
  391.         if (wild_menu_showing == WM_SCROLLED)
  392.         {
  393.             /* Entered command line, move it up */
  394.             cmdline_row--;
  395.             redrawcmd();
  396.         }
  397.         else if (save_p_ls != -1)
  398.         {
  399.             /* restore 'laststatus' and 'winminheight' */
  400.             p_ls = save_p_ls;
  401.             p_wmh = save_p_wmh;
  402.             last_status(FALSE);
  403.             update_screen(VALID);    /* redraw the screen NOW */
  404.             redrawcmd();
  405.             save_p_ls = -1;
  406.         }
  407.         else
  408.         {
  409. # ifdef FEAT_VERTSPLIT
  410.             win_redraw_last_status(topframe);
  411. # else
  412.             lastwin->w_redr_status = TRUE;
  413. # endif
  414.             redraw_statuslines();
  415.         }
  416.         KeyTyped = skt;
  417.         wild_menu_showing = 0;
  418.         }
  419. #endif
  420.     }
  421.  
  422. #ifdef FEAT_WILDMENU
  423.     /* Special translations for 'wildmenu' */
  424.     if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
  425.     {
  426.         /* Hitting <Down> after "emenu Name.": complete submenu */
  427.         if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
  428.         c = p_wc;
  429.         else if (c == K_UP)
  430.         {
  431.         /* Hitting <Up>: Remove one submenu name in front of the
  432.          * cursor */
  433.         int found = FALSE;
  434.  
  435.         j = (int)(xpc.xp_pattern - ccline.cmdbuff);
  436.         i = 0;
  437.         while (--j > 0)
  438.         {
  439.             /* check for start of menu name */
  440.             if (ccline.cmdbuff[j] == ' '
  441.                 && ccline.cmdbuff[j - 1] != '\\')
  442.             {
  443.             i = j + 1;
  444.             break;
  445.             }
  446.             /* check for start of submenu name */
  447.             if (ccline.cmdbuff[j] == '.'
  448.                 && ccline.cmdbuff[j - 1] != '\\')
  449.             {
  450.             if (found)
  451.             {
  452.                 i = j + 1;
  453.                 break;
  454.             }
  455.             else
  456.                 found = TRUE;
  457.             }
  458.         }
  459.         if (i > 0)
  460.             cmdline_del(i);
  461.         c = p_wc;
  462.         xpc.xp_context = EXPAND_NOTHING;
  463.         }
  464.     }
  465.     if (xpc.xp_context == EXPAND_FILES && p_wmnu)
  466.     {
  467.         char_u upseg[5];
  468.  
  469.         upseg[0] = PATHSEP;
  470.         upseg[1] = '.';
  471.         upseg[2] = '.';
  472.         upseg[3] = PATHSEP;
  473.         upseg[4] = NUL;
  474.  
  475.         if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
  476.             && c == K_DOWN
  477.             && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
  478.             || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
  479.         {
  480.         /* go down a directory */
  481.         c = p_wc;
  482.         }
  483.         else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
  484.         {
  485.         /* If in a direct ancestor, strip off one ../ to go down */
  486.         int found = FALSE;
  487.  
  488.         j = ccline.cmdpos;
  489.         i = (int)(xpc.xp_pattern - ccline.cmdbuff);
  490.         while (--j > i)
  491.         {
  492.             if (vim_ispathsep(ccline.cmdbuff[j]))
  493.             {
  494.             found = TRUE;
  495.             break;
  496.             }
  497.         }
  498.         if (found
  499.             && ccline.cmdbuff[j - 1] == '.'
  500.             && ccline.cmdbuff[j - 2] == '.'
  501.             && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
  502.         {
  503.             cmdline_del(j - 2);
  504.             c = p_wc;
  505.         }
  506.         }
  507.         else if (c == K_UP)
  508.         {
  509.         /* go up a directory */
  510.         int found = FALSE;
  511.  
  512.         j = ccline.cmdpos - 1;
  513.         i = (int)(xpc.xp_pattern - ccline.cmdbuff);
  514.         while (--j > i)
  515.         {
  516. #ifdef FEAT_MBYTE
  517.             if (has_mbyte)
  518.             j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
  519. #endif
  520.             if (vim_ispathsep(ccline.cmdbuff[j])
  521. #ifdef BACKSLASH_IN_FILENAME
  522.                 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
  523.                    == NULL
  524. #endif
  525.                )
  526.             {
  527.             if (found)
  528.             {
  529.                 i = j + 1;
  530.                 break;
  531.             }
  532.             else
  533.                 found = TRUE;
  534.             }
  535.         }
  536.  
  537.         if (!found)
  538.             j = i;
  539.         else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
  540.             j += 4;
  541.         else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
  542.                  && j == i)
  543.             j += 3;
  544.         else
  545.             j = 0;
  546.         if (j > 0)
  547.         {
  548.             /* TODO this is only for DOS/UNIX systems - need to put in
  549.              * machine-specific stuff here and in upseg init */
  550.             cmdline_del(j);
  551.             put_on_cmdline(upseg + 1, 3, FALSE);
  552.         }
  553.         else if (ccline.cmdpos > i)
  554.             cmdline_del(i);
  555.         c = p_wc;
  556.         }
  557.     }
  558. #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
  559.     if (p_wmnu
  560.         && (xpc.xp_context == EXPAND_FILES
  561.             || xpc.xp_context == EXPAND_MENUNAMES)
  562.         && (c == K_UP || c == K_DOWN))
  563.         xpc.xp_context = EXPAND_NOTHING;
  564. #endif
  565.  
  566. #endif    /* FEAT_WILDMENU */
  567.  
  568.     /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
  569.      * mode when 'insertmode' is set. */
  570.     if (c == Ctrl_BSL)
  571.     {
  572.         ++no_mapping;
  573.         ++allow_keys;
  574.         c = safe_vgetc();
  575.         --no_mapping;
  576.         --allow_keys;
  577.         if (c != Ctrl_N && c != Ctrl_G)
  578.         {
  579.         vungetc(c);
  580.         c = Ctrl_BSL;
  581.         }
  582.         else
  583.         {
  584.         if (c == Ctrl_G && p_im && restart_edit == 0)
  585.             restart_edit = 'a';
  586.         gotesc = TRUE;    /* will free ccline.cmdbuff after putting it
  587.                    in history */
  588.         goto returncmd;    /* back to Normal mode */
  589.         }
  590.     }
  591.  
  592. #ifdef FEAT_CMDWIN
  593.     if (c == cedit_key || c == K_CMDWIN)
  594.     {
  595.         /*
  596.          * Open a window to edit the command line (and history).
  597.          */
  598.         c = ex_window();
  599.         some_key_typed = TRUE;
  600.     }
  601. # ifdef FEAT_DIGRAPHS
  602.     else
  603. # endif
  604. #endif
  605. #ifdef FEAT_DIGRAPHS
  606.         c = do_digraph(c);
  607. #endif
  608.  
  609.     if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
  610.             && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
  611.     {
  612.         gotesc = FALSE;    /* Might have typed ESC previously, don't
  613.                    truncate the cmdline now. */
  614.         if (ccheck_abbr(c + ABBR_OFF))
  615.         goto cmdline_changed;
  616.         if (!cmd_silent)
  617.         {
  618.         windgoto(msg_row, 0);
  619.         out_flush();
  620.         }
  621.         break;
  622.     }
  623.  
  624.     /*
  625.      * Completion for 'wildchar' or 'wildcharm' key.
  626.      * - hitting <ESC> twice means: abandon command line.
  627.      * - wildcard expansion is only done when the 'wildchar' key is really
  628.      *   typed, not when it comes from a macro
  629.      */
  630.     if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
  631.     {
  632.         if (cmd_numfiles > 0)   /* typed p_wc at least twice */
  633.         {
  634.         /* if 'wildmode' contains "list" may still need to list */
  635.         if (cmd_numfiles > 1
  636.             && !did_wild_list
  637.             && (wim_flags[wim_index] & WIM_LIST))
  638.         {
  639.             (void)showmatches(&xpc, FALSE);
  640.             redrawcmd();
  641.             did_wild_list = TRUE;
  642.         }
  643.         if (wim_flags[wim_index] & WIM_LONGEST)
  644.             res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
  645.         else if (wim_flags[wim_index] & WIM_FULL)
  646.             res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
  647.         else
  648.             res = OK;        /* don't insert 'wildchar' now */
  649.         }
  650.         else            /* typed p_wc first time */
  651.         {
  652.         wim_index = 0;
  653.         j = ccline.cmdpos;
  654.         /* if 'wildmode' first contains "longest", get longest
  655.          * common part */
  656.         if (wim_flags[0] & WIM_LONGEST)
  657.             res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
  658.         else
  659.             res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
  660.  
  661.         /* if interrupted while completing, behave like it failed */
  662.         if (got_int)
  663.         {
  664.             (void)vpeekc();    /* remove <C-C> from input stream */
  665.             got_int = FALSE;    /* don't abandon the command line */
  666.             (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
  667. #ifdef FEAT_WILDMENU
  668.             xpc.xp_context = EXPAND_NOTHING;
  669. #endif
  670.             goto cmdline_changed;
  671.         }
  672.  
  673.         /* when more than one match, and 'wildmode' first contains
  674.          * "list", or no change and 'wildmode' contains "longest,list",
  675.          * list all matches */
  676.         if (res == OK && cmd_numfiles > 1)
  677.         {
  678.             /* a "longest" that didn't do anything is skipped (but not
  679.              * "list:longest") */
  680.             if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
  681.             wim_index = 1;
  682.             if ((wim_flags[wim_index] & WIM_LIST)
  683. #ifdef FEAT_WILDMENU
  684.                 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
  685. #endif
  686.                 )
  687.             {
  688.             if (!(wim_flags[0] & WIM_LONGEST))
  689.             {
  690. #ifdef FEAT_WILDMENU
  691.                 int p_wmnu_save = p_wmnu;
  692.                 p_wmnu = 0;
  693. #endif
  694.                 nextwild(&xpc, WILD_PREV, 0); /* remove match */
  695. #ifdef FEAT_WILDMENU
  696.                 p_wmnu = p_wmnu_save;
  697. #endif
  698.             }
  699. #ifdef FEAT_WILDMENU
  700.             (void)showmatches(&xpc, p_wmnu
  701.                 && ((wim_flags[wim_index] & WIM_LIST) == 0));
  702. #else
  703.             (void)showmatches(&xpc, FALSE);
  704. #endif
  705.             redrawcmd();
  706.             did_wild_list = TRUE;
  707.             if (wim_flags[wim_index] & WIM_LONGEST)
  708.                 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
  709.             else if (wim_flags[wim_index] & WIM_FULL)
  710.                 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
  711.             }
  712.             else
  713.             vim_beep();
  714.         }
  715. #ifdef FEAT_WILDMENU
  716.         else if (cmd_numfiles == -1)
  717.             xpc.xp_context = EXPAND_NOTHING;
  718. #endif
  719.         }
  720.         if (wim_index < 3)
  721.         ++wim_index;
  722.         if (c == ESC)
  723.         gotesc = TRUE;
  724.         if (res == OK)
  725.         goto cmdline_changed;
  726.     }
  727.  
  728.     gotesc = FALSE;
  729.  
  730.     /* <S-Tab> goes to last match, in a clumsy way */
  731.     if (c == K_S_TAB && KeyTyped)
  732.     {
  733.         if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
  734.             && nextwild(&xpc, WILD_PREV, 0) == OK
  735.             && nextwild(&xpc, WILD_PREV, 0) == OK)
  736.         goto cmdline_changed;
  737.     }
  738.  
  739.     if (c == NUL || c == K_ZERO)        /* NUL is stored as NL */
  740.         c = NL;
  741.  
  742.     do_abbr = TRUE;        /* default: check for abbreviation */
  743.  
  744.     /*
  745.      * Big switch for a typed command line character.
  746.      */
  747.     switch (c)
  748.     {
  749.     case K_BS:
  750.     case Ctrl_H:
  751.     case K_DEL:
  752.     case K_KDEL:
  753.     case Ctrl_W:
  754. #ifdef FEAT_FKMAP
  755.         if (cmd_fkmap && c == K_BS)
  756.             c = K_DEL;
  757. #endif
  758.         if (c == K_KDEL)
  759.             c = K_DEL;
  760.  
  761.         /*
  762.          * delete current character is the same as backspace on next
  763.          * character, except at end of line
  764.          */
  765.         if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
  766.             ++ccline.cmdpos;
  767. #ifdef FEAT_MBYTE
  768.         if (has_mbyte && c == K_DEL)
  769.             ccline.cmdpos += mb_off_next(ccline.cmdbuff,
  770.                           ccline.cmdbuff + ccline.cmdpos);
  771. #endif
  772.         if (ccline.cmdpos > 0)
  773.         {
  774.             char_u *p;
  775.  
  776.             j = ccline.cmdpos;
  777.             p = ccline.cmdbuff + j;
  778. #ifdef FEAT_MBYTE
  779.             if (has_mbyte)
  780.             {
  781.             p = mb_prevptr(ccline.cmdbuff, p);
  782.             if (c == Ctrl_W)
  783.             {
  784.                 while (p > ccline.cmdbuff && vim_isspace(*p))
  785.                 p = mb_prevptr(ccline.cmdbuff, p);
  786.                 i = mb_get_class(p);
  787.                 while (p > ccline.cmdbuff && mb_get_class(p) == i)
  788.                 p = mb_prevptr(ccline.cmdbuff, p);
  789.                 if (mb_get_class(p) != i)
  790.                 p += (*mb_ptr2len_check)(p);
  791.             }
  792.             }
  793.             else
  794. #endif
  795.             if (c == Ctrl_W)
  796.             {
  797.             while (p > ccline.cmdbuff && vim_isspace(p[-1]))
  798.                 --p;
  799.             i = vim_iswordc(p[-1]);
  800.             while (p > ccline.cmdbuff && !vim_isspace(p[-1])
  801.                 && vim_iswordc(p[-1]) == i)
  802.                 --p;
  803.             }
  804.             else
  805.             --p;
  806.             ccline.cmdpos = (int)(p - ccline.cmdbuff);
  807.             ccline.cmdlen -= j - ccline.cmdpos;
  808.             i = ccline.cmdpos;
  809.             while (i < ccline.cmdlen)
  810.             ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
  811.  
  812.             /* Truncate at the end, required for multi-byte chars. */
  813.             ccline.cmdbuff[ccline.cmdlen] = NUL;
  814.             redrawcmd();
  815.         }
  816.         else if (ccline.cmdlen == 0 && c != Ctrl_W
  817.                    && ccline.cmdprompt == NULL && indent == 0)
  818.         {
  819.             /* In ex and debug mode it doesn't make sense to return. */
  820.             if (exmode_active
  821. #ifdef FEAT_EVAL
  822.                 || ccline.cmdfirstc == '>'
  823. #endif
  824.                 )
  825.             goto cmdline_not_changed;
  826.  
  827.             vim_free(ccline.cmdbuff);    /* no commandline to return */
  828.             ccline.cmdbuff = NULL;
  829.             if (!cmd_silent)
  830.             {
  831. #ifdef FEAT_RIGHTLEFT
  832.             if (cmdmsg_rl)
  833.                 msg_col = Columns;
  834.             else
  835. #endif
  836.                 msg_col = 0;
  837.             msg_putchar(' ');        /* delete ':' */
  838.             }
  839.             redraw_cmdline = TRUE;
  840.             goto returncmd;        /* back to cmd mode */
  841.         }
  842.         goto cmdline_changed;
  843.  
  844.     case K_INS:
  845.     case K_KINS:
  846. #ifdef FEAT_FKMAP
  847.         /* if Farsi mode set, we are in reverse insert mode -
  848.            Do not change the mode */
  849.         if (cmd_fkmap)
  850.             beep_flush();
  851.         else
  852. #endif
  853.         ccline.overstrike = !ccline.overstrike;
  854. #ifdef CURSOR_SHAPE
  855.         ui_cursor_shape();    /* may show different cursor shape */
  856. #endif
  857.         goto cmdline_not_changed;
  858.  
  859.     case Ctrl_HAT:
  860.         if (map_to_exists_mode((char_u *)"", LANGMAP))
  861.         {
  862.             /* ":lmap" mappings exists, toggle use of mappings. */
  863.             State ^= LANGMAP;
  864. #ifdef USE_IM_CONTROL
  865.             im_set_active(FALSE);    /* Disable input method */
  866. #endif
  867.             if (b_im_ptr != NULL)
  868.             {
  869.             if (State & LANGMAP)
  870.                 *b_im_ptr = B_IMODE_LMAP;
  871.             else
  872.                 *b_im_ptr = B_IMODE_NONE;
  873.             }
  874.         }
  875. #ifdef USE_IM_CONTROL
  876.         else
  877.         {
  878.             /* There are no ":lmap" mappings, toggle IM.  When
  879.              * 'imdisable' is set don't try getting the status, it's
  880.              * always off. */
  881.             if ((p_imdisable && b_im_ptr != NULL)
  882.                 ? *b_im_ptr == B_IMODE_IM : im_get_status())
  883.             {
  884.             im_set_active(FALSE);    /* Disable input method */
  885.             if (b_im_ptr != NULL)
  886.                 *b_im_ptr = B_IMODE_NONE;
  887.             }
  888.             else
  889.             {
  890.             im_set_active(TRUE);    /* Enable input method */
  891.             if (b_im_ptr != NULL)
  892.                 *b_im_ptr = B_IMODE_IM;
  893.             }
  894.         }
  895. #endif
  896.         if (b_im_ptr != NULL)
  897.         {
  898.             if (b_im_ptr == &curbuf->b_p_iminsert)
  899.             set_iminsert_global();
  900.             else
  901.             set_imsearch_global();
  902.         }
  903. #ifdef CURSOR_SHAPE
  904.         ui_cursor_shape();    /* may show different cursor shape */
  905. #endif
  906.         goto cmdline_not_changed;
  907.  
  908. /*    case '@':   only in very old vi */
  909.     case Ctrl_U:
  910.         /* delete all characters left of the cursor */
  911.         j = ccline.cmdpos;
  912.         ccline.cmdlen -= j;
  913.         i = ccline.cmdpos = 0;
  914.         while (i < ccline.cmdlen)
  915.             ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
  916.         /* Truncate at the end, required for multi-byte chars. */
  917.         ccline.cmdbuff[ccline.cmdlen] = NUL;
  918.         redrawcmd();
  919.         goto cmdline_changed;
  920.  
  921. #ifdef FEAT_CLIPBOARD
  922.     case Ctrl_Y:
  923.         /* Copy the modeless selection, if there is one. */
  924.         if (clip_star.state != SELECT_CLEARED)
  925.         {
  926.             if (clip_star.state == SELECT_DONE)
  927.             clip_copy_modeless_selection(TRUE);
  928.             goto cmdline_not_changed;
  929.         }
  930.         break;
  931. #endif
  932.  
  933.     case ESC:    /* get here if p_wc != ESC or when ESC typed twice */
  934.     case Ctrl_C:
  935.         /* In exmode it doesn't make sense to return. */
  936.         if (exmode_active)
  937.             goto cmdline_not_changed;
  938.  
  939.         gotesc = TRUE;        /* will free ccline.cmdbuff after
  940.                        putting it in history */
  941.         goto returncmd;        /* back to cmd mode */
  942.  
  943.     case Ctrl_R:            /* insert register */
  944. #ifdef USE_ON_FLY_SCROLL
  945.         dont_scroll = TRUE;    /* disallow scrolling here */
  946. #endif
  947.         putcmdline('"', TRUE);
  948.         ++no_mapping;
  949.         i = c = safe_vgetc();    /* CTRL-R <char> */
  950.         if (i == Ctrl_O)
  951.             i = Ctrl_R;        /* CTRL-R CTRL-O == CTRL-R CTRL-R */
  952.         if (i == Ctrl_R)
  953.             c = safe_vgetc();    /* CTRL-R CTRL-R <char> */
  954.         --no_mapping;
  955. #ifdef FEAT_EVAL
  956.         /*
  957.          * Insert the result of an expression.
  958.          * Need to save the current command line, to be able to enter
  959.          * a new one...
  960.          */
  961.         if (c == '=')
  962.         {
  963.             struct cmdline_info        save_ccline;
  964.  
  965.             if (ccline.cmdfirstc == '=')/* can't do this recursively */
  966.             {
  967.             beep_flush();
  968.             c = ESC;
  969.             }
  970.             else
  971.             {
  972.             save_ccline = ccline;
  973.             ccline.cmdbuff = NULL;
  974.             ccline.cmdprompt = NULL;
  975.             c = get_expr_register();
  976.             ccline = save_ccline;
  977.             }
  978.         }
  979. #endif
  980.         if (c != ESC)        /* use ESC to cancel inserting register */
  981.         {
  982.             cmdline_paste(c, i == Ctrl_R);
  983.             KeyTyped = FALSE;    /* Don't do p_wc completion. */
  984.         }
  985.         redrawcmd();
  986.         goto cmdline_changed;
  987.  
  988.     case Ctrl_D:
  989.         if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
  990.             break;    /* Use ^D as normal char instead */
  991.  
  992.         redrawcmd();
  993.         continue;    /* don't do incremental search now */
  994.  
  995.     case K_RIGHT:
  996.     case K_S_RIGHT:
  997.     case K_C_RIGHT:
  998.         do
  999.         {
  1000.             if (ccline.cmdpos >= ccline.cmdlen)
  1001.             break;
  1002.             i = cmdline_charsize(ccline.cmdpos);
  1003.             if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
  1004.             break;
  1005.             ccline.cmdspos += i;
  1006. #ifdef FEAT_MBYTE
  1007.             if (has_mbyte)
  1008.             ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
  1009.                                  + ccline.cmdpos);
  1010.             else
  1011. #endif
  1012.             ++ccline.cmdpos;
  1013.         }
  1014.         while ((c == K_S_RIGHT || c == K_C_RIGHT)
  1015.             && ccline.cmdbuff[ccline.cmdpos] != ' ');
  1016. #ifdef FEAT_MBYTE
  1017.         if (has_mbyte)
  1018.             set_cmdspos_cursor();
  1019. #endif
  1020.         goto cmdline_not_changed;
  1021.  
  1022.     case K_LEFT:
  1023.     case K_S_LEFT:
  1024.     case K_C_LEFT:
  1025.         do
  1026.         {
  1027.             if (ccline.cmdpos == 0)
  1028.             break;
  1029.             --ccline.cmdpos;
  1030. #ifdef FEAT_MBYTE
  1031.             if (has_mbyte)    /* move to first byte of char */
  1032.             ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
  1033.                           ccline.cmdbuff + ccline.cmdpos);
  1034. #endif
  1035.             ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
  1036.         }
  1037.         while ((c == K_S_LEFT || c == K_C_LEFT)
  1038.             && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
  1039. #ifdef FEAT_MBYTE
  1040.         if (has_mbyte)
  1041.             set_cmdspos_cursor();
  1042. #endif
  1043.         goto cmdline_not_changed;
  1044.  
  1045.     case K_IGNORE:
  1046.         goto cmdline_not_changed;    /* Ignore mouse */
  1047.  
  1048. #ifdef FEAT_MOUSE
  1049.     case K_MIDDLEDRAG:
  1050.     case K_MIDDLERELEASE:
  1051.         goto cmdline_not_changed;    /* Ignore mouse */
  1052.  
  1053.     case K_MIDDLEMOUSE:
  1054. # ifdef FEAT_GUI
  1055.         /* When GUI is active, also paste when 'mouse' is empty */
  1056.         if (!gui.in_use)
  1057. # endif
  1058.             if (!mouse_has(MOUSE_COMMAND))
  1059.             goto cmdline_not_changed;   /* Ignore mouse */
  1060. #ifdef FEAT_CLIPBOARD
  1061.         if (clip_star.available)
  1062.             cmdline_paste('*', TRUE);
  1063.         else
  1064. #endif
  1065.             cmdline_paste(0, TRUE);
  1066.         redrawcmd();
  1067.         goto cmdline_changed;
  1068.  
  1069. #ifdef FEAT_DND
  1070.     case K_DROP:
  1071.         cmdline_paste('~', TRUE);
  1072.         redrawcmd();
  1073.         goto cmdline_changed;
  1074. #endif
  1075.  
  1076.     case K_LEFTDRAG:
  1077.     case K_LEFTRELEASE:
  1078.     case K_RIGHTDRAG:
  1079.     case K_RIGHTRELEASE:
  1080.         /* Ignore drag and release events when the button-down wasn't
  1081.          * seen before. */
  1082.         if (ignore_drag_release)
  1083.             goto cmdline_not_changed;
  1084.         /* FALLTHROUGH */
  1085.     case K_LEFTMOUSE:
  1086.     case K_RIGHTMOUSE:
  1087.         if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
  1088.             ignore_drag_release = TRUE;
  1089.         else
  1090.             ignore_drag_release = FALSE;
  1091. # ifdef FEAT_GUI
  1092.         /* When GUI is active, also move when 'mouse' is empty */
  1093.         if (!gui.in_use)
  1094. # endif
  1095.             if (!mouse_has(MOUSE_COMMAND))
  1096.             goto cmdline_not_changed;   /* Ignore mouse */
  1097. # ifdef FEAT_CLIPBOARD
  1098.         if (mouse_row < cmdline_row && clip_star.available)
  1099.         {
  1100.             int        button, is_click, is_drag;
  1101.  
  1102.             /*
  1103.              * Handle modeless selection.
  1104.              */
  1105.             button = get_mouse_button(KEY2TERMCAP1(c),
  1106.                              &is_click, &is_drag);
  1107.             if (mouse_model_popup() && button == MOUSE_LEFT
  1108.                            && (mod_mask & MOD_MASK_SHIFT))
  1109.             {
  1110.             /* Translate shift-left to right button. */
  1111.             button = MOUSE_RIGHT;
  1112.             mod_mask &= ~MOD_MASK_SHIFT;
  1113.             }
  1114.             clip_modeless(button, is_click, is_drag);
  1115.             goto cmdline_not_changed;
  1116.         }
  1117. # endif
  1118.  
  1119.         set_cmdspos();
  1120.         for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
  1121.                                   ++ccline.cmdpos)
  1122.         {
  1123.             i = cmdline_charsize(ccline.cmdpos);
  1124.             if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
  1125.                   && mouse_col < ccline.cmdspos % Columns + i)
  1126.             break;
  1127. #ifdef FEAT_MBYTE
  1128.             if (has_mbyte)
  1129.             {
  1130.             /* Count ">" for double-wide char that doesn't fit. */
  1131.             correct_cmdspos(ccline.cmdpos, i);
  1132.             ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
  1133.                              + ccline.cmdpos) - 1;
  1134.             }
  1135. #endif
  1136.             ccline.cmdspos += i;
  1137.         }
  1138.         goto cmdline_not_changed;
  1139.  
  1140.     /* Mouse scroll wheel: ignored here */
  1141.     case K_MOUSEDOWN:
  1142.     case K_MOUSEUP:
  1143.     /* Alternate buttons ignored here */
  1144.     case K_X1MOUSE:
  1145.     case K_X1DRAG:
  1146.     case K_X1RELEASE:
  1147.     case K_X2MOUSE:
  1148.     case K_X2DRAG:
  1149.     case K_X2RELEASE:
  1150.         goto cmdline_not_changed;
  1151.  
  1152. #endif    /* FEAT_MOUSE */
  1153.  
  1154. #ifdef FEAT_GUI
  1155.     case K_LEFTMOUSE_NM:    /* mousefocus click, ignored */
  1156.     case K_LEFTRELEASE_NM:
  1157.         goto cmdline_not_changed;
  1158.  
  1159.     case K_VER_SCROLLBAR:
  1160.         if (!msg_scrolled)
  1161.         {
  1162.             gui_do_scroll();
  1163.             redrawcmd();
  1164.         }
  1165.         goto cmdline_not_changed;
  1166.  
  1167.     case K_HOR_SCROLLBAR:
  1168.         if (!msg_scrolled)
  1169.         {
  1170.             gui_do_horiz_scroll();
  1171.             redrawcmd();
  1172.         }
  1173.         goto cmdline_not_changed;
  1174. #endif
  1175.     case K_SELECT:        /* end of Select mode mapping - ignore */
  1176.         goto cmdline_not_changed;
  1177.  
  1178.     case Ctrl_B:        /* begin of command line */
  1179.     case K_HOME:
  1180.     case K_KHOME:
  1181.     case K_XHOME:
  1182.     case K_S_HOME:
  1183.     case K_C_HOME:
  1184.         ccline.cmdpos = 0;
  1185.         set_cmdspos();
  1186.         goto cmdline_not_changed;
  1187.  
  1188.     case Ctrl_E:        /* end of command line */
  1189.     case K_END:
  1190.     case K_KEND:
  1191.     case K_XEND:
  1192.     case K_S_END:
  1193.     case K_C_END:
  1194.         ccline.cmdpos = ccline.cmdlen;
  1195.         set_cmdspos_cursor();
  1196.         goto cmdline_not_changed;
  1197.  
  1198.     case Ctrl_A:        /* all matches */
  1199.         if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
  1200.             break;
  1201.         goto cmdline_changed;
  1202.  
  1203.     case Ctrl_L:        /* longest common part */
  1204.         if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
  1205.             break;
  1206.         goto cmdline_changed;
  1207.  
  1208.     case Ctrl_N:        /* next match */
  1209.     case Ctrl_P:        /* previous match */
  1210.         if (cmd_numfiles > 0)
  1211.         {
  1212.             if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
  1213.                                       == FAIL)
  1214.             break;
  1215.             goto cmdline_changed;
  1216.         }
  1217.  
  1218. #ifdef FEAT_CMDHIST
  1219.     case K_UP:
  1220.     case K_DOWN:
  1221.     case K_S_UP:
  1222.     case K_S_DOWN:
  1223.     case K_PAGEUP:
  1224.     case K_KPAGEUP:
  1225.     case K_PAGEDOWN:
  1226.     case K_KPAGEDOWN:
  1227.         if (hislen == 0 || firstc == NUL)    /* no history */
  1228.             goto cmdline_not_changed;
  1229.  
  1230.         i = hiscnt;
  1231.  
  1232.         /* save current command string so it can be restored later */
  1233.         if (lookfor == NULL)
  1234.         {
  1235.             if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
  1236.             goto cmdline_not_changed;
  1237.             lookfor[ccline.cmdpos] = NUL;
  1238.         }
  1239.  
  1240.         j = (int)STRLEN(lookfor);
  1241.         for (;;)
  1242.         {
  1243.             /* one step backwards */
  1244.             if (c == K_UP || c == K_S_UP || c == Ctrl_P ||
  1245.                 c == K_PAGEUP || c == K_KPAGEUP)
  1246.             {
  1247.             if (hiscnt == hislen)    /* first time */
  1248.                 hiscnt = hisidx[histype];
  1249.             else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
  1250.                 hiscnt = hislen - 1;
  1251.             else if (hiscnt != hisidx[histype] + 1)
  1252.                 --hiscnt;
  1253.             else            /* at top of list */
  1254.             {
  1255.                 hiscnt = i;
  1256.                 break;
  1257.             }
  1258.             }
  1259.             else    /* one step forwards */
  1260.             {
  1261.             /* on last entry, clear the line */
  1262.             if (hiscnt == hisidx[histype])
  1263.             {
  1264.                 hiscnt = hislen;
  1265.                 break;
  1266.             }
  1267.  
  1268.             /* not on a history line, nothing to do */
  1269.             if (hiscnt == hislen)
  1270.                 break;
  1271.             if (hiscnt == hislen - 1)   /* wrap around */
  1272.                 hiscnt = 0;
  1273.             else
  1274.                 ++hiscnt;
  1275.             }
  1276.             if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
  1277.             {
  1278.             hiscnt = i;
  1279.             break;
  1280.             }
  1281.             if ((c != K_UP && c != K_DOWN) || hiscnt == i
  1282.                 || STRNCMP(history[histype][hiscnt].hisstr,
  1283.                             lookfor, (size_t)j) == 0)
  1284.             break;
  1285.         }
  1286.  
  1287.         if (hiscnt != i)    /* jumped to other entry */
  1288.         {
  1289.             char_u    *p;
  1290.  
  1291.             vim_free(ccline.cmdbuff);
  1292.             if (hiscnt == hislen)
  1293.             p = lookfor;    /* back to the old one */
  1294.             else
  1295.             p = history[histype][hiscnt].hisstr;
  1296.  
  1297.             alloc_cmdbuff((int)STRLEN(p));
  1298.             if (ccline.cmdbuff == NULL)
  1299.             goto returncmd;
  1300.             STRCPY(ccline.cmdbuff, p);
  1301.  
  1302.             ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
  1303.             redrawcmd();
  1304.             goto cmdline_changed;
  1305.         }
  1306.         beep_flush();
  1307.         goto cmdline_not_changed;
  1308. #endif
  1309.  
  1310.     case Ctrl_V:
  1311.     case Ctrl_Q:
  1312. #ifdef FEAT_MOUSE
  1313.         ignore_drag_release = TRUE;
  1314. #endif
  1315.         putcmdline('^', TRUE);
  1316.         c = get_literal();        /* get next (two) character(s) */
  1317.         do_abbr = FALSE;        /* don't do abbreviation now */
  1318. #ifdef FEAT_MBYTE
  1319.         /* may need to remove ^ when composing char was typed */
  1320.         if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
  1321.         {
  1322.             draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
  1323.             msg_putchar(' ');
  1324.             cursorcmd();
  1325.         }
  1326. #endif
  1327.         break;
  1328.  
  1329. #ifdef FEAT_DIGRAPHS
  1330.     case Ctrl_K:
  1331. #ifdef FEAT_MOUSE
  1332.         ignore_drag_release = TRUE;
  1333. #endif
  1334.         putcmdline('?', TRUE);
  1335. #ifdef USE_ON_FLY_SCROLL
  1336.         dont_scroll = TRUE;        /* disallow scrolling here */
  1337. #endif
  1338.         c = get_digraph(TRUE);
  1339.         if (c != NUL)
  1340.             break;
  1341.  
  1342.         redrawcmd();
  1343.         goto cmdline_not_changed;
  1344. #endif /* FEAT_DIGRAPHS */
  1345.  
  1346. #ifdef FEAT_RIGHTLEFT
  1347.     case Ctrl__:        /* CTRL-_: switch language mode */
  1348.         if (!p_ari)
  1349.             break;
  1350. #ifdef FEAT_FKMAP
  1351.         if (p_altkeymap)
  1352.         {
  1353.             cmd_fkmap = !cmd_fkmap;
  1354.             if (cmd_fkmap)    /* in Farsi always in Insert mode */
  1355.             ccline.overstrike = FALSE;
  1356.         }
  1357.         else                /* Hebrew is default */
  1358. #endif
  1359.             cmd_hkmap = !cmd_hkmap;
  1360.         goto cmdline_not_changed;
  1361. #endif
  1362.  
  1363.     default:
  1364. #ifdef UNIX
  1365.         if (c == intr_char)
  1366.         {
  1367.             gotesc = TRUE;    /* will free ccline.cmdbuff after
  1368.                        putting it in history */
  1369.             goto returncmd;    /* back to Normal mode */
  1370.         }
  1371. #endif
  1372.         /*
  1373.          * Normal character with no special meaning.  Just set mod_mask
  1374.          * to 0x0 so that typing Shift-Space in the GUI doesn't enter
  1375.          * the string <S-Space>.  This should only happen after ^V.
  1376.          */
  1377.         if (!IS_SPECIAL(c))
  1378.             mod_mask = 0x0;
  1379.         break;
  1380.     }
  1381.     /*
  1382.      * End of switch on command line character.
  1383.      * We come here if we have a normal character.
  1384.      */
  1385.  
  1386.     if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
  1387. #ifdef FEAT_MBYTE
  1388.             /* Add ABBR_OFF for characters above 0x100, this is
  1389.              * what check_abbr() expects. */
  1390.             (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
  1391. #endif
  1392.                                     c))
  1393.         goto cmdline_changed;
  1394.  
  1395.     /*
  1396.      * put the character in the command line
  1397.      */
  1398.     if (IS_SPECIAL(c) || mod_mask != 0)
  1399.         put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
  1400.     else
  1401.     {
  1402. #ifdef FEAT_MBYTE
  1403.         if (has_mbyte)
  1404.         {
  1405.         j = (*mb_char2bytes)(c, IObuff);
  1406.         IObuff[j] = NUL;    /* exclude composing chars */
  1407.         put_on_cmdline(IObuff, j, TRUE);
  1408.         }
  1409.         else
  1410. #endif
  1411.         {
  1412.         IObuff[0] = c;
  1413.         put_on_cmdline(IObuff, 1, TRUE);
  1414.         }
  1415.     }
  1416.     goto cmdline_changed;
  1417.  
  1418. /*
  1419.  * This part implements incremental searches for "/" and "?"
  1420.  * Jump to cmdline_not_changed when a character has been read but the command
  1421.  * line did not change. Then we only search and redraw if something changed in
  1422.  * the past.
  1423.  * Jump to cmdline_changed when the command line did change.
  1424.  * (Sorry for the goto's, I know it is ugly).
  1425.  */
  1426. cmdline_not_changed:
  1427. #ifdef FEAT_SEARCH_EXTRA
  1428.     if (!incsearch_postponed)
  1429.         continue;
  1430. #endif
  1431.  
  1432. cmdline_changed:
  1433. #ifdef FEAT_SEARCH_EXTRA
  1434.     /*
  1435.      * 'incsearch' highlighting.
  1436.      */
  1437.     if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
  1438.     {
  1439.         /* if there is a character waiting, search and redraw later */
  1440.         if (char_avail())
  1441.         {
  1442.         incsearch_postponed = TRUE;
  1443.         continue;
  1444.         }
  1445.         incsearch_postponed = FALSE;
  1446.         curwin->w_cursor = old_cursor;  /* start at old position */
  1447.  
  1448.         /* If there is no command line, don't do anything */
  1449.         if (ccline.cmdlen == 0)
  1450.         i = 0;
  1451.         else
  1452.         {
  1453.         cursor_off();        /* so the user knows we're busy */
  1454.         out_flush();
  1455.         ++emsg_off;    /* So it doesn't beep if bad expr */
  1456.         i = do_search(NULL, firstc, ccline.cmdbuff, count,
  1457.             SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
  1458.         --emsg_off;
  1459.         /* if interrupted while searching, behave like it failed */
  1460.         if (got_int)
  1461.         {
  1462.             (void)vpeekc();    /* remove <C-C> from input stream */
  1463.             got_int = FALSE;    /* don't abandon the command line */
  1464.             i = 0;
  1465.         }
  1466.         else if (char_avail())
  1467.             /* cancelled searching because a char was typed */
  1468.             incsearch_postponed = TRUE;
  1469.         }
  1470.         if (i)
  1471.         highlight_match = TRUE;        /* highlight position */
  1472.         else
  1473.         highlight_match = FALSE;    /* remove highlight */
  1474.  
  1475.         /* first restore the old curwin values, so the screen is
  1476.          * positioned in the same way as the actual search command */
  1477.         curwin->w_leftcol = old_leftcol;
  1478.         curwin->w_topline = old_topline;
  1479. # ifdef FEAT_DIFF
  1480.         curwin->w_topfill = old_topfill;
  1481. # endif
  1482.         curwin->w_botline = old_botline;
  1483.         changed_cline_bef_curs();
  1484.         update_topline();
  1485.  
  1486.         if (i != 0)
  1487.         {
  1488.         /*
  1489.          * First move cursor to end of match, then to start.  This
  1490.          * moves the whole match onto the screen when 'nowrap' is set.
  1491.          */
  1492.         i = curwin->w_cursor.col;
  1493.         curwin->w_cursor.lnum += search_match_lines;
  1494.         curwin->w_cursor.col = search_match_endcol;
  1495.         validate_cursor();
  1496.         curwin->w_cursor.lnum -= search_match_lines;
  1497.         curwin->w_cursor.col = i;
  1498.         }
  1499.         validate_cursor();
  1500.  
  1501.         update_screen(NOT_VALID);
  1502.         msg_starthere();
  1503.         redrawcmdline();
  1504.         did_incsearch = TRUE;
  1505.     }
  1506. #else /* FEAT_SEARCH_EXTRA */
  1507.     ;
  1508. #endif
  1509.  
  1510. #ifdef FEAT_RIGHTLEFT
  1511.     if (cmdmsg_rl
  1512. # ifdef FEAT_ARABIC
  1513.         || p_arshape
  1514. # endif
  1515.         )
  1516.         /* Always redraw the whole command line to fix shaping and
  1517.          * right-left typing.  Not efficient, but it works. */
  1518.         redrawcmd();
  1519. #endif
  1520.     }
  1521.  
  1522. returncmd:
  1523.  
  1524. #ifdef FEAT_RIGHTLEFT
  1525.     cmdmsg_rl = FALSE;
  1526. #endif
  1527.  
  1528. #ifdef FEAT_FKMAP
  1529.     cmd_fkmap = 0;
  1530. #endif
  1531.  
  1532. #ifdef FEAT_SEARCH_EXTRA
  1533.     if (did_incsearch)
  1534.     {
  1535.     curwin->w_cursor = old_cursor;
  1536.     curwin->w_curswant = old_curswant;
  1537.     curwin->w_leftcol = old_leftcol;
  1538.     curwin->w_topline = old_topline;
  1539. # ifdef FEAT_DIFF
  1540.     curwin->w_topfill = old_topfill;
  1541. # endif
  1542.     curwin->w_botline = old_botline;
  1543.     highlight_match = FALSE;
  1544.     validate_cursor();    /* needed for TAB */
  1545.     redraw_later(NOT_VALID);
  1546.     }
  1547. #endif
  1548.  
  1549.     if (ccline.cmdbuff != NULL)
  1550.     {
  1551.     /*
  1552.      * Put line in history buffer (":" and "=" only when it was typed).
  1553.      */
  1554. #ifdef FEAT_CMDHIST
  1555.     if (ccline.cmdlen && firstc != NUL
  1556.         && (some_key_typed || histype == HIST_SEARCH))
  1557.     {
  1558.         add_to_history(histype, ccline.cmdbuff, TRUE);
  1559.         if (firstc == ':')
  1560.         {
  1561.         vim_free(new_last_cmdline);
  1562.         new_last_cmdline = vim_strsave(ccline.cmdbuff);
  1563.         }
  1564.     }
  1565. #endif
  1566.  
  1567.     if (gotesc)        /* abandon command line */
  1568.     {
  1569.         vim_free(ccline.cmdbuff);
  1570.         ccline.cmdbuff = NULL;
  1571.         if (msg_scrolled == 0)
  1572.         compute_cmdrow();
  1573.         MSG("");
  1574.         redraw_cmdline = TRUE;
  1575.     }
  1576.     }
  1577.  
  1578.     /*
  1579.      * If the screen was shifted up, redraw the whole screen (later).
  1580.      * If the line is too long, clear it, so ruler and shown command do
  1581.      * not get printed in the middle of it.
  1582.      */
  1583.     msg_check();
  1584.     msg_scroll = save_msg_scroll;
  1585.     redir_off = FALSE;
  1586.  
  1587.     /* When the command line was typed, no need for a wait-return prompt. */
  1588.     if (some_key_typed)
  1589.     need_wait_return = FALSE;
  1590.  
  1591.     State = save_State;
  1592. #ifdef USE_IM_CONTROL
  1593.     if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
  1594.     im_save_status(b_im_ptr);
  1595.     im_set_active(FALSE);
  1596. #endif
  1597. #ifdef FEAT_MOUSE
  1598.     setmouse();
  1599. #endif
  1600. #ifdef CURSOR_SHAPE
  1601.     ui_cursor_shape();        /* may show different cursor shape */
  1602. #endif
  1603.  
  1604.     return ccline.cmdbuff;
  1605. }
  1606.  
  1607. #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
  1608. /*
  1609.  * Get a command line with a prompt.
  1610.  * This is prepared to be called recursively from getcmdline() (e.g. by
  1611.  * f_input() when evaluating an expression from CTRL-R =).
  1612.  * Returns the command line in allocated memory, or NULL.
  1613.  */
  1614.     char_u *
  1615. getcmdline_prompt(firstc, prompt, attr)
  1616.     int        firstc;
  1617.     char_u    *prompt;    /* command line prompt */
  1618.     int        attr;        /* attributes for prompt */
  1619. {
  1620.     char_u        *s;
  1621.     struct cmdline_info    save_ccline;
  1622.     int            msg_col_save = msg_col;
  1623.  
  1624.     save_ccline = ccline;
  1625.     ccline.cmdbuff = NULL;
  1626.     ccline.cmdprompt = prompt;
  1627.     ccline.cmdattr = attr;
  1628.     s = getcmdline(firstc, 1L, 0);
  1629.     ccline = save_ccline;
  1630.     /* Restore msg_col, the prompt from input() may have changed it. */
  1631.     msg_col = msg_col_save;
  1632.  
  1633.     return s;
  1634. }
  1635. #endif
  1636.  
  1637.     static int
  1638. cmdline_charsize(idx)
  1639.     int        idx;
  1640. {
  1641. #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
  1642.     if (cmdline_star > 0)        /* showing '*', always 1 position */
  1643.     return 1;
  1644. #endif
  1645.     return ptr2cells(ccline.cmdbuff + idx);
  1646. }
  1647.  
  1648. /*
  1649.  * Compute the offset of the cursor on the command line for the prompt and
  1650.  * indent.
  1651.  */
  1652.     static void
  1653. set_cmdspos()
  1654. {
  1655.     if (ccline.cmdfirstc)
  1656.     ccline.cmdspos = 1 + ccline.cmdindent;
  1657.     else
  1658.     ccline.cmdspos = 0 + ccline.cmdindent;
  1659. }
  1660.  
  1661. /*
  1662.  * Compute the screen position for the cursor on the command line.
  1663.  */
  1664.     static void
  1665. set_cmdspos_cursor()
  1666. {
  1667.     int        i, m, c;
  1668.  
  1669.     set_cmdspos();
  1670.     if (KeyTyped)
  1671.     m = Columns * Rows;
  1672.     else
  1673.     m = MAXCOL;
  1674.     for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
  1675.     {
  1676.     c = cmdline_charsize(i);
  1677. #ifdef FEAT_MBYTE
  1678.     /* Count ">" for double-wide multi-byte char that doesn't fit. */
  1679.     if (has_mbyte)
  1680.         correct_cmdspos(i, c);
  1681. #endif
  1682.     /* If the cmdline doesn't fit, put cursor on last visible char. */
  1683.     if ((ccline.cmdspos += c) >= m)
  1684.     {
  1685.         ccline.cmdpos = i - 1;
  1686.         ccline.cmdspos -= c;
  1687.         break;
  1688.     }
  1689. #ifdef FEAT_MBYTE
  1690.     if (has_mbyte)
  1691.         i += (*mb_ptr2len_check)(ccline.cmdbuff + i) - 1;
  1692. #endif
  1693.     }
  1694. }
  1695.  
  1696. #ifdef FEAT_MBYTE
  1697. /*
  1698.  * Check if the character at "idx", which is "cells" wide, is a multi-byte
  1699.  * character that doesn't fit, so that a ">" must be displayed.
  1700.  */
  1701.     static void
  1702. correct_cmdspos(idx, cells)
  1703.     int        idx;
  1704.     int        cells;
  1705. {
  1706.     if ((*mb_ptr2len_check)(ccline.cmdbuff + idx) > 1
  1707.         && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
  1708.         && ccline.cmdspos % Columns + cells > Columns)
  1709.     ccline.cmdspos++;
  1710. }
  1711. #endif
  1712.  
  1713. /*
  1714.  * Get an Ex command line for the ":" command.
  1715.  */
  1716. /* ARGSUSED */
  1717.     char_u *
  1718. getexline(c, dummy, indent)
  1719.     int        c;        /* normally ':', NUL for ":append" */
  1720.     void    *dummy;        /* cookie not used */
  1721.     int        indent;        /* indent for inside conditionals */
  1722. {
  1723.     /* When executing a register, remove ':' that's in front of each line. */
  1724.     if (exec_from_reg && vpeekc() == ':')
  1725.     (void)vgetc();
  1726.     return getcmdline(c, 1L, indent);
  1727. }
  1728.  
  1729. /*
  1730.  * Get an Ex command line for Ex mode.
  1731.  * In Ex mode we only use the OS supplied line editing features and no
  1732.  * mappings or abbreviations.
  1733.  */
  1734. /* ARGSUSED */
  1735.     char_u *
  1736. getexmodeline(c, dummy, indent)
  1737.     int        c;        /* normally ':', NUL for ":append" */
  1738.     void    *dummy;        /* cookie not used */
  1739.     int        indent;        /* indent for inside conditionals */
  1740. {
  1741.     garray_T        line_ga;
  1742.     int            len;
  1743.     int            off = 0;
  1744.     char_u        *p;
  1745.     int            finished = FALSE;
  1746. #if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
  1747.     int            startcol = 0;
  1748.     int            c1;
  1749.     int            escaped = FALSE;    /* CTRL-V typed */
  1750.     int            vcol = 0;
  1751. #endif
  1752.  
  1753.     /* Switch cursor on now.  This avoids that it happens after the "\n", which
  1754.      * confuses the system function that computes tabstops. */
  1755.     cursor_on();
  1756.  
  1757.     /* always start in column 0; write a newline if necessary */
  1758.     compute_cmdrow();
  1759.     if (msg_col)
  1760.     msg_putchar('\n');
  1761.     if (c == ':')
  1762.     {
  1763.     msg_putchar(':');
  1764.     while (indent-- > 0)
  1765.         msg_putchar(' ');
  1766. #if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
  1767.     startcol = msg_col;
  1768. #endif
  1769.     }
  1770.  
  1771.     ga_init2(&line_ga, 1, 30);
  1772.  
  1773.     /*
  1774.      * Get the line, one character at a time.
  1775.      */
  1776.     got_int = FALSE;
  1777.     while (!got_int && !finished)
  1778.     {
  1779.     if (ga_grow(&line_ga, 40) == FAIL)
  1780.         break;
  1781.     p = (char_u *)line_ga.ga_data + line_ga.ga_len;
  1782.  
  1783.     /* Get one character (inchar gets a third of maxlen characters!) */
  1784.     len = inchar(p + off, 3, -1L);
  1785.     if (len < 0)
  1786.         continue;        /* end of input script reached */
  1787.     /* for a special character, we need at least three characters */
  1788.     if ((*p == K_SPECIAL || *p == CSI) && off + len < 3)
  1789.     {
  1790.         off += len;
  1791.         continue;
  1792.     }
  1793.     len += off;
  1794.     off = 0;
  1795.  
  1796.     /*
  1797.      * When using the GUI, and for systems that don't have cooked input,
  1798.      * handle line editing here.
  1799.      */
  1800. #if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
  1801. # ifndef NO_COOKED_INPUT
  1802.     if (gui.in_use)
  1803. # endif
  1804.     {
  1805.         if (got_int)
  1806.         {
  1807.         msg_putchar('\n');
  1808.         break;
  1809.         }
  1810.  
  1811.         while (len > 0)
  1812.         {
  1813.         c1 = *p++;
  1814.         --len;
  1815.         if ((c1 == K_SPECIAL
  1816. #  if !defined(NO_COOKED_INPUT) || defined(FEAT_GUI)
  1817.                 || c1 == CSI
  1818. #  endif
  1819.             ) && len >= 2)
  1820.         {
  1821.             c1 = TO_SPECIAL(p[0], p[1]);
  1822.             p += 2;
  1823.             len -= 2;
  1824.         }
  1825.  
  1826.         if (!escaped)
  1827.         {
  1828.             /* CR typed means "enter", which is NL */
  1829.             if (c1 == '\r')
  1830.             c1 = '\n';
  1831.  
  1832.             if (c1 == BS || c1 == K_BS
  1833.                   || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
  1834.             {
  1835.             if (line_ga.ga_len > 0)
  1836.             {
  1837.                 int        i, v;
  1838.                 char_u    *q;
  1839.  
  1840.                 --line_ga.ga_len;
  1841.                 ++line_ga.ga_room;
  1842.                 /* compute column that cursor should be in */
  1843.                 v = 0;
  1844.                 q = ((char_u *)line_ga.ga_data);
  1845.                 for (i = 0; i < line_ga.ga_len; ++i)
  1846.                 {
  1847.                 if (*q == TAB)
  1848.                     v += 8 - v % 8;
  1849.                 else
  1850.                     v += ptr2cells(q);
  1851.                 ++q;
  1852.                 }
  1853.                 /* erase characters to position cursor */
  1854.                 while (vcol > v)
  1855.                 {
  1856.                 msg_putchar('\b');
  1857.                 msg_putchar(' ');
  1858.                 msg_putchar('\b');
  1859.                 --vcol;
  1860.                 }
  1861.             }
  1862.             continue;
  1863.             }
  1864.  
  1865.             if (c1 == Ctrl_U)
  1866.             {
  1867.             msg_col = startcol;
  1868.             msg_clr_eos();
  1869.             line_ga.ga_room += line_ga.ga_len;
  1870.             line_ga.ga_len = 0;
  1871.             continue;
  1872.             }
  1873.  
  1874.             if (c1 == Ctrl_V)
  1875.             {
  1876.             escaped = TRUE;
  1877.             continue;
  1878.             }
  1879.         }
  1880.  
  1881.         if (IS_SPECIAL(c1))
  1882.             c1 = '?';
  1883.         ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
  1884.         if (c1 == '\n')
  1885.             msg_putchar('\n');
  1886.         else if (c1 == TAB)
  1887.         {
  1888.             /* Don't use chartabsize(), 'ts' can be different */
  1889.             do
  1890.             {
  1891.             msg_putchar(' ');
  1892.             } while (++vcol % 8);
  1893.         }
  1894.         else
  1895.         {
  1896.             msg_outtrans_len(
  1897.                  ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
  1898.             vcol += char2cells(c1);
  1899.         }
  1900.         ++line_ga.ga_len;
  1901.         --line_ga.ga_room;
  1902.         escaped = FALSE;
  1903.         }
  1904.         windgoto(msg_row, msg_col);
  1905.     }
  1906. # ifndef NO_COOKED_INPUT
  1907.     else
  1908. # endif
  1909. #endif
  1910. #ifndef NO_COOKED_INPUT
  1911.     {
  1912.         line_ga.ga_len += len;
  1913.         line_ga.ga_room -= len;
  1914.     }
  1915. #endif
  1916.     p = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
  1917.     if (line_ga.ga_len && p[-1] == '\n')
  1918.     {
  1919.         finished = TRUE;
  1920.         --line_ga.ga_len;
  1921.         --p;
  1922.         *p = NUL;
  1923.     }
  1924.     }
  1925.  
  1926.     /* note that cursor has moved, because of the echoed <CR> */
  1927.     screen_down();
  1928.     /* make following messages go to the next line */
  1929.     msg_didout = FALSE;
  1930.     msg_col = 0;
  1931.     if (msg_row < Rows - 1)
  1932.     ++msg_row;
  1933.     emsg_on_display = FALSE;        /* don't want ui_delay() */
  1934.  
  1935.     if (got_int)
  1936.     ga_clear(&line_ga);
  1937.  
  1938.     return (char_u *)line_ga.ga_data;
  1939. }
  1940.  
  1941. #ifdef CURSOR_SHAPE
  1942. /*
  1943.  * Return TRUE if ccline.overstrike is on.
  1944.  */
  1945.     int
  1946. cmdline_overstrike()
  1947. {
  1948.     return ccline.overstrike;
  1949. }
  1950.  
  1951. /*
  1952.  * Return TRUE if the cursor is at the end of the cmdline.
  1953.  */
  1954.     int
  1955. cmdline_at_end()
  1956. {
  1957.     return (ccline.cmdpos >= ccline.cmdlen);
  1958. }
  1959. #endif
  1960.  
  1961. #if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
  1962. /*
  1963.  * Return the virtual column number at the current cursor position.
  1964.  * This is used by the IM code to obtain the start of the preedit string.
  1965.  */
  1966.     colnr_T
  1967. cmdline_getvcol_cursor()
  1968. {
  1969.     if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
  1970.     return MAXCOL;
  1971.  
  1972. # ifdef FEAT_MBYTE
  1973.     if (has_mbyte)
  1974.     {
  1975.     colnr_T    col;
  1976.     int    i = 0;
  1977.  
  1978.     for (col = 0; i < ccline.cmdpos; ++col)
  1979.         i += (*mb_ptr2len_check)(ccline.cmdbuff + i);
  1980.  
  1981.     return col;
  1982.     }
  1983.     else
  1984. # endif
  1985.     return ccline.cmdpos;
  1986. }
  1987. #endif
  1988.  
  1989. #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
  1990. /*
  1991.  * If part of the command line is an IM preedit string, redraw it with
  1992.  * IM feedback attributes.  The cursor position is restored after drawing.
  1993.  */
  1994.     static void
  1995. redrawcmd_preedit()
  1996. {
  1997.     if ((State & CMDLINE)
  1998.     && xic != NULL && im_get_status() && !p_imdisable
  1999.     && preedit_start_col != MAXCOL)
  2000.     {
  2001.     int    cmdpos = 0;
  2002.     int    cmdspos;
  2003.     int    old_row;
  2004.     int    old_col;
  2005.     colnr_T    col;
  2006.  
  2007.     old_row = msg_row;
  2008.     old_col = msg_col;
  2009.     cmdspos = ((ccline.cmdfirstc) ? 1 : 0) + ccline.cmdindent;
  2010.  
  2011. # ifdef FEAT_MBYTE
  2012.     if (has_mbyte)
  2013.     {
  2014.         for (col = 0; col < preedit_start_col
  2015.               && cmdpos < ccline.cmdlen; ++col)
  2016.         {
  2017.         cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
  2018.         cmdpos  += (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
  2019.         }
  2020.     }
  2021.     else
  2022. # endif
  2023.     {
  2024.         cmdspos += preedit_start_col;
  2025.         cmdpos  += preedit_start_col;
  2026.     }
  2027.  
  2028.     msg_row = cmdline_row + (cmdspos / (int)Columns);
  2029.     msg_col = cmdspos % (int)Columns;
  2030.     if (msg_row >= Rows)
  2031.         msg_row = Rows - 1;
  2032.  
  2033.     for (col = 0; cmdpos < ccline.cmdlen; ++col)
  2034.     {
  2035.         int char_len;
  2036.         int char_attr;
  2037.  
  2038.         char_attr = im_get_feedback_attr(col);
  2039.         if (char_attr < 0)
  2040.         break; /* end of preedit string */
  2041.  
  2042. # ifdef FEAT_MBYTE
  2043.         if (has_mbyte)
  2044.         char_len = (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
  2045.         else
  2046. # endif
  2047.         char_len = 1;
  2048.  
  2049.         msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
  2050.         cmdpos += char_len;
  2051.     }
  2052.  
  2053.     msg_row = old_row;
  2054.     msg_col = old_col;
  2055.     }
  2056. }
  2057. #endif /* FEAT_XIM && FEAT_GUI_GTK */
  2058.  
  2059. /*
  2060.  * Allocate a new command line buffer.
  2061.  * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
  2062.  * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
  2063.  */
  2064.     static void
  2065. alloc_cmdbuff(len)
  2066.     int        len;
  2067. {
  2068.     /*
  2069.      * give some extra space to avoid having to allocate all the time
  2070.      */
  2071.     if (len < 80)
  2072.     len = 100;
  2073.     else
  2074.     len += 20;
  2075.  
  2076.     ccline.cmdbuff = alloc(len);    /* caller should check for out-of-memory */
  2077.     ccline.cmdbufflen = len;
  2078. }
  2079.  
  2080. /*
  2081.  * Re-allocate the command line to length len + something extra.
  2082.  * return FAIL for failure, OK otherwise
  2083.  */
  2084.     static int
  2085. realloc_cmdbuff(len)
  2086.     int        len;
  2087. {
  2088.     char_u    *p;
  2089.  
  2090.     p = ccline.cmdbuff;
  2091.     alloc_cmdbuff(len);            /* will get some more */
  2092.     if (ccline.cmdbuff == NULL)        /* out of memory */
  2093.     {
  2094.     ccline.cmdbuff = p;        /* keep the old one */
  2095.     return FAIL;
  2096.     }
  2097.     mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
  2098.     vim_free(p);
  2099.     return OK;
  2100. }
  2101.  
  2102. /*
  2103.  * Draw part of the cmdline at the current cursor position.  But draw stars
  2104.  * when cmdline_star is TRUE.
  2105.  */
  2106.     static void
  2107. draw_cmdline(start, len)
  2108.     int        start;
  2109.     int        len;
  2110. {
  2111. #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
  2112.     int        i;
  2113.  
  2114.     if (cmdline_star > 0)
  2115.     for (i = 0; i < len; ++i)
  2116.     {
  2117.         msg_putchar('*');
  2118. # ifdef FEAT_MBYTE
  2119.         if (has_mbyte)
  2120.         i += (*mb_ptr2len_check)(ccline.cmdbuff + start + i) - 1;
  2121. # endif
  2122.     }
  2123.     else
  2124. #endif
  2125. #ifdef FEAT_ARABIC
  2126.     if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
  2127.     {
  2128.     static char_u    *buf;
  2129.     static int    buflen = 0;
  2130.     char_u        *p;
  2131.     int        j;
  2132.     int        newlen = 0;
  2133.     int        mb_l;
  2134.     int        pc, pc1;
  2135.     int        prev_c = 0;
  2136.     int        prev_c1 = 0;
  2137.     int        u8c, u8c_c1, u8c_c2;
  2138.     int        nc = 0;
  2139.     int        dummy;
  2140.  
  2141.     /*
  2142.      * Do arabic shaping into a temporary buffer.  This is very
  2143.      * inefficient!
  2144.      */
  2145.     if (len * 2 > buflen)
  2146.     {
  2147.         /* Re-allocate the buffer.  We keep it around to avoid a lot of
  2148.          * alloc()/free() calls. */
  2149.         vim_free(buf);
  2150.         buflen = len * 2;
  2151.         buf = alloc(buflen);
  2152.         if (buf == NULL)
  2153.         return;    /* out of memory */
  2154.     }
  2155.  
  2156.     for (j = start; j < start + len; j += mb_l)
  2157.     {
  2158.         p = ccline.cmdbuff + j;
  2159.         u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
  2160.         mb_l = utfc_ptr2len_check_len(p, start + len - j);
  2161.         if (ARABIC_CHAR(u8c))
  2162.         {
  2163.         /* Do Arabic shaping. */
  2164.         if (cmdmsg_rl)
  2165.         {
  2166.             /* displaying from right to left */
  2167.             pc = prev_c;
  2168.             pc1 = prev_c1;
  2169.             prev_c1 = u8c_c1;
  2170.             if (j + mb_l >= start + len)
  2171.             nc = NUL;
  2172.             else
  2173.             nc = utf_ptr2char(p + mb_l);
  2174.         }
  2175.         else
  2176.         {
  2177.             /* displaying from left to right */
  2178.             if (j + mb_l >= start + len)
  2179.             pc = NUL;
  2180.             else
  2181.             pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
  2182.                               start + len - j - mb_l);
  2183.             nc = prev_c;
  2184.         }
  2185.         prev_c = u8c;
  2186.  
  2187.         u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
  2188.  
  2189.         newlen += (*mb_char2bytes)(u8c, buf + newlen);
  2190.         if (u8c_c1 != 0)
  2191.         {
  2192.             newlen += (*mb_char2bytes)(u8c_c1, buf + newlen);
  2193.             if (u8c_c2 != 0)
  2194.             newlen += (*mb_char2bytes)(u8c_c2, buf + newlen);
  2195.         }
  2196.         }
  2197.         else
  2198.         {
  2199.         prev_c = u8c;
  2200.         mch_memmove(buf + newlen, p, mb_l);
  2201.         newlen += mb_l;
  2202.         }
  2203.     }
  2204.  
  2205.     msg_outtrans_len(buf, newlen);
  2206.     }
  2207.     else
  2208. #endif
  2209.     msg_outtrans_len(ccline.cmdbuff + start, len);
  2210. }
  2211.  
  2212. /*
  2213.  * Put a character on the command line.  Shifts the following text to the
  2214.  * right when "shift" is TRUE.  Used for CTRL-V, CTRL-K, etc.
  2215.  * "c" must be printable (fit in one display cell)!
  2216.  */
  2217.     void
  2218. putcmdline(c, shift)
  2219.     int        c;
  2220.     int        shift;
  2221. {
  2222.     if (cmd_silent)
  2223.     return;
  2224.     msg_no_more = TRUE;
  2225.     msg_putchar(c);
  2226.     if (shift)
  2227.     draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
  2228.     msg_no_more = FALSE;
  2229.     cursorcmd();
  2230. }
  2231.  
  2232. /*
  2233.  * Undo a putcmdline(c, FALSE).
  2234.  */
  2235.     void
  2236. unputcmdline()
  2237. {
  2238.     if (cmd_silent)
  2239.     return;
  2240.     msg_no_more = TRUE;
  2241.     if (ccline.cmdlen == ccline.cmdpos)
  2242.     msg_putchar(' ');
  2243.     else
  2244.     draw_cmdline(ccline.cmdpos, 1);
  2245.     msg_no_more = FALSE;
  2246.     cursorcmd();
  2247. }
  2248.  
  2249. /*
  2250.  * Put the given string, of the given length, onto the command line.
  2251.  * If len is -1, then STRLEN() is used to calculate the length.
  2252.  * If 'redraw' is TRUE then the new part of the command line, and the remaining
  2253.  * part will be redrawn, otherwise it will not.  If this function is called
  2254.  * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
  2255.  * called afterwards.
  2256.  */
  2257.     int
  2258. put_on_cmdline(str, len, redraw)
  2259.     char_u    *str;
  2260.     int        len;
  2261.     int        redraw;
  2262. {
  2263.     int        retval;
  2264.     int        i;
  2265.     int        m;
  2266.     int        c;
  2267.  
  2268.     if (len < 0)
  2269.     len = (int)STRLEN(str);
  2270.  
  2271.     /* Check if ccline.cmdbuff needs to be longer */
  2272.     if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
  2273.     retval = realloc_cmdbuff(ccline.cmdlen + len);
  2274.     else
  2275.     retval = OK;
  2276.     if (retval == OK)
  2277.     {
  2278.     if (!ccline.overstrike)
  2279.     {
  2280.         mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
  2281.                            ccline.cmdbuff + ccline.cmdpos,
  2282.                      (size_t)(ccline.cmdlen - ccline.cmdpos));
  2283.         ccline.cmdlen += len;
  2284.     }
  2285.     else
  2286.     {
  2287. #ifdef FEAT_MBYTE
  2288.         if (has_mbyte)
  2289.         {
  2290.         /* Count nr of characters in the new string. */
  2291.         m = 0;
  2292.         for (i = 0; i < len; i += (*mb_ptr2len_check)(str + i))
  2293.             ++m;
  2294.         /* Count nr of bytes in cmdline that are overwritten by these
  2295.          * characters. */
  2296.         for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
  2297.                  i += (*mb_ptr2len_check)(ccline.cmdbuff + i))
  2298.             --m;
  2299.         if (i < ccline.cmdlen)
  2300.         {
  2301.             mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
  2302.                 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
  2303.             ccline.cmdlen += ccline.cmdpos + len - i;
  2304.         }
  2305.         else
  2306.             ccline.cmdlen = ccline.cmdpos + len;
  2307.         }
  2308.         else
  2309. #endif
  2310.         if (ccline.cmdpos + len > ccline.cmdlen)
  2311.         ccline.cmdlen = ccline.cmdpos + len;
  2312.     }
  2313.     mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
  2314.     ccline.cmdbuff[ccline.cmdlen] = NUL;
  2315.  
  2316. #ifdef FEAT_MBYTE
  2317.     if (enc_utf8)
  2318.     {
  2319.         /* When the inserted text starts with a composing character,
  2320.          * backup to the character before it.  There could be two of them.
  2321.          */
  2322.         i = 0;
  2323.         c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
  2324.         while (ccline.cmdpos > 0 && utf_iscomposing(c))
  2325.         {
  2326.         i = (*mb_head_off)(ccline.cmdbuff,
  2327.                       ccline.cmdbuff + ccline.cmdpos - 1) + 1;
  2328.         ccline.cmdpos -= i;
  2329.         len += i;
  2330.         c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
  2331.         }
  2332. # ifdef FEAT_ARABIC
  2333.         if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
  2334.         {
  2335.         /* Check the previous character for Arabic combining pair. */
  2336.         i = (*mb_head_off)(ccline.cmdbuff,
  2337.                       ccline.cmdbuff + ccline.cmdpos - 1) + 1;
  2338.         if (arabic_combine(utf_ptr2char(ccline.cmdbuff
  2339.                              + ccline.cmdpos - i), c))
  2340.         {
  2341.             ccline.cmdpos -= i;
  2342.             len += i;
  2343.         }
  2344.         else
  2345.             i = 0;
  2346.         }
  2347. # endif
  2348.         if (i != 0)
  2349.         {
  2350.         /* Also backup the cursor position. */
  2351.         i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
  2352.         ccline.cmdspos -= i;
  2353.         msg_col -= i;
  2354.         if (msg_col < 0)
  2355.         {
  2356.             msg_col += Columns;
  2357.             --msg_row;
  2358.         }
  2359.         }
  2360.     }
  2361. #endif
  2362.  
  2363.     if (redraw && !cmd_silent)
  2364.     {
  2365.         msg_no_more = TRUE;
  2366.         i = cmdline_row;
  2367.         draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
  2368.         /* Avoid clearing the rest of the line too often. */
  2369.         if (cmdline_row != i || ccline.overstrike)
  2370.         msg_clr_eos();
  2371.         msg_no_more = FALSE;
  2372.     }
  2373. #ifdef FEAT_FKMAP
  2374.     /*
  2375.      * If we are in Farsi command mode, the character input must be in
  2376.      * Insert mode. So do not advance the cmdpos.
  2377.      */
  2378.     if (!cmd_fkmap)
  2379. #endif
  2380.     {
  2381.         if (KeyTyped)
  2382.         m = Columns * Rows;
  2383.         else
  2384.         m = MAXCOL;
  2385.         for (i = 0; i < len; ++i)
  2386.         {
  2387.         c = cmdline_charsize(ccline.cmdpos);
  2388. #ifdef FEAT_MBYTE
  2389.         /* count ">" for a double-wide char that doesn't fit. */
  2390.         if (has_mbyte)
  2391.             correct_cmdspos(ccline.cmdpos, c);
  2392. #endif
  2393.         /* Stop cursor at the end of the screen */
  2394.         if (ccline.cmdspos + c >= m)
  2395.             break;
  2396.         ccline.cmdspos += c;
  2397. #ifdef FEAT_MBYTE
  2398.         if (has_mbyte)
  2399.         {
  2400.             c = (*mb_ptr2len_check)(ccline.cmdbuff + ccline.cmdpos) - 1;
  2401.             if (c > len - i - 1)
  2402.             c = len - i - 1;
  2403.             ccline.cmdpos += c;
  2404.             i += c;
  2405.         }
  2406. #endif
  2407.         ++ccline.cmdpos;
  2408.         }
  2409.     }
  2410.     }
  2411.     if (redraw)
  2412.     msg_check();
  2413.     return retval;
  2414. }
  2415.  
  2416. #ifdef FEAT_WILDMENU
  2417. /*
  2418.  * Delete characters on the command line, from "from" to the current
  2419.  * position.
  2420.  */
  2421.     static void
  2422. cmdline_del(from)
  2423.     int from;
  2424. {
  2425.     mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
  2426.         (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
  2427.     ccline.cmdlen -= ccline.cmdpos - from;
  2428.     ccline.cmdpos = from;
  2429. }
  2430. #endif
  2431.  
  2432. /*
  2433.  * this fuction is called when the screen size changes and with incremental
  2434.  * search
  2435.  */
  2436.     void
  2437. redrawcmdline()
  2438. {
  2439.     if (cmd_silent)
  2440.     return;
  2441.     need_wait_return = FALSE;
  2442.     compute_cmdrow();
  2443.     redrawcmd();
  2444.     cursorcmd();
  2445. }
  2446.  
  2447.     static void
  2448. redrawcmdprompt()
  2449. {
  2450.     int        i;
  2451.  
  2452.     if (cmd_silent)
  2453.     return;
  2454.     if (ccline.cmdfirstc)
  2455.     msg_putchar(ccline.cmdfirstc);
  2456.     if (ccline.cmdprompt != NULL)
  2457.     {
  2458.     msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
  2459.     ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
  2460.     /* do the reverse of set_cmdspos() */
  2461.     if (ccline.cmdfirstc)
  2462.         --ccline.cmdindent;
  2463.     }
  2464.     else
  2465.     for (i = ccline.cmdindent; i > 0; --i)
  2466.         msg_putchar(' ');
  2467. }
  2468.  
  2469. /*
  2470.  * Redraw what is currently on the command line.
  2471.  */
  2472.     void
  2473. redrawcmd()
  2474. {
  2475.     if (cmd_silent)
  2476.     return;
  2477.  
  2478.     msg_start();
  2479.     redrawcmdprompt();
  2480.  
  2481.     /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
  2482.     msg_no_more = TRUE;
  2483.     draw_cmdline(0, ccline.cmdlen);
  2484.     msg_clr_eos();
  2485.     msg_no_more = FALSE;
  2486.  
  2487.     set_cmdspos_cursor();
  2488.  
  2489.     /*
  2490.      * An emsg() before may have set msg_scroll. This is used in normal mode,
  2491.      * in cmdline mode we can reset them now.
  2492.      */
  2493.     msg_scroll = FALSE;        /* next message overwrites cmdline */
  2494.  
  2495.     /* Typing ':' at the more prompt may set skip_redraw.  We don't want this
  2496.      * in cmdline mode */
  2497.     skip_redraw = FALSE;
  2498. }
  2499.  
  2500.     void
  2501. compute_cmdrow()
  2502. {
  2503.     if (exmode_active || msg_scrolled)
  2504.     cmdline_row = Rows - 1;
  2505.     else
  2506.     cmdline_row = W_WINROW(lastwin) + lastwin->w_height
  2507.                            + W_STATUS_HEIGHT(lastwin);
  2508. }
  2509.  
  2510.     static void
  2511. cursorcmd()
  2512. {
  2513.     if (cmd_silent)
  2514.     return;
  2515.  
  2516. #ifdef FEAT_RIGHTLEFT
  2517.     if (cmdmsg_rl)
  2518.     {
  2519.     msg_row = cmdline_row  + (ccline.cmdspos / (int)(Columns - 1));
  2520.     msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
  2521.     if (msg_row <= 0)
  2522.         msg_row = Rows - 1;
  2523.     }
  2524.     else
  2525. #endif
  2526.     {
  2527.     msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
  2528.     msg_col = ccline.cmdspos % (int)Columns;
  2529.     if (msg_row >= Rows)
  2530.         msg_row = Rows - 1;
  2531.     }
  2532.  
  2533.     windgoto(msg_row, msg_col);
  2534. #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
  2535.     redrawcmd_preedit();
  2536. #endif
  2537. #ifdef MCH_CURSOR_SHAPE
  2538.     mch_update_cursor();
  2539. #endif
  2540. }
  2541.  
  2542.     void
  2543. gotocmdline(clr)
  2544.     int            clr;
  2545. {
  2546.     msg_start();
  2547. #ifdef FEAT_RIGHTLEFT
  2548.     if (cmdmsg_rl)
  2549.     msg_col = Columns - 1;
  2550.     else
  2551. #endif
  2552.     msg_col = 0;        /* always start in column 0 */
  2553.     if (clr)            /* clear the bottom line(s) */
  2554.     msg_clr_eos();        /* will reset clear_cmdline */
  2555.     windgoto(cmdline_row, 0);
  2556. }
  2557.  
  2558. /*
  2559.  * Check the word in front of the cursor for an abbreviation.
  2560.  * Called when the non-id character "c" has been entered.
  2561.  * When an abbreviation is recognized it is removed from the text with
  2562.  * backspaces and the replacement string is inserted, followed by "c".
  2563.  */
  2564.     static int
  2565. ccheck_abbr(c)
  2566.     int c;
  2567. {
  2568.     if (p_paste || no_abbr)        /* no abbreviations or in paste mode */
  2569.     return FALSE;
  2570.  
  2571.     return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
  2572. }
  2573.  
  2574. /*
  2575.  * Return FAIL if this is not an appropriate context in which to do
  2576.  * completion of anything, return OK if it is (even if there are no matches).
  2577.  * For the caller, this means that the character is just passed through like a
  2578.  * normal character (instead of being expanded).  This allows :s/^I^D etc.
  2579.  */
  2580.     static int
  2581. nextwild(xp, type, options)
  2582.     expand_T    *xp;
  2583.     int        type;
  2584.     int        options;    /* extra options for ExpandOne() */
  2585. {
  2586.     int        i, j;
  2587.     char_u    *p1;
  2588.     char_u    *p2;
  2589.     int        oldlen;
  2590.     int        difflen;
  2591.     int        v;
  2592.  
  2593.     if (cmd_numfiles == -1)
  2594.     {
  2595.     set_expand_context(xp);
  2596.     cmd_showtail = !glob_in_path_prefix(xp);
  2597.     }
  2598.  
  2599.     if (xp->xp_context == EXPAND_UNSUCCESSFUL)
  2600.     {
  2601.     beep_flush();
  2602.     return OK;  /* Something illegal on command line */
  2603.     }
  2604.     if (xp->xp_context == EXPAND_NOTHING)
  2605.     {
  2606.     /* Caller can use the character as a normal char instead */
  2607.     return FAIL;
  2608.     }
  2609.  
  2610.     MSG_PUTS("...");        /* show that we are busy */
  2611.     out_flush();
  2612.  
  2613.     i = (int)(xp->xp_pattern - ccline.cmdbuff);
  2614.     oldlen = ccline.cmdpos - i;
  2615.  
  2616.     if (type == WILD_NEXT || type == WILD_PREV)
  2617.     {
  2618.     /*
  2619.      * Get next/previous match for a previous expanded pattern.
  2620.      */
  2621.     p2 = ExpandOne(xp, NULL, NULL, 0, type);
  2622.     }
  2623.     else
  2624.     {
  2625.     /*
  2626.      * Translate string into pattern and expand it.
  2627.      */
  2628.     if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
  2629.         p2 = NULL;
  2630.     else
  2631.     {
  2632.         p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
  2633.             WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
  2634.                                   |options, type);
  2635.         vim_free(p1);
  2636.         /* longest match: make sure it is not shorter (happens with :help */
  2637.         if (p2 != NULL && type == WILD_LONGEST)
  2638.         {
  2639.         for (j = 0; j < oldlen; ++j)
  2640.              if (ccline.cmdbuff[i + j] == '*'
  2641.                  || ccline.cmdbuff[i + j] == '?')
  2642.              break;
  2643.         if ((int)STRLEN(p2) < j)
  2644.         {
  2645.             vim_free(p2);
  2646.             p2 = NULL;
  2647.         }
  2648.         }
  2649.     }
  2650.     }
  2651.  
  2652.     if (p2 != NULL && !got_int)
  2653.     {
  2654.     difflen = (int)STRLEN(p2) - oldlen;
  2655.     if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
  2656.     {
  2657.         v = realloc_cmdbuff(ccline.cmdlen + difflen);
  2658.         xp->xp_pattern = ccline.cmdbuff + i;
  2659.     }
  2660.     else
  2661.         v = OK;
  2662.     if (v == OK)
  2663.     {
  2664.         vim_strncpy(&ccline.cmdbuff[ccline.cmdpos + difflen],
  2665.                            &ccline.cmdbuff[ccline.cmdpos],
  2666.             ccline.cmdlen - ccline.cmdpos + 1);
  2667.         STRNCPY(&ccline.cmdbuff[i], p2, STRLEN(p2));
  2668.         ccline.cmdlen += difflen;
  2669.         ccline.cmdpos += difflen;
  2670.     }
  2671.     }
  2672.     vim_free(p2);
  2673.  
  2674.     redrawcmd();
  2675.  
  2676.     /* When expanding a ":map" command and no matches are found, assume that
  2677.      * the key is supposed to be inserted literally */
  2678.     if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
  2679.     return FAIL;
  2680.  
  2681.     if (cmd_numfiles <= 0 && p2 == NULL)
  2682.     beep_flush();
  2683.     else if (cmd_numfiles == 1)
  2684.     /* free expanded pattern */
  2685.     (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
  2686.  
  2687.     return OK;
  2688. }
  2689.  
  2690. /*
  2691.  * Do wildcard expansion on the string 'str'.
  2692.  * Chars that should not be expanded must be preceded with a backslash.
  2693.  * Return a pointer to alloced memory containing the new string.
  2694.  * Return NULL for failure.
  2695.  *
  2696.  * Results are cached in cmd_files and cmd_numfiles.
  2697.  *
  2698.  * mode = WILD_FREE:        just free previously expanded matches
  2699.  * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
  2700.  * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
  2701.  * mode = WILD_NEXT:        use next match in multiple match, wrap to first
  2702.  * mode = WILD_PREV:        use previous match in multiple match, wrap to first
  2703.  * mode = WILD_ALL:        return all matches concatenated
  2704.  * mode = WILD_LONGEST:        return longest matched part
  2705.  *
  2706.  * options = WILD_LIST_NOTFOUND:    list entries without a match
  2707.  * options = WILD_HOME_REPLACE:        do home_replace() for buffer names
  2708.  * options = WILD_USE_NL:        Use '\n' for WILD_ALL
  2709.  * options = WILD_NO_BEEP:        Don't beep for multiple matches
  2710.  * options = WILD_ADD_SLASH:        add a slash after directory names
  2711.  * options = WILD_KEEP_ALL:        don't remove 'wildignore' entries
  2712.  * options = WILD_SILENT:        don't print warning messages
  2713.  * options = WILD_ESCAPE:        put backslash before special chars
  2714.  *
  2715.  * The variables xp->xp_context and xp->xp_backslash must have been set!
  2716.  */
  2717.     char_u *
  2718. ExpandOne(xp, str, orig, options, mode)
  2719.     expand_T    *xp;
  2720.     char_u    *str;
  2721.     char_u    *orig;        /* allocated copy of original of expanded string */
  2722.     int        options;
  2723.     int        mode;
  2724. {
  2725.     char_u    *ss = NULL;
  2726.     static int    findex;
  2727.     static char_u *orig_save = NULL;    /* kept value of orig */
  2728.     int        i;
  2729.     long_u    len;
  2730.     int        non_suf_match;        /* number without matching suffix */
  2731.  
  2732.     /*
  2733.      * first handle the case of using an old match
  2734.      */
  2735.     if (mode == WILD_NEXT || mode == WILD_PREV)
  2736.     {
  2737.     if (cmd_numfiles > 0)
  2738.     {
  2739.         if (mode == WILD_PREV)
  2740.         {
  2741.         if (findex == -1)
  2742.             findex = cmd_numfiles;
  2743.         --findex;
  2744.         }
  2745.         else    /* mode == WILD_NEXT */
  2746.         ++findex;
  2747.  
  2748.         /*
  2749.          * When wrapping around, return the original string, set findex to
  2750.          * -1.
  2751.          */
  2752.         if (findex < 0)
  2753.         {
  2754.         if (orig_save == NULL)
  2755.             findex = cmd_numfiles - 1;
  2756.         else
  2757.             findex = -1;
  2758.         }
  2759.         if (findex >= cmd_numfiles)
  2760.         {
  2761.         if (orig_save == NULL)
  2762.             findex = 0;
  2763.         else
  2764.             findex = -1;
  2765.         }
  2766. #ifdef FEAT_WILDMENU
  2767.         if (p_wmnu)
  2768.         win_redr_status_matches(xp, cmd_numfiles, cmd_files, findex,
  2769.                     cmd_showtail);
  2770. #endif
  2771.         if (findex == -1)
  2772.         return vim_strsave(orig_save);
  2773.         return vim_strsave(cmd_files[findex]);
  2774.     }
  2775.     else
  2776.         return NULL;
  2777.     }
  2778.  
  2779. /* free old names */
  2780.     if (cmd_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
  2781.     {
  2782.     FreeWild(cmd_numfiles, cmd_files);
  2783.     cmd_numfiles = -1;
  2784.     vim_free(orig_save);
  2785.     orig_save = NULL;
  2786.     }
  2787.     findex = 0;
  2788.  
  2789.     if (mode == WILD_FREE)    /* only release file name */
  2790.     return NULL;
  2791.  
  2792.     if (cmd_numfiles == -1)
  2793.     {
  2794.     vim_free(orig_save);
  2795.     orig_save = orig;
  2796.  
  2797.     /*
  2798.      * Do the expansion.
  2799.      */
  2800.     if (ExpandFromContext(xp, str, &cmd_numfiles, &cmd_files,
  2801.                                  options) == FAIL)
  2802.     {
  2803. #ifdef FNAME_ILLEGAL
  2804.         /* Illegal file name has been silently skipped.  But when there
  2805.          * are wildcards, the real problem is that there was no match,
  2806.          * causing the pattern to be added, which has illegal characters.
  2807.          */
  2808.         if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
  2809.         EMSG2(_(e_nomatch2), str);
  2810. #endif
  2811.     }
  2812.     else if (cmd_numfiles == 0)
  2813.     {
  2814.         if (!(options & WILD_SILENT))
  2815.         EMSG2(_(e_nomatch2), str);
  2816.     }
  2817.     else
  2818.     {
  2819.         /* Escape the matches for use on the command line. */
  2820.         ExpandEscape(xp, str, cmd_numfiles, cmd_files, options);
  2821.  
  2822.         /*
  2823.          * Check for matching suffixes in file names.
  2824.          */
  2825.         if (mode != WILD_ALL && mode != WILD_LONGEST)
  2826.         {
  2827.         if (cmd_numfiles)
  2828.             non_suf_match = cmd_numfiles;
  2829.         else
  2830.             non_suf_match = 1;
  2831.         if ((xp->xp_context == EXPAND_FILES
  2832.                 || xp->xp_context == EXPAND_DIRECTORIES)
  2833.             && cmd_numfiles > 1)
  2834.         {
  2835.             /*
  2836.              * More than one match; check suffix.
  2837.              * The files will have been sorted on matching suffix in
  2838.              * expand_wildcards, only need to check the first two.
  2839.              */
  2840.             non_suf_match = 0;
  2841.             for (i = 0; i < 2; ++i)
  2842.             if (match_suffix(cmd_files[i]))
  2843.                 ++non_suf_match;
  2844.         }
  2845.         if (non_suf_match != 1)
  2846.         {
  2847.             /* Can we ever get here unless it's while expanding
  2848.              * interactively?  If not, we can get rid of this all
  2849.              * together. Don't really want to wait for this message
  2850.              * (and possibly have to hit return to continue!).
  2851.              */
  2852.             if (!(options & WILD_SILENT))
  2853.             EMSG(_(e_toomany));
  2854.             else if (!(options & WILD_NO_BEEP))
  2855.             beep_flush();
  2856.         }
  2857.         if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
  2858.             ss = vim_strsave(cmd_files[0]);
  2859.         }
  2860.     }
  2861.     }
  2862.  
  2863.     /* Find longest common part */
  2864.     if (mode == WILD_LONGEST && cmd_numfiles > 0)
  2865.     {
  2866.     for (len = 0; cmd_files[0][len]; ++len)
  2867.     {
  2868.         for (i = 0; i < cmd_numfiles; ++i)
  2869.         {
  2870. #ifdef CASE_INSENSITIVE_FILENAME
  2871.         if (xp->xp_context == EXPAND_DIRECTORIES
  2872.             || xp->xp_context == EXPAND_FILES
  2873.             || xp->xp_context == EXPAND_BUFFERS)
  2874.         {
  2875.             if (TOLOWER_LOC(cmd_files[i][len]) !=
  2876.                            TOLOWER_LOC(cmd_files[0][len]))
  2877.             break;
  2878.         }
  2879.         else
  2880. #endif
  2881.              if (cmd_files[i][len] != cmd_files[0][len])
  2882.             break;
  2883.         }
  2884.         if (i < cmd_numfiles)
  2885.         {
  2886.         if (!(options & WILD_NO_BEEP))
  2887.             vim_beep();
  2888.         break;
  2889.         }
  2890.     }
  2891.     ss = alloc((unsigned)len + 1);
  2892.     if (ss)
  2893.     {
  2894.         STRNCPY(ss, cmd_files[0], len);
  2895.         ss[len] = NUL;
  2896.     }
  2897.     findex = -1;                /* next p_wc gets first one */
  2898.     }
  2899.  
  2900.     /* Concatenate all matching names */
  2901.     if (mode == WILD_ALL && cmd_numfiles > 0)
  2902.     {
  2903.     len = 0;
  2904.     for (i = 0; i < cmd_numfiles; ++i)
  2905.         len += (long_u)STRLEN(cmd_files[i]) + 1;
  2906.     ss = lalloc(len, TRUE);
  2907.     if (ss != NULL)
  2908.     {
  2909.         *ss = NUL;
  2910.         for (i = 0; i < cmd_numfiles; ++i)
  2911.         {
  2912.         STRCAT(ss, cmd_files[i]);
  2913.         if (i != cmd_numfiles - 1)
  2914.             STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
  2915.         }
  2916.     }
  2917.     }
  2918.  
  2919.     if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
  2920.     {
  2921.     FreeWild(cmd_numfiles, cmd_files);
  2922.     cmd_numfiles = -1;
  2923.     }
  2924.  
  2925.     return ss;
  2926. }
  2927.  
  2928.     void
  2929. ExpandEscape(xp, str, numfiles, files, options)
  2930.     expand_T    *xp;
  2931.     char_u    *str;
  2932.     int        numfiles;
  2933.     char_u    **files;
  2934.     int        options;
  2935. {
  2936.     int        i;
  2937.     char_u    *p;
  2938.  
  2939.     /*
  2940.      * May change home directory back to "~"
  2941.      */
  2942.     if (options & WILD_HOME_REPLACE)
  2943.     tilde_replace(str, numfiles, files);
  2944.  
  2945.     if (options & WILD_ESCAPE)
  2946.     {
  2947.     if (xp->xp_context == EXPAND_FILES
  2948.         || xp->xp_context == EXPAND_BUFFERS
  2949.         || xp->xp_context == EXPAND_DIRECTORIES)
  2950.     {
  2951.         /*
  2952.          * Insert a backslash into a file name before a space, \, %, #
  2953.          * and wildmatch characters, except '~'.
  2954.          */
  2955.         for (i = 0; i < numfiles; ++i)
  2956.         {
  2957.         /* for ":set path=" we need to escape spaces twice */
  2958.         if (xp->xp_backslash == XP_BS_THREE)
  2959.         {
  2960.             p = vim_strsave_escaped(files[i], (char_u *)" ");
  2961.             if (p != NULL)
  2962.             {
  2963.             vim_free(files[i]);
  2964.             files[i] = p;
  2965. #if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
  2966.             p = vim_strsave_escaped(files[i], (char_u *)" ");
  2967.             if (p != NULL)
  2968.             {
  2969.                 vim_free(files[i]);
  2970.                 files[i] = p;
  2971.             }
  2972. #endif
  2973.             }
  2974.         }
  2975. #ifdef BACKSLASH_IN_FILENAME
  2976.         {
  2977.             char_u    buf[20];
  2978.             int        j = 0;
  2979.  
  2980.             /* Don't escape '[' and '{' if they are in 'isfname'. */
  2981.             for (p = PATH_ESC_CHARS; *p != NUL; ++p)
  2982.             if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
  2983.                 buf[j++] = *p;
  2984.             buf[j] = NUL;
  2985.             p = vim_strsave_escaped(files[i], buf);
  2986.         }
  2987. #else
  2988.         p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
  2989. #endif
  2990.         if (p != NULL)
  2991.         {
  2992.             vim_free(files[i]);
  2993.             files[i] = p;
  2994.         }
  2995.  
  2996.         /* If 'str' starts with "\~", replace "~" at start of
  2997.          * files[i] with "\~". */
  2998.         if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
  2999.         {
  3000.             p = alloc((unsigned)(STRLEN(files[i]) + 2));
  3001.             if (p != NULL)
  3002.             {
  3003.             p[0] = '\\';
  3004.             STRCPY(p + 1, files[i]);
  3005.             vim_free(files[i]);
  3006.             files[i] = p;
  3007.             }
  3008.         }
  3009.         }
  3010.         xp->xp_backslash = XP_BS_NONE;
  3011.     }
  3012.     else if (xp->xp_context == EXPAND_TAGS)
  3013.     {
  3014.         /*
  3015.          * Insert a backslash before characters in a tag name that
  3016.          * would terminate the ":tag" command.
  3017.          */
  3018.         for (i = 0; i < numfiles; ++i)
  3019.         {
  3020.         p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
  3021.         if (p != NULL)
  3022.         {
  3023.             vim_free(files[i]);
  3024.             files[i] = p;
  3025.         }
  3026.         }
  3027.     }
  3028.     }
  3029. }
  3030.  
  3031. /*
  3032.  * For each file name in files[num_files]:
  3033.  * If 'orig_pat' starts with "~/", replace the home directory with "~".
  3034.  */
  3035.     void
  3036. tilde_replace(orig_pat, num_files, files)
  3037.     char_u  *orig_pat;
  3038.     int        num_files;
  3039.     char_u  **files;
  3040. {
  3041.     int        i;
  3042.     char_u  *p;
  3043.  
  3044.     if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
  3045.     {
  3046.     for (i = 0; i < num_files; ++i)
  3047.     {
  3048.         p = home_replace_save(NULL, files[i]);
  3049.         if (p != NULL)
  3050.         {
  3051.         vim_free(files[i]);
  3052.         files[i] = p;
  3053.         }
  3054.     }
  3055.     }
  3056. }
  3057.  
  3058. /*
  3059.  * Show all matches for completion on the command line.
  3060.  * Returns EXPAND_NOTHING when the character that triggered expansion should
  3061.  * be inserted like a normal character.
  3062.  */
  3063. /*ARGSUSED*/
  3064.     static int
  3065. showmatches(xp, wildmenu)
  3066.     expand_T    *xp;
  3067.     int        wildmenu;
  3068. {
  3069. #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
  3070.     int        num_files;
  3071.     char_u    **files_found;
  3072.     int        i, j, k;
  3073.     int        maxlen;
  3074.     int        lines;
  3075.     int        columns;
  3076.     char_u    *p;
  3077.     int        lastlen;
  3078.     int        attr;
  3079.     int        showtail;
  3080.  
  3081.     if (cmd_numfiles == -1)
  3082.     {
  3083.     set_expand_context(xp);
  3084.     i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
  3085.                             &num_files, &files_found);
  3086.     showtail = !glob_in_path_prefix(xp);
  3087.     if (i != EXPAND_OK)
  3088.         return i;
  3089.  
  3090.     }
  3091.     else
  3092.     {
  3093.     num_files = cmd_numfiles;
  3094.     files_found = cmd_files;
  3095.     showtail = cmd_showtail;
  3096.     }
  3097.  
  3098. #ifdef FEAT_WILDMENU
  3099.     if (!wildmenu)
  3100.     {
  3101. #endif
  3102.     msg_didany = FALSE;        /* lines_left will be set */
  3103.     msg_start();            /* prepare for paging */
  3104.     msg_putchar('\n');
  3105.     out_flush();
  3106.     cmdline_row = msg_row;
  3107.     msg_didany = FALSE;        /* lines_left will be set again */
  3108.     msg_start();            /* prepare for paging */
  3109. #ifdef FEAT_WILDMENU
  3110.     }
  3111. #endif
  3112.  
  3113.     if (got_int)
  3114.     got_int = FALSE;    /* only int. the completion, not the cmd line */
  3115. #ifdef FEAT_WILDMENU
  3116.     else if (wildmenu)
  3117.     win_redr_status_matches(xp, num_files, files_found, 0, showtail);
  3118. #endif
  3119.     else
  3120.     {
  3121.     /* find the length of the longest file name */
  3122.     maxlen = 0;
  3123.     for (i = 0; i < num_files; ++i)
  3124.     {
  3125.         if (!showtail && (xp->xp_context == EXPAND_FILES
  3126.               || xp->xp_context == EXPAND_BUFFERS))
  3127.         {
  3128.         home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
  3129.         j = vim_strsize(NameBuff);
  3130.         }
  3131.         else
  3132.         j = vim_strsize(L_SHOWFILE(i));
  3133.         if (j > maxlen)
  3134.         maxlen = j;
  3135.     }
  3136.  
  3137.     if (xp->xp_context == EXPAND_TAGS_LISTFILES)
  3138.         lines = num_files;
  3139.     else
  3140.     {
  3141.         /* compute the number of columns and lines for the listing */
  3142.         maxlen += 2;    /* two spaces between file names */
  3143.         columns = ((int)Columns + 2) / maxlen;
  3144.         if (columns < 1)
  3145.         columns = 1;
  3146.         lines = (num_files + columns - 1) / columns;
  3147.     }
  3148.  
  3149.     attr = hl_attr(HLF_D);    /* find out highlighting for directories */
  3150.  
  3151.     if (xp->xp_context == EXPAND_TAGS_LISTFILES)
  3152.     {
  3153.         MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
  3154.         msg_clr_eos();
  3155.         msg_advance(maxlen - 3);
  3156.         MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
  3157.     }
  3158.  
  3159.     /* list the files line by line */
  3160.     for (i = 0; i < lines; ++i)
  3161.     {
  3162.         lastlen = 999;
  3163.         for (k = i; k < num_files; k += lines)
  3164.         {
  3165.         if (xp->xp_context == EXPAND_TAGS_LISTFILES)
  3166.         {
  3167.             msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
  3168.             p = files_found[k] + STRLEN(files_found[k]) + 1;
  3169.             msg_advance(maxlen + 1);
  3170.             msg_puts(p);
  3171.             msg_advance(maxlen + 3);
  3172.             msg_puts_long_attr(p + 2, hl_attr(HLF_D));
  3173.             break;
  3174.         }
  3175.         for (j = maxlen - lastlen; --j >= 0; )
  3176.             msg_putchar(' ');
  3177.         if (xp->xp_context == EXPAND_FILES
  3178.                       || xp->xp_context == EXPAND_BUFFERS)
  3179.         {
  3180.                 /* highlight directories */
  3181.             j = (mch_isdir(files_found[k]));
  3182.             if (showtail)
  3183.             p = L_SHOWFILE(k);
  3184.             else
  3185.             {
  3186.             home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
  3187.                                     TRUE);
  3188.             p = NameBuff;
  3189.             }
  3190.         }
  3191.         else
  3192.         {
  3193.             j = FALSE;
  3194.             p = L_SHOWFILE(k);
  3195.         }
  3196.         lastlen = msg_outtrans_attr(p, j ? attr : 0);
  3197.         }
  3198.         if (msg_col > 0)    /* when not wrapped around */
  3199.         {
  3200.         msg_clr_eos();
  3201.         msg_putchar('\n');
  3202.         }
  3203.         out_flush();            /* show one line at a time */
  3204.         if (got_int)
  3205.         {
  3206.         got_int = FALSE;
  3207.         break;
  3208.         }
  3209.     }
  3210.  
  3211.     /*
  3212.      * we redraw the command below the lines that we have just listed
  3213.      * This is a bit tricky, but it saves a lot of screen updating.
  3214.      */
  3215.     cmdline_row = msg_row;    /* will put it back later */
  3216.     }
  3217.  
  3218.     if (cmd_numfiles == -1)
  3219.     FreeWild(num_files, files_found);
  3220.  
  3221.     return EXPAND_OK;
  3222. }
  3223.  
  3224. /*
  3225.  * Private gettail for showmatches() (and win_redr_status_matches()):
  3226.  * Find tail of file name path, but ignore trailing "/".
  3227.  */
  3228.     char_u *
  3229. sm_gettail(s)
  3230.     char_u    *s;
  3231. {
  3232.     char_u    *p;
  3233.     char_u    *t = s;
  3234.     int        had_sep = FALSE;
  3235.  
  3236.     for (p = s; *p != NUL; )
  3237.     {
  3238.     if (vim_ispathsep(*p)
  3239. #ifdef BACKSLASH_IN_FILENAME
  3240.         && !rem_backslash(p)
  3241. #endif
  3242.        )
  3243.         had_sep = TRUE;
  3244.     else if (had_sep)
  3245.     {
  3246.         t = p;
  3247.         had_sep = FALSE;
  3248.     }
  3249. #ifdef FEAT_MBYTE
  3250.     if (has_mbyte)
  3251.         p += (*mb_ptr2len_check)(p);
  3252.     else
  3253. #endif
  3254.         ++p;
  3255.     }
  3256.     return t;
  3257. }
  3258.  
  3259.  
  3260.     static int
  3261. glob_in_path_prefix(xp)
  3262.     expand_T    *xp;
  3263. {
  3264.     char_u    *s;
  3265.     char_u    *end;
  3266.  
  3267.     if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
  3268.     return FALSE;
  3269.  
  3270.     end = gettail(xp->xp_pattern);
  3271.     if (end == xp->xp_pattern)
  3272.     return FALSE;
  3273.  
  3274.     for (s = xp->xp_pattern; s < end; s++)
  3275.     {
  3276.     if (*s == '\\')    /* skip backslash */
  3277.         if (*++s == 0)
  3278.         break;
  3279.  
  3280.     switch (*s)
  3281.     {
  3282.         case '*': case '?': case '[':
  3283.         return TRUE;
  3284.     }
  3285.     }
  3286.     return FALSE;
  3287. }
  3288.  
  3289. /*
  3290.  * Prepare a string for expansion.
  3291.  * When expanding file names: The string will be used with expand_wildcards().
  3292.  * Copy the file name into allocated memory and add a '*' at the end.
  3293.  * When expanding other names: The string will be used with regcomp().  Copy
  3294.  * the name into allocated memory and add ".*" at the end.
  3295.  */
  3296.     char_u *
  3297. addstar(fname, len, context)
  3298.     char_u    *fname;
  3299.     int        len;
  3300.     int        context;    /* EXPAND_FILES etc. */
  3301. {
  3302.     char_u    *retval;
  3303.     int        i, j;
  3304.     int        new_len;
  3305.     char_u    *tail;
  3306.  
  3307.     if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
  3308.     {
  3309.     /*
  3310.      * Matching will be done internally (on something other than files).
  3311.      * So we convert the file-matching-type wildcards into our kind for
  3312.      * use with vim_regcomp().  First work out how long it will be:
  3313.      */
  3314.  
  3315.     /* For help tags the translation is done in find_help_tags().
  3316.      * For a tag pattern starting with "/" no translation is needed. */
  3317.     if (context == EXPAND_HELP
  3318.         || context == EXPAND_COLORS
  3319.         || context == EXPAND_COMPILER
  3320.         || (context == EXPAND_TAGS && fname[0] == '/'))
  3321.         retval = vim_strnsave(fname, len);
  3322.     else
  3323.     {
  3324.         new_len = len + 2;        /* +2 for '^' at start, NUL at end */
  3325.         for (i = 0; i < len; i++)
  3326.         {
  3327.         if (fname[i] == '*' || fname[i] == '~')
  3328.             new_len++;        /* '*' needs to be replaced by ".*"
  3329.                        '~' needs to be replaced by "\~" */
  3330.  
  3331.         /* Buffer names are like file names.  "." should be literal */
  3332.         if (context == EXPAND_BUFFERS && fname[i] == '.')
  3333.             new_len++;        /* "." becomes "\." */
  3334.         }
  3335.         retval = alloc(new_len);
  3336.         if (retval != NULL)
  3337.         {
  3338.         retval[0] = '^';
  3339.         j = 1;
  3340.         for (i = 0; i < len; i++, j++)
  3341.         {
  3342.             if (fname[i] == '\\' && ++i == len)    /* skip backslash */
  3343.             break;
  3344.  
  3345.             switch (fname[i])
  3346.             {
  3347.             case '*':   retval[j++] = '.';
  3348.                     break;
  3349.             case '~':   retval[j++] = '\\';
  3350.                     break;
  3351.             case '?':   retval[j] = '.';
  3352.                     continue;
  3353.             case '.':   if (context == EXPAND_BUFFERS)
  3354.                     retval[j++] = '\\';
  3355.                     break;
  3356.             }
  3357.             retval[j] = fname[i];
  3358.         }
  3359.         retval[j] = NUL;
  3360.         }
  3361.     }
  3362.     }
  3363.     else
  3364.     {
  3365.     retval = alloc(len + 4);
  3366.     if (retval != NULL)
  3367.     {
  3368.         STRNCPY(retval, fname, len);
  3369.         retval[len] = NUL;
  3370.  
  3371.         /*
  3372.          * Don't add a star to ~, ~user, $var or `cmd`.
  3373.          * ~ would be at the start of the file name, but not the tail.
  3374.          * $ could be anywhere in the tail.
  3375.          * ` could be anywhere in the file name.
  3376.          */
  3377.         tail = gettail(retval);
  3378.         if ((*retval != '~' || tail != retval)
  3379.             && vim_strchr(tail, '$') == NULL
  3380.             && vim_strchr(retval, '`') == NULL)
  3381.         retval[len++] = '*';
  3382.         retval[len] = NUL;
  3383.     }
  3384.     }
  3385.     return retval;
  3386. }
  3387.  
  3388. /*
  3389.  * Must parse the command line so far to work out what context we are in.
  3390.  * Completion can then be done based on that context.
  3391.  * This routine sets the variables:
  3392.  *  xp->xp_pattern        The start of the pattern to be expanded within
  3393.  *                the command line (ends at the cursor).
  3394.  *  xp->xp_context        The type of thing to expand.  Will be one of:
  3395.  *
  3396.  *  EXPAND_UNSUCCESSFUL        Used sometimes when there is something illegal on
  3397.  *                the command line, like an unknown command.    Caller
  3398.  *                should beep.
  3399.  *  EXPAND_NOTHING        Unrecognised context for completion, use char like
  3400.  *                a normal char, rather than for completion.    eg
  3401.  *                :s/^I/
  3402.  *  EXPAND_COMMANDS        Cursor is still touching the command, so complete
  3403.  *                it.
  3404.  *  EXPAND_BUFFERS        Complete file names for :buf and :sbuf commands.
  3405.  *  EXPAND_FILES        After command with XFILE set, or after setting
  3406.  *                with P_EXPAND set.    eg :e ^I, :w>>^I
  3407.  *  EXPAND_DIRECTORIES        In some cases this is used instead of the latter
  3408.  *                when we know only directories are of interest.  eg
  3409.  *                :set dir=^I
  3410.  *  EXPAND_SETTINGS        Complete variable names.  eg :set d^I
  3411.  *  EXPAND_BOOL_SETTINGS    Complete boolean variables only,  eg :set no^I
  3412.  *  EXPAND_TAGS            Complete tags from the files in p_tags.  eg :ta a^I
  3413.  *  EXPAND_TAGS_LISTFILES   As above, but list filenames on ^D, after :tselect
  3414.  *  EXPAND_HELP            Complete tags from the file 'helpfile'/tags
  3415.  *  EXPAND_EVENTS        Complete event names
  3416.  *  EXPAND_SYNTAX        Complete :syntax command arguments
  3417.  *  EXPAND_HIGHLIGHT        Complete highlight (syntax) group names
  3418.  *  EXPAND_AUGROUP        Complete autocommand group names
  3419.  *  EXPAND_USER_VARS        Complete user defined variable names, eg :unlet a^I
  3420.  *  EXPAND_MAPPINGS        Complete mapping and abbreviation names,
  3421.  *                  eg :unmap a^I , :cunab x^I
  3422.  *  EXPAND_FUNCTIONS        Complete internal or user defined function names,
  3423.  *                  eg :call sub^I
  3424.  *  EXPAND_USER_FUNC        Complete user defined function names, eg :delf F^I
  3425.  *  EXPAND_EXPRESSION        Complete internal or user defined function/variable
  3426.  *                names in expressions, eg :while s^I
  3427.  *  EXPAND_ENV_VARS        Complete environment variable names
  3428.  */
  3429.     static void
  3430. set_expand_context(xp)
  3431.     expand_T    *xp;
  3432. {
  3433.     /* only expansion for ':' and '>' commands */
  3434.     if (ccline.cmdfirstc != ':'
  3435. #ifdef FEAT_EVAL
  3436.         && ccline.cmdfirstc != '>'
  3437. #endif
  3438.         )
  3439.     {
  3440.     xp->xp_context = EXPAND_NOTHING;
  3441.     return;
  3442.     }
  3443.     set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
  3444. }
  3445.  
  3446.     void
  3447. set_cmd_context(xp, str, len, col)
  3448.     expand_T    *xp;
  3449.     char_u    *str;        /* start of command line */
  3450.     int        len;        /* length of command line (excl. NUL) */
  3451.     int        col;        /* position of cursor */
  3452. {
  3453.     int        old_char = NUL;
  3454.     char_u    *nextcomm;
  3455.  
  3456.     /*
  3457.      * Avoid a UMR warning from Purify, only save the character if it has been
  3458.      * written before.
  3459.      */
  3460.     if (col < len)
  3461.     old_char = str[col];
  3462.     str[col] = NUL;
  3463.     nextcomm = str;
  3464.     while (nextcomm != NULL)
  3465.     nextcomm = set_one_cmd_context(xp, nextcomm);
  3466.     str[col] = old_char;
  3467. }
  3468.  
  3469. /*
  3470.  * Expand the command line "str" from context "xp".
  3471.  * "xp" must have been set by set_cmd_context().
  3472.  * xp->xp_pattern points into "str", to where the text that is to be expanded
  3473.  * starts.
  3474.  * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
  3475.  * cursor.
  3476.  * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
  3477.  * key that triggered expansion literally.
  3478.  * Returns EXPAND_OK otherwise.
  3479.  */
  3480.     int
  3481. expand_cmdline(xp, str, col, matchcount, matches)
  3482.     expand_T    *xp;
  3483.     char_u    *str;        /* start of command line */
  3484.     int        col;        /* position of cursor */
  3485.     int        *matchcount;    /* return: nr of matches */
  3486.     char_u    ***matches;    /* return: array of pointers to matches */
  3487. {
  3488.     char_u    *file_str = NULL;
  3489.  
  3490.     if (xp->xp_context == EXPAND_UNSUCCESSFUL)
  3491.     {
  3492.     beep_flush();
  3493.     return EXPAND_UNSUCCESSFUL;  /* Something illegal on command line */
  3494.     }
  3495.     if (xp->xp_context == EXPAND_NOTHING)
  3496.     {
  3497.     /* Caller can use the character as a normal char instead */
  3498.     return EXPAND_NOTHING;
  3499.     }
  3500.  
  3501.     /* add star to file name, or convert to regexp if not exp. files. */
  3502.     file_str = addstar(xp->xp_pattern,
  3503.                (int)(str + col - xp->xp_pattern), xp->xp_context);
  3504.     if (file_str == NULL)
  3505.     return EXPAND_UNSUCCESSFUL;
  3506.  
  3507.     /* find all files that match the description */
  3508.     if (ExpandFromContext(xp, file_str, matchcount, matches,
  3509.                       WILD_ADD_SLASH|WILD_SILENT) == FAIL)
  3510.     {
  3511.     *matchcount = 0;
  3512.     *matches = NULL;
  3513.     }
  3514.     vim_free(file_str);
  3515.  
  3516.     return EXPAND_OK;
  3517. }
  3518.  
  3519. /*
  3520.  * Do the expansion based on xp->xp_context and "pat".
  3521.  */
  3522.     static int
  3523. ExpandFromContext(xp, pat, num_file, file, options)
  3524.     expand_T    *xp;
  3525.     char_u    *pat;
  3526.     int        *num_file;
  3527.     char_u    ***file;
  3528.     int        options;
  3529. {
  3530. #ifdef FEAT_CMDL_COMPL
  3531.     regmatch_T    regmatch;
  3532. #endif
  3533.     int        ret;
  3534.     int        flags;
  3535.  
  3536.     flags = EW_DIR;    /* include directories */
  3537.     if (options & WILD_LIST_NOTFOUND)
  3538.     flags |= EW_NOTFOUND;
  3539.     if (options & WILD_ADD_SLASH)
  3540.     flags |= EW_ADDSLASH;
  3541.     if (options & WILD_KEEP_ALL)
  3542.     flags |= EW_KEEPALL;
  3543.     if (options & WILD_SILENT)
  3544.     flags |= EW_SILENT;
  3545.  
  3546.     if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
  3547.     {
  3548.     /*
  3549.      * Expand file or directory names.
  3550.      */
  3551.     int    free_pat = FALSE;
  3552.     int    i;
  3553.  
  3554.     /* for ":set path=" and ":set tags=" halve backslashes for escaped
  3555.      * space */
  3556.     if (xp->xp_backslash != XP_BS_NONE)
  3557.     {
  3558.         free_pat = TRUE;
  3559.         pat = vim_strsave(pat);
  3560.         for (i = 0; pat[i]; ++i)
  3561.         if (pat[i] == '\\')
  3562.         {
  3563.             if (xp->xp_backslash == XP_BS_THREE
  3564.                 && pat[i + 1] == '\\'
  3565.                 && pat[i + 2] == '\\'
  3566.                 && pat[i + 3] == ' ')
  3567.             STRCPY(pat + i, pat + i + 3);
  3568.             if (xp->xp_backslash == XP_BS_ONE
  3569.                 && pat[i + 1] == ' ')
  3570.             STRCPY(pat + i, pat + i + 1);
  3571.         }
  3572.     }
  3573.  
  3574.     if (xp->xp_context == EXPAND_FILES)
  3575.         flags |= EW_FILE;
  3576.     else
  3577.         flags = (flags | EW_DIR) & ~EW_FILE;
  3578.     ret = expand_wildcards(1, &pat, num_file, file, flags);
  3579.     if (free_pat)
  3580.         vim_free(pat);
  3581.     return ret;
  3582.     }
  3583.  
  3584.     *file = (char_u **)"";
  3585.     *num_file = 0;
  3586.     if (xp->xp_context == EXPAND_HELP)
  3587.     return find_help_tags(pat, num_file, file);
  3588.  
  3589. #ifndef FEAT_CMDL_COMPL
  3590.     return FAIL;
  3591. #else
  3592.     if (xp->xp_context == EXPAND_OLD_SETTING)
  3593.     return ExpandOldSetting(num_file, file);
  3594.     if (xp->xp_context == EXPAND_BUFFERS)
  3595.     return ExpandBufnames(pat, num_file, file, options);
  3596.     if (xp->xp_context == EXPAND_TAGS
  3597.         || xp->xp_context == EXPAND_TAGS_LISTFILES)
  3598.     return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
  3599.     if (xp->xp_context == EXPAND_COLORS)
  3600.     return ExpandRTDir(pat, num_file, file, "colors");
  3601.     if (xp->xp_context == EXPAND_COMPILER)
  3602.     return ExpandRTDir(pat, num_file, file, "compiler");
  3603.  
  3604.     regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
  3605.     if (regmatch.regprog == NULL)
  3606.     return FAIL;
  3607.  
  3608.     /* set ignore-case according to p_ic, p_scs and pat */
  3609.     regmatch.rm_ic = ignorecase(pat);
  3610.  
  3611.     if (xp->xp_context == EXPAND_SETTINGS
  3612.         || xp->xp_context == EXPAND_BOOL_SETTINGS)
  3613.     ret = ExpandSettings(xp, ®match, num_file, file);
  3614.     else if (xp->xp_context == EXPAND_MAPPINGS)
  3615.     ret = ExpandMappings(®match, num_file, file);
  3616.     else if (xp->xp_context == EXPAND_USER_DEFINED)
  3617.     ret = ExpandUserDefined(xp, ®match, num_file, file);
  3618.     else
  3619.     {
  3620.     static struct expgen
  3621.     {
  3622.         int        context;
  3623.         char_u    *((*func)__ARGS((expand_T *, int)));
  3624.         int        ic;
  3625.     } tab[] =
  3626.     {
  3627.         {EXPAND_COMMANDS, get_command_name, FALSE},
  3628. #ifdef FEAT_USR_CMDS
  3629.         {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
  3630.         {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
  3631.         {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
  3632.         {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
  3633. #endif
  3634. #ifdef FEAT_EVAL
  3635.         {EXPAND_USER_VARS, get_user_var_name, FALSE},
  3636.         {EXPAND_FUNCTIONS, get_function_name, FALSE},
  3637.         {EXPAND_USER_FUNC, get_user_func_name, FALSE},
  3638.         {EXPAND_EXPRESSION, get_expr_name, FALSE},
  3639. #endif
  3640. #ifdef FEAT_MENU
  3641.         {EXPAND_MENUS, get_menu_name, FALSE},
  3642.         {EXPAND_MENUNAMES, get_menu_names, FALSE},
  3643. #endif
  3644. #ifdef FEAT_SYN_HL
  3645.         {EXPAND_SYNTAX, get_syntax_name, TRUE},
  3646. #endif
  3647.         {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
  3648. #ifdef FEAT_AUTOCMD
  3649.         {EXPAND_EVENTS, get_event_name, TRUE},
  3650.         {EXPAND_AUGROUP, get_augroup_name, TRUE},
  3651. #endif
  3652. #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
  3653.     && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
  3654.         {EXPAND_LANGUAGE, get_lang_arg, TRUE},
  3655. #endif
  3656.         {EXPAND_ENV_VARS, get_env_name, TRUE},
  3657.     };
  3658.     int    i;
  3659.  
  3660.     /*
  3661.      * Find a context in the table and call the ExpandGeneric() with the
  3662.      * right function to do the expansion.
  3663.      */
  3664.     ret = FAIL;
  3665.     for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
  3666.         if (xp->xp_context == tab[i].context)
  3667.         {
  3668.         if (tab[i].ic)
  3669.             regmatch.rm_ic = TRUE;
  3670.         ret = ExpandGeneric(xp, ®match, num_file, file, tab[i].func);
  3671.         break;
  3672.         }
  3673.     }
  3674.  
  3675.     vim_free(regmatch.regprog);
  3676.  
  3677.     return ret;
  3678. #endif /* FEAT_CMDL_COMPL */
  3679. }
  3680.  
  3681. #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
  3682. /*
  3683.  * Expand a list of names.
  3684.  *
  3685.  * Generic function for command line completion.  It calls a function to
  3686.  * obtain strings, one by one.    The strings are matched against a regexp
  3687.  * program.  Matching strings are copied into an array, which is returned.
  3688.  *
  3689.  * Returns OK when no problems encountered, FAIL for error (out of memory).
  3690.  */
  3691.     int
  3692. ExpandGeneric(xp, regmatch, num_file, file, func)
  3693.     expand_T    *xp;
  3694.     regmatch_T    *regmatch;
  3695.     int        *num_file;
  3696.     char_u    ***file;
  3697.     char_u    *((*func)__ARGS((expand_T *, int)));
  3698.                       /* returns a string from the list */
  3699. {
  3700.     int        i;
  3701.     int        count = 0;
  3702.     int        loop;
  3703.     char_u    *str;
  3704.  
  3705.     /* do this loop twice:
  3706.      * loop == 0: count the number of matching names
  3707.      * loop == 1: copy the matching names into allocated memory
  3708.      */
  3709.     for (loop = 0; loop <= 1; ++loop)
  3710.     {
  3711.     for (i = 0; ; ++i)
  3712.     {
  3713.         str = (*func)(xp, i);
  3714.         if (str == NULL)        /* end of list */
  3715.         break;
  3716.         if (*str == NUL)        /* skip empty strings */
  3717.         continue;
  3718.  
  3719.         if (vim_regexec(regmatch, str, (colnr_T)0))
  3720.         {
  3721.         if (loop)
  3722.         {
  3723.             str = vim_strsave_escaped(str, (char_u *)" \t\\.");
  3724.             (*file)[count] = str;
  3725. #ifdef FEAT_MENU
  3726.             if (func == get_menu_names && str != NULL)
  3727.             {
  3728.             /* test for separator added by get_menu_names() */
  3729.             str += STRLEN(str) - 1;
  3730.             if (*str == '\001')
  3731.                 *str = '.';
  3732.             }
  3733. #endif
  3734.         }
  3735.         ++count;
  3736.         }
  3737.     }
  3738.     if (loop == 0)
  3739.     {
  3740.         if (count == 0)
  3741.         return OK;
  3742.         *num_file = count;
  3743.         *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
  3744.         if (*file == NULL)
  3745.         {
  3746.         *file = (char_u **)"";
  3747.         return FAIL;
  3748.         }
  3749.         count = 0;
  3750.     }
  3751.     }
  3752.     return OK;
  3753. }
  3754.  
  3755. /*
  3756.  * Expand names with a function defined by the user.
  3757.  */
  3758.     static int
  3759. ExpandUserDefined(xp, regmatch, num_file, file)
  3760.     expand_T    *xp;
  3761.     regmatch_T    *regmatch;
  3762.     int        *num_file;
  3763.     char_u    ***file;
  3764. {
  3765. #ifdef FEAT_EVAL
  3766.     char_u    *args[3];
  3767.     char_u    *all;
  3768.     char_u    *s;
  3769.     char_u    *e;
  3770.     char_u      keep;
  3771.     char_u      num[50];
  3772.     garray_T    ga;
  3773.  
  3774.     if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
  3775.     return FAIL;
  3776.     *num_file = 0;
  3777.     *file = NULL;
  3778.  
  3779.     keep = ccline.cmdbuff[ccline.cmdlen];
  3780.     ccline.cmdbuff[ccline.cmdlen] = 0;
  3781.     sprintf((char *)num, "%d", ccline.cmdpos);
  3782.     args[0] = xp->xp_pattern;
  3783.     args[1] = ccline.cmdbuff;
  3784.     args[2] = num;
  3785.  
  3786.     all = call_vim_function(xp->xp_arg, 3, args, FALSE);
  3787.     ccline.cmdbuff[ccline.cmdlen] = keep;
  3788.     if (all == NULL)
  3789.     return FAIL;
  3790.  
  3791.     ga_init2(&ga, (int)sizeof(char *), 3);
  3792.     for (s = all; *s != NUL; s = e)
  3793.     {
  3794.     e = vim_strchr(s, '\n');
  3795.     if (e == NULL)
  3796.         e = s + STRLEN(s);
  3797.     keep = *e;
  3798.     *e = 0;
  3799.  
  3800.     if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
  3801.     {
  3802.         *e = keep;
  3803.         if (*e != NUL)
  3804.         ++e;
  3805.         continue;
  3806.     }
  3807.  
  3808.     if (ga_grow(&ga, 1) == FAIL)
  3809.         break;
  3810.  
  3811.     ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
  3812.     ++ga.ga_len;
  3813.     --ga.ga_room;
  3814.  
  3815.     *e = keep;
  3816.     if (*e != NUL)
  3817.         ++e;
  3818.     }
  3819.     vim_free(all);
  3820.     *file = ga.ga_data;
  3821.     *num_file = ga.ga_len;
  3822.     return OK;
  3823. #else
  3824.     return FAIL;
  3825. #endif
  3826. }
  3827.  
  3828. /*
  3829.  * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
  3830.  * or compiler names.
  3831.  */
  3832.     static int
  3833. ExpandRTDir(pat, num_file, file, dirname)
  3834.     char_u    *pat;
  3835.     int        *num_file;
  3836.     char_u    ***file;
  3837.     char    *dirname;    /* "colors" or "compiler" */
  3838. {
  3839.     char_u    *all;
  3840.     char_u    *s;
  3841.     char_u    *e;
  3842.     garray_T    ga;
  3843.  
  3844.     *num_file = 0;
  3845.     *file = NULL;
  3846.     s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
  3847.     if (s == NULL)
  3848.     return FAIL;
  3849.     sprintf((char *)s, "%s/%s*.vim", dirname, pat);
  3850.     all = globpath(p_rtp, s);
  3851.     vim_free(s);
  3852.     if (all == NULL)
  3853.     return FAIL;
  3854.  
  3855.     ga_init2(&ga, (int)sizeof(char *), 3);
  3856.     for (s = all; *s != NUL; s = e)
  3857.     {
  3858.     e = vim_strchr(s, '\n');
  3859.     if (e == NULL)
  3860.         e = s + STRLEN(s);
  3861.     if (ga_grow(&ga, 1) == FAIL)
  3862.         break;
  3863.     if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
  3864.     {
  3865.         for (s = e - 4; s > all; --s)
  3866.         if (*s == '\n' || vim_ispathsep(*s))
  3867.             break;
  3868.         ++s;
  3869.         ((char_u **)ga.ga_data)[ga.ga_len] =
  3870.                         vim_strnsave(s, (int)(e - s - 4));
  3871.         ++ga.ga_len;
  3872.         --ga.ga_room;
  3873.     }
  3874.     if (*e != NUL)
  3875.         ++e;
  3876.     }
  3877.     vim_free(all);
  3878.     *file = ga.ga_data;
  3879.     *num_file = ga.ga_len;
  3880.     return OK;
  3881. }
  3882.  
  3883. #endif
  3884.  
  3885. #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
  3886. /*
  3887.  * Expand "file" for all comma-separated directories in "path".
  3888.  * Returns an allocated string with all matches concatenated, separated by
  3889.  * newlines.  Returns NULL for an error or no matches.
  3890.  */
  3891.     char_u *
  3892. globpath(path, file)
  3893.     char_u    *path;
  3894.     char_u    *file;
  3895. {
  3896.     expand_T    xpc;
  3897.     char_u    *buf;
  3898.     garray_T    ga;
  3899.     int        i;
  3900.     int        len;
  3901.     int        num_p;
  3902.     char_u    **p;
  3903.     char_u    *cur = NULL;
  3904.  
  3905.     buf = alloc(MAXPATHL);
  3906.     if (buf == NULL)
  3907.     return NULL;
  3908.  
  3909.     xpc.xp_context = EXPAND_FILES;
  3910.     xpc.xp_backslash = XP_BS_NONE;
  3911.     ga_init2(&ga, 1, 100);
  3912.  
  3913.     /* Loop over all entries in {path}. */
  3914.     while (*path != NUL)
  3915.     {
  3916.     /* Copy one item of the path to buf[] and concatenate the file name. */
  3917.     copy_option_part(&path, buf, MAXPATHL, ",");
  3918.     if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
  3919.     {
  3920.         add_pathsep(buf);
  3921.         STRCAT(buf, file);
  3922.         if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
  3923.                                  && num_p > 0)
  3924.         {
  3925.         ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
  3926.         for (len = 0, i = 0; i < num_p; ++i)
  3927.             len += (long_u)STRLEN(p[i]) + 1;
  3928.  
  3929.         /* Concatenate new results to previous ones. */
  3930.         if (ga_grow(&ga, len) == OK)
  3931.         {
  3932.             cur = (char_u *)ga.ga_data + ga.ga_len;
  3933.             for (i = 0; i < num_p; ++i)
  3934.             {
  3935.             STRCPY(cur, p[i]);
  3936.             cur += STRLEN(p[i]);
  3937.             *cur++ = '\n';
  3938.             }
  3939.             ga.ga_len += len;
  3940.             ga.ga_room -= len;
  3941.         }
  3942.         }
  3943.     }
  3944.     }
  3945.     if (cur != NULL)
  3946.     *--cur = 0; /* Replace trailing newline with NUL */
  3947.  
  3948.     vim_free(buf);
  3949.     return (char_u *)ga.ga_data;
  3950. }
  3951.  
  3952. #endif
  3953.  
  3954. #if defined(FEAT_CMDHIST) || defined(PROTO)
  3955.  
  3956. /*********************************
  3957.  *  Command line history stuff     *
  3958.  *********************************/
  3959.  
  3960. /*
  3961.  * Translate a history character to the associated type number.
  3962.  */
  3963.     static int
  3964. hist_char2type(c)
  3965.     int        c;
  3966. {
  3967.     if (c == ':')
  3968.     return HIST_CMD;
  3969.     if (c == '=')
  3970.     return HIST_EXPR;
  3971.     if (c == '@')
  3972.     return HIST_INPUT;
  3973.     if (c == '>')
  3974.     return HIST_DEBUG;
  3975.     return HIST_SEARCH;        /* must be '?' or '/' */
  3976. }
  3977.  
  3978. /*
  3979.  * Table of history names.
  3980.  * These names are used in :history and various hist...() functions.
  3981.  * It is sufficient to give the significant prefix of a history name.
  3982.  */
  3983.  
  3984. static char *(history_names[]) =
  3985. {
  3986.     "cmd",
  3987.     "search",
  3988.     "expr",
  3989.     "input",
  3990.     "debug",
  3991.     NULL
  3992. };
  3993.  
  3994. /*
  3995.  * init_history() - Initialize the command line history.
  3996.  * Also used to re-allocate the history when the size changes.
  3997.  */
  3998.     static void
  3999. init_history()
  4000. {
  4001.     int        newlen;        /* new length of history table */
  4002.     histentry_T    *temp;
  4003.     int        i;
  4004.     int        j;
  4005.     int        type;
  4006.  
  4007.     /*
  4008.      * If size of history table changed, reallocate it
  4009.      */
  4010.     newlen = (int)p_hi;
  4011.     if (newlen != hislen)            /* history length changed */
  4012.     {
  4013.     for (type = 0; type < HIST_COUNT; ++type)   /* adjust the tables */
  4014.     {
  4015.         if (newlen)
  4016.         {
  4017.         temp = (histentry_T *)lalloc(
  4018.                 (long_u)(newlen * sizeof(histentry_T)), TRUE);
  4019.         if (temp == NULL)   /* out of memory! */
  4020.         {
  4021.             if (type == 0)  /* first one: just keep the old length */
  4022.             {
  4023.             newlen = hislen;
  4024.             break;
  4025.             }
  4026.             /* Already changed one table, now we can only have zero
  4027.              * length for all tables. */
  4028.             newlen = 0;
  4029.             type = -1;
  4030.             continue;
  4031.         }
  4032.         }
  4033.         else
  4034.         temp = NULL;
  4035.         if (newlen == 0 || temp != NULL)
  4036.         {
  4037.         if (hisidx[type] < 0)        /* there are no entries yet */
  4038.         {
  4039.             for (i = 0; i < newlen; ++i)
  4040.             {
  4041.             temp[i].hisnum = 0;
  4042.             temp[i].hisstr = NULL;
  4043.             }
  4044.         }
  4045.         else if (newlen > hislen)    /* array becomes bigger */
  4046.         {
  4047.             for (i = 0; i <= hisidx[type]; ++i)
  4048.             temp[i] = history[type][i];
  4049.             j = i;
  4050.             for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
  4051.             {
  4052.             temp[i].hisnum = 0;
  4053.             temp[i].hisstr = NULL;
  4054.             }
  4055.             for ( ; j < hislen; ++i, ++j)
  4056.             temp[i] = history[type][j];
  4057.         }
  4058.         else                /* array becomes smaller or 0 */
  4059.         {
  4060.             j = hisidx[type];
  4061.             for (i = newlen - 1; ; --i)
  4062.             {
  4063.             if (i >= 0)        /* copy newest entries */
  4064.                 temp[i] = history[type][j];
  4065.             else            /* remove older entries */
  4066.                 vim_free(history[type][j].hisstr);
  4067.             if (--j < 0)
  4068.                 j = hislen - 1;
  4069.             if (j == hisidx[type])
  4070.                 break;
  4071.             }
  4072.             hisidx[type] = newlen - 1;
  4073.         }
  4074.         vim_free(history[type]);
  4075.         history[type] = temp;
  4076.         }
  4077.     }
  4078.     hislen = newlen;
  4079.     }
  4080. }
  4081.  
  4082. /*
  4083.  * Check if command line 'str' is already in history.
  4084.  * If 'move_to_front' is TRUE, matching entry is moved to end of history.
  4085.  */
  4086.     static int
  4087. in_history(type, str, move_to_front)
  4088.     int        type;
  4089.     char_u  *str;
  4090.     int        move_to_front;    /* Move the entry to the front if it exists */
  4091. {
  4092.     int        i;
  4093.     int        last_i = -1;
  4094.  
  4095.     if (hisidx[type] < 0)
  4096.     return FALSE;
  4097.     i = hisidx[type];
  4098.     do
  4099.     {
  4100.     if (history[type][i].hisstr == NULL)
  4101.         return FALSE;
  4102.     if (STRCMP(str, history[type][i].hisstr) == 0)
  4103.     {
  4104.         if (!move_to_front)
  4105.         return TRUE;
  4106.         last_i = i;
  4107.         break;
  4108.     }
  4109.     if (--i < 0)
  4110.         i = hislen - 1;
  4111.     } while (i != hisidx[type]);
  4112.  
  4113.     if (last_i >= 0)
  4114.     {
  4115.     str = history[type][i].hisstr;
  4116.     while (i != hisidx[type])
  4117.     {
  4118.         if (++i >= hislen)
  4119.         i = 0;
  4120.         history[type][last_i] = history[type][i];
  4121.         last_i = i;
  4122.     }
  4123.     history[type][i].hisstr = str;
  4124.     history[type][i].hisnum = ++hisnum[type];
  4125.     return TRUE;
  4126.     }
  4127.     return FALSE;
  4128. }
  4129.  
  4130. /*
  4131.  * Convert history name (from table above) to its HIST_ equivalent.
  4132.  * When "name" is empty, return "cmd" history.
  4133.  * Returns -1 for unknown history name.
  4134.  */
  4135.     int
  4136. get_histtype(name)
  4137.     char_u    *name;
  4138. {
  4139.     int        i;
  4140.     int        len = (int)STRLEN(name);
  4141.  
  4142.     /* No argument: use current history. */
  4143.     if (len == 0)
  4144.     return hist_char2type(ccline.cmdfirstc);
  4145.  
  4146.     for (i = 0; history_names[i] != NULL; ++i)
  4147.     if (STRNICMP(name, history_names[i], len) == 0)
  4148.         return i;
  4149.  
  4150.     if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
  4151.     return hist_char2type(name[0]);
  4152.  
  4153.     return -1;
  4154. }
  4155.  
  4156. static int    last_maptick = -1;    /* last seen maptick */
  4157.  
  4158. /*
  4159.  * Add the given string to the given history.  If the string is already in the
  4160.  * history then it is moved to the front.  "histype" may be one of he HIST_
  4161.  * values.
  4162.  */
  4163.     void
  4164. add_to_history(histype, new_entry, in_map)
  4165.     int        histype;
  4166.     char_u    *new_entry;
  4167.     int        in_map;        /* consider maptick when inside a mapping */
  4168. {
  4169.     histentry_T    *hisptr;
  4170.  
  4171.     if (hislen == 0)        /* no history */
  4172.     return;
  4173.  
  4174.     /*
  4175.      * Searches inside the same mapping overwrite each other, so that only
  4176.      * the last line is kept.  Be careful not to remove a line that was moved
  4177.      * down, only lines that were added.
  4178.      */
  4179.     if (histype == HIST_SEARCH && in_map)
  4180.     {
  4181.     if (maptick == last_maptick)
  4182.     {
  4183.         /* Current line is from the same mapping, remove it */
  4184.         hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
  4185.         vim_free(hisptr->hisstr);
  4186.         hisptr->hisstr = NULL;
  4187.         hisptr->hisnum = 0;
  4188.         --hisnum[histype];
  4189.         if (--hisidx[HIST_SEARCH] < 0)
  4190.         hisidx[HIST_SEARCH] = hislen - 1;
  4191.     }
  4192.     last_maptick = -1;
  4193.     }
  4194.     if (!in_history(histype, new_entry, TRUE))
  4195.     {
  4196.     if (++hisidx[histype] == hislen)
  4197.         hisidx[histype] = 0;
  4198.     hisptr = &history[histype][hisidx[histype]];
  4199.     vim_free(hisptr->hisstr);
  4200.     hisptr->hisstr = vim_strsave(new_entry);
  4201.     hisptr->hisnum = ++hisnum[histype];
  4202.     if (histype == HIST_SEARCH && in_map)
  4203.         last_maptick = maptick;
  4204.     }
  4205. }
  4206.  
  4207. #if defined(FEAT_EVAL) || defined(PROTO)
  4208.  
  4209. /*
  4210.  * Get identifier of newest history entry.
  4211.  * "histype" may be one of the HIST_ values.
  4212.  */
  4213.     int
  4214. get_history_idx(histype)
  4215.     int        histype;
  4216. {
  4217.     if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
  4218.             || hisidx[histype] < 0)
  4219.     return -1;
  4220.  
  4221.     return history[histype][hisidx[histype]].hisnum;
  4222. }
  4223.  
  4224. /*
  4225.  * Calculate history index from a number:
  4226.  *   num > 0: seen as identifying number of a history entry
  4227.  *   num < 0: relative position in history wrt newest entry
  4228.  * "histype" may be one of the HIST_ values.
  4229.  */
  4230.     static int
  4231. calc_hist_idx(histype, num)
  4232.     int        histype;
  4233.     int        num;
  4234. {
  4235.     int        i;
  4236.     histentry_T    *hist;
  4237.     int        wrapped = FALSE;
  4238.  
  4239.     if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
  4240.             || (i = hisidx[histype]) < 0 || num == 0)
  4241.     return -1;
  4242.  
  4243.     hist = history[histype];
  4244.     if (num > 0)
  4245.     {
  4246.     while (hist[i].hisnum > num)
  4247.         if (--i < 0)
  4248.         {
  4249.         if (wrapped)
  4250.             break;
  4251.         i += hislen;
  4252.         wrapped = TRUE;
  4253.         }
  4254.     if (hist[i].hisnum == num && hist[i].hisstr != NULL)
  4255.         return i;
  4256.     }
  4257.     else if (-num <= hislen)
  4258.     {
  4259.     i += num + 1;
  4260.     if (i < 0)
  4261.         i += hislen;
  4262.     if (hist[i].hisstr != NULL)
  4263.         return i;
  4264.     }
  4265.     return -1;
  4266. }
  4267.  
  4268. /*
  4269.  * Get a history entry by its index.
  4270.  * "histype" may be one of the HIST_ values.
  4271.  */
  4272.     char_u *
  4273. get_history_entry(histype, idx)
  4274.     int        histype;
  4275.     int        idx;
  4276. {
  4277.     idx = calc_hist_idx(histype, idx);
  4278.     if (idx >= 0)
  4279.     return history[histype][idx].hisstr;
  4280.     else
  4281.     return (char_u *)"";
  4282. }
  4283.  
  4284. /*
  4285.  * Clear all entries of a history.
  4286.  * "histype" may be one of the HIST_ values.
  4287.  */
  4288.     int
  4289. clr_history(histype)
  4290.     int        histype;
  4291. {
  4292.     int        i;
  4293.     histentry_T    *hisptr;
  4294.  
  4295.     if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
  4296.     {
  4297.     hisptr = history[histype];
  4298.     for (i = hislen; i--;)
  4299.     {
  4300.         vim_free(hisptr->hisstr);
  4301.         hisptr->hisnum = 0;
  4302.         hisptr++->hisstr = NULL;
  4303.     }
  4304.     hisidx[histype] = -1;    /* mark history as cleared */
  4305.     hisnum[histype] = 0;    /* reset identifier counter */
  4306.     return OK;
  4307.     }
  4308.     return FAIL;
  4309. }
  4310.  
  4311. /*
  4312.  * Remove all entries matching {str} from a history.
  4313.  * "histype" may be one of the HIST_ values.
  4314.  */
  4315.     int
  4316. del_history_entry(histype, str)
  4317.     int        histype;
  4318.     char_u    *str;
  4319. {
  4320.     regmatch_T    regmatch;
  4321.     histentry_T    *hisptr;
  4322.     int        idx;
  4323.     int        i;
  4324.     int        last;
  4325.     int        found = FALSE;
  4326.  
  4327.     regmatch.regprog = NULL;
  4328.     regmatch.rm_ic = FALSE;    /* always match case */
  4329.     if (hislen != 0
  4330.         && histype >= 0
  4331.         && histype < HIST_COUNT
  4332.         && *str != NUL
  4333.         && (idx = hisidx[histype]) >= 0
  4334.         && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
  4335.                                       != NULL)
  4336.     {
  4337.     i = last = idx;
  4338.     do
  4339.     {
  4340.         hisptr = &history[histype][i];
  4341.         if (hisptr->hisstr == NULL)
  4342.         break;
  4343.         if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0))
  4344.         {
  4345.         found = TRUE;
  4346.         vim_free(hisptr->hisstr);
  4347.         hisptr->hisstr = NULL;
  4348.         hisptr->hisnum = 0;
  4349.         }
  4350.         else
  4351.         {
  4352.         if (i != last)
  4353.         {
  4354.             history[histype][last] = *hisptr;
  4355.             hisptr->hisstr = NULL;
  4356.             hisptr->hisnum = 0;
  4357.         }
  4358.         if (--last < 0)
  4359.             last += hislen;
  4360.         }
  4361.         if (--i < 0)
  4362.         i += hislen;
  4363.     } while (i != idx);
  4364.     if (history[histype][idx].hisstr == NULL)
  4365.         hisidx[histype] = -1;
  4366.     }
  4367.     vim_free(regmatch.regprog);
  4368.     return found;
  4369. }
  4370.  
  4371. /*
  4372.  * Remove an indexed entry from a history.
  4373.  * "histype" may be one of the HIST_ values.
  4374.  */
  4375.     int
  4376. del_history_idx(histype, idx)
  4377.     int        histype;
  4378.     int        idx;
  4379. {
  4380.     int        i, j;
  4381.  
  4382.     i = calc_hist_idx(histype, idx);
  4383.     if (i < 0)
  4384.     return FALSE;
  4385.     idx = hisidx[histype];
  4386.     vim_free(history[histype][i].hisstr);
  4387.  
  4388.     /* When deleting the last added search string in a mapping, reset
  4389.      * last_maptick, so that the last added search string isn't deleted again.
  4390.      */
  4391.     if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
  4392.     last_maptick = -1;
  4393.  
  4394.     while (i != idx)
  4395.     {
  4396.     j = (i + 1) % hislen;
  4397.     history[histype][i] = history[histype][j];
  4398.     i = j;
  4399.     }
  4400.     history[histype][i].hisstr = NULL;
  4401.     history[histype][i].hisnum = 0;
  4402.     if (--i < 0)
  4403.     i += hislen;
  4404.     hisidx[histype] = i;
  4405.     return TRUE;
  4406. }
  4407.  
  4408. #endif /* FEAT_EVAL */
  4409.  
  4410. #if defined(FEAT_CRYPT) || defined(PROTO)
  4411. /*
  4412.  * Very specific function to remove the value in ":set key=val" from the
  4413.  * history.
  4414.  */
  4415.     void
  4416. remove_key_from_history()
  4417. {
  4418.     char_u    *p;
  4419.     int        i;
  4420.  
  4421.     i = hisidx[HIST_CMD];
  4422.     if (i < 0)
  4423.     return;
  4424.     p = history[HIST_CMD][i].hisstr;
  4425.     if (p != NULL)
  4426.     for ( ; *p; ++p)
  4427.         if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
  4428.         {
  4429.         p = vim_strchr(p + 3, '=');
  4430.         if (p == NULL)
  4431.             break;
  4432.         ++p;
  4433.         for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
  4434.             if (p[i] == '\\' && p[i + 1])
  4435.             ++i;
  4436.         mch_memmove(p, p + i, STRLEN(p + i) + 1);
  4437.         --p;
  4438.         }
  4439. }
  4440. #endif
  4441.  
  4442. #endif /* FEAT_CMDHIST */
  4443.  
  4444. #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
  4445. /*
  4446.  * Get indices "num1,num2" that specify a range within a list (not a range of
  4447.  * text lines in a buffer!) from a string.  Used for ":history" and ":clist".
  4448.  * Returns OK if parsed successfully, otherwise FAIL.
  4449.  */
  4450.     int
  4451. get_list_range(str, num1, num2)
  4452.     char_u    **str;
  4453.     int        *num1;
  4454.     int        *num2;
  4455. {
  4456.     int        len;
  4457.     int        first = FALSE;
  4458.     long    num;
  4459.  
  4460.     *str = skipwhite(*str);
  4461.     if (**str == '-' || isdigit(**str))    /* parse "from" part of range */
  4462.     {
  4463.     vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
  4464.     *str += len;
  4465.     *num1 = (int)num;
  4466.     first = TRUE;
  4467.     }
  4468.     *str = skipwhite(*str);
  4469.     if (**str == ',')            /* parse "to" part of range */
  4470.     {
  4471.     *str = skipwhite(*str + 1);
  4472.     vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
  4473.     if (len > 0)
  4474.     {
  4475.         *num2 = (int)num;
  4476.         *str = skipwhite(*str + len);
  4477.     }
  4478.     else if (!first)        /* no number given at all */
  4479.         return FAIL;
  4480.     }
  4481.     else if (first)            /* only one number given */
  4482.     *num2 = *num1;
  4483.     return OK;
  4484. }
  4485. #endif
  4486.  
  4487. #if defined(FEAT_CMDHIST) || defined(PROTO)
  4488. /*
  4489.  * :history command - print a history
  4490.  */
  4491.     void
  4492. ex_history(eap)
  4493.     exarg_T    *eap;
  4494. {
  4495.     histentry_T    *hist;
  4496.     int        histype1 = HIST_CMD;
  4497.     int        histype2 = HIST_CMD;
  4498.     int        hisidx1 = 1;
  4499.     int        hisidx2 = -1;
  4500.     int        idx;
  4501.     int        i, j, k;
  4502.     char_u    *end;
  4503.     char_u    *arg = eap->arg;
  4504.  
  4505.     if (hislen == 0)
  4506.     {
  4507.     MSG(_("'history' option is zero"));
  4508.     return;
  4509.     }
  4510.  
  4511.     if (!(isdigit(*arg) || *arg == '-' || *arg == ','))
  4512.     {
  4513.     end = arg;
  4514.     while (ASCII_ISALPHA(*end)
  4515.         || vim_strchr((char_u *)":=@>/?", *end) != NULL)
  4516.         end++;
  4517.     i = *end;
  4518.     *end = NUL;
  4519.     histype1 = get_histtype(arg);
  4520.     if (histype1 == -1)
  4521.     {
  4522.         if (STRICMP(arg, "all") == 0)
  4523.         {
  4524.         histype1 = 0;
  4525.         histype2 = HIST_COUNT-1;
  4526.         }
  4527.         else
  4528.         {
  4529.         *end = i;
  4530.         EMSG(_(e_trailing));
  4531.         return;
  4532.         }
  4533.     }
  4534.     else
  4535.         histype2 = histype1;
  4536.     *end = i;
  4537.     }
  4538.     else
  4539.     end = arg;
  4540.     if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
  4541.     {
  4542.     EMSG(_(e_trailing));
  4543.     return;
  4544.     }
  4545.  
  4546.     for (; !got_int && histype1 <= histype2; ++histype1)
  4547.     {
  4548.     STRCPY(IObuff, "\n      #  ");
  4549.     STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
  4550.     MSG_PUTS_TITLE(IObuff);
  4551.     idx = hisidx[histype1];
  4552.     hist = history[histype1];
  4553.     j = hisidx1;
  4554.     k = hisidx2;
  4555.     if (j < 0)
  4556.         j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
  4557.     if (k < 0)
  4558.         k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
  4559.     if (idx >= 0 && j <= k)
  4560.         for (i = idx + 1; !got_int; ++i)
  4561.         {
  4562.         if (i == hislen)
  4563.             i = 0;
  4564.         if (hist[i].hisstr != NULL
  4565.             && hist[i].hisnum >= j && hist[i].hisnum <= k)
  4566.         {
  4567.             msg_putchar('\n');
  4568.             sprintf((char *)IObuff, "%c%6d  %s", i == idx ? '>' : ' ',
  4569.                         hist[i].hisnum, hist[i].hisstr);
  4570.             msg_outtrans(IObuff);
  4571.             out_flush();
  4572.         }
  4573.         if (i == idx)
  4574.             break;
  4575.         }
  4576.     }
  4577. }
  4578. #endif
  4579.  
  4580. #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
  4581. static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
  4582. static int    viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
  4583. static int    viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
  4584. static int    viminfo_add_at_front = FALSE;
  4585.  
  4586. static int    hist_type2char __ARGS((int type, int use_question));
  4587.  
  4588. /*
  4589.  * Translate a history type number to the associated character.
  4590.  */
  4591.     static int
  4592. hist_type2char(type, use_question)
  4593.     int        type;
  4594.     int        use_question;        /* use '?' instead of '/' */
  4595. {
  4596.     if (type == HIST_CMD)
  4597.     return ':';
  4598.     if (type == HIST_SEARCH)
  4599.     {
  4600.     if (use_question)
  4601.         return '?';
  4602.     else
  4603.         return '/';
  4604.     }
  4605.     if (type == HIST_EXPR)
  4606.     return '=';
  4607.     return '@';
  4608. }
  4609.  
  4610. /*
  4611.  * Prepare for reading the history from the viminfo file.
  4612.  * This allocates history arrays to store the read history lines.
  4613.  */
  4614.     void
  4615. prepare_viminfo_history(asklen)
  4616.     int        asklen;
  4617. {
  4618.     int        i;
  4619.     int        num;
  4620.     int        type;
  4621.     int        len;
  4622.  
  4623.     init_history();
  4624.     viminfo_add_at_front = (asklen != 0);
  4625.     if (asklen > hislen)
  4626.     asklen = hislen;
  4627.  
  4628.     for (type = 0; type < HIST_COUNT; ++type)
  4629.     {
  4630.     /*
  4631.      * Count the number of empty spaces in the history list.  If there are
  4632.      * more spaces available than we request, then fill them up.
  4633.      */
  4634.     for (i = 0, num = 0; i < hislen; i++)
  4635.         if (history[type][i].hisstr == NULL)
  4636.         num++;
  4637.     len = asklen;
  4638.     if (num > len)
  4639.         len = num;
  4640.     if (len <= 0)
  4641.         viminfo_history[type] = NULL;
  4642.     else
  4643.         viminfo_history[type] =
  4644.            (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
  4645.     if (viminfo_history[type] == NULL)
  4646.         len = 0;
  4647.     viminfo_hislen[type] = len;
  4648.     viminfo_hisidx[type] = 0;
  4649.     }
  4650. }
  4651.  
  4652. /*
  4653.  * Accept a line from the viminfo, store it in the history array when it's
  4654.  * new.
  4655.  */
  4656.     int
  4657. read_viminfo_history(virp)
  4658.     vir_T    *virp;
  4659. {
  4660.     int        type;
  4661.     char_u    *val;
  4662.  
  4663.     type = hist_char2type(virp->vir_line[0]);
  4664.     if (viminfo_hisidx[type] < viminfo_hislen[type])
  4665.     {
  4666.     val = viminfo_readstring(virp, 1, TRUE);
  4667.     if (val != NULL)
  4668.     {
  4669.         if (!in_history(type, val, viminfo_add_at_front))
  4670.         viminfo_history[type][viminfo_hisidx[type]++] = val;
  4671.         else
  4672.         vim_free(val);
  4673.     }
  4674.     }
  4675.     return viminfo_readline(virp);
  4676. }
  4677.  
  4678.     void
  4679. finish_viminfo_history()
  4680. {
  4681.     int idx;
  4682.     int i;
  4683.     int    type;
  4684.  
  4685.     for (type = 0; type < HIST_COUNT; ++type)
  4686.     {
  4687.     if (history[type] == NULL)
  4688.         return;
  4689.     idx = hisidx[type] + viminfo_hisidx[type];
  4690.     if (idx >= hislen)
  4691.         idx -= hislen;
  4692.     else if (idx < 0)
  4693.         idx = hislen - 1;
  4694.     if (viminfo_add_at_front)
  4695.         hisidx[type] = idx;
  4696.     else
  4697.     {
  4698.         if (hisidx[type] == -1)
  4699.         hisidx[type] = hislen - 1;
  4700.         do
  4701.         {
  4702.         if (history[type][idx].hisstr != NULL)
  4703.             break;
  4704.         if (++idx == hislen)
  4705.             idx = 0;
  4706.         } while (idx != hisidx[type]);
  4707.         if (idx != hisidx[type] && --idx < 0)
  4708.         idx = hislen - 1;
  4709.     }
  4710.     for (i = 0; i < viminfo_hisidx[type]; i++)
  4711.     {
  4712.         vim_free(history[type][idx].hisstr);
  4713.         history[type][idx].hisstr = viminfo_history[type][i];
  4714.         if (--idx < 0)
  4715.         idx = hislen - 1;
  4716.     }
  4717.     idx += 1;
  4718.     idx %= hislen;
  4719.     for (i = 0; i < viminfo_hisidx[type]; i++)
  4720.     {
  4721.         history[type][idx++].hisnum = ++hisnum[type];
  4722.         idx %= hislen;
  4723.     }
  4724.     vim_free(viminfo_history[type]);
  4725.     viminfo_history[type] = NULL;
  4726.     }
  4727. }
  4728.  
  4729.     void
  4730. write_viminfo_history(fp)
  4731.     FILE    *fp;
  4732. {
  4733.     int        i;
  4734.     int        type;
  4735.     int        num_saved;
  4736.  
  4737.     init_history();
  4738.     if (hislen == 0)
  4739.     return;
  4740.     for (type = 0; type < HIST_COUNT; ++type)
  4741.     {
  4742.     num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
  4743.     if (num_saved == 0)
  4744.         continue;
  4745.     if (num_saved < 0)  /* Use default */
  4746.         num_saved = hislen;
  4747.     fprintf(fp, _("\n# %s History (newest to oldest):\n"),
  4748.                 type == HIST_CMD ? _("Command Line") :
  4749.                 type == HIST_SEARCH ? _("Search String") :
  4750.                 type == HIST_EXPR ?  _("Expression") :
  4751.                     _("Input Line"));
  4752.     if (num_saved > hislen)
  4753.         num_saved = hislen;
  4754.     i = hisidx[type];
  4755.     if (i >= 0)
  4756.         while (num_saved--)
  4757.         {
  4758.         if (history[type][i].hisstr != NULL)
  4759.         {
  4760.             putc(hist_type2char(type, TRUE), fp);
  4761.             viminfo_writestring(fp, history[type][i].hisstr);
  4762.         }
  4763.         if (--i < 0)
  4764.             i = hislen - 1;
  4765.         }
  4766.     }
  4767. }
  4768. #endif /* FEAT_VIMINFO */
  4769.  
  4770. #if defined(FEAT_FKMAP) || defined(PROTO)
  4771. /*
  4772.  * Write a character at the current cursor+offset position.
  4773.  * It is directly written into the command buffer block.
  4774.  */
  4775.     void
  4776. cmd_pchar(c, offset)
  4777.     int        c, offset;
  4778. {
  4779.     if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
  4780.     {
  4781.     EMSG(_("E198: cmd_pchar beyond the command length"));
  4782.     return;
  4783.     }
  4784.     ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
  4785.     ccline.cmdbuff[ccline.cmdlen] = NUL;
  4786. }
  4787.  
  4788.     int
  4789. cmd_gchar(offset)
  4790.     int        offset;
  4791. {
  4792.     if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
  4793.     {
  4794.     /*  EMSG(_("cmd_gchar beyond the command length")); */
  4795.     return NUL;
  4796.     }
  4797.     return (int)ccline.cmdbuff[ccline.cmdpos + offset];
  4798. }
  4799. #endif
  4800.  
  4801. #if defined(FEAT_CMDWIN) || defined(PROTO)
  4802. /*
  4803.  * Open a window on the current command line and history.  Allow editing in
  4804.  * the window.  Returns when the window is closed.
  4805.  * Returns:
  4806.  *    CR     if the command is to be executed
  4807.  *    Ctrl_C     if it is to be abandoned
  4808.  *    K_IGNORE if editing continues
  4809.  */
  4810.     static int
  4811. ex_window()
  4812. {
  4813.     struct cmdline_info    save_ccline;
  4814.     buf_T        *old_curbuf = curbuf;
  4815.     win_T        *old_curwin = curwin;
  4816.     buf_T        *bp;
  4817.     win_T        *wp;
  4818.     int            i;
  4819.     linenr_T        lnum;
  4820.     int            histtype;
  4821.     garray_T        winsizes;
  4822.     char_u        typestr[2];
  4823.     int            save_restart_edit = restart_edit;
  4824.     int            save_State = State;
  4825.     int            save_exmode = exmode_active;
  4826.  
  4827.     /* Can't do this recursively.  Can't do it when typing a password. */
  4828.     if (cmdwin_type != 0
  4829. # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
  4830.         || cmdline_star > 0
  4831. # endif
  4832.         )
  4833.     {
  4834.     beep_flush();
  4835.     return K_IGNORE;
  4836.     }
  4837.  
  4838.     /* Save current window sizes. */
  4839.     win_size_save(&winsizes);
  4840.  
  4841. # ifdef FEAT_AUTOCMD
  4842.     /* Don't execute autocommands while creating the window. */
  4843.     ++autocmd_block;
  4844. # endif
  4845.     /* Create a window for the command-line buffer. */
  4846.     if (win_split((int)p_cwh, WSP_BOT) == FAIL)
  4847.     {
  4848.     beep_flush();
  4849.     return K_IGNORE;
  4850.     }
  4851.     cmdwin_type = ccline.cmdfirstc;
  4852.     if (cmdwin_type == NUL)
  4853.     cmdwin_type = '-';
  4854.  
  4855.     /* Create the command-line buffer empty. */
  4856.     (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
  4857.     (void)setfname((char_u *)"command-line", NULL, TRUE);
  4858.     set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
  4859.     set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
  4860.     curbuf->b_p_ma = TRUE;
  4861. # ifdef FEAT_RIGHTLEFT
  4862.     curwin->w_p_rl = FALSE;
  4863. # endif
  4864. # ifdef FEAT_SCROLLBIND
  4865.     curwin->w_p_scb = FALSE;
  4866. # endif
  4867.  
  4868. # ifdef FEAT_AUTOCMD
  4869.     /* Do execute autocommands for setting the filetype (load syntax). */
  4870.     --autocmd_block;
  4871. # endif
  4872.  
  4873.     histtype = hist_char2type(ccline.cmdfirstc);
  4874.     if (histtype == HIST_CMD || histtype == HIST_DEBUG)
  4875.     {
  4876.     if (p_wc == TAB)
  4877.     {
  4878.         add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
  4879.         add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
  4880.     }
  4881.     set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
  4882.     }
  4883.  
  4884.     /* Fill the buffer with the history. */
  4885.     init_history();
  4886.     if (hislen > 0)
  4887.     {
  4888.     i = hisidx[histtype];
  4889.     if (i >= 0)
  4890.     {
  4891.         lnum = 0;
  4892.         do
  4893.         {
  4894.         if (++i == hislen)
  4895.             i = 0;
  4896.         if (history[histtype][i].hisstr != NULL)
  4897.             ml_append(lnum++, history[histtype][i].hisstr,
  4898.                                (colnr_T)0, FALSE);
  4899.         }
  4900.         while (i != hisidx[histtype]);
  4901.     }
  4902.     }
  4903.  
  4904.     /* Replace the empty last line with the current command-line and put the
  4905.      * cursor there. */
  4906.     ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
  4907.     curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  4908.     curwin->w_cursor.col = ccline.cmdpos;
  4909.     redraw_later(NOT_VALID);
  4910.  
  4911.     /* Save the command line info, can be used recursively. */
  4912.     save_ccline = ccline;
  4913.     ccline.cmdbuff = NULL;
  4914.     ccline.cmdprompt = NULL;
  4915.  
  4916.     /* No Ex mode here! */
  4917.     exmode_active = 0;
  4918.  
  4919.     State = NORMAL;
  4920. # ifdef FEAT_MOUSE
  4921.     setmouse();
  4922. # endif
  4923.  
  4924. # ifdef FEAT_AUTOCMD
  4925.     /* Trigger CmdwinEnter autocommands. */
  4926.     typestr[0] = cmdwin_type;
  4927.     typestr[1] = NUL;
  4928.     apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
  4929. # endif
  4930.  
  4931.     i = RedrawingDisabled;
  4932.     RedrawingDisabled = 0;
  4933.  
  4934.     /*
  4935.      * Call the main loop until <CR> or CTRL-C is typed.
  4936.      */
  4937.     cmdwin_result = 0;
  4938.     main_loop(TRUE);
  4939.  
  4940.     RedrawingDisabled = i;
  4941.  
  4942. # ifdef FEAT_AUTOCMD
  4943.     /* Trigger CmdwinLeave autocommands. */
  4944.     apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
  4945. # endif
  4946.  
  4947.     /* Restore the comand line info. */
  4948.     ccline = save_ccline;
  4949.     cmdwin_type = 0;
  4950.  
  4951.     exmode_active = save_exmode;
  4952.  
  4953.     /* Safety check: The old window or buffer was deleted: It's a a bug when
  4954.      * this happens! */
  4955.     if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
  4956.     {
  4957.     cmdwin_result = Ctrl_C;
  4958.     EMSG(_("E199: Active window or buffer deleted"));
  4959.     }
  4960.     else
  4961.     {
  4962. # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
  4963.     /* autocmds may abort script processing */
  4964.     if (aborting() && cmdwin_result != K_IGNORE)
  4965.         cmdwin_result = Ctrl_C;
  4966. # endif
  4967.     /* Set the new command line from the cmdline buffer. */
  4968.     vim_free(ccline.cmdbuff);
  4969.     if (cmdwin_result == K_XF1)        /* :qa! typed */
  4970.     {
  4971.         ccline.cmdbuff = vim_strsave((char_u *)"qa!");
  4972.         cmdwin_result = CR;
  4973.     }
  4974.     else if (cmdwin_result == K_XF2)    /* :qa typed */
  4975.     {
  4976.         ccline.cmdbuff = vim_strsave((char_u *)"qa");
  4977.         cmdwin_result = CR;
  4978.     }
  4979.     else
  4980.         ccline.cmdbuff = vim_strsave(ml_get_curline());
  4981.     if (ccline.cmdbuff == NULL)
  4982.         cmdwin_result = Ctrl_C;
  4983.     else
  4984.     {
  4985.         ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
  4986.         ccline.cmdbufflen = ccline.cmdlen + 1;
  4987.         ccline.cmdpos = curwin->w_cursor.col;
  4988.         if (ccline.cmdpos > ccline.cmdlen)
  4989.         ccline.cmdpos = ccline.cmdlen;
  4990.         if (cmdwin_result == K_IGNORE)
  4991.         {
  4992.         set_cmdspos_cursor();
  4993.         redrawcmd();
  4994.         }
  4995.     }
  4996.  
  4997. # ifdef FEAT_AUTOCMD
  4998.     /* Don't execute autocommands while deleting the window. */
  4999.     ++autocmd_block;
  5000. # endif
  5001.     wp = curwin;
  5002.     bp = curbuf;
  5003.     win_goto(old_curwin);
  5004.     win_close(wp, TRUE);
  5005.     close_buffer(NULL, bp, DOBUF_WIPE);
  5006.  
  5007.     /* Restore window sizes. */
  5008.     win_size_restore(&winsizes);
  5009.  
  5010. # ifdef FEAT_AUTOCMD
  5011.     --autocmd_block;
  5012. # endif
  5013.     }
  5014.  
  5015.     ga_clear(&winsizes);
  5016.     restart_edit = save_restart_edit;
  5017.  
  5018.     State = save_State;
  5019. # ifdef FEAT_MOUSE
  5020.     setmouse();
  5021. # endif
  5022.  
  5023.     return cmdwin_result;
  5024. }
  5025. #endif /* FEAT_CMDWIN */
  5026.  
  5027. #if defined(FEAT_PYTHON) || defined(FEAT_RUBY) || defined(FEAT_TCL) \
  5028.     || defined(FEAT_PERL) || defined(PROTO)
  5029. /*
  5030.  * Used for commands that either take a simple command string argument, or:
  5031.  *    cmd << endmarker
  5032.  *      {script}
  5033.  *    endmarker
  5034.  * Returns a pointer to allocated memory with {script} or NULL.
  5035.  */
  5036.     char_u *
  5037. script_get(eap, cmd)
  5038.     exarg_T    *eap;
  5039.     char_u    *cmd;
  5040. {
  5041.     char_u    *theline;
  5042.     char    *end_pattern = NULL;
  5043.     char    dot[] = ".";
  5044.     garray_T    ga;
  5045.  
  5046.     if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
  5047.     return NULL;
  5048.  
  5049.     ga_init2(&ga, 1, 0x400);
  5050.  
  5051.     if (cmd[2] != NUL)
  5052.     end_pattern = (char *)skipwhite(cmd + 2);
  5053.     else
  5054.     end_pattern = dot;
  5055.  
  5056.     for (;;)
  5057.     {
  5058.     theline = eap->getline(
  5059. #ifdef FEAT_EVAL
  5060.         eap->cstack->cs_whilelevel > 0 ? -1 :
  5061. #endif
  5062.         NUL, eap->cookie, 0);
  5063.  
  5064.     if (theline == NULL || STRCMP(end_pattern, theline) == 0)
  5065.         break;
  5066.  
  5067.     ga_concat(&ga, theline);
  5068.     ga_append(&ga, '\n');
  5069.     vim_free(theline);
  5070.     }
  5071.  
  5072.     return (char_u *)ga.ga_data;
  5073. }
  5074. #endif /* SCRIPTS */
  5075.