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 */ |
| 28 | int cmdfirstc; /* ':', '/', '?', '=' or NUL */ |
| 29 | int cmdindent; /* number of spaces before cmdline */ |
| 30 | char_u *cmdprompt; /* message in front of cmdline */ |
| 31 | int cmdattr; /* attributes for prompt */ |
| 32 | int overstrike; /* Typing mode on the command line. Shared by |
| 33 | getcmdline() and put_on_cmdline(). */ |
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 */ |
| 59 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
| 60 | } histentry_T; |
| 61 | |
| 62 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 63 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 64 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 65 | /* identifying (unique) number of newest history entry */ |
| 66 | static int hislen = 0; /* actual length of history tables */ |
| 67 | |
| 68 | static int hist_char2type __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | |
| 70 | static int in_history __ARGS((int, char_u *, int)); |
| 71 | # ifdef FEAT_EVAL |
| 72 | static int calc_hist_idx __ARGS((int histype, int num)); |
| 73 | # endif |
| 74 | #endif |
| 75 | |
| 76 | #ifdef FEAT_RIGHTLEFT |
| 77 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 78 | #endif |
| 79 | |
| 80 | #ifdef FEAT_FKMAP |
| 81 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 82 | #endif |
| 83 | |
| 84 | static int cmdline_charsize __ARGS((int idx)); |
| 85 | static void set_cmdspos __ARGS((void)); |
| 86 | static void set_cmdspos_cursor __ARGS((void)); |
| 87 | #ifdef FEAT_MBYTE |
| 88 | static void correct_cmdspos __ARGS((int idx, int cells)); |
| 89 | #endif |
| 90 | static void alloc_cmdbuff __ARGS((int len)); |
| 91 | static int realloc_cmdbuff __ARGS((int len)); |
| 92 | static void draw_cmdline __ARGS((int start, int len)); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 93 | static void save_cmdline __ARGS((struct cmdline_info *ccp)); |
| 94 | static void restore_cmdline __ARGS((struct cmdline_info *ccp)); |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 95 | static int cmdline_paste __ARGS((int regname, int literally, int remcr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 97 | static void redrawcmd_preedit __ARGS((void)); |
| 98 | #endif |
| 99 | #ifdef FEAT_WILDMENU |
| 100 | static void cmdline_del __ARGS((int from)); |
| 101 | #endif |
| 102 | static void redrawcmdprompt __ARGS((void)); |
| 103 | static void cursorcmd __ARGS((void)); |
| 104 | static int ccheck_abbr __ARGS((int)); |
| 105 | static int nextwild __ARGS((expand_T *xp, int type, int options)); |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 106 | static void escape_fname __ARGS((char_u **pp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | static int showmatches __ARGS((expand_T *xp, int wildmenu)); |
| 108 | static void set_expand_context __ARGS((expand_T *xp)); |
| 109 | static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int)); |
| 110 | static int expand_showtail __ARGS((expand_T *xp)); |
| 111 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 112 | static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname)); |
| 114 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 115 | static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file)); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 116 | static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | # endif |
| 118 | #endif |
| 119 | |
| 120 | #ifdef FEAT_CMDWIN |
| 121 | static int ex_window __ARGS((void)); |
| 122 | #endif |
| 123 | |
| 124 | /* |
| 125 | * getcmdline() - accept a command line starting with firstc. |
| 126 | * |
| 127 | * firstc == ':' get ":" command line. |
| 128 | * firstc == '/' or '?' get search pattern |
| 129 | * firstc == '=' get expression |
| 130 | * firstc == '@' get text for input() function |
| 131 | * firstc == '>' get text for debug mode |
| 132 | * firstc == NUL get text for :insert command |
| 133 | * firstc == -1 like NUL, and break on CTRL-C |
| 134 | * |
| 135 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 136 | * command line. |
| 137 | * |
| 138 | * Careful: getcmdline() can be called recursively! |
| 139 | * |
| 140 | * Return pointer to allocated string if there is a commandline, NULL |
| 141 | * otherwise. |
| 142 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | char_u * |
| 144 | getcmdline(firstc, count, indent) |
| 145 | int firstc; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 146 | long count UNUSED; /* only used for incremental search */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | int indent; /* indent for inside conditionals */ |
| 148 | { |
| 149 | int c; |
| 150 | int i; |
| 151 | int j; |
| 152 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 153 | int do_abbr; /* when TRUE check for abbr. */ |
| 154 | #ifdef FEAT_CMDHIST |
| 155 | char_u *lookfor = NULL; /* string to match */ |
| 156 | int hiscnt; /* current history line in use */ |
| 157 | int histype; /* history type to be used */ |
| 158 | #endif |
| 159 | #ifdef FEAT_SEARCH_EXTRA |
| 160 | pos_T old_cursor; |
| 161 | colnr_T old_curswant; |
| 162 | colnr_T old_leftcol; |
| 163 | linenr_T old_topline; |
| 164 | # ifdef FEAT_DIFF |
| 165 | int old_topfill; |
| 166 | # endif |
| 167 | linenr_T old_botline; |
| 168 | int did_incsearch = FALSE; |
| 169 | int incsearch_postponed = FALSE; |
| 170 | #endif |
| 171 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 172 | int wim_index = 0; /* index in wim_flags[] */ |
| 173 | int res; |
| 174 | int save_msg_scroll = msg_scroll; |
| 175 | int save_State = State; /* remember State when called */ |
| 176 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 177 | #ifdef FEAT_MOUSE |
| 178 | /* mouse drag and release events are ignored, unless they are |
| 179 | * preceded with a mouse down event */ |
| 180 | int ignore_drag_release = TRUE; |
| 181 | #endif |
| 182 | #ifdef FEAT_EVAL |
| 183 | int break_ctrl_c = FALSE; |
| 184 | #endif |
| 185 | expand_T xpc; |
| 186 | long *b_im_ptr = NULL; |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 187 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
| 188 | /* Everything that may work recursively should save and restore the |
| 189 | * current command line in save_ccline. That includes update_screen(), a |
| 190 | * custom status line may invoke ":normal". */ |
| 191 | struct cmdline_info save_ccline; |
| 192 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | |
| 194 | #ifdef FEAT_SNIFF |
| 195 | want_sniff_request = 0; |
| 196 | #endif |
| 197 | #ifdef FEAT_EVAL |
| 198 | if (firstc == -1) |
| 199 | { |
| 200 | firstc = NUL; |
| 201 | break_ctrl_c = TRUE; |
| 202 | } |
| 203 | #endif |
| 204 | #ifdef FEAT_RIGHTLEFT |
| 205 | /* start without Hebrew mapping for a command line */ |
| 206 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 207 | cmd_hkmap = 0; |
| 208 | #endif |
| 209 | |
| 210 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 211 | #ifdef FEAT_SEARCH_EXTRA |
| 212 | old_cursor = curwin->w_cursor; /* needs to be restored later */ |
| 213 | old_curswant = curwin->w_curswant; |
| 214 | old_leftcol = curwin->w_leftcol; |
| 215 | old_topline = curwin->w_topline; |
| 216 | # ifdef FEAT_DIFF |
| 217 | old_topfill = curwin->w_topfill; |
| 218 | # endif |
| 219 | old_botline = curwin->w_botline; |
| 220 | #endif |
| 221 | |
| 222 | /* |
| 223 | * set some variables for redrawcmd() |
| 224 | */ |
| 225 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 226 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 227 | |
| 228 | /* alloc initial ccline.cmdbuff */ |
| 229 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | if (ccline.cmdbuff == NULL) |
| 231 | return NULL; /* out of memory */ |
| 232 | ccline.cmdlen = ccline.cmdpos = 0; |
| 233 | ccline.cmdbuff[0] = NUL; |
| 234 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 235 | /* autoindent for :insert and :append */ |
| 236 | if (firstc <= 0) |
| 237 | { |
| 238 | copy_spaces(ccline.cmdbuff, indent); |
| 239 | ccline.cmdbuff[indent] = NUL; |
| 240 | ccline.cmdpos = indent; |
| 241 | ccline.cmdspos = indent; |
| 242 | ccline.cmdlen = indent; |
| 243 | } |
| 244 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 246 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 247 | |
| 248 | #ifdef FEAT_RIGHTLEFT |
| 249 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 250 | && (firstc == '/' || firstc == '?')) |
| 251 | cmdmsg_rl = TRUE; |
| 252 | else |
| 253 | cmdmsg_rl = FALSE; |
| 254 | #endif |
| 255 | |
| 256 | redir_off = TRUE; /* don't redirect the typed command */ |
| 257 | if (!cmd_silent) |
| 258 | { |
| 259 | i = msg_scrolled; |
| 260 | msg_scrolled = 0; /* avoid wait_return message */ |
| 261 | gotocmdline(TRUE); |
| 262 | msg_scrolled += i; |
| 263 | redrawcmdprompt(); /* draw prompt or indent */ |
| 264 | set_cmdspos(); |
| 265 | } |
| 266 | xpc.xp_context = EXPAND_NOTHING; |
| 267 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 268 | #ifndef BACKSLASH_IN_FILENAME |
| 269 | xpc.xp_shell = FALSE; |
| 270 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 271 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 272 | #if defined(FEAT_EVAL) |
| 273 | if (ccline.input_fn) |
| 274 | { |
| 275 | xpc.xp_context = ccline.xp_context; |
| 276 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 277 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 278 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 279 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 280 | } |
| 281 | #endif |
| 282 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 285 | * doing ":@0" when register 0 doesn't contain a CR. |
| 286 | */ |
| 287 | msg_scroll = FALSE; |
| 288 | |
| 289 | State = CMDLINE; |
| 290 | |
| 291 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 292 | { |
| 293 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 294 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 295 | b_im_ptr = &curbuf->b_p_iminsert; |
| 296 | else |
| 297 | b_im_ptr = &curbuf->b_p_imsearch; |
| 298 | if (*b_im_ptr == B_IMODE_LMAP) |
| 299 | State |= LANGMAP; |
| 300 | #ifdef USE_IM_CONTROL |
| 301 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 302 | #endif |
| 303 | } |
| 304 | #ifdef USE_IM_CONTROL |
| 305 | else if (p_imcmdline) |
| 306 | im_set_active(TRUE); |
| 307 | #endif |
| 308 | |
| 309 | #ifdef FEAT_MOUSE |
| 310 | setmouse(); |
| 311 | #endif |
| 312 | #ifdef CURSOR_SHAPE |
| 313 | ui_cursor_shape(); /* may show different cursor shape */ |
| 314 | #endif |
| 315 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 316 | /* When inside an autocommand for writing "exiting" may be set and |
| 317 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 318 | settmode(TMODE_RAW); |
| 319 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | #ifdef FEAT_CMDHIST |
| 321 | init_history(); |
| 322 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 323 | histype = hist_char2type(firstc); |
| 324 | #endif |
| 325 | |
| 326 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 327 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | #endif |
| 329 | |
| 330 | /* |
| 331 | * Collect the command string, handling editing keys. |
| 332 | */ |
| 333 | for (;;) |
| 334 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 335 | redir_off = TRUE; /* Don't redirect the typed command. |
| 336 | Repeated, because a ":redir" inside |
| 337 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | #ifdef USE_ON_FLY_SCROLL |
| 339 | dont_scroll = FALSE; /* allow scrolling here */ |
| 340 | #endif |
| 341 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 342 | |
| 343 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 344 | |
| 345 | /* Get a character. Ignore K_IGNORE, it should not do anything, such |
| 346 | * as stop completion. */ |
| 347 | do |
| 348 | { |
| 349 | c = safe_vgetc(); |
| 350 | } while (c == K_IGNORE); |
| 351 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | if (KeyTyped) |
| 353 | { |
| 354 | some_key_typed = TRUE; |
| 355 | #ifdef FEAT_RIGHTLEFT |
| 356 | if (cmd_hkmap) |
| 357 | c = hkmap(c); |
| 358 | # ifdef FEAT_FKMAP |
| 359 | if (cmd_fkmap) |
| 360 | c = cmdl_fkmap(c); |
| 361 | # endif |
| 362 | if (cmdmsg_rl && !KeyStuffed) |
| 363 | { |
| 364 | /* Invert horizontal movements and operations. Only when |
| 365 | * typed by the user directly, not when the result of a |
| 366 | * mapping. */ |
| 367 | switch (c) |
| 368 | { |
| 369 | case K_RIGHT: c = K_LEFT; break; |
| 370 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 371 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 372 | case K_LEFT: c = K_RIGHT; break; |
| 373 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 374 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 375 | } |
| 376 | } |
| 377 | #endif |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Ignore got_int when CTRL-C was typed here. |
| 382 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 383 | * ":g/pat/normal /pat" (without the <CR>). |
| 384 | * Don't ignore it for the input() function. |
| 385 | */ |
| 386 | if ((c == Ctrl_C |
| 387 | #ifdef UNIX |
| 388 | || c == intr_char |
| 389 | #endif |
| 390 | ) |
| 391 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 392 | && firstc != '@' |
| 393 | #endif |
| 394 | #ifdef FEAT_EVAL |
| 395 | && !break_ctrl_c |
| 396 | #endif |
| 397 | && !global_busy) |
| 398 | got_int = FALSE; |
| 399 | |
| 400 | #ifdef FEAT_CMDHIST |
| 401 | /* free old command line when finished moving around in the history |
| 402 | * list */ |
| 403 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 404 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 405 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 406 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 407 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 408 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 410 | { |
| 411 | vim_free(lookfor); |
| 412 | lookfor = NULL; |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 417 | * When there are matching completions to select <S-Tab> works like |
| 418 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 419 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 420 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 421 | c = Ctrl_P; |
| 422 | |
| 423 | #ifdef FEAT_WILDMENU |
| 424 | /* Special translations for 'wildmenu' */ |
| 425 | if (did_wild_list && p_wmnu) |
| 426 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 427 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 428 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 429 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | c = Ctrl_N; |
| 431 | } |
| 432 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 433 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 434 | && ccline.cmdpos > 1 |
| 435 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 436 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 437 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 438 | c = K_DOWN; |
| 439 | #endif |
| 440 | |
| 441 | /* free expanded names when finished walking through matches */ |
| 442 | if (xpc.xp_numfiles != -1 |
| 443 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 444 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 445 | && c != Ctrl_L) |
| 446 | { |
| 447 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 448 | did_wild_list = FALSE; |
| 449 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 450 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 451 | #endif |
| 452 | xpc.xp_context = EXPAND_NOTHING; |
| 453 | wim_index = 0; |
| 454 | #ifdef FEAT_WILDMENU |
| 455 | if (p_wmnu && wild_menu_showing != 0) |
| 456 | { |
| 457 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 458 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 459 | |
| 460 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 461 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 462 | |
| 463 | if (wild_menu_showing == WM_SCROLLED) |
| 464 | { |
| 465 | /* Entered command line, move it up */ |
| 466 | cmdline_row--; |
| 467 | redrawcmd(); |
| 468 | } |
| 469 | else if (save_p_ls != -1) |
| 470 | { |
| 471 | /* restore 'laststatus' and 'winminheight' */ |
| 472 | p_ls = save_p_ls; |
| 473 | p_wmh = save_p_wmh; |
| 474 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 475 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 477 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | redrawcmd(); |
| 479 | save_p_ls = -1; |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | # ifdef FEAT_VERTSPLIT |
| 484 | win_redraw_last_status(topframe); |
| 485 | # else |
| 486 | lastwin->w_redr_status = TRUE; |
| 487 | # endif |
| 488 | redraw_statuslines(); |
| 489 | } |
| 490 | KeyTyped = skt; |
| 491 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 492 | if (ccline.input_fn) |
| 493 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 494 | } |
| 495 | #endif |
| 496 | } |
| 497 | |
| 498 | #ifdef FEAT_WILDMENU |
| 499 | /* Special translations for 'wildmenu' */ |
| 500 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 501 | { |
| 502 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 503 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 504 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 506 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | { |
| 508 | /* Hitting <Up>: Remove one submenu name in front of the |
| 509 | * cursor */ |
| 510 | int found = FALSE; |
| 511 | |
| 512 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 513 | i = 0; |
| 514 | while (--j > 0) |
| 515 | { |
| 516 | /* check for start of menu name */ |
| 517 | if (ccline.cmdbuff[j] == ' ' |
| 518 | && ccline.cmdbuff[j - 1] != '\\') |
| 519 | { |
| 520 | i = j + 1; |
| 521 | break; |
| 522 | } |
| 523 | /* check for start of submenu name */ |
| 524 | if (ccline.cmdbuff[j] == '.' |
| 525 | && ccline.cmdbuff[j - 1] != '\\') |
| 526 | { |
| 527 | if (found) |
| 528 | { |
| 529 | i = j + 1; |
| 530 | break; |
| 531 | } |
| 532 | else |
| 533 | found = TRUE; |
| 534 | } |
| 535 | } |
| 536 | if (i > 0) |
| 537 | cmdline_del(i); |
| 538 | c = p_wc; |
| 539 | xpc.xp_context = EXPAND_NOTHING; |
| 540 | } |
| 541 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 542 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 543 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 544 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | { |
| 546 | char_u upseg[5]; |
| 547 | |
| 548 | upseg[0] = PATHSEP; |
| 549 | upseg[1] = '.'; |
| 550 | upseg[2] = '.'; |
| 551 | upseg[3] = PATHSEP; |
| 552 | upseg[4] = NUL; |
| 553 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 554 | if (c == K_DOWN |
| 555 | && ccline.cmdpos > 0 |
| 556 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 557 | && (ccline.cmdpos < 3 |
| 558 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 559 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 560 | { |
| 561 | /* go down a directory */ |
| 562 | c = p_wc; |
| 563 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 564 | 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] | 565 | { |
| 566 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 567 | int found = FALSE; |
| 568 | |
| 569 | j = ccline.cmdpos; |
| 570 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 571 | while (--j > i) |
| 572 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 573 | #ifdef FEAT_MBYTE |
| 574 | if (has_mbyte) |
| 575 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 576 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 578 | { |
| 579 | found = TRUE; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | if (found |
| 584 | && ccline.cmdbuff[j - 1] == '.' |
| 585 | && ccline.cmdbuff[j - 2] == '.' |
| 586 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 587 | { |
| 588 | cmdline_del(j - 2); |
| 589 | c = p_wc; |
| 590 | } |
| 591 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 592 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 593 | { |
| 594 | /* go up a directory */ |
| 595 | int found = FALSE; |
| 596 | |
| 597 | j = ccline.cmdpos - 1; |
| 598 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 599 | while (--j > i) |
| 600 | { |
| 601 | #ifdef FEAT_MBYTE |
| 602 | if (has_mbyte) |
| 603 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 604 | #endif |
| 605 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 606 | #ifdef BACKSLASH_IN_FILENAME |
| 607 | && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1]) |
| 608 | == NULL |
| 609 | #endif |
| 610 | ) |
| 611 | { |
| 612 | if (found) |
| 613 | { |
| 614 | i = j + 1; |
| 615 | break; |
| 616 | } |
| 617 | else |
| 618 | found = TRUE; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (!found) |
| 623 | j = i; |
| 624 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 625 | j += 4; |
| 626 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 627 | && j == i) |
| 628 | j += 3; |
| 629 | else |
| 630 | j = 0; |
| 631 | if (j > 0) |
| 632 | { |
| 633 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 634 | * machine-specific stuff here and in upseg init */ |
| 635 | cmdline_del(j); |
| 636 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 637 | } |
| 638 | else if (ccline.cmdpos > i) |
| 639 | cmdline_del(i); |
| 640 | c = p_wc; |
| 641 | } |
| 642 | } |
| 643 | #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */ |
| 644 | if (p_wmnu |
| 645 | && (xpc.xp_context == EXPAND_FILES |
| 646 | || xpc.xp_context == EXPAND_MENUNAMES) |
| 647 | && (c == K_UP || c == K_DOWN)) |
| 648 | xpc.xp_context = EXPAND_NOTHING; |
| 649 | #endif |
| 650 | |
| 651 | #endif /* FEAT_WILDMENU */ |
| 652 | |
| 653 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 654 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 655 | if (c == Ctrl_BSL) |
| 656 | { |
| 657 | ++no_mapping; |
| 658 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 659 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | --no_mapping; |
| 661 | --allow_keys; |
| 662 | /* CTRL-\ e doesn't work when obtaining an expression. */ |
| 663 | if (c != Ctrl_N && c != Ctrl_G |
| 664 | && (c != 'e' || ccline.cmdfirstc == '=')) |
| 665 | { |
| 666 | vungetc(c); |
| 667 | c = Ctrl_BSL; |
| 668 | } |
| 669 | #ifdef FEAT_EVAL |
| 670 | else if (c == 'e') |
| 671 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 672 | char_u *p = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 676 | * Need to save and restore the current command line, to be |
| 677 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | */ |
| 679 | if (ccline.cmdpos == ccline.cmdlen) |
| 680 | new_cmdpos = 99999; /* keep it at the end */ |
| 681 | else |
| 682 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 683 | |
| 684 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 686 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 687 | if (c == '=') |
| 688 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 689 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 690 | * to avoid nasty things like going to another buffer when |
| 691 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 692 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 693 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 695 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 696 | restore_cmdline(&save_ccline); |
| 697 | |
| 698 | if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 699 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 700 | ccline.cmdlen = (int)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 701 | STRCPY(ccline.cmdbuff, p); |
| 702 | vim_free(p); |
| 703 | |
| 704 | /* Restore the cursor or use the position set with |
| 705 | * set_cmdline_pos(). */ |
| 706 | if (new_cmdpos > ccline.cmdlen) |
| 707 | ccline.cmdpos = ccline.cmdlen; |
| 708 | else |
| 709 | ccline.cmdpos = new_cmdpos; |
| 710 | |
| 711 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 712 | redrawcmd(); |
| 713 | goto cmdline_changed; |
| 714 | } |
| 715 | } |
| 716 | beep_flush(); |
| 717 | c = ESC; |
| 718 | } |
| 719 | #endif |
| 720 | else |
| 721 | { |
| 722 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 723 | restart_edit = 'a'; |
| 724 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 725 | in history */ |
| 726 | goto returncmd; /* back to Normal mode */ |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | #ifdef FEAT_CMDWIN |
| 731 | if (c == cedit_key || c == K_CMDWIN) |
| 732 | { |
| 733 | /* |
| 734 | * Open a window to edit the command line (and history). |
| 735 | */ |
| 736 | c = ex_window(); |
| 737 | some_key_typed = TRUE; |
| 738 | } |
| 739 | # ifdef FEAT_DIGRAPHS |
| 740 | else |
| 741 | # endif |
| 742 | #endif |
| 743 | #ifdef FEAT_DIGRAPHS |
| 744 | c = do_digraph(c); |
| 745 | #endif |
| 746 | |
| 747 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 748 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 749 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 750 | /* In Ex mode a backslash escapes a newline. */ |
| 751 | if (exmode_active |
| 752 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 753 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 754 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 755 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 757 | if (c == K_KENTER) |
| 758 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 760 | else |
| 761 | { |
| 762 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 763 | truncate the cmdline now. */ |
| 764 | if (ccheck_abbr(c + ABBR_OFF)) |
| 765 | goto cmdline_changed; |
| 766 | if (!cmd_silent) |
| 767 | { |
| 768 | windgoto(msg_row, 0); |
| 769 | out_flush(); |
| 770 | } |
| 771 | break; |
| 772 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Completion for 'wildchar' or 'wildcharm' key. |
| 777 | * - hitting <ESC> twice means: abandon command line. |
| 778 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 779 | * typed, not when it comes from a macro |
| 780 | */ |
| 781 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 782 | { |
| 783 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 784 | { |
| 785 | /* if 'wildmode' contains "list" may still need to list */ |
| 786 | if (xpc.xp_numfiles > 1 |
| 787 | && !did_wild_list |
| 788 | && (wim_flags[wim_index] & WIM_LIST)) |
| 789 | { |
| 790 | (void)showmatches(&xpc, FALSE); |
| 791 | redrawcmd(); |
| 792 | did_wild_list = TRUE; |
| 793 | } |
| 794 | if (wim_flags[wim_index] & WIM_LONGEST) |
| 795 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); |
| 796 | else if (wim_flags[wim_index] & WIM_FULL) |
| 797 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP); |
| 798 | else |
| 799 | res = OK; /* don't insert 'wildchar' now */ |
| 800 | } |
| 801 | else /* typed p_wc first time */ |
| 802 | { |
| 803 | wim_index = 0; |
| 804 | j = ccline.cmdpos; |
| 805 | /* if 'wildmode' first contains "longest", get longest |
| 806 | * common part */ |
| 807 | if (wim_flags[0] & WIM_LONGEST) |
| 808 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); |
| 809 | else |
| 810 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP); |
| 811 | |
| 812 | /* if interrupted while completing, behave like it failed */ |
| 813 | if (got_int) |
| 814 | { |
| 815 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 816 | got_int = FALSE; /* don't abandon the command line */ |
| 817 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 818 | #ifdef FEAT_WILDMENU |
| 819 | xpc.xp_context = EXPAND_NOTHING; |
| 820 | #endif |
| 821 | goto cmdline_changed; |
| 822 | } |
| 823 | |
| 824 | /* when more than one match, and 'wildmode' first contains |
| 825 | * "list", or no change and 'wildmode' contains "longest,list", |
| 826 | * list all matches */ |
| 827 | if (res == OK && xpc.xp_numfiles > 1) |
| 828 | { |
| 829 | /* a "longest" that didn't do anything is skipped (but not |
| 830 | * "list:longest") */ |
| 831 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 832 | wim_index = 1; |
| 833 | if ((wim_flags[wim_index] & WIM_LIST) |
| 834 | #ifdef FEAT_WILDMENU |
| 835 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 836 | #endif |
| 837 | ) |
| 838 | { |
| 839 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 840 | { |
| 841 | #ifdef FEAT_WILDMENU |
| 842 | int p_wmnu_save = p_wmnu; |
| 843 | p_wmnu = 0; |
| 844 | #endif |
| 845 | nextwild(&xpc, WILD_PREV, 0); /* remove match */ |
| 846 | #ifdef FEAT_WILDMENU |
| 847 | p_wmnu = p_wmnu_save; |
| 848 | #endif |
| 849 | } |
| 850 | #ifdef FEAT_WILDMENU |
| 851 | (void)showmatches(&xpc, p_wmnu |
| 852 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 853 | #else |
| 854 | (void)showmatches(&xpc, FALSE); |
| 855 | #endif |
| 856 | redrawcmd(); |
| 857 | did_wild_list = TRUE; |
| 858 | if (wim_flags[wim_index] & WIM_LONGEST) |
| 859 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); |
| 860 | else if (wim_flags[wim_index] & WIM_FULL) |
| 861 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP); |
| 862 | } |
| 863 | else |
| 864 | vim_beep(); |
| 865 | } |
| 866 | #ifdef FEAT_WILDMENU |
| 867 | else if (xpc.xp_numfiles == -1) |
| 868 | xpc.xp_context = EXPAND_NOTHING; |
| 869 | #endif |
| 870 | } |
| 871 | if (wim_index < 3) |
| 872 | ++wim_index; |
| 873 | if (c == ESC) |
| 874 | gotesc = TRUE; |
| 875 | if (res == OK) |
| 876 | goto cmdline_changed; |
| 877 | } |
| 878 | |
| 879 | gotesc = FALSE; |
| 880 | |
| 881 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 882 | if (c == K_S_TAB && KeyTyped) |
| 883 | { |
| 884 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK |
| 885 | && nextwild(&xpc, WILD_PREV, 0) == OK |
| 886 | && nextwild(&xpc, WILD_PREV, 0) == OK) |
| 887 | goto cmdline_changed; |
| 888 | } |
| 889 | |
| 890 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 891 | c = NL; |
| 892 | |
| 893 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 894 | |
| 895 | /* |
| 896 | * Big switch for a typed command line character. |
| 897 | */ |
| 898 | switch (c) |
| 899 | { |
| 900 | case K_BS: |
| 901 | case Ctrl_H: |
| 902 | case K_DEL: |
| 903 | case K_KDEL: |
| 904 | case Ctrl_W: |
| 905 | #ifdef FEAT_FKMAP |
| 906 | if (cmd_fkmap && c == K_BS) |
| 907 | c = K_DEL; |
| 908 | #endif |
| 909 | if (c == K_KDEL) |
| 910 | c = K_DEL; |
| 911 | |
| 912 | /* |
| 913 | * delete current character is the same as backspace on next |
| 914 | * character, except at end of line |
| 915 | */ |
| 916 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 917 | ++ccline.cmdpos; |
| 918 | #ifdef FEAT_MBYTE |
| 919 | if (has_mbyte && c == K_DEL) |
| 920 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 921 | ccline.cmdbuff + ccline.cmdpos); |
| 922 | #endif |
| 923 | if (ccline.cmdpos > 0) |
| 924 | { |
| 925 | char_u *p; |
| 926 | |
| 927 | j = ccline.cmdpos; |
| 928 | p = ccline.cmdbuff + j; |
| 929 | #ifdef FEAT_MBYTE |
| 930 | if (has_mbyte) |
| 931 | { |
| 932 | p = mb_prevptr(ccline.cmdbuff, p); |
| 933 | if (c == Ctrl_W) |
| 934 | { |
| 935 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 936 | p = mb_prevptr(ccline.cmdbuff, p); |
| 937 | i = mb_get_class(p); |
| 938 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 939 | p = mb_prevptr(ccline.cmdbuff, p); |
| 940 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 941 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | else |
| 945 | #endif |
| 946 | if (c == Ctrl_W) |
| 947 | { |
| 948 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 949 | --p; |
| 950 | i = vim_iswordc(p[-1]); |
| 951 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 952 | && vim_iswordc(p[-1]) == i) |
| 953 | --p; |
| 954 | } |
| 955 | else |
| 956 | --p; |
| 957 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 958 | ccline.cmdlen -= j - ccline.cmdpos; |
| 959 | i = ccline.cmdpos; |
| 960 | while (i < ccline.cmdlen) |
| 961 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 962 | |
| 963 | /* Truncate at the end, required for multi-byte chars. */ |
| 964 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 965 | redrawcmd(); |
| 966 | } |
| 967 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 968 | && ccline.cmdprompt == NULL && indent == 0) |
| 969 | { |
| 970 | /* In ex and debug mode it doesn't make sense to return. */ |
| 971 | if (exmode_active |
| 972 | #ifdef FEAT_EVAL |
| 973 | || ccline.cmdfirstc == '>' |
| 974 | #endif |
| 975 | ) |
| 976 | goto cmdline_not_changed; |
| 977 | |
| 978 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 979 | ccline.cmdbuff = NULL; |
| 980 | if (!cmd_silent) |
| 981 | { |
| 982 | #ifdef FEAT_RIGHTLEFT |
| 983 | if (cmdmsg_rl) |
| 984 | msg_col = Columns; |
| 985 | else |
| 986 | #endif |
| 987 | msg_col = 0; |
| 988 | msg_putchar(' '); /* delete ':' */ |
| 989 | } |
| 990 | redraw_cmdline = TRUE; |
| 991 | goto returncmd; /* back to cmd mode */ |
| 992 | } |
| 993 | goto cmdline_changed; |
| 994 | |
| 995 | case K_INS: |
| 996 | case K_KINS: |
| 997 | #ifdef FEAT_FKMAP |
| 998 | /* if Farsi mode set, we are in reverse insert mode - |
| 999 | Do not change the mode */ |
| 1000 | if (cmd_fkmap) |
| 1001 | beep_flush(); |
| 1002 | else |
| 1003 | #endif |
| 1004 | ccline.overstrike = !ccline.overstrike; |
| 1005 | #ifdef CURSOR_SHAPE |
| 1006 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1007 | #endif |
| 1008 | goto cmdline_not_changed; |
| 1009 | |
| 1010 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1011 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | { |
| 1013 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1014 | State ^= LANGMAP; |
| 1015 | #ifdef USE_IM_CONTROL |
| 1016 | im_set_active(FALSE); /* Disable input method */ |
| 1017 | #endif |
| 1018 | if (b_im_ptr != NULL) |
| 1019 | { |
| 1020 | if (State & LANGMAP) |
| 1021 | *b_im_ptr = B_IMODE_LMAP; |
| 1022 | else |
| 1023 | *b_im_ptr = B_IMODE_NONE; |
| 1024 | } |
| 1025 | } |
| 1026 | #ifdef USE_IM_CONTROL |
| 1027 | else |
| 1028 | { |
| 1029 | /* There are no ":lmap" mappings, toggle IM. When |
| 1030 | * 'imdisable' is set don't try getting the status, it's |
| 1031 | * always off. */ |
| 1032 | if ((p_imdisable && b_im_ptr != NULL) |
| 1033 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1034 | { |
| 1035 | im_set_active(FALSE); /* Disable input method */ |
| 1036 | if (b_im_ptr != NULL) |
| 1037 | *b_im_ptr = B_IMODE_NONE; |
| 1038 | } |
| 1039 | else |
| 1040 | { |
| 1041 | im_set_active(TRUE); /* Enable input method */ |
| 1042 | if (b_im_ptr != NULL) |
| 1043 | *b_im_ptr = B_IMODE_IM; |
| 1044 | } |
| 1045 | } |
| 1046 | #endif |
| 1047 | if (b_im_ptr != NULL) |
| 1048 | { |
| 1049 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1050 | set_iminsert_global(); |
| 1051 | else |
| 1052 | set_imsearch_global(); |
| 1053 | } |
| 1054 | #ifdef CURSOR_SHAPE |
| 1055 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1056 | #endif |
| 1057 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 1058 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1059 | status_redraw_curbuf(); |
| 1060 | #endif |
| 1061 | goto cmdline_not_changed; |
| 1062 | |
| 1063 | /* case '@': only in very old vi */ |
| 1064 | case Ctrl_U: |
| 1065 | /* delete all characters left of the cursor */ |
| 1066 | j = ccline.cmdpos; |
| 1067 | ccline.cmdlen -= j; |
| 1068 | i = ccline.cmdpos = 0; |
| 1069 | while (i < ccline.cmdlen) |
| 1070 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1071 | /* Truncate at the end, required for multi-byte chars. */ |
| 1072 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 1073 | redrawcmd(); |
| 1074 | goto cmdline_changed; |
| 1075 | |
| 1076 | #ifdef FEAT_CLIPBOARD |
| 1077 | case Ctrl_Y: |
| 1078 | /* Copy the modeless selection, if there is one. */ |
| 1079 | if (clip_star.state != SELECT_CLEARED) |
| 1080 | { |
| 1081 | if (clip_star.state == SELECT_DONE) |
| 1082 | clip_copy_modeless_selection(TRUE); |
| 1083 | goto cmdline_not_changed; |
| 1084 | } |
| 1085 | break; |
| 1086 | #endif |
| 1087 | |
| 1088 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1089 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1090 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1091 | * ":normal" runs out of characters. */ |
| 1092 | if (exmode_active |
| 1093 | #ifdef FEAT_EX_EXTRA |
| 1094 | && (ex_normal_busy == 0 || typebuf.tb_len > 0) |
| 1095 | #endif |
| 1096 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1097 | goto cmdline_not_changed; |
| 1098 | |
| 1099 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1100 | putting it in history */ |
| 1101 | goto returncmd; /* back to cmd mode */ |
| 1102 | |
| 1103 | case Ctrl_R: /* insert register */ |
| 1104 | #ifdef USE_ON_FLY_SCROLL |
| 1105 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1106 | #endif |
| 1107 | putcmdline('"', TRUE); |
| 1108 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1109 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | if (i == Ctrl_O) |
| 1111 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1112 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1113 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1114 | --no_mapping; |
| 1115 | #ifdef FEAT_EVAL |
| 1116 | /* |
| 1117 | * Insert the result of an expression. |
| 1118 | * Need to save the current command line, to be able to enter |
| 1119 | * a new one... |
| 1120 | */ |
| 1121 | new_cmdpos = -1; |
| 1122 | if (c == '=') |
| 1123 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1124 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1125 | { |
| 1126 | beep_flush(); |
| 1127 | c = ESC; |
| 1128 | } |
| 1129 | else |
| 1130 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1131 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1133 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | #endif |
| 1137 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1138 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1139 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1140 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1141 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1142 | /* When there was a serious error abort getting the |
| 1143 | * command line. */ |
| 1144 | if (aborting()) |
| 1145 | { |
| 1146 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1147 | putting it in history */ |
| 1148 | goto returncmd; /* back to cmd mode */ |
| 1149 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1150 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1152 | #ifdef FEAT_EVAL |
| 1153 | if (new_cmdpos >= 0) |
| 1154 | { |
| 1155 | /* set_cmdline_pos() was used */ |
| 1156 | if (new_cmdpos > ccline.cmdlen) |
| 1157 | ccline.cmdpos = ccline.cmdlen; |
| 1158 | else |
| 1159 | ccline.cmdpos = new_cmdpos; |
| 1160 | } |
| 1161 | #endif |
| 1162 | } |
| 1163 | redrawcmd(); |
| 1164 | goto cmdline_changed; |
| 1165 | |
| 1166 | case Ctrl_D: |
| 1167 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1168 | break; /* Use ^D as normal char instead */ |
| 1169 | |
| 1170 | redrawcmd(); |
| 1171 | continue; /* don't do incremental search now */ |
| 1172 | |
| 1173 | case K_RIGHT: |
| 1174 | case K_S_RIGHT: |
| 1175 | case K_C_RIGHT: |
| 1176 | do |
| 1177 | { |
| 1178 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1179 | break; |
| 1180 | i = cmdline_charsize(ccline.cmdpos); |
| 1181 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1182 | break; |
| 1183 | ccline.cmdspos += i; |
| 1184 | #ifdef FEAT_MBYTE |
| 1185 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1186 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | + ccline.cmdpos); |
| 1188 | else |
| 1189 | #endif |
| 1190 | ++ccline.cmdpos; |
| 1191 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1192 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1193 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1194 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1195 | #ifdef FEAT_MBYTE |
| 1196 | if (has_mbyte) |
| 1197 | set_cmdspos_cursor(); |
| 1198 | #endif |
| 1199 | goto cmdline_not_changed; |
| 1200 | |
| 1201 | case K_LEFT: |
| 1202 | case K_S_LEFT: |
| 1203 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1204 | if (ccline.cmdpos == 0) |
| 1205 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | do |
| 1207 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | --ccline.cmdpos; |
| 1209 | #ifdef FEAT_MBYTE |
| 1210 | if (has_mbyte) /* move to first byte of char */ |
| 1211 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1212 | ccline.cmdbuff + ccline.cmdpos); |
| 1213 | #endif |
| 1214 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1215 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1216 | while (ccline.cmdpos > 0 |
| 1217 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1218 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1220 | #ifdef FEAT_MBYTE |
| 1221 | if (has_mbyte) |
| 1222 | set_cmdspos_cursor(); |
| 1223 | #endif |
| 1224 | goto cmdline_not_changed; |
| 1225 | |
| 1226 | case K_IGNORE: |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1227 | /* Ignore mouse event or ex_window() result. */ |
| 1228 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1230 | #ifdef FEAT_GUI_W32 |
| 1231 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1232 | * cancelled. */ |
| 1233 | case K_F4: |
| 1234 | if (mod_mask == MOD_MASK_ALT) |
| 1235 | { |
| 1236 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1237 | goto cmdline_not_changed; |
| 1238 | } |
| 1239 | break; |
| 1240 | #endif |
| 1241 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1242 | #ifdef FEAT_MOUSE |
| 1243 | case K_MIDDLEDRAG: |
| 1244 | case K_MIDDLERELEASE: |
| 1245 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1246 | |
| 1247 | case K_MIDDLEMOUSE: |
| 1248 | # ifdef FEAT_GUI |
| 1249 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1250 | if (!gui.in_use) |
| 1251 | # endif |
| 1252 | if (!mouse_has(MOUSE_COMMAND)) |
| 1253 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1254 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1256 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1257 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1258 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1259 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | redrawcmd(); |
| 1261 | goto cmdline_changed; |
| 1262 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1263 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1265 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | redrawcmd(); |
| 1267 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1268 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | |
| 1270 | case K_LEFTDRAG: |
| 1271 | case K_LEFTRELEASE: |
| 1272 | case K_RIGHTDRAG: |
| 1273 | case K_RIGHTRELEASE: |
| 1274 | /* Ignore drag and release events when the button-down wasn't |
| 1275 | * seen before. */ |
| 1276 | if (ignore_drag_release) |
| 1277 | goto cmdline_not_changed; |
| 1278 | /* FALLTHROUGH */ |
| 1279 | case K_LEFTMOUSE: |
| 1280 | case K_RIGHTMOUSE: |
| 1281 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1282 | ignore_drag_release = TRUE; |
| 1283 | else |
| 1284 | ignore_drag_release = FALSE; |
| 1285 | # ifdef FEAT_GUI |
| 1286 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1287 | if (!gui.in_use) |
| 1288 | # endif |
| 1289 | if (!mouse_has(MOUSE_COMMAND)) |
| 1290 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1291 | # ifdef FEAT_CLIPBOARD |
| 1292 | if (mouse_row < cmdline_row && clip_star.available) |
| 1293 | { |
| 1294 | int button, is_click, is_drag; |
| 1295 | |
| 1296 | /* |
| 1297 | * Handle modeless selection. |
| 1298 | */ |
| 1299 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1300 | &is_click, &is_drag); |
| 1301 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1302 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1303 | { |
| 1304 | /* Translate shift-left to right button. */ |
| 1305 | button = MOUSE_RIGHT; |
| 1306 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1307 | } |
| 1308 | clip_modeless(button, is_click, is_drag); |
| 1309 | goto cmdline_not_changed; |
| 1310 | } |
| 1311 | # endif |
| 1312 | |
| 1313 | set_cmdspos(); |
| 1314 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1315 | ++ccline.cmdpos) |
| 1316 | { |
| 1317 | i = cmdline_charsize(ccline.cmdpos); |
| 1318 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1319 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1320 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1321 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | if (has_mbyte) |
| 1323 | { |
| 1324 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1325 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1326 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | + ccline.cmdpos) - 1; |
| 1328 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1329 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1330 | ccline.cmdspos += i; |
| 1331 | } |
| 1332 | goto cmdline_not_changed; |
| 1333 | |
| 1334 | /* Mouse scroll wheel: ignored here */ |
| 1335 | case K_MOUSEDOWN: |
| 1336 | case K_MOUSEUP: |
| 1337 | /* Alternate buttons ignored here */ |
| 1338 | case K_X1MOUSE: |
| 1339 | case K_X1DRAG: |
| 1340 | case K_X1RELEASE: |
| 1341 | case K_X2MOUSE: |
| 1342 | case K_X2DRAG: |
| 1343 | case K_X2RELEASE: |
| 1344 | goto cmdline_not_changed; |
| 1345 | |
| 1346 | #endif /* FEAT_MOUSE */ |
| 1347 | |
| 1348 | #ifdef FEAT_GUI |
| 1349 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1350 | case K_LEFTRELEASE_NM: |
| 1351 | goto cmdline_not_changed; |
| 1352 | |
| 1353 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1354 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | { |
| 1356 | gui_do_scroll(); |
| 1357 | redrawcmd(); |
| 1358 | } |
| 1359 | goto cmdline_not_changed; |
| 1360 | |
| 1361 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1362 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1363 | { |
| 1364 | gui_do_horiz_scroll(); |
| 1365 | redrawcmd(); |
| 1366 | } |
| 1367 | goto cmdline_not_changed; |
| 1368 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1369 | #ifdef FEAT_GUI_TABLINE |
| 1370 | case K_TABLINE: |
| 1371 | case K_TABMENU: |
| 1372 | /* Don't want to change any tabs here. Make sure the same tab |
| 1373 | * is still selected. */ |
| 1374 | if (gui_use_tabline()) |
| 1375 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1376 | goto cmdline_not_changed; |
| 1377 | #endif |
| 1378 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1379 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1380 | goto cmdline_not_changed; |
| 1381 | |
| 1382 | case Ctrl_B: /* begin of command line */ |
| 1383 | case K_HOME: |
| 1384 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1385 | case K_S_HOME: |
| 1386 | case K_C_HOME: |
| 1387 | ccline.cmdpos = 0; |
| 1388 | set_cmdspos(); |
| 1389 | goto cmdline_not_changed; |
| 1390 | |
| 1391 | case Ctrl_E: /* end of command line */ |
| 1392 | case K_END: |
| 1393 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1394 | case K_S_END: |
| 1395 | case K_C_END: |
| 1396 | ccline.cmdpos = ccline.cmdlen; |
| 1397 | set_cmdspos_cursor(); |
| 1398 | goto cmdline_not_changed; |
| 1399 | |
| 1400 | case Ctrl_A: /* all matches */ |
| 1401 | if (nextwild(&xpc, WILD_ALL, 0) == FAIL) |
| 1402 | break; |
| 1403 | goto cmdline_changed; |
| 1404 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1405 | case Ctrl_L: |
| 1406 | #ifdef FEAT_SEARCH_EXTRA |
| 1407 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1408 | { |
| 1409 | /* Add a character from under the cursor for 'incsearch' */ |
| 1410 | if (did_incsearch |
| 1411 | && !equalpos(curwin->w_cursor, old_cursor)) |
| 1412 | { |
| 1413 | c = gchar_cursor(); |
| 1414 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1415 | { |
| 1416 | if (c == firstc || vim_strchr((char_u *)( |
| 1417 | p_magic ? "\\^$.*[" : "\\^$"), c) |
| 1418 | != NULL) |
| 1419 | { |
| 1420 | /* put a backslash before special characters */ |
| 1421 | stuffcharReadbuff(c); |
| 1422 | c = '\\'; |
| 1423 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1424 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1425 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1426 | } |
| 1427 | goto cmdline_not_changed; |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
| 1431 | /* completion: longest common part */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL) |
| 1433 | break; |
| 1434 | goto cmdline_changed; |
| 1435 | |
| 1436 | case Ctrl_N: /* next match */ |
| 1437 | case Ctrl_P: /* previous match */ |
| 1438 | if (xpc.xp_numfiles > 0) |
| 1439 | { |
| 1440 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0) |
| 1441 | == FAIL) |
| 1442 | break; |
| 1443 | goto cmdline_changed; |
| 1444 | } |
| 1445 | |
| 1446 | #ifdef FEAT_CMDHIST |
| 1447 | case K_UP: |
| 1448 | case K_DOWN: |
| 1449 | case K_S_UP: |
| 1450 | case K_S_DOWN: |
| 1451 | case K_PAGEUP: |
| 1452 | case K_KPAGEUP: |
| 1453 | case K_PAGEDOWN: |
| 1454 | case K_KPAGEDOWN: |
| 1455 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1456 | goto cmdline_not_changed; |
| 1457 | |
| 1458 | i = hiscnt; |
| 1459 | |
| 1460 | /* save current command string so it can be restored later */ |
| 1461 | if (lookfor == NULL) |
| 1462 | { |
| 1463 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1464 | goto cmdline_not_changed; |
| 1465 | lookfor[ccline.cmdpos] = NUL; |
| 1466 | } |
| 1467 | |
| 1468 | j = (int)STRLEN(lookfor); |
| 1469 | for (;;) |
| 1470 | { |
| 1471 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1472 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1473 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | { |
| 1475 | if (hiscnt == hislen) /* first time */ |
| 1476 | hiscnt = hisidx[histype]; |
| 1477 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1478 | hiscnt = hislen - 1; |
| 1479 | else if (hiscnt != hisidx[histype] + 1) |
| 1480 | --hiscnt; |
| 1481 | else /* at top of list */ |
| 1482 | { |
| 1483 | hiscnt = i; |
| 1484 | break; |
| 1485 | } |
| 1486 | } |
| 1487 | else /* one step forwards */ |
| 1488 | { |
| 1489 | /* on last entry, clear the line */ |
| 1490 | if (hiscnt == hisidx[histype]) |
| 1491 | { |
| 1492 | hiscnt = hislen; |
| 1493 | break; |
| 1494 | } |
| 1495 | |
| 1496 | /* not on a history line, nothing to do */ |
| 1497 | if (hiscnt == hislen) |
| 1498 | break; |
| 1499 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1500 | hiscnt = 0; |
| 1501 | else |
| 1502 | ++hiscnt; |
| 1503 | } |
| 1504 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1505 | { |
| 1506 | hiscnt = i; |
| 1507 | break; |
| 1508 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1509 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1510 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1511 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1512 | lookfor, (size_t)j) == 0) |
| 1513 | break; |
| 1514 | } |
| 1515 | |
| 1516 | if (hiscnt != i) /* jumped to other entry */ |
| 1517 | { |
| 1518 | char_u *p; |
| 1519 | int len; |
| 1520 | int old_firstc; |
| 1521 | |
| 1522 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1523 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1524 | if (hiscnt == hislen) |
| 1525 | p = lookfor; /* back to the old one */ |
| 1526 | else |
| 1527 | p = history[histype][hiscnt].hisstr; |
| 1528 | |
| 1529 | if (histype == HIST_SEARCH |
| 1530 | && p != lookfor |
| 1531 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1532 | { |
| 1533 | /* Correct for the separator character used when |
| 1534 | * adding the history entry vs the one used now. |
| 1535 | * First loop: count length. |
| 1536 | * Second loop: copy the characters. */ |
| 1537 | for (i = 0; i <= 1; ++i) |
| 1538 | { |
| 1539 | len = 0; |
| 1540 | for (j = 0; p[j] != NUL; ++j) |
| 1541 | { |
| 1542 | /* Replace old sep with new sep, unless it is |
| 1543 | * escaped. */ |
| 1544 | if (p[j] == old_firstc |
| 1545 | && (j == 0 || p[j - 1] != '\\')) |
| 1546 | { |
| 1547 | if (i > 0) |
| 1548 | ccline.cmdbuff[len] = firstc; |
| 1549 | } |
| 1550 | else |
| 1551 | { |
| 1552 | /* Escape new sep, unless it is already |
| 1553 | * escaped. */ |
| 1554 | if (p[j] == firstc |
| 1555 | && (j == 0 || p[j - 1] != '\\')) |
| 1556 | { |
| 1557 | if (i > 0) |
| 1558 | ccline.cmdbuff[len] = '\\'; |
| 1559 | ++len; |
| 1560 | } |
| 1561 | if (i > 0) |
| 1562 | ccline.cmdbuff[len] = p[j]; |
| 1563 | } |
| 1564 | ++len; |
| 1565 | } |
| 1566 | if (i == 0) |
| 1567 | { |
| 1568 | alloc_cmdbuff(len); |
| 1569 | if (ccline.cmdbuff == NULL) |
| 1570 | goto returncmd; |
| 1571 | } |
| 1572 | } |
| 1573 | ccline.cmdbuff[len] = NUL; |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | alloc_cmdbuff((int)STRLEN(p)); |
| 1578 | if (ccline.cmdbuff == NULL) |
| 1579 | goto returncmd; |
| 1580 | STRCPY(ccline.cmdbuff, p); |
| 1581 | } |
| 1582 | |
| 1583 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1584 | redrawcmd(); |
| 1585 | goto cmdline_changed; |
| 1586 | } |
| 1587 | beep_flush(); |
| 1588 | goto cmdline_not_changed; |
| 1589 | #endif |
| 1590 | |
| 1591 | case Ctrl_V: |
| 1592 | case Ctrl_Q: |
| 1593 | #ifdef FEAT_MOUSE |
| 1594 | ignore_drag_release = TRUE; |
| 1595 | #endif |
| 1596 | putcmdline('^', TRUE); |
| 1597 | c = get_literal(); /* get next (two) character(s) */ |
| 1598 | do_abbr = FALSE; /* don't do abbreviation now */ |
| 1599 | #ifdef FEAT_MBYTE |
| 1600 | /* may need to remove ^ when composing char was typed */ |
| 1601 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1602 | { |
| 1603 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1604 | msg_putchar(' '); |
| 1605 | cursorcmd(); |
| 1606 | } |
| 1607 | #endif |
| 1608 | break; |
| 1609 | |
| 1610 | #ifdef FEAT_DIGRAPHS |
| 1611 | case Ctrl_K: |
| 1612 | #ifdef FEAT_MOUSE |
| 1613 | ignore_drag_release = TRUE; |
| 1614 | #endif |
| 1615 | putcmdline('?', TRUE); |
| 1616 | #ifdef USE_ON_FLY_SCROLL |
| 1617 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1618 | #endif |
| 1619 | c = get_digraph(TRUE); |
| 1620 | if (c != NUL) |
| 1621 | break; |
| 1622 | |
| 1623 | redrawcmd(); |
| 1624 | goto cmdline_not_changed; |
| 1625 | #endif /* FEAT_DIGRAPHS */ |
| 1626 | |
| 1627 | #ifdef FEAT_RIGHTLEFT |
| 1628 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1629 | if (!p_ari) |
| 1630 | break; |
| 1631 | #ifdef FEAT_FKMAP |
| 1632 | if (p_altkeymap) |
| 1633 | { |
| 1634 | cmd_fkmap = !cmd_fkmap; |
| 1635 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1636 | ccline.overstrike = FALSE; |
| 1637 | } |
| 1638 | else /* Hebrew is default */ |
| 1639 | #endif |
| 1640 | cmd_hkmap = !cmd_hkmap; |
| 1641 | goto cmdline_not_changed; |
| 1642 | #endif |
| 1643 | |
| 1644 | default: |
| 1645 | #ifdef UNIX |
| 1646 | if (c == intr_char) |
| 1647 | { |
| 1648 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1649 | putting it in history */ |
| 1650 | goto returncmd; /* back to Normal mode */ |
| 1651 | } |
| 1652 | #endif |
| 1653 | /* |
| 1654 | * Normal character with no special meaning. Just set mod_mask |
| 1655 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1656 | * the string <S-Space>. This should only happen after ^V. |
| 1657 | */ |
| 1658 | if (!IS_SPECIAL(c)) |
| 1659 | mod_mask = 0x0; |
| 1660 | break; |
| 1661 | } |
| 1662 | /* |
| 1663 | * End of switch on command line character. |
| 1664 | * We come here if we have a normal character. |
| 1665 | */ |
| 1666 | |
| 1667 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr( |
| 1668 | #ifdef FEAT_MBYTE |
| 1669 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1670 | * what check_abbr() expects. */ |
| 1671 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1672 | #endif |
| 1673 | c)) |
| 1674 | goto cmdline_changed; |
| 1675 | |
| 1676 | /* |
| 1677 | * put the character in the command line |
| 1678 | */ |
| 1679 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1680 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1681 | else |
| 1682 | { |
| 1683 | #ifdef FEAT_MBYTE |
| 1684 | if (has_mbyte) |
| 1685 | { |
| 1686 | j = (*mb_char2bytes)(c, IObuff); |
| 1687 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1688 | put_on_cmdline(IObuff, j, TRUE); |
| 1689 | } |
| 1690 | else |
| 1691 | #endif |
| 1692 | { |
| 1693 | IObuff[0] = c; |
| 1694 | put_on_cmdline(IObuff, 1, TRUE); |
| 1695 | } |
| 1696 | } |
| 1697 | goto cmdline_changed; |
| 1698 | |
| 1699 | /* |
| 1700 | * This part implements incremental searches for "/" and "?" |
| 1701 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1702 | * line did not change. Then we only search and redraw if something changed in |
| 1703 | * the past. |
| 1704 | * Jump to cmdline_changed when the command line did change. |
| 1705 | * (Sorry for the goto's, I know it is ugly). |
| 1706 | */ |
| 1707 | cmdline_not_changed: |
| 1708 | #ifdef FEAT_SEARCH_EXTRA |
| 1709 | if (!incsearch_postponed) |
| 1710 | continue; |
| 1711 | #endif |
| 1712 | |
| 1713 | cmdline_changed: |
| 1714 | #ifdef FEAT_SEARCH_EXTRA |
| 1715 | /* |
| 1716 | * 'incsearch' highlighting. |
| 1717 | */ |
| 1718 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1719 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1720 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1721 | #ifdef FEAT_RELTIME |
| 1722 | proftime_T tm; |
| 1723 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1724 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | /* if there is a character waiting, search and redraw later */ |
| 1726 | if (char_avail()) |
| 1727 | { |
| 1728 | incsearch_postponed = TRUE; |
| 1729 | continue; |
| 1730 | } |
| 1731 | incsearch_postponed = FALSE; |
| 1732 | curwin->w_cursor = old_cursor; /* start at old position */ |
| 1733 | |
| 1734 | /* If there is no command line, don't do anything */ |
| 1735 | if (ccline.cmdlen == 0) |
| 1736 | i = 0; |
| 1737 | else |
| 1738 | { |
| 1739 | cursor_off(); /* so the user knows we're busy */ |
| 1740 | out_flush(); |
| 1741 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1742 | #ifdef FEAT_RELTIME |
| 1743 | /* Set the time limit to half a second. */ |
| 1744 | profile_setlimit(500L, &tm); |
| 1745 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1746 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1747 | SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
| 1748 | #ifdef FEAT_RELTIME |
| 1749 | &tm |
| 1750 | #else |
| 1751 | NULL |
| 1752 | #endif |
| 1753 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1754 | --emsg_off; |
| 1755 | /* if interrupted while searching, behave like it failed */ |
| 1756 | if (got_int) |
| 1757 | { |
| 1758 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1759 | got_int = FALSE; /* don't abandon the command line */ |
| 1760 | i = 0; |
| 1761 | } |
| 1762 | else if (char_avail()) |
| 1763 | /* cancelled searching because a char was typed */ |
| 1764 | incsearch_postponed = TRUE; |
| 1765 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1766 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1767 | highlight_match = TRUE; /* highlight position */ |
| 1768 | else |
| 1769 | highlight_match = FALSE; /* remove highlight */ |
| 1770 | |
| 1771 | /* first restore the old curwin values, so the screen is |
| 1772 | * positioned in the same way as the actual search command */ |
| 1773 | curwin->w_leftcol = old_leftcol; |
| 1774 | curwin->w_topline = old_topline; |
| 1775 | # ifdef FEAT_DIFF |
| 1776 | curwin->w_topfill = old_topfill; |
| 1777 | # endif |
| 1778 | curwin->w_botline = old_botline; |
| 1779 | changed_cline_bef_curs(); |
| 1780 | update_topline(); |
| 1781 | |
| 1782 | if (i != 0) |
| 1783 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1784 | pos_T save_pos = curwin->w_cursor; |
| 1785 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1786 | /* |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1787 | * First move cursor to end of match, then to the start. This |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1788 | * moves the whole match onto the screen when 'nowrap' is set. |
| 1789 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | curwin->w_cursor.lnum += search_match_lines; |
| 1791 | curwin->w_cursor.col = search_match_endcol; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1792 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 1793 | { |
| 1794 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 1795 | coladvance((colnr_T)MAXCOL); |
| 1796 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1797 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1798 | end_pos = curwin->w_cursor; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1799 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1801 | else |
| 1802 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1803 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1804 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 1805 | # ifdef FEAT_WINDOWS |
| 1806 | /* May redraw the status line to show the cursor position. */ |
| 1807 | if (p_ru && curwin->w_status_height > 0) |
| 1808 | curwin->w_redr_status = TRUE; |
| 1809 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1811 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1812 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1813 | restore_cmdline(&save_ccline); |
| 1814 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1815 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 1816 | if (i != 0) |
| 1817 | curwin->w_cursor = end_pos; |
| 1818 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1819 | msg_starthere(); |
| 1820 | redrawcmdline(); |
| 1821 | did_incsearch = TRUE; |
| 1822 | } |
| 1823 | #else /* FEAT_SEARCH_EXTRA */ |
| 1824 | ; |
| 1825 | #endif |
| 1826 | |
| 1827 | #ifdef FEAT_RIGHTLEFT |
| 1828 | if (cmdmsg_rl |
| 1829 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1830 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | # endif |
| 1832 | ) |
| 1833 | /* Always redraw the whole command line to fix shaping and |
| 1834 | * right-left typing. Not efficient, but it works. */ |
| 1835 | redrawcmd(); |
| 1836 | #endif |
| 1837 | } |
| 1838 | |
| 1839 | returncmd: |
| 1840 | |
| 1841 | #ifdef FEAT_RIGHTLEFT |
| 1842 | cmdmsg_rl = FALSE; |
| 1843 | #endif |
| 1844 | |
| 1845 | #ifdef FEAT_FKMAP |
| 1846 | cmd_fkmap = 0; |
| 1847 | #endif |
| 1848 | |
| 1849 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1850 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1851 | |
| 1852 | #ifdef FEAT_SEARCH_EXTRA |
| 1853 | if (did_incsearch) |
| 1854 | { |
| 1855 | curwin->w_cursor = old_cursor; |
| 1856 | curwin->w_curswant = old_curswant; |
| 1857 | curwin->w_leftcol = old_leftcol; |
| 1858 | curwin->w_topline = old_topline; |
| 1859 | # ifdef FEAT_DIFF |
| 1860 | curwin->w_topfill = old_topfill; |
| 1861 | # endif |
| 1862 | curwin->w_botline = old_botline; |
| 1863 | highlight_match = FALSE; |
| 1864 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1865 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1866 | } |
| 1867 | #endif |
| 1868 | |
| 1869 | if (ccline.cmdbuff != NULL) |
| 1870 | { |
| 1871 | /* |
| 1872 | * Put line in history buffer (":" and "=" only when it was typed). |
| 1873 | */ |
| 1874 | #ifdef FEAT_CMDHIST |
| 1875 | if (ccline.cmdlen && firstc != NUL |
| 1876 | && (some_key_typed || histype == HIST_SEARCH)) |
| 1877 | { |
| 1878 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 1879 | histype == HIST_SEARCH ? firstc : NUL); |
| 1880 | if (firstc == ':') |
| 1881 | { |
| 1882 | vim_free(new_last_cmdline); |
| 1883 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 1884 | } |
| 1885 | } |
| 1886 | #endif |
| 1887 | |
| 1888 | if (gotesc) /* abandon command line */ |
| 1889 | { |
| 1890 | vim_free(ccline.cmdbuff); |
| 1891 | ccline.cmdbuff = NULL; |
| 1892 | if (msg_scrolled == 0) |
| 1893 | compute_cmdrow(); |
| 1894 | MSG(""); |
| 1895 | redraw_cmdline = TRUE; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * If the screen was shifted up, redraw the whole screen (later). |
| 1901 | * If the line is too long, clear it, so ruler and shown command do |
| 1902 | * not get printed in the middle of it. |
| 1903 | */ |
| 1904 | msg_check(); |
| 1905 | msg_scroll = save_msg_scroll; |
| 1906 | redir_off = FALSE; |
| 1907 | |
| 1908 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 1909 | if (some_key_typed) |
| 1910 | need_wait_return = FALSE; |
| 1911 | |
| 1912 | State = save_State; |
| 1913 | #ifdef USE_IM_CONTROL |
| 1914 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 1915 | im_save_status(b_im_ptr); |
| 1916 | im_set_active(FALSE); |
| 1917 | #endif |
| 1918 | #ifdef FEAT_MOUSE |
| 1919 | setmouse(); |
| 1920 | #endif |
| 1921 | #ifdef CURSOR_SHAPE |
| 1922 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1923 | #endif |
| 1924 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1925 | { |
| 1926 | char_u *p = ccline.cmdbuff; |
| 1927 | |
| 1928 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 1929 | ccline.cmdbuff = NULL; |
| 1930 | return p; |
| 1931 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 1935 | /* |
| 1936 | * Get a command line with a prompt. |
| 1937 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 1938 | * f_input() when evaluating an expression from CTRL-R =). |
| 1939 | * Returns the command line in allocated memory, or NULL. |
| 1940 | */ |
| 1941 | char_u * |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1942 | getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1943 | int firstc; |
| 1944 | char_u *prompt; /* command line prompt */ |
| 1945 | int attr; /* attributes for prompt */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1946 | int xp_context; /* type of expansion */ |
| 1947 | char_u *xp_arg; /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | { |
| 1949 | char_u *s; |
| 1950 | struct cmdline_info save_ccline; |
| 1951 | int msg_col_save = msg_col; |
| 1952 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1953 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1954 | ccline.cmdprompt = prompt; |
| 1955 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1956 | # ifdef FEAT_EVAL |
| 1957 | ccline.xp_context = xp_context; |
| 1958 | ccline.xp_arg = xp_arg; |
| 1959 | ccline.input_fn = (firstc == '@'); |
| 1960 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1962 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | /* Restore msg_col, the prompt from input() may have changed it. */ |
| 1964 | msg_col = msg_col_save; |
| 1965 | |
| 1966 | return s; |
| 1967 | } |
| 1968 | #endif |
| 1969 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1970 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1971 | * Return TRUE when the text must not be changed and we can't switch to |
| 1972 | * another window or buffer. Used when editing the command line, evaluating |
| 1973 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1974 | */ |
| 1975 | int |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1976 | text_locked() |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1977 | { |
| 1978 | #ifdef FEAT_CMDWIN |
| 1979 | if (cmdwin_type != 0) |
| 1980 | return TRUE; |
| 1981 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1982 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * Give an error message for a command that isn't allowed while the cmdline |
| 1987 | * window is open or editing the cmdline in another way. |
| 1988 | */ |
| 1989 | void |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1990 | text_locked_msg() |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1991 | { |
| 1992 | #ifdef FEAT_CMDWIN |
| 1993 | if (cmdwin_type != 0) |
| 1994 | EMSG(_(e_cmdwin)); |
| 1995 | else |
| 1996 | #endif |
| 1997 | EMSG(_(e_secure)); |
| 1998 | } |
| 1999 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2000 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2001 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2002 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2003 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2004 | */ |
| 2005 | int |
| 2006 | curbuf_locked() |
| 2007 | { |
| 2008 | if (curbuf_lock > 0) |
| 2009 | { |
| 2010 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2011 | return TRUE; |
| 2012 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2013 | return allbuf_locked(); |
| 2014 | } |
| 2015 | |
| 2016 | /* |
| 2017 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2018 | * message. |
| 2019 | */ |
| 2020 | int |
| 2021 | allbuf_locked() |
| 2022 | { |
| 2023 | if (allbuf_lock > 0) |
| 2024 | { |
| 2025 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2026 | return TRUE; |
| 2027 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2028 | return FALSE; |
| 2029 | } |
| 2030 | #endif |
| 2031 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2032 | static int |
| 2033 | cmdline_charsize(idx) |
| 2034 | int idx; |
| 2035 | { |
| 2036 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2037 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2038 | return 1; |
| 2039 | #endif |
| 2040 | return ptr2cells(ccline.cmdbuff + idx); |
| 2041 | } |
| 2042 | |
| 2043 | /* |
| 2044 | * Compute the offset of the cursor on the command line for the prompt and |
| 2045 | * indent. |
| 2046 | */ |
| 2047 | static void |
| 2048 | set_cmdspos() |
| 2049 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2050 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2052 | else |
| 2053 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Compute the screen position for the cursor on the command line. |
| 2058 | */ |
| 2059 | static void |
| 2060 | set_cmdspos_cursor() |
| 2061 | { |
| 2062 | int i, m, c; |
| 2063 | |
| 2064 | set_cmdspos(); |
| 2065 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2066 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2068 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2069 | m = MAXCOL; |
| 2070 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | else |
| 2072 | m = MAXCOL; |
| 2073 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2074 | { |
| 2075 | c = cmdline_charsize(i); |
| 2076 | #ifdef FEAT_MBYTE |
| 2077 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2078 | if (has_mbyte) |
| 2079 | correct_cmdspos(i, c); |
| 2080 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2081 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2082 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2083 | if ((ccline.cmdspos += c) >= m) |
| 2084 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2085 | ccline.cmdspos -= c; |
| 2086 | break; |
| 2087 | } |
| 2088 | #ifdef FEAT_MBYTE |
| 2089 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2090 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | #endif |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | #ifdef FEAT_MBYTE |
| 2096 | /* |
| 2097 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2098 | * character that doesn't fit, so that a ">" must be displayed. |
| 2099 | */ |
| 2100 | static void |
| 2101 | correct_cmdspos(idx, cells) |
| 2102 | int idx; |
| 2103 | int cells; |
| 2104 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2105 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2106 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2107 | && ccline.cmdspos % Columns + cells > Columns) |
| 2108 | ccline.cmdspos++; |
| 2109 | } |
| 2110 | #endif |
| 2111 | |
| 2112 | /* |
| 2113 | * Get an Ex command line for the ":" command. |
| 2114 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | char_u * |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2116 | getexline(c, cookie, indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2117 | int c; /* normally ':', NUL for ":append" */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2118 | void *cookie UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | int indent; /* indent for inside conditionals */ |
| 2120 | { |
| 2121 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2122 | if (exec_from_reg && vpeekc() == ':') |
| 2123 | (void)vgetc(); |
| 2124 | return getcmdline(c, 1L, indent); |
| 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * Get an Ex command line for Ex mode. |
| 2129 | * In Ex mode we only use the OS supplied line editing features and no |
| 2130 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2131 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2133 | char_u * |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2134 | getexmodeline(promptc, cookie, indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2135 | int promptc; /* normally ':', NUL for ":append" and '?' for |
| 2136 | :s prompt */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 2137 | void *cookie UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | int indent; /* indent for inside conditionals */ |
| 2139 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2140 | garray_T line_ga; |
| 2141 | char_u *pend; |
| 2142 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2143 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2144 | int escaped = FALSE; /* CTRL-V typed */ |
| 2145 | int vcol = 0; |
| 2146 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2147 | int prev_char; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | |
| 2149 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2150 | * confuses the system function that computes tabstops. */ |
| 2151 | cursor_on(); |
| 2152 | |
| 2153 | /* always start in column 0; write a newline if necessary */ |
| 2154 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2155 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2156 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2157 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2158 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2159 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2160 | if (p_prompt) |
| 2161 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2162 | while (indent-- > 0) |
| 2163 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2164 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | ga_init2(&line_ga, 1, 30); |
| 2168 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2169 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2170 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2171 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2172 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2173 | while (indent >= 8) |
| 2174 | { |
| 2175 | ga_append(&line_ga, TAB); |
| 2176 | msg_puts((char_u *)" "); |
| 2177 | indent -= 8; |
| 2178 | } |
| 2179 | while (indent-- > 0) |
| 2180 | { |
| 2181 | ga_append(&line_ga, ' '); |
| 2182 | msg_putchar(' '); |
| 2183 | } |
| 2184 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2185 | ++no_mapping; |
| 2186 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2187 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | /* |
| 2189 | * Get the line, one character at a time. |
| 2190 | */ |
| 2191 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2192 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2193 | { |
| 2194 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2195 | break; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2196 | pend = (char_u *)line_ga.ga_data + line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2198 | /* Get one character at a time. Don't use inchar(), it can't handle |
| 2199 | * special characters. */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2200 | prev_char = c1; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2201 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2204 | * Handle line editing. |
| 2205 | * Previously this was left to the system, putting the terminal in |
| 2206 | * 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] | 2207 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2208 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2209 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2210 | msg_putchar('\n'); |
| 2211 | break; |
| 2212 | } |
| 2213 | |
| 2214 | if (!escaped) |
| 2215 | { |
| 2216 | /* CR typed means "enter", which is NL */ |
| 2217 | if (c1 == '\r') |
| 2218 | c1 = '\n'; |
| 2219 | |
| 2220 | if (c1 == BS || c1 == K_BS |
| 2221 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2222 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2223 | if (line_ga.ga_len > 0) |
| 2224 | { |
| 2225 | --line_ga.ga_len; |
| 2226 | goto redraw; |
| 2227 | } |
| 2228 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2231 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2232 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2233 | msg_col = startcol; |
| 2234 | msg_clr_eos(); |
| 2235 | line_ga.ga_len = 0; |
| 2236 | continue; |
| 2237 | } |
| 2238 | |
| 2239 | if (c1 == Ctrl_T) |
| 2240 | { |
| 2241 | p = (char_u *)line_ga.ga_data; |
| 2242 | p[line_ga.ga_len] = NUL; |
| 2243 | indent = get_indent_str(p, 8); |
| 2244 | indent += curbuf->b_p_sw - indent % curbuf->b_p_sw; |
| 2245 | add_indent: |
| 2246 | while (get_indent_str(p, 8) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2247 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2248 | char_u *s = skipwhite(p); |
| 2249 | |
| 2250 | ga_grow(&line_ga, 1); |
| 2251 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2252 | *s = ' '; |
| 2253 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2254 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2255 | redraw: |
| 2256 | /* redraw the line */ |
| 2257 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2258 | vcol = 0; |
| 2259 | for (p = (char_u *)line_ga.ga_data; |
| 2260 | p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2261 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2262 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2264 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2266 | msg_putchar(' '); |
| 2267 | } while (++vcol % 8); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2268 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2269 | else |
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 | msg_outtrans_len(p, 1); |
| 2272 | vcol += char2cells(*p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2275 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2276 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2277 | continue; |
| 2278 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2280 | if (c1 == Ctrl_D) |
| 2281 | { |
| 2282 | /* Delete one shiftwidth. */ |
| 2283 | p = (char_u *)line_ga.ga_data; |
| 2284 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2286 | if (prev_char == '^') |
| 2287 | ex_keep_indent = TRUE; |
| 2288 | indent = 0; |
| 2289 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2290 | } |
| 2291 | else |
| 2292 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2293 | p[line_ga.ga_len] = NUL; |
| 2294 | indent = get_indent_str(p, 8); |
| 2295 | --indent; |
| 2296 | indent -= indent % curbuf->b_p_sw; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2297 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2298 | while (get_indent_str(p, 8) > indent) |
| 2299 | { |
| 2300 | char_u *s = skipwhite(p); |
| 2301 | |
| 2302 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2303 | --line_ga.ga_len; |
| 2304 | } |
| 2305 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2307 | |
| 2308 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2309 | { |
| 2310 | escaped = TRUE; |
| 2311 | continue; |
| 2312 | } |
| 2313 | |
| 2314 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2315 | if (IS_SPECIAL(c1)) |
| 2316 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2318 | |
| 2319 | if (IS_SPECIAL(c1)) |
| 2320 | c1 = '?'; |
| 2321 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2322 | if (c1 == '\n') |
| 2323 | msg_putchar('\n'); |
| 2324 | else if (c1 == TAB) |
| 2325 | { |
| 2326 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2327 | do |
| 2328 | { |
| 2329 | msg_putchar(' '); |
| 2330 | } while (++vcol % 8); |
| 2331 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2332 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2334 | msg_outtrans_len( |
| 2335 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1); |
| 2336 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2338 | ++line_ga.ga_len; |
| 2339 | escaped = FALSE; |
| 2340 | |
| 2341 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2342 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2343 | |
| 2344 | /* we are done when a NL is entered, but not when it comes after a |
| 2345 | * backslash */ |
| 2346 | if (line_ga.ga_len > 0 && pend[-1] == '\n' |
| 2347 | && (line_ga.ga_len <= 1 || pend[-2] != '\\')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | --line_ga.ga_len; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2350 | --pend; |
| 2351 | *pend = NUL; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2352 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2356 | --no_mapping; |
| 2357 | --allow_keys; |
| 2358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | /* make following messages go to the next line */ |
| 2360 | msg_didout = FALSE; |
| 2361 | msg_col = 0; |
| 2362 | if (msg_row < Rows - 1) |
| 2363 | ++msg_row; |
| 2364 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2365 | |
| 2366 | if (got_int) |
| 2367 | ga_clear(&line_ga); |
| 2368 | |
| 2369 | return (char_u *)line_ga.ga_data; |
| 2370 | } |
| 2371 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2372 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2373 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2374 | /* |
| 2375 | * Return TRUE if ccline.overstrike is on. |
| 2376 | */ |
| 2377 | int |
| 2378 | cmdline_overstrike() |
| 2379 | { |
| 2380 | return ccline.overstrike; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
| 2384 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2385 | */ |
| 2386 | int |
| 2387 | cmdline_at_end() |
| 2388 | { |
| 2389 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2390 | } |
| 2391 | #endif |
| 2392 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2393 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2394 | /* |
| 2395 | * Return the virtual column number at the current cursor position. |
| 2396 | * This is used by the IM code to obtain the start of the preedit string. |
| 2397 | */ |
| 2398 | colnr_T |
| 2399 | cmdline_getvcol_cursor() |
| 2400 | { |
| 2401 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2402 | return MAXCOL; |
| 2403 | |
| 2404 | # ifdef FEAT_MBYTE |
| 2405 | if (has_mbyte) |
| 2406 | { |
| 2407 | colnr_T col; |
| 2408 | int i = 0; |
| 2409 | |
| 2410 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2411 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2412 | |
| 2413 | return col; |
| 2414 | } |
| 2415 | else |
| 2416 | # endif |
| 2417 | return ccline.cmdpos; |
| 2418 | } |
| 2419 | #endif |
| 2420 | |
| 2421 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2422 | /* |
| 2423 | * If part of the command line is an IM preedit string, redraw it with |
| 2424 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2425 | */ |
| 2426 | static void |
| 2427 | redrawcmd_preedit() |
| 2428 | { |
| 2429 | if ((State & CMDLINE) |
| 2430 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2431 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | && !p_imdisable |
| 2433 | && im_is_preediting()) |
| 2434 | { |
| 2435 | int cmdpos = 0; |
| 2436 | int cmdspos; |
| 2437 | int old_row; |
| 2438 | int old_col; |
| 2439 | colnr_T col; |
| 2440 | |
| 2441 | old_row = msg_row; |
| 2442 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2443 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2444 | |
| 2445 | # ifdef FEAT_MBYTE |
| 2446 | if (has_mbyte) |
| 2447 | { |
| 2448 | for (col = 0; col < preedit_start_col |
| 2449 | && cmdpos < ccline.cmdlen; ++col) |
| 2450 | { |
| 2451 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2452 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | else |
| 2456 | # endif |
| 2457 | { |
| 2458 | cmdspos += preedit_start_col; |
| 2459 | cmdpos += preedit_start_col; |
| 2460 | } |
| 2461 | |
| 2462 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2463 | msg_col = cmdspos % (int)Columns; |
| 2464 | if (msg_row >= Rows) |
| 2465 | msg_row = Rows - 1; |
| 2466 | |
| 2467 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2468 | { |
| 2469 | int char_len; |
| 2470 | int char_attr; |
| 2471 | |
| 2472 | char_attr = im_get_feedback_attr(col); |
| 2473 | if (char_attr < 0) |
| 2474 | break; /* end of preedit string */ |
| 2475 | |
| 2476 | # ifdef FEAT_MBYTE |
| 2477 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2478 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2479 | else |
| 2480 | # endif |
| 2481 | char_len = 1; |
| 2482 | |
| 2483 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2484 | cmdpos += char_len; |
| 2485 | } |
| 2486 | |
| 2487 | msg_row = old_row; |
| 2488 | msg_col = old_col; |
| 2489 | } |
| 2490 | } |
| 2491 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2492 | |
| 2493 | /* |
| 2494 | * Allocate a new command line buffer. |
| 2495 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2496 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2497 | */ |
| 2498 | static void |
| 2499 | alloc_cmdbuff(len) |
| 2500 | int len; |
| 2501 | { |
| 2502 | /* |
| 2503 | * give some extra space to avoid having to allocate all the time |
| 2504 | */ |
| 2505 | if (len < 80) |
| 2506 | len = 100; |
| 2507 | else |
| 2508 | len += 20; |
| 2509 | |
| 2510 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2511 | ccline.cmdbufflen = len; |
| 2512 | } |
| 2513 | |
| 2514 | /* |
| 2515 | * Re-allocate the command line to length len + something extra. |
| 2516 | * return FAIL for failure, OK otherwise |
| 2517 | */ |
| 2518 | static int |
| 2519 | realloc_cmdbuff(len) |
| 2520 | int len; |
| 2521 | { |
| 2522 | char_u *p; |
| 2523 | |
| 2524 | p = ccline.cmdbuff; |
| 2525 | alloc_cmdbuff(len); /* will get some more */ |
| 2526 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2527 | { |
| 2528 | ccline.cmdbuff = p; /* keep the old one */ |
| 2529 | return FAIL; |
| 2530 | } |
| 2531 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1); |
| 2532 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2533 | |
| 2534 | if (ccline.xpc != NULL |
| 2535 | && ccline.xpc->xp_pattern != NULL |
| 2536 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2537 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2538 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2539 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2540 | |
| 2541 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2542 | * to point into the newly allocated memory. */ |
| 2543 | if (i >= 0 && i <= ccline.cmdlen) |
| 2544 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2545 | } |
| 2546 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2547 | return OK; |
| 2548 | } |
| 2549 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2550 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2551 | static char_u *arshape_buf = NULL; |
| 2552 | |
| 2553 | # if defined(EXITFREE) || defined(PROTO) |
| 2554 | void |
| 2555 | free_cmdline_buf() |
| 2556 | { |
| 2557 | vim_free(arshape_buf); |
| 2558 | } |
| 2559 | # endif |
| 2560 | #endif |
| 2561 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2562 | /* |
| 2563 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2564 | * when cmdline_star is TRUE. |
| 2565 | */ |
| 2566 | static void |
| 2567 | draw_cmdline(start, len) |
| 2568 | int start; |
| 2569 | int len; |
| 2570 | { |
| 2571 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2572 | int i; |
| 2573 | |
| 2574 | if (cmdline_star > 0) |
| 2575 | for (i = 0; i < len; ++i) |
| 2576 | { |
| 2577 | msg_putchar('*'); |
| 2578 | # ifdef FEAT_MBYTE |
| 2579 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2580 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2581 | # endif |
| 2582 | } |
| 2583 | else |
| 2584 | #endif |
| 2585 | #ifdef FEAT_ARABIC |
| 2586 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2587 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2588 | static int buflen = 0; |
| 2589 | char_u *p; |
| 2590 | int j; |
| 2591 | int newlen = 0; |
| 2592 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2593 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | int prev_c = 0; |
| 2595 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2596 | int u8c; |
| 2597 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2598 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2599 | |
| 2600 | /* |
| 2601 | * Do arabic shaping into a temporary buffer. This is very |
| 2602 | * inefficient! |
| 2603 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2604 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | { |
| 2606 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2607 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2608 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2609 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2610 | arshape_buf = alloc(buflen); |
| 2611 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2612 | return; /* out of memory */ |
| 2613 | } |
| 2614 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2615 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2616 | { |
| 2617 | /* Prepend a space to draw the leading composing char on. */ |
| 2618 | arshape_buf[0] = ' '; |
| 2619 | newlen = 1; |
| 2620 | } |
| 2621 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | for (j = start; j < start + len; j += mb_l) |
| 2623 | { |
| 2624 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2625 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2626 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2627 | if (ARABIC_CHAR(u8c)) |
| 2628 | { |
| 2629 | /* Do Arabic shaping. */ |
| 2630 | if (cmdmsg_rl) |
| 2631 | { |
| 2632 | /* displaying from right to left */ |
| 2633 | pc = prev_c; |
| 2634 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2635 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | if (j + mb_l >= start + len) |
| 2637 | nc = NUL; |
| 2638 | else |
| 2639 | nc = utf_ptr2char(p + mb_l); |
| 2640 | } |
| 2641 | else |
| 2642 | { |
| 2643 | /* displaying from left to right */ |
| 2644 | if (j + mb_l >= start + len) |
| 2645 | pc = NUL; |
| 2646 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2647 | { |
| 2648 | int pcc[MAX_MCO]; |
| 2649 | |
| 2650 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2651 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2652 | pc1 = pcc[0]; |
| 2653 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2654 | nc = prev_c; |
| 2655 | } |
| 2656 | prev_c = u8c; |
| 2657 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2658 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2659 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2660 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2661 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2663 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2664 | if (u8cc[1] != 0) |
| 2665 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2666 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | else |
| 2670 | { |
| 2671 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2672 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2673 | newlen += mb_l; |
| 2674 | } |
| 2675 | } |
| 2676 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2677 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2678 | } |
| 2679 | else |
| 2680 | #endif |
| 2681 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 2682 | } |
| 2683 | |
| 2684 | /* |
| 2685 | * Put a character on the command line. Shifts the following text to the |
| 2686 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 2687 | * "c" must be printable (fit in one display cell)! |
| 2688 | */ |
| 2689 | void |
| 2690 | putcmdline(c, shift) |
| 2691 | int c; |
| 2692 | int shift; |
| 2693 | { |
| 2694 | if (cmd_silent) |
| 2695 | return; |
| 2696 | msg_no_more = TRUE; |
| 2697 | msg_putchar(c); |
| 2698 | if (shift) |
| 2699 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2700 | msg_no_more = FALSE; |
| 2701 | cursorcmd(); |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * Undo a putcmdline(c, FALSE). |
| 2706 | */ |
| 2707 | void |
| 2708 | unputcmdline() |
| 2709 | { |
| 2710 | if (cmd_silent) |
| 2711 | return; |
| 2712 | msg_no_more = TRUE; |
| 2713 | if (ccline.cmdlen == ccline.cmdpos) |
| 2714 | msg_putchar(' '); |
| 2715 | else |
| 2716 | draw_cmdline(ccline.cmdpos, 1); |
| 2717 | msg_no_more = FALSE; |
| 2718 | cursorcmd(); |
| 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * Put the given string, of the given length, onto the command line. |
| 2723 | * If len is -1, then STRLEN() is used to calculate the length. |
| 2724 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 2725 | * part will be redrawn, otherwise it will not. If this function is called |
| 2726 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 2727 | * called afterwards. |
| 2728 | */ |
| 2729 | int |
| 2730 | put_on_cmdline(str, len, redraw) |
| 2731 | char_u *str; |
| 2732 | int len; |
| 2733 | int redraw; |
| 2734 | { |
| 2735 | int retval; |
| 2736 | int i; |
| 2737 | int m; |
| 2738 | int c; |
| 2739 | |
| 2740 | if (len < 0) |
| 2741 | len = (int)STRLEN(str); |
| 2742 | |
| 2743 | /* Check if ccline.cmdbuff needs to be longer */ |
| 2744 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
| 2745 | retval = realloc_cmdbuff(ccline.cmdlen + len); |
| 2746 | else |
| 2747 | retval = OK; |
| 2748 | if (retval == OK) |
| 2749 | { |
| 2750 | if (!ccline.overstrike) |
| 2751 | { |
| 2752 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2753 | ccline.cmdbuff + ccline.cmdpos, |
| 2754 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 2755 | ccline.cmdlen += len; |
| 2756 | } |
| 2757 | else |
| 2758 | { |
| 2759 | #ifdef FEAT_MBYTE |
| 2760 | if (has_mbyte) |
| 2761 | { |
| 2762 | /* Count nr of characters in the new string. */ |
| 2763 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2764 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | ++m; |
| 2766 | /* Count nr of bytes in cmdline that are overwritten by these |
| 2767 | * characters. */ |
| 2768 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2769 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2770 | --m; |
| 2771 | if (i < ccline.cmdlen) |
| 2772 | { |
| 2773 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2774 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 2775 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 2776 | } |
| 2777 | else |
| 2778 | ccline.cmdlen = ccline.cmdpos + len; |
| 2779 | } |
| 2780 | else |
| 2781 | #endif |
| 2782 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 2783 | ccline.cmdlen = ccline.cmdpos + len; |
| 2784 | } |
| 2785 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 2786 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 2787 | |
| 2788 | #ifdef FEAT_MBYTE |
| 2789 | if (enc_utf8) |
| 2790 | { |
| 2791 | /* When the inserted text starts with a composing character, |
| 2792 | * backup to the character before it. There could be two of them. |
| 2793 | */ |
| 2794 | i = 0; |
| 2795 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2796 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 2797 | { |
| 2798 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2799 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2800 | ccline.cmdpos -= i; |
| 2801 | len += i; |
| 2802 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2803 | } |
| 2804 | # ifdef FEAT_ARABIC |
| 2805 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 2806 | { |
| 2807 | /* Check the previous character for Arabic combining pair. */ |
| 2808 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2809 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2810 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 2811 | + ccline.cmdpos - i), c)) |
| 2812 | { |
| 2813 | ccline.cmdpos -= i; |
| 2814 | len += i; |
| 2815 | } |
| 2816 | else |
| 2817 | i = 0; |
| 2818 | } |
| 2819 | # endif |
| 2820 | if (i != 0) |
| 2821 | { |
| 2822 | /* Also backup the cursor position. */ |
| 2823 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 2824 | ccline.cmdspos -= i; |
| 2825 | msg_col -= i; |
| 2826 | if (msg_col < 0) |
| 2827 | { |
| 2828 | msg_col += Columns; |
| 2829 | --msg_row; |
| 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | #endif |
| 2834 | |
| 2835 | if (redraw && !cmd_silent) |
| 2836 | { |
| 2837 | msg_no_more = TRUE; |
| 2838 | i = cmdline_row; |
| 2839 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2840 | /* Avoid clearing the rest of the line too often. */ |
| 2841 | if (cmdline_row != i || ccline.overstrike) |
| 2842 | msg_clr_eos(); |
| 2843 | msg_no_more = FALSE; |
| 2844 | } |
| 2845 | #ifdef FEAT_FKMAP |
| 2846 | /* |
| 2847 | * If we are in Farsi command mode, the character input must be in |
| 2848 | * Insert mode. So do not advance the cmdpos. |
| 2849 | */ |
| 2850 | if (!cmd_fkmap) |
| 2851 | #endif |
| 2852 | { |
| 2853 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2854 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2856 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2857 | m = MAXCOL; |
| 2858 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | else |
| 2860 | m = MAXCOL; |
| 2861 | for (i = 0; i < len; ++i) |
| 2862 | { |
| 2863 | c = cmdline_charsize(ccline.cmdpos); |
| 2864 | #ifdef FEAT_MBYTE |
| 2865 | /* count ">" for a double-wide char that doesn't fit. */ |
| 2866 | if (has_mbyte) |
| 2867 | correct_cmdspos(ccline.cmdpos, c); |
| 2868 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2869 | /* Stop cursor at the end of the screen, but do increment the |
| 2870 | * insert position, so that entering a very long command |
| 2871 | * works, even though you can't see it. */ |
| 2872 | if (ccline.cmdspos + c < m) |
| 2873 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2874 | #ifdef FEAT_MBYTE |
| 2875 | if (has_mbyte) |
| 2876 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2877 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2878 | if (c > len - i - 1) |
| 2879 | c = len - i - 1; |
| 2880 | ccline.cmdpos += c; |
| 2881 | i += c; |
| 2882 | } |
| 2883 | #endif |
| 2884 | ++ccline.cmdpos; |
| 2885 | } |
| 2886 | } |
| 2887 | } |
| 2888 | if (redraw) |
| 2889 | msg_check(); |
| 2890 | return retval; |
| 2891 | } |
| 2892 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2893 | static struct cmdline_info prev_ccline; |
| 2894 | static int prev_ccline_used = FALSE; |
| 2895 | |
| 2896 | /* |
| 2897 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 2898 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 2899 | * available globally in prev_ccline. |
| 2900 | */ |
| 2901 | static void |
| 2902 | save_cmdline(ccp) |
| 2903 | struct cmdline_info *ccp; |
| 2904 | { |
| 2905 | if (!prev_ccline_used) |
| 2906 | { |
| 2907 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 2908 | prev_ccline_used = TRUE; |
| 2909 | } |
| 2910 | *ccp = prev_ccline; |
| 2911 | prev_ccline = ccline; |
| 2912 | ccline.cmdbuff = NULL; |
| 2913 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2914 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 2918 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2919 | */ |
| 2920 | static void |
| 2921 | restore_cmdline(ccp) |
| 2922 | struct cmdline_info *ccp; |
| 2923 | { |
| 2924 | ccline = prev_ccline; |
| 2925 | prev_ccline = *ccp; |
| 2926 | } |
| 2927 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 2928 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2929 | /* |
| 2930 | * Save the command line into allocated memory. Returns a pointer to be |
| 2931 | * passed to restore_cmdline_alloc() later. |
| 2932 | * Returns NULL when failed. |
| 2933 | */ |
| 2934 | char_u * |
| 2935 | save_cmdline_alloc() |
| 2936 | { |
| 2937 | struct cmdline_info *p; |
| 2938 | |
| 2939 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 2940 | if (p != NULL) |
| 2941 | save_cmdline(p); |
| 2942 | return (char_u *)p; |
| 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 2947 | */ |
| 2948 | void |
| 2949 | restore_cmdline_alloc(p) |
| 2950 | char_u *p; |
| 2951 | { |
| 2952 | if (p != NULL) |
| 2953 | { |
| 2954 | restore_cmdline((struct cmdline_info *)p); |
| 2955 | vim_free(p); |
| 2956 | } |
| 2957 | } |
| 2958 | #endif |
| 2959 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 2960 | /* |
| 2961 | * paste a yank register into the command line. |
| 2962 | * used by CTRL-R command in command-line mode |
| 2963 | * insert_reg() can't be used here, because special characters from the |
| 2964 | * register contents will be interpreted as commands. |
| 2965 | * |
| 2966 | * return FAIL for failure, OK otherwise |
| 2967 | */ |
| 2968 | static int |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 2969 | cmdline_paste(regname, literally, remcr) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 2970 | int regname; |
| 2971 | int literally; /* Insert text literally instead of "as typed" */ |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 2972 | int remcr; /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 2973 | { |
| 2974 | long i; |
| 2975 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2976 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 2977 | int allocated; |
| 2978 | struct cmdline_info save_ccline; |
| 2979 | |
| 2980 | /* check for valid regname; also accept special characters for CTRL-R in |
| 2981 | * the command line */ |
| 2982 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 2983 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 2984 | return FAIL; |
| 2985 | |
| 2986 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 2987 | * CTRL-C to break the loop. */ |
| 2988 | line_breakcheck(); |
| 2989 | if (got_int) |
| 2990 | return FAIL; |
| 2991 | |
| 2992 | #ifdef FEAT_CLIPBOARD |
| 2993 | regname = may_get_selection(regname); |
| 2994 | #endif |
| 2995 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2996 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2997 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2998 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2999 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3000 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3001 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3002 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3003 | |
| 3004 | if (i) |
| 3005 | { |
| 3006 | /* Got the value of a special register in "arg". */ |
| 3007 | if (arg == NULL) |
| 3008 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3009 | |
| 3010 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3011 | * part of the word. */ |
| 3012 | p = arg; |
| 3013 | if (p_is && regname == Ctrl_W) |
| 3014 | { |
| 3015 | char_u *w; |
| 3016 | int len; |
| 3017 | |
| 3018 | /* Locate start of last word in the cmd buffer. */ |
| 3019 | for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; ) |
| 3020 | { |
| 3021 | #ifdef FEAT_MBYTE |
| 3022 | if (has_mbyte) |
| 3023 | { |
| 3024 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3025 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3026 | break; |
| 3027 | w -= len; |
| 3028 | } |
| 3029 | else |
| 3030 | #endif |
| 3031 | { |
| 3032 | if (!vim_iswordc(w[-1])) |
| 3033 | break; |
| 3034 | --w; |
| 3035 | } |
| 3036 | } |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3037 | len = (int)((ccline.cmdbuff + ccline.cmdlen) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3038 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3039 | p += len; |
| 3040 | } |
| 3041 | |
| 3042 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3043 | if (allocated) |
| 3044 | vim_free(arg); |
| 3045 | return OK; |
| 3046 | } |
| 3047 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3048 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Put a string on the command line. |
| 3053 | * When "literally" is TRUE, insert literally. |
| 3054 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3055 | * line. |
| 3056 | */ |
| 3057 | void |
| 3058 | cmdline_paste_str(s, literally) |
| 3059 | char_u *s; |
| 3060 | int literally; |
| 3061 | { |
| 3062 | int c, cv; |
| 3063 | |
| 3064 | if (literally) |
| 3065 | put_on_cmdline(s, -1, TRUE); |
| 3066 | else |
| 3067 | while (*s != NUL) |
| 3068 | { |
| 3069 | cv = *s; |
| 3070 | if (cv == Ctrl_V && s[1]) |
| 3071 | ++s; |
| 3072 | #ifdef FEAT_MBYTE |
| 3073 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3074 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3075 | else |
| 3076 | #endif |
| 3077 | c = *s++; |
| 3078 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL |
| 3079 | #ifdef UNIX |
| 3080 | || c == intr_char |
| 3081 | #endif |
| 3082 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3083 | stuffcharReadbuff(Ctrl_V); |
| 3084 | stuffcharReadbuff(c); |
| 3085 | } |
| 3086 | } |
| 3087 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3088 | #ifdef FEAT_WILDMENU |
| 3089 | /* |
| 3090 | * Delete characters on the command line, from "from" to the current |
| 3091 | * position. |
| 3092 | */ |
| 3093 | static void |
| 3094 | cmdline_del(from) |
| 3095 | int from; |
| 3096 | { |
| 3097 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3098 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3099 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3100 | ccline.cmdpos = from; |
| 3101 | } |
| 3102 | #endif |
| 3103 | |
| 3104 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3105 | * this function is called when the screen size changes and with incremental |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3106 | * search |
| 3107 | */ |
| 3108 | void |
| 3109 | redrawcmdline() |
| 3110 | { |
| 3111 | if (cmd_silent) |
| 3112 | return; |
| 3113 | need_wait_return = FALSE; |
| 3114 | compute_cmdrow(); |
| 3115 | redrawcmd(); |
| 3116 | cursorcmd(); |
| 3117 | } |
| 3118 | |
| 3119 | static void |
| 3120 | redrawcmdprompt() |
| 3121 | { |
| 3122 | int i; |
| 3123 | |
| 3124 | if (cmd_silent) |
| 3125 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3126 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3127 | msg_putchar(ccline.cmdfirstc); |
| 3128 | if (ccline.cmdprompt != NULL) |
| 3129 | { |
| 3130 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3131 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3132 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3133 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3134 | --ccline.cmdindent; |
| 3135 | } |
| 3136 | else |
| 3137 | for (i = ccline.cmdindent; i > 0; --i) |
| 3138 | msg_putchar(' '); |
| 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * Redraw what is currently on the command line. |
| 3143 | */ |
| 3144 | void |
| 3145 | redrawcmd() |
| 3146 | { |
| 3147 | if (cmd_silent) |
| 3148 | return; |
| 3149 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3150 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3151 | if (ccline.cmdbuff == NULL) |
| 3152 | { |
| 3153 | windgoto(cmdline_row, 0); |
| 3154 | msg_clr_eos(); |
| 3155 | return; |
| 3156 | } |
| 3157 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | msg_start(); |
| 3159 | redrawcmdprompt(); |
| 3160 | |
| 3161 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3162 | msg_no_more = TRUE; |
| 3163 | draw_cmdline(0, ccline.cmdlen); |
| 3164 | msg_clr_eos(); |
| 3165 | msg_no_more = FALSE; |
| 3166 | |
| 3167 | set_cmdspos_cursor(); |
| 3168 | |
| 3169 | /* |
| 3170 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3171 | * in cmdline mode we can reset them now. |
| 3172 | */ |
| 3173 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3174 | |
| 3175 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3176 | * in cmdline mode */ |
| 3177 | skip_redraw = FALSE; |
| 3178 | } |
| 3179 | |
| 3180 | void |
| 3181 | compute_cmdrow() |
| 3182 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3183 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3184 | cmdline_row = Rows - 1; |
| 3185 | else |
| 3186 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
| 3187 | + W_STATUS_HEIGHT(lastwin); |
| 3188 | } |
| 3189 | |
| 3190 | static void |
| 3191 | cursorcmd() |
| 3192 | { |
| 3193 | if (cmd_silent) |
| 3194 | return; |
| 3195 | |
| 3196 | #ifdef FEAT_RIGHTLEFT |
| 3197 | if (cmdmsg_rl) |
| 3198 | { |
| 3199 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3200 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3201 | if (msg_row <= 0) |
| 3202 | msg_row = Rows - 1; |
| 3203 | } |
| 3204 | else |
| 3205 | #endif |
| 3206 | { |
| 3207 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3208 | msg_col = ccline.cmdspos % (int)Columns; |
| 3209 | if (msg_row >= Rows) |
| 3210 | msg_row = Rows - 1; |
| 3211 | } |
| 3212 | |
| 3213 | windgoto(msg_row, msg_col); |
| 3214 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3215 | redrawcmd_preedit(); |
| 3216 | #endif |
| 3217 | #ifdef MCH_CURSOR_SHAPE |
| 3218 | mch_update_cursor(); |
| 3219 | #endif |
| 3220 | } |
| 3221 | |
| 3222 | void |
| 3223 | gotocmdline(clr) |
| 3224 | int clr; |
| 3225 | { |
| 3226 | msg_start(); |
| 3227 | #ifdef FEAT_RIGHTLEFT |
| 3228 | if (cmdmsg_rl) |
| 3229 | msg_col = Columns - 1; |
| 3230 | else |
| 3231 | #endif |
| 3232 | msg_col = 0; /* always start in column 0 */ |
| 3233 | if (clr) /* clear the bottom line(s) */ |
| 3234 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3235 | windgoto(cmdline_row, 0); |
| 3236 | } |
| 3237 | |
| 3238 | /* |
| 3239 | * Check the word in front of the cursor for an abbreviation. |
| 3240 | * Called when the non-id character "c" has been entered. |
| 3241 | * When an abbreviation is recognized it is removed from the text with |
| 3242 | * backspaces and the replacement string is inserted, followed by "c". |
| 3243 | */ |
| 3244 | static int |
| 3245 | ccheck_abbr(c) |
| 3246 | int c; |
| 3247 | { |
| 3248 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3249 | return FALSE; |
| 3250 | |
| 3251 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3252 | } |
| 3253 | |
| 3254 | /* |
| 3255 | * Return FAIL if this is not an appropriate context in which to do |
| 3256 | * completion of anything, return OK if it is (even if there are no matches). |
| 3257 | * For the caller, this means that the character is just passed through like a |
| 3258 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3259 | */ |
| 3260 | static int |
| 3261 | nextwild(xp, type, options) |
| 3262 | expand_T *xp; |
| 3263 | int type; |
| 3264 | int options; /* extra options for ExpandOne() */ |
| 3265 | { |
| 3266 | int i, j; |
| 3267 | char_u *p1; |
| 3268 | char_u *p2; |
| 3269 | int oldlen; |
| 3270 | int difflen; |
| 3271 | int v; |
| 3272 | |
| 3273 | if (xp->xp_numfiles == -1) |
| 3274 | { |
| 3275 | set_expand_context(xp); |
| 3276 | cmd_showtail = expand_showtail(xp); |
| 3277 | } |
| 3278 | |
| 3279 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3280 | { |
| 3281 | beep_flush(); |
| 3282 | return OK; /* Something illegal on command line */ |
| 3283 | } |
| 3284 | if (xp->xp_context == EXPAND_NOTHING) |
| 3285 | { |
| 3286 | /* Caller can use the character as a normal char instead */ |
| 3287 | return FAIL; |
| 3288 | } |
| 3289 | |
| 3290 | MSG_PUTS("..."); /* show that we are busy */ |
| 3291 | out_flush(); |
| 3292 | |
| 3293 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
| 3294 | oldlen = ccline.cmdpos - i; |
| 3295 | |
| 3296 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3297 | { |
| 3298 | /* |
| 3299 | * Get next/previous match for a previous expanded pattern. |
| 3300 | */ |
| 3301 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3302 | } |
| 3303 | else |
| 3304 | { |
| 3305 | /* |
| 3306 | * Translate string into pattern and expand it. |
| 3307 | */ |
| 3308 | if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL) |
| 3309 | p2 = NULL; |
| 3310 | else |
| 3311 | { |
| 3312 | p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen), |
| 3313 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE |
| 3314 | |options, type); |
| 3315 | vim_free(p1); |
| 3316 | /* longest match: make sure it is not shorter (happens with :help */ |
| 3317 | if (p2 != NULL && type == WILD_LONGEST) |
| 3318 | { |
| 3319 | for (j = 0; j < oldlen; ++j) |
| 3320 | if (ccline.cmdbuff[i + j] == '*' |
| 3321 | || ccline.cmdbuff[i + j] == '?') |
| 3322 | break; |
| 3323 | if ((int)STRLEN(p2) < j) |
| 3324 | { |
| 3325 | vim_free(p2); |
| 3326 | p2 = NULL; |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | if (p2 != NULL && !got_int) |
| 3333 | { |
| 3334 | difflen = (int)STRLEN(p2) - oldlen; |
| 3335 | if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4) |
| 3336 | { |
| 3337 | v = realloc_cmdbuff(ccline.cmdlen + difflen); |
| 3338 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3339 | } |
| 3340 | else |
| 3341 | v = OK; |
| 3342 | if (v == OK) |
| 3343 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3344 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3345 | &ccline.cmdbuff[ccline.cmdpos], |
| 3346 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3347 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3348 | ccline.cmdlen += difflen; |
| 3349 | ccline.cmdpos += difflen; |
| 3350 | } |
| 3351 | } |
| 3352 | vim_free(p2); |
| 3353 | |
| 3354 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3355 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3356 | |
| 3357 | /* When expanding a ":map" command and no matches are found, assume that |
| 3358 | * the key is supposed to be inserted literally */ |
| 3359 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3360 | return FAIL; |
| 3361 | |
| 3362 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3363 | beep_flush(); |
| 3364 | else if (xp->xp_numfiles == 1) |
| 3365 | /* free expanded pattern */ |
| 3366 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3367 | |
| 3368 | return OK; |
| 3369 | } |
| 3370 | |
| 3371 | /* |
| 3372 | * Do wildcard expansion on the string 'str'. |
| 3373 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3374 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3375 | * Return NULL for failure. |
| 3376 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3377 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3378 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3379 | * WILD_PREV "orig" should be NULL. |
| 3380 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3381 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3382 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | * |
| 3384 | * mode = WILD_FREE: just free previously expanded matches |
| 3385 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3386 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3387 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3388 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3389 | * mode = WILD_ALL: return all matches concatenated |
| 3390 | * mode = WILD_LONGEST: return longest matched part |
| 3391 | * |
| 3392 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3393 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3394 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3395 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3396 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3397 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3398 | * options = WILD_SILENT: don't print warning messages |
| 3399 | * options = WILD_ESCAPE: put backslash before special chars |
| 3400 | * |
| 3401 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3402 | */ |
| 3403 | char_u * |
| 3404 | ExpandOne(xp, str, orig, options, mode) |
| 3405 | expand_T *xp; |
| 3406 | char_u *str; |
| 3407 | char_u *orig; /* allocated copy of original of expanded string */ |
| 3408 | int options; |
| 3409 | int mode; |
| 3410 | { |
| 3411 | char_u *ss = NULL; |
| 3412 | static int findex; |
| 3413 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3414 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | int i; |
| 3416 | long_u len; |
| 3417 | int non_suf_match; /* number without matching suffix */ |
| 3418 | |
| 3419 | /* |
| 3420 | * first handle the case of using an old match |
| 3421 | */ |
| 3422 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3423 | { |
| 3424 | if (xp->xp_numfiles > 0) |
| 3425 | { |
| 3426 | if (mode == WILD_PREV) |
| 3427 | { |
| 3428 | if (findex == -1) |
| 3429 | findex = xp->xp_numfiles; |
| 3430 | --findex; |
| 3431 | } |
| 3432 | else /* mode == WILD_NEXT */ |
| 3433 | ++findex; |
| 3434 | |
| 3435 | /* |
| 3436 | * When wrapping around, return the original string, set findex to |
| 3437 | * -1. |
| 3438 | */ |
| 3439 | if (findex < 0) |
| 3440 | { |
| 3441 | if (orig_save == NULL) |
| 3442 | findex = xp->xp_numfiles - 1; |
| 3443 | else |
| 3444 | findex = -1; |
| 3445 | } |
| 3446 | if (findex >= xp->xp_numfiles) |
| 3447 | { |
| 3448 | if (orig_save == NULL) |
| 3449 | findex = 0; |
| 3450 | else |
| 3451 | findex = -1; |
| 3452 | } |
| 3453 | #ifdef FEAT_WILDMENU |
| 3454 | if (p_wmnu) |
| 3455 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3456 | findex, cmd_showtail); |
| 3457 | #endif |
| 3458 | if (findex == -1) |
| 3459 | return vim_strsave(orig_save); |
| 3460 | return vim_strsave(xp->xp_files[findex]); |
| 3461 | } |
| 3462 | else |
| 3463 | return NULL; |
| 3464 | } |
| 3465 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3466 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3468 | { |
| 3469 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3470 | xp->xp_numfiles = -1; |
| 3471 | vim_free(orig_save); |
| 3472 | orig_save = NULL; |
| 3473 | } |
| 3474 | findex = 0; |
| 3475 | |
| 3476 | if (mode == WILD_FREE) /* only release file name */ |
| 3477 | return NULL; |
| 3478 | |
| 3479 | if (xp->xp_numfiles == -1) |
| 3480 | { |
| 3481 | vim_free(orig_save); |
| 3482 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3483 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3484 | |
| 3485 | /* |
| 3486 | * Do the expansion. |
| 3487 | */ |
| 3488 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3489 | options) == FAIL) |
| 3490 | { |
| 3491 | #ifdef FNAME_ILLEGAL |
| 3492 | /* Illegal file name has been silently skipped. But when there |
| 3493 | * are wildcards, the real problem is that there was no match, |
| 3494 | * causing the pattern to be added, which has illegal characters. |
| 3495 | */ |
| 3496 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3497 | EMSG2(_(e_nomatch2), str); |
| 3498 | #endif |
| 3499 | } |
| 3500 | else if (xp->xp_numfiles == 0) |
| 3501 | { |
| 3502 | if (!(options & WILD_SILENT)) |
| 3503 | EMSG2(_(e_nomatch2), str); |
| 3504 | } |
| 3505 | else |
| 3506 | { |
| 3507 | /* Escape the matches for use on the command line. */ |
| 3508 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3509 | |
| 3510 | /* |
| 3511 | * Check for matching suffixes in file names. |
| 3512 | */ |
| 3513 | if (mode != WILD_ALL && mode != WILD_LONGEST) |
| 3514 | { |
| 3515 | if (xp->xp_numfiles) |
| 3516 | non_suf_match = xp->xp_numfiles; |
| 3517 | else |
| 3518 | non_suf_match = 1; |
| 3519 | if ((xp->xp_context == EXPAND_FILES |
| 3520 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3521 | && xp->xp_numfiles > 1) |
| 3522 | { |
| 3523 | /* |
| 3524 | * More than one match; check suffix. |
| 3525 | * The files will have been sorted on matching suffix in |
| 3526 | * expand_wildcards, only need to check the first two. |
| 3527 | */ |
| 3528 | non_suf_match = 0; |
| 3529 | for (i = 0; i < 2; ++i) |
| 3530 | if (match_suffix(xp->xp_files[i])) |
| 3531 | ++non_suf_match; |
| 3532 | } |
| 3533 | if (non_suf_match != 1) |
| 3534 | { |
| 3535 | /* Can we ever get here unless it's while expanding |
| 3536 | * interactively? If not, we can get rid of this all |
| 3537 | * together. Don't really want to wait for this message |
| 3538 | * (and possibly have to hit return to continue!). |
| 3539 | */ |
| 3540 | if (!(options & WILD_SILENT)) |
| 3541 | EMSG(_(e_toomany)); |
| 3542 | else if (!(options & WILD_NO_BEEP)) |
| 3543 | beep_flush(); |
| 3544 | } |
| 3545 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3546 | ss = vim_strsave(xp->xp_files[0]); |
| 3547 | } |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | /* Find longest common part */ |
| 3552 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3553 | { |
| 3554 | for (len = 0; xp->xp_files[0][len]; ++len) |
| 3555 | { |
| 3556 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3557 | { |
| 3558 | #ifdef CASE_INSENSITIVE_FILENAME |
| 3559 | if (xp->xp_context == EXPAND_DIRECTORIES |
| 3560 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3561 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3562 | || xp->xp_context == EXPAND_BUFFERS) |
| 3563 | { |
| 3564 | if (TOLOWER_LOC(xp->xp_files[i][len]) != |
| 3565 | TOLOWER_LOC(xp->xp_files[0][len])) |
| 3566 | break; |
| 3567 | } |
| 3568 | else |
| 3569 | #endif |
| 3570 | if (xp->xp_files[i][len] != xp->xp_files[0][len]) |
| 3571 | break; |
| 3572 | } |
| 3573 | if (i < xp->xp_numfiles) |
| 3574 | { |
| 3575 | if (!(options & WILD_NO_BEEP)) |
| 3576 | vim_beep(); |
| 3577 | break; |
| 3578 | } |
| 3579 | } |
| 3580 | ss = alloc((unsigned)len + 1); |
| 3581 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3582 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3583 | findex = -1; /* next p_wc gets first one */ |
| 3584 | } |
| 3585 | |
| 3586 | /* Concatenate all matching names */ |
| 3587 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3588 | { |
| 3589 | len = 0; |
| 3590 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3591 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3592 | ss = lalloc(len, TRUE); |
| 3593 | if (ss != NULL) |
| 3594 | { |
| 3595 | *ss = NUL; |
| 3596 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3597 | { |
| 3598 | STRCAT(ss, xp->xp_files[i]); |
| 3599 | if (i != xp->xp_numfiles - 1) |
| 3600 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3601 | } |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3606 | ExpandCleanup(xp); |
| 3607 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3608 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3609 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3610 | vim_free(orig); |
| 3611 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3612 | return ss; |
| 3613 | } |
| 3614 | |
| 3615 | /* |
| 3616 | * Prepare an expand structure for use. |
| 3617 | */ |
| 3618 | void |
| 3619 | ExpandInit(xp) |
| 3620 | expand_T *xp; |
| 3621 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3622 | xp->xp_pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3623 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3624 | #ifndef BACKSLASH_IN_FILENAME |
| 3625 | xp->xp_shell = FALSE; |
| 3626 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3627 | xp->xp_numfiles = -1; |
| 3628 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3629 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3630 | xp->xp_arg = NULL; |
| 3631 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* |
| 3635 | * Cleanup an expand structure after use. |
| 3636 | */ |
| 3637 | void |
| 3638 | ExpandCleanup(xp) |
| 3639 | expand_T *xp; |
| 3640 | { |
| 3641 | if (xp->xp_numfiles >= 0) |
| 3642 | { |
| 3643 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3644 | xp->xp_numfiles = -1; |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | void |
| 3649 | ExpandEscape(xp, str, numfiles, files, options) |
| 3650 | expand_T *xp; |
| 3651 | char_u *str; |
| 3652 | int numfiles; |
| 3653 | char_u **files; |
| 3654 | int options; |
| 3655 | { |
| 3656 | int i; |
| 3657 | char_u *p; |
| 3658 | |
| 3659 | /* |
| 3660 | * May change home directory back to "~" |
| 3661 | */ |
| 3662 | if (options & WILD_HOME_REPLACE) |
| 3663 | tilde_replace(str, numfiles, files); |
| 3664 | |
| 3665 | if (options & WILD_ESCAPE) |
| 3666 | { |
| 3667 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3668 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3669 | || xp->xp_context == EXPAND_BUFFERS |
| 3670 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3671 | { |
| 3672 | /* |
| 3673 | * Insert a backslash into a file name before a space, \, %, # |
| 3674 | * and wildmatch characters, except '~'. |
| 3675 | */ |
| 3676 | for (i = 0; i < numfiles; ++i) |
| 3677 | { |
| 3678 | /* for ":set path=" we need to escape spaces twice */ |
| 3679 | if (xp->xp_backslash == XP_BS_THREE) |
| 3680 | { |
| 3681 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3682 | if (p != NULL) |
| 3683 | { |
| 3684 | vim_free(files[i]); |
| 3685 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3686 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3687 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3688 | if (p != NULL) |
| 3689 | { |
| 3690 | vim_free(files[i]); |
| 3691 | files[i] = p; |
| 3692 | } |
| 3693 | #endif |
| 3694 | } |
| 3695 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3696 | #ifdef BACKSLASH_IN_FILENAME |
| 3697 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 3698 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3699 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3700 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3701 | if (p != NULL) |
| 3702 | { |
| 3703 | vim_free(files[i]); |
| 3704 | files[i] = p; |
| 3705 | } |
| 3706 | |
| 3707 | /* If 'str' starts with "\~", replace "~" at start of |
| 3708 | * files[i] with "\~". */ |
| 3709 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3710 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3711 | } |
| 3712 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* If the first file starts with a '+' escape it. Otherwise it |
| 3715 | * could be seen as "+cmd". */ |
| 3716 | if (*files[0] == '+') |
| 3717 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3718 | } |
| 3719 | else if (xp->xp_context == EXPAND_TAGS) |
| 3720 | { |
| 3721 | /* |
| 3722 | * Insert a backslash before characters in a tag name that |
| 3723 | * would terminate the ":tag" command. |
| 3724 | */ |
| 3725 | for (i = 0; i < numfiles; ++i) |
| 3726 | { |
| 3727 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 3728 | if (p != NULL) |
| 3729 | { |
| 3730 | vim_free(files[i]); |
| 3731 | files[i] = p; |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3739 | * Escape special characters in "fname" for when used as a file name argument |
| 3740 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 3741 | * Returns the result in allocated memory. |
| 3742 | */ |
| 3743 | char_u * |
| 3744 | vim_strsave_fnameescape(fname, shell) |
| 3745 | char_u *fname; |
| 3746 | int shell; |
| 3747 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3748 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3749 | #ifdef BACKSLASH_IN_FILENAME |
| 3750 | char_u buf[20]; |
| 3751 | int j = 0; |
| 3752 | |
| 3753 | /* Don't escape '[' and '{' if they are in 'isfname'. */ |
| 3754 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
| 3755 | if ((*p != '[' && *p != '{') || !vim_isfilec(*p)) |
| 3756 | buf[j++] = *p; |
| 3757 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3758 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3759 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3760 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 3761 | if (shell && csh_like_shell() && p != NULL) |
| 3762 | { |
| 3763 | char_u *s; |
| 3764 | |
| 3765 | /* For csh and similar shells need to put two backslashes before '!'. |
| 3766 | * One is taken by Vim, one by the shell. */ |
| 3767 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 3768 | vim_free(p); |
| 3769 | p = s; |
| 3770 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3771 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3772 | |
| 3773 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 3774 | * ":write". "cd -" has a special meaning. */ |
| 3775 | if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)) |
| 3776 | escape_fname(&p); |
| 3777 | |
| 3778 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3782 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 3783 | */ |
| 3784 | static void |
| 3785 | escape_fname(pp) |
| 3786 | char_u **pp; |
| 3787 | { |
| 3788 | char_u *p; |
| 3789 | |
| 3790 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 3791 | if (p != NULL) |
| 3792 | { |
| 3793 | p[0] = '\\'; |
| 3794 | STRCPY(p + 1, *pp); |
| 3795 | vim_free(*pp); |
| 3796 | *pp = p; |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3801 | * For each file name in files[num_files]: |
| 3802 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 3803 | */ |
| 3804 | void |
| 3805 | tilde_replace(orig_pat, num_files, files) |
| 3806 | char_u *orig_pat; |
| 3807 | int num_files; |
| 3808 | char_u **files; |
| 3809 | { |
| 3810 | int i; |
| 3811 | char_u *p; |
| 3812 | |
| 3813 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 3814 | { |
| 3815 | for (i = 0; i < num_files; ++i) |
| 3816 | { |
| 3817 | p = home_replace_save(NULL, files[i]); |
| 3818 | if (p != NULL) |
| 3819 | { |
| 3820 | vim_free(files[i]); |
| 3821 | files[i] = p; |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | /* |
| 3828 | * Show all matches for completion on the command line. |
| 3829 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 3830 | * be inserted like a normal character. |
| 3831 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | static int |
| 3833 | showmatches(xp, wildmenu) |
| 3834 | expand_T *xp; |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3835 | int wildmenu UNUSED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3836 | { |
| 3837 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 3838 | int num_files; |
| 3839 | char_u **files_found; |
| 3840 | int i, j, k; |
| 3841 | int maxlen; |
| 3842 | int lines; |
| 3843 | int columns; |
| 3844 | char_u *p; |
| 3845 | int lastlen; |
| 3846 | int attr; |
| 3847 | int showtail; |
| 3848 | |
| 3849 | if (xp->xp_numfiles == -1) |
| 3850 | { |
| 3851 | set_expand_context(xp); |
| 3852 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 3853 | &num_files, &files_found); |
| 3854 | showtail = expand_showtail(xp); |
| 3855 | if (i != EXPAND_OK) |
| 3856 | return i; |
| 3857 | |
| 3858 | } |
| 3859 | else |
| 3860 | { |
| 3861 | num_files = xp->xp_numfiles; |
| 3862 | files_found = xp->xp_files; |
| 3863 | showtail = cmd_showtail; |
| 3864 | } |
| 3865 | |
| 3866 | #ifdef FEAT_WILDMENU |
| 3867 | if (!wildmenu) |
| 3868 | { |
| 3869 | #endif |
| 3870 | msg_didany = FALSE; /* lines_left will be set */ |
| 3871 | msg_start(); /* prepare for paging */ |
| 3872 | msg_putchar('\n'); |
| 3873 | out_flush(); |
| 3874 | cmdline_row = msg_row; |
| 3875 | msg_didany = FALSE; /* lines_left will be set again */ |
| 3876 | msg_start(); /* prepare for paging */ |
| 3877 | #ifdef FEAT_WILDMENU |
| 3878 | } |
| 3879 | #endif |
| 3880 | |
| 3881 | if (got_int) |
| 3882 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 3883 | #ifdef FEAT_WILDMENU |
| 3884 | else if (wildmenu) |
| 3885 | win_redr_status_matches(xp, num_files, files_found, 0, showtail); |
| 3886 | #endif |
| 3887 | else |
| 3888 | { |
| 3889 | /* find the length of the longest file name */ |
| 3890 | maxlen = 0; |
| 3891 | for (i = 0; i < num_files; ++i) |
| 3892 | { |
| 3893 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3894 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3895 | || xp->xp_context == EXPAND_BUFFERS)) |
| 3896 | { |
| 3897 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 3898 | j = vim_strsize(NameBuff); |
| 3899 | } |
| 3900 | else |
| 3901 | j = vim_strsize(L_SHOWFILE(i)); |
| 3902 | if (j > maxlen) |
| 3903 | maxlen = j; |
| 3904 | } |
| 3905 | |
| 3906 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 3907 | lines = num_files; |
| 3908 | else |
| 3909 | { |
| 3910 | /* compute the number of columns and lines for the listing */ |
| 3911 | maxlen += 2; /* two spaces between file names */ |
| 3912 | columns = ((int)Columns + 2) / maxlen; |
| 3913 | if (columns < 1) |
| 3914 | columns = 1; |
| 3915 | lines = (num_files + columns - 1) / columns; |
| 3916 | } |
| 3917 | |
| 3918 | attr = hl_attr(HLF_D); /* find out highlighting for directories */ |
| 3919 | |
| 3920 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 3921 | { |
| 3922 | MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T)); |
| 3923 | msg_clr_eos(); |
| 3924 | msg_advance(maxlen - 3); |
| 3925 | MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T)); |
| 3926 | } |
| 3927 | |
| 3928 | /* list the files line by line */ |
| 3929 | for (i = 0; i < lines; ++i) |
| 3930 | { |
| 3931 | lastlen = 999; |
| 3932 | for (k = i; k < num_files; k += lines) |
| 3933 | { |
| 3934 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 3935 | { |
| 3936 | msg_outtrans_attr(files_found[k], hl_attr(HLF_D)); |
| 3937 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 3938 | msg_advance(maxlen + 1); |
| 3939 | msg_puts(p); |
| 3940 | msg_advance(maxlen + 3); |
| 3941 | msg_puts_long_attr(p + 2, hl_attr(HLF_D)); |
| 3942 | break; |
| 3943 | } |
| 3944 | for (j = maxlen - lastlen; --j >= 0; ) |
| 3945 | msg_putchar(' '); |
| 3946 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3947 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3948 | || xp->xp_context == EXPAND_BUFFERS) |
| 3949 | { |
| 3950 | /* highlight directories */ |
| 3951 | j = (mch_isdir(files_found[k])); |
| 3952 | if (showtail) |
| 3953 | p = L_SHOWFILE(k); |
| 3954 | else |
| 3955 | { |
| 3956 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 3957 | TRUE); |
| 3958 | p = NameBuff; |
| 3959 | } |
| 3960 | } |
| 3961 | else |
| 3962 | { |
| 3963 | j = FALSE; |
| 3964 | p = L_SHOWFILE(k); |
| 3965 | } |
| 3966 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 3967 | } |
| 3968 | if (msg_col > 0) /* when not wrapped around */ |
| 3969 | { |
| 3970 | msg_clr_eos(); |
| 3971 | msg_putchar('\n'); |
| 3972 | } |
| 3973 | out_flush(); /* show one line at a time */ |
| 3974 | if (got_int) |
| 3975 | { |
| 3976 | got_int = FALSE; |
| 3977 | break; |
| 3978 | } |
| 3979 | } |
| 3980 | |
| 3981 | /* |
| 3982 | * we redraw the command below the lines that we have just listed |
| 3983 | * This is a bit tricky, but it saves a lot of screen updating. |
| 3984 | */ |
| 3985 | cmdline_row = msg_row; /* will put it back later */ |
| 3986 | } |
| 3987 | |
| 3988 | if (xp->xp_numfiles == -1) |
| 3989 | FreeWild(num_files, files_found); |
| 3990 | |
| 3991 | return EXPAND_OK; |
| 3992 | } |
| 3993 | |
| 3994 | /* |
| 3995 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 3996 | * Find tail of file name path, but ignore trailing "/". |
| 3997 | */ |
| 3998 | char_u * |
| 3999 | sm_gettail(s) |
| 4000 | char_u *s; |
| 4001 | { |
| 4002 | char_u *p; |
| 4003 | char_u *t = s; |
| 4004 | int had_sep = FALSE; |
| 4005 | |
| 4006 | for (p = s; *p != NUL; ) |
| 4007 | { |
| 4008 | if (vim_ispathsep(*p) |
| 4009 | #ifdef BACKSLASH_IN_FILENAME |
| 4010 | && !rem_backslash(p) |
| 4011 | #endif |
| 4012 | ) |
| 4013 | had_sep = TRUE; |
| 4014 | else if (had_sep) |
| 4015 | { |
| 4016 | t = p; |
| 4017 | had_sep = FALSE; |
| 4018 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4019 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | } |
| 4021 | return t; |
| 4022 | } |
| 4023 | |
| 4024 | /* |
| 4025 | * Return TRUE if we only need to show the tail of completion matches. |
| 4026 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4027 | * returned. |
| 4028 | */ |
| 4029 | static int |
| 4030 | expand_showtail(xp) |
| 4031 | expand_T *xp; |
| 4032 | { |
| 4033 | char_u *s; |
| 4034 | char_u *end; |
| 4035 | |
| 4036 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4037 | if (xp->xp_context != EXPAND_FILES |
| 4038 | && xp->xp_context != EXPAND_SHELLCMD |
| 4039 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4040 | return FALSE; |
| 4041 | |
| 4042 | end = gettail(xp->xp_pattern); |
| 4043 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4044 | return FALSE; |
| 4045 | |
| 4046 | for (s = xp->xp_pattern; s < end; s++) |
| 4047 | { |
| 4048 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4049 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4050 | if (rem_backslash(s)) |
| 4051 | ++s; |
| 4052 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4053 | return FALSE; |
| 4054 | } |
| 4055 | return TRUE; |
| 4056 | } |
| 4057 | |
| 4058 | /* |
| 4059 | * Prepare a string for expansion. |
| 4060 | * When expanding file names: The string will be used with expand_wildcards(). |
| 4061 | * Copy the file name into allocated memory and add a '*' at the end. |
| 4062 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4063 | * the name into allocated memory and prepend "^". |
| 4064 | */ |
| 4065 | char_u * |
| 4066 | addstar(fname, len, context) |
| 4067 | char_u *fname; |
| 4068 | int len; |
| 4069 | int context; /* EXPAND_FILES etc. */ |
| 4070 | { |
| 4071 | char_u *retval; |
| 4072 | int i, j; |
| 4073 | int new_len; |
| 4074 | char_u *tail; |
| 4075 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4076 | if (context != EXPAND_FILES |
| 4077 | && context != EXPAND_SHELLCMD |
| 4078 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4079 | { |
| 4080 | /* |
| 4081 | * Matching will be done internally (on something other than files). |
| 4082 | * So we convert the file-matching-type wildcards into our kind for |
| 4083 | * use with vim_regcomp(). First work out how long it will be: |
| 4084 | */ |
| 4085 | |
| 4086 | /* For help tags the translation is done in find_help_tags(). |
| 4087 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4088 | if (context == EXPAND_HELP |
| 4089 | || context == EXPAND_COLORS |
| 4090 | || context == EXPAND_COMPILER |
| 4091 | || (context == EXPAND_TAGS && fname[0] == '/')) |
| 4092 | retval = vim_strnsave(fname, len); |
| 4093 | else |
| 4094 | { |
| 4095 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4096 | for (i = 0; i < len; i++) |
| 4097 | { |
| 4098 | if (fname[i] == '*' || fname[i] == '~') |
| 4099 | new_len++; /* '*' needs to be replaced by ".*" |
| 4100 | '~' needs to be replaced by "\~" */ |
| 4101 | |
| 4102 | /* Buffer names are like file names. "." should be literal */ |
| 4103 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4104 | new_len++; /* "." becomes "\." */ |
| 4105 | |
| 4106 | /* Custom expansion takes care of special things, match |
| 4107 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4108 | if ((context == EXPAND_USER_DEFINED |
| 4109 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4110 | new_len++; /* '\' becomes "\\" */ |
| 4111 | } |
| 4112 | retval = alloc(new_len); |
| 4113 | if (retval != NULL) |
| 4114 | { |
| 4115 | retval[0] = '^'; |
| 4116 | j = 1; |
| 4117 | for (i = 0; i < len; i++, j++) |
| 4118 | { |
| 4119 | /* Skip backslash. But why? At least keep it for custom |
| 4120 | * expansion. */ |
| 4121 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4122 | && context != EXPAND_USER_LIST |
| 4123 | && fname[i] == '\\' |
| 4124 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4125 | break; |
| 4126 | |
| 4127 | switch (fname[i]) |
| 4128 | { |
| 4129 | case '*': retval[j++] = '.'; |
| 4130 | break; |
| 4131 | case '~': retval[j++] = '\\'; |
| 4132 | break; |
| 4133 | case '?': retval[j] = '.'; |
| 4134 | continue; |
| 4135 | case '.': if (context == EXPAND_BUFFERS) |
| 4136 | retval[j++] = '\\'; |
| 4137 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4138 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4139 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4140 | retval[j++] = '\\'; |
| 4141 | break; |
| 4142 | } |
| 4143 | retval[j] = fname[i]; |
| 4144 | } |
| 4145 | retval[j] = NUL; |
| 4146 | } |
| 4147 | } |
| 4148 | } |
| 4149 | else |
| 4150 | { |
| 4151 | retval = alloc(len + 4); |
| 4152 | if (retval != NULL) |
| 4153 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4154 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4155 | |
| 4156 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4157 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4158 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4159 | * ~ would be at the start of the file name, but not the tail. |
| 4160 | * $ could be anywhere in the tail. |
| 4161 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4162 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | */ |
| 4164 | tail = gettail(retval); |
| 4165 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4166 | && (len == 0 || retval[len - 1] != '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4167 | && vim_strchr(tail, '$') == NULL |
| 4168 | && vim_strchr(retval, '`') == NULL) |
| 4169 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4170 | else if (len > 0 && retval[len - 1] == '$') |
| 4171 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4172 | retval[len] = NUL; |
| 4173 | } |
| 4174 | } |
| 4175 | return retval; |
| 4176 | } |
| 4177 | |
| 4178 | /* |
| 4179 | * Must parse the command line so far to work out what context we are in. |
| 4180 | * Completion can then be done based on that context. |
| 4181 | * This routine sets the variables: |
| 4182 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4183 | * the command line (ends at the cursor). |
| 4184 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4185 | * |
| 4186 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4187 | * the command line, like an unknown command. Caller |
| 4188 | * should beep. |
| 4189 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4190 | * a normal char, rather than for completion. eg |
| 4191 | * :s/^I/ |
| 4192 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4193 | * it. |
| 4194 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4195 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4196 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4197 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4198 | * when we know only directories are of interest. eg |
| 4199 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4200 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4202 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4203 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4204 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4205 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4206 | * EXPAND_EVENTS Complete event names |
| 4207 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4208 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4209 | * EXPAND_AUGROUP Complete autocommand group names |
| 4210 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4211 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4212 | * eg :unmap a^I , :cunab x^I |
| 4213 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4214 | * eg :call sub^I |
| 4215 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4216 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4217 | * names in expressions, eg :while s^I |
| 4218 | * EXPAND_ENV_VARS Complete environment variable names |
| 4219 | */ |
| 4220 | static void |
| 4221 | set_expand_context(xp) |
| 4222 | expand_T *xp; |
| 4223 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4224 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4225 | if (ccline.cmdfirstc != ':' |
| 4226 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4227 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4228 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4229 | #endif |
| 4230 | ) |
| 4231 | { |
| 4232 | xp->xp_context = EXPAND_NOTHING; |
| 4233 | return; |
| 4234 | } |
| 4235 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos); |
| 4236 | } |
| 4237 | |
| 4238 | void |
| 4239 | set_cmd_context(xp, str, len, col) |
| 4240 | expand_T *xp; |
| 4241 | char_u *str; /* start of command line */ |
| 4242 | int len; /* length of command line (excl. NUL) */ |
| 4243 | int col; /* position of cursor */ |
| 4244 | { |
| 4245 | int old_char = NUL; |
| 4246 | char_u *nextcomm; |
| 4247 | |
| 4248 | /* |
| 4249 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4250 | * written before. |
| 4251 | */ |
| 4252 | if (col < len) |
| 4253 | old_char = str[col]; |
| 4254 | str[col] = NUL; |
| 4255 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4256 | |
| 4257 | #ifdef FEAT_EVAL |
| 4258 | if (ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4259 | { |
| 4260 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4261 | /* pass CMD_SIZE because there is no real command */ |
| 4262 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4263 | # endif |
| 4264 | } |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4265 | else if (ccline.input_fn) |
| 4266 | { |
| 4267 | xp->xp_context = ccline.xp_context; |
| 4268 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4269 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4270 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4271 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4272 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4273 | else |
| 4274 | #endif |
| 4275 | while (nextcomm != NULL) |
| 4276 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4277 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4278 | str[col] = old_char; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * Expand the command line "str" from context "xp". |
| 4283 | * "xp" must have been set by set_cmd_context(). |
| 4284 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4285 | * starts. |
| 4286 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4287 | * cursor. |
| 4288 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4289 | * key that triggered expansion literally. |
| 4290 | * Returns EXPAND_OK otherwise. |
| 4291 | */ |
| 4292 | int |
| 4293 | expand_cmdline(xp, str, col, matchcount, matches) |
| 4294 | expand_T *xp; |
| 4295 | char_u *str; /* start of command line */ |
| 4296 | int col; /* position of cursor */ |
| 4297 | int *matchcount; /* return: nr of matches */ |
| 4298 | char_u ***matches; /* return: array of pointers to matches */ |
| 4299 | { |
| 4300 | char_u *file_str = NULL; |
| 4301 | |
| 4302 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4303 | { |
| 4304 | beep_flush(); |
| 4305 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4306 | } |
| 4307 | if (xp->xp_context == EXPAND_NOTHING) |
| 4308 | { |
| 4309 | /* Caller can use the character as a normal char instead */ |
| 4310 | return EXPAND_NOTHING; |
| 4311 | } |
| 4312 | |
| 4313 | /* add star to file name, or convert to regexp if not exp. files. */ |
| 4314 | file_str = addstar(xp->xp_pattern, |
| 4315 | (int)(str + col - xp->xp_pattern), xp->xp_context); |
| 4316 | if (file_str == NULL) |
| 4317 | return EXPAND_UNSUCCESSFUL; |
| 4318 | |
| 4319 | /* find all files that match the description */ |
| 4320 | if (ExpandFromContext(xp, file_str, matchcount, matches, |
| 4321 | WILD_ADD_SLASH|WILD_SILENT) == FAIL) |
| 4322 | { |
| 4323 | *matchcount = 0; |
| 4324 | *matches = NULL; |
| 4325 | } |
| 4326 | vim_free(file_str); |
| 4327 | |
| 4328 | return EXPAND_OK; |
| 4329 | } |
| 4330 | |
| 4331 | #ifdef FEAT_MULTI_LANG |
| 4332 | /* |
| 4333 | * Cleanup matches for help tags: remove "@en" if "en" is the only language. |
| 4334 | */ |
| 4335 | static void cleanup_help_tags __ARGS((int num_file, char_u **file)); |
| 4336 | |
| 4337 | static void |
| 4338 | cleanup_help_tags(num_file, file) |
| 4339 | int num_file; |
| 4340 | char_u **file; |
| 4341 | { |
| 4342 | int i, j; |
| 4343 | int len; |
| 4344 | |
| 4345 | for (i = 0; i < num_file; ++i) |
| 4346 | { |
| 4347 | len = (int)STRLEN(file[i]) - 3; |
| 4348 | if (len > 0 && STRCMP(file[i] + len, "@en") == 0) |
| 4349 | { |
| 4350 | /* Sorting on priority means the same item in another language may |
| 4351 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4352 | for (j = 0; j < num_file; ++j) |
| 4353 | if (j != i |
| 4354 | && (int)STRLEN(file[j]) == len + 3 |
| 4355 | && STRNCMP(file[i], file[j], len + 1) == 0) |
| 4356 | break; |
| 4357 | if (j == num_file) |
| 4358 | file[i][len] = NUL; |
| 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | #endif |
| 4363 | |
| 4364 | /* |
| 4365 | * Do the expansion based on xp->xp_context and "pat". |
| 4366 | */ |
| 4367 | static int |
| 4368 | ExpandFromContext(xp, pat, num_file, file, options) |
| 4369 | expand_T *xp; |
| 4370 | char_u *pat; |
| 4371 | int *num_file; |
| 4372 | char_u ***file; |
| 4373 | int options; |
| 4374 | { |
| 4375 | #ifdef FEAT_CMDL_COMPL |
| 4376 | regmatch_T regmatch; |
| 4377 | #endif |
| 4378 | int ret; |
| 4379 | int flags; |
| 4380 | |
| 4381 | flags = EW_DIR; /* include directories */ |
| 4382 | if (options & WILD_LIST_NOTFOUND) |
| 4383 | flags |= EW_NOTFOUND; |
| 4384 | if (options & WILD_ADD_SLASH) |
| 4385 | flags |= EW_ADDSLASH; |
| 4386 | if (options & WILD_KEEP_ALL) |
| 4387 | flags |= EW_KEEPALL; |
| 4388 | if (options & WILD_SILENT) |
| 4389 | flags |= EW_SILENT; |
| 4390 | |
| 4391 | if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES) |
| 4392 | { |
| 4393 | /* |
| 4394 | * Expand file or directory names. |
| 4395 | */ |
| 4396 | int free_pat = FALSE; |
| 4397 | int i; |
| 4398 | |
| 4399 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4400 | * space */ |
| 4401 | if (xp->xp_backslash != XP_BS_NONE) |
| 4402 | { |
| 4403 | free_pat = TRUE; |
| 4404 | pat = vim_strsave(pat); |
| 4405 | for (i = 0; pat[i]; ++i) |
| 4406 | if (pat[i] == '\\') |
| 4407 | { |
| 4408 | if (xp->xp_backslash == XP_BS_THREE |
| 4409 | && pat[i + 1] == '\\' |
| 4410 | && pat[i + 2] == '\\' |
| 4411 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4412 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4413 | if (xp->xp_backslash == XP_BS_ONE |
| 4414 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4415 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | if (xp->xp_context == EXPAND_FILES) |
| 4420 | flags |= EW_FILE; |
| 4421 | else |
| 4422 | flags = (flags | EW_DIR) & ~EW_FILE; |
| 4423 | ret = expand_wildcards(1, &pat, num_file, file, flags); |
| 4424 | if (free_pat) |
| 4425 | vim_free(pat); |
| 4426 | return ret; |
| 4427 | } |
| 4428 | |
| 4429 | *file = (char_u **)""; |
| 4430 | *num_file = 0; |
| 4431 | if (xp->xp_context == EXPAND_HELP) |
| 4432 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4433 | /* With an empty argument we would get all the help tags, which is |
| 4434 | * very slow. Get matches for "help" instead. */ |
| 4435 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4436 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4437 | { |
| 4438 | #ifdef FEAT_MULTI_LANG |
| 4439 | cleanup_help_tags(*num_file, *file); |
| 4440 | #endif |
| 4441 | return OK; |
| 4442 | } |
| 4443 | return FAIL; |
| 4444 | } |
| 4445 | |
| 4446 | #ifndef FEAT_CMDL_COMPL |
| 4447 | return FAIL; |
| 4448 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4449 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4450 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4452 | return ExpandOldSetting(num_file, file); |
| 4453 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4454 | return ExpandBufnames(pat, num_file, file, options); |
| 4455 | if (xp->xp_context == EXPAND_TAGS |
| 4456 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4457 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4458 | if (xp->xp_context == EXPAND_COLORS) |
| 4459 | return ExpandRTDir(pat, num_file, file, "colors"); |
| 4460 | if (xp->xp_context == EXPAND_COMPILER) |
| 4461 | return ExpandRTDir(pat, num_file, file, "compiler"); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4462 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4463 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4464 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4465 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4466 | |
| 4467 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4468 | if (regmatch.regprog == NULL) |
| 4469 | return FAIL; |
| 4470 | |
| 4471 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4472 | regmatch.rm_ic = ignorecase(pat); |
| 4473 | |
| 4474 | if (xp->xp_context == EXPAND_SETTINGS |
| 4475 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4476 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4477 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4478 | ret = ExpandMappings(®match, num_file, file); |
| 4479 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4480 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4481 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4482 | # endif |
| 4483 | else |
| 4484 | { |
| 4485 | static struct expgen |
| 4486 | { |
| 4487 | int context; |
| 4488 | char_u *((*func)__ARGS((expand_T *, int))); |
| 4489 | int ic; |
| 4490 | } tab[] = |
| 4491 | { |
| 4492 | {EXPAND_COMMANDS, get_command_name, FALSE}, |
| 4493 | #ifdef FEAT_USR_CMDS |
| 4494 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE}, |
| 4495 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE}, |
| 4496 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE}, |
| 4497 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE}, |
| 4498 | #endif |
| 4499 | #ifdef FEAT_EVAL |
| 4500 | {EXPAND_USER_VARS, get_user_var_name, FALSE}, |
| 4501 | {EXPAND_FUNCTIONS, get_function_name, FALSE}, |
| 4502 | {EXPAND_USER_FUNC, get_user_func_name, FALSE}, |
| 4503 | {EXPAND_EXPRESSION, get_expr_name, FALSE}, |
| 4504 | #endif |
| 4505 | #ifdef FEAT_MENU |
| 4506 | {EXPAND_MENUS, get_menu_name, FALSE}, |
| 4507 | {EXPAND_MENUNAMES, get_menu_names, FALSE}, |
| 4508 | #endif |
| 4509 | #ifdef FEAT_SYN_HL |
| 4510 | {EXPAND_SYNTAX, get_syntax_name, TRUE}, |
| 4511 | #endif |
| 4512 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE}, |
| 4513 | #ifdef FEAT_AUTOCMD |
| 4514 | {EXPAND_EVENTS, get_event_name, TRUE}, |
| 4515 | {EXPAND_AUGROUP, get_augroup_name, TRUE}, |
| 4516 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4517 | #ifdef FEAT_CSCOPE |
| 4518 | {EXPAND_CSCOPE, get_cscope_name, TRUE}, |
| 4519 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4520 | #ifdef FEAT_SIGNS |
| 4521 | {EXPAND_SIGN, get_sign_name, TRUE}, |
| 4522 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4523 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4524 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
| 4525 | {EXPAND_LANGUAGE, get_lang_arg, TRUE}, |
| 4526 | #endif |
| 4527 | {EXPAND_ENV_VARS, get_env_name, TRUE}, |
| 4528 | }; |
| 4529 | int i; |
| 4530 | |
| 4531 | /* |
| 4532 | * Find a context in the table and call the ExpandGeneric() with the |
| 4533 | * right function to do the expansion. |
| 4534 | */ |
| 4535 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 4536 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4537 | if (xp->xp_context == tab[i].context) |
| 4538 | { |
| 4539 | if (tab[i].ic) |
| 4540 | regmatch.rm_ic = TRUE; |
| 4541 | ret = ExpandGeneric(xp, ®match, num_file, file, tab[i].func); |
| 4542 | break; |
| 4543 | } |
| 4544 | } |
| 4545 | |
| 4546 | vim_free(regmatch.regprog); |
| 4547 | |
| 4548 | return ret; |
| 4549 | #endif /* FEAT_CMDL_COMPL */ |
| 4550 | } |
| 4551 | |
| 4552 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4553 | /* |
| 4554 | * Expand a list of names. |
| 4555 | * |
| 4556 | * Generic function for command line completion. It calls a function to |
| 4557 | * obtain strings, one by one. The strings are matched against a regexp |
| 4558 | * program. Matching strings are copied into an array, which is returned. |
| 4559 | * |
| 4560 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 4561 | */ |
| 4562 | int |
| 4563 | ExpandGeneric(xp, regmatch, num_file, file, func) |
| 4564 | expand_T *xp; |
| 4565 | regmatch_T *regmatch; |
| 4566 | int *num_file; |
| 4567 | char_u ***file; |
| 4568 | char_u *((*func)__ARGS((expand_T *, int))); |
| 4569 | /* returns a string from the list */ |
| 4570 | { |
| 4571 | int i; |
| 4572 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4573 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4574 | char_u *str; |
| 4575 | |
| 4576 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4577 | * round == 0: count the number of matching names |
| 4578 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4580 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4581 | { |
| 4582 | for (i = 0; ; ++i) |
| 4583 | { |
| 4584 | str = (*func)(xp, i); |
| 4585 | if (str == NULL) /* end of list */ |
| 4586 | break; |
| 4587 | if (*str == NUL) /* skip empty strings */ |
| 4588 | continue; |
| 4589 | |
| 4590 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 4591 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4592 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4593 | { |
| 4594 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 4595 | (*file)[count] = str; |
| 4596 | #ifdef FEAT_MENU |
| 4597 | if (func == get_menu_names && str != NULL) |
| 4598 | { |
| 4599 | /* test for separator added by get_menu_names() */ |
| 4600 | str += STRLEN(str) - 1; |
| 4601 | if (*str == '\001') |
| 4602 | *str = '.'; |
| 4603 | } |
| 4604 | #endif |
| 4605 | } |
| 4606 | ++count; |
| 4607 | } |
| 4608 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4609 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4610 | { |
| 4611 | if (count == 0) |
| 4612 | return OK; |
| 4613 | *num_file = count; |
| 4614 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 4615 | if (*file == NULL) |
| 4616 | { |
| 4617 | *file = (char_u **)""; |
| 4618 | return FAIL; |
| 4619 | } |
| 4620 | count = 0; |
| 4621 | } |
| 4622 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4623 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 4624 | /* Sort the results. Keep menu's in the specified order. */ |
| 4625 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
| 4626 | sort_strings(*file, *num_file); |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4627 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4628 | #ifdef FEAT_CMDL_COMPL |
| 4629 | /* Reset the variables used for special highlight names expansion, so that |
| 4630 | * they don't show up when getting normal highlight names by ID. */ |
| 4631 | reset_expand_highlight(); |
| 4632 | #endif |
| 4633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4634 | return OK; |
| 4635 | } |
| 4636 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4637 | /* |
| 4638 | * Complete a shell command. |
| 4639 | * Returns FAIL or OK; |
| 4640 | */ |
| 4641 | static int |
| 4642 | expand_shellcmd(filepat, num_file, file, flagsarg) |
| 4643 | char_u *filepat; /* pattern to match with command names */ |
| 4644 | int *num_file; /* return: number of matches */ |
| 4645 | char_u ***file; /* return: array with matches */ |
| 4646 | int flagsarg; /* EW_ flags */ |
| 4647 | { |
| 4648 | char_u *pat; |
| 4649 | int i; |
| 4650 | char_u *path; |
| 4651 | int mustfree = FALSE; |
| 4652 | garray_T ga; |
| 4653 | char_u *buf = alloc(MAXPATHL); |
| 4654 | size_t l; |
| 4655 | char_u *s, *e; |
| 4656 | int flags = flagsarg; |
| 4657 | int ret; |
| 4658 | |
| 4659 | if (buf == NULL) |
| 4660 | return FAIL; |
| 4661 | |
| 4662 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4663 | * space */ |
| 4664 | pat = vim_strsave(filepat); |
| 4665 | for (i = 0; pat[i]; ++i) |
| 4666 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4667 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4668 | |
| 4669 | flags |= EW_FILE | EW_EXEC; |
| 4670 | |
| 4671 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 4672 | if (mch_isFullName(pat)) |
| 4673 | path = (char_u *)" "; |
| 4674 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4675 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 4676 | path = (char_u *)"."; |
| 4677 | else |
| 4678 | path = vim_getenv((char_u *)"PATH", &mustfree); |
| 4679 | |
| 4680 | /* |
| 4681 | * Go over all directories in $PATH. Expand matches in that directory and |
| 4682 | * collect them in "ga". |
| 4683 | */ |
| 4684 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 4685 | for (s = path; *s != NUL; s = e) |
| 4686 | { |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 4687 | if (*s == ' ') |
| 4688 | ++s; /* Skip space used for absolute path name. */ |
| 4689 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4690 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 4691 | e = vim_strchr(s, ';'); |
| 4692 | #else |
| 4693 | e = vim_strchr(s, ':'); |
| 4694 | #endif |
| 4695 | if (e == NULL) |
| 4696 | e = s + STRLEN(s); |
| 4697 | |
| 4698 | l = e - s; |
| 4699 | if (l > MAXPATHL - 5) |
| 4700 | break; |
| 4701 | vim_strncpy(buf, s, l); |
| 4702 | add_pathsep(buf); |
| 4703 | l = STRLEN(buf); |
| 4704 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 4705 | |
| 4706 | /* Expand matches in one directory of $PATH. */ |
| 4707 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 4708 | if (ret == OK) |
| 4709 | { |
| 4710 | if (ga_grow(&ga, *num_file) == FAIL) |
| 4711 | FreeWild(*num_file, *file); |
| 4712 | else |
| 4713 | { |
| 4714 | for (i = 0; i < *num_file; ++i) |
| 4715 | { |
| 4716 | s = (*file)[i]; |
| 4717 | if (STRLEN(s) > l) |
| 4718 | { |
| 4719 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4720 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4721 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 4722 | } |
| 4723 | else |
| 4724 | vim_free(s); |
| 4725 | } |
| 4726 | vim_free(*file); |
| 4727 | } |
| 4728 | } |
| 4729 | if (*e != NUL) |
| 4730 | ++e; |
| 4731 | } |
| 4732 | *file = ga.ga_data; |
| 4733 | *num_file = ga.ga_len; |
| 4734 | |
| 4735 | vim_free(buf); |
| 4736 | vim_free(pat); |
| 4737 | if (mustfree) |
| 4738 | vim_free(path); |
| 4739 | return OK; |
| 4740 | } |
| 4741 | |
| 4742 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4743 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 4744 | static void * call_user_expand_func __ARGS((void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int)), expand_T *xp, int *num_file, char_u ***file)); |
| 4745 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4746 | /* |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 4747 | * 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] | 4748 | * the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4750 | static void * |
| 4751 | call_user_expand_func(user_expand_func, xp, num_file, file) |
| 4752 | void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4753 | expand_T *xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | int *num_file; |
| 4755 | char_u ***file; |
| 4756 | { |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4757 | char_u keep; |
| 4758 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4760 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4761 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 4762 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4763 | |
| 4764 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0') |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4765 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4766 | *num_file = 0; |
| 4767 | *file = NULL; |
| 4768 | |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 4769 | if (ccline.cmdbuff == NULL) |
| 4770 | { |
| 4771 | /* Completion from Insert mode, pass fake arguments. */ |
| 4772 | keep = 0; |
Bram Moolenaar | 9a31f88 | 2008-01-22 11:44:47 +0000 | [diff] [blame] | 4773 | sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern)); |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 4774 | args[1] = xp->xp_pattern; |
| 4775 | } |
| 4776 | else |
| 4777 | { |
| 4778 | /* Completion on the command line, pass real arguments. */ |
| 4779 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 4780 | ccline.cmdbuff[ccline.cmdlen] = 0; |
| 4781 | sprintf((char *)num, "%d", ccline.cmdpos); |
| 4782 | args[1] = ccline.cmdbuff; |
| 4783 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4784 | args[0] = xp->xp_pattern; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4785 | args[2] = num; |
| 4786 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 4787 | /* Save the cmdline, we don't know what the function may do. */ |
| 4788 | save_ccline = ccline; |
| 4789 | ccline.cmdbuff = NULL; |
| 4790 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4791 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 4792 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4793 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 4794 | |
| 4795 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 4797 | if (ccline.cmdbuff != NULL) |
| 4798 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4799 | |
| 4800 | return ret; |
| 4801 | } |
| 4802 | |
| 4803 | /* |
| 4804 | * Expand names with a function defined by the user. |
| 4805 | */ |
| 4806 | static int |
| 4807 | ExpandUserDefined(xp, regmatch, num_file, file) |
| 4808 | expand_T *xp; |
| 4809 | regmatch_T *regmatch; |
| 4810 | int *num_file; |
| 4811 | char_u ***file; |
| 4812 | { |
| 4813 | char_u *retstr; |
| 4814 | char_u *s; |
| 4815 | char_u *e; |
| 4816 | char_u keep; |
| 4817 | garray_T ga; |
| 4818 | |
| 4819 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 4820 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | return FAIL; |
| 4822 | |
| 4823 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4824 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4825 | { |
| 4826 | e = vim_strchr(s, '\n'); |
| 4827 | if (e == NULL) |
| 4828 | e = s + STRLEN(s); |
| 4829 | keep = *e; |
| 4830 | *e = 0; |
| 4831 | |
| 4832 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 4833 | { |
| 4834 | *e = keep; |
| 4835 | if (*e != NUL) |
| 4836 | ++e; |
| 4837 | continue; |
| 4838 | } |
| 4839 | |
| 4840 | if (ga_grow(&ga, 1) == FAIL) |
| 4841 | break; |
| 4842 | |
| 4843 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 4844 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4845 | |
| 4846 | *e = keep; |
| 4847 | if (*e != NUL) |
| 4848 | ++e; |
| 4849 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4850 | vim_free(retstr); |
| 4851 | *file = ga.ga_data; |
| 4852 | *num_file = ga.ga_len; |
| 4853 | return OK; |
| 4854 | } |
| 4855 | |
| 4856 | /* |
| 4857 | * Expand names with a list returned by a function defined by the user. |
| 4858 | */ |
| 4859 | static int |
| 4860 | ExpandUserList(xp, num_file, file) |
| 4861 | expand_T *xp; |
| 4862 | int *num_file; |
| 4863 | char_u ***file; |
| 4864 | { |
| 4865 | list_T *retlist; |
| 4866 | listitem_T *li; |
| 4867 | garray_T ga; |
| 4868 | |
| 4869 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 4870 | if (retlist == NULL) |
| 4871 | return FAIL; |
| 4872 | |
| 4873 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 4874 | /* Loop over the items in the list. */ |
| 4875 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 4876 | { |
| 4877 | if (li->li_tv.v_type != VAR_STRING) |
| 4878 | continue; /* Skip non-string items */ |
| 4879 | |
| 4880 | if (ga_grow(&ga, 1) == FAIL) |
| 4881 | break; |
| 4882 | |
| 4883 | ((char_u **)ga.ga_data)[ga.ga_len] = |
| 4884 | vim_strsave(li->li_tv.vval.v_string); |
| 4885 | ++ga.ga_len; |
| 4886 | } |
| 4887 | list_unref(retlist); |
| 4888 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4889 | *file = ga.ga_data; |
| 4890 | *num_file = ga.ga_len; |
| 4891 | return OK; |
| 4892 | } |
| 4893 | #endif |
| 4894 | |
| 4895 | /* |
| 4896 | * Expand color scheme names: 'runtimepath'/colors/{pat}.vim |
| 4897 | * or compiler names. |
| 4898 | */ |
| 4899 | static int |
| 4900 | ExpandRTDir(pat, num_file, file, dirname) |
| 4901 | char_u *pat; |
| 4902 | int *num_file; |
| 4903 | char_u ***file; |
| 4904 | char *dirname; /* "colors" or "compiler" */ |
| 4905 | { |
| 4906 | char_u *all; |
| 4907 | char_u *s; |
| 4908 | char_u *e; |
| 4909 | garray_T ga; |
| 4910 | |
| 4911 | *num_file = 0; |
| 4912 | *file = NULL; |
| 4913 | s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7)); |
| 4914 | if (s == NULL) |
| 4915 | return FAIL; |
| 4916 | sprintf((char *)s, "%s/%s*.vim", dirname, pat); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 4917 | all = globpath(p_rtp, s, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4918 | vim_free(s); |
| 4919 | if (all == NULL) |
| 4920 | return FAIL; |
| 4921 | |
| 4922 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 4923 | for (s = all; *s != NUL; s = e) |
| 4924 | { |
| 4925 | e = vim_strchr(s, '\n'); |
| 4926 | if (e == NULL) |
| 4927 | e = s + STRLEN(s); |
| 4928 | if (ga_grow(&ga, 1) == FAIL) |
| 4929 | break; |
| 4930 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 4931 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4932 | for (s = e - 4; s > all; mb_ptr_back(all, s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4933 | if (*s == '\n' || vim_ispathsep(*s)) |
| 4934 | break; |
| 4935 | ++s; |
| 4936 | ((char_u **)ga.ga_data)[ga.ga_len] = |
| 4937 | vim_strnsave(s, (int)(e - s - 4)); |
| 4938 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4939 | } |
| 4940 | if (*e != NUL) |
| 4941 | ++e; |
| 4942 | } |
| 4943 | vim_free(all); |
| 4944 | *file = ga.ga_data; |
| 4945 | *num_file = ga.ga_len; |
| 4946 | return OK; |
| 4947 | } |
| 4948 | |
| 4949 | #endif |
| 4950 | |
| 4951 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 4952 | /* |
| 4953 | * Expand "file" for all comma-separated directories in "path". |
| 4954 | * Returns an allocated string with all matches concatenated, separated by |
| 4955 | * newlines. Returns NULL for an error or no matches. |
| 4956 | */ |
| 4957 | char_u * |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 4958 | globpath(path, file, expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4959 | char_u *path; |
| 4960 | char_u *file; |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 4961 | int expand_options; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | { |
| 4963 | expand_T xpc; |
| 4964 | char_u *buf; |
| 4965 | garray_T ga; |
| 4966 | int i; |
| 4967 | int len; |
| 4968 | int num_p; |
| 4969 | char_u **p; |
| 4970 | char_u *cur = NULL; |
| 4971 | |
| 4972 | buf = alloc(MAXPATHL); |
| 4973 | if (buf == NULL) |
| 4974 | return NULL; |
| 4975 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4976 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4977 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4978 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | ga_init2(&ga, 1, 100); |
| 4980 | |
| 4981 | /* Loop over all entries in {path}. */ |
| 4982 | while (*path != NUL) |
| 4983 | { |
| 4984 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 4985 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 4986 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 4987 | { |
| 4988 | add_pathsep(buf); |
| 4989 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 4990 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 4991 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4992 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 4993 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4994 | for (len = 0, i = 0; i < num_p; ++i) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4995 | len += (int)STRLEN(p[i]) + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4996 | |
| 4997 | /* Concatenate new results to previous ones. */ |
| 4998 | if (ga_grow(&ga, len) == OK) |
| 4999 | { |
| 5000 | cur = (char_u *)ga.ga_data + ga.ga_len; |
| 5001 | for (i = 0; i < num_p; ++i) |
| 5002 | { |
| 5003 | STRCPY(cur, p[i]); |
| 5004 | cur += STRLEN(p[i]); |
| 5005 | *cur++ = '\n'; |
| 5006 | } |
| 5007 | ga.ga_len += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | } |
| 5009 | FreeWild(num_p, p); |
| 5010 | } |
| 5011 | } |
| 5012 | } |
| 5013 | if (cur != NULL) |
| 5014 | *--cur = 0; /* Replace trailing newline with NUL */ |
| 5015 | |
| 5016 | vim_free(buf); |
| 5017 | return (char_u *)ga.ga_data; |
| 5018 | } |
| 5019 | |
| 5020 | #endif |
| 5021 | |
| 5022 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5023 | |
| 5024 | /********************************* |
| 5025 | * Command line history stuff * |
| 5026 | *********************************/ |
| 5027 | |
| 5028 | /* |
| 5029 | * Translate a history character to the associated type number. |
| 5030 | */ |
| 5031 | static int |
| 5032 | hist_char2type(c) |
| 5033 | int c; |
| 5034 | { |
| 5035 | if (c == ':') |
| 5036 | return HIST_CMD; |
| 5037 | if (c == '=') |
| 5038 | return HIST_EXPR; |
| 5039 | if (c == '@') |
| 5040 | return HIST_INPUT; |
| 5041 | if (c == '>') |
| 5042 | return HIST_DEBUG; |
| 5043 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5044 | } |
| 5045 | |
| 5046 | /* |
| 5047 | * Table of history names. |
| 5048 | * These names are used in :history and various hist...() functions. |
| 5049 | * It is sufficient to give the significant prefix of a history name. |
| 5050 | */ |
| 5051 | |
| 5052 | static char *(history_names[]) = |
| 5053 | { |
| 5054 | "cmd", |
| 5055 | "search", |
| 5056 | "expr", |
| 5057 | "input", |
| 5058 | "debug", |
| 5059 | NULL |
| 5060 | }; |
| 5061 | |
| 5062 | /* |
| 5063 | * init_history() - Initialize the command line history. |
| 5064 | * Also used to re-allocate the history when the size changes. |
| 5065 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5066 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | init_history() |
| 5068 | { |
| 5069 | int newlen; /* new length of history table */ |
| 5070 | histentry_T *temp; |
| 5071 | int i; |
| 5072 | int j; |
| 5073 | int type; |
| 5074 | |
| 5075 | /* |
| 5076 | * If size of history table changed, reallocate it |
| 5077 | */ |
| 5078 | newlen = (int)p_hi; |
| 5079 | if (newlen != hislen) /* history length changed */ |
| 5080 | { |
| 5081 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5082 | { |
| 5083 | if (newlen) |
| 5084 | { |
| 5085 | temp = (histentry_T *)lalloc( |
| 5086 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5087 | if (temp == NULL) /* out of memory! */ |
| 5088 | { |
| 5089 | if (type == 0) /* first one: just keep the old length */ |
| 5090 | { |
| 5091 | newlen = hislen; |
| 5092 | break; |
| 5093 | } |
| 5094 | /* Already changed one table, now we can only have zero |
| 5095 | * length for all tables. */ |
| 5096 | newlen = 0; |
| 5097 | type = -1; |
| 5098 | continue; |
| 5099 | } |
| 5100 | } |
| 5101 | else |
| 5102 | temp = NULL; |
| 5103 | if (newlen == 0 || temp != NULL) |
| 5104 | { |
| 5105 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5106 | { |
| 5107 | for (i = 0; i < newlen; ++i) |
| 5108 | { |
| 5109 | temp[i].hisnum = 0; |
| 5110 | temp[i].hisstr = NULL; |
| 5111 | } |
| 5112 | } |
| 5113 | else if (newlen > hislen) /* array becomes bigger */ |
| 5114 | { |
| 5115 | for (i = 0; i <= hisidx[type]; ++i) |
| 5116 | temp[i] = history[type][i]; |
| 5117 | j = i; |
| 5118 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
| 5119 | { |
| 5120 | temp[i].hisnum = 0; |
| 5121 | temp[i].hisstr = NULL; |
| 5122 | } |
| 5123 | for ( ; j < hislen; ++i, ++j) |
| 5124 | temp[i] = history[type][j]; |
| 5125 | } |
| 5126 | else /* array becomes smaller or 0 */ |
| 5127 | { |
| 5128 | j = hisidx[type]; |
| 5129 | for (i = newlen - 1; ; --i) |
| 5130 | { |
| 5131 | if (i >= 0) /* copy newest entries */ |
| 5132 | temp[i] = history[type][j]; |
| 5133 | else /* remove older entries */ |
| 5134 | vim_free(history[type][j].hisstr); |
| 5135 | if (--j < 0) |
| 5136 | j = hislen - 1; |
| 5137 | if (j == hisidx[type]) |
| 5138 | break; |
| 5139 | } |
| 5140 | hisidx[type] = newlen - 1; |
| 5141 | } |
| 5142 | vim_free(history[type]); |
| 5143 | history[type] = temp; |
| 5144 | } |
| 5145 | } |
| 5146 | hislen = newlen; |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * Check if command line 'str' is already in history. |
| 5152 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5153 | */ |
| 5154 | static int |
| 5155 | in_history(type, str, move_to_front) |
| 5156 | int type; |
| 5157 | char_u *str; |
| 5158 | int move_to_front; /* Move the entry to the front if it exists */ |
| 5159 | { |
| 5160 | int i; |
| 5161 | int last_i = -1; |
| 5162 | |
| 5163 | if (hisidx[type] < 0) |
| 5164 | return FALSE; |
| 5165 | i = hisidx[type]; |
| 5166 | do |
| 5167 | { |
| 5168 | if (history[type][i].hisstr == NULL) |
| 5169 | return FALSE; |
| 5170 | if (STRCMP(str, history[type][i].hisstr) == 0) |
| 5171 | { |
| 5172 | if (!move_to_front) |
| 5173 | return TRUE; |
| 5174 | last_i = i; |
| 5175 | break; |
| 5176 | } |
| 5177 | if (--i < 0) |
| 5178 | i = hislen - 1; |
| 5179 | } while (i != hisidx[type]); |
| 5180 | |
| 5181 | if (last_i >= 0) |
| 5182 | { |
| 5183 | str = history[type][i].hisstr; |
| 5184 | while (i != hisidx[type]) |
| 5185 | { |
| 5186 | if (++i >= hislen) |
| 5187 | i = 0; |
| 5188 | history[type][last_i] = history[type][i]; |
| 5189 | last_i = i; |
| 5190 | } |
| 5191 | history[type][i].hisstr = str; |
| 5192 | history[type][i].hisnum = ++hisnum[type]; |
| 5193 | return TRUE; |
| 5194 | } |
| 5195 | return FALSE; |
| 5196 | } |
| 5197 | |
| 5198 | /* |
| 5199 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5200 | * When "name" is empty, return "cmd" history. |
| 5201 | * Returns -1 for unknown history name. |
| 5202 | */ |
| 5203 | int |
| 5204 | get_histtype(name) |
| 5205 | char_u *name; |
| 5206 | { |
| 5207 | int i; |
| 5208 | int len = (int)STRLEN(name); |
| 5209 | |
| 5210 | /* No argument: use current history. */ |
| 5211 | if (len == 0) |
| 5212 | return hist_char2type(ccline.cmdfirstc); |
| 5213 | |
| 5214 | for (i = 0; history_names[i] != NULL; ++i) |
| 5215 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5216 | return i; |
| 5217 | |
| 5218 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5219 | return hist_char2type(name[0]); |
| 5220 | |
| 5221 | return -1; |
| 5222 | } |
| 5223 | |
| 5224 | static int last_maptick = -1; /* last seen maptick */ |
| 5225 | |
| 5226 | /* |
| 5227 | * Add the given string to the given history. If the string is already in the |
| 5228 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5229 | * values. |
| 5230 | */ |
| 5231 | void |
| 5232 | add_to_history(histype, new_entry, in_map, sep) |
| 5233 | int histype; |
| 5234 | char_u *new_entry; |
| 5235 | int in_map; /* consider maptick when inside a mapping */ |
| 5236 | int sep; /* separator character used (search hist) */ |
| 5237 | { |
| 5238 | histentry_T *hisptr; |
| 5239 | int len; |
| 5240 | |
| 5241 | if (hislen == 0) /* no history */ |
| 5242 | return; |
| 5243 | |
| 5244 | /* |
| 5245 | * Searches inside the same mapping overwrite each other, so that only |
| 5246 | * the last line is kept. Be careful not to remove a line that was moved |
| 5247 | * down, only lines that were added. |
| 5248 | */ |
| 5249 | if (histype == HIST_SEARCH && in_map) |
| 5250 | { |
| 5251 | if (maptick == last_maptick) |
| 5252 | { |
| 5253 | /* Current line is from the same mapping, remove it */ |
| 5254 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5255 | vim_free(hisptr->hisstr); |
| 5256 | hisptr->hisstr = NULL; |
| 5257 | hisptr->hisnum = 0; |
| 5258 | --hisnum[histype]; |
| 5259 | if (--hisidx[HIST_SEARCH] < 0) |
| 5260 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5261 | } |
| 5262 | last_maptick = -1; |
| 5263 | } |
| 5264 | if (!in_history(histype, new_entry, TRUE)) |
| 5265 | { |
| 5266 | if (++hisidx[histype] == hislen) |
| 5267 | hisidx[histype] = 0; |
| 5268 | hisptr = &history[histype][hisidx[histype]]; |
| 5269 | vim_free(hisptr->hisstr); |
| 5270 | |
| 5271 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5272 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5273 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5274 | if (hisptr->hisstr != NULL) |
| 5275 | hisptr->hisstr[len + 1] = sep; |
| 5276 | |
| 5277 | hisptr->hisnum = ++hisnum[histype]; |
| 5278 | if (histype == HIST_SEARCH && in_map) |
| 5279 | last_maptick = maptick; |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5284 | |
| 5285 | /* |
| 5286 | * Get identifier of newest history entry. |
| 5287 | * "histype" may be one of the HIST_ values. |
| 5288 | */ |
| 5289 | int |
| 5290 | get_history_idx(histype) |
| 5291 | int histype; |
| 5292 | { |
| 5293 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5294 | || hisidx[histype] < 0) |
| 5295 | return -1; |
| 5296 | |
| 5297 | return history[histype][hisidx[histype]].hisnum; |
| 5298 | } |
| 5299 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5300 | static struct cmdline_info *get_ccline_ptr __ARGS((void)); |
| 5301 | |
| 5302 | /* |
| 5303 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 5304 | * ccline and put the previous value in prev_ccline. |
| 5305 | */ |
| 5306 | static struct cmdline_info * |
| 5307 | get_ccline_ptr() |
| 5308 | { |
| 5309 | if ((State & CMDLINE) == 0) |
| 5310 | return NULL; |
| 5311 | if (ccline.cmdbuff != NULL) |
| 5312 | return &ccline; |
| 5313 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 5314 | return &prev_ccline; |
| 5315 | return NULL; |
| 5316 | } |
| 5317 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | /* |
| 5319 | * Get the current command line in allocated memory. |
| 5320 | * Only works when the command line is being edited. |
| 5321 | * Returns NULL when something is wrong. |
| 5322 | */ |
| 5323 | char_u * |
| 5324 | get_cmdline_str() |
| 5325 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5326 | struct cmdline_info *p = get_ccline_ptr(); |
| 5327 | |
| 5328 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | return NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5330 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | /* |
| 5334 | * Get the current command line position, counted in bytes. |
| 5335 | * Zero is the first position. |
| 5336 | * Only works when the command line is being edited. |
| 5337 | * Returns -1 when something is wrong. |
| 5338 | */ |
| 5339 | int |
| 5340 | get_cmdline_pos() |
| 5341 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5342 | struct cmdline_info *p = get_ccline_ptr(); |
| 5343 | |
| 5344 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5345 | return -1; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5346 | return p->cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /* |
| 5350 | * Set the command line byte position to "pos". Zero is the first position. |
| 5351 | * Only works when the command line is being edited. |
| 5352 | * Returns 1 when failed, 0 when OK. |
| 5353 | */ |
| 5354 | int |
| 5355 | set_cmdline_pos(pos) |
| 5356 | int pos; |
| 5357 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 5358 | struct cmdline_info *p = get_ccline_ptr(); |
| 5359 | |
| 5360 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5361 | return 1; |
| 5362 | |
| 5363 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 5364 | * changed the command line. */ |
| 5365 | if (pos < 0) |
| 5366 | new_cmdpos = 0; |
| 5367 | else |
| 5368 | new_cmdpos = pos; |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
| 5372 | /* |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5373 | * Get the current command-line type. |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 5374 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5375 | * Only works when the command line is being edited. |
| 5376 | * Returns NUL when something is wrong. |
| 5377 | */ |
| 5378 | int |
| 5379 | get_cmdline_type() |
| 5380 | { |
| 5381 | struct cmdline_info *p = get_ccline_ptr(); |
| 5382 | |
| 5383 | if (p == NULL) |
| 5384 | return NUL; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 5385 | if (p->cmdfirstc == NUL) |
| 5386 | return (p->input_fn) ? '@' : '-'; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5387 | return p->cmdfirstc; |
| 5388 | } |
| 5389 | |
| 5390 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5391 | * Calculate history index from a number: |
| 5392 | * num > 0: seen as identifying number of a history entry |
| 5393 | * num < 0: relative position in history wrt newest entry |
| 5394 | * "histype" may be one of the HIST_ values. |
| 5395 | */ |
| 5396 | static int |
| 5397 | calc_hist_idx(histype, num) |
| 5398 | int histype; |
| 5399 | int num; |
| 5400 | { |
| 5401 | int i; |
| 5402 | histentry_T *hist; |
| 5403 | int wrapped = FALSE; |
| 5404 | |
| 5405 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5406 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5407 | return -1; |
| 5408 | |
| 5409 | hist = history[histype]; |
| 5410 | if (num > 0) |
| 5411 | { |
| 5412 | while (hist[i].hisnum > num) |
| 5413 | if (--i < 0) |
| 5414 | { |
| 5415 | if (wrapped) |
| 5416 | break; |
| 5417 | i += hislen; |
| 5418 | wrapped = TRUE; |
| 5419 | } |
| 5420 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5421 | return i; |
| 5422 | } |
| 5423 | else if (-num <= hislen) |
| 5424 | { |
| 5425 | i += num + 1; |
| 5426 | if (i < 0) |
| 5427 | i += hislen; |
| 5428 | if (hist[i].hisstr != NULL) |
| 5429 | return i; |
| 5430 | } |
| 5431 | return -1; |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * Get a history entry by its index. |
| 5436 | * "histype" may be one of the HIST_ values. |
| 5437 | */ |
| 5438 | char_u * |
| 5439 | get_history_entry(histype, idx) |
| 5440 | int histype; |
| 5441 | int idx; |
| 5442 | { |
| 5443 | idx = calc_hist_idx(histype, idx); |
| 5444 | if (idx >= 0) |
| 5445 | return history[histype][idx].hisstr; |
| 5446 | else |
| 5447 | return (char_u *)""; |
| 5448 | } |
| 5449 | |
| 5450 | /* |
| 5451 | * Clear all entries of a history. |
| 5452 | * "histype" may be one of the HIST_ values. |
| 5453 | */ |
| 5454 | int |
| 5455 | clr_history(histype) |
| 5456 | int histype; |
| 5457 | { |
| 5458 | int i; |
| 5459 | histentry_T *hisptr; |
| 5460 | |
| 5461 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5462 | { |
| 5463 | hisptr = history[histype]; |
| 5464 | for (i = hislen; i--;) |
| 5465 | { |
| 5466 | vim_free(hisptr->hisstr); |
| 5467 | hisptr->hisnum = 0; |
| 5468 | hisptr++->hisstr = NULL; |
| 5469 | } |
| 5470 | hisidx[histype] = -1; /* mark history as cleared */ |
| 5471 | hisnum[histype] = 0; /* reset identifier counter */ |
| 5472 | return OK; |
| 5473 | } |
| 5474 | return FAIL; |
| 5475 | } |
| 5476 | |
| 5477 | /* |
| 5478 | * Remove all entries matching {str} from a history. |
| 5479 | * "histype" may be one of the HIST_ values. |
| 5480 | */ |
| 5481 | int |
| 5482 | del_history_entry(histype, str) |
| 5483 | int histype; |
| 5484 | char_u *str; |
| 5485 | { |
| 5486 | regmatch_T regmatch; |
| 5487 | histentry_T *hisptr; |
| 5488 | int idx; |
| 5489 | int i; |
| 5490 | int last; |
| 5491 | int found = FALSE; |
| 5492 | |
| 5493 | regmatch.regprog = NULL; |
| 5494 | regmatch.rm_ic = FALSE; /* always match case */ |
| 5495 | if (hislen != 0 |
| 5496 | && histype >= 0 |
| 5497 | && histype < HIST_COUNT |
| 5498 | && *str != NUL |
| 5499 | && (idx = hisidx[histype]) >= 0 |
| 5500 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 5501 | != NULL) |
| 5502 | { |
| 5503 | i = last = idx; |
| 5504 | do |
| 5505 | { |
| 5506 | hisptr = &history[histype][i]; |
| 5507 | if (hisptr->hisstr == NULL) |
| 5508 | break; |
| 5509 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 5510 | { |
| 5511 | found = TRUE; |
| 5512 | vim_free(hisptr->hisstr); |
| 5513 | hisptr->hisstr = NULL; |
| 5514 | hisptr->hisnum = 0; |
| 5515 | } |
| 5516 | else |
| 5517 | { |
| 5518 | if (i != last) |
| 5519 | { |
| 5520 | history[histype][last] = *hisptr; |
| 5521 | hisptr->hisstr = NULL; |
| 5522 | hisptr->hisnum = 0; |
| 5523 | } |
| 5524 | if (--last < 0) |
| 5525 | last += hislen; |
| 5526 | } |
| 5527 | if (--i < 0) |
| 5528 | i += hislen; |
| 5529 | } while (i != idx); |
| 5530 | if (history[histype][idx].hisstr == NULL) |
| 5531 | hisidx[histype] = -1; |
| 5532 | } |
| 5533 | vim_free(regmatch.regprog); |
| 5534 | return found; |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * Remove an indexed entry from a history. |
| 5539 | * "histype" may be one of the HIST_ values. |
| 5540 | */ |
| 5541 | int |
| 5542 | del_history_idx(histype, idx) |
| 5543 | int histype; |
| 5544 | int idx; |
| 5545 | { |
| 5546 | int i, j; |
| 5547 | |
| 5548 | i = calc_hist_idx(histype, idx); |
| 5549 | if (i < 0) |
| 5550 | return FALSE; |
| 5551 | idx = hisidx[histype]; |
| 5552 | vim_free(history[histype][i].hisstr); |
| 5553 | |
| 5554 | /* When deleting the last added search string in a mapping, reset |
| 5555 | * last_maptick, so that the last added search string isn't deleted again. |
| 5556 | */ |
| 5557 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 5558 | last_maptick = -1; |
| 5559 | |
| 5560 | while (i != idx) |
| 5561 | { |
| 5562 | j = (i + 1) % hislen; |
| 5563 | history[histype][i] = history[histype][j]; |
| 5564 | i = j; |
| 5565 | } |
| 5566 | history[histype][i].hisstr = NULL; |
| 5567 | history[histype][i].hisnum = 0; |
| 5568 | if (--i < 0) |
| 5569 | i += hislen; |
| 5570 | hisidx[histype] = i; |
| 5571 | return TRUE; |
| 5572 | } |
| 5573 | |
| 5574 | #endif /* FEAT_EVAL */ |
| 5575 | |
| 5576 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 5577 | /* |
| 5578 | * Very specific function to remove the value in ":set key=val" from the |
| 5579 | * history. |
| 5580 | */ |
| 5581 | void |
| 5582 | remove_key_from_history() |
| 5583 | { |
| 5584 | char_u *p; |
| 5585 | int i; |
| 5586 | |
| 5587 | i = hisidx[HIST_CMD]; |
| 5588 | if (i < 0) |
| 5589 | return; |
| 5590 | p = history[HIST_CMD][i].hisstr; |
| 5591 | if (p != NULL) |
| 5592 | for ( ; *p; ++p) |
| 5593 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 5594 | { |
| 5595 | p = vim_strchr(p + 3, '='); |
| 5596 | if (p == NULL) |
| 5597 | break; |
| 5598 | ++p; |
| 5599 | for (i = 0; p[i] && !vim_iswhite(p[i]); ++i) |
| 5600 | if (p[i] == '\\' && p[i + 1]) |
| 5601 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5602 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5603 | --p; |
| 5604 | } |
| 5605 | } |
| 5606 | #endif |
| 5607 | |
| 5608 | #endif /* FEAT_CMDHIST */ |
| 5609 | |
| 5610 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 5611 | /* |
| 5612 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 5613 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 5614 | * Returns OK if parsed successfully, otherwise FAIL. |
| 5615 | */ |
| 5616 | int |
| 5617 | get_list_range(str, num1, num2) |
| 5618 | char_u **str; |
| 5619 | int *num1; |
| 5620 | int *num2; |
| 5621 | { |
| 5622 | int len; |
| 5623 | int first = FALSE; |
| 5624 | long num; |
| 5625 | |
| 5626 | *str = skipwhite(*str); |
| 5627 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 5628 | { |
| 5629 | vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL); |
| 5630 | *str += len; |
| 5631 | *num1 = (int)num; |
| 5632 | first = TRUE; |
| 5633 | } |
| 5634 | *str = skipwhite(*str); |
| 5635 | if (**str == ',') /* parse "to" part of range */ |
| 5636 | { |
| 5637 | *str = skipwhite(*str + 1); |
| 5638 | vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL); |
| 5639 | if (len > 0) |
| 5640 | { |
| 5641 | *num2 = (int)num; |
| 5642 | *str = skipwhite(*str + len); |
| 5643 | } |
| 5644 | else if (!first) /* no number given at all */ |
| 5645 | return FAIL; |
| 5646 | } |
| 5647 | else if (first) /* only one number given */ |
| 5648 | *num2 = *num1; |
| 5649 | return OK; |
| 5650 | } |
| 5651 | #endif |
| 5652 | |
| 5653 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5654 | /* |
| 5655 | * :history command - print a history |
| 5656 | */ |
| 5657 | void |
| 5658 | ex_history(eap) |
| 5659 | exarg_T *eap; |
| 5660 | { |
| 5661 | histentry_T *hist; |
| 5662 | int histype1 = HIST_CMD; |
| 5663 | int histype2 = HIST_CMD; |
| 5664 | int hisidx1 = 1; |
| 5665 | int hisidx2 = -1; |
| 5666 | int idx; |
| 5667 | int i, j, k; |
| 5668 | char_u *end; |
| 5669 | char_u *arg = eap->arg; |
| 5670 | |
| 5671 | if (hislen == 0) |
| 5672 | { |
| 5673 | MSG(_("'history' option is zero")); |
| 5674 | return; |
| 5675 | } |
| 5676 | |
| 5677 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 5678 | { |
| 5679 | end = arg; |
| 5680 | while (ASCII_ISALPHA(*end) |
| 5681 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 5682 | end++; |
| 5683 | i = *end; |
| 5684 | *end = NUL; |
| 5685 | histype1 = get_histtype(arg); |
| 5686 | if (histype1 == -1) |
| 5687 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 5688 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5689 | { |
| 5690 | histype1 = 0; |
| 5691 | histype2 = HIST_COUNT-1; |
| 5692 | } |
| 5693 | else |
| 5694 | { |
| 5695 | *end = i; |
| 5696 | EMSG(_(e_trailing)); |
| 5697 | return; |
| 5698 | } |
| 5699 | } |
| 5700 | else |
| 5701 | histype2 = histype1; |
| 5702 | *end = i; |
| 5703 | } |
| 5704 | else |
| 5705 | end = arg; |
| 5706 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 5707 | { |
| 5708 | EMSG(_(e_trailing)); |
| 5709 | return; |
| 5710 | } |
| 5711 | |
| 5712 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 5713 | { |
| 5714 | STRCPY(IObuff, "\n # "); |
| 5715 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 5716 | MSG_PUTS_TITLE(IObuff); |
| 5717 | idx = hisidx[histype1]; |
| 5718 | hist = history[histype1]; |
| 5719 | j = hisidx1; |
| 5720 | k = hisidx2; |
| 5721 | if (j < 0) |
| 5722 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 5723 | if (k < 0) |
| 5724 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 5725 | if (idx >= 0 && j <= k) |
| 5726 | for (i = idx + 1; !got_int; ++i) |
| 5727 | { |
| 5728 | if (i == hislen) |
| 5729 | i = 0; |
| 5730 | if (hist[i].hisstr != NULL |
| 5731 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 5732 | { |
| 5733 | msg_putchar('\n'); |
| 5734 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 5735 | hist[i].hisnum); |
| 5736 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 5737 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
| 5738 | (int)Columns - 10); |
| 5739 | else |
| 5740 | STRCAT(IObuff, hist[i].hisstr); |
| 5741 | msg_outtrans(IObuff); |
| 5742 | out_flush(); |
| 5743 | } |
| 5744 | if (i == idx) |
| 5745 | break; |
| 5746 | } |
| 5747 | } |
| 5748 | } |
| 5749 | #endif |
| 5750 | |
| 5751 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
| 5752 | static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL}; |
| 5753 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0}; |
| 5754 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0}; |
| 5755 | static int viminfo_add_at_front = FALSE; |
| 5756 | |
| 5757 | static int hist_type2char __ARGS((int type, int use_question)); |
| 5758 | |
| 5759 | /* |
| 5760 | * Translate a history type number to the associated character. |
| 5761 | */ |
| 5762 | static int |
| 5763 | hist_type2char(type, use_question) |
| 5764 | int type; |
| 5765 | int use_question; /* use '?' instead of '/' */ |
| 5766 | { |
| 5767 | if (type == HIST_CMD) |
| 5768 | return ':'; |
| 5769 | if (type == HIST_SEARCH) |
| 5770 | { |
| 5771 | if (use_question) |
| 5772 | return '?'; |
| 5773 | else |
| 5774 | return '/'; |
| 5775 | } |
| 5776 | if (type == HIST_EXPR) |
| 5777 | return '='; |
| 5778 | return '@'; |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * Prepare for reading the history from the viminfo file. |
| 5783 | * This allocates history arrays to store the read history lines. |
| 5784 | */ |
| 5785 | void |
| 5786 | prepare_viminfo_history(asklen) |
| 5787 | int asklen; |
| 5788 | { |
| 5789 | int i; |
| 5790 | int num; |
| 5791 | int type; |
| 5792 | int len; |
| 5793 | |
| 5794 | init_history(); |
| 5795 | viminfo_add_at_front = (asklen != 0); |
| 5796 | if (asklen > hislen) |
| 5797 | asklen = hislen; |
| 5798 | |
| 5799 | for (type = 0; type < HIST_COUNT; ++type) |
| 5800 | { |
| 5801 | /* |
| 5802 | * Count the number of empty spaces in the history list. If there are |
| 5803 | * more spaces available than we request, then fill them up. |
| 5804 | */ |
| 5805 | for (i = 0, num = 0; i < hislen; i++) |
| 5806 | if (history[type][i].hisstr == NULL) |
| 5807 | num++; |
| 5808 | len = asklen; |
| 5809 | if (num > len) |
| 5810 | len = num; |
| 5811 | if (len <= 0) |
| 5812 | viminfo_history[type] = NULL; |
| 5813 | else |
| 5814 | viminfo_history[type] = |
| 5815 | (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE); |
| 5816 | if (viminfo_history[type] == NULL) |
| 5817 | len = 0; |
| 5818 | viminfo_hislen[type] = len; |
| 5819 | viminfo_hisidx[type] = 0; |
| 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | /* |
| 5824 | * Accept a line from the viminfo, store it in the history array when it's |
| 5825 | * new. |
| 5826 | */ |
| 5827 | int |
| 5828 | read_viminfo_history(virp) |
| 5829 | vir_T *virp; |
| 5830 | { |
| 5831 | int type; |
| 5832 | long_u len; |
| 5833 | char_u *val; |
| 5834 | char_u *p; |
| 5835 | |
| 5836 | type = hist_char2type(virp->vir_line[0]); |
| 5837 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 5838 | { |
| 5839 | val = viminfo_readstring(virp, 1, TRUE); |
| 5840 | if (val != NULL && *val != NUL) |
| 5841 | { |
| 5842 | if (!in_history(type, val + (type == HIST_SEARCH), |
| 5843 | viminfo_add_at_front)) |
| 5844 | { |
| 5845 | /* Need to re-allocate to append the separator byte. */ |
| 5846 | len = STRLEN(val); |
| 5847 | p = lalloc(len + 2, TRUE); |
| 5848 | if (p != NULL) |
| 5849 | { |
| 5850 | if (type == HIST_SEARCH) |
| 5851 | { |
| 5852 | /* Search entry: Move the separator from the first |
| 5853 | * column to after the NUL. */ |
| 5854 | mch_memmove(p, val + 1, (size_t)len); |
| 5855 | p[len] = (*val == ' ' ? NUL : *val); |
| 5856 | } |
| 5857 | else |
| 5858 | { |
| 5859 | /* Not a search entry: No separator in the viminfo |
| 5860 | * file, add a NUL separator. */ |
| 5861 | mch_memmove(p, val, (size_t)len + 1); |
| 5862 | p[len + 1] = NUL; |
| 5863 | } |
| 5864 | viminfo_history[type][viminfo_hisidx[type]++] = p; |
| 5865 | } |
| 5866 | } |
| 5867 | } |
| 5868 | vim_free(val); |
| 5869 | } |
| 5870 | return viminfo_readline(virp); |
| 5871 | } |
| 5872 | |
| 5873 | void |
| 5874 | finish_viminfo_history() |
| 5875 | { |
| 5876 | int idx; |
| 5877 | int i; |
| 5878 | int type; |
| 5879 | |
| 5880 | for (type = 0; type < HIST_COUNT; ++type) |
| 5881 | { |
| 5882 | if (history[type] == NULL) |
| 5883 | return; |
| 5884 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 5885 | if (idx >= hislen) |
| 5886 | idx -= hislen; |
| 5887 | else if (idx < 0) |
| 5888 | idx = hislen - 1; |
| 5889 | if (viminfo_add_at_front) |
| 5890 | hisidx[type] = idx; |
| 5891 | else |
| 5892 | { |
| 5893 | if (hisidx[type] == -1) |
| 5894 | hisidx[type] = hislen - 1; |
| 5895 | do |
| 5896 | { |
| 5897 | if (history[type][idx].hisstr != NULL) |
| 5898 | break; |
| 5899 | if (++idx == hislen) |
| 5900 | idx = 0; |
| 5901 | } while (idx != hisidx[type]); |
| 5902 | if (idx != hisidx[type] && --idx < 0) |
| 5903 | idx = hislen - 1; |
| 5904 | } |
| 5905 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 5906 | { |
| 5907 | vim_free(history[type][idx].hisstr); |
| 5908 | history[type][idx].hisstr = viminfo_history[type][i]; |
| 5909 | if (--idx < 0) |
| 5910 | idx = hislen - 1; |
| 5911 | } |
| 5912 | idx += 1; |
| 5913 | idx %= hislen; |
| 5914 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 5915 | { |
| 5916 | history[type][idx++].hisnum = ++hisnum[type]; |
| 5917 | idx %= hislen; |
| 5918 | } |
| 5919 | vim_free(viminfo_history[type]); |
| 5920 | viminfo_history[type] = NULL; |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | void |
| 5925 | write_viminfo_history(fp) |
| 5926 | FILE *fp; |
| 5927 | { |
| 5928 | int i; |
| 5929 | int type; |
| 5930 | int num_saved; |
| 5931 | char_u *p; |
| 5932 | int c; |
| 5933 | |
| 5934 | init_history(); |
| 5935 | if (hislen == 0) |
| 5936 | return; |
| 5937 | for (type = 0; type < HIST_COUNT; ++type) |
| 5938 | { |
| 5939 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 5940 | if (num_saved == 0) |
| 5941 | continue; |
| 5942 | if (num_saved < 0) /* Use default */ |
| 5943 | num_saved = hislen; |
| 5944 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 5945 | type == HIST_CMD ? _("Command Line") : |
| 5946 | type == HIST_SEARCH ? _("Search String") : |
| 5947 | type == HIST_EXPR ? _("Expression") : |
| 5948 | _("Input Line")); |
| 5949 | if (num_saved > hislen) |
| 5950 | num_saved = hislen; |
| 5951 | i = hisidx[type]; |
| 5952 | if (i >= 0) |
| 5953 | while (num_saved--) |
| 5954 | { |
| 5955 | p = history[type][i].hisstr; |
| 5956 | if (p != NULL) |
| 5957 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5958 | fputc(hist_type2char(type, TRUE), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5959 | /* For the search history: put the separator in the second |
| 5960 | * column; use a space if there isn't one. */ |
| 5961 | if (type == HIST_SEARCH) |
| 5962 | { |
| 5963 | c = p[STRLEN(p) + 1]; |
| 5964 | putc(c == NUL ? ' ' : c, fp); |
| 5965 | } |
| 5966 | viminfo_writestring(fp, p); |
| 5967 | } |
| 5968 | if (--i < 0) |
| 5969 | i = hislen - 1; |
| 5970 | } |
| 5971 | } |
| 5972 | } |
| 5973 | #endif /* FEAT_VIMINFO */ |
| 5974 | |
| 5975 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 5976 | /* |
| 5977 | * Write a character at the current cursor+offset position. |
| 5978 | * It is directly written into the command buffer block. |
| 5979 | */ |
| 5980 | void |
| 5981 | cmd_pchar(c, offset) |
| 5982 | int c, offset; |
| 5983 | { |
| 5984 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 5985 | { |
| 5986 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 5987 | return; |
| 5988 | } |
| 5989 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 5990 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 5991 | } |
| 5992 | |
| 5993 | int |
| 5994 | cmd_gchar(offset) |
| 5995 | int offset; |
| 5996 | { |
| 5997 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 5998 | { |
| 5999 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6000 | return NUL; |
| 6001 | } |
| 6002 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6003 | } |
| 6004 | #endif |
| 6005 | |
| 6006 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6007 | /* |
| 6008 | * Open a window on the current command line and history. Allow editing in |
| 6009 | * the window. Returns when the window is closed. |
| 6010 | * Returns: |
| 6011 | * CR if the command is to be executed |
| 6012 | * Ctrl_C if it is to be abandoned |
| 6013 | * K_IGNORE if editing continues |
| 6014 | */ |
| 6015 | static int |
| 6016 | ex_window() |
| 6017 | { |
| 6018 | struct cmdline_info save_ccline; |
| 6019 | buf_T *old_curbuf = curbuf; |
| 6020 | win_T *old_curwin = curwin; |
| 6021 | buf_T *bp; |
| 6022 | win_T *wp; |
| 6023 | int i; |
| 6024 | linenr_T lnum; |
| 6025 | int histtype; |
| 6026 | garray_T winsizes; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6027 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6028 | char_u typestr[2]; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6029 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6030 | int save_restart_edit = restart_edit; |
| 6031 | int save_State = State; |
| 6032 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6033 | #ifdef FEAT_RIGHTLEFT |
| 6034 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6035 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6036 | |
| 6037 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6038 | if (cmdwin_type != 0 |
| 6039 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6040 | || cmdline_star > 0 |
| 6041 | # endif |
| 6042 | ) |
| 6043 | { |
| 6044 | beep_flush(); |
| 6045 | return K_IGNORE; |
| 6046 | } |
| 6047 | |
| 6048 | /* Save current window sizes. */ |
| 6049 | win_size_save(&winsizes); |
| 6050 | |
| 6051 | # ifdef FEAT_AUTOCMD |
| 6052 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6053 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6054 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6055 | /* don't use a new tab page */ |
| 6056 | cmdmod.tab = 0; |
| 6057 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6058 | /* Create a window for the command-line buffer. */ |
| 6059 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6060 | { |
| 6061 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6062 | # ifdef FEAT_AUTOCMD |
| 6063 | unblock_autocmds(); |
| 6064 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6065 | return K_IGNORE; |
| 6066 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6067 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6068 | |
| 6069 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6070 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6071 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6072 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
| 6073 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 6074 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6075 | #ifdef FEAT_FOLDING |
| 6076 | curwin->w_p_fen = FALSE; |
| 6077 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6078 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6079 | curwin->w_p_rl = cmdmsg_rl; |
| 6080 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6081 | # endif |
| 6082 | # ifdef FEAT_SCROLLBIND |
| 6083 | curwin->w_p_scb = FALSE; |
| 6084 | # endif |
| 6085 | |
| 6086 | # ifdef FEAT_AUTOCMD |
| 6087 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6088 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6089 | # endif |
| 6090 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6091 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6092 | need_wait_return = FALSE; |
| 6093 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6094 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6095 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6096 | { |
| 6097 | if (p_wc == TAB) |
| 6098 | { |
| 6099 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6100 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6101 | } |
| 6102 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6103 | } |
| 6104 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6105 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6106 | * sets 'textwidth' to 78). */ |
| 6107 | curbuf->b_p_tw = 0; |
| 6108 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6109 | /* Fill the buffer with the history. */ |
| 6110 | init_history(); |
| 6111 | if (hislen > 0) |
| 6112 | { |
| 6113 | i = hisidx[histtype]; |
| 6114 | if (i >= 0) |
| 6115 | { |
| 6116 | lnum = 0; |
| 6117 | do |
| 6118 | { |
| 6119 | if (++i == hislen) |
| 6120 | i = 0; |
| 6121 | if (history[histtype][i].hisstr != NULL) |
| 6122 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6123 | (colnr_T)0, FALSE); |
| 6124 | } |
| 6125 | while (i != hisidx[histtype]); |
| 6126 | } |
| 6127 | } |
| 6128 | |
| 6129 | /* Replace the empty last line with the current command-line and put the |
| 6130 | * cursor there. */ |
| 6131 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 6132 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6133 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6134 | changed_line_abv_curs(); |
| 6135 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 6136 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6137 | |
| 6138 | /* Save the command line info, can be used recursively. */ |
| 6139 | save_ccline = ccline; |
| 6140 | ccline.cmdbuff = NULL; |
| 6141 | ccline.cmdprompt = NULL; |
| 6142 | |
| 6143 | /* No Ex mode here! */ |
| 6144 | exmode_active = 0; |
| 6145 | |
| 6146 | State = NORMAL; |
| 6147 | # ifdef FEAT_MOUSE |
| 6148 | setmouse(); |
| 6149 | # endif |
| 6150 | |
| 6151 | # ifdef FEAT_AUTOCMD |
| 6152 | /* Trigger CmdwinEnter autocommands. */ |
| 6153 | typestr[0] = cmdwin_type; |
| 6154 | typestr[1] = NUL; |
| 6155 | apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 6156 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 6157 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6158 | # endif |
| 6159 | |
| 6160 | i = RedrawingDisabled; |
| 6161 | RedrawingDisabled = 0; |
| 6162 | |
| 6163 | /* |
| 6164 | * Call the main loop until <CR> or CTRL-C is typed. |
| 6165 | */ |
| 6166 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 6167 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6168 | |
| 6169 | RedrawingDisabled = i; |
| 6170 | |
| 6171 | # ifdef FEAT_AUTOCMD |
| 6172 | /* Trigger CmdwinLeave autocommands. */ |
| 6173 | apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf); |
| 6174 | # endif |
| 6175 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 6176 | /* Restore the command line info. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6177 | ccline = save_ccline; |
| 6178 | cmdwin_type = 0; |
| 6179 | |
| 6180 | exmode_active = save_exmode; |
| 6181 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 6182 | /* 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] | 6183 | * this happens! */ |
| 6184 | if (!win_valid(old_curwin) || !buf_valid(old_curbuf)) |
| 6185 | { |
| 6186 | cmdwin_result = Ctrl_C; |
| 6187 | EMSG(_("E199: Active window or buffer deleted")); |
| 6188 | } |
| 6189 | else |
| 6190 | { |
| 6191 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 6192 | /* autocmds may abort script processing */ |
| 6193 | if (aborting() && cmdwin_result != K_IGNORE) |
| 6194 | cmdwin_result = Ctrl_C; |
| 6195 | # endif |
| 6196 | /* Set the new command line from the cmdline buffer. */ |
| 6197 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6198 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6199 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6200 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 6201 | |
| 6202 | if (histtype == HIST_CMD) |
| 6203 | { |
| 6204 | /* Execute the command directly. */ |
| 6205 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 6206 | cmdwin_result = CAR; |
| 6207 | } |
| 6208 | else |
| 6209 | { |
| 6210 | /* First need to cancel what we were doing. */ |
| 6211 | ccline.cmdbuff = NULL; |
| 6212 | stuffcharReadbuff(':'); |
| 6213 | stuffReadbuff((char_u *)p); |
| 6214 | stuffcharReadbuff(CAR); |
| 6215 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6216 | } |
| 6217 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 6218 | { |
| 6219 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 6220 | cmdwin_result = CAR; |
| 6221 | } |
| 6222 | else |
| 6223 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 6224 | if (ccline.cmdbuff == NULL) |
| 6225 | cmdwin_result = Ctrl_C; |
| 6226 | else |
| 6227 | { |
| 6228 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 6229 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 6230 | ccline.cmdpos = curwin->w_cursor.col; |
| 6231 | if (ccline.cmdpos > ccline.cmdlen) |
| 6232 | ccline.cmdpos = ccline.cmdlen; |
| 6233 | if (cmdwin_result == K_IGNORE) |
| 6234 | { |
| 6235 | set_cmdspos_cursor(); |
| 6236 | redrawcmd(); |
| 6237 | } |
| 6238 | } |
| 6239 | |
| 6240 | # ifdef FEAT_AUTOCMD |
| 6241 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6242 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6243 | # endif |
| 6244 | wp = curwin; |
| 6245 | bp = curbuf; |
| 6246 | win_goto(old_curwin); |
| 6247 | win_close(wp, TRUE); |
| 6248 | close_buffer(NULL, bp, DOBUF_WIPE); |
| 6249 | |
| 6250 | /* Restore window sizes. */ |
| 6251 | win_size_restore(&winsizes); |
| 6252 | |
| 6253 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6254 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6255 | # endif |
| 6256 | } |
| 6257 | |
| 6258 | ga_clear(&winsizes); |
| 6259 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6260 | # ifdef FEAT_RIGHTLEFT |
| 6261 | cmdmsg_rl = save_cmdmsg_rl; |
| 6262 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6263 | |
| 6264 | State = save_State; |
| 6265 | # ifdef FEAT_MOUSE |
| 6266 | setmouse(); |
| 6267 | # endif |
| 6268 | |
| 6269 | return cmdwin_result; |
| 6270 | } |
| 6271 | #endif /* FEAT_CMDWIN */ |
| 6272 | |
| 6273 | /* |
| 6274 | * Used for commands that either take a simple command string argument, or: |
| 6275 | * cmd << endmarker |
| 6276 | * {script} |
| 6277 | * endmarker |
| 6278 | * Returns a pointer to allocated memory with {script} or NULL. |
| 6279 | */ |
| 6280 | char_u * |
| 6281 | script_get(eap, cmd) |
| 6282 | exarg_T *eap; |
| 6283 | char_u *cmd; |
| 6284 | { |
| 6285 | char_u *theline; |
| 6286 | char *end_pattern = NULL; |
| 6287 | char dot[] = "."; |
| 6288 | garray_T ga; |
| 6289 | |
| 6290 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 6291 | return NULL; |
| 6292 | |
| 6293 | ga_init2(&ga, 1, 0x400); |
| 6294 | |
| 6295 | if (cmd[2] != NUL) |
| 6296 | end_pattern = (char *)skipwhite(cmd + 2); |
| 6297 | else |
| 6298 | end_pattern = dot; |
| 6299 | |
| 6300 | for (;;) |
| 6301 | { |
| 6302 | theline = eap->getline( |
| 6303 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 6304 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6305 | #endif |
| 6306 | NUL, eap->cookie, 0); |
| 6307 | |
| 6308 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 6309 | { |
| 6310 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 6312 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6313 | |
| 6314 | ga_concat(&ga, theline); |
| 6315 | ga_append(&ga, '\n'); |
| 6316 | vim_free(theline); |
| 6317 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 6318 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | |
| 6320 | return (char_u *)ga.ga_data; |
| 6321 | } |