Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 28 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 34 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 35 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 36 | int xp_context; /* type of expansion */ |
| 37 | # ifdef FEAT_EVAL |
| 38 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 39 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 43 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 44 | * used by other functions. When invoking getcmdline() recursively it needs |
| 45 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 46 | * TODO: make it local to getcmdline() and pass it around. */ |
| 47 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 50 | |
| 51 | #ifdef FEAT_EVAL |
| 52 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 53 | #endif |
| 54 | |
| 55 | #ifdef FEAT_CMDHIST |
| 56 | typedef struct hist_entry |
| 57 | { |
| 58 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 59 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 61 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | } histentry_T; |
| 63 | |
| 64 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 65 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 66 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 67 | /* identifying (unique) number of newest history entry */ |
| 68 | static int hislen = 0; /* actual length of history tables */ |
| 69 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 70 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 72 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 74 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | # endif |
| 76 | #endif |
| 77 | |
| 78 | #ifdef FEAT_RIGHTLEFT |
| 79 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 80 | #endif |
| 81 | |
| 82 | #ifdef FEAT_FKMAP |
| 83 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 84 | #endif |
| 85 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 86 | static int cmdline_charsize(int idx); |
| 87 | static void set_cmdspos(void); |
| 88 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 90 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 92 | static void alloc_cmdbuff(int len); |
| 93 | static int realloc_cmdbuff(int len); |
| 94 | static void draw_cmdline(int start, int len); |
| 95 | static void save_cmdline(struct cmdline_info *ccp); |
| 96 | static void restore_cmdline(struct cmdline_info *ccp); |
| 97 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 98 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 99 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | #endif |
| 101 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 102 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 104 | static void redrawcmdprompt(void); |
| 105 | static void cursorcmd(void); |
| 106 | static int ccheck_abbr(int); |
| 107 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 108 | static void escape_fname(char_u **pp); |
| 109 | static int showmatches(expand_T *xp, int wildmenu); |
| 110 | static void set_expand_context(expand_T *xp); |
| 111 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 112 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 114 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 115 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 116 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 117 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 119 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 121 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 122 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | # endif |
| 124 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 125 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 127 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 130 | static int ex_window(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 133 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 134 | static int |
| 135 | #ifdef __BORLANDC__ |
| 136 | _RTLENTRYF |
| 137 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 138 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 139 | #endif |
| 140 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | /* |
| 142 | * getcmdline() - accept a command line starting with firstc. |
| 143 | * |
| 144 | * firstc == ':' get ":" command line. |
| 145 | * firstc == '/' or '?' get search pattern |
| 146 | * firstc == '=' get expression |
| 147 | * firstc == '@' get text for input() function |
| 148 | * firstc == '>' get text for debug mode |
| 149 | * firstc == NUL get text for :insert command |
| 150 | * firstc == -1 like NUL, and break on CTRL-C |
| 151 | * |
| 152 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 153 | * command line. |
| 154 | * |
| 155 | * Careful: getcmdline() can be called recursively! |
| 156 | * |
| 157 | * Return pointer to allocated string if there is a commandline, NULL |
| 158 | * otherwise. |
| 159 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 161 | getcmdline( |
| 162 | int firstc, |
| 163 | long count UNUSED, /* only used for incremental search */ |
| 164 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | { |
| 166 | int c; |
| 167 | int i; |
| 168 | int j; |
| 169 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 170 | int do_abbr; /* when TRUE check for abbr. */ |
| 171 | #ifdef FEAT_CMDHIST |
| 172 | char_u *lookfor = NULL; /* string to match */ |
| 173 | int hiscnt; /* current history line in use */ |
| 174 | int histype; /* history type to be used */ |
| 175 | #endif |
| 176 | #ifdef FEAT_SEARCH_EXTRA |
| 177 | pos_T old_cursor; |
| 178 | colnr_T old_curswant; |
| 179 | colnr_T old_leftcol; |
| 180 | linenr_T old_topline; |
| 181 | # ifdef FEAT_DIFF |
| 182 | int old_topfill; |
| 183 | # endif |
| 184 | linenr_T old_botline; |
| 185 | int did_incsearch = FALSE; |
| 186 | int incsearch_postponed = FALSE; |
| 187 | #endif |
| 188 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 189 | int wim_index = 0; /* index in wim_flags[] */ |
| 190 | int res; |
| 191 | int save_msg_scroll = msg_scroll; |
| 192 | int save_State = State; /* remember State when called */ |
| 193 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 194 | #ifdef FEAT_MOUSE |
| 195 | /* mouse drag and release events are ignored, unless they are |
| 196 | * preceded with a mouse down event */ |
| 197 | int ignore_drag_release = TRUE; |
| 198 | #endif |
| 199 | #ifdef FEAT_EVAL |
| 200 | int break_ctrl_c = FALSE; |
| 201 | #endif |
| 202 | expand_T xpc; |
| 203 | long *b_im_ptr = NULL; |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 204 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
| 205 | /* Everything that may work recursively should save and restore the |
| 206 | * current command line in save_ccline. That includes update_screen(), a |
| 207 | * custom status line may invoke ":normal". */ |
| 208 | struct cmdline_info save_ccline; |
| 209 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 210 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 211 | #ifdef FEAT_EVAL |
| 212 | if (firstc == -1) |
| 213 | { |
| 214 | firstc = NUL; |
| 215 | break_ctrl_c = TRUE; |
| 216 | } |
| 217 | #endif |
| 218 | #ifdef FEAT_RIGHTLEFT |
| 219 | /* start without Hebrew mapping for a command line */ |
| 220 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 221 | cmd_hkmap = 0; |
| 222 | #endif |
| 223 | |
| 224 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 225 | #ifdef FEAT_SEARCH_EXTRA |
| 226 | old_cursor = curwin->w_cursor; /* needs to be restored later */ |
| 227 | old_curswant = curwin->w_curswant; |
| 228 | old_leftcol = curwin->w_leftcol; |
| 229 | old_topline = curwin->w_topline; |
| 230 | # ifdef FEAT_DIFF |
| 231 | old_topfill = curwin->w_topfill; |
| 232 | # endif |
| 233 | old_botline = curwin->w_botline; |
| 234 | #endif |
| 235 | |
| 236 | /* |
| 237 | * set some variables for redrawcmd() |
| 238 | */ |
| 239 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 240 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 241 | |
| 242 | /* alloc initial ccline.cmdbuff */ |
| 243 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 244 | if (ccline.cmdbuff == NULL) |
| 245 | return NULL; /* out of memory */ |
| 246 | ccline.cmdlen = ccline.cmdpos = 0; |
| 247 | ccline.cmdbuff[0] = NUL; |
| 248 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 249 | /* autoindent for :insert and :append */ |
| 250 | if (firstc <= 0) |
| 251 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 252 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 253 | ccline.cmdbuff[indent] = NUL; |
| 254 | ccline.cmdpos = indent; |
| 255 | ccline.cmdspos = indent; |
| 256 | ccline.cmdlen = indent; |
| 257 | } |
| 258 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 259 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 260 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | |
| 262 | #ifdef FEAT_RIGHTLEFT |
| 263 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 264 | && (firstc == '/' || firstc == '?')) |
| 265 | cmdmsg_rl = TRUE; |
| 266 | else |
| 267 | cmdmsg_rl = FALSE; |
| 268 | #endif |
| 269 | |
| 270 | redir_off = TRUE; /* don't redirect the typed command */ |
| 271 | if (!cmd_silent) |
| 272 | { |
| 273 | i = msg_scrolled; |
| 274 | msg_scrolled = 0; /* avoid wait_return message */ |
| 275 | gotocmdline(TRUE); |
| 276 | msg_scrolled += i; |
| 277 | redrawcmdprompt(); /* draw prompt or indent */ |
| 278 | set_cmdspos(); |
| 279 | } |
| 280 | xpc.xp_context = EXPAND_NOTHING; |
| 281 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 282 | #ifndef BACKSLASH_IN_FILENAME |
| 283 | xpc.xp_shell = FALSE; |
| 284 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 286 | #if defined(FEAT_EVAL) |
| 287 | if (ccline.input_fn) |
| 288 | { |
| 289 | xpc.xp_context = ccline.xp_context; |
| 290 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 291 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 292 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 293 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 294 | } |
| 295 | #endif |
| 296 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 297 | /* |
| 298 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 299 | * doing ":@0" when register 0 doesn't contain a CR. |
| 300 | */ |
| 301 | msg_scroll = FALSE; |
| 302 | |
| 303 | State = CMDLINE; |
| 304 | |
| 305 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 306 | { |
| 307 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 308 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 309 | b_im_ptr = &curbuf->b_p_iminsert; |
| 310 | else |
| 311 | b_im_ptr = &curbuf->b_p_imsearch; |
| 312 | if (*b_im_ptr == B_IMODE_LMAP) |
| 313 | State |= LANGMAP; |
| 314 | #ifdef USE_IM_CONTROL |
| 315 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 316 | #endif |
| 317 | } |
| 318 | #ifdef USE_IM_CONTROL |
| 319 | else if (p_imcmdline) |
| 320 | im_set_active(TRUE); |
| 321 | #endif |
| 322 | |
| 323 | #ifdef FEAT_MOUSE |
| 324 | setmouse(); |
| 325 | #endif |
| 326 | #ifdef CURSOR_SHAPE |
| 327 | ui_cursor_shape(); /* may show different cursor shape */ |
| 328 | #endif |
| 329 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 330 | /* When inside an autocommand for writing "exiting" may be set and |
| 331 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 332 | settmode(TMODE_RAW); |
| 333 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 334 | #ifdef FEAT_CMDHIST |
| 335 | init_history(); |
| 336 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 337 | histype = hist_char2type(firstc); |
| 338 | #endif |
| 339 | |
| 340 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 341 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 342 | #endif |
| 343 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 344 | /* If something above caused an error, reset the flags, we do want to type |
| 345 | * and execute commands. Display may be messed up a bit. */ |
| 346 | if (did_emsg) |
| 347 | redrawcmd(); |
| 348 | did_emsg = FALSE; |
| 349 | got_int = FALSE; |
| 350 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | /* |
| 352 | * Collect the command string, handling editing keys. |
| 353 | */ |
| 354 | for (;;) |
| 355 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 356 | redir_off = TRUE; /* Don't redirect the typed command. |
| 357 | Repeated, because a ":redir" inside |
| 358 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | #ifdef USE_ON_FLY_SCROLL |
| 360 | dont_scroll = FALSE; /* allow scrolling here */ |
| 361 | #endif |
| 362 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 363 | |
| 364 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 365 | |
| 366 | /* Get a character. Ignore K_IGNORE, it should not do anything, such |
| 367 | * as stop completion. */ |
| 368 | do |
| 369 | { |
| 370 | c = safe_vgetc(); |
| 371 | } while (c == K_IGNORE); |
| 372 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 373 | if (KeyTyped) |
| 374 | { |
| 375 | some_key_typed = TRUE; |
| 376 | #ifdef FEAT_RIGHTLEFT |
| 377 | if (cmd_hkmap) |
| 378 | c = hkmap(c); |
| 379 | # ifdef FEAT_FKMAP |
| 380 | if (cmd_fkmap) |
| 381 | c = cmdl_fkmap(c); |
| 382 | # endif |
| 383 | if (cmdmsg_rl && !KeyStuffed) |
| 384 | { |
| 385 | /* Invert horizontal movements and operations. Only when |
| 386 | * typed by the user directly, not when the result of a |
| 387 | * mapping. */ |
| 388 | switch (c) |
| 389 | { |
| 390 | case K_RIGHT: c = K_LEFT; break; |
| 391 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 392 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 393 | case K_LEFT: c = K_RIGHT; break; |
| 394 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 395 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 396 | } |
| 397 | } |
| 398 | #endif |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Ignore got_int when CTRL-C was typed here. |
| 403 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 404 | * ":g/pat/normal /pat" (without the <CR>). |
| 405 | * Don't ignore it for the input() function. |
| 406 | */ |
| 407 | if ((c == Ctrl_C |
| 408 | #ifdef UNIX |
| 409 | || c == intr_char |
| 410 | #endif |
| 411 | ) |
| 412 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 413 | && firstc != '@' |
| 414 | #endif |
| 415 | #ifdef FEAT_EVAL |
| 416 | && !break_ctrl_c |
| 417 | #endif |
| 418 | && !global_busy) |
| 419 | got_int = FALSE; |
| 420 | |
| 421 | #ifdef FEAT_CMDHIST |
| 422 | /* free old command line when finished moving around in the history |
| 423 | * list */ |
| 424 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 425 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 426 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 427 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 428 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 429 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 431 | { |
| 432 | vim_free(lookfor); |
| 433 | lookfor = NULL; |
| 434 | } |
| 435 | #endif |
| 436 | |
| 437 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 438 | * When there are matching completions to select <S-Tab> works like |
| 439 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 440 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 441 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 442 | c = Ctrl_P; |
| 443 | |
| 444 | #ifdef FEAT_WILDMENU |
| 445 | /* Special translations for 'wildmenu' */ |
| 446 | if (did_wild_list && p_wmnu) |
| 447 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 448 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 450 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | c = Ctrl_N; |
| 452 | } |
| 453 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 454 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 455 | && ccline.cmdpos > 1 |
| 456 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 457 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 458 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 459 | c = K_DOWN; |
| 460 | #endif |
| 461 | |
| 462 | /* free expanded names when finished walking through matches */ |
| 463 | if (xpc.xp_numfiles != -1 |
| 464 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 465 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 466 | && c != Ctrl_L) |
| 467 | { |
| 468 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 469 | did_wild_list = FALSE; |
| 470 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 471 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 472 | #endif |
| 473 | xpc.xp_context = EXPAND_NOTHING; |
| 474 | wim_index = 0; |
| 475 | #ifdef FEAT_WILDMENU |
| 476 | if (p_wmnu && wild_menu_showing != 0) |
| 477 | { |
| 478 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 479 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 480 | |
| 481 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 482 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 483 | |
| 484 | if (wild_menu_showing == WM_SCROLLED) |
| 485 | { |
| 486 | /* Entered command line, move it up */ |
| 487 | cmdline_row--; |
| 488 | redrawcmd(); |
| 489 | } |
| 490 | else if (save_p_ls != -1) |
| 491 | { |
| 492 | /* restore 'laststatus' and 'winminheight' */ |
| 493 | p_ls = save_p_ls; |
| 494 | p_wmh = save_p_wmh; |
| 495 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 496 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 498 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | redrawcmd(); |
| 500 | save_p_ls = -1; |
| 501 | } |
| 502 | else |
| 503 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 504 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | redraw_statuslines(); |
| 506 | } |
| 507 | KeyTyped = skt; |
| 508 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 509 | if (ccline.input_fn) |
| 510 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | } |
| 512 | #endif |
| 513 | } |
| 514 | |
| 515 | #ifdef FEAT_WILDMENU |
| 516 | /* Special translations for 'wildmenu' */ |
| 517 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 518 | { |
| 519 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 520 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 521 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 523 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | { |
| 525 | /* Hitting <Up>: Remove one submenu name in front of the |
| 526 | * cursor */ |
| 527 | int found = FALSE; |
| 528 | |
| 529 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 530 | i = 0; |
| 531 | while (--j > 0) |
| 532 | { |
| 533 | /* check for start of menu name */ |
| 534 | if (ccline.cmdbuff[j] == ' ' |
| 535 | && ccline.cmdbuff[j - 1] != '\\') |
| 536 | { |
| 537 | i = j + 1; |
| 538 | break; |
| 539 | } |
| 540 | /* check for start of submenu name */ |
| 541 | if (ccline.cmdbuff[j] == '.' |
| 542 | && ccline.cmdbuff[j - 1] != '\\') |
| 543 | { |
| 544 | if (found) |
| 545 | { |
| 546 | i = j + 1; |
| 547 | break; |
| 548 | } |
| 549 | else |
| 550 | found = TRUE; |
| 551 | } |
| 552 | } |
| 553 | if (i > 0) |
| 554 | cmdline_del(i); |
| 555 | c = p_wc; |
| 556 | xpc.xp_context = EXPAND_NOTHING; |
| 557 | } |
| 558 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 559 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 560 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 561 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 562 | { |
| 563 | char_u upseg[5]; |
| 564 | |
| 565 | upseg[0] = PATHSEP; |
| 566 | upseg[1] = '.'; |
| 567 | upseg[2] = '.'; |
| 568 | upseg[3] = PATHSEP; |
| 569 | upseg[4] = NUL; |
| 570 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 571 | if (c == K_DOWN |
| 572 | && ccline.cmdpos > 0 |
| 573 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 574 | && (ccline.cmdpos < 3 |
| 575 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 577 | { |
| 578 | /* go down a directory */ |
| 579 | c = p_wc; |
| 580 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 581 | else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 582 | { |
| 583 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 584 | int found = FALSE; |
| 585 | |
| 586 | j = ccline.cmdpos; |
| 587 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 588 | while (--j > i) |
| 589 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 590 | #ifdef FEAT_MBYTE |
| 591 | if (has_mbyte) |
| 592 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 593 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 595 | { |
| 596 | found = TRUE; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | if (found |
| 601 | && ccline.cmdbuff[j - 1] == '.' |
| 602 | && ccline.cmdbuff[j - 2] == '.' |
| 603 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 604 | { |
| 605 | cmdline_del(j - 2); |
| 606 | c = p_wc; |
| 607 | } |
| 608 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 609 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 610 | { |
| 611 | /* go up a directory */ |
| 612 | int found = FALSE; |
| 613 | |
| 614 | j = ccline.cmdpos - 1; |
| 615 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 616 | while (--j > i) |
| 617 | { |
| 618 | #ifdef FEAT_MBYTE |
| 619 | if (has_mbyte) |
| 620 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 621 | #endif |
| 622 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 623 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 624 | && vim_strchr((char_u *)" *?[{`$%#", |
| 625 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | #endif |
| 627 | ) |
| 628 | { |
| 629 | if (found) |
| 630 | { |
| 631 | i = j + 1; |
| 632 | break; |
| 633 | } |
| 634 | else |
| 635 | found = TRUE; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (!found) |
| 640 | j = i; |
| 641 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 642 | j += 4; |
| 643 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 644 | && j == i) |
| 645 | j += 3; |
| 646 | else |
| 647 | j = 0; |
| 648 | if (j > 0) |
| 649 | { |
| 650 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 651 | * machine-specific stuff here and in upseg init */ |
| 652 | cmdline_del(j); |
| 653 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 654 | } |
| 655 | else if (ccline.cmdpos > i) |
| 656 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 657 | |
| 658 | /* Now complete in the new directory. Set KeyTyped in case the |
| 659 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 661 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | |
| 665 | #endif /* FEAT_WILDMENU */ |
| 666 | |
| 667 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 668 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 669 | if (c == Ctrl_BSL) |
| 670 | { |
| 671 | ++no_mapping; |
| 672 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 673 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 674 | --no_mapping; |
| 675 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 676 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 677 | * is in a mapping. */ |
| 678 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 679 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 680 | { |
| 681 | vungetc(c); |
| 682 | c = Ctrl_BSL; |
| 683 | } |
| 684 | #ifdef FEAT_EVAL |
| 685 | else if (c == 'e') |
| 686 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 687 | char_u *p = NULL; |
| 688 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 689 | |
| 690 | /* |
| 691 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 692 | * Need to save and restore the current command line, to be |
| 693 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | */ |
| 695 | if (ccline.cmdpos == ccline.cmdlen) |
| 696 | new_cmdpos = 99999; /* keep it at the end */ |
| 697 | else |
| 698 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 699 | |
| 700 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 701 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 702 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 703 | if (c == '=') |
| 704 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 705 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 706 | * to avoid nasty things like going to another buffer when |
| 707 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 708 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 709 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 711 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 712 | restore_cmdline(&save_ccline); |
| 713 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 714 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 715 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 716 | len = (int)STRLEN(p); |
| 717 | if (realloc_cmdbuff(len + 1) == OK) |
| 718 | { |
| 719 | ccline.cmdlen = len; |
| 720 | STRCPY(ccline.cmdbuff, p); |
| 721 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 722 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 723 | /* Restore the cursor or use the position set with |
| 724 | * set_cmdline_pos(). */ |
| 725 | if (new_cmdpos > ccline.cmdlen) |
| 726 | ccline.cmdpos = ccline.cmdlen; |
| 727 | else |
| 728 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 729 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 730 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 731 | redrawcmd(); |
| 732 | goto cmdline_changed; |
| 733 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 737 | got_int = FALSE; /* don't abandon the command line */ |
| 738 | did_emsg = FALSE; |
| 739 | emsg_on_display = FALSE; |
| 740 | redrawcmd(); |
| 741 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | } |
| 743 | #endif |
| 744 | else |
| 745 | { |
| 746 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 747 | restart_edit = 'a'; |
| 748 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 749 | in history */ |
| 750 | goto returncmd; /* back to Normal mode */ |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | #ifdef FEAT_CMDWIN |
| 755 | if (c == cedit_key || c == K_CMDWIN) |
| 756 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 757 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 758 | { |
| 759 | /* |
| 760 | * Open a window to edit the command line (and history). |
| 761 | */ |
| 762 | c = ex_window(); |
| 763 | some_key_typed = TRUE; |
| 764 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 765 | } |
| 766 | # ifdef FEAT_DIGRAPHS |
| 767 | else |
| 768 | # endif |
| 769 | #endif |
| 770 | #ifdef FEAT_DIGRAPHS |
| 771 | c = do_digraph(c); |
| 772 | #endif |
| 773 | |
| 774 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 775 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 776 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 777 | /* In Ex mode a backslash escapes a newline. */ |
| 778 | if (exmode_active |
| 779 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 780 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 781 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 782 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 783 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 784 | if (c == K_KENTER) |
| 785 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 786 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 787 | else |
| 788 | { |
| 789 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 790 | truncate the cmdline now. */ |
| 791 | if (ccheck_abbr(c + ABBR_OFF)) |
| 792 | goto cmdline_changed; |
| 793 | if (!cmd_silent) |
| 794 | { |
| 795 | windgoto(msg_row, 0); |
| 796 | out_flush(); |
| 797 | } |
| 798 | break; |
| 799 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Completion for 'wildchar' or 'wildcharm' key. |
| 804 | * - hitting <ESC> twice means: abandon command line. |
| 805 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 806 | * typed, not when it comes from a macro |
| 807 | */ |
| 808 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 809 | { |
| 810 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 811 | { |
| 812 | /* if 'wildmode' contains "list" may still need to list */ |
| 813 | if (xpc.xp_numfiles > 1 |
| 814 | && !did_wild_list |
| 815 | && (wim_flags[wim_index] & WIM_LIST)) |
| 816 | { |
| 817 | (void)showmatches(&xpc, FALSE); |
| 818 | redrawcmd(); |
| 819 | did_wild_list = TRUE; |
| 820 | } |
| 821 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 822 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 823 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 825 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 826 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | else |
| 828 | res = OK; /* don't insert 'wildchar' now */ |
| 829 | } |
| 830 | else /* typed p_wc first time */ |
| 831 | { |
| 832 | wim_index = 0; |
| 833 | j = ccline.cmdpos; |
| 834 | /* if 'wildmode' first contains "longest", get longest |
| 835 | * common part */ |
| 836 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 837 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 838 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 839 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 840 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 841 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | |
| 843 | /* if interrupted while completing, behave like it failed */ |
| 844 | if (got_int) |
| 845 | { |
| 846 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 847 | got_int = FALSE; /* don't abandon the command line */ |
| 848 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 849 | #ifdef FEAT_WILDMENU |
| 850 | xpc.xp_context = EXPAND_NOTHING; |
| 851 | #endif |
| 852 | goto cmdline_changed; |
| 853 | } |
| 854 | |
| 855 | /* when more than one match, and 'wildmode' first contains |
| 856 | * "list", or no change and 'wildmode' contains "longest,list", |
| 857 | * list all matches */ |
| 858 | if (res == OK && xpc.xp_numfiles > 1) |
| 859 | { |
| 860 | /* a "longest" that didn't do anything is skipped (but not |
| 861 | * "list:longest") */ |
| 862 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 863 | wim_index = 1; |
| 864 | if ((wim_flags[wim_index] & WIM_LIST) |
| 865 | #ifdef FEAT_WILDMENU |
| 866 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 867 | #endif |
| 868 | ) |
| 869 | { |
| 870 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 871 | { |
| 872 | #ifdef FEAT_WILDMENU |
| 873 | int p_wmnu_save = p_wmnu; |
| 874 | p_wmnu = 0; |
| 875 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 876 | /* remove match */ |
| 877 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 878 | #ifdef FEAT_WILDMENU |
| 879 | p_wmnu = p_wmnu_save; |
| 880 | #endif |
| 881 | } |
| 882 | #ifdef FEAT_WILDMENU |
| 883 | (void)showmatches(&xpc, p_wmnu |
| 884 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 885 | #else |
| 886 | (void)showmatches(&xpc, FALSE); |
| 887 | #endif |
| 888 | redrawcmd(); |
| 889 | did_wild_list = TRUE; |
| 890 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 891 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 892 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 893 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 894 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 895 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 896 | } |
| 897 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 898 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 899 | } |
| 900 | #ifdef FEAT_WILDMENU |
| 901 | else if (xpc.xp_numfiles == -1) |
| 902 | xpc.xp_context = EXPAND_NOTHING; |
| 903 | #endif |
| 904 | } |
| 905 | if (wim_index < 3) |
| 906 | ++wim_index; |
| 907 | if (c == ESC) |
| 908 | gotesc = TRUE; |
| 909 | if (res == OK) |
| 910 | goto cmdline_changed; |
| 911 | } |
| 912 | |
| 913 | gotesc = FALSE; |
| 914 | |
| 915 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 916 | if (c == K_S_TAB && KeyTyped) |
| 917 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 918 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 919 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 920 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | goto cmdline_changed; |
| 922 | } |
| 923 | |
| 924 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 925 | c = NL; |
| 926 | |
| 927 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 928 | |
| 929 | /* |
| 930 | * Big switch for a typed command line character. |
| 931 | */ |
| 932 | switch (c) |
| 933 | { |
| 934 | case K_BS: |
| 935 | case Ctrl_H: |
| 936 | case K_DEL: |
| 937 | case K_KDEL: |
| 938 | case Ctrl_W: |
| 939 | #ifdef FEAT_FKMAP |
| 940 | if (cmd_fkmap && c == K_BS) |
| 941 | c = K_DEL; |
| 942 | #endif |
| 943 | if (c == K_KDEL) |
| 944 | c = K_DEL; |
| 945 | |
| 946 | /* |
| 947 | * delete current character is the same as backspace on next |
| 948 | * character, except at end of line |
| 949 | */ |
| 950 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 951 | ++ccline.cmdpos; |
| 952 | #ifdef FEAT_MBYTE |
| 953 | if (has_mbyte && c == K_DEL) |
| 954 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 955 | ccline.cmdbuff + ccline.cmdpos); |
| 956 | #endif |
| 957 | if (ccline.cmdpos > 0) |
| 958 | { |
| 959 | char_u *p; |
| 960 | |
| 961 | j = ccline.cmdpos; |
| 962 | p = ccline.cmdbuff + j; |
| 963 | #ifdef FEAT_MBYTE |
| 964 | if (has_mbyte) |
| 965 | { |
| 966 | p = mb_prevptr(ccline.cmdbuff, p); |
| 967 | if (c == Ctrl_W) |
| 968 | { |
| 969 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 970 | p = mb_prevptr(ccline.cmdbuff, p); |
| 971 | i = mb_get_class(p); |
| 972 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 973 | p = mb_prevptr(ccline.cmdbuff, p); |
| 974 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 975 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | else |
| 979 | #endif |
| 980 | if (c == Ctrl_W) |
| 981 | { |
| 982 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 983 | --p; |
| 984 | i = vim_iswordc(p[-1]); |
| 985 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 986 | && vim_iswordc(p[-1]) == i) |
| 987 | --p; |
| 988 | } |
| 989 | else |
| 990 | --p; |
| 991 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 992 | ccline.cmdlen -= j - ccline.cmdpos; |
| 993 | i = ccline.cmdpos; |
| 994 | while (i < ccline.cmdlen) |
| 995 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 996 | |
| 997 | /* Truncate at the end, required for multi-byte chars. */ |
| 998 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 999 | redrawcmd(); |
| 1000 | } |
| 1001 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1002 | && ccline.cmdprompt == NULL && indent == 0) |
| 1003 | { |
| 1004 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1005 | if (exmode_active |
| 1006 | #ifdef FEAT_EVAL |
| 1007 | || ccline.cmdfirstc == '>' |
| 1008 | #endif |
| 1009 | ) |
| 1010 | goto cmdline_not_changed; |
| 1011 | |
| 1012 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 1013 | ccline.cmdbuff = NULL; |
| 1014 | if (!cmd_silent) |
| 1015 | { |
| 1016 | #ifdef FEAT_RIGHTLEFT |
| 1017 | if (cmdmsg_rl) |
| 1018 | msg_col = Columns; |
| 1019 | else |
| 1020 | #endif |
| 1021 | msg_col = 0; |
| 1022 | msg_putchar(' '); /* delete ':' */ |
| 1023 | } |
| 1024 | redraw_cmdline = TRUE; |
| 1025 | goto returncmd; /* back to cmd mode */ |
| 1026 | } |
| 1027 | goto cmdline_changed; |
| 1028 | |
| 1029 | case K_INS: |
| 1030 | case K_KINS: |
| 1031 | #ifdef FEAT_FKMAP |
| 1032 | /* if Farsi mode set, we are in reverse insert mode - |
| 1033 | Do not change the mode */ |
| 1034 | if (cmd_fkmap) |
| 1035 | beep_flush(); |
| 1036 | else |
| 1037 | #endif |
| 1038 | ccline.overstrike = !ccline.overstrike; |
| 1039 | #ifdef CURSOR_SHAPE |
| 1040 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1041 | #endif |
| 1042 | goto cmdline_not_changed; |
| 1043 | |
| 1044 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1045 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1046 | { |
| 1047 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1048 | State ^= LANGMAP; |
| 1049 | #ifdef USE_IM_CONTROL |
| 1050 | im_set_active(FALSE); /* Disable input method */ |
| 1051 | #endif |
| 1052 | if (b_im_ptr != NULL) |
| 1053 | { |
| 1054 | if (State & LANGMAP) |
| 1055 | *b_im_ptr = B_IMODE_LMAP; |
| 1056 | else |
| 1057 | *b_im_ptr = B_IMODE_NONE; |
| 1058 | } |
| 1059 | } |
| 1060 | #ifdef USE_IM_CONTROL |
| 1061 | else |
| 1062 | { |
| 1063 | /* There are no ":lmap" mappings, toggle IM. When |
| 1064 | * 'imdisable' is set don't try getting the status, it's |
| 1065 | * always off. */ |
| 1066 | if ((p_imdisable && b_im_ptr != NULL) |
| 1067 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1068 | { |
| 1069 | im_set_active(FALSE); /* Disable input method */ |
| 1070 | if (b_im_ptr != NULL) |
| 1071 | *b_im_ptr = B_IMODE_NONE; |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | im_set_active(TRUE); /* Enable input method */ |
| 1076 | if (b_im_ptr != NULL) |
| 1077 | *b_im_ptr = B_IMODE_IM; |
| 1078 | } |
| 1079 | } |
| 1080 | #endif |
| 1081 | if (b_im_ptr != NULL) |
| 1082 | { |
| 1083 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1084 | set_iminsert_global(); |
| 1085 | else |
| 1086 | set_imsearch_global(); |
| 1087 | } |
| 1088 | #ifdef CURSOR_SHAPE |
| 1089 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1090 | #endif |
| 1091 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 1092 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1093 | status_redraw_curbuf(); |
| 1094 | #endif |
| 1095 | goto cmdline_not_changed; |
| 1096 | |
| 1097 | /* case '@': only in very old vi */ |
| 1098 | case Ctrl_U: |
| 1099 | /* delete all characters left of the cursor */ |
| 1100 | j = ccline.cmdpos; |
| 1101 | ccline.cmdlen -= j; |
| 1102 | i = ccline.cmdpos = 0; |
| 1103 | while (i < ccline.cmdlen) |
| 1104 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1105 | /* Truncate at the end, required for multi-byte chars. */ |
| 1106 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 1107 | redrawcmd(); |
| 1108 | goto cmdline_changed; |
| 1109 | |
| 1110 | #ifdef FEAT_CLIPBOARD |
| 1111 | case Ctrl_Y: |
| 1112 | /* Copy the modeless selection, if there is one. */ |
| 1113 | if (clip_star.state != SELECT_CLEARED) |
| 1114 | { |
| 1115 | if (clip_star.state == SELECT_DONE) |
| 1116 | clip_copy_modeless_selection(TRUE); |
| 1117 | goto cmdline_not_changed; |
| 1118 | } |
| 1119 | break; |
| 1120 | #endif |
| 1121 | |
| 1122 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1123 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1124 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1125 | * ":normal" runs out of characters. */ |
| 1126 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1127 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1128 | goto cmdline_not_changed; |
| 1129 | |
| 1130 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1131 | putting it in history */ |
| 1132 | goto returncmd; /* back to cmd mode */ |
| 1133 | |
| 1134 | case Ctrl_R: /* insert register */ |
| 1135 | #ifdef USE_ON_FLY_SCROLL |
| 1136 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1137 | #endif |
| 1138 | putcmdline('"', TRUE); |
| 1139 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1140 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | if (i == Ctrl_O) |
| 1142 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1143 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1144 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1145 | --no_mapping; |
| 1146 | #ifdef FEAT_EVAL |
| 1147 | /* |
| 1148 | * Insert the result of an expression. |
| 1149 | * Need to save the current command line, to be able to enter |
| 1150 | * a new one... |
| 1151 | */ |
| 1152 | new_cmdpos = -1; |
| 1153 | if (c == '=') |
| 1154 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1155 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1156 | { |
| 1157 | beep_flush(); |
| 1158 | c = ESC; |
| 1159 | } |
| 1160 | else |
| 1161 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1162 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1163 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1164 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | #endif |
| 1168 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1169 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1170 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1171 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1172 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1173 | /* When there was a serious error abort getting the |
| 1174 | * command line. */ |
| 1175 | if (aborting()) |
| 1176 | { |
| 1177 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1178 | putting it in history */ |
| 1179 | goto returncmd; /* back to cmd mode */ |
| 1180 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1181 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1182 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1183 | #ifdef FEAT_EVAL |
| 1184 | if (new_cmdpos >= 0) |
| 1185 | { |
| 1186 | /* set_cmdline_pos() was used */ |
| 1187 | if (new_cmdpos > ccline.cmdlen) |
| 1188 | ccline.cmdpos = ccline.cmdlen; |
| 1189 | else |
| 1190 | ccline.cmdpos = new_cmdpos; |
| 1191 | } |
| 1192 | #endif |
| 1193 | } |
| 1194 | redrawcmd(); |
| 1195 | goto cmdline_changed; |
| 1196 | |
| 1197 | case Ctrl_D: |
| 1198 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1199 | break; /* Use ^D as normal char instead */ |
| 1200 | |
| 1201 | redrawcmd(); |
| 1202 | continue; /* don't do incremental search now */ |
| 1203 | |
| 1204 | case K_RIGHT: |
| 1205 | case K_S_RIGHT: |
| 1206 | case K_C_RIGHT: |
| 1207 | do |
| 1208 | { |
| 1209 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1210 | break; |
| 1211 | i = cmdline_charsize(ccline.cmdpos); |
| 1212 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1213 | break; |
| 1214 | ccline.cmdspos += i; |
| 1215 | #ifdef FEAT_MBYTE |
| 1216 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1217 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | + ccline.cmdpos); |
| 1219 | else |
| 1220 | #endif |
| 1221 | ++ccline.cmdpos; |
| 1222 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1223 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1224 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1225 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1226 | #ifdef FEAT_MBYTE |
| 1227 | if (has_mbyte) |
| 1228 | set_cmdspos_cursor(); |
| 1229 | #endif |
| 1230 | goto cmdline_not_changed; |
| 1231 | |
| 1232 | case K_LEFT: |
| 1233 | case K_S_LEFT: |
| 1234 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1235 | if (ccline.cmdpos == 0) |
| 1236 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | do |
| 1238 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | --ccline.cmdpos; |
| 1240 | #ifdef FEAT_MBYTE |
| 1241 | if (has_mbyte) /* move to first byte of char */ |
| 1242 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1243 | ccline.cmdbuff + ccline.cmdpos); |
| 1244 | #endif |
| 1245 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1246 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1247 | while (ccline.cmdpos > 0 |
| 1248 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1249 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1251 | #ifdef FEAT_MBYTE |
| 1252 | if (has_mbyte) |
| 1253 | set_cmdspos_cursor(); |
| 1254 | #endif |
| 1255 | goto cmdline_not_changed; |
| 1256 | |
| 1257 | case K_IGNORE: |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1258 | /* Ignore mouse event or ex_window() result. */ |
| 1259 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1261 | #ifdef FEAT_GUI_W32 |
| 1262 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1263 | * cancelled. */ |
| 1264 | case K_F4: |
| 1265 | if (mod_mask == MOD_MASK_ALT) |
| 1266 | { |
| 1267 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1268 | goto cmdline_not_changed; |
| 1269 | } |
| 1270 | break; |
| 1271 | #endif |
| 1272 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | #ifdef FEAT_MOUSE |
| 1274 | case K_MIDDLEDRAG: |
| 1275 | case K_MIDDLERELEASE: |
| 1276 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1277 | |
| 1278 | case K_MIDDLEMOUSE: |
| 1279 | # ifdef FEAT_GUI |
| 1280 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1281 | if (!gui.in_use) |
| 1282 | # endif |
| 1283 | if (!mouse_has(MOUSE_COMMAND)) |
| 1284 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1285 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1287 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1288 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1289 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1290 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1291 | redrawcmd(); |
| 1292 | goto cmdline_changed; |
| 1293 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1294 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1295 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1296 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | redrawcmd(); |
| 1298 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1299 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1300 | |
| 1301 | case K_LEFTDRAG: |
| 1302 | case K_LEFTRELEASE: |
| 1303 | case K_RIGHTDRAG: |
| 1304 | case K_RIGHTRELEASE: |
| 1305 | /* Ignore drag and release events when the button-down wasn't |
| 1306 | * seen before. */ |
| 1307 | if (ignore_drag_release) |
| 1308 | goto cmdline_not_changed; |
| 1309 | /* FALLTHROUGH */ |
| 1310 | case K_LEFTMOUSE: |
| 1311 | case K_RIGHTMOUSE: |
| 1312 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1313 | ignore_drag_release = TRUE; |
| 1314 | else |
| 1315 | ignore_drag_release = FALSE; |
| 1316 | # ifdef FEAT_GUI |
| 1317 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1318 | if (!gui.in_use) |
| 1319 | # endif |
| 1320 | if (!mouse_has(MOUSE_COMMAND)) |
| 1321 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1322 | # ifdef FEAT_CLIPBOARD |
| 1323 | if (mouse_row < cmdline_row && clip_star.available) |
| 1324 | { |
| 1325 | int button, is_click, is_drag; |
| 1326 | |
| 1327 | /* |
| 1328 | * Handle modeless selection. |
| 1329 | */ |
| 1330 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1331 | &is_click, &is_drag); |
| 1332 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1333 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1334 | { |
| 1335 | /* Translate shift-left to right button. */ |
| 1336 | button = MOUSE_RIGHT; |
| 1337 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1338 | } |
| 1339 | clip_modeless(button, is_click, is_drag); |
| 1340 | goto cmdline_not_changed; |
| 1341 | } |
| 1342 | # endif |
| 1343 | |
| 1344 | set_cmdspos(); |
| 1345 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1346 | ++ccline.cmdpos) |
| 1347 | { |
| 1348 | i = cmdline_charsize(ccline.cmdpos); |
| 1349 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1350 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1351 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1352 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1353 | if (has_mbyte) |
| 1354 | { |
| 1355 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1356 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1357 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1358 | + ccline.cmdpos) - 1; |
| 1359 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1360 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | ccline.cmdspos += i; |
| 1362 | } |
| 1363 | goto cmdline_not_changed; |
| 1364 | |
| 1365 | /* Mouse scroll wheel: ignored here */ |
| 1366 | case K_MOUSEDOWN: |
| 1367 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1368 | case K_MOUSELEFT: |
| 1369 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1370 | /* Alternate buttons ignored here */ |
| 1371 | case K_X1MOUSE: |
| 1372 | case K_X1DRAG: |
| 1373 | case K_X1RELEASE: |
| 1374 | case K_X2MOUSE: |
| 1375 | case K_X2DRAG: |
| 1376 | case K_X2RELEASE: |
| 1377 | goto cmdline_not_changed; |
| 1378 | |
| 1379 | #endif /* FEAT_MOUSE */ |
| 1380 | |
| 1381 | #ifdef FEAT_GUI |
| 1382 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1383 | case K_LEFTRELEASE_NM: |
| 1384 | goto cmdline_not_changed; |
| 1385 | |
| 1386 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1387 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | { |
| 1389 | gui_do_scroll(); |
| 1390 | redrawcmd(); |
| 1391 | } |
| 1392 | goto cmdline_not_changed; |
| 1393 | |
| 1394 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1395 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1397 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1398 | redrawcmd(); |
| 1399 | } |
| 1400 | goto cmdline_not_changed; |
| 1401 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1402 | #ifdef FEAT_GUI_TABLINE |
| 1403 | case K_TABLINE: |
| 1404 | case K_TABMENU: |
| 1405 | /* Don't want to change any tabs here. Make sure the same tab |
| 1406 | * is still selected. */ |
| 1407 | if (gui_use_tabline()) |
| 1408 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1409 | goto cmdline_not_changed; |
| 1410 | #endif |
| 1411 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1412 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1413 | goto cmdline_not_changed; |
| 1414 | |
| 1415 | case Ctrl_B: /* begin of command line */ |
| 1416 | case K_HOME: |
| 1417 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | case K_S_HOME: |
| 1419 | case K_C_HOME: |
| 1420 | ccline.cmdpos = 0; |
| 1421 | set_cmdspos(); |
| 1422 | goto cmdline_not_changed; |
| 1423 | |
| 1424 | case Ctrl_E: /* end of command line */ |
| 1425 | case K_END: |
| 1426 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | case K_S_END: |
| 1428 | case K_C_END: |
| 1429 | ccline.cmdpos = ccline.cmdlen; |
| 1430 | set_cmdspos_cursor(); |
| 1431 | goto cmdline_not_changed; |
| 1432 | |
| 1433 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1434 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | goto cmdline_changed; |
| 1437 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1438 | case Ctrl_L: |
| 1439 | #ifdef FEAT_SEARCH_EXTRA |
| 1440 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1441 | { |
| 1442 | /* Add a character from under the cursor for 'incsearch' */ |
| 1443 | if (did_incsearch |
| 1444 | && !equalpos(curwin->w_cursor, old_cursor)) |
| 1445 | { |
| 1446 | c = gchar_cursor(); |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 1447 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1448 | * command line has no uppercase characters, convert |
| 1449 | * the character to lowercase */ |
| 1450 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff)) |
| 1451 | c = MB_TOLOWER(c); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1452 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1453 | { |
| 1454 | if (c == firstc || vim_strchr((char_u *)( |
| 1455 | p_magic ? "\\^$.*[" : "\\^$"), c) |
| 1456 | != NULL) |
| 1457 | { |
| 1458 | /* put a backslash before special characters */ |
| 1459 | stuffcharReadbuff(c); |
| 1460 | c = '\\'; |
| 1461 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1462 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1463 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1464 | } |
| 1465 | goto cmdline_not_changed; |
| 1466 | } |
| 1467 | #endif |
| 1468 | |
| 1469 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1470 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1471 | break; |
| 1472 | goto cmdline_changed; |
| 1473 | |
| 1474 | case Ctrl_N: /* next match */ |
| 1475 | case Ctrl_P: /* previous match */ |
| 1476 | if (xpc.xp_numfiles > 0) |
| 1477 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1478 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1479 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | goto cmdline_changed; |
| 1482 | } |
| 1483 | |
| 1484 | #ifdef FEAT_CMDHIST |
| 1485 | case K_UP: |
| 1486 | case K_DOWN: |
| 1487 | case K_S_UP: |
| 1488 | case K_S_DOWN: |
| 1489 | case K_PAGEUP: |
| 1490 | case K_KPAGEUP: |
| 1491 | case K_PAGEDOWN: |
| 1492 | case K_KPAGEDOWN: |
| 1493 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1494 | goto cmdline_not_changed; |
| 1495 | |
| 1496 | i = hiscnt; |
| 1497 | |
| 1498 | /* save current command string so it can be restored later */ |
| 1499 | if (lookfor == NULL) |
| 1500 | { |
| 1501 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1502 | goto cmdline_not_changed; |
| 1503 | lookfor[ccline.cmdpos] = NUL; |
| 1504 | } |
| 1505 | |
| 1506 | j = (int)STRLEN(lookfor); |
| 1507 | for (;;) |
| 1508 | { |
| 1509 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1510 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1511 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | { |
| 1513 | if (hiscnt == hislen) /* first time */ |
| 1514 | hiscnt = hisidx[histype]; |
| 1515 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1516 | hiscnt = hislen - 1; |
| 1517 | else if (hiscnt != hisidx[histype] + 1) |
| 1518 | --hiscnt; |
| 1519 | else /* at top of list */ |
| 1520 | { |
| 1521 | hiscnt = i; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | else /* one step forwards */ |
| 1526 | { |
| 1527 | /* on last entry, clear the line */ |
| 1528 | if (hiscnt == hisidx[histype]) |
| 1529 | { |
| 1530 | hiscnt = hislen; |
| 1531 | break; |
| 1532 | } |
| 1533 | |
| 1534 | /* not on a history line, nothing to do */ |
| 1535 | if (hiscnt == hislen) |
| 1536 | break; |
| 1537 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1538 | hiscnt = 0; |
| 1539 | else |
| 1540 | ++hiscnt; |
| 1541 | } |
| 1542 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1543 | { |
| 1544 | hiscnt = i; |
| 1545 | break; |
| 1546 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1547 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1548 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1550 | lookfor, (size_t)j) == 0) |
| 1551 | break; |
| 1552 | } |
| 1553 | |
| 1554 | if (hiscnt != i) /* jumped to other entry */ |
| 1555 | { |
| 1556 | char_u *p; |
| 1557 | int len; |
| 1558 | int old_firstc; |
| 1559 | |
| 1560 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1561 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | if (hiscnt == hislen) |
| 1563 | p = lookfor; /* back to the old one */ |
| 1564 | else |
| 1565 | p = history[histype][hiscnt].hisstr; |
| 1566 | |
| 1567 | if (histype == HIST_SEARCH |
| 1568 | && p != lookfor |
| 1569 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1570 | { |
| 1571 | /* Correct for the separator character used when |
| 1572 | * adding the history entry vs the one used now. |
| 1573 | * First loop: count length. |
| 1574 | * Second loop: copy the characters. */ |
| 1575 | for (i = 0; i <= 1; ++i) |
| 1576 | { |
| 1577 | len = 0; |
| 1578 | for (j = 0; p[j] != NUL; ++j) |
| 1579 | { |
| 1580 | /* Replace old sep with new sep, unless it is |
| 1581 | * escaped. */ |
| 1582 | if (p[j] == old_firstc |
| 1583 | && (j == 0 || p[j - 1] != '\\')) |
| 1584 | { |
| 1585 | if (i > 0) |
| 1586 | ccline.cmdbuff[len] = firstc; |
| 1587 | } |
| 1588 | else |
| 1589 | { |
| 1590 | /* Escape new sep, unless it is already |
| 1591 | * escaped. */ |
| 1592 | if (p[j] == firstc |
| 1593 | && (j == 0 || p[j - 1] != '\\')) |
| 1594 | { |
| 1595 | if (i > 0) |
| 1596 | ccline.cmdbuff[len] = '\\'; |
| 1597 | ++len; |
| 1598 | } |
| 1599 | if (i > 0) |
| 1600 | ccline.cmdbuff[len] = p[j]; |
| 1601 | } |
| 1602 | ++len; |
| 1603 | } |
| 1604 | if (i == 0) |
| 1605 | { |
| 1606 | alloc_cmdbuff(len); |
| 1607 | if (ccline.cmdbuff == NULL) |
| 1608 | goto returncmd; |
| 1609 | } |
| 1610 | } |
| 1611 | ccline.cmdbuff[len] = NUL; |
| 1612 | } |
| 1613 | else |
| 1614 | { |
| 1615 | alloc_cmdbuff((int)STRLEN(p)); |
| 1616 | if (ccline.cmdbuff == NULL) |
| 1617 | goto returncmd; |
| 1618 | STRCPY(ccline.cmdbuff, p); |
| 1619 | } |
| 1620 | |
| 1621 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1622 | redrawcmd(); |
| 1623 | goto cmdline_changed; |
| 1624 | } |
| 1625 | beep_flush(); |
| 1626 | goto cmdline_not_changed; |
| 1627 | #endif |
| 1628 | |
| 1629 | case Ctrl_V: |
| 1630 | case Ctrl_Q: |
| 1631 | #ifdef FEAT_MOUSE |
| 1632 | ignore_drag_release = TRUE; |
| 1633 | #endif |
| 1634 | putcmdline('^', TRUE); |
| 1635 | c = get_literal(); /* get next (two) character(s) */ |
| 1636 | do_abbr = FALSE; /* don't do abbreviation now */ |
| 1637 | #ifdef FEAT_MBYTE |
| 1638 | /* may need to remove ^ when composing char was typed */ |
| 1639 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1640 | { |
| 1641 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1642 | msg_putchar(' '); |
| 1643 | cursorcmd(); |
| 1644 | } |
| 1645 | #endif |
| 1646 | break; |
| 1647 | |
| 1648 | #ifdef FEAT_DIGRAPHS |
| 1649 | case Ctrl_K: |
| 1650 | #ifdef FEAT_MOUSE |
| 1651 | ignore_drag_release = TRUE; |
| 1652 | #endif |
| 1653 | putcmdline('?', TRUE); |
| 1654 | #ifdef USE_ON_FLY_SCROLL |
| 1655 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1656 | #endif |
| 1657 | c = get_digraph(TRUE); |
| 1658 | if (c != NUL) |
| 1659 | break; |
| 1660 | |
| 1661 | redrawcmd(); |
| 1662 | goto cmdline_not_changed; |
| 1663 | #endif /* FEAT_DIGRAPHS */ |
| 1664 | |
| 1665 | #ifdef FEAT_RIGHTLEFT |
| 1666 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1667 | if (!p_ari) |
| 1668 | break; |
| 1669 | #ifdef FEAT_FKMAP |
| 1670 | if (p_altkeymap) |
| 1671 | { |
| 1672 | cmd_fkmap = !cmd_fkmap; |
| 1673 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1674 | ccline.overstrike = FALSE; |
| 1675 | } |
| 1676 | else /* Hebrew is default */ |
| 1677 | #endif |
| 1678 | cmd_hkmap = !cmd_hkmap; |
| 1679 | goto cmdline_not_changed; |
| 1680 | #endif |
| 1681 | |
| 1682 | default: |
| 1683 | #ifdef UNIX |
| 1684 | if (c == intr_char) |
| 1685 | { |
| 1686 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1687 | putting it in history */ |
| 1688 | goto returncmd; /* back to Normal mode */ |
| 1689 | } |
| 1690 | #endif |
| 1691 | /* |
| 1692 | * Normal character with no special meaning. Just set mod_mask |
| 1693 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1694 | * the string <S-Space>. This should only happen after ^V. |
| 1695 | */ |
| 1696 | if (!IS_SPECIAL(c)) |
| 1697 | mod_mask = 0x0; |
| 1698 | break; |
| 1699 | } |
| 1700 | /* |
| 1701 | * End of switch on command line character. |
| 1702 | * We come here if we have a normal character. |
| 1703 | */ |
| 1704 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1705 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1706 | #ifdef FEAT_MBYTE |
| 1707 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1708 | * what check_abbr() expects. */ |
| 1709 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1710 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1711 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1712 | goto cmdline_changed; |
| 1713 | |
| 1714 | /* |
| 1715 | * put the character in the command line |
| 1716 | */ |
| 1717 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1718 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1719 | else |
| 1720 | { |
| 1721 | #ifdef FEAT_MBYTE |
| 1722 | if (has_mbyte) |
| 1723 | { |
| 1724 | j = (*mb_char2bytes)(c, IObuff); |
| 1725 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1726 | put_on_cmdline(IObuff, j, TRUE); |
| 1727 | } |
| 1728 | else |
| 1729 | #endif |
| 1730 | { |
| 1731 | IObuff[0] = c; |
| 1732 | put_on_cmdline(IObuff, 1, TRUE); |
| 1733 | } |
| 1734 | } |
| 1735 | goto cmdline_changed; |
| 1736 | |
| 1737 | /* |
| 1738 | * This part implements incremental searches for "/" and "?" |
| 1739 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1740 | * line did not change. Then we only search and redraw if something changed in |
| 1741 | * the past. |
| 1742 | * Jump to cmdline_changed when the command line did change. |
| 1743 | * (Sorry for the goto's, I know it is ugly). |
| 1744 | */ |
| 1745 | cmdline_not_changed: |
| 1746 | #ifdef FEAT_SEARCH_EXTRA |
| 1747 | if (!incsearch_postponed) |
| 1748 | continue; |
| 1749 | #endif |
| 1750 | |
| 1751 | cmdline_changed: |
| 1752 | #ifdef FEAT_SEARCH_EXTRA |
| 1753 | /* |
| 1754 | * 'incsearch' highlighting. |
| 1755 | */ |
| 1756 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1757 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1758 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1759 | #ifdef FEAT_RELTIME |
| 1760 | proftime_T tm; |
| 1761 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1762 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1763 | /* if there is a character waiting, search and redraw later */ |
| 1764 | if (char_avail()) |
| 1765 | { |
| 1766 | incsearch_postponed = TRUE; |
| 1767 | continue; |
| 1768 | } |
| 1769 | incsearch_postponed = FALSE; |
| 1770 | curwin->w_cursor = old_cursor; /* start at old position */ |
| 1771 | |
| 1772 | /* If there is no command line, don't do anything */ |
| 1773 | if (ccline.cmdlen == 0) |
| 1774 | i = 0; |
| 1775 | else |
| 1776 | { |
| 1777 | cursor_off(); /* so the user knows we're busy */ |
| 1778 | out_flush(); |
| 1779 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1780 | #ifdef FEAT_RELTIME |
| 1781 | /* Set the time limit to half a second. */ |
| 1782 | profile_setlimit(500L, &tm); |
| 1783 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1784 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1785 | SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
| 1786 | #ifdef FEAT_RELTIME |
| 1787 | &tm |
| 1788 | #else |
| 1789 | NULL |
| 1790 | #endif |
| 1791 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | --emsg_off; |
| 1793 | /* if interrupted while searching, behave like it failed */ |
| 1794 | if (got_int) |
| 1795 | { |
| 1796 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1797 | got_int = FALSE; /* don't abandon the command line */ |
| 1798 | i = 0; |
| 1799 | } |
| 1800 | else if (char_avail()) |
| 1801 | /* cancelled searching because a char was typed */ |
| 1802 | incsearch_postponed = TRUE; |
| 1803 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1804 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | highlight_match = TRUE; /* highlight position */ |
| 1806 | else |
| 1807 | highlight_match = FALSE; /* remove highlight */ |
| 1808 | |
| 1809 | /* first restore the old curwin values, so the screen is |
| 1810 | * positioned in the same way as the actual search command */ |
| 1811 | curwin->w_leftcol = old_leftcol; |
| 1812 | curwin->w_topline = old_topline; |
| 1813 | # ifdef FEAT_DIFF |
| 1814 | curwin->w_topfill = old_topfill; |
| 1815 | # endif |
| 1816 | curwin->w_botline = old_botline; |
| 1817 | changed_cline_bef_curs(); |
| 1818 | update_topline(); |
| 1819 | |
| 1820 | if (i != 0) |
| 1821 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1822 | pos_T save_pos = curwin->w_cursor; |
| 1823 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | /* |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1825 | * First move cursor to end of match, then to the start. This |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1826 | * moves the whole match onto the screen when 'nowrap' is set. |
| 1827 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1828 | curwin->w_cursor.lnum += search_match_lines; |
| 1829 | curwin->w_cursor.col = search_match_endcol; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1830 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 1831 | { |
| 1832 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 1833 | coladvance((colnr_T)MAXCOL); |
| 1834 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1835 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1836 | end_pos = curwin->w_cursor; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1837 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1838 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1839 | else |
| 1840 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1841 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 1843 | # ifdef FEAT_WINDOWS |
| 1844 | /* May redraw the status line to show the cursor position. */ |
| 1845 | if (p_ru && curwin->w_status_height > 0) |
| 1846 | curwin->w_redr_status = TRUE; |
| 1847 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1848 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1849 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1850 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1851 | restore_cmdline(&save_ccline); |
| 1852 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1853 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 1854 | if (i != 0) |
| 1855 | curwin->w_cursor = end_pos; |
| 1856 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | msg_starthere(); |
| 1858 | redrawcmdline(); |
| 1859 | did_incsearch = TRUE; |
| 1860 | } |
| 1861 | #else /* FEAT_SEARCH_EXTRA */ |
| 1862 | ; |
| 1863 | #endif |
| 1864 | |
| 1865 | #ifdef FEAT_RIGHTLEFT |
| 1866 | if (cmdmsg_rl |
| 1867 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1868 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1869 | # endif |
| 1870 | ) |
| 1871 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 1872 | * right-left typing. Not efficient, but it works. |
| 1873 | * Do it only when there are no characters left to read |
| 1874 | * to avoid useless intermediate redraws. */ |
| 1875 | if (vpeekc() == NUL) |
| 1876 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1877 | #endif |
| 1878 | } |
| 1879 | |
| 1880 | returncmd: |
| 1881 | |
| 1882 | #ifdef FEAT_RIGHTLEFT |
| 1883 | cmdmsg_rl = FALSE; |
| 1884 | #endif |
| 1885 | |
| 1886 | #ifdef FEAT_FKMAP |
| 1887 | cmd_fkmap = 0; |
| 1888 | #endif |
| 1889 | |
| 1890 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1891 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1892 | |
| 1893 | #ifdef FEAT_SEARCH_EXTRA |
| 1894 | if (did_incsearch) |
| 1895 | { |
| 1896 | curwin->w_cursor = old_cursor; |
| 1897 | curwin->w_curswant = old_curswant; |
| 1898 | curwin->w_leftcol = old_leftcol; |
| 1899 | curwin->w_topline = old_topline; |
| 1900 | # ifdef FEAT_DIFF |
| 1901 | curwin->w_topfill = old_topfill; |
| 1902 | # endif |
| 1903 | curwin->w_botline = old_botline; |
| 1904 | highlight_match = FALSE; |
| 1905 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1906 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | } |
| 1908 | #endif |
| 1909 | |
| 1910 | if (ccline.cmdbuff != NULL) |
| 1911 | { |
| 1912 | /* |
| 1913 | * Put line in history buffer (":" and "=" only when it was typed). |
| 1914 | */ |
| 1915 | #ifdef FEAT_CMDHIST |
| 1916 | if (ccline.cmdlen && firstc != NUL |
| 1917 | && (some_key_typed || histype == HIST_SEARCH)) |
| 1918 | { |
| 1919 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 1920 | histype == HIST_SEARCH ? firstc : NUL); |
| 1921 | if (firstc == ':') |
| 1922 | { |
| 1923 | vim_free(new_last_cmdline); |
| 1924 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 1925 | } |
| 1926 | } |
| 1927 | #endif |
| 1928 | |
| 1929 | if (gotesc) /* abandon command line */ |
| 1930 | { |
| 1931 | vim_free(ccline.cmdbuff); |
| 1932 | ccline.cmdbuff = NULL; |
| 1933 | if (msg_scrolled == 0) |
| 1934 | compute_cmdrow(); |
| 1935 | MSG(""); |
| 1936 | redraw_cmdline = TRUE; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * If the screen was shifted up, redraw the whole screen (later). |
| 1942 | * If the line is too long, clear it, so ruler and shown command do |
| 1943 | * not get printed in the middle of it. |
| 1944 | */ |
| 1945 | msg_check(); |
| 1946 | msg_scroll = save_msg_scroll; |
| 1947 | redir_off = FALSE; |
| 1948 | |
| 1949 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 1950 | if (some_key_typed) |
| 1951 | need_wait_return = FALSE; |
| 1952 | |
| 1953 | State = save_State; |
| 1954 | #ifdef USE_IM_CONTROL |
| 1955 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 1956 | im_save_status(b_im_ptr); |
| 1957 | im_set_active(FALSE); |
| 1958 | #endif |
| 1959 | #ifdef FEAT_MOUSE |
| 1960 | setmouse(); |
| 1961 | #endif |
| 1962 | #ifdef CURSOR_SHAPE |
| 1963 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1964 | #endif |
| 1965 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1966 | { |
| 1967 | char_u *p = ccline.cmdbuff; |
| 1968 | |
| 1969 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 1970 | ccline.cmdbuff = NULL; |
| 1971 | return p; |
| 1972 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 1976 | /* |
| 1977 | * Get a command line with a prompt. |
| 1978 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 1979 | * f_input() when evaluating an expression from CTRL-R =). |
| 1980 | * Returns the command line in allocated memory, or NULL. |
| 1981 | */ |
| 1982 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1983 | getcmdline_prompt( |
| 1984 | int firstc, |
| 1985 | char_u *prompt, /* command line prompt */ |
| 1986 | int attr, /* attributes for prompt */ |
| 1987 | int xp_context, /* type of expansion */ |
| 1988 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | { |
| 1990 | char_u *s; |
| 1991 | struct cmdline_info save_ccline; |
| 1992 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 1993 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1995 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1996 | ccline.cmdprompt = prompt; |
| 1997 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1998 | # ifdef FEAT_EVAL |
| 1999 | ccline.xp_context = xp_context; |
| 2000 | ccline.xp_arg = xp_arg; |
| 2001 | ccline.input_fn = (firstc == '@'); |
| 2002 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2003 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2005 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2006 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2007 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2008 | * But only if called recursively and the commandline is therefore being |
| 2009 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2010 | * so we need its modified msg_col left intact. */ |
| 2011 | if (ccline.cmdbuff != NULL) |
| 2012 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2013 | |
| 2014 | return s; |
| 2015 | } |
| 2016 | #endif |
| 2017 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2018 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2019 | * Return TRUE when the text must not be changed and we can't switch to |
| 2020 | * another window or buffer. Used when editing the command line, evaluating |
| 2021 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2022 | */ |
| 2023 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2024 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2025 | { |
| 2026 | #ifdef FEAT_CMDWIN |
| 2027 | if (cmdwin_type != 0) |
| 2028 | return TRUE; |
| 2029 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2030 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | * Give an error message for a command that isn't allowed while the cmdline |
| 2035 | * window is open or editing the cmdline in another way. |
| 2036 | */ |
| 2037 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2038 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2039 | { |
| 2040 | #ifdef FEAT_CMDWIN |
| 2041 | if (cmdwin_type != 0) |
| 2042 | EMSG(_(e_cmdwin)); |
| 2043 | else |
| 2044 | #endif |
| 2045 | EMSG(_(e_secure)); |
| 2046 | } |
| 2047 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2048 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2049 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2050 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2051 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2052 | */ |
| 2053 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2054 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2055 | { |
| 2056 | if (curbuf_lock > 0) |
| 2057 | { |
| 2058 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2059 | return TRUE; |
| 2060 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2061 | return allbuf_locked(); |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2066 | * message. |
| 2067 | */ |
| 2068 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2069 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2070 | { |
| 2071 | if (allbuf_lock > 0) |
| 2072 | { |
| 2073 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2074 | return TRUE; |
| 2075 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2076 | return FALSE; |
| 2077 | } |
| 2078 | #endif |
| 2079 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2080 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2081 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2082 | { |
| 2083 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2084 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2085 | return 1; |
| 2086 | #endif |
| 2087 | return ptr2cells(ccline.cmdbuff + idx); |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Compute the offset of the cursor on the command line for the prompt and |
| 2092 | * indent. |
| 2093 | */ |
| 2094 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2095 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2096 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2097 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2099 | else |
| 2100 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * Compute the screen position for the cursor on the command line. |
| 2105 | */ |
| 2106 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2107 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2108 | { |
| 2109 | int i, m, c; |
| 2110 | |
| 2111 | set_cmdspos(); |
| 2112 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2113 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2114 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2115 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2116 | m = MAXCOL; |
| 2117 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2118 | else |
| 2119 | m = MAXCOL; |
| 2120 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2121 | { |
| 2122 | c = cmdline_charsize(i); |
| 2123 | #ifdef FEAT_MBYTE |
| 2124 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2125 | if (has_mbyte) |
| 2126 | correct_cmdspos(i, c); |
| 2127 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2128 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2129 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | if ((ccline.cmdspos += c) >= m) |
| 2131 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | ccline.cmdspos -= c; |
| 2133 | break; |
| 2134 | } |
| 2135 | #ifdef FEAT_MBYTE |
| 2136 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2137 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | #endif |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | #ifdef FEAT_MBYTE |
| 2143 | /* |
| 2144 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2145 | * character that doesn't fit, so that a ">" must be displayed. |
| 2146 | */ |
| 2147 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2148 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2150 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2151 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2152 | && ccline.cmdspos % Columns + cells > Columns) |
| 2153 | ccline.cmdspos++; |
| 2154 | } |
| 2155 | #endif |
| 2156 | |
| 2157 | /* |
| 2158 | * Get an Ex command line for the ":" command. |
| 2159 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2160 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2161 | getexline( |
| 2162 | int c, /* normally ':', NUL for ":append" */ |
| 2163 | void *cookie UNUSED, |
| 2164 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | { |
| 2166 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2167 | if (exec_from_reg && vpeekc() == ':') |
| 2168 | (void)vgetc(); |
| 2169 | return getcmdline(c, 1L, indent); |
| 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | * Get an Ex command line for Ex mode. |
| 2174 | * In Ex mode we only use the OS supplied line editing features and no |
| 2175 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2176 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2178 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2179 | getexmodeline( |
| 2180 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2181 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2182 | void *cookie UNUSED, |
| 2183 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2184 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2185 | garray_T line_ga; |
| 2186 | char_u *pend; |
| 2187 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2188 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2189 | int escaped = FALSE; /* CTRL-V typed */ |
| 2190 | int vcol = 0; |
| 2191 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2192 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2193 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2194 | |
| 2195 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2196 | * confuses the system function that computes tabstops. */ |
| 2197 | cursor_on(); |
| 2198 | |
| 2199 | /* always start in column 0; write a newline if necessary */ |
| 2200 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2201 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2203 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2204 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2205 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2206 | if (p_prompt) |
| 2207 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | while (indent-- > 0) |
| 2209 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | ga_init2(&line_ga, 1, 30); |
| 2214 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2215 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2216 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2217 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2218 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2219 | while (indent >= 8) |
| 2220 | { |
| 2221 | ga_append(&line_ga, TAB); |
| 2222 | msg_puts((char_u *)" "); |
| 2223 | indent -= 8; |
| 2224 | } |
| 2225 | while (indent-- > 0) |
| 2226 | { |
| 2227 | ga_append(&line_ga, ' '); |
| 2228 | msg_putchar(' '); |
| 2229 | } |
| 2230 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2231 | ++no_mapping; |
| 2232 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2233 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | /* |
| 2235 | * Get the line, one character at a time. |
| 2236 | */ |
| 2237 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2238 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2239 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2240 | long sw; |
| 2241 | char_u *s; |
| 2242 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2243 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2244 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2245 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2246 | /* Get one character at a time. Don't use inchar(), it can't handle |
| 2247 | * special characters. */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2248 | prev_char = c1; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2249 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2250 | |
| 2251 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2252 | * Handle line editing. |
| 2253 | * Previously this was left to the system, putting the terminal in |
| 2254 | * cooked mode, but then CTRL-D and CTRL-T can't be used properly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2256 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2257 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2258 | msg_putchar('\n'); |
| 2259 | break; |
| 2260 | } |
| 2261 | |
| 2262 | if (!escaped) |
| 2263 | { |
| 2264 | /* CR typed means "enter", which is NL */ |
| 2265 | if (c1 == '\r') |
| 2266 | c1 = '\n'; |
| 2267 | |
| 2268 | if (c1 == BS || c1 == K_BS |
| 2269 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2271 | if (line_ga.ga_len > 0) |
| 2272 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2273 | #ifdef FEAT_MBYTE |
| 2274 | if (has_mbyte) |
| 2275 | { |
| 2276 | p = (char_u *)line_ga.ga_data; |
| 2277 | p[line_ga.ga_len] = NUL; |
| 2278 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2279 | line_ga.ga_len -= len; |
| 2280 | } |
| 2281 | else |
| 2282 | #endif |
| 2283 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2284 | goto redraw; |
| 2285 | } |
| 2286 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2289 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2290 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2291 | msg_col = startcol; |
| 2292 | msg_clr_eos(); |
| 2293 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2294 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | if (c1 == Ctrl_T) |
| 2298 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2299 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2300 | p = (char_u *)line_ga.ga_data; |
| 2301 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2302 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2303 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2304 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2305 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2307 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2308 | p = (char_u *)line_ga.ga_data; |
| 2309 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2310 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2311 | *s = ' '; |
| 2312 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2314 | redraw: |
| 2315 | /* redraw the line */ |
| 2316 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2317 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2318 | p = (char_u *)line_ga.ga_data; |
| 2319 | p[line_ga.ga_len] = NUL; |
| 2320 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2321 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2322 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2323 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2324 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2326 | msg_putchar(' '); |
| 2327 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2328 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2330 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2332 | len = MB_PTR2LEN(p); |
| 2333 | msg_outtrans_len(p, len); |
| 2334 | vcol += ptr2cells(p); |
| 2335 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2336 | } |
| 2337 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2338 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2339 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2340 | continue; |
| 2341 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2343 | if (c1 == Ctrl_D) |
| 2344 | { |
| 2345 | /* Delete one shiftwidth. */ |
| 2346 | p = (char_u *)line_ga.ga_data; |
| 2347 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2349 | if (prev_char == '^') |
| 2350 | ex_keep_indent = TRUE; |
| 2351 | indent = 0; |
| 2352 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | } |
| 2354 | else |
| 2355 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2356 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2357 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2358 | if (indent > 0) |
| 2359 | { |
| 2360 | --indent; |
| 2361 | indent -= indent % get_sw_value(curbuf); |
| 2362 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2364 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2365 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2366 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2367 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2368 | --line_ga.ga_len; |
| 2369 | } |
| 2370 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2372 | |
| 2373 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2374 | { |
| 2375 | escaped = TRUE; |
| 2376 | continue; |
| 2377 | } |
| 2378 | |
| 2379 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2380 | if (IS_SPECIAL(c1)) |
| 2381 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2383 | |
| 2384 | if (IS_SPECIAL(c1)) |
| 2385 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2386 | #ifdef FEAT_MBYTE |
| 2387 | if (has_mbyte) |
| 2388 | len = (*mb_char2bytes)(c1, |
| 2389 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2390 | else |
| 2391 | #endif |
| 2392 | { |
| 2393 | len = 1; |
| 2394 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2395 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2396 | if (c1 == '\n') |
| 2397 | msg_putchar('\n'); |
| 2398 | else if (c1 == TAB) |
| 2399 | { |
| 2400 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2401 | do |
| 2402 | { |
| 2403 | msg_putchar(' '); |
| 2404 | } while (++vcol % 8); |
| 2405 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2408 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2409 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2410 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2411 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2412 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2413 | escaped = FALSE; |
| 2414 | |
| 2415 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2416 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2417 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2418 | /* We are done when a NL is entered, but not when it comes after an |
| 2419 | * odd number of backslashes, that results in a NUL. */ |
| 2420 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2421 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2422 | int bcount = 0; |
| 2423 | |
| 2424 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2425 | ++bcount; |
| 2426 | |
| 2427 | if (bcount > 0) |
| 2428 | { |
| 2429 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2430 | * "\NL", etc. */ |
| 2431 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2432 | pend -= (bcount + 1) / 2; |
| 2433 | pend[-1] = '\n'; |
| 2434 | } |
| 2435 | |
| 2436 | if ((bcount & 1) == 0) |
| 2437 | { |
| 2438 | --line_ga.ga_len; |
| 2439 | --pend; |
| 2440 | *pend = NUL; |
| 2441 | break; |
| 2442 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2446 | --no_mapping; |
| 2447 | --allow_keys; |
| 2448 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2449 | /* make following messages go to the next line */ |
| 2450 | msg_didout = FALSE; |
| 2451 | msg_col = 0; |
| 2452 | if (msg_row < Rows - 1) |
| 2453 | ++msg_row; |
| 2454 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2455 | |
| 2456 | if (got_int) |
| 2457 | ga_clear(&line_ga); |
| 2458 | |
| 2459 | return (char_u *)line_ga.ga_data; |
| 2460 | } |
| 2461 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2462 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2463 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | /* |
| 2465 | * Return TRUE if ccline.overstrike is on. |
| 2466 | */ |
| 2467 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2468 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
| 2470 | return ccline.overstrike; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2475 | */ |
| 2476 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2477 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2478 | { |
| 2479 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2480 | } |
| 2481 | #endif |
| 2482 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2483 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2484 | /* |
| 2485 | * Return the virtual column number at the current cursor position. |
| 2486 | * This is used by the IM code to obtain the start of the preedit string. |
| 2487 | */ |
| 2488 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2489 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | { |
| 2491 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2492 | return MAXCOL; |
| 2493 | |
| 2494 | # ifdef FEAT_MBYTE |
| 2495 | if (has_mbyte) |
| 2496 | { |
| 2497 | colnr_T col; |
| 2498 | int i = 0; |
| 2499 | |
| 2500 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2501 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | |
| 2503 | return col; |
| 2504 | } |
| 2505 | else |
| 2506 | # endif |
| 2507 | return ccline.cmdpos; |
| 2508 | } |
| 2509 | #endif |
| 2510 | |
| 2511 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2512 | /* |
| 2513 | * If part of the command line is an IM preedit string, redraw it with |
| 2514 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2515 | */ |
| 2516 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2517 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2518 | { |
| 2519 | if ((State & CMDLINE) |
| 2520 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2521 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | && !p_imdisable |
| 2523 | && im_is_preediting()) |
| 2524 | { |
| 2525 | int cmdpos = 0; |
| 2526 | int cmdspos; |
| 2527 | int old_row; |
| 2528 | int old_col; |
| 2529 | colnr_T col; |
| 2530 | |
| 2531 | old_row = msg_row; |
| 2532 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2533 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2534 | |
| 2535 | # ifdef FEAT_MBYTE |
| 2536 | if (has_mbyte) |
| 2537 | { |
| 2538 | for (col = 0; col < preedit_start_col |
| 2539 | && cmdpos < ccline.cmdlen; ++col) |
| 2540 | { |
| 2541 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2542 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | else |
| 2546 | # endif |
| 2547 | { |
| 2548 | cmdspos += preedit_start_col; |
| 2549 | cmdpos += preedit_start_col; |
| 2550 | } |
| 2551 | |
| 2552 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2553 | msg_col = cmdspos % (int)Columns; |
| 2554 | if (msg_row >= Rows) |
| 2555 | msg_row = Rows - 1; |
| 2556 | |
| 2557 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2558 | { |
| 2559 | int char_len; |
| 2560 | int char_attr; |
| 2561 | |
| 2562 | char_attr = im_get_feedback_attr(col); |
| 2563 | if (char_attr < 0) |
| 2564 | break; /* end of preedit string */ |
| 2565 | |
| 2566 | # ifdef FEAT_MBYTE |
| 2567 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2568 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | else |
| 2570 | # endif |
| 2571 | char_len = 1; |
| 2572 | |
| 2573 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2574 | cmdpos += char_len; |
| 2575 | } |
| 2576 | |
| 2577 | msg_row = old_row; |
| 2578 | msg_col = old_col; |
| 2579 | } |
| 2580 | } |
| 2581 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2582 | |
| 2583 | /* |
| 2584 | * Allocate a new command line buffer. |
| 2585 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2586 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2587 | */ |
| 2588 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2589 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2590 | { |
| 2591 | /* |
| 2592 | * give some extra space to avoid having to allocate all the time |
| 2593 | */ |
| 2594 | if (len < 80) |
| 2595 | len = 100; |
| 2596 | else |
| 2597 | len += 20; |
| 2598 | |
| 2599 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2600 | ccline.cmdbufflen = len; |
| 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * Re-allocate the command line to length len + something extra. |
| 2605 | * return FAIL for failure, OK otherwise |
| 2606 | */ |
| 2607 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2608 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2609 | { |
| 2610 | char_u *p; |
| 2611 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2612 | if (len < ccline.cmdbufflen) |
| 2613 | return OK; /* no need to resize */ |
| 2614 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | p = ccline.cmdbuff; |
| 2616 | alloc_cmdbuff(len); /* will get some more */ |
| 2617 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2618 | { |
| 2619 | ccline.cmdbuff = p; /* keep the old one */ |
| 2620 | return FAIL; |
| 2621 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2622 | /* There isn't always a NUL after the command, but it may need to be |
| 2623 | * there, thus copy up to the NUL and add a NUL. */ |
| 2624 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2625 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2627 | |
| 2628 | if (ccline.xpc != NULL |
| 2629 | && ccline.xpc->xp_pattern != NULL |
| 2630 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2631 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2632 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2633 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2634 | |
| 2635 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2636 | * to point into the newly allocated memory. */ |
| 2637 | if (i >= 0 && i <= ccline.cmdlen) |
| 2638 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2639 | } |
| 2640 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | return OK; |
| 2642 | } |
| 2643 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2644 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2645 | static char_u *arshape_buf = NULL; |
| 2646 | |
| 2647 | # if defined(EXITFREE) || defined(PROTO) |
| 2648 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2649 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2650 | { |
| 2651 | vim_free(arshape_buf); |
| 2652 | } |
| 2653 | # endif |
| 2654 | #endif |
| 2655 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | /* |
| 2657 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2658 | * when cmdline_star is TRUE. |
| 2659 | */ |
| 2660 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2661 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | { |
| 2663 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2664 | int i; |
| 2665 | |
| 2666 | if (cmdline_star > 0) |
| 2667 | for (i = 0; i < len; ++i) |
| 2668 | { |
| 2669 | msg_putchar('*'); |
| 2670 | # ifdef FEAT_MBYTE |
| 2671 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2672 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2673 | # endif |
| 2674 | } |
| 2675 | else |
| 2676 | #endif |
| 2677 | #ifdef FEAT_ARABIC |
| 2678 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2679 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | static int buflen = 0; |
| 2681 | char_u *p; |
| 2682 | int j; |
| 2683 | int newlen = 0; |
| 2684 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2685 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | int prev_c = 0; |
| 2687 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2688 | int u8c; |
| 2689 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2691 | |
| 2692 | /* |
| 2693 | * Do arabic shaping into a temporary buffer. This is very |
| 2694 | * inefficient! |
| 2695 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2696 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | { |
| 2698 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2699 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2700 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2701 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2702 | arshape_buf = alloc(buflen); |
| 2703 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2704 | return; /* out of memory */ |
| 2705 | } |
| 2706 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2707 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2708 | { |
| 2709 | /* Prepend a space to draw the leading composing char on. */ |
| 2710 | arshape_buf[0] = ' '; |
| 2711 | newlen = 1; |
| 2712 | } |
| 2713 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | for (j = start; j < start + len; j += mb_l) |
| 2715 | { |
| 2716 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2717 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2718 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | if (ARABIC_CHAR(u8c)) |
| 2720 | { |
| 2721 | /* Do Arabic shaping. */ |
| 2722 | if (cmdmsg_rl) |
| 2723 | { |
| 2724 | /* displaying from right to left */ |
| 2725 | pc = prev_c; |
| 2726 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2727 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | if (j + mb_l >= start + len) |
| 2729 | nc = NUL; |
| 2730 | else |
| 2731 | nc = utf_ptr2char(p + mb_l); |
| 2732 | } |
| 2733 | else |
| 2734 | { |
| 2735 | /* displaying from left to right */ |
| 2736 | if (j + mb_l >= start + len) |
| 2737 | pc = NUL; |
| 2738 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2739 | { |
| 2740 | int pcc[MAX_MCO]; |
| 2741 | |
| 2742 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2743 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2744 | pc1 = pcc[0]; |
| 2745 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2746 | nc = prev_c; |
| 2747 | } |
| 2748 | prev_c = u8c; |
| 2749 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2750 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2751 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2752 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2753 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2755 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2756 | if (u8cc[1] != 0) |
| 2757 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2758 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | else |
| 2762 | { |
| 2763 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2764 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | newlen += mb_l; |
| 2766 | } |
| 2767 | } |
| 2768 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2769 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2770 | } |
| 2771 | else |
| 2772 | #endif |
| 2773 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 2774 | } |
| 2775 | |
| 2776 | /* |
| 2777 | * Put a character on the command line. Shifts the following text to the |
| 2778 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 2779 | * "c" must be printable (fit in one display cell)! |
| 2780 | */ |
| 2781 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2782 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2783 | { |
| 2784 | if (cmd_silent) |
| 2785 | return; |
| 2786 | msg_no_more = TRUE; |
| 2787 | msg_putchar(c); |
| 2788 | if (shift) |
| 2789 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2790 | msg_no_more = FALSE; |
| 2791 | cursorcmd(); |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * Undo a putcmdline(c, FALSE). |
| 2796 | */ |
| 2797 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2798 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2799 | { |
| 2800 | if (cmd_silent) |
| 2801 | return; |
| 2802 | msg_no_more = TRUE; |
| 2803 | if (ccline.cmdlen == ccline.cmdpos) |
| 2804 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 2805 | #ifdef FEAT_MBYTE |
| 2806 | else if (has_mbyte) |
| 2807 | draw_cmdline(ccline.cmdpos, |
| 2808 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 2809 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | else |
| 2811 | draw_cmdline(ccline.cmdpos, 1); |
| 2812 | msg_no_more = FALSE; |
| 2813 | cursorcmd(); |
| 2814 | } |
| 2815 | |
| 2816 | /* |
| 2817 | * Put the given string, of the given length, onto the command line. |
| 2818 | * If len is -1, then STRLEN() is used to calculate the length. |
| 2819 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 2820 | * part will be redrawn, otherwise it will not. If this function is called |
| 2821 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 2822 | * called afterwards. |
| 2823 | */ |
| 2824 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2825 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2826 | { |
| 2827 | int retval; |
| 2828 | int i; |
| 2829 | int m; |
| 2830 | int c; |
| 2831 | |
| 2832 | if (len < 0) |
| 2833 | len = (int)STRLEN(str); |
| 2834 | |
| 2835 | /* Check if ccline.cmdbuff needs to be longer */ |
| 2836 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2837 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | else |
| 2839 | retval = OK; |
| 2840 | if (retval == OK) |
| 2841 | { |
| 2842 | if (!ccline.overstrike) |
| 2843 | { |
| 2844 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2845 | ccline.cmdbuff + ccline.cmdpos, |
| 2846 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 2847 | ccline.cmdlen += len; |
| 2848 | } |
| 2849 | else |
| 2850 | { |
| 2851 | #ifdef FEAT_MBYTE |
| 2852 | if (has_mbyte) |
| 2853 | { |
| 2854 | /* Count nr of characters in the new string. */ |
| 2855 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2856 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | ++m; |
| 2858 | /* Count nr of bytes in cmdline that are overwritten by these |
| 2859 | * characters. */ |
| 2860 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2861 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | --m; |
| 2863 | if (i < ccline.cmdlen) |
| 2864 | { |
| 2865 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2866 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 2867 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 2868 | } |
| 2869 | else |
| 2870 | ccline.cmdlen = ccline.cmdpos + len; |
| 2871 | } |
| 2872 | else |
| 2873 | #endif |
| 2874 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 2875 | ccline.cmdlen = ccline.cmdpos + len; |
| 2876 | } |
| 2877 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 2878 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 2879 | |
| 2880 | #ifdef FEAT_MBYTE |
| 2881 | if (enc_utf8) |
| 2882 | { |
| 2883 | /* When the inserted text starts with a composing character, |
| 2884 | * backup to the character before it. There could be two of them. |
| 2885 | */ |
| 2886 | i = 0; |
| 2887 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2888 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 2889 | { |
| 2890 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2891 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2892 | ccline.cmdpos -= i; |
| 2893 | len += i; |
| 2894 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2895 | } |
| 2896 | # ifdef FEAT_ARABIC |
| 2897 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 2898 | { |
| 2899 | /* Check the previous character for Arabic combining pair. */ |
| 2900 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2901 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2902 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 2903 | + ccline.cmdpos - i), c)) |
| 2904 | { |
| 2905 | ccline.cmdpos -= i; |
| 2906 | len += i; |
| 2907 | } |
| 2908 | else |
| 2909 | i = 0; |
| 2910 | } |
| 2911 | # endif |
| 2912 | if (i != 0) |
| 2913 | { |
| 2914 | /* Also backup the cursor position. */ |
| 2915 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 2916 | ccline.cmdspos -= i; |
| 2917 | msg_col -= i; |
| 2918 | if (msg_col < 0) |
| 2919 | { |
| 2920 | msg_col += Columns; |
| 2921 | --msg_row; |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | #endif |
| 2926 | |
| 2927 | if (redraw && !cmd_silent) |
| 2928 | { |
| 2929 | msg_no_more = TRUE; |
| 2930 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 2931 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2932 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2933 | /* Avoid clearing the rest of the line too often. */ |
| 2934 | if (cmdline_row != i || ccline.overstrike) |
| 2935 | msg_clr_eos(); |
| 2936 | msg_no_more = FALSE; |
| 2937 | } |
| 2938 | #ifdef FEAT_FKMAP |
| 2939 | /* |
| 2940 | * If we are in Farsi command mode, the character input must be in |
| 2941 | * Insert mode. So do not advance the cmdpos. |
| 2942 | */ |
| 2943 | if (!cmd_fkmap) |
| 2944 | #endif |
| 2945 | { |
| 2946 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2947 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2948 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2949 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2950 | m = MAXCOL; |
| 2951 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | else |
| 2953 | m = MAXCOL; |
| 2954 | for (i = 0; i < len; ++i) |
| 2955 | { |
| 2956 | c = cmdline_charsize(ccline.cmdpos); |
| 2957 | #ifdef FEAT_MBYTE |
| 2958 | /* count ">" for a double-wide char that doesn't fit. */ |
| 2959 | if (has_mbyte) |
| 2960 | correct_cmdspos(ccline.cmdpos, c); |
| 2961 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2962 | /* Stop cursor at the end of the screen, but do increment the |
| 2963 | * insert position, so that entering a very long command |
| 2964 | * works, even though you can't see it. */ |
| 2965 | if (ccline.cmdspos + c < m) |
| 2966 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2967 | #ifdef FEAT_MBYTE |
| 2968 | if (has_mbyte) |
| 2969 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2970 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | if (c > len - i - 1) |
| 2972 | c = len - i - 1; |
| 2973 | ccline.cmdpos += c; |
| 2974 | i += c; |
| 2975 | } |
| 2976 | #endif |
| 2977 | ++ccline.cmdpos; |
| 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | if (redraw) |
| 2982 | msg_check(); |
| 2983 | return retval; |
| 2984 | } |
| 2985 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2986 | static struct cmdline_info prev_ccline; |
| 2987 | static int prev_ccline_used = FALSE; |
| 2988 | |
| 2989 | /* |
| 2990 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 2991 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 2992 | * available globally in prev_ccline. |
| 2993 | */ |
| 2994 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2995 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2996 | { |
| 2997 | if (!prev_ccline_used) |
| 2998 | { |
| 2999 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3000 | prev_ccline_used = TRUE; |
| 3001 | } |
| 3002 | *ccp = prev_ccline; |
| 3003 | prev_ccline = ccline; |
| 3004 | ccline.cmdbuff = NULL; |
| 3005 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3006 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3010 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3011 | */ |
| 3012 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3013 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3014 | { |
| 3015 | ccline = prev_ccline; |
| 3016 | prev_ccline = *ccp; |
| 3017 | } |
| 3018 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3019 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3020 | /* |
| 3021 | * Save the command line into allocated memory. Returns a pointer to be |
| 3022 | * passed to restore_cmdline_alloc() later. |
| 3023 | * Returns NULL when failed. |
| 3024 | */ |
| 3025 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3026 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3027 | { |
| 3028 | struct cmdline_info *p; |
| 3029 | |
| 3030 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3031 | if (p != NULL) |
| 3032 | save_cmdline(p); |
| 3033 | return (char_u *)p; |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3038 | */ |
| 3039 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3040 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3041 | { |
| 3042 | if (p != NULL) |
| 3043 | { |
| 3044 | restore_cmdline((struct cmdline_info *)p); |
| 3045 | vim_free(p); |
| 3046 | } |
| 3047 | } |
| 3048 | #endif |
| 3049 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3050 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3051 | * Paste a yank register into the command line. |
| 3052 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3053 | * insert_reg() can't be used here, because special characters from the |
| 3054 | * register contents will be interpreted as commands. |
| 3055 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3056 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3057 | */ |
| 3058 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3059 | cmdline_paste( |
| 3060 | int regname, |
| 3061 | int literally, /* Insert text literally instead of "as typed" */ |
| 3062 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3063 | { |
| 3064 | long i; |
| 3065 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3066 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3067 | int allocated; |
| 3068 | struct cmdline_info save_ccline; |
| 3069 | |
| 3070 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3071 | * the command line */ |
| 3072 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3073 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3074 | return FAIL; |
| 3075 | |
| 3076 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3077 | * CTRL-C to break the loop. */ |
| 3078 | line_breakcheck(); |
| 3079 | if (got_int) |
| 3080 | return FAIL; |
| 3081 | |
| 3082 | #ifdef FEAT_CLIPBOARD |
| 3083 | regname = may_get_selection(regname); |
| 3084 | #endif |
| 3085 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3086 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3087 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3088 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3089 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3090 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3091 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3092 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3093 | |
| 3094 | if (i) |
| 3095 | { |
| 3096 | /* Got the value of a special register in "arg". */ |
| 3097 | if (arg == NULL) |
| 3098 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3099 | |
| 3100 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3101 | * part of the word. */ |
| 3102 | p = arg; |
| 3103 | if (p_is && regname == Ctrl_W) |
| 3104 | { |
| 3105 | char_u *w; |
| 3106 | int len; |
| 3107 | |
| 3108 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3109 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3110 | { |
| 3111 | #ifdef FEAT_MBYTE |
| 3112 | if (has_mbyte) |
| 3113 | { |
| 3114 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3115 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3116 | break; |
| 3117 | w -= len; |
| 3118 | } |
| 3119 | else |
| 3120 | #endif |
| 3121 | { |
| 3122 | if (!vim_iswordc(w[-1])) |
| 3123 | break; |
| 3124 | --w; |
| 3125 | } |
| 3126 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3127 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3128 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3129 | p += len; |
| 3130 | } |
| 3131 | |
| 3132 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3133 | if (allocated) |
| 3134 | vim_free(arg); |
| 3135 | return OK; |
| 3136 | } |
| 3137 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3138 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * Put a string on the command line. |
| 3143 | * When "literally" is TRUE, insert literally. |
| 3144 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3145 | * line. |
| 3146 | */ |
| 3147 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3148 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3149 | { |
| 3150 | int c, cv; |
| 3151 | |
| 3152 | if (literally) |
| 3153 | put_on_cmdline(s, -1, TRUE); |
| 3154 | else |
| 3155 | while (*s != NUL) |
| 3156 | { |
| 3157 | cv = *s; |
| 3158 | if (cv == Ctrl_V && s[1]) |
| 3159 | ++s; |
| 3160 | #ifdef FEAT_MBYTE |
| 3161 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3162 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3163 | else |
| 3164 | #endif |
| 3165 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3166 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3167 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3168 | #ifdef UNIX |
| 3169 | || c == intr_char |
| 3170 | #endif |
| 3171 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3172 | stuffcharReadbuff(Ctrl_V); |
| 3173 | stuffcharReadbuff(c); |
| 3174 | } |
| 3175 | } |
| 3176 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3177 | #ifdef FEAT_WILDMENU |
| 3178 | /* |
| 3179 | * Delete characters on the command line, from "from" to the current |
| 3180 | * position. |
| 3181 | */ |
| 3182 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3183 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3184 | { |
| 3185 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3186 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3187 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3188 | ccline.cmdpos = from; |
| 3189 | } |
| 3190 | #endif |
| 3191 | |
| 3192 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3193 | * This function is called when the screen size changes and with incremental |
| 3194 | * search and in other situations where the command line may have been |
| 3195 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3196 | */ |
| 3197 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3198 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3199 | { |
| 3200 | if (cmd_silent) |
| 3201 | return; |
| 3202 | need_wait_return = FALSE; |
| 3203 | compute_cmdrow(); |
| 3204 | redrawcmd(); |
| 3205 | cursorcmd(); |
| 3206 | } |
| 3207 | |
| 3208 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3209 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3210 | { |
| 3211 | int i; |
| 3212 | |
| 3213 | if (cmd_silent) |
| 3214 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3215 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3216 | msg_putchar(ccline.cmdfirstc); |
| 3217 | if (ccline.cmdprompt != NULL) |
| 3218 | { |
| 3219 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3220 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3221 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3222 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3223 | --ccline.cmdindent; |
| 3224 | } |
| 3225 | else |
| 3226 | for (i = ccline.cmdindent; i > 0; --i) |
| 3227 | msg_putchar(' '); |
| 3228 | } |
| 3229 | |
| 3230 | /* |
| 3231 | * Redraw what is currently on the command line. |
| 3232 | */ |
| 3233 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3234 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3235 | { |
| 3236 | if (cmd_silent) |
| 3237 | return; |
| 3238 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3239 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3240 | if (ccline.cmdbuff == NULL) |
| 3241 | { |
| 3242 | windgoto(cmdline_row, 0); |
| 3243 | msg_clr_eos(); |
| 3244 | return; |
| 3245 | } |
| 3246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3247 | msg_start(); |
| 3248 | redrawcmdprompt(); |
| 3249 | |
| 3250 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3251 | msg_no_more = TRUE; |
| 3252 | draw_cmdline(0, ccline.cmdlen); |
| 3253 | msg_clr_eos(); |
| 3254 | msg_no_more = FALSE; |
| 3255 | |
| 3256 | set_cmdspos_cursor(); |
| 3257 | |
| 3258 | /* |
| 3259 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3260 | * in cmdline mode we can reset them now. |
| 3261 | */ |
| 3262 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3263 | |
| 3264 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3265 | * in cmdline mode */ |
| 3266 | skip_redraw = FALSE; |
| 3267 | } |
| 3268 | |
| 3269 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3270 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3271 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3272 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | cmdline_row = Rows - 1; |
| 3274 | else |
| 3275 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
| 3276 | + W_STATUS_HEIGHT(lastwin); |
| 3277 | } |
| 3278 | |
| 3279 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3280 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3281 | { |
| 3282 | if (cmd_silent) |
| 3283 | return; |
| 3284 | |
| 3285 | #ifdef FEAT_RIGHTLEFT |
| 3286 | if (cmdmsg_rl) |
| 3287 | { |
| 3288 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3289 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3290 | if (msg_row <= 0) |
| 3291 | msg_row = Rows - 1; |
| 3292 | } |
| 3293 | else |
| 3294 | #endif |
| 3295 | { |
| 3296 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3297 | msg_col = ccline.cmdspos % (int)Columns; |
| 3298 | if (msg_row >= Rows) |
| 3299 | msg_row = Rows - 1; |
| 3300 | } |
| 3301 | |
| 3302 | windgoto(msg_row, msg_col); |
| 3303 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3304 | redrawcmd_preedit(); |
| 3305 | #endif |
| 3306 | #ifdef MCH_CURSOR_SHAPE |
| 3307 | mch_update_cursor(); |
| 3308 | #endif |
| 3309 | } |
| 3310 | |
| 3311 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3312 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3313 | { |
| 3314 | msg_start(); |
| 3315 | #ifdef FEAT_RIGHTLEFT |
| 3316 | if (cmdmsg_rl) |
| 3317 | msg_col = Columns - 1; |
| 3318 | else |
| 3319 | #endif |
| 3320 | msg_col = 0; /* always start in column 0 */ |
| 3321 | if (clr) /* clear the bottom line(s) */ |
| 3322 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3323 | windgoto(cmdline_row, 0); |
| 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | * Check the word in front of the cursor for an abbreviation. |
| 3328 | * Called when the non-id character "c" has been entered. |
| 3329 | * When an abbreviation is recognized it is removed from the text with |
| 3330 | * backspaces and the replacement string is inserted, followed by "c". |
| 3331 | */ |
| 3332 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3333 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | { |
| 3335 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3336 | return FALSE; |
| 3337 | |
| 3338 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3339 | } |
| 3340 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3341 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3342 | static int |
| 3343 | #ifdef __BORLANDC__ |
| 3344 | _RTLENTRYF |
| 3345 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3346 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3347 | { |
| 3348 | char_u *p1 = *(char_u **)s1; |
| 3349 | char_u *p2 = *(char_u **)s2; |
| 3350 | |
| 3351 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3352 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3353 | return STRCMP(p1, p2); |
| 3354 | } |
| 3355 | #endif |
| 3356 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3357 | /* |
| 3358 | * Return FAIL if this is not an appropriate context in which to do |
| 3359 | * completion of anything, return OK if it is (even if there are no matches). |
| 3360 | * For the caller, this means that the character is just passed through like a |
| 3361 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3362 | */ |
| 3363 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3364 | nextwild( |
| 3365 | expand_T *xp, |
| 3366 | int type, |
| 3367 | int options, /* extra options for ExpandOne() */ |
| 3368 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3369 | { |
| 3370 | int i, j; |
| 3371 | char_u *p1; |
| 3372 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3373 | int difflen; |
| 3374 | int v; |
| 3375 | |
| 3376 | if (xp->xp_numfiles == -1) |
| 3377 | { |
| 3378 | set_expand_context(xp); |
| 3379 | cmd_showtail = expand_showtail(xp); |
| 3380 | } |
| 3381 | |
| 3382 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3383 | { |
| 3384 | beep_flush(); |
| 3385 | return OK; /* Something illegal on command line */ |
| 3386 | } |
| 3387 | if (xp->xp_context == EXPAND_NOTHING) |
| 3388 | { |
| 3389 | /* Caller can use the character as a normal char instead */ |
| 3390 | return FAIL; |
| 3391 | } |
| 3392 | |
| 3393 | MSG_PUTS("..."); /* show that we are busy */ |
| 3394 | out_flush(); |
| 3395 | |
| 3396 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3397 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3398 | |
| 3399 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3400 | { |
| 3401 | /* |
| 3402 | * Get next/previous match for a previous expanded pattern. |
| 3403 | */ |
| 3404 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3405 | } |
| 3406 | else |
| 3407 | { |
| 3408 | /* |
| 3409 | * Translate string into pattern and expand it. |
| 3410 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3411 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3412 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3413 | p2 = NULL; |
| 3414 | else |
| 3415 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3416 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3417 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3418 | if (escape) |
| 3419 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3420 | |
| 3421 | if (p_wic) |
| 3422 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3423 | p2 = ExpandOne(xp, p1, |
| 3424 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3425 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3427 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3428 | if (p2 != NULL && type == WILD_LONGEST) |
| 3429 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3430 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3431 | if (ccline.cmdbuff[i + j] == '*' |
| 3432 | || ccline.cmdbuff[i + j] == '?') |
| 3433 | break; |
| 3434 | if ((int)STRLEN(p2) < j) |
| 3435 | { |
| 3436 | vim_free(p2); |
| 3437 | p2 = NULL; |
| 3438 | } |
| 3439 | } |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | if (p2 != NULL && !got_int) |
| 3444 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3445 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3446 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3447 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3448 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3450 | } |
| 3451 | else |
| 3452 | v = OK; |
| 3453 | if (v == OK) |
| 3454 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3455 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3456 | &ccline.cmdbuff[ccline.cmdpos], |
| 3457 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3458 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | ccline.cmdlen += difflen; |
| 3460 | ccline.cmdpos += difflen; |
| 3461 | } |
| 3462 | } |
| 3463 | vim_free(p2); |
| 3464 | |
| 3465 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3466 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | |
| 3468 | /* When expanding a ":map" command and no matches are found, assume that |
| 3469 | * the key is supposed to be inserted literally */ |
| 3470 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3471 | return FAIL; |
| 3472 | |
| 3473 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3474 | beep_flush(); |
| 3475 | else if (xp->xp_numfiles == 1) |
| 3476 | /* free expanded pattern */ |
| 3477 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3478 | |
| 3479 | return OK; |
| 3480 | } |
| 3481 | |
| 3482 | /* |
| 3483 | * Do wildcard expansion on the string 'str'. |
| 3484 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3485 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3486 | * Return NULL for failure. |
| 3487 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3488 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3489 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3490 | * WILD_PREV "orig" should be NULL. |
| 3491 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3492 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3493 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3494 | * |
| 3495 | * mode = WILD_FREE: just free previously expanded matches |
| 3496 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3497 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3498 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3499 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3500 | * mode = WILD_ALL: return all matches concatenated |
| 3501 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3502 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3503 | * |
| 3504 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3505 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3506 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3507 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3508 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3509 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3510 | * options = WILD_SILENT: don't print warning messages |
| 3511 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3512 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3513 | * |
| 3514 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3515 | */ |
| 3516 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3517 | ExpandOne( |
| 3518 | expand_T *xp, |
| 3519 | char_u *str, |
| 3520 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3521 | int options, |
| 3522 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | { |
| 3524 | char_u *ss = NULL; |
| 3525 | static int findex; |
| 3526 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3527 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | int i; |
| 3529 | long_u len; |
| 3530 | int non_suf_match; /* number without matching suffix */ |
| 3531 | |
| 3532 | /* |
| 3533 | * first handle the case of using an old match |
| 3534 | */ |
| 3535 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3536 | { |
| 3537 | if (xp->xp_numfiles > 0) |
| 3538 | { |
| 3539 | if (mode == WILD_PREV) |
| 3540 | { |
| 3541 | if (findex == -1) |
| 3542 | findex = xp->xp_numfiles; |
| 3543 | --findex; |
| 3544 | } |
| 3545 | else /* mode == WILD_NEXT */ |
| 3546 | ++findex; |
| 3547 | |
| 3548 | /* |
| 3549 | * When wrapping around, return the original string, set findex to |
| 3550 | * -1. |
| 3551 | */ |
| 3552 | if (findex < 0) |
| 3553 | { |
| 3554 | if (orig_save == NULL) |
| 3555 | findex = xp->xp_numfiles - 1; |
| 3556 | else |
| 3557 | findex = -1; |
| 3558 | } |
| 3559 | if (findex >= xp->xp_numfiles) |
| 3560 | { |
| 3561 | if (orig_save == NULL) |
| 3562 | findex = 0; |
| 3563 | else |
| 3564 | findex = -1; |
| 3565 | } |
| 3566 | #ifdef FEAT_WILDMENU |
| 3567 | if (p_wmnu) |
| 3568 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3569 | findex, cmd_showtail); |
| 3570 | #endif |
| 3571 | if (findex == -1) |
| 3572 | return vim_strsave(orig_save); |
| 3573 | return vim_strsave(xp->xp_files[findex]); |
| 3574 | } |
| 3575 | else |
| 3576 | return NULL; |
| 3577 | } |
| 3578 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3579 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3580 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3581 | { |
| 3582 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3583 | xp->xp_numfiles = -1; |
| 3584 | vim_free(orig_save); |
| 3585 | orig_save = NULL; |
| 3586 | } |
| 3587 | findex = 0; |
| 3588 | |
| 3589 | if (mode == WILD_FREE) /* only release file name */ |
| 3590 | return NULL; |
| 3591 | |
| 3592 | if (xp->xp_numfiles == -1) |
| 3593 | { |
| 3594 | vim_free(orig_save); |
| 3595 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3596 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3597 | |
| 3598 | /* |
| 3599 | * Do the expansion. |
| 3600 | */ |
| 3601 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3602 | options) == FAIL) |
| 3603 | { |
| 3604 | #ifdef FNAME_ILLEGAL |
| 3605 | /* Illegal file name has been silently skipped. But when there |
| 3606 | * are wildcards, the real problem is that there was no match, |
| 3607 | * causing the pattern to be added, which has illegal characters. |
| 3608 | */ |
| 3609 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3610 | EMSG2(_(e_nomatch2), str); |
| 3611 | #endif |
| 3612 | } |
| 3613 | else if (xp->xp_numfiles == 0) |
| 3614 | { |
| 3615 | if (!(options & WILD_SILENT)) |
| 3616 | EMSG2(_(e_nomatch2), str); |
| 3617 | } |
| 3618 | else |
| 3619 | { |
| 3620 | /* Escape the matches for use on the command line. */ |
| 3621 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3622 | |
| 3623 | /* |
| 3624 | * Check for matching suffixes in file names. |
| 3625 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3626 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3627 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3628 | { |
| 3629 | if (xp->xp_numfiles) |
| 3630 | non_suf_match = xp->xp_numfiles; |
| 3631 | else |
| 3632 | non_suf_match = 1; |
| 3633 | if ((xp->xp_context == EXPAND_FILES |
| 3634 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3635 | && xp->xp_numfiles > 1) |
| 3636 | { |
| 3637 | /* |
| 3638 | * More than one match; check suffix. |
| 3639 | * The files will have been sorted on matching suffix in |
| 3640 | * expand_wildcards, only need to check the first two. |
| 3641 | */ |
| 3642 | non_suf_match = 0; |
| 3643 | for (i = 0; i < 2; ++i) |
| 3644 | if (match_suffix(xp->xp_files[i])) |
| 3645 | ++non_suf_match; |
| 3646 | } |
| 3647 | if (non_suf_match != 1) |
| 3648 | { |
| 3649 | /* Can we ever get here unless it's while expanding |
| 3650 | * interactively? If not, we can get rid of this all |
| 3651 | * together. Don't really want to wait for this message |
| 3652 | * (and possibly have to hit return to continue!). |
| 3653 | */ |
| 3654 | if (!(options & WILD_SILENT)) |
| 3655 | EMSG(_(e_toomany)); |
| 3656 | else if (!(options & WILD_NO_BEEP)) |
| 3657 | beep_flush(); |
| 3658 | } |
| 3659 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3660 | ss = vim_strsave(xp->xp_files[0]); |
| 3661 | } |
| 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | /* Find longest common part */ |
| 3666 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3667 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3668 | int mb_len = 1; |
| 3669 | int c0, ci; |
| 3670 | |
| 3671 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3672 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3673 | #ifdef FEAT_MBYTE |
| 3674 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3675 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3676 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3677 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3678 | } |
| 3679 | else |
| 3680 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3681 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3682 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3683 | { |
| 3684 | #ifdef FEAT_MBYTE |
| 3685 | if (has_mbyte) |
| 3686 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3687 | else |
| 3688 | #endif |
| 3689 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3690 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3691 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3692 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3693 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3694 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3695 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3696 | break; |
| 3697 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3698 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3699 | break; |
| 3700 | } |
| 3701 | if (i < xp->xp_numfiles) |
| 3702 | { |
| 3703 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3704 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3705 | break; |
| 3706 | } |
| 3707 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3708 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3709 | ss = alloc((unsigned)len + 1); |
| 3710 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3711 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3712 | findex = -1; /* next p_wc gets first one */ |
| 3713 | } |
| 3714 | |
| 3715 | /* Concatenate all matching names */ |
| 3716 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3717 | { |
| 3718 | len = 0; |
| 3719 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3720 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3721 | ss = lalloc(len, TRUE); |
| 3722 | if (ss != NULL) |
| 3723 | { |
| 3724 | *ss = NUL; |
| 3725 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3726 | { |
| 3727 | STRCAT(ss, xp->xp_files[i]); |
| 3728 | if (i != xp->xp_numfiles - 1) |
| 3729 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3730 | } |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3735 | ExpandCleanup(xp); |
| 3736 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3737 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3738 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3739 | vim_free(orig); |
| 3740 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3741 | return ss; |
| 3742 | } |
| 3743 | |
| 3744 | /* |
| 3745 | * Prepare an expand structure for use. |
| 3746 | */ |
| 3747 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3748 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3749 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3750 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3751 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3753 | #ifndef BACKSLASH_IN_FILENAME |
| 3754 | xp->xp_shell = FALSE; |
| 3755 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | xp->xp_numfiles = -1; |
| 3757 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3758 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3759 | xp->xp_arg = NULL; |
| 3760 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 3761 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | /* |
| 3765 | * Cleanup an expand structure after use. |
| 3766 | */ |
| 3767 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3768 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | { |
| 3770 | if (xp->xp_numfiles >= 0) |
| 3771 | { |
| 3772 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3773 | xp->xp_numfiles = -1; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3778 | ExpandEscape( |
| 3779 | expand_T *xp, |
| 3780 | char_u *str, |
| 3781 | int numfiles, |
| 3782 | char_u **files, |
| 3783 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3784 | { |
| 3785 | int i; |
| 3786 | char_u *p; |
| 3787 | |
| 3788 | /* |
| 3789 | * May change home directory back to "~" |
| 3790 | */ |
| 3791 | if (options & WILD_HOME_REPLACE) |
| 3792 | tilde_replace(str, numfiles, files); |
| 3793 | |
| 3794 | if (options & WILD_ESCAPE) |
| 3795 | { |
| 3796 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 3797 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3798 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3799 | || xp->xp_context == EXPAND_BUFFERS |
| 3800 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3801 | { |
| 3802 | /* |
| 3803 | * Insert a backslash into a file name before a space, \, %, # |
| 3804 | * and wildmatch characters, except '~'. |
| 3805 | */ |
| 3806 | for (i = 0; i < numfiles; ++i) |
| 3807 | { |
| 3808 | /* for ":set path=" we need to escape spaces twice */ |
| 3809 | if (xp->xp_backslash == XP_BS_THREE) |
| 3810 | { |
| 3811 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3812 | if (p != NULL) |
| 3813 | { |
| 3814 | vim_free(files[i]); |
| 3815 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3816 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3817 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3818 | if (p != NULL) |
| 3819 | { |
| 3820 | vim_free(files[i]); |
| 3821 | files[i] = p; |
| 3822 | } |
| 3823 | #endif |
| 3824 | } |
| 3825 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3826 | #ifdef BACKSLASH_IN_FILENAME |
| 3827 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 3828 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3829 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3830 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3831 | if (p != NULL) |
| 3832 | { |
| 3833 | vim_free(files[i]); |
| 3834 | files[i] = p; |
| 3835 | } |
| 3836 | |
| 3837 | /* If 'str' starts with "\~", replace "~" at start of |
| 3838 | * files[i] with "\~". */ |
| 3839 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3840 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | } |
| 3842 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3843 | |
| 3844 | /* If the first file starts with a '+' escape it. Otherwise it |
| 3845 | * could be seen as "+cmd". */ |
| 3846 | if (*files[0] == '+') |
| 3847 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | } |
| 3849 | else if (xp->xp_context == EXPAND_TAGS) |
| 3850 | { |
| 3851 | /* |
| 3852 | * Insert a backslash before characters in a tag name that |
| 3853 | * would terminate the ":tag" command. |
| 3854 | */ |
| 3855 | for (i = 0; i < numfiles; ++i) |
| 3856 | { |
| 3857 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 3858 | if (p != NULL) |
| 3859 | { |
| 3860 | vim_free(files[i]); |
| 3861 | files[i] = p; |
| 3862 | } |
| 3863 | } |
| 3864 | } |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3869 | * Escape special characters in "fname" for when used as a file name argument |
| 3870 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 3871 | * Returns the result in allocated memory. |
| 3872 | */ |
| 3873 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3874 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3875 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3876 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3877 | #ifdef BACKSLASH_IN_FILENAME |
| 3878 | char_u buf[20]; |
| 3879 | int j = 0; |
| 3880 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 3881 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3882 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 3883 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3884 | buf[j++] = *p; |
| 3885 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3886 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3887 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3888 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 3889 | if (shell && csh_like_shell() && p != NULL) |
| 3890 | { |
| 3891 | char_u *s; |
| 3892 | |
| 3893 | /* For csh and similar shells need to put two backslashes before '!'. |
| 3894 | * One is taken by Vim, one by the shell. */ |
| 3895 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 3896 | vim_free(p); |
| 3897 | p = s; |
| 3898 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3899 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3900 | |
| 3901 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 3902 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 3903 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3904 | escape_fname(&p); |
| 3905 | |
| 3906 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3910 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 3911 | */ |
| 3912 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3913 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3914 | { |
| 3915 | char_u *p; |
| 3916 | |
| 3917 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 3918 | if (p != NULL) |
| 3919 | { |
| 3920 | p[0] = '\\'; |
| 3921 | STRCPY(p + 1, *pp); |
| 3922 | vim_free(*pp); |
| 3923 | *pp = p; |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | * For each file name in files[num_files]: |
| 3929 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 3930 | */ |
| 3931 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3932 | tilde_replace( |
| 3933 | char_u *orig_pat, |
| 3934 | int num_files, |
| 3935 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3936 | { |
| 3937 | int i; |
| 3938 | char_u *p; |
| 3939 | |
| 3940 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 3941 | { |
| 3942 | for (i = 0; i < num_files; ++i) |
| 3943 | { |
| 3944 | p = home_replace_save(NULL, files[i]); |
| 3945 | if (p != NULL) |
| 3946 | { |
| 3947 | vim_free(files[i]); |
| 3948 | files[i] = p; |
| 3949 | } |
| 3950 | } |
| 3951 | } |
| 3952 | } |
| 3953 | |
| 3954 | /* |
| 3955 | * Show all matches for completion on the command line. |
| 3956 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 3957 | * be inserted like a normal character. |
| 3958 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3959 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3960 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3961 | { |
| 3962 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 3963 | int num_files; |
| 3964 | char_u **files_found; |
| 3965 | int i, j, k; |
| 3966 | int maxlen; |
| 3967 | int lines; |
| 3968 | int columns; |
| 3969 | char_u *p; |
| 3970 | int lastlen; |
| 3971 | int attr; |
| 3972 | int showtail; |
| 3973 | |
| 3974 | if (xp->xp_numfiles == -1) |
| 3975 | { |
| 3976 | set_expand_context(xp); |
| 3977 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 3978 | &num_files, &files_found); |
| 3979 | showtail = expand_showtail(xp); |
| 3980 | if (i != EXPAND_OK) |
| 3981 | return i; |
| 3982 | |
| 3983 | } |
| 3984 | else |
| 3985 | { |
| 3986 | num_files = xp->xp_numfiles; |
| 3987 | files_found = xp->xp_files; |
| 3988 | showtail = cmd_showtail; |
| 3989 | } |
| 3990 | |
| 3991 | #ifdef FEAT_WILDMENU |
| 3992 | if (!wildmenu) |
| 3993 | { |
| 3994 | #endif |
| 3995 | msg_didany = FALSE; /* lines_left will be set */ |
| 3996 | msg_start(); /* prepare for paging */ |
| 3997 | msg_putchar('\n'); |
| 3998 | out_flush(); |
| 3999 | cmdline_row = msg_row; |
| 4000 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4001 | msg_start(); /* prepare for paging */ |
| 4002 | #ifdef FEAT_WILDMENU |
| 4003 | } |
| 4004 | #endif |
| 4005 | |
| 4006 | if (got_int) |
| 4007 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4008 | #ifdef FEAT_WILDMENU |
| 4009 | else if (wildmenu) |
| 4010 | win_redr_status_matches(xp, num_files, files_found, 0, showtail); |
| 4011 | #endif |
| 4012 | else |
| 4013 | { |
| 4014 | /* find the length of the longest file name */ |
| 4015 | maxlen = 0; |
| 4016 | for (i = 0; i < num_files; ++i) |
| 4017 | { |
| 4018 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4019 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4021 | { |
| 4022 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4023 | j = vim_strsize(NameBuff); |
| 4024 | } |
| 4025 | else |
| 4026 | j = vim_strsize(L_SHOWFILE(i)); |
| 4027 | if (j > maxlen) |
| 4028 | maxlen = j; |
| 4029 | } |
| 4030 | |
| 4031 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4032 | lines = num_files; |
| 4033 | else |
| 4034 | { |
| 4035 | /* compute the number of columns and lines for the listing */ |
| 4036 | maxlen += 2; /* two spaces between file names */ |
| 4037 | columns = ((int)Columns + 2) / maxlen; |
| 4038 | if (columns < 1) |
| 4039 | columns = 1; |
| 4040 | lines = (num_files + columns - 1) / columns; |
| 4041 | } |
| 4042 | |
| 4043 | attr = hl_attr(HLF_D); /* find out highlighting for directories */ |
| 4044 | |
| 4045 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4046 | { |
| 4047 | MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T)); |
| 4048 | msg_clr_eos(); |
| 4049 | msg_advance(maxlen - 3); |
| 4050 | MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T)); |
| 4051 | } |
| 4052 | |
| 4053 | /* list the files line by line */ |
| 4054 | for (i = 0; i < lines; ++i) |
| 4055 | { |
| 4056 | lastlen = 999; |
| 4057 | for (k = i; k < num_files; k += lines) |
| 4058 | { |
| 4059 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4060 | { |
| 4061 | msg_outtrans_attr(files_found[k], hl_attr(HLF_D)); |
| 4062 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4063 | msg_advance(maxlen + 1); |
| 4064 | msg_puts(p); |
| 4065 | msg_advance(maxlen + 3); |
| 4066 | msg_puts_long_attr(p + 2, hl_attr(HLF_D)); |
| 4067 | break; |
| 4068 | } |
| 4069 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4070 | msg_putchar(' '); |
| 4071 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4072 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4073 | || xp->xp_context == EXPAND_BUFFERS) |
| 4074 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4075 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4076 | if (xp->xp_numfiles != -1) |
| 4077 | { |
| 4078 | char_u *halved_slash; |
| 4079 | char_u *exp_path; |
| 4080 | |
| 4081 | /* Expansion was done before and special characters |
| 4082 | * were escaped, need to halve backslashes. Also |
| 4083 | * $HOME has been replaced with ~/. */ |
| 4084 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4085 | halved_slash = backslash_halve_save( |
| 4086 | exp_path != NULL ? exp_path : files_found[k]); |
| 4087 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4088 | : files_found[k]); |
| 4089 | vim_free(exp_path); |
| 4090 | vim_free(halved_slash); |
| 4091 | } |
| 4092 | else |
| 4093 | /* Expansion was done here, file names are literal. */ |
| 4094 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | if (showtail) |
| 4096 | p = L_SHOWFILE(k); |
| 4097 | else |
| 4098 | { |
| 4099 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4100 | TRUE); |
| 4101 | p = NameBuff; |
| 4102 | } |
| 4103 | } |
| 4104 | else |
| 4105 | { |
| 4106 | j = FALSE; |
| 4107 | p = L_SHOWFILE(k); |
| 4108 | } |
| 4109 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4110 | } |
| 4111 | if (msg_col > 0) /* when not wrapped around */ |
| 4112 | { |
| 4113 | msg_clr_eos(); |
| 4114 | msg_putchar('\n'); |
| 4115 | } |
| 4116 | out_flush(); /* show one line at a time */ |
| 4117 | if (got_int) |
| 4118 | { |
| 4119 | got_int = FALSE; |
| 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | /* |
| 4125 | * we redraw the command below the lines that we have just listed |
| 4126 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4127 | */ |
| 4128 | cmdline_row = msg_row; /* will put it back later */ |
| 4129 | } |
| 4130 | |
| 4131 | if (xp->xp_numfiles == -1) |
| 4132 | FreeWild(num_files, files_found); |
| 4133 | |
| 4134 | return EXPAND_OK; |
| 4135 | } |
| 4136 | |
| 4137 | /* |
| 4138 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4139 | * Find tail of file name path, but ignore trailing "/". |
| 4140 | */ |
| 4141 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4142 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | { |
| 4144 | char_u *p; |
| 4145 | char_u *t = s; |
| 4146 | int had_sep = FALSE; |
| 4147 | |
| 4148 | for (p = s; *p != NUL; ) |
| 4149 | { |
| 4150 | if (vim_ispathsep(*p) |
| 4151 | #ifdef BACKSLASH_IN_FILENAME |
| 4152 | && !rem_backslash(p) |
| 4153 | #endif |
| 4154 | ) |
| 4155 | had_sep = TRUE; |
| 4156 | else if (had_sep) |
| 4157 | { |
| 4158 | t = p; |
| 4159 | had_sep = FALSE; |
| 4160 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4161 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4162 | } |
| 4163 | return t; |
| 4164 | } |
| 4165 | |
| 4166 | /* |
| 4167 | * Return TRUE if we only need to show the tail of completion matches. |
| 4168 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4169 | * returned. |
| 4170 | */ |
| 4171 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4172 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4173 | { |
| 4174 | char_u *s; |
| 4175 | char_u *end; |
| 4176 | |
| 4177 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4178 | if (xp->xp_context != EXPAND_FILES |
| 4179 | && xp->xp_context != EXPAND_SHELLCMD |
| 4180 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4181 | return FALSE; |
| 4182 | |
| 4183 | end = gettail(xp->xp_pattern); |
| 4184 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4185 | return FALSE; |
| 4186 | |
| 4187 | for (s = xp->xp_pattern; s < end; s++) |
| 4188 | { |
| 4189 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4190 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4191 | if (rem_backslash(s)) |
| 4192 | ++s; |
| 4193 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4194 | return FALSE; |
| 4195 | } |
| 4196 | return TRUE; |
| 4197 | } |
| 4198 | |
| 4199 | /* |
| 4200 | * Prepare a string for expansion. |
| 4201 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4202 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4203 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4204 | * the name into allocated memory and prepend "^". |
| 4205 | */ |
| 4206 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4207 | addstar( |
| 4208 | char_u *fname, |
| 4209 | int len, |
| 4210 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | { |
| 4212 | char_u *retval; |
| 4213 | int i, j; |
| 4214 | int new_len; |
| 4215 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4216 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4218 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4219 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4220 | && context != EXPAND_SHELLCMD |
| 4221 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4222 | { |
| 4223 | /* |
| 4224 | * Matching will be done internally (on something other than files). |
| 4225 | * So we convert the file-matching-type wildcards into our kind for |
| 4226 | * use with vim_regcomp(). First work out how long it will be: |
| 4227 | */ |
| 4228 | |
| 4229 | /* For help tags the translation is done in find_help_tags(). |
| 4230 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4231 | if (context == EXPAND_HELP |
| 4232 | || context == EXPAND_COLORS |
| 4233 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4234 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4235 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4236 | || context == EXPAND_PACKADD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4237 | || (context == EXPAND_TAGS && fname[0] == '/')) |
| 4238 | retval = vim_strnsave(fname, len); |
| 4239 | else |
| 4240 | { |
| 4241 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4242 | for (i = 0; i < len; i++) |
| 4243 | { |
| 4244 | if (fname[i] == '*' || fname[i] == '~') |
| 4245 | new_len++; /* '*' needs to be replaced by ".*" |
| 4246 | '~' needs to be replaced by "\~" */ |
| 4247 | |
| 4248 | /* Buffer names are like file names. "." should be literal */ |
| 4249 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4250 | new_len++; /* "." becomes "\." */ |
| 4251 | |
| 4252 | /* Custom expansion takes care of special things, match |
| 4253 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4254 | if ((context == EXPAND_USER_DEFINED |
| 4255 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | new_len++; /* '\' becomes "\\" */ |
| 4257 | } |
| 4258 | retval = alloc(new_len); |
| 4259 | if (retval != NULL) |
| 4260 | { |
| 4261 | retval[0] = '^'; |
| 4262 | j = 1; |
| 4263 | for (i = 0; i < len; i++, j++) |
| 4264 | { |
| 4265 | /* Skip backslash. But why? At least keep it for custom |
| 4266 | * expansion. */ |
| 4267 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4268 | && context != EXPAND_USER_LIST |
| 4269 | && fname[i] == '\\' |
| 4270 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4271 | break; |
| 4272 | |
| 4273 | switch (fname[i]) |
| 4274 | { |
| 4275 | case '*': retval[j++] = '.'; |
| 4276 | break; |
| 4277 | case '~': retval[j++] = '\\'; |
| 4278 | break; |
| 4279 | case '?': retval[j] = '.'; |
| 4280 | continue; |
| 4281 | case '.': if (context == EXPAND_BUFFERS) |
| 4282 | retval[j++] = '\\'; |
| 4283 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4284 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4285 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | retval[j++] = '\\'; |
| 4287 | break; |
| 4288 | } |
| 4289 | retval[j] = fname[i]; |
| 4290 | } |
| 4291 | retval[j] = NUL; |
| 4292 | } |
| 4293 | } |
| 4294 | } |
| 4295 | else |
| 4296 | { |
| 4297 | retval = alloc(len + 4); |
| 4298 | if (retval != NULL) |
| 4299 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4300 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | |
| 4302 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4303 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4304 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4305 | * ~ would be at the start of the file name, but not the tail. |
| 4306 | * $ could be anywhere in the tail. |
| 4307 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4308 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | */ |
| 4310 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4311 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4312 | #ifndef BACKSLASH_IN_FILENAME |
| 4313 | for (i = len - 2; i >= 0; --i) |
| 4314 | { |
| 4315 | if (retval[i] != '\\') |
| 4316 | break; |
| 4317 | ends_in_star = !ends_in_star; |
| 4318 | } |
| 4319 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4320 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4321 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4322 | && vim_strchr(tail, '$') == NULL |
| 4323 | && vim_strchr(retval, '`') == NULL) |
| 4324 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4325 | else if (len > 0 && retval[len - 1] == '$') |
| 4326 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4327 | retval[len] = NUL; |
| 4328 | } |
| 4329 | } |
| 4330 | return retval; |
| 4331 | } |
| 4332 | |
| 4333 | /* |
| 4334 | * Must parse the command line so far to work out what context we are in. |
| 4335 | * Completion can then be done based on that context. |
| 4336 | * This routine sets the variables: |
| 4337 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4338 | * the command line (ends at the cursor). |
| 4339 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4340 | * |
| 4341 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4342 | * the command line, like an unknown command. Caller |
| 4343 | * should beep. |
| 4344 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4345 | * a normal char, rather than for completion. eg |
| 4346 | * :s/^I/ |
| 4347 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4348 | * it. |
| 4349 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4350 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4351 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4352 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4353 | * when we know only directories are of interest. eg |
| 4354 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4355 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4356 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4357 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4358 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4359 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4360 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4361 | * EXPAND_EVENTS Complete event names |
| 4362 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4363 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4364 | * EXPAND_AUGROUP Complete autocommand group names |
| 4365 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4366 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4367 | * eg :unmap a^I , :cunab x^I |
| 4368 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4369 | * eg :call sub^I |
| 4370 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4371 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4372 | * names in expressions, eg :while s^I |
| 4373 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4374 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4375 | */ |
| 4376 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4377 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4379 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4380 | if (ccline.cmdfirstc != ':' |
| 4381 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4382 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4383 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4384 | #endif |
| 4385 | ) |
| 4386 | { |
| 4387 | xp->xp_context = EXPAND_NOTHING; |
| 4388 | return; |
| 4389 | } |
| 4390 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos); |
| 4391 | } |
| 4392 | |
| 4393 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4394 | set_cmd_context( |
| 4395 | expand_T *xp, |
| 4396 | char_u *str, /* start of command line */ |
| 4397 | int len, /* length of command line (excl. NUL) */ |
| 4398 | int col) /* position of cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4399 | { |
| 4400 | int old_char = NUL; |
| 4401 | char_u *nextcomm; |
| 4402 | |
| 4403 | /* |
| 4404 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4405 | * written before. |
| 4406 | */ |
| 4407 | if (col < len) |
| 4408 | old_char = str[col]; |
| 4409 | str[col] = NUL; |
| 4410 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4411 | |
| 4412 | #ifdef FEAT_EVAL |
| 4413 | if (ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4414 | { |
| 4415 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4416 | /* pass CMD_SIZE because there is no real command */ |
| 4417 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4418 | # endif |
| 4419 | } |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4420 | else if (ccline.input_fn) |
| 4421 | { |
| 4422 | xp->xp_context = ccline.xp_context; |
| 4423 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4424 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4425 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4426 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4427 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4428 | else |
| 4429 | #endif |
| 4430 | while (nextcomm != NULL) |
| 4431 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4432 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4433 | /* Store the string here so that call_user_expand_func() can get to them |
| 4434 | * easily. */ |
| 4435 | xp->xp_line = str; |
| 4436 | xp->xp_col = col; |
| 4437 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4438 | str[col] = old_char; |
| 4439 | } |
| 4440 | |
| 4441 | /* |
| 4442 | * Expand the command line "str" from context "xp". |
| 4443 | * "xp" must have been set by set_cmd_context(). |
| 4444 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4445 | * starts. |
| 4446 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4447 | * cursor. |
| 4448 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4449 | * key that triggered expansion literally. |
| 4450 | * Returns EXPAND_OK otherwise. |
| 4451 | */ |
| 4452 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4453 | expand_cmdline( |
| 4454 | expand_T *xp, |
| 4455 | char_u *str, /* start of command line */ |
| 4456 | int col, /* position of cursor */ |
| 4457 | int *matchcount, /* return: nr of matches */ |
| 4458 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4459 | { |
| 4460 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4461 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | |
| 4463 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4464 | { |
| 4465 | beep_flush(); |
| 4466 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4467 | } |
| 4468 | if (xp->xp_context == EXPAND_NOTHING) |
| 4469 | { |
| 4470 | /* Caller can use the character as a normal char instead */ |
| 4471 | return EXPAND_NOTHING; |
| 4472 | } |
| 4473 | |
| 4474 | /* add star to file name, or convert to regexp if not exp. files. */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4475 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4476 | file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4477 | if (file_str == NULL) |
| 4478 | return EXPAND_UNSUCCESSFUL; |
| 4479 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4480 | if (p_wic) |
| 4481 | options += WILD_ICASE; |
| 4482 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4483 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4484 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4485 | { |
| 4486 | *matchcount = 0; |
| 4487 | *matches = NULL; |
| 4488 | } |
| 4489 | vim_free(file_str); |
| 4490 | |
| 4491 | return EXPAND_OK; |
| 4492 | } |
| 4493 | |
| 4494 | #ifdef FEAT_MULTI_LANG |
| 4495 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4496 | * Cleanup matches for help tags: |
| 4497 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4498 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4499 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4500 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4501 | |
| 4502 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4503 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | { |
| 4505 | int i, j; |
| 4506 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4507 | char_u buf[4]; |
| 4508 | char_u *p = buf; |
| 4509 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4510 | if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n')) |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4511 | { |
| 4512 | *p++ = '@'; |
| 4513 | *p++ = p_hlg[0]; |
| 4514 | *p++ = p_hlg[1]; |
| 4515 | } |
| 4516 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | |
| 4518 | for (i = 0; i < num_file; ++i) |
| 4519 | { |
| 4520 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4521 | if (len <= 0) |
| 4522 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4523 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4524 | { |
| 4525 | /* Sorting on priority means the same item in another language may |
| 4526 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4527 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4528 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4529 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4530 | break; |
| 4531 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4532 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | file[i][len] = NUL; |
| 4534 | } |
| 4535 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4536 | |
| 4537 | if (*buf != NUL) |
| 4538 | for (i = 0; i < num_file; ++i) |
| 4539 | { |
| 4540 | len = (int)STRLEN(file[i]) - 3; |
| 4541 | if (len <= 0) |
| 4542 | continue; |
| 4543 | if (STRCMP(file[i] + len, buf) == 0) |
| 4544 | { |
| 4545 | /* remove the default language */ |
| 4546 | file[i][len] = NUL; |
| 4547 | } |
| 4548 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | } |
| 4550 | #endif |
| 4551 | |
| 4552 | /* |
| 4553 | * Do the expansion based on xp->xp_context and "pat". |
| 4554 | */ |
| 4555 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4556 | ExpandFromContext( |
| 4557 | expand_T *xp, |
| 4558 | char_u *pat, |
| 4559 | int *num_file, |
| 4560 | char_u ***file, |
| 4561 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4562 | { |
| 4563 | #ifdef FEAT_CMDL_COMPL |
| 4564 | regmatch_T regmatch; |
| 4565 | #endif |
| 4566 | int ret; |
| 4567 | int flags; |
| 4568 | |
| 4569 | flags = EW_DIR; /* include directories */ |
| 4570 | if (options & WILD_LIST_NOTFOUND) |
| 4571 | flags |= EW_NOTFOUND; |
| 4572 | if (options & WILD_ADD_SLASH) |
| 4573 | flags |= EW_ADDSLASH; |
| 4574 | if (options & WILD_KEEP_ALL) |
| 4575 | flags |= EW_KEEPALL; |
| 4576 | if (options & WILD_SILENT) |
| 4577 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4578 | if (options & WILD_ALLLINKS) |
| 4579 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4580 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4581 | if (xp->xp_context == EXPAND_FILES |
| 4582 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4583 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4584 | { |
| 4585 | /* |
| 4586 | * Expand file or directory names. |
| 4587 | */ |
| 4588 | int free_pat = FALSE; |
| 4589 | int i; |
| 4590 | |
| 4591 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4592 | * space */ |
| 4593 | if (xp->xp_backslash != XP_BS_NONE) |
| 4594 | { |
| 4595 | free_pat = TRUE; |
| 4596 | pat = vim_strsave(pat); |
| 4597 | for (i = 0; pat[i]; ++i) |
| 4598 | if (pat[i] == '\\') |
| 4599 | { |
| 4600 | if (xp->xp_backslash == XP_BS_THREE |
| 4601 | && pat[i + 1] == '\\' |
| 4602 | && pat[i + 2] == '\\' |
| 4603 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4604 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4605 | if (xp->xp_backslash == XP_BS_ONE |
| 4606 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4607 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | if (xp->xp_context == EXPAND_FILES) |
| 4612 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4613 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4614 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4615 | else |
| 4616 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4617 | if (options & WILD_ICASE) |
| 4618 | flags |= EW_ICASE; |
| 4619 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4620 | /* Expand wildcards, supporting %:h and the like. */ |
| 4621 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | if (free_pat) |
| 4623 | vim_free(pat); |
| 4624 | return ret; |
| 4625 | } |
| 4626 | |
| 4627 | *file = (char_u **)""; |
| 4628 | *num_file = 0; |
| 4629 | if (xp->xp_context == EXPAND_HELP) |
| 4630 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4631 | /* With an empty argument we would get all the help tags, which is |
| 4632 | * very slow. Get matches for "help" instead. */ |
| 4633 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4634 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4635 | { |
| 4636 | #ifdef FEAT_MULTI_LANG |
| 4637 | cleanup_help_tags(*num_file, *file); |
| 4638 | #endif |
| 4639 | return OK; |
| 4640 | } |
| 4641 | return FAIL; |
| 4642 | } |
| 4643 | |
| 4644 | #ifndef FEAT_CMDL_COMPL |
| 4645 | return FAIL; |
| 4646 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4647 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4648 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4649 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4650 | return ExpandOldSetting(num_file, file); |
| 4651 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4652 | return ExpandBufnames(pat, num_file, file, options); |
| 4653 | if (xp->xp_context == EXPAND_TAGS |
| 4654 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4655 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4656 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4657 | { |
| 4658 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4659 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4660 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4661 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4662 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4663 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4664 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4665 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4666 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4667 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4668 | { |
| 4669 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4670 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4671 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4672 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4673 | { |
| 4674 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4675 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4676 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4677 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4678 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4679 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4680 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4681 | if (xp->xp_context == EXPAND_PACKADD) |
| 4682 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4683 | |
| 4684 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4685 | if (regmatch.regprog == NULL) |
| 4686 | return FAIL; |
| 4687 | |
| 4688 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4689 | regmatch.rm_ic = ignorecase(pat); |
| 4690 | |
| 4691 | if (xp->xp_context == EXPAND_SETTINGS |
| 4692 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4693 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4694 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4695 | ret = ExpandMappings(®match, num_file, file); |
| 4696 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4697 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4698 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4699 | # endif |
| 4700 | else |
| 4701 | { |
| 4702 | static struct expgen |
| 4703 | { |
| 4704 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4705 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4706 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4707 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4708 | } tab[] = |
| 4709 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4710 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4711 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4712 | #ifdef FEAT_CMDHIST |
| 4713 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4714 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4715 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4716 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4717 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4718 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4719 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4720 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | #endif |
| 4722 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4723 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4724 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4725 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4726 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4727 | #endif |
| 4728 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4729 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4730 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4731 | #endif |
| 4732 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4733 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4734 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4735 | #ifdef FEAT_PROFILE |
| 4736 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4737 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4738 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4739 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4740 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4741 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4742 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4743 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4744 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4745 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4746 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4747 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4748 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4749 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4750 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4751 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4752 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4753 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4754 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 4755 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4756 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4757 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4758 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | }; |
| 4760 | int i; |
| 4761 | |
| 4762 | /* |
| 4763 | * Find a context in the table and call the ExpandGeneric() with the |
| 4764 | * right function to do the expansion. |
| 4765 | */ |
| 4766 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 4767 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4768 | if (xp->xp_context == tab[i].context) |
| 4769 | { |
| 4770 | if (tab[i].ic) |
| 4771 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4772 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 4773 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4774 | break; |
| 4775 | } |
| 4776 | } |
| 4777 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4778 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | |
| 4780 | return ret; |
| 4781 | #endif /* FEAT_CMDL_COMPL */ |
| 4782 | } |
| 4783 | |
| 4784 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4785 | /* |
| 4786 | * Expand a list of names. |
| 4787 | * |
| 4788 | * Generic function for command line completion. It calls a function to |
| 4789 | * obtain strings, one by one. The strings are matched against a regexp |
| 4790 | * program. Matching strings are copied into an array, which is returned. |
| 4791 | * |
| 4792 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 4793 | */ |
| 4794 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4795 | ExpandGeneric( |
| 4796 | expand_T *xp, |
| 4797 | regmatch_T *regmatch, |
| 4798 | int *num_file, |
| 4799 | char_u ***file, |
| 4800 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4801 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4802 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4803 | { |
| 4804 | int i; |
| 4805 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4806 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4807 | char_u *str; |
| 4808 | |
| 4809 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4810 | * round == 0: count the number of matching names |
| 4811 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4812 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4813 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4814 | { |
| 4815 | for (i = 0; ; ++i) |
| 4816 | { |
| 4817 | str = (*func)(xp, i); |
| 4818 | if (str == NULL) /* end of list */ |
| 4819 | break; |
| 4820 | if (*str == NUL) /* skip empty strings */ |
| 4821 | continue; |
| 4822 | |
| 4823 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 4824 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4825 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4827 | if (escaped) |
| 4828 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 4829 | else |
| 4830 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | (*file)[count] = str; |
| 4832 | #ifdef FEAT_MENU |
| 4833 | if (func == get_menu_names && str != NULL) |
| 4834 | { |
| 4835 | /* test for separator added by get_menu_names() */ |
| 4836 | str += STRLEN(str) - 1; |
| 4837 | if (*str == '\001') |
| 4838 | *str = '.'; |
| 4839 | } |
| 4840 | #endif |
| 4841 | } |
| 4842 | ++count; |
| 4843 | } |
| 4844 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4845 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4846 | { |
| 4847 | if (count == 0) |
| 4848 | return OK; |
| 4849 | *num_file = count; |
| 4850 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 4851 | if (*file == NULL) |
| 4852 | { |
| 4853 | *file = (char_u **)""; |
| 4854 | return FAIL; |
| 4855 | } |
| 4856 | count = 0; |
| 4857 | } |
| 4858 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4859 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 4860 | /* Sort the results. Keep menu's in the specified order. */ |
| 4861 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 4862 | { |
| 4863 | if (xp->xp_context == EXPAND_EXPRESSION |
| 4864 | || xp->xp_context == EXPAND_FUNCTIONS |
| 4865 | || xp->xp_context == EXPAND_USER_FUNC) |
| 4866 | /* <SNR> functions should be sorted to the end. */ |
| 4867 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 4868 | sort_func_compare); |
| 4869 | else |
| 4870 | sort_strings(*file, *num_file); |
| 4871 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4872 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4873 | #ifdef FEAT_CMDL_COMPL |
| 4874 | /* Reset the variables used for special highlight names expansion, so that |
| 4875 | * they don't show up when getting normal highlight names by ID. */ |
| 4876 | reset_expand_highlight(); |
| 4877 | #endif |
| 4878 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4879 | return OK; |
| 4880 | } |
| 4881 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4882 | /* |
| 4883 | * Complete a shell command. |
| 4884 | * Returns FAIL or OK; |
| 4885 | */ |
| 4886 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4887 | expand_shellcmd( |
| 4888 | char_u *filepat, /* pattern to match with command names */ |
| 4889 | int *num_file, /* return: number of matches */ |
| 4890 | char_u ***file, /* return: array with matches */ |
| 4891 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4892 | { |
| 4893 | char_u *pat; |
| 4894 | int i; |
| 4895 | char_u *path; |
| 4896 | int mustfree = FALSE; |
| 4897 | garray_T ga; |
| 4898 | char_u *buf = alloc(MAXPATHL); |
| 4899 | size_t l; |
| 4900 | char_u *s, *e; |
| 4901 | int flags = flagsarg; |
| 4902 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4903 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4904 | |
| 4905 | if (buf == NULL) |
| 4906 | return FAIL; |
| 4907 | |
| 4908 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4909 | * space */ |
| 4910 | pat = vim_strsave(filepat); |
| 4911 | for (i = 0; pat[i]; ++i) |
| 4912 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4913 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4914 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4915 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4916 | |
| 4917 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 4918 | if (mch_isFullName(pat)) |
| 4919 | path = (char_u *)" "; |
| 4920 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4921 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 4922 | path = (char_u *)"."; |
| 4923 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 4924 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4925 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 4926 | if (path == NULL) |
| 4927 | path = (char_u *)""; |
| 4928 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4929 | |
| 4930 | /* |
| 4931 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4932 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 4933 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4934 | */ |
| 4935 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4936 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4937 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4938 | if (*s == NUL) |
| 4939 | { |
| 4940 | if (did_curdir) |
| 4941 | break; |
| 4942 | /* Find directories in the current directory, path is empty. */ |
| 4943 | did_curdir = TRUE; |
| 4944 | } |
| 4945 | else if (*s == '.') |
| 4946 | did_curdir = TRUE; |
| 4947 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 4948 | if (*s == ' ') |
| 4949 | ++s; /* Skip space used for absolute path name. */ |
| 4950 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 4951 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4952 | e = vim_strchr(s, ';'); |
| 4953 | #else |
| 4954 | e = vim_strchr(s, ':'); |
| 4955 | #endif |
| 4956 | if (e == NULL) |
| 4957 | e = s + STRLEN(s); |
| 4958 | |
| 4959 | l = e - s; |
| 4960 | if (l > MAXPATHL - 5) |
| 4961 | break; |
| 4962 | vim_strncpy(buf, s, l); |
| 4963 | add_pathsep(buf); |
| 4964 | l = STRLEN(buf); |
| 4965 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 4966 | |
| 4967 | /* Expand matches in one directory of $PATH. */ |
| 4968 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 4969 | if (ret == OK) |
| 4970 | { |
| 4971 | if (ga_grow(&ga, *num_file) == FAIL) |
| 4972 | FreeWild(*num_file, *file); |
| 4973 | else |
| 4974 | { |
| 4975 | for (i = 0; i < *num_file; ++i) |
| 4976 | { |
| 4977 | s = (*file)[i]; |
| 4978 | if (STRLEN(s) > l) |
| 4979 | { |
| 4980 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4981 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4982 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 4983 | } |
| 4984 | else |
| 4985 | vim_free(s); |
| 4986 | } |
| 4987 | vim_free(*file); |
| 4988 | } |
| 4989 | } |
| 4990 | if (*e != NUL) |
| 4991 | ++e; |
| 4992 | } |
| 4993 | *file = ga.ga_data; |
| 4994 | *num_file = ga.ga_len; |
| 4995 | |
| 4996 | vim_free(buf); |
| 4997 | vim_free(pat); |
| 4998 | if (mustfree) |
| 4999 | vim_free(path); |
| 5000 | return OK; |
| 5001 | } |
| 5002 | |
| 5003 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5004 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5005 | static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, char_u **, int), expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 5006 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5007 | /* |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5008 | * Call "user_expand_func()" to invoke a user defined VimL function and return |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5009 | * the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5011 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5012 | call_user_expand_func( |
| 5013 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5014 | expand_T *xp, |
| 5015 | int *num_file, |
| 5016 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5017 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5018 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5019 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5020 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5021 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5022 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5023 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5024 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5025 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5026 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | *num_file = 0; |
| 5028 | *file = NULL; |
| 5029 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5030 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5031 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5032 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5033 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5034 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5035 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5036 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5037 | args[1] = xp->xp_line; |
| 5038 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5039 | args[2] = num; |
| 5040 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5041 | /* Save the cmdline, we don't know what the function may do. */ |
| 5042 | save_ccline = ccline; |
| 5043 | ccline.cmdbuff = NULL; |
| 5044 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5045 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5046 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5047 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5048 | |
| 5049 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5051 | if (ccline.cmdbuff != NULL) |
| 5052 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5053 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5054 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5055 | return ret; |
| 5056 | } |
| 5057 | |
| 5058 | /* |
| 5059 | * Expand names with a function defined by the user. |
| 5060 | */ |
| 5061 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5062 | ExpandUserDefined( |
| 5063 | expand_T *xp, |
| 5064 | regmatch_T *regmatch, |
| 5065 | int *num_file, |
| 5066 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5067 | { |
| 5068 | char_u *retstr; |
| 5069 | char_u *s; |
| 5070 | char_u *e; |
| 5071 | char_u keep; |
| 5072 | garray_T ga; |
| 5073 | |
| 5074 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5075 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5076 | return FAIL; |
| 5077 | |
| 5078 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5079 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5080 | { |
| 5081 | e = vim_strchr(s, '\n'); |
| 5082 | if (e == NULL) |
| 5083 | e = s + STRLEN(s); |
| 5084 | keep = *e; |
| 5085 | *e = 0; |
| 5086 | |
| 5087 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 5088 | { |
| 5089 | *e = keep; |
| 5090 | if (*e != NUL) |
| 5091 | ++e; |
| 5092 | continue; |
| 5093 | } |
| 5094 | |
| 5095 | if (ga_grow(&ga, 1) == FAIL) |
| 5096 | break; |
| 5097 | |
| 5098 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5099 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5100 | |
| 5101 | *e = keep; |
| 5102 | if (*e != NUL) |
| 5103 | ++e; |
| 5104 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5105 | vim_free(retstr); |
| 5106 | *file = ga.ga_data; |
| 5107 | *num_file = ga.ga_len; |
| 5108 | return OK; |
| 5109 | } |
| 5110 | |
| 5111 | /* |
| 5112 | * Expand names with a list returned by a function defined by the user. |
| 5113 | */ |
| 5114 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5115 | ExpandUserList( |
| 5116 | expand_T *xp, |
| 5117 | int *num_file, |
| 5118 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5119 | { |
| 5120 | list_T *retlist; |
| 5121 | listitem_T *li; |
| 5122 | garray_T ga; |
| 5123 | |
| 5124 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5125 | if (retlist == NULL) |
| 5126 | return FAIL; |
| 5127 | |
| 5128 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5129 | /* Loop over the items in the list. */ |
| 5130 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5131 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5132 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5133 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5134 | |
| 5135 | if (ga_grow(&ga, 1) == FAIL) |
| 5136 | break; |
| 5137 | |
| 5138 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5139 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5140 | ++ga.ga_len; |
| 5141 | } |
| 5142 | list_unref(retlist); |
| 5143 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5144 | *file = ga.ga_data; |
| 5145 | *num_file = ga.ga_len; |
| 5146 | return OK; |
| 5147 | } |
| 5148 | #endif |
| 5149 | |
| 5150 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5151 | * Expand color scheme, compiler or filetype names. |
| 5152 | * Search from 'runtimepath': |
| 5153 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5154 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5155 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5156 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5157 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5158 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5159 | */ |
| 5160 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5161 | ExpandRTDir( |
| 5162 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5163 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5164 | int *num_file, |
| 5165 | char_u ***file, |
| 5166 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5167 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5168 | char_u *s; |
| 5169 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5170 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5171 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5172 | int i; |
| 5173 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5174 | |
| 5175 | *num_file = 0; |
| 5176 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5177 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5178 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5179 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5180 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5181 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5182 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5183 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5184 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5185 | ga_clear_strings(&ga); |
| 5186 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5187 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5188 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5189 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5190 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5191 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5192 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5193 | if (flags & DIP_START) { |
| 5194 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5195 | { |
| 5196 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5197 | if (s == NULL) |
| 5198 | { |
| 5199 | ga_clear_strings(&ga); |
| 5200 | return FAIL; |
| 5201 | } |
| 5202 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5203 | globpath(p_pp, s, &ga, 0); |
| 5204 | vim_free(s); |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | if (flags & DIP_OPT) { |
| 5209 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5210 | { |
| 5211 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5212 | if (s == NULL) |
| 5213 | { |
| 5214 | ga_clear_strings(&ga); |
| 5215 | return FAIL; |
| 5216 | } |
| 5217 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5218 | globpath(p_pp, s, &ga, 0); |
| 5219 | vim_free(s); |
| 5220 | } |
| 5221 | } |
| 5222 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5223 | for (i = 0; i < ga.ga_len; ++i) |
| 5224 | { |
| 5225 | match = ((char_u **)ga.ga_data)[i]; |
| 5226 | s = match; |
| 5227 | e = s + STRLEN(s); |
| 5228 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5229 | { |
| 5230 | e -= 4; |
| 5231 | for (s = e; s > match; mb_ptr_back(match, s)) |
| 5232 | if (s < match || vim_ispathsep(*s)) |
| 5233 | break; |
| 5234 | ++s; |
| 5235 | *e = NUL; |
| 5236 | mch_memmove(match, s, e - s + 1); |
| 5237 | } |
| 5238 | } |
| 5239 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5240 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5241 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5242 | |
| 5243 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5244 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5245 | remove_duplicates(&ga); |
| 5246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5247 | *file = ga.ga_data; |
| 5248 | *num_file = ga.ga_len; |
| 5249 | return OK; |
| 5250 | } |
| 5251 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5252 | /* |
| 5253 | * Expand loadplugin names: |
| 5254 | * 'packpath'/pack/ * /opt/{pat} |
| 5255 | */ |
| 5256 | static int |
| 5257 | ExpandPackAddDir( |
| 5258 | char_u *pat, |
| 5259 | int *num_file, |
| 5260 | char_u ***file) |
| 5261 | { |
| 5262 | char_u *s; |
| 5263 | char_u *e; |
| 5264 | char_u *match; |
| 5265 | garray_T ga; |
| 5266 | int i; |
| 5267 | int pat_len; |
| 5268 | |
| 5269 | *num_file = 0; |
| 5270 | *file = NULL; |
| 5271 | pat_len = (int)STRLEN(pat); |
| 5272 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5273 | |
| 5274 | s = alloc((unsigned)(pat_len + 26)); |
| 5275 | if (s == NULL) |
| 5276 | { |
| 5277 | ga_clear_strings(&ga); |
| 5278 | return FAIL; |
| 5279 | } |
| 5280 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5281 | globpath(p_pp, s, &ga, 0); |
| 5282 | vim_free(s); |
| 5283 | |
| 5284 | for (i = 0; i < ga.ga_len; ++i) |
| 5285 | { |
| 5286 | match = ((char_u **)ga.ga_data)[i]; |
| 5287 | s = gettail(match); |
| 5288 | e = s + STRLEN(s); |
| 5289 | mch_memmove(match, s, e - s + 1); |
| 5290 | } |
| 5291 | |
| 5292 | if (ga.ga_len == 0) |
| 5293 | return FAIL; |
| 5294 | |
| 5295 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5296 | * directories in dirnames. */ |
| 5297 | remove_duplicates(&ga); |
| 5298 | |
| 5299 | *file = ga.ga_data; |
| 5300 | *num_file = ga.ga_len; |
| 5301 | return OK; |
| 5302 | } |
| 5303 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5304 | #endif |
| 5305 | |
| 5306 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5307 | /* |
| 5308 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5309 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5310 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5311 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5312 | globpath( |
| 5313 | char_u *path, |
| 5314 | char_u *file, |
| 5315 | garray_T *ga, |
| 5316 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5317 | { |
| 5318 | expand_T xpc; |
| 5319 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5320 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | int num_p; |
| 5322 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5323 | |
| 5324 | buf = alloc(MAXPATHL); |
| 5325 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5326 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5327 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5328 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5330 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5331 | /* Loop over all entries in {path}. */ |
| 5332 | while (*path != NUL) |
| 5333 | { |
| 5334 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5335 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5336 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5337 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5338 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5339 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5340 | * treat it as an escape character, use '/' instead. */ |
| 5341 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5342 | STRCAT(buf, "/"); |
| 5343 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5344 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5345 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5346 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5347 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5348 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5349 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5350 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5352 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5353 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5354 | for (i = 0; i < num_p; ++i) |
| 5355 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5356 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5357 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5358 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5359 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5360 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5361 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5362 | FreeWild(num_p, p); |
| 5363 | } |
| 5364 | } |
| 5365 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5366 | |
| 5367 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | #endif |
| 5371 | |
| 5372 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5373 | |
| 5374 | /********************************* |
| 5375 | * Command line history stuff * |
| 5376 | *********************************/ |
| 5377 | |
| 5378 | /* |
| 5379 | * Translate a history character to the associated type number. |
| 5380 | */ |
| 5381 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5382 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5383 | { |
| 5384 | if (c == ':') |
| 5385 | return HIST_CMD; |
| 5386 | if (c == '=') |
| 5387 | return HIST_EXPR; |
| 5388 | if (c == '@') |
| 5389 | return HIST_INPUT; |
| 5390 | if (c == '>') |
| 5391 | return HIST_DEBUG; |
| 5392 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5393 | } |
| 5394 | |
| 5395 | /* |
| 5396 | * Table of history names. |
| 5397 | * These names are used in :history and various hist...() functions. |
| 5398 | * It is sufficient to give the significant prefix of a history name. |
| 5399 | */ |
| 5400 | |
| 5401 | static char *(history_names[]) = |
| 5402 | { |
| 5403 | "cmd", |
| 5404 | "search", |
| 5405 | "expr", |
| 5406 | "input", |
| 5407 | "debug", |
| 5408 | NULL |
| 5409 | }; |
| 5410 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5411 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5412 | /* |
| 5413 | * Function given to ExpandGeneric() to obtain the possible first |
| 5414 | * arguments of the ":history command. |
| 5415 | */ |
| 5416 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5417 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5418 | { |
| 5419 | static char_u compl[2] = { NUL, NUL }; |
| 5420 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5421 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5422 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5423 | |
| 5424 | if (idx < short_names_count) |
| 5425 | { |
| 5426 | compl[0] = (char_u)short_names[idx]; |
| 5427 | return compl; |
| 5428 | } |
| 5429 | if (idx < short_names_count + history_name_count) |
| 5430 | return (char_u *)history_names[idx - short_names_count]; |
| 5431 | if (idx == short_names_count + history_name_count) |
| 5432 | return (char_u *)"all"; |
| 5433 | return NULL; |
| 5434 | } |
| 5435 | #endif |
| 5436 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5437 | /* |
| 5438 | * init_history() - Initialize the command line history. |
| 5439 | * Also used to re-allocate the history when the size changes. |
| 5440 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5441 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5442 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | { |
| 5444 | int newlen; /* new length of history table */ |
| 5445 | histentry_T *temp; |
| 5446 | int i; |
| 5447 | int j; |
| 5448 | int type; |
| 5449 | |
| 5450 | /* |
| 5451 | * If size of history table changed, reallocate it |
| 5452 | */ |
| 5453 | newlen = (int)p_hi; |
| 5454 | if (newlen != hislen) /* history length changed */ |
| 5455 | { |
| 5456 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5457 | { |
| 5458 | if (newlen) |
| 5459 | { |
| 5460 | temp = (histentry_T *)lalloc( |
| 5461 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5462 | if (temp == NULL) /* out of memory! */ |
| 5463 | { |
| 5464 | if (type == 0) /* first one: just keep the old length */ |
| 5465 | { |
| 5466 | newlen = hislen; |
| 5467 | break; |
| 5468 | } |
| 5469 | /* Already changed one table, now we can only have zero |
| 5470 | * length for all tables. */ |
| 5471 | newlen = 0; |
| 5472 | type = -1; |
| 5473 | continue; |
| 5474 | } |
| 5475 | } |
| 5476 | else |
| 5477 | temp = NULL; |
| 5478 | if (newlen == 0 || temp != NULL) |
| 5479 | { |
| 5480 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5481 | { |
| 5482 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5483 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5484 | } |
| 5485 | else if (newlen > hislen) /* array becomes bigger */ |
| 5486 | { |
| 5487 | for (i = 0; i <= hisidx[type]; ++i) |
| 5488 | temp[i] = history[type][i]; |
| 5489 | j = i; |
| 5490 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5491 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5492 | for ( ; j < hislen; ++i, ++j) |
| 5493 | temp[i] = history[type][j]; |
| 5494 | } |
| 5495 | else /* array becomes smaller or 0 */ |
| 5496 | { |
| 5497 | j = hisidx[type]; |
| 5498 | for (i = newlen - 1; ; --i) |
| 5499 | { |
| 5500 | if (i >= 0) /* copy newest entries */ |
| 5501 | temp[i] = history[type][j]; |
| 5502 | else /* remove older entries */ |
| 5503 | vim_free(history[type][j].hisstr); |
| 5504 | if (--j < 0) |
| 5505 | j = hislen - 1; |
| 5506 | if (j == hisidx[type]) |
| 5507 | break; |
| 5508 | } |
| 5509 | hisidx[type] = newlen - 1; |
| 5510 | } |
| 5511 | vim_free(history[type]); |
| 5512 | history[type] = temp; |
| 5513 | } |
| 5514 | } |
| 5515 | hislen = newlen; |
| 5516 | } |
| 5517 | } |
| 5518 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5519 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5520 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5521 | { |
| 5522 | hisptr->hisnum = 0; |
| 5523 | hisptr->viminfo = FALSE; |
| 5524 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5525 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5526 | } |
| 5527 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5528 | /* |
| 5529 | * Check if command line 'str' is already in history. |
| 5530 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5531 | */ |
| 5532 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5533 | in_history( |
| 5534 | int type, |
| 5535 | char_u *str, |
| 5536 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5537 | int sep, |
| 5538 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5539 | { |
| 5540 | int i; |
| 5541 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5542 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5543 | |
| 5544 | if (hisidx[type] < 0) |
| 5545 | return FALSE; |
| 5546 | i = hisidx[type]; |
| 5547 | do |
| 5548 | { |
| 5549 | if (history[type][i].hisstr == NULL) |
| 5550 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5551 | |
| 5552 | /* For search history, check that the separator character matches as |
| 5553 | * well. */ |
| 5554 | p = history[type][i].hisstr; |
| 5555 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5556 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5557 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5558 | { |
| 5559 | if (!move_to_front) |
| 5560 | return TRUE; |
| 5561 | last_i = i; |
| 5562 | break; |
| 5563 | } |
| 5564 | if (--i < 0) |
| 5565 | i = hislen - 1; |
| 5566 | } while (i != hisidx[type]); |
| 5567 | |
| 5568 | if (last_i >= 0) |
| 5569 | { |
| 5570 | str = history[type][i].hisstr; |
| 5571 | while (i != hisidx[type]) |
| 5572 | { |
| 5573 | if (++i >= hislen) |
| 5574 | i = 0; |
| 5575 | history[type][last_i] = history[type][i]; |
| 5576 | last_i = i; |
| 5577 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5578 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5579 | history[type][i].viminfo = FALSE; |
| 5580 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5581 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5582 | return TRUE; |
| 5583 | } |
| 5584 | return FALSE; |
| 5585 | } |
| 5586 | |
| 5587 | /* |
| 5588 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5589 | * When "name" is empty, return "cmd" history. |
| 5590 | * Returns -1 for unknown history name. |
| 5591 | */ |
| 5592 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5593 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5594 | { |
| 5595 | int i; |
| 5596 | int len = (int)STRLEN(name); |
| 5597 | |
| 5598 | /* No argument: use current history. */ |
| 5599 | if (len == 0) |
| 5600 | return hist_char2type(ccline.cmdfirstc); |
| 5601 | |
| 5602 | for (i = 0; history_names[i] != NULL; ++i) |
| 5603 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5604 | return i; |
| 5605 | |
| 5606 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5607 | return hist_char2type(name[0]); |
| 5608 | |
| 5609 | return -1; |
| 5610 | } |
| 5611 | |
| 5612 | static int last_maptick = -1; /* last seen maptick */ |
| 5613 | |
| 5614 | /* |
| 5615 | * Add the given string to the given history. If the string is already in the |
| 5616 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5617 | * values. |
| 5618 | */ |
| 5619 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5620 | add_to_history( |
| 5621 | int histype, |
| 5622 | char_u *new_entry, |
| 5623 | int in_map, /* consider maptick when inside a mapping */ |
| 5624 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5625 | { |
| 5626 | histentry_T *hisptr; |
| 5627 | int len; |
| 5628 | |
| 5629 | if (hislen == 0) /* no history */ |
| 5630 | return; |
| 5631 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5632 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5633 | return; |
| 5634 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5635 | /* |
| 5636 | * Searches inside the same mapping overwrite each other, so that only |
| 5637 | * the last line is kept. Be careful not to remove a line that was moved |
| 5638 | * down, only lines that were added. |
| 5639 | */ |
| 5640 | if (histype == HIST_SEARCH && in_map) |
| 5641 | { |
| 5642 | if (maptick == last_maptick) |
| 5643 | { |
| 5644 | /* Current line is from the same mapping, remove it */ |
| 5645 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5646 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5647 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | --hisnum[histype]; |
| 5649 | if (--hisidx[HIST_SEARCH] < 0) |
| 5650 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5651 | } |
| 5652 | last_maptick = -1; |
| 5653 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5654 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5655 | { |
| 5656 | if (++hisidx[histype] == hislen) |
| 5657 | hisidx[histype] = 0; |
| 5658 | hisptr = &history[histype][hisidx[histype]]; |
| 5659 | vim_free(hisptr->hisstr); |
| 5660 | |
| 5661 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5662 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5663 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5664 | if (hisptr->hisstr != NULL) |
| 5665 | hisptr->hisstr[len + 1] = sep; |
| 5666 | |
| 5667 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5668 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5669 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5670 | if (histype == HIST_SEARCH && in_map) |
| 5671 | last_maptick = maptick; |
| 5672 | } |
| 5673 | } |
| 5674 | |
| 5675 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5676 | |
| 5677 | /* |
| 5678 | * Get identifier of newest history entry. |
| 5679 | * "histype" may be one of the HIST_ values. |
| 5680 | */ |
| 5681 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5682 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5683 | { |
| 5684 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5685 | || hisidx[histype] < 0) |
| 5686 | return -1; |
| 5687 | |
| 5688 | return history[histype][hisidx[histype]].hisnum; |
| 5689 | } |
| 5690 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5691 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5692 | * Calculate history index from a number: |
| 5693 | * num > 0: seen as identifying number of a history entry |
| 5694 | * num < 0: relative position in history wrt newest entry |
| 5695 | * "histype" may be one of the HIST_ values. |
| 5696 | */ |
| 5697 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5698 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5699 | { |
| 5700 | int i; |
| 5701 | histentry_T *hist; |
| 5702 | int wrapped = FALSE; |
| 5703 | |
| 5704 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5705 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5706 | return -1; |
| 5707 | |
| 5708 | hist = history[histype]; |
| 5709 | if (num > 0) |
| 5710 | { |
| 5711 | while (hist[i].hisnum > num) |
| 5712 | if (--i < 0) |
| 5713 | { |
| 5714 | if (wrapped) |
| 5715 | break; |
| 5716 | i += hislen; |
| 5717 | wrapped = TRUE; |
| 5718 | } |
| 5719 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5720 | return i; |
| 5721 | } |
| 5722 | else if (-num <= hislen) |
| 5723 | { |
| 5724 | i += num + 1; |
| 5725 | if (i < 0) |
| 5726 | i += hislen; |
| 5727 | if (hist[i].hisstr != NULL) |
| 5728 | return i; |
| 5729 | } |
| 5730 | return -1; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
| 5734 | * Get a history entry by its index. |
| 5735 | * "histype" may be one of the HIST_ values. |
| 5736 | */ |
| 5737 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5738 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5739 | { |
| 5740 | idx = calc_hist_idx(histype, idx); |
| 5741 | if (idx >= 0) |
| 5742 | return history[histype][idx].hisstr; |
| 5743 | else |
| 5744 | return (char_u *)""; |
| 5745 | } |
| 5746 | |
| 5747 | /* |
| 5748 | * Clear all entries of a history. |
| 5749 | * "histype" may be one of the HIST_ values. |
| 5750 | */ |
| 5751 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5752 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5753 | { |
| 5754 | int i; |
| 5755 | histentry_T *hisptr; |
| 5756 | |
| 5757 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5758 | { |
| 5759 | hisptr = history[histype]; |
| 5760 | for (i = hislen; i--;) |
| 5761 | { |
| 5762 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5763 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 5764 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5765 | } |
| 5766 | hisidx[histype] = -1; /* mark history as cleared */ |
| 5767 | hisnum[histype] = 0; /* reset identifier counter */ |
| 5768 | return OK; |
| 5769 | } |
| 5770 | return FAIL; |
| 5771 | } |
| 5772 | |
| 5773 | /* |
| 5774 | * Remove all entries matching {str} from a history. |
| 5775 | * "histype" may be one of the HIST_ values. |
| 5776 | */ |
| 5777 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5778 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | { |
| 5780 | regmatch_T regmatch; |
| 5781 | histentry_T *hisptr; |
| 5782 | int idx; |
| 5783 | int i; |
| 5784 | int last; |
| 5785 | int found = FALSE; |
| 5786 | |
| 5787 | regmatch.regprog = NULL; |
| 5788 | regmatch.rm_ic = FALSE; /* always match case */ |
| 5789 | if (hislen != 0 |
| 5790 | && histype >= 0 |
| 5791 | && histype < HIST_COUNT |
| 5792 | && *str != NUL |
| 5793 | && (idx = hisidx[histype]) >= 0 |
| 5794 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 5795 | != NULL) |
| 5796 | { |
| 5797 | i = last = idx; |
| 5798 | do |
| 5799 | { |
| 5800 | hisptr = &history[histype][i]; |
| 5801 | if (hisptr->hisstr == NULL) |
| 5802 | break; |
| 5803 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 5804 | { |
| 5805 | found = TRUE; |
| 5806 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5807 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5808 | } |
| 5809 | else |
| 5810 | { |
| 5811 | if (i != last) |
| 5812 | { |
| 5813 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5814 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5815 | } |
| 5816 | if (--last < 0) |
| 5817 | last += hislen; |
| 5818 | } |
| 5819 | if (--i < 0) |
| 5820 | i += hislen; |
| 5821 | } while (i != idx); |
| 5822 | if (history[histype][idx].hisstr == NULL) |
| 5823 | hisidx[histype] = -1; |
| 5824 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5825 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5826 | return found; |
| 5827 | } |
| 5828 | |
| 5829 | /* |
| 5830 | * Remove an indexed entry from a history. |
| 5831 | * "histype" may be one of the HIST_ values. |
| 5832 | */ |
| 5833 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5834 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5835 | { |
| 5836 | int i, j; |
| 5837 | |
| 5838 | i = calc_hist_idx(histype, idx); |
| 5839 | if (i < 0) |
| 5840 | return FALSE; |
| 5841 | idx = hisidx[histype]; |
| 5842 | vim_free(history[histype][i].hisstr); |
| 5843 | |
| 5844 | /* When deleting the last added search string in a mapping, reset |
| 5845 | * last_maptick, so that the last added search string isn't deleted again. |
| 5846 | */ |
| 5847 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 5848 | last_maptick = -1; |
| 5849 | |
| 5850 | while (i != idx) |
| 5851 | { |
| 5852 | j = (i + 1) % hislen; |
| 5853 | history[histype][i] = history[histype][j]; |
| 5854 | i = j; |
| 5855 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5856 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5857 | if (--i < 0) |
| 5858 | i += hislen; |
| 5859 | hisidx[histype] = i; |
| 5860 | return TRUE; |
| 5861 | } |
| 5862 | |
| 5863 | #endif /* FEAT_EVAL */ |
| 5864 | |
| 5865 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 5866 | /* |
| 5867 | * Very specific function to remove the value in ":set key=val" from the |
| 5868 | * history. |
| 5869 | */ |
| 5870 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5871 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | { |
| 5873 | char_u *p; |
| 5874 | int i; |
| 5875 | |
| 5876 | i = hisidx[HIST_CMD]; |
| 5877 | if (i < 0) |
| 5878 | return; |
| 5879 | p = history[HIST_CMD][i].hisstr; |
| 5880 | if (p != NULL) |
| 5881 | for ( ; *p; ++p) |
| 5882 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 5883 | { |
| 5884 | p = vim_strchr(p + 3, '='); |
| 5885 | if (p == NULL) |
| 5886 | break; |
| 5887 | ++p; |
| 5888 | for (i = 0; p[i] && !vim_iswhite(p[i]); ++i) |
| 5889 | if (p[i] == '\\' && p[i + 1]) |
| 5890 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5891 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5892 | --p; |
| 5893 | } |
| 5894 | } |
| 5895 | #endif |
| 5896 | |
| 5897 | #endif /* FEAT_CMDHIST */ |
| 5898 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 5899 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 5900 | /* |
| 5901 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 5902 | * ccline and put the previous value in prev_ccline. |
| 5903 | */ |
| 5904 | static struct cmdline_info * |
| 5905 | get_ccline_ptr(void) |
| 5906 | { |
| 5907 | if ((State & CMDLINE) == 0) |
| 5908 | return NULL; |
| 5909 | if (ccline.cmdbuff != NULL) |
| 5910 | return &ccline; |
| 5911 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 5912 | return &prev_ccline; |
| 5913 | return NULL; |
| 5914 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 5915 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 5916 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 5917 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 5918 | /* |
| 5919 | * Get the current command line in allocated memory. |
| 5920 | * Only works when the command line is being edited. |
| 5921 | * Returns NULL when something is wrong. |
| 5922 | */ |
| 5923 | char_u * |
| 5924 | get_cmdline_str(void) |
| 5925 | { |
| 5926 | struct cmdline_info *p = get_ccline_ptr(); |
| 5927 | |
| 5928 | if (p == NULL) |
| 5929 | return NULL; |
| 5930 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 5931 | } |
| 5932 | |
| 5933 | /* |
| 5934 | * Get the current command line position, counted in bytes. |
| 5935 | * Zero is the first position. |
| 5936 | * Only works when the command line is being edited. |
| 5937 | * Returns -1 when something is wrong. |
| 5938 | */ |
| 5939 | int |
| 5940 | get_cmdline_pos(void) |
| 5941 | { |
| 5942 | struct cmdline_info *p = get_ccline_ptr(); |
| 5943 | |
| 5944 | if (p == NULL) |
| 5945 | return -1; |
| 5946 | return p->cmdpos; |
| 5947 | } |
| 5948 | |
| 5949 | /* |
| 5950 | * Set the command line byte position to "pos". Zero is the first position. |
| 5951 | * Only works when the command line is being edited. |
| 5952 | * Returns 1 when failed, 0 when OK. |
| 5953 | */ |
| 5954 | int |
| 5955 | set_cmdline_pos( |
| 5956 | int pos) |
| 5957 | { |
| 5958 | struct cmdline_info *p = get_ccline_ptr(); |
| 5959 | |
| 5960 | if (p == NULL) |
| 5961 | return 1; |
| 5962 | |
| 5963 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 5964 | * changed the command line. */ |
| 5965 | if (pos < 0) |
| 5966 | new_cmdpos = 0; |
| 5967 | else |
| 5968 | new_cmdpos = pos; |
| 5969 | return 0; |
| 5970 | } |
| 5971 | #endif |
| 5972 | |
| 5973 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 5974 | /* |
| 5975 | * Get the current command-line type. |
| 5976 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 5977 | * Only works when the command line is being edited. |
| 5978 | * Returns NUL when something is wrong. |
| 5979 | */ |
| 5980 | int |
| 5981 | get_cmdline_type(void) |
| 5982 | { |
| 5983 | struct cmdline_info *p = get_ccline_ptr(); |
| 5984 | |
| 5985 | if (p == NULL) |
| 5986 | return NUL; |
| 5987 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 5988 | return |
| 5989 | # ifdef FEAT_EVAL |
| 5990 | (p->input_fn) ? '@' : |
| 5991 | # endif |
| 5992 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 5993 | return p->cmdfirstc; |
| 5994 | } |
| 5995 | #endif |
| 5996 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5997 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 5998 | /* |
| 5999 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6000 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6001 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6002 | */ |
| 6003 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6004 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6005 | { |
| 6006 | int len; |
| 6007 | int first = FALSE; |
| 6008 | long num; |
| 6009 | |
| 6010 | *str = skipwhite(*str); |
| 6011 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6012 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6013 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6014 | *str += len; |
| 6015 | *num1 = (int)num; |
| 6016 | first = TRUE; |
| 6017 | } |
| 6018 | *str = skipwhite(*str); |
| 6019 | if (**str == ',') /* parse "to" part of range */ |
| 6020 | { |
| 6021 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6022 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6023 | if (len > 0) |
| 6024 | { |
| 6025 | *num2 = (int)num; |
| 6026 | *str = skipwhite(*str + len); |
| 6027 | } |
| 6028 | else if (!first) /* no number given at all */ |
| 6029 | return FAIL; |
| 6030 | } |
| 6031 | else if (first) /* only one number given */ |
| 6032 | *num2 = *num1; |
| 6033 | return OK; |
| 6034 | } |
| 6035 | #endif |
| 6036 | |
| 6037 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6038 | /* |
| 6039 | * :history command - print a history |
| 6040 | */ |
| 6041 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6042 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6043 | { |
| 6044 | histentry_T *hist; |
| 6045 | int histype1 = HIST_CMD; |
| 6046 | int histype2 = HIST_CMD; |
| 6047 | int hisidx1 = 1; |
| 6048 | int hisidx2 = -1; |
| 6049 | int idx; |
| 6050 | int i, j, k; |
| 6051 | char_u *end; |
| 6052 | char_u *arg = eap->arg; |
| 6053 | |
| 6054 | if (hislen == 0) |
| 6055 | { |
| 6056 | MSG(_("'history' option is zero")); |
| 6057 | return; |
| 6058 | } |
| 6059 | |
| 6060 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6061 | { |
| 6062 | end = arg; |
| 6063 | while (ASCII_ISALPHA(*end) |
| 6064 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6065 | end++; |
| 6066 | i = *end; |
| 6067 | *end = NUL; |
| 6068 | histype1 = get_histtype(arg); |
| 6069 | if (histype1 == -1) |
| 6070 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6071 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6072 | { |
| 6073 | histype1 = 0; |
| 6074 | histype2 = HIST_COUNT-1; |
| 6075 | } |
| 6076 | else |
| 6077 | { |
| 6078 | *end = i; |
| 6079 | EMSG(_(e_trailing)); |
| 6080 | return; |
| 6081 | } |
| 6082 | } |
| 6083 | else |
| 6084 | histype2 = histype1; |
| 6085 | *end = i; |
| 6086 | } |
| 6087 | else |
| 6088 | end = arg; |
| 6089 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6090 | { |
| 6091 | EMSG(_(e_trailing)); |
| 6092 | return; |
| 6093 | } |
| 6094 | |
| 6095 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6096 | { |
| 6097 | STRCPY(IObuff, "\n # "); |
| 6098 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6099 | MSG_PUTS_TITLE(IObuff); |
| 6100 | idx = hisidx[histype1]; |
| 6101 | hist = history[histype1]; |
| 6102 | j = hisidx1; |
| 6103 | k = hisidx2; |
| 6104 | if (j < 0) |
| 6105 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6106 | if (k < 0) |
| 6107 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6108 | if (idx >= 0 && j <= k) |
| 6109 | for (i = idx + 1; !got_int; ++i) |
| 6110 | { |
| 6111 | if (i == hislen) |
| 6112 | i = 0; |
| 6113 | if (hist[i].hisstr != NULL |
| 6114 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6115 | { |
| 6116 | msg_putchar('\n'); |
| 6117 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6118 | hist[i].hisnum); |
| 6119 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6120 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6121 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6122 | else |
| 6123 | STRCAT(IObuff, hist[i].hisstr); |
| 6124 | msg_outtrans(IObuff); |
| 6125 | out_flush(); |
| 6126 | } |
| 6127 | if (i == idx) |
| 6128 | break; |
| 6129 | } |
| 6130 | } |
| 6131 | } |
| 6132 | #endif |
| 6133 | |
| 6134 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6135 | /* |
| 6136 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6137 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6138 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6139 | {NULL, NULL, NULL, NULL, NULL}; |
| 6140 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6141 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6142 | static int viminfo_add_at_front = FALSE; |
| 6143 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6144 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6145 | |
| 6146 | /* |
| 6147 | * Translate a history type number to the associated character. |
| 6148 | */ |
| 6149 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6150 | hist_type2char( |
| 6151 | int type, |
| 6152 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6153 | { |
| 6154 | if (type == HIST_CMD) |
| 6155 | return ':'; |
| 6156 | if (type == HIST_SEARCH) |
| 6157 | { |
| 6158 | if (use_question) |
| 6159 | return '?'; |
| 6160 | else |
| 6161 | return '/'; |
| 6162 | } |
| 6163 | if (type == HIST_EXPR) |
| 6164 | return '='; |
| 6165 | return '@'; |
| 6166 | } |
| 6167 | |
| 6168 | /* |
| 6169 | * Prepare for reading the history from the viminfo file. |
| 6170 | * This allocates history arrays to store the read history lines. |
| 6171 | */ |
| 6172 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6173 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6174 | { |
| 6175 | int i; |
| 6176 | int num; |
| 6177 | int type; |
| 6178 | int len; |
| 6179 | |
| 6180 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6181 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6182 | if (asklen > hislen) |
| 6183 | asklen = hislen; |
| 6184 | |
| 6185 | for (type = 0; type < HIST_COUNT; ++type) |
| 6186 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6187 | /* Count the number of empty spaces in the history list. Entries read |
| 6188 | * from viminfo previously are also considered empty. If there are |
| 6189 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6190 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6191 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | num++; |
| 6193 | len = asklen; |
| 6194 | if (num > len) |
| 6195 | len = num; |
| 6196 | if (len <= 0) |
| 6197 | viminfo_history[type] = NULL; |
| 6198 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6199 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6200 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6201 | if (viminfo_history[type] == NULL) |
| 6202 | len = 0; |
| 6203 | viminfo_hislen[type] = len; |
| 6204 | viminfo_hisidx[type] = 0; |
| 6205 | } |
| 6206 | } |
| 6207 | |
| 6208 | /* |
| 6209 | * Accept a line from the viminfo, store it in the history array when it's |
| 6210 | * new. |
| 6211 | */ |
| 6212 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6213 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | { |
| 6215 | int type; |
| 6216 | long_u len; |
| 6217 | char_u *val; |
| 6218 | char_u *p; |
| 6219 | |
| 6220 | type = hist_char2type(virp->vir_line[0]); |
| 6221 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6222 | { |
| 6223 | val = viminfo_readstring(virp, 1, TRUE); |
| 6224 | if (val != NULL && *val != NUL) |
| 6225 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6226 | int sep = (*val == ' ' ? NUL : *val); |
| 6227 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6228 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6229 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | { |
| 6231 | /* Need to re-allocate to append the separator byte. */ |
| 6232 | len = STRLEN(val); |
| 6233 | p = lalloc(len + 2, TRUE); |
| 6234 | if (p != NULL) |
| 6235 | { |
| 6236 | if (type == HIST_SEARCH) |
| 6237 | { |
| 6238 | /* Search entry: Move the separator from the first |
| 6239 | * column to after the NUL. */ |
| 6240 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6241 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6242 | } |
| 6243 | else |
| 6244 | { |
| 6245 | /* Not a search entry: No separator in the viminfo |
| 6246 | * file, add a NUL separator. */ |
| 6247 | mch_memmove(p, val, (size_t)len + 1); |
| 6248 | p[len + 1] = NUL; |
| 6249 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6250 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6251 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6252 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6253 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6254 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6255 | } |
| 6256 | } |
| 6257 | } |
| 6258 | vim_free(val); |
| 6259 | } |
| 6260 | return viminfo_readline(virp); |
| 6261 | } |
| 6262 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6263 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6264 | * Accept a new style history line from the viminfo, store it in the history |
| 6265 | * array when it's new. |
| 6266 | */ |
| 6267 | void |
| 6268 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6269 | garray_T *values, |
| 6270 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6271 | { |
| 6272 | int type; |
| 6273 | long_u len; |
| 6274 | char_u *val; |
| 6275 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6276 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6277 | |
| 6278 | /* Check the format: |
| 6279 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6280 | if (values->ga_len < 4 |
| 6281 | || vp[0].bv_type != BVAL_NR |
| 6282 | || vp[1].bv_type != BVAL_NR |
| 6283 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6284 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6285 | return; |
| 6286 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6287 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6288 | if (type >= HIST_COUNT) |
| 6289 | return; |
| 6290 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6291 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6292 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6293 | if (val != NULL && *val != NUL) |
| 6294 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6295 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6296 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6297 | int idx; |
| 6298 | int overwrite = FALSE; |
| 6299 | |
| 6300 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6301 | { |
| 6302 | /* If lines were written by an older Vim we need to avoid |
| 6303 | * getting duplicates. See if the entry already exists. */ |
| 6304 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6305 | { |
| 6306 | p = viminfo_history[type][idx].hisstr; |
| 6307 | if (STRCMP(val, p) == 0 |
| 6308 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6309 | { |
| 6310 | overwrite = TRUE; |
| 6311 | break; |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | if (!overwrite) |
| 6316 | { |
| 6317 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6318 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6319 | p = lalloc(len + 2, TRUE); |
| 6320 | } |
| 6321 | if (p != NULL) |
| 6322 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6323 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6324 | if (!overwrite) |
| 6325 | { |
| 6326 | mch_memmove(p, val, (size_t)len + 1); |
| 6327 | /* Put the separator after the NUL. */ |
| 6328 | p[len + 1] = sep; |
| 6329 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6330 | viminfo_history[type][idx].hisnum = 0; |
| 6331 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6332 | viminfo_hisidx[type]++; |
| 6333 | } |
| 6334 | } |
| 6335 | } |
| 6336 | } |
| 6337 | } |
| 6338 | } |
| 6339 | |
| 6340 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6341 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6342 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6343 | static void |
| 6344 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | { |
| 6346 | int idx; |
| 6347 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6348 | |
| 6349 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6350 | if (idx >= hislen) |
| 6351 | idx -= hislen; |
| 6352 | else if (idx < 0) |
| 6353 | idx = hislen - 1; |
| 6354 | if (viminfo_add_at_front) |
| 6355 | hisidx[type] = idx; |
| 6356 | else |
| 6357 | { |
| 6358 | if (hisidx[type] == -1) |
| 6359 | hisidx[type] = hislen - 1; |
| 6360 | do |
| 6361 | { |
| 6362 | if (history[type][idx].hisstr != NULL |
| 6363 | || history[type][idx].viminfo) |
| 6364 | break; |
| 6365 | if (++idx == hislen) |
| 6366 | idx = 0; |
| 6367 | } while (idx != hisidx[type]); |
| 6368 | if (idx != hisidx[type] && --idx < 0) |
| 6369 | idx = hislen - 1; |
| 6370 | } |
| 6371 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6372 | { |
| 6373 | vim_free(history[type][idx].hisstr); |
| 6374 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6375 | history[type][idx].viminfo = TRUE; |
| 6376 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6377 | if (--idx < 0) |
| 6378 | idx = hislen - 1; |
| 6379 | } |
| 6380 | idx += 1; |
| 6381 | idx %= hislen; |
| 6382 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6383 | { |
| 6384 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6385 | idx %= hislen; |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6390 | static int |
| 6391 | #ifdef __BORLANDC__ |
| 6392 | _RTLENTRYF |
| 6393 | #endif |
| 6394 | sort_hist(const void *s1, const void *s2) |
| 6395 | { |
| 6396 | histentry_T *p1 = *(histentry_T **)s1; |
| 6397 | histentry_T *p2 = *(histentry_T **)s2; |
| 6398 | |
| 6399 | if (p1->time_set < p2->time_set) return -1; |
| 6400 | if (p1->time_set > p2->time_set) return 1; |
| 6401 | return 0; |
| 6402 | } |
| 6403 | #endif |
| 6404 | |
| 6405 | /* |
| 6406 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6407 | * timestamp; |
| 6408 | */ |
| 6409 | static void |
| 6410 | merge_history(int type) |
| 6411 | { |
| 6412 | int max_len; |
| 6413 | histentry_T **tot_hist; |
| 6414 | histentry_T *new_hist; |
| 6415 | int i; |
| 6416 | int len; |
| 6417 | |
| 6418 | /* Make one long list with all entries. */ |
| 6419 | max_len = hislen + viminfo_hisidx[type]; |
| 6420 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6421 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6422 | if (tot_hist == NULL || new_hist == NULL) |
| 6423 | { |
| 6424 | vim_free(tot_hist); |
| 6425 | vim_free(new_hist); |
| 6426 | return; |
| 6427 | } |
| 6428 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6429 | tot_hist[i] = &viminfo_history[type][i]; |
| 6430 | len = i; |
| 6431 | for (i = 0; i < hislen; i++) |
| 6432 | if (history[type][i].hisstr != NULL) |
| 6433 | tot_hist[len++] = &history[type][i]; |
| 6434 | |
| 6435 | /* Sort the list on timestamp. */ |
| 6436 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6437 | |
| 6438 | /* Keep the newest ones. */ |
| 6439 | for (i = 0; i < hislen; i++) |
| 6440 | { |
| 6441 | if (i < len) |
| 6442 | { |
| 6443 | new_hist[i] = *tot_hist[i]; |
| 6444 | tot_hist[i]->hisstr = NULL; |
| 6445 | if (new_hist[i].hisnum == 0) |
| 6446 | new_hist[i].hisnum = ++hisnum[type]; |
| 6447 | } |
| 6448 | else |
| 6449 | clear_hist_entry(&new_hist[i]); |
| 6450 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6451 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6452 | |
| 6453 | /* Free what is not kept. */ |
| 6454 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6455 | vim_free(viminfo_history[type][i].hisstr); |
| 6456 | for (i = 0; i < hislen; i++) |
| 6457 | vim_free(history[type][i].hisstr); |
| 6458 | vim_free(history[type]); |
| 6459 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6460 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | /* |
| 6464 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6465 | */ |
| 6466 | void |
| 6467 | finish_viminfo_history(vir_T *virp) |
| 6468 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6469 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6470 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6471 | |
| 6472 | for (type = 0; type < HIST_COUNT; ++type) |
| 6473 | { |
| 6474 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6475 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6476 | |
| 6477 | if (merge) |
| 6478 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6479 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6480 | concat_history(type); |
| 6481 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6482 | vim_free(viminfo_history[type]); |
| 6483 | viminfo_history[type] = NULL; |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6484 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6485 | } |
| 6486 | } |
| 6487 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6488 | /* |
| 6489 | * Write history to viminfo file in "fp". |
| 6490 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6491 | * file, data is in viminfo_history[]. |
| 6492 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6493 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6494 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6495 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6496 | { |
| 6497 | int i; |
| 6498 | int type; |
| 6499 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6500 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6501 | |
| 6502 | init_history(); |
| 6503 | if (hislen == 0) |
| 6504 | return; |
| 6505 | for (type = 0; type < HIST_COUNT; ++type) |
| 6506 | { |
| 6507 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6508 | if (num_saved == 0) |
| 6509 | continue; |
| 6510 | if (num_saved < 0) /* Use default */ |
| 6511 | num_saved = hislen; |
| 6512 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6513 | type == HIST_CMD ? _("Command Line") : |
| 6514 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6515 | type == HIST_EXPR ? _("Expression") : |
| 6516 | type == HIST_INPUT ? _("Input Line") : |
| 6517 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6518 | if (num_saved > hislen) |
| 6519 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6520 | |
| 6521 | /* |
| 6522 | * Merge typed and viminfo history: |
| 6523 | * round 1: history of typed commands. |
| 6524 | * round 2: history from recently read viminfo. |
| 6525 | */ |
| 6526 | for (round = 1; round <= 2; ++round) |
| 6527 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6528 | if (round == 1) |
| 6529 | /* start at newest entry, somewhere in the list */ |
| 6530 | i = hisidx[type]; |
| 6531 | else if (viminfo_hisidx[type] > 0) |
| 6532 | /* start at newest entry, first in the list */ |
| 6533 | i = 0; |
| 6534 | else |
| 6535 | /* empty list */ |
| 6536 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6537 | if (i >= 0) |
| 6538 | while (num_saved > 0 |
| 6539 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6540 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6541 | char_u *p; |
| 6542 | time_t timestamp; |
| 6543 | int c = NUL; |
| 6544 | |
| 6545 | if (round == 1) |
| 6546 | { |
| 6547 | p = history[type][i].hisstr; |
| 6548 | timestamp = history[type][i].time_set; |
| 6549 | } |
| 6550 | else |
| 6551 | { |
| 6552 | p = viminfo_history[type] == NULL ? NULL |
| 6553 | : viminfo_history[type][i].hisstr; |
| 6554 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6555 | : viminfo_history[type][i].time_set; |
| 6556 | } |
| 6557 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6558 | if (p != NULL && (round == 2 |
| 6559 | || !merge |
| 6560 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6561 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6562 | --num_saved; |
| 6563 | fputc(hist_type2char(type, TRUE), fp); |
| 6564 | /* For the search history: put the separator in the |
| 6565 | * second column; use a space if there isn't one. */ |
| 6566 | if (type == HIST_SEARCH) |
| 6567 | { |
| 6568 | c = p[STRLEN(p) + 1]; |
| 6569 | putc(c == NUL ? ' ' : c, fp); |
| 6570 | } |
| 6571 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6572 | |
| 6573 | { |
| 6574 | char cbuf[NUMBUFLEN]; |
| 6575 | |
| 6576 | /* New style history with a bar line. Format: |
| 6577 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6578 | if (c == NUL) |
| 6579 | cbuf[0] = NUL; |
| 6580 | else |
| 6581 | sprintf(cbuf, "%d", c); |
| 6582 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6583 | type, (long)timestamp, cbuf); |
| 6584 | barline_writestring(fp, p, LSIZE - 20); |
| 6585 | putc('\n', fp); |
| 6586 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6587 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6588 | if (round == 1) |
| 6589 | { |
| 6590 | /* Decrement index, loop around and stop when back at |
| 6591 | * the start. */ |
| 6592 | if (--i < 0) |
| 6593 | i = hislen - 1; |
| 6594 | if (i == hisidx[type]) |
| 6595 | break; |
| 6596 | } |
| 6597 | else |
| 6598 | { |
| 6599 | /* Increment index. Stop at the end in the while. */ |
| 6600 | ++i; |
| 6601 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6602 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6603 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6604 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6605 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6606 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6607 | vim_free(viminfo_history[type]); |
| 6608 | viminfo_history[type] = NULL; |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6609 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | #endif /* FEAT_VIMINFO */ |
| 6613 | |
| 6614 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6615 | /* |
| 6616 | * Write a character at the current cursor+offset position. |
| 6617 | * It is directly written into the command buffer block. |
| 6618 | */ |
| 6619 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6620 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6621 | { |
| 6622 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6623 | { |
| 6624 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6625 | return; |
| 6626 | } |
| 6627 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6628 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6629 | } |
| 6630 | |
| 6631 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6632 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6633 | { |
| 6634 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6635 | { |
| 6636 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6637 | return NUL; |
| 6638 | } |
| 6639 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6640 | } |
| 6641 | #endif |
| 6642 | |
| 6643 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6644 | /* |
| 6645 | * Open a window on the current command line and history. Allow editing in |
| 6646 | * the window. Returns when the window is closed. |
| 6647 | * Returns: |
| 6648 | * CR if the command is to be executed |
| 6649 | * Ctrl_C if it is to be abandoned |
| 6650 | * K_IGNORE if editing continues |
| 6651 | */ |
| 6652 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6653 | ex_window(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6654 | { |
| 6655 | struct cmdline_info save_ccline; |
| 6656 | buf_T *old_curbuf = curbuf; |
| 6657 | win_T *old_curwin = curwin; |
| 6658 | buf_T *bp; |
| 6659 | win_T *wp; |
| 6660 | int i; |
| 6661 | linenr_T lnum; |
| 6662 | int histtype; |
| 6663 | garray_T winsizes; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6664 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6665 | char_u typestr[2]; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6666 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6667 | int save_restart_edit = restart_edit; |
| 6668 | int save_State = State; |
| 6669 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6670 | #ifdef FEAT_RIGHTLEFT |
| 6671 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6672 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6673 | #ifdef FEAT_FOLDING |
| 6674 | int save_KeyTyped; |
| 6675 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6676 | |
| 6677 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6678 | if (cmdwin_type != 0 |
| 6679 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6680 | || cmdline_star > 0 |
| 6681 | # endif |
| 6682 | ) |
| 6683 | { |
| 6684 | beep_flush(); |
| 6685 | return K_IGNORE; |
| 6686 | } |
| 6687 | |
| 6688 | /* Save current window sizes. */ |
| 6689 | win_size_save(&winsizes); |
| 6690 | |
| 6691 | # ifdef FEAT_AUTOCMD |
| 6692 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6693 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6694 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6695 | /* don't use a new tab page */ |
| 6696 | cmdmod.tab = 0; |
| 6697 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6698 | /* Create a window for the command-line buffer. */ |
| 6699 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6700 | { |
| 6701 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6702 | # ifdef FEAT_AUTOCMD |
| 6703 | unblock_autocmds(); |
| 6704 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6705 | return K_IGNORE; |
| 6706 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6707 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6708 | |
| 6709 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6710 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6711 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6712 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
| 6713 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 6714 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6715 | #ifdef FEAT_FOLDING |
| 6716 | curwin->w_p_fen = FALSE; |
| 6717 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6718 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6719 | curwin->w_p_rl = cmdmsg_rl; |
| 6720 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6722 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6723 | |
| 6724 | # ifdef FEAT_AUTOCMD |
| 6725 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6726 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6727 | # endif |
| 6728 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6729 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6730 | need_wait_return = FALSE; |
| 6731 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6732 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6733 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6734 | { |
| 6735 | if (p_wc == TAB) |
| 6736 | { |
| 6737 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6738 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6739 | } |
| 6740 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6741 | } |
| 6742 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6743 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6744 | * sets 'textwidth' to 78). */ |
| 6745 | curbuf->b_p_tw = 0; |
| 6746 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6747 | /* Fill the buffer with the history. */ |
| 6748 | init_history(); |
| 6749 | if (hislen > 0) |
| 6750 | { |
| 6751 | i = hisidx[histtype]; |
| 6752 | if (i >= 0) |
| 6753 | { |
| 6754 | lnum = 0; |
| 6755 | do |
| 6756 | { |
| 6757 | if (++i == hislen) |
| 6758 | i = 0; |
| 6759 | if (history[histtype][i].hisstr != NULL) |
| 6760 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6761 | (colnr_T)0, FALSE); |
| 6762 | } |
| 6763 | while (i != hisidx[histtype]); |
| 6764 | } |
| 6765 | } |
| 6766 | |
| 6767 | /* Replace the empty last line with the current command-line and put the |
| 6768 | * cursor there. */ |
| 6769 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 6770 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6771 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6772 | changed_line_abv_curs(); |
| 6773 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 6774 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6775 | |
| 6776 | /* Save the command line info, can be used recursively. */ |
| 6777 | save_ccline = ccline; |
| 6778 | ccline.cmdbuff = NULL; |
| 6779 | ccline.cmdprompt = NULL; |
| 6780 | |
| 6781 | /* No Ex mode here! */ |
| 6782 | exmode_active = 0; |
| 6783 | |
| 6784 | State = NORMAL; |
| 6785 | # ifdef FEAT_MOUSE |
| 6786 | setmouse(); |
| 6787 | # endif |
| 6788 | |
| 6789 | # ifdef FEAT_AUTOCMD |
| 6790 | /* Trigger CmdwinEnter autocommands. */ |
| 6791 | typestr[0] = cmdwin_type; |
| 6792 | typestr[1] = NUL; |
| 6793 | apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 6794 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 6795 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6796 | # endif |
| 6797 | |
| 6798 | i = RedrawingDisabled; |
| 6799 | RedrawingDisabled = 0; |
| 6800 | |
| 6801 | /* |
| 6802 | * Call the main loop until <CR> or CTRL-C is typed. |
| 6803 | */ |
| 6804 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 6805 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6806 | |
| 6807 | RedrawingDisabled = i; |
| 6808 | |
| 6809 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6810 | |
| 6811 | # ifdef FEAT_FOLDING |
| 6812 | save_KeyTyped = KeyTyped; |
| 6813 | # endif |
| 6814 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6815 | /* Trigger CmdwinLeave autocommands. */ |
| 6816 | apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6817 | |
| 6818 | # ifdef FEAT_FOLDING |
| 6819 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 6820 | KeyTyped = save_KeyTyped; |
| 6821 | # endif |
| 6822 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6823 | # endif |
| 6824 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 6825 | /* Restore the command line info. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6826 | ccline = save_ccline; |
| 6827 | cmdwin_type = 0; |
| 6828 | |
| 6829 | exmode_active = save_exmode; |
| 6830 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 6831 | /* Safety check: The old window or buffer was deleted: It's a bug when |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6832 | * this happens! */ |
| 6833 | if (!win_valid(old_curwin) || !buf_valid(old_curbuf)) |
| 6834 | { |
| 6835 | cmdwin_result = Ctrl_C; |
| 6836 | EMSG(_("E199: Active window or buffer deleted")); |
| 6837 | } |
| 6838 | else |
| 6839 | { |
| 6840 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 6841 | /* autocmds may abort script processing */ |
| 6842 | if (aborting() && cmdwin_result != K_IGNORE) |
| 6843 | cmdwin_result = Ctrl_C; |
| 6844 | # endif |
| 6845 | /* Set the new command line from the cmdline buffer. */ |
| 6846 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6847 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6848 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6849 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 6850 | |
| 6851 | if (histtype == HIST_CMD) |
| 6852 | { |
| 6853 | /* Execute the command directly. */ |
| 6854 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 6855 | cmdwin_result = CAR; |
| 6856 | } |
| 6857 | else |
| 6858 | { |
| 6859 | /* First need to cancel what we were doing. */ |
| 6860 | ccline.cmdbuff = NULL; |
| 6861 | stuffcharReadbuff(':'); |
| 6862 | stuffReadbuff((char_u *)p); |
| 6863 | stuffcharReadbuff(CAR); |
| 6864 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6865 | } |
| 6866 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 6867 | { |
| 6868 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 6869 | cmdwin_result = CAR; |
| 6870 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 6871 | else if (cmdwin_result == Ctrl_C) |
| 6872 | { |
| 6873 | /* :q or :close, don't execute any command |
| 6874 | * and don't modify the cmd window. */ |
| 6875 | ccline.cmdbuff = NULL; |
| 6876 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6877 | else |
| 6878 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 6879 | if (ccline.cmdbuff == NULL) |
| 6880 | cmdwin_result = Ctrl_C; |
| 6881 | else |
| 6882 | { |
| 6883 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 6884 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 6885 | ccline.cmdpos = curwin->w_cursor.col; |
| 6886 | if (ccline.cmdpos > ccline.cmdlen) |
| 6887 | ccline.cmdpos = ccline.cmdlen; |
| 6888 | if (cmdwin_result == K_IGNORE) |
| 6889 | { |
| 6890 | set_cmdspos_cursor(); |
| 6891 | redrawcmd(); |
| 6892 | } |
| 6893 | } |
| 6894 | |
| 6895 | # ifdef FEAT_AUTOCMD |
| 6896 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6897 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | # endif |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 6899 | # ifdef FEAT_CONCEAL |
| 6900 | /* Avoid command-line window first character being concealed. */ |
| 6901 | curwin->w_p_cole = 0; |
| 6902 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6903 | wp = curwin; |
| 6904 | bp = curbuf; |
| 6905 | win_goto(old_curwin); |
| 6906 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 6907 | |
| 6908 | /* win_close() may have already wiped the buffer when 'bh' is |
| 6909 | * set to 'wipe' */ |
| 6910 | if (buf_valid(bp)) |
Bram Moolenaar | 42ec656 | 2012-02-22 14:58:37 +0100 | [diff] [blame] | 6911 | close_buffer(NULL, bp, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6912 | |
| 6913 | /* Restore window sizes. */ |
| 6914 | win_size_restore(&winsizes); |
| 6915 | |
| 6916 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6917 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6918 | # endif |
| 6919 | } |
| 6920 | |
| 6921 | ga_clear(&winsizes); |
| 6922 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6923 | # ifdef FEAT_RIGHTLEFT |
| 6924 | cmdmsg_rl = save_cmdmsg_rl; |
| 6925 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | |
| 6927 | State = save_State; |
| 6928 | # ifdef FEAT_MOUSE |
| 6929 | setmouse(); |
| 6930 | # endif |
| 6931 | |
| 6932 | return cmdwin_result; |
| 6933 | } |
| 6934 | #endif /* FEAT_CMDWIN */ |
| 6935 | |
| 6936 | /* |
| 6937 | * Used for commands that either take a simple command string argument, or: |
| 6938 | * cmd << endmarker |
| 6939 | * {script} |
| 6940 | * endmarker |
| 6941 | * Returns a pointer to allocated memory with {script} or NULL. |
| 6942 | */ |
| 6943 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6944 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | { |
| 6946 | char_u *theline; |
| 6947 | char *end_pattern = NULL; |
| 6948 | char dot[] = "."; |
| 6949 | garray_T ga; |
| 6950 | |
| 6951 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 6952 | return NULL; |
| 6953 | |
| 6954 | ga_init2(&ga, 1, 0x400); |
| 6955 | |
| 6956 | if (cmd[2] != NUL) |
| 6957 | end_pattern = (char *)skipwhite(cmd + 2); |
| 6958 | else |
| 6959 | end_pattern = dot; |
| 6960 | |
| 6961 | for (;;) |
| 6962 | { |
| 6963 | theline = eap->getline( |
| 6964 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 6965 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6966 | #endif |
| 6967 | NUL, eap->cookie, 0); |
| 6968 | |
| 6969 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 6970 | { |
| 6971 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6972 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 6973 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6974 | |
| 6975 | ga_concat(&ga, theline); |
| 6976 | ga_append(&ga, '\n'); |
| 6977 | vim_free(theline); |
| 6978 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 6979 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6980 | |
| 6981 | return (char_u *)ga.ga_data; |
| 6982 | } |