Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_getln.c: Functions for entering and editing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * Variables shared between getcmdline(), redrawcmdline() and others. |
| 18 | * These need to be saved when using CTRL-R |, that's why they are in a |
| 19 | * structure. |
| 20 | */ |
| 21 | struct cmdline_info |
| 22 | { |
| 23 | char_u *cmdbuff; /* pointer to command line buffer */ |
| 24 | int cmdbufflen; /* length of cmdbuff */ |
| 25 | int cmdlen; /* number of chars in command line */ |
| 26 | int cmdpos; /* current cursor position */ |
| 27 | int cmdspos; /* cursor column on screen */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 28 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | int cmdindent; /* number of spaces before cmdline */ |
| 30 | char_u *cmdprompt; /* message in front of cmdline */ |
| 31 | int cmdattr; /* attributes for prompt */ |
| 32 | int overstrike; /* Typing mode on the command line. Shared by |
| 33 | getcmdline() and put_on_cmdline(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 34 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 35 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 36 | int xp_context; /* type of expansion */ |
| 37 | # ifdef FEAT_EVAL |
| 38 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 39 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 43 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 44 | * used by other functions. When invoking getcmdline() recursively it needs |
| 45 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 46 | * TODO: make it local to getcmdline() and pass it around. */ |
| 47 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 50 | |
| 51 | #ifdef FEAT_EVAL |
| 52 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 53 | #endif |
| 54 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 55 | static int extra_char = NUL; /* extra character to display when redrawing |
| 56 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 57 | static int extra_char_shift; |
| 58 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | #ifdef FEAT_CMDHIST |
| 60 | typedef struct hist_entry |
| 61 | { |
| 62 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 63 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 65 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | } histentry_T; |
| 67 | |
| 68 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 69 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 70 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 71 | /* identifying (unique) number of newest history entry */ |
| 72 | static int hislen = 0; /* actual length of history tables */ |
| 73 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 74 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 76 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 78 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | # endif |
| 80 | #endif |
| 81 | |
| 82 | #ifdef FEAT_RIGHTLEFT |
| 83 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 84 | #endif |
| 85 | |
| 86 | #ifdef FEAT_FKMAP |
| 87 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 88 | #endif |
| 89 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 90 | static int cmdline_charsize(int idx); |
| 91 | static void set_cmdspos(void); |
| 92 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 94 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 96 | static void alloc_cmdbuff(int len); |
| 97 | static int realloc_cmdbuff(int len); |
| 98 | static void draw_cmdline(int start, int len); |
| 99 | static void save_cmdline(struct cmdline_info *ccp); |
| 100 | static void restore_cmdline(struct cmdline_info *ccp); |
| 101 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 102 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 103 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 106 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 108 | static void redrawcmdprompt(void); |
| 109 | static void cursorcmd(void); |
| 110 | static int ccheck_abbr(int); |
| 111 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 112 | static void escape_fname(char_u **pp); |
| 113 | static int showmatches(expand_T *xp, int wildmenu); |
| 114 | static void set_expand_context(expand_T *xp); |
| 115 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 116 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 119 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 120 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 121 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 122 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 123 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 125 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 126 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | # endif |
| 128 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 129 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 130 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 131 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | |
| 133 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 134 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 137 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 138 | static int |
| 139 | #ifdef __BORLANDC__ |
| 140 | _RTLENTRYF |
| 141 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 142 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 143 | #endif |
| 144 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 145 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 146 | static void |
| 147 | trigger_cmd_autocmd(int typechar, int evt) |
| 148 | { |
| 149 | char_u typestr[2]; |
| 150 | |
| 151 | typestr[0] = typechar; |
| 152 | typestr[1] = NUL; |
| 153 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 154 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 155 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 157 | * Abandon the command line. |
| 158 | */ |
| 159 | static void |
| 160 | abandon_cmdline(void) |
| 161 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 162 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 163 | if (msg_scrolled == 0) |
| 164 | compute_cmdrow(); |
| 165 | MSG(""); |
| 166 | redraw_cmdline = TRUE; |
| 167 | } |
| 168 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 169 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 170 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 171 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 172 | * as a trailing \|, which can happen while typing a pattern. |
| 173 | */ |
| 174 | static int |
| 175 | empty_pattern(char_u *p) |
| 176 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 177 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 178 | |
| 179 | /* remove trailing \v and the like */ |
| 180 | while (n >= 2 && p[n - 2] == '\\' |
| 181 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 182 | n -= 2; |
| 183 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 184 | } |
| 185 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 186 | // Struct to store the viewstate during 'incsearch' highlighting. |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 187 | typedef struct { |
| 188 | colnr_T vs_curswant; |
| 189 | colnr_T vs_leftcol; |
| 190 | linenr_T vs_topline; |
| 191 | # ifdef FEAT_DIFF |
| 192 | int vs_topfill; |
| 193 | # endif |
| 194 | linenr_T vs_botline; |
| 195 | linenr_T vs_empty_rows; |
| 196 | } viewstate_T; |
| 197 | |
| 198 | static void |
| 199 | save_viewstate(viewstate_T *vs) |
| 200 | { |
| 201 | vs->vs_curswant = curwin->w_curswant; |
| 202 | vs->vs_leftcol = curwin->w_leftcol; |
| 203 | vs->vs_topline = curwin->w_topline; |
| 204 | # ifdef FEAT_DIFF |
| 205 | vs->vs_topfill = curwin->w_topfill; |
| 206 | # endif |
| 207 | vs->vs_botline = curwin->w_botline; |
| 208 | vs->vs_empty_rows = curwin->w_empty_rows; |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | restore_viewstate(viewstate_T *vs) |
| 213 | { |
| 214 | curwin->w_curswant = vs->vs_curswant; |
| 215 | curwin->w_leftcol = vs->vs_leftcol; |
| 216 | curwin->w_topline = vs->vs_topline; |
| 217 | # ifdef FEAT_DIFF |
| 218 | curwin->w_topfill = vs->vs_topfill; |
| 219 | # endif |
| 220 | curwin->w_botline = vs->vs_botline; |
| 221 | curwin->w_empty_rows = vs->vs_empty_rows; |
| 222 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 223 | |
| 224 | // Struct to store the state of 'incsearch' highlighting. |
| 225 | typedef struct { |
| 226 | pos_T search_start; // where 'incsearch' starts searching |
| 227 | pos_T save_cursor; |
| 228 | viewstate_T init_viewstate; |
| 229 | viewstate_T old_viewstate; |
| 230 | pos_T match_start; |
| 231 | pos_T match_end; |
| 232 | int did_incsearch; |
| 233 | int incsearch_postponed; |
| 234 | } incsearch_state_T; |
| 235 | |
| 236 | static void |
| 237 | init_incsearch_state(incsearch_state_T *is_state) |
| 238 | { |
| 239 | is_state->match_start = curwin->w_cursor; |
| 240 | is_state->did_incsearch = FALSE; |
| 241 | is_state->incsearch_postponed = FALSE; |
| 242 | CLEAR_POS(&is_state->match_end); |
| 243 | is_state->save_cursor = curwin->w_cursor; // may be restored later |
| 244 | is_state->search_start = curwin->w_cursor; |
| 245 | save_viewstate(&is_state->init_viewstate); |
| 246 | save_viewstate(&is_state->old_viewstate); |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * First move cursor to end of match, then to the start. This |
| 251 | * moves the whole match onto the screen when 'nowrap' is set. |
| 252 | */ |
| 253 | static void |
| 254 | set_search_match(pos_T *t) |
| 255 | { |
| 256 | t->lnum += search_match_lines; |
| 257 | t->col = search_match_endcol; |
| 258 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 259 | { |
| 260 | t->lnum = curbuf->b_ml.ml_line_count; |
| 261 | coladvance((colnr_T)MAXCOL); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Return TRUE when 'incsearch' highlighting is to be done. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 267 | * Sets search_first_line and search_last_line to the address range. |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 268 | */ |
| 269 | static int |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 270 | do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, |
| 271 | int *skiplen, int *patlen) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 272 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 273 | *skiplen = 0; |
| 274 | *patlen = ccline.cmdlen; |
| 275 | |
| 276 | if (p_is && !cmd_silent) |
| 277 | { |
| 278 | // by default search all lines |
| 279 | search_first_line = 0; |
| 280 | search_last_line = MAXLNUM; |
| 281 | |
| 282 | if (firstc == '/' || firstc == '?') |
| 283 | return TRUE; |
| 284 | if (firstc == ':') |
| 285 | { |
| 286 | char_u *cmd = skip_range(ccline.cmdbuff, NULL); |
| 287 | char_u *p; |
| 288 | int delim; |
| 289 | char_u *end; |
| 290 | |
| 291 | if (*cmd == 's' || *cmd == 'g' || *cmd == 'v') |
| 292 | { |
| 293 | // Skip over "substitute" to find the pattern separator. |
| 294 | for (p = cmd; ASCII_ISALPHA(*p); ++p) |
| 295 | ; |
| 296 | if (*p != NUL) |
| 297 | { |
| 298 | delim = *p++; |
| 299 | end = skip_regexp(p, delim, p_magic, NULL); |
| 300 | if (end > p) |
| 301 | { |
| 302 | char_u *dummy; |
| 303 | exarg_T ea; |
| 304 | pos_T save_cursor = curwin->w_cursor; |
| 305 | |
| 306 | // found a non-empty pattern |
| 307 | *skiplen = (int)(p - ccline.cmdbuff); |
| 308 | *patlen = (int)(end - p); |
| 309 | |
| 310 | // parse the address range |
| 311 | vim_memset(&ea, 0, sizeof(ea)); |
| 312 | ea.line1 = 1; |
| 313 | ea.line2 = 1; |
| 314 | ea.cmd = ccline.cmdbuff; |
| 315 | ea.addr_type = ADDR_LINES; |
| 316 | parse_cmd_address(&ea, &dummy); |
| 317 | curwin->w_cursor = is_state->search_start; |
| 318 | if (ea.addr_count > 0) |
| 319 | { |
| 320 | search_first_line = ea.line1; |
| 321 | search_last_line = ea.line2; |
| 322 | } |
| 323 | else if (*cmd == 's') |
| 324 | { |
| 325 | // :s defaults to the current line |
| 326 | search_first_line = curwin->w_cursor.lnum; |
| 327 | search_last_line = curwin->w_cursor.lnum; |
| 328 | } |
| 329 | |
| 330 | curwin->w_cursor = save_cursor; |
| 331 | return TRUE; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return FALSE; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Do 'incsearch' highlighting if desired. |
| 343 | */ |
| 344 | static void |
| 345 | may_do_incsearch_highlighting( |
| 346 | int firstc, |
| 347 | long count, |
| 348 | incsearch_state_T *is_state) |
| 349 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 350 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 351 | int i; |
| 352 | pos_T end_pos; |
| 353 | struct cmdline_info save_ccline; |
| 354 | #ifdef FEAT_RELTIME |
| 355 | proftime_T tm; |
| 356 | #endif |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 357 | int c; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 358 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 359 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 360 | return; |
| 361 | |
| 362 | // If there is a character waiting, search and redraw later. |
| 363 | if (char_avail()) |
| 364 | { |
| 365 | is_state->incsearch_postponed = TRUE; |
| 366 | return; |
| 367 | } |
| 368 | is_state->incsearch_postponed = FALSE; |
| 369 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 370 | if (search_first_line == 0) |
| 371 | // start at the original cursor position |
| 372 | curwin->w_cursor = is_state->search_start; |
| 373 | else |
| 374 | { |
| 375 | // start at the first line in the range |
| 376 | curwin->w_cursor.lnum = search_first_line; |
| 377 | curwin->w_cursor.col = 0; |
| 378 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 379 | save_last_search_pattern(); |
| 380 | |
| 381 | // If there is no command line, don't do anything. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 382 | if (patlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 383 | { |
| 384 | i = 0; |
| 385 | set_no_hlsearch(TRUE); // turn off previous highlight |
| 386 | redraw_all_later(SOME_VALID); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
| 391 | |
| 392 | cursor_off(); // so the user knows we're busy |
| 393 | out_flush(); |
| 394 | ++emsg_off; // so it doesn't beep if bad expr |
| 395 | #ifdef FEAT_RELTIME |
| 396 | // Set the time limit to half a second. |
| 397 | profile_setlimit(500L, &tm); |
| 398 | #endif |
| 399 | if (!p_hls) |
| 400 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 401 | c = ccline.cmdbuff[skiplen + patlen]; |
| 402 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 403 | i = do_search(NULL, firstc == ':' ? '/' : firstc, |
| 404 | ccline.cmdbuff + skiplen, count, search_flags, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 405 | #ifdef FEAT_RELTIME |
| 406 | &tm, NULL |
| 407 | #else |
| 408 | NULL, NULL |
| 409 | #endif |
| 410 | ); |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 411 | ccline.cmdbuff[skiplen + patlen] = c; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 412 | --emsg_off; |
| 413 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 414 | if (curwin->w_cursor.lnum < search_first_line |
| 415 | || curwin->w_cursor.lnum > search_last_line) |
| 416 | // match outside of address range |
| 417 | i = 0; |
| 418 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 419 | // if interrupted while searching, behave like it failed |
| 420 | if (got_int) |
| 421 | { |
| 422 | (void)vpeekc(); // remove <C-C> from input stream |
| 423 | got_int = FALSE; // don't abandon the command line |
| 424 | i = 0; |
| 425 | } |
| 426 | else if (char_avail()) |
| 427 | // cancelled searching because a char was typed |
| 428 | is_state->incsearch_postponed = TRUE; |
| 429 | } |
| 430 | if (i != 0) |
| 431 | highlight_match = TRUE; // highlight position |
| 432 | else |
| 433 | highlight_match = FALSE; // remove highlight |
| 434 | |
| 435 | // First restore the old curwin values, so the screen is positioned in the |
| 436 | // same way as the actual search command. |
| 437 | restore_viewstate(&is_state->old_viewstate); |
| 438 | changed_cline_bef_curs(); |
| 439 | update_topline(); |
| 440 | |
| 441 | if (i != 0) |
| 442 | { |
| 443 | pos_T save_pos = curwin->w_cursor; |
| 444 | |
| 445 | is_state->match_start = curwin->w_cursor; |
| 446 | set_search_match(&curwin->w_cursor); |
| 447 | validate_cursor(); |
| 448 | end_pos = curwin->w_cursor; |
| 449 | is_state->match_end = end_pos; |
| 450 | curwin->w_cursor = save_pos; |
| 451 | } |
| 452 | else |
| 453 | end_pos = curwin->w_cursor; // shutup gcc 4 |
| 454 | |
| 455 | // Disable 'hlsearch' highlighting if the pattern matches everything. |
| 456 | // Avoids a flash when typing "foo\|". |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 457 | c = ccline.cmdbuff[skiplen + patlen]; |
| 458 | ccline.cmdbuff[skiplen + patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 459 | if (empty_pattern(ccline.cmdbuff)) |
| 460 | set_no_hlsearch(TRUE); |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 461 | ccline.cmdbuff[skiplen + patlen] = c; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 462 | |
| 463 | validate_cursor(); |
| 464 | // May redraw the status line to show the cursor position. |
| 465 | if (p_ru && curwin->w_status_height > 0) |
| 466 | curwin->w_redr_status = TRUE; |
| 467 | |
| 468 | save_cmdline(&save_ccline); |
| 469 | update_screen(SOME_VALID); |
| 470 | restore_cmdline(&save_ccline); |
| 471 | restore_last_search_pattern(); |
| 472 | |
| 473 | // Leave it at the end to make CTRL-R CTRL-W work. |
| 474 | if (i != 0) |
| 475 | curwin->w_cursor = end_pos; |
| 476 | |
| 477 | msg_starthere(); |
| 478 | redrawcmdline(); |
| 479 | is_state->did_incsearch = TRUE; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next |
| 484 | * or previous match. |
| 485 | * Returns FAIL when jumping to cmdline_not_changed; |
| 486 | */ |
| 487 | static int |
| 488 | may_adjust_incsearch_highlighting( |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 489 | int firstc, |
| 490 | long count, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 491 | incsearch_state_T *is_state, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 492 | int c) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 493 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 494 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 495 | pos_T t; |
| 496 | char_u *pat; |
| 497 | int search_flags = SEARCH_NOOF; |
| 498 | int i; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 499 | int save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 500 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 501 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 502 | return OK; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 503 | if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 504 | return FAIL; |
| 505 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 506 | if (firstc == ccline.cmdbuff[skiplen]) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 507 | pat = last_search_pattern(); |
| 508 | else |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 509 | pat = ccline.cmdbuff + skiplen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 510 | |
| 511 | save_last_search_pattern(); |
| 512 | cursor_off(); |
| 513 | out_flush(); |
| 514 | if (c == Ctrl_G) |
| 515 | { |
| 516 | t = is_state->match_end; |
| 517 | if (LT_POS(is_state->match_start, is_state->match_end)) |
| 518 | // Start searching at the end of the match not at the beginning of |
| 519 | // the next column. |
| 520 | (void)decl(&t); |
| 521 | search_flags += SEARCH_COL; |
| 522 | } |
| 523 | else |
| 524 | t = is_state->match_start; |
| 525 | if (!p_hls) |
| 526 | search_flags += SEARCH_KEEP; |
| 527 | ++emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 528 | save = pat[patlen]; |
| 529 | pat[patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 530 | i = searchit(curwin, curbuf, &t, |
| 531 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 532 | pat, count, search_flags, |
| 533 | RE_SEARCH, 0, NULL, NULL); |
| 534 | --emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 535 | pat[patlen] = save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 536 | if (i) |
| 537 | { |
| 538 | is_state->search_start = is_state->match_start; |
| 539 | is_state->match_end = t; |
| 540 | is_state->match_start = t; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 541 | if (c == Ctrl_T && firstc != '?') |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 542 | { |
| 543 | // Move just before the current match, so that when nv_search |
| 544 | // finishes the cursor will be put back on the match. |
| 545 | is_state->search_start = t; |
| 546 | (void)decl(&is_state->search_start); |
| 547 | } |
| 548 | else if (c == Ctrl_G && firstc == '?') |
| 549 | { |
| 550 | // Move just after the current match, so that when nv_search |
| 551 | // finishes the cursor will be put back on the match. |
| 552 | is_state->search_start = t; |
| 553 | (void)incl(&is_state->search_start); |
| 554 | } |
| 555 | if (LT_POS(t, is_state->search_start) && c == Ctrl_G) |
| 556 | { |
| 557 | // wrap around |
| 558 | is_state->search_start = t; |
| 559 | if (firstc == '?') |
| 560 | (void)incl(&is_state->search_start); |
| 561 | else |
| 562 | (void)decl(&is_state->search_start); |
| 563 | } |
| 564 | |
| 565 | set_search_match(&is_state->match_end); |
| 566 | curwin->w_cursor = is_state->match_start; |
| 567 | changed_cline_bef_curs(); |
| 568 | update_topline(); |
| 569 | validate_cursor(); |
| 570 | highlight_match = TRUE; |
| 571 | save_viewstate(&is_state->old_viewstate); |
| 572 | update_screen(NOT_VALID); |
| 573 | redrawcmdline(); |
| 574 | } |
| 575 | else |
| 576 | vim_beep(BO_ERROR); |
| 577 | restore_last_search_pattern(); |
| 578 | return FAIL; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * When CTRL-L typed: add character from the match to the pattern. |
| 583 | * May set "*c" to the added character. |
| 584 | * Return OK when jumping to cmdline_not_changed. |
| 585 | */ |
| 586 | static int |
| 587 | may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state) |
| 588 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 589 | int skiplen, patlen; |
| 590 | |
| 591 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 592 | return FAIL; |
| 593 | |
| 594 | // Add a character from under the cursor for 'incsearch'. |
| 595 | if (is_state->did_incsearch) |
| 596 | { |
| 597 | curwin->w_cursor = is_state->match_end; |
| 598 | if (!EQUAL_POS(curwin->w_cursor, is_state->search_start)) |
| 599 | { |
| 600 | *c = gchar_cursor(); |
| 601 | |
| 602 | // If 'ignorecase' and 'smartcase' are set and the |
| 603 | // command line has no uppercase characters, convert |
| 604 | // the character to lowercase. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 605 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 606 | *c = MB_TOLOWER(*c); |
| 607 | if (*c != NUL) |
| 608 | { |
| 609 | if (*c == firstc || vim_strchr((char_u *)( |
| 610 | p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) |
| 611 | { |
| 612 | // put a backslash before special characters |
| 613 | stuffcharReadbuff(*c); |
| 614 | *c = '\\'; |
| 615 | } |
| 616 | return FAIL; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | return OK; |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state) |
| 625 | { |
| 626 | if (is_state->did_incsearch) |
| 627 | { |
| 628 | if (gotesc) |
| 629 | curwin->w_cursor = is_state->save_cursor; |
| 630 | else |
| 631 | { |
| 632 | if (!EQUAL_POS(is_state->save_cursor, is_state->search_start)) |
| 633 | { |
| 634 | // put the '" mark at the original position |
| 635 | curwin->w_cursor = is_state->save_cursor; |
| 636 | setpcmark(); |
| 637 | } |
| 638 | curwin->w_cursor = is_state->search_start; |
| 639 | } |
| 640 | restore_viewstate(&is_state->old_viewstate); |
| 641 | highlight_match = FALSE; |
| 642 | validate_cursor(); /* needed for TAB */ |
| 643 | redraw_all_later(SOME_VALID); |
| 644 | } |
| 645 | } |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 646 | #endif |
| 647 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 648 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 649 | * getcmdline() - accept a command line starting with firstc. |
| 650 | * |
| 651 | * firstc == ':' get ":" command line. |
| 652 | * firstc == '/' or '?' get search pattern |
| 653 | * firstc == '=' get expression |
| 654 | * firstc == '@' get text for input() function |
| 655 | * firstc == '>' get text for debug mode |
| 656 | * firstc == NUL get text for :insert command |
| 657 | * firstc == -1 like NUL, and break on CTRL-C |
| 658 | * |
| 659 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 660 | * command line. |
| 661 | * |
| 662 | * Careful: getcmdline() can be called recursively! |
| 663 | * |
| 664 | * Return pointer to allocated string if there is a commandline, NULL |
| 665 | * otherwise. |
| 666 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 667 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 668 | getcmdline( |
| 669 | int firstc, |
| 670 | long count UNUSED, /* only used for incremental search */ |
| 671 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 672 | { |
| 673 | int c; |
| 674 | int i; |
| 675 | int j; |
| 676 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 677 | int do_abbr; /* when TRUE check for abbr. */ |
| 678 | #ifdef FEAT_CMDHIST |
| 679 | char_u *lookfor = NULL; /* string to match */ |
| 680 | int hiscnt; /* current history line in use */ |
| 681 | int histype; /* history type to be used */ |
| 682 | #endif |
| 683 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 684 | incsearch_state_T is_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | #endif |
| 686 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 687 | int wim_index = 0; /* index in wim_flags[] */ |
| 688 | int res; |
| 689 | int save_msg_scroll = msg_scroll; |
| 690 | int save_State = State; /* remember State when called */ |
| 691 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 692 | #ifdef FEAT_MOUSE |
| 693 | /* mouse drag and release events are ignored, unless they are |
| 694 | * preceded with a mouse down event */ |
| 695 | int ignore_drag_release = TRUE; |
| 696 | #endif |
| 697 | #ifdef FEAT_EVAL |
| 698 | int break_ctrl_c = FALSE; |
| 699 | #endif |
| 700 | expand_T xpc; |
| 701 | long *b_im_ptr = NULL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 702 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 703 | /* Everything that may work recursively should save and restore the |
| 704 | * current command line in save_ccline. That includes update_screen(), a |
| 705 | * custom status line may invoke ":normal". */ |
| 706 | struct cmdline_info save_ccline; |
| 707 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 708 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | #ifdef FEAT_EVAL |
| 711 | if (firstc == -1) |
| 712 | { |
| 713 | firstc = NUL; |
| 714 | break_ctrl_c = TRUE; |
| 715 | } |
| 716 | #endif |
| 717 | #ifdef FEAT_RIGHTLEFT |
| 718 | /* start without Hebrew mapping for a command line */ |
| 719 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 720 | cmd_hkmap = 0; |
| 721 | #endif |
| 722 | |
| 723 | ccline.overstrike = FALSE; /* always start in insert mode */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 724 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 726 | init_incsearch_state(&is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 727 | #endif |
| 728 | |
| 729 | /* |
| 730 | * set some variables for redrawcmd() |
| 731 | */ |
| 732 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 733 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 734 | |
| 735 | /* alloc initial ccline.cmdbuff */ |
| 736 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | if (ccline.cmdbuff == NULL) |
| 738 | return NULL; /* out of memory */ |
| 739 | ccline.cmdlen = ccline.cmdpos = 0; |
| 740 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 741 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 743 | /* autoindent for :insert and :append */ |
| 744 | if (firstc <= 0) |
| 745 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 746 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 747 | ccline.cmdbuff[indent] = NUL; |
| 748 | ccline.cmdpos = indent; |
| 749 | ccline.cmdspos = indent; |
| 750 | ccline.cmdlen = indent; |
| 751 | } |
| 752 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 754 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 755 | |
| 756 | #ifdef FEAT_RIGHTLEFT |
| 757 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 758 | && (firstc == '/' || firstc == '?')) |
| 759 | cmdmsg_rl = TRUE; |
| 760 | else |
| 761 | cmdmsg_rl = FALSE; |
| 762 | #endif |
| 763 | |
| 764 | redir_off = TRUE; /* don't redirect the typed command */ |
| 765 | if (!cmd_silent) |
| 766 | { |
| 767 | i = msg_scrolled; |
| 768 | msg_scrolled = 0; /* avoid wait_return message */ |
| 769 | gotocmdline(TRUE); |
| 770 | msg_scrolled += i; |
| 771 | redrawcmdprompt(); /* draw prompt or indent */ |
| 772 | set_cmdspos(); |
| 773 | } |
| 774 | xpc.xp_context = EXPAND_NOTHING; |
| 775 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 776 | #ifndef BACKSLASH_IN_FILENAME |
| 777 | xpc.xp_shell = FALSE; |
| 778 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 780 | #if defined(FEAT_EVAL) |
| 781 | if (ccline.input_fn) |
| 782 | { |
| 783 | xpc.xp_context = ccline.xp_context; |
| 784 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 785 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 786 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 787 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 788 | } |
| 789 | #endif |
| 790 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 791 | /* |
| 792 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 793 | * doing ":@0" when register 0 doesn't contain a CR. |
| 794 | */ |
| 795 | msg_scroll = FALSE; |
| 796 | |
| 797 | State = CMDLINE; |
| 798 | |
| 799 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 800 | { |
| 801 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 802 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 803 | b_im_ptr = &curbuf->b_p_iminsert; |
| 804 | else |
| 805 | b_im_ptr = &curbuf->b_p_imsearch; |
| 806 | if (*b_im_ptr == B_IMODE_LMAP) |
| 807 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 808 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 809 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 810 | #endif |
| 811 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 812 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 813 | else if (p_imcmdline) |
| 814 | im_set_active(TRUE); |
| 815 | #endif |
| 816 | |
| 817 | #ifdef FEAT_MOUSE |
| 818 | setmouse(); |
| 819 | #endif |
| 820 | #ifdef CURSOR_SHAPE |
| 821 | ui_cursor_shape(); /* may show different cursor shape */ |
| 822 | #endif |
| 823 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 824 | /* When inside an autocommand for writing "exiting" may be set and |
| 825 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 826 | settmode(TMODE_RAW); |
| 827 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 828 | /* Trigger CmdlineEnter autocommands. */ |
| 829 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 830 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 831 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | #ifdef FEAT_CMDHIST |
| 833 | init_history(); |
| 834 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 835 | histype = hist_char2type(firstc); |
| 836 | #endif |
| 837 | |
| 838 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 839 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | #endif |
| 841 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 842 | /* If something above caused an error, reset the flags, we do want to type |
| 843 | * and execute commands. Display may be messed up a bit. */ |
| 844 | if (did_emsg) |
| 845 | redrawcmd(); |
| 846 | did_emsg = FALSE; |
| 847 | got_int = FALSE; |
| 848 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 849 | /* |
| 850 | * Collect the command string, handling editing keys. |
| 851 | */ |
| 852 | for (;;) |
| 853 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 854 | redir_off = TRUE; /* Don't redirect the typed command. |
| 855 | Repeated, because a ":redir" inside |
| 856 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | #ifdef USE_ON_FLY_SCROLL |
| 858 | dont_scroll = FALSE; /* allow scrolling here */ |
| 859 | #endif |
| 860 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 861 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 862 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 863 | that occurs while typing a command should |
| 864 | cause the command not to be executed. */ |
| 865 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 867 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 868 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 869 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 870 | do |
| 871 | { |
| 872 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 873 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 874 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | if (KeyTyped) |
| 876 | { |
| 877 | some_key_typed = TRUE; |
| 878 | #ifdef FEAT_RIGHTLEFT |
| 879 | if (cmd_hkmap) |
| 880 | c = hkmap(c); |
| 881 | # ifdef FEAT_FKMAP |
| 882 | if (cmd_fkmap) |
| 883 | c = cmdl_fkmap(c); |
| 884 | # endif |
| 885 | if (cmdmsg_rl && !KeyStuffed) |
| 886 | { |
| 887 | /* Invert horizontal movements and operations. Only when |
| 888 | * typed by the user directly, not when the result of a |
| 889 | * mapping. */ |
| 890 | switch (c) |
| 891 | { |
| 892 | case K_RIGHT: c = K_LEFT; break; |
| 893 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 894 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 895 | case K_LEFT: c = K_RIGHT; break; |
| 896 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 897 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 898 | } |
| 899 | } |
| 900 | #endif |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Ignore got_int when CTRL-C was typed here. |
| 905 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 906 | * ":g/pat/normal /pat" (without the <CR>). |
| 907 | * Don't ignore it for the input() function. |
| 908 | */ |
| 909 | if ((c == Ctrl_C |
| 910 | #ifdef UNIX |
| 911 | || c == intr_char |
| 912 | #endif |
| 913 | ) |
| 914 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 915 | && firstc != '@' |
| 916 | #endif |
| 917 | #ifdef FEAT_EVAL |
| 918 | && !break_ctrl_c |
| 919 | #endif |
| 920 | && !global_busy) |
| 921 | got_int = FALSE; |
| 922 | |
| 923 | #ifdef FEAT_CMDHIST |
| 924 | /* free old command line when finished moving around in the history |
| 925 | * list */ |
| 926 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 927 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 928 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 930 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 931 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 933 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 934 | #endif |
| 935 | |
| 936 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 937 | * When there are matching completions to select <S-Tab> works like |
| 938 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 940 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | c = Ctrl_P; |
| 942 | |
| 943 | #ifdef FEAT_WILDMENU |
| 944 | /* Special translations for 'wildmenu' */ |
| 945 | if (did_wild_list && p_wmnu) |
| 946 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 947 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 949 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | c = Ctrl_N; |
| 951 | } |
| 952 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 953 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 954 | && ccline.cmdpos > 1 |
| 955 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 956 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 957 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 958 | c = K_DOWN; |
| 959 | #endif |
| 960 | |
| 961 | /* free expanded names when finished walking through matches */ |
| 962 | if (xpc.xp_numfiles != -1 |
| 963 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 964 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 965 | && c != Ctrl_L) |
| 966 | { |
| 967 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 968 | did_wild_list = FALSE; |
| 969 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 970 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | #endif |
| 972 | xpc.xp_context = EXPAND_NOTHING; |
| 973 | wim_index = 0; |
| 974 | #ifdef FEAT_WILDMENU |
| 975 | if (p_wmnu && wild_menu_showing != 0) |
| 976 | { |
| 977 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 978 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 979 | |
| 980 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 981 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 982 | |
| 983 | if (wild_menu_showing == WM_SCROLLED) |
| 984 | { |
| 985 | /* Entered command line, move it up */ |
| 986 | cmdline_row--; |
| 987 | redrawcmd(); |
| 988 | } |
| 989 | else if (save_p_ls != -1) |
| 990 | { |
| 991 | /* restore 'laststatus' and 'winminheight' */ |
| 992 | p_ls = save_p_ls; |
| 993 | p_wmh = save_p_wmh; |
| 994 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 995 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 997 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 998 | redrawcmd(); |
| 999 | save_p_ls = -1; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1003 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | redraw_statuslines(); |
| 1005 | } |
| 1006 | KeyTyped = skt; |
| 1007 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1008 | if (ccline.input_fn) |
| 1009 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | } |
| 1011 | #endif |
| 1012 | } |
| 1013 | |
| 1014 | #ifdef FEAT_WILDMENU |
| 1015 | /* Special translations for 'wildmenu' */ |
| 1016 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 1017 | { |
| 1018 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1019 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 1020 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1022 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1023 | { |
| 1024 | /* Hitting <Up>: Remove one submenu name in front of the |
| 1025 | * cursor */ |
| 1026 | int found = FALSE; |
| 1027 | |
| 1028 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1029 | i = 0; |
| 1030 | while (--j > 0) |
| 1031 | { |
| 1032 | /* check for start of menu name */ |
| 1033 | if (ccline.cmdbuff[j] == ' ' |
| 1034 | && ccline.cmdbuff[j - 1] != '\\') |
| 1035 | { |
| 1036 | i = j + 1; |
| 1037 | break; |
| 1038 | } |
| 1039 | /* check for start of submenu name */ |
| 1040 | if (ccline.cmdbuff[j] == '.' |
| 1041 | && ccline.cmdbuff[j - 1] != '\\') |
| 1042 | { |
| 1043 | if (found) |
| 1044 | { |
| 1045 | i = j + 1; |
| 1046 | break; |
| 1047 | } |
| 1048 | else |
| 1049 | found = TRUE; |
| 1050 | } |
| 1051 | } |
| 1052 | if (i > 0) |
| 1053 | cmdline_del(i); |
| 1054 | c = p_wc; |
| 1055 | xpc.xp_context = EXPAND_NOTHING; |
| 1056 | } |
| 1057 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1058 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1059 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1060 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | { |
| 1062 | char_u upseg[5]; |
| 1063 | |
| 1064 | upseg[0] = PATHSEP; |
| 1065 | upseg[1] = '.'; |
| 1066 | upseg[2] = '.'; |
| 1067 | upseg[3] = PATHSEP; |
| 1068 | upseg[4] = NUL; |
| 1069 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1070 | if (c == K_DOWN |
| 1071 | && ccline.cmdpos > 0 |
| 1072 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 1073 | && (ccline.cmdpos < 3 |
| 1074 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1075 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 1076 | { |
| 1077 | /* go down a directory */ |
| 1078 | c = p_wc; |
| 1079 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1080 | 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] | 1081 | { |
| 1082 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 1083 | int found = FALSE; |
| 1084 | |
| 1085 | j = ccline.cmdpos; |
| 1086 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1087 | while (--j > i) |
| 1088 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1089 | #ifdef FEAT_MBYTE |
| 1090 | if (has_mbyte) |
| 1091 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1092 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 1094 | { |
| 1095 | found = TRUE; |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | if (found |
| 1100 | && ccline.cmdbuff[j - 1] == '.' |
| 1101 | && ccline.cmdbuff[j - 2] == '.' |
| 1102 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 1103 | { |
| 1104 | cmdline_del(j - 2); |
| 1105 | c = p_wc; |
| 1106 | } |
| 1107 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1108 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | { |
| 1110 | /* go up a directory */ |
| 1111 | int found = FALSE; |
| 1112 | |
| 1113 | j = ccline.cmdpos - 1; |
| 1114 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1115 | while (--j > i) |
| 1116 | { |
| 1117 | #ifdef FEAT_MBYTE |
| 1118 | if (has_mbyte) |
| 1119 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1120 | #endif |
| 1121 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 1122 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1123 | && vim_strchr((char_u *)" *?[{`$%#", |
| 1124 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | #endif |
| 1126 | ) |
| 1127 | { |
| 1128 | if (found) |
| 1129 | { |
| 1130 | i = j + 1; |
| 1131 | break; |
| 1132 | } |
| 1133 | else |
| 1134 | found = TRUE; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | if (!found) |
| 1139 | j = i; |
| 1140 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 1141 | j += 4; |
| 1142 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 1143 | && j == i) |
| 1144 | j += 3; |
| 1145 | else |
| 1146 | j = 0; |
| 1147 | if (j > 0) |
| 1148 | { |
| 1149 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 1150 | * machine-specific stuff here and in upseg init */ |
| 1151 | cmdline_del(j); |
| 1152 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 1153 | } |
| 1154 | else if (ccline.cmdpos > i) |
| 1155 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1156 | |
| 1157 | /* Now complete in the new directory. Set KeyTyped in case the |
| 1158 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1159 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1160 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1163 | |
| 1164 | #endif /* FEAT_WILDMENU */ |
| 1165 | |
| 1166 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 1167 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 1168 | if (c == Ctrl_BSL) |
| 1169 | { |
| 1170 | ++no_mapping; |
| 1171 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1172 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1173 | --no_mapping; |
| 1174 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 1175 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 1176 | * is in a mapping. */ |
| 1177 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 1178 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | { |
| 1180 | vungetc(c); |
| 1181 | c = Ctrl_BSL; |
| 1182 | } |
| 1183 | #ifdef FEAT_EVAL |
| 1184 | else if (c == 'e') |
| 1185 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 1186 | char_u *p = NULL; |
| 1187 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1188 | |
| 1189 | /* |
| 1190 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1191 | * Need to save and restore the current command line, to be |
| 1192 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1193 | */ |
| 1194 | if (ccline.cmdpos == ccline.cmdlen) |
| 1195 | new_cmdpos = 99999; /* keep it at the end */ |
| 1196 | else |
| 1197 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1198 | |
| 1199 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1200 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1201 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | if (c == '=') |
| 1203 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1204 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1205 | * to avoid nasty things like going to another buffer when |
| 1206 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1207 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1208 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1210 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1211 | restore_cmdline(&save_ccline); |
| 1212 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1213 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1214 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1215 | len = (int)STRLEN(p); |
| 1216 | if (realloc_cmdbuff(len + 1) == OK) |
| 1217 | { |
| 1218 | ccline.cmdlen = len; |
| 1219 | STRCPY(ccline.cmdbuff, p); |
| 1220 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1221 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1222 | /* Restore the cursor or use the position set with |
| 1223 | * set_cmdline_pos(). */ |
| 1224 | if (new_cmdpos > ccline.cmdlen) |
| 1225 | ccline.cmdpos = ccline.cmdlen; |
| 1226 | else |
| 1227 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1228 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1229 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1230 | redrawcmd(); |
| 1231 | goto cmdline_changed; |
| 1232 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 1236 | got_int = FALSE; /* don't abandon the command line */ |
| 1237 | did_emsg = FALSE; |
| 1238 | emsg_on_display = FALSE; |
| 1239 | redrawcmd(); |
| 1240 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1241 | } |
| 1242 | #endif |
| 1243 | else |
| 1244 | { |
| 1245 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 1246 | restart_edit = 'a'; |
| 1247 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 1248 | in history */ |
| 1249 | goto returncmd; /* back to Normal mode */ |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | #ifdef FEAT_CMDWIN |
| 1254 | if (c == cedit_key || c == K_CMDWIN) |
| 1255 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1256 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 1257 | { |
| 1258 | /* |
| 1259 | * Open a window to edit the command line (and history). |
| 1260 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1261 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1262 | some_key_typed = TRUE; |
| 1263 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | } |
| 1265 | # ifdef FEAT_DIGRAPHS |
| 1266 | else |
| 1267 | # endif |
| 1268 | #endif |
| 1269 | #ifdef FEAT_DIGRAPHS |
| 1270 | c = do_digraph(c); |
| 1271 | #endif |
| 1272 | |
| 1273 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 1274 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 1275 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1276 | /* In Ex mode a backslash escapes a newline. */ |
| 1277 | if (exmode_active |
| 1278 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1279 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1280 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1281 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1282 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1283 | if (c == K_KENTER) |
| 1284 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1286 | else |
| 1287 | { |
| 1288 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 1289 | truncate the cmdline now. */ |
| 1290 | if (ccheck_abbr(c + ABBR_OFF)) |
| 1291 | goto cmdline_changed; |
| 1292 | if (!cmd_silent) |
| 1293 | { |
| 1294 | windgoto(msg_row, 0); |
| 1295 | out_flush(); |
| 1296 | } |
| 1297 | break; |
| 1298 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Completion for 'wildchar' or 'wildcharm' key. |
| 1303 | * - hitting <ESC> twice means: abandon command line. |
| 1304 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 1305 | * typed, not when it comes from a macro |
| 1306 | */ |
| 1307 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 1308 | { |
| 1309 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 1310 | { |
| 1311 | /* if 'wildmode' contains "list" may still need to list */ |
| 1312 | if (xpc.xp_numfiles > 1 |
| 1313 | && !did_wild_list |
| 1314 | && (wim_flags[wim_index] & WIM_LIST)) |
| 1315 | { |
| 1316 | (void)showmatches(&xpc, FALSE); |
| 1317 | redrawcmd(); |
| 1318 | did_wild_list = TRUE; |
| 1319 | } |
| 1320 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1321 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1322 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1323 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1324 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1325 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1326 | else |
| 1327 | res = OK; /* don't insert 'wildchar' now */ |
| 1328 | } |
| 1329 | else /* typed p_wc first time */ |
| 1330 | { |
| 1331 | wim_index = 0; |
| 1332 | j = ccline.cmdpos; |
| 1333 | /* if 'wildmode' first contains "longest", get longest |
| 1334 | * common part */ |
| 1335 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1336 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1337 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1339 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 1340 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | |
| 1342 | /* if interrupted while completing, behave like it failed */ |
| 1343 | if (got_int) |
| 1344 | { |
| 1345 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1346 | got_int = FALSE; /* don't abandon the command line */ |
| 1347 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1348 | #ifdef FEAT_WILDMENU |
| 1349 | xpc.xp_context = EXPAND_NOTHING; |
| 1350 | #endif |
| 1351 | goto cmdline_changed; |
| 1352 | } |
| 1353 | |
| 1354 | /* when more than one match, and 'wildmode' first contains |
| 1355 | * "list", or no change and 'wildmode' contains "longest,list", |
| 1356 | * list all matches */ |
| 1357 | if (res == OK && xpc.xp_numfiles > 1) |
| 1358 | { |
| 1359 | /* a "longest" that didn't do anything is skipped (but not |
| 1360 | * "list:longest") */ |
| 1361 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 1362 | wim_index = 1; |
| 1363 | if ((wim_flags[wim_index] & WIM_LIST) |
| 1364 | #ifdef FEAT_WILDMENU |
| 1365 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 1366 | #endif |
| 1367 | ) |
| 1368 | { |
| 1369 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 1370 | { |
| 1371 | #ifdef FEAT_WILDMENU |
| 1372 | int p_wmnu_save = p_wmnu; |
| 1373 | p_wmnu = 0; |
| 1374 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1375 | /* remove match */ |
| 1376 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | #ifdef FEAT_WILDMENU |
| 1378 | p_wmnu = p_wmnu_save; |
| 1379 | #endif |
| 1380 | } |
| 1381 | #ifdef FEAT_WILDMENU |
| 1382 | (void)showmatches(&xpc, p_wmnu |
| 1383 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 1384 | #else |
| 1385 | (void)showmatches(&xpc, FALSE); |
| 1386 | #endif |
| 1387 | redrawcmd(); |
| 1388 | did_wild_list = TRUE; |
| 1389 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1390 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1391 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1392 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1393 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1394 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1395 | } |
| 1396 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1397 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1398 | } |
| 1399 | #ifdef FEAT_WILDMENU |
| 1400 | else if (xpc.xp_numfiles == -1) |
| 1401 | xpc.xp_context = EXPAND_NOTHING; |
| 1402 | #endif |
| 1403 | } |
| 1404 | if (wim_index < 3) |
| 1405 | ++wim_index; |
| 1406 | if (c == ESC) |
| 1407 | gotesc = TRUE; |
| 1408 | if (res == OK) |
| 1409 | goto cmdline_changed; |
| 1410 | } |
| 1411 | |
| 1412 | gotesc = FALSE; |
| 1413 | |
| 1414 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 1415 | if (c == K_S_TAB && KeyTyped) |
| 1416 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1417 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 1418 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 1419 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1420 | goto cmdline_changed; |
| 1421 | } |
| 1422 | |
| 1423 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 1424 | c = NL; |
| 1425 | |
| 1426 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1427 | |
| 1428 | /* |
| 1429 | * Big switch for a typed command line character. |
| 1430 | */ |
| 1431 | switch (c) |
| 1432 | { |
| 1433 | case K_BS: |
| 1434 | case Ctrl_H: |
| 1435 | case K_DEL: |
| 1436 | case K_KDEL: |
| 1437 | case Ctrl_W: |
| 1438 | #ifdef FEAT_FKMAP |
| 1439 | if (cmd_fkmap && c == K_BS) |
| 1440 | c = K_DEL; |
| 1441 | #endif |
| 1442 | if (c == K_KDEL) |
| 1443 | c = K_DEL; |
| 1444 | |
| 1445 | /* |
| 1446 | * delete current character is the same as backspace on next |
| 1447 | * character, except at end of line |
| 1448 | */ |
| 1449 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1450 | ++ccline.cmdpos; |
| 1451 | #ifdef FEAT_MBYTE |
| 1452 | if (has_mbyte && c == K_DEL) |
| 1453 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1454 | ccline.cmdbuff + ccline.cmdpos); |
| 1455 | #endif |
| 1456 | if (ccline.cmdpos > 0) |
| 1457 | { |
| 1458 | char_u *p; |
| 1459 | |
| 1460 | j = ccline.cmdpos; |
| 1461 | p = ccline.cmdbuff + j; |
| 1462 | #ifdef FEAT_MBYTE |
| 1463 | if (has_mbyte) |
| 1464 | { |
| 1465 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1466 | if (c == Ctrl_W) |
| 1467 | { |
| 1468 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1469 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1470 | i = mb_get_class(p); |
| 1471 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1472 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1473 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1474 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | else |
| 1478 | #endif |
| 1479 | if (c == Ctrl_W) |
| 1480 | { |
| 1481 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1482 | --p; |
| 1483 | i = vim_iswordc(p[-1]); |
| 1484 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1485 | && vim_iswordc(p[-1]) == i) |
| 1486 | --p; |
| 1487 | } |
| 1488 | else |
| 1489 | --p; |
| 1490 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1491 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1492 | i = ccline.cmdpos; |
| 1493 | while (i < ccline.cmdlen) |
| 1494 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1495 | |
| 1496 | /* Truncate at the end, required for multi-byte chars. */ |
| 1497 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1498 | #ifdef FEAT_SEARCH_EXTRA |
| 1499 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1500 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1501 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1502 | /* save view settings, so that the screen |
| 1503 | * won't be restored at the wrong position */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1504 | is_state.old_viewstate = is_state.init_viewstate; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1505 | } |
| 1506 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1507 | redrawcmd(); |
| 1508 | } |
| 1509 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1510 | && ccline.cmdprompt == NULL && indent == 0) |
| 1511 | { |
| 1512 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1513 | if (exmode_active |
| 1514 | #ifdef FEAT_EVAL |
| 1515 | || ccline.cmdfirstc == '>' |
| 1516 | #endif |
| 1517 | ) |
| 1518 | goto cmdline_not_changed; |
| 1519 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1520 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1521 | if (!cmd_silent) |
| 1522 | { |
| 1523 | #ifdef FEAT_RIGHTLEFT |
| 1524 | if (cmdmsg_rl) |
| 1525 | msg_col = Columns; |
| 1526 | else |
| 1527 | #endif |
| 1528 | msg_col = 0; |
| 1529 | msg_putchar(' '); /* delete ':' */ |
| 1530 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1531 | #ifdef FEAT_SEARCH_EXTRA |
| 1532 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1533 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1534 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | redraw_cmdline = TRUE; |
| 1536 | goto returncmd; /* back to cmd mode */ |
| 1537 | } |
| 1538 | goto cmdline_changed; |
| 1539 | |
| 1540 | case K_INS: |
| 1541 | case K_KINS: |
| 1542 | #ifdef FEAT_FKMAP |
| 1543 | /* if Farsi mode set, we are in reverse insert mode - |
| 1544 | Do not change the mode */ |
| 1545 | if (cmd_fkmap) |
| 1546 | beep_flush(); |
| 1547 | else |
| 1548 | #endif |
| 1549 | ccline.overstrike = !ccline.overstrike; |
| 1550 | #ifdef CURSOR_SHAPE |
| 1551 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1552 | #endif |
| 1553 | goto cmdline_not_changed; |
| 1554 | |
| 1555 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1556 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1557 | { |
| 1558 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1559 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1560 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1561 | im_set_active(FALSE); /* Disable input method */ |
| 1562 | #endif |
| 1563 | if (b_im_ptr != NULL) |
| 1564 | { |
| 1565 | if (State & LANGMAP) |
| 1566 | *b_im_ptr = B_IMODE_LMAP; |
| 1567 | else |
| 1568 | *b_im_ptr = B_IMODE_NONE; |
| 1569 | } |
| 1570 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1571 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | else |
| 1573 | { |
| 1574 | /* There are no ":lmap" mappings, toggle IM. When |
| 1575 | * 'imdisable' is set don't try getting the status, it's |
| 1576 | * always off. */ |
| 1577 | if ((p_imdisable && b_im_ptr != NULL) |
| 1578 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1579 | { |
| 1580 | im_set_active(FALSE); /* Disable input method */ |
| 1581 | if (b_im_ptr != NULL) |
| 1582 | *b_im_ptr = B_IMODE_NONE; |
| 1583 | } |
| 1584 | else |
| 1585 | { |
| 1586 | im_set_active(TRUE); /* Enable input method */ |
| 1587 | if (b_im_ptr != NULL) |
| 1588 | *b_im_ptr = B_IMODE_IM; |
| 1589 | } |
| 1590 | } |
| 1591 | #endif |
| 1592 | if (b_im_ptr != NULL) |
| 1593 | { |
| 1594 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1595 | set_iminsert_global(); |
| 1596 | else |
| 1597 | set_imsearch_global(); |
| 1598 | } |
| 1599 | #ifdef CURSOR_SHAPE |
| 1600 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1601 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1602 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1603 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1604 | status_redraw_curbuf(); |
| 1605 | #endif |
| 1606 | goto cmdline_not_changed; |
| 1607 | |
| 1608 | /* case '@': only in very old vi */ |
| 1609 | case Ctrl_U: |
| 1610 | /* delete all characters left of the cursor */ |
| 1611 | j = ccline.cmdpos; |
| 1612 | ccline.cmdlen -= j; |
| 1613 | i = ccline.cmdpos = 0; |
| 1614 | while (i < ccline.cmdlen) |
| 1615 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1616 | /* Truncate at the end, required for multi-byte chars. */ |
| 1617 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1618 | #ifdef FEAT_SEARCH_EXTRA |
| 1619 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1620 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1621 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | redrawcmd(); |
| 1623 | goto cmdline_changed; |
| 1624 | |
| 1625 | #ifdef FEAT_CLIPBOARD |
| 1626 | case Ctrl_Y: |
| 1627 | /* Copy the modeless selection, if there is one. */ |
| 1628 | if (clip_star.state != SELECT_CLEARED) |
| 1629 | { |
| 1630 | if (clip_star.state == SELECT_DONE) |
| 1631 | clip_copy_modeless_selection(TRUE); |
| 1632 | goto cmdline_not_changed; |
| 1633 | } |
| 1634 | break; |
| 1635 | #endif |
| 1636 | |
| 1637 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1638 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1639 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1640 | * ":normal" runs out of characters. */ |
| 1641 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1642 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1643 | goto cmdline_not_changed; |
| 1644 | |
| 1645 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1646 | putting it in history */ |
| 1647 | goto returncmd; /* back to cmd mode */ |
| 1648 | |
| 1649 | case Ctrl_R: /* insert register */ |
| 1650 | #ifdef USE_ON_FLY_SCROLL |
| 1651 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1652 | #endif |
| 1653 | putcmdline('"', TRUE); |
| 1654 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1655 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1656 | if (i == Ctrl_O) |
| 1657 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1658 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1659 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1660 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1661 | --no_mapping; |
| 1662 | #ifdef FEAT_EVAL |
| 1663 | /* |
| 1664 | * Insert the result of an expression. |
| 1665 | * Need to save the current command line, to be able to enter |
| 1666 | * a new one... |
| 1667 | */ |
| 1668 | new_cmdpos = -1; |
| 1669 | if (c == '=') |
| 1670 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1671 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1672 | { |
| 1673 | beep_flush(); |
| 1674 | c = ESC; |
| 1675 | } |
| 1676 | else |
| 1677 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1678 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1679 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1680 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | #endif |
| 1684 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1685 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1686 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1687 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1688 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1689 | /* When there was a serious error abort getting the |
| 1690 | * command line. */ |
| 1691 | if (aborting()) |
| 1692 | { |
| 1693 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1694 | putting it in history */ |
| 1695 | goto returncmd; /* back to cmd mode */ |
| 1696 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1697 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1698 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1699 | #ifdef FEAT_EVAL |
| 1700 | if (new_cmdpos >= 0) |
| 1701 | { |
| 1702 | /* set_cmdline_pos() was used */ |
| 1703 | if (new_cmdpos > ccline.cmdlen) |
| 1704 | ccline.cmdpos = ccline.cmdlen; |
| 1705 | else |
| 1706 | ccline.cmdpos = new_cmdpos; |
| 1707 | } |
| 1708 | #endif |
| 1709 | } |
| 1710 | redrawcmd(); |
| 1711 | goto cmdline_changed; |
| 1712 | |
| 1713 | case Ctrl_D: |
| 1714 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1715 | break; /* Use ^D as normal char instead */ |
| 1716 | |
| 1717 | redrawcmd(); |
| 1718 | continue; /* don't do incremental search now */ |
| 1719 | |
| 1720 | case K_RIGHT: |
| 1721 | case K_S_RIGHT: |
| 1722 | case K_C_RIGHT: |
| 1723 | do |
| 1724 | { |
| 1725 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1726 | break; |
| 1727 | i = cmdline_charsize(ccline.cmdpos); |
| 1728 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1729 | break; |
| 1730 | ccline.cmdspos += i; |
| 1731 | #ifdef FEAT_MBYTE |
| 1732 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1733 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1734 | + ccline.cmdpos); |
| 1735 | else |
| 1736 | #endif |
| 1737 | ++ccline.cmdpos; |
| 1738 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1739 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1740 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1741 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1742 | #ifdef FEAT_MBYTE |
| 1743 | if (has_mbyte) |
| 1744 | set_cmdspos_cursor(); |
| 1745 | #endif |
| 1746 | goto cmdline_not_changed; |
| 1747 | |
| 1748 | case K_LEFT: |
| 1749 | case K_S_LEFT: |
| 1750 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1751 | if (ccline.cmdpos == 0) |
| 1752 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | do |
| 1754 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | --ccline.cmdpos; |
| 1756 | #ifdef FEAT_MBYTE |
| 1757 | if (has_mbyte) /* move to first byte of char */ |
| 1758 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1759 | ccline.cmdbuff + ccline.cmdpos); |
| 1760 | #endif |
| 1761 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1762 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1763 | while (ccline.cmdpos > 0 |
| 1764 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1765 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1767 | #ifdef FEAT_MBYTE |
| 1768 | if (has_mbyte) |
| 1769 | set_cmdspos_cursor(); |
| 1770 | #endif |
| 1771 | goto cmdline_not_changed; |
| 1772 | |
| 1773 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1774 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1775 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1777 | #ifdef FEAT_GUI_W32 |
| 1778 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1779 | * cancelled. */ |
| 1780 | case K_F4: |
| 1781 | if (mod_mask == MOD_MASK_ALT) |
| 1782 | { |
| 1783 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1784 | goto cmdline_not_changed; |
| 1785 | } |
| 1786 | break; |
| 1787 | #endif |
| 1788 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1789 | #ifdef FEAT_MOUSE |
| 1790 | case K_MIDDLEDRAG: |
| 1791 | case K_MIDDLERELEASE: |
| 1792 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1793 | |
| 1794 | case K_MIDDLEMOUSE: |
| 1795 | # ifdef FEAT_GUI |
| 1796 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1797 | if (!gui.in_use) |
| 1798 | # endif |
| 1799 | if (!mouse_has(MOUSE_COMMAND)) |
| 1800 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1801 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1802 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1803 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1804 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1805 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1806 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1807 | redrawcmd(); |
| 1808 | goto cmdline_changed; |
| 1809 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1810 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1811 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1812 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1813 | redrawcmd(); |
| 1814 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1815 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1816 | |
| 1817 | case K_LEFTDRAG: |
| 1818 | case K_LEFTRELEASE: |
| 1819 | case K_RIGHTDRAG: |
| 1820 | case K_RIGHTRELEASE: |
| 1821 | /* Ignore drag and release events when the button-down wasn't |
| 1822 | * seen before. */ |
| 1823 | if (ignore_drag_release) |
| 1824 | goto cmdline_not_changed; |
| 1825 | /* FALLTHROUGH */ |
| 1826 | case K_LEFTMOUSE: |
| 1827 | case K_RIGHTMOUSE: |
| 1828 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1829 | ignore_drag_release = TRUE; |
| 1830 | else |
| 1831 | ignore_drag_release = FALSE; |
| 1832 | # ifdef FEAT_GUI |
| 1833 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1834 | if (!gui.in_use) |
| 1835 | # endif |
| 1836 | if (!mouse_has(MOUSE_COMMAND)) |
| 1837 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1838 | # ifdef FEAT_CLIPBOARD |
| 1839 | if (mouse_row < cmdline_row && clip_star.available) |
| 1840 | { |
| 1841 | int button, is_click, is_drag; |
| 1842 | |
| 1843 | /* |
| 1844 | * Handle modeless selection. |
| 1845 | */ |
| 1846 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1847 | &is_click, &is_drag); |
| 1848 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1849 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1850 | { |
| 1851 | /* Translate shift-left to right button. */ |
| 1852 | button = MOUSE_RIGHT; |
| 1853 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1854 | } |
| 1855 | clip_modeless(button, is_click, is_drag); |
| 1856 | goto cmdline_not_changed; |
| 1857 | } |
| 1858 | # endif |
| 1859 | |
| 1860 | set_cmdspos(); |
| 1861 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1862 | ++ccline.cmdpos) |
| 1863 | { |
| 1864 | i = cmdline_charsize(ccline.cmdpos); |
| 1865 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1866 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1867 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1868 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1869 | if (has_mbyte) |
| 1870 | { |
| 1871 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1872 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1873 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | + ccline.cmdpos) - 1; |
| 1875 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1876 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1877 | ccline.cmdspos += i; |
| 1878 | } |
| 1879 | goto cmdline_not_changed; |
| 1880 | |
| 1881 | /* Mouse scroll wheel: ignored here */ |
| 1882 | case K_MOUSEDOWN: |
| 1883 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1884 | case K_MOUSELEFT: |
| 1885 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1886 | /* Alternate buttons ignored here */ |
| 1887 | case K_X1MOUSE: |
| 1888 | case K_X1DRAG: |
| 1889 | case K_X1RELEASE: |
| 1890 | case K_X2MOUSE: |
| 1891 | case K_X2DRAG: |
| 1892 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1893 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | goto cmdline_not_changed; |
| 1895 | |
| 1896 | #endif /* FEAT_MOUSE */ |
| 1897 | |
| 1898 | #ifdef FEAT_GUI |
| 1899 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1900 | case K_LEFTRELEASE_NM: |
| 1901 | goto cmdline_not_changed; |
| 1902 | |
| 1903 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1904 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | { |
| 1906 | gui_do_scroll(); |
| 1907 | redrawcmd(); |
| 1908 | } |
| 1909 | goto cmdline_not_changed; |
| 1910 | |
| 1911 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1912 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1913 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1914 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | redrawcmd(); |
| 1916 | } |
| 1917 | goto cmdline_not_changed; |
| 1918 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1919 | #ifdef FEAT_GUI_TABLINE |
| 1920 | case K_TABLINE: |
| 1921 | case K_TABMENU: |
| 1922 | /* Don't want to change any tabs here. Make sure the same tab |
| 1923 | * is still selected. */ |
| 1924 | if (gui_use_tabline()) |
| 1925 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1926 | goto cmdline_not_changed; |
| 1927 | #endif |
| 1928 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1929 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1930 | goto cmdline_not_changed; |
| 1931 | |
| 1932 | case Ctrl_B: /* begin of command line */ |
| 1933 | case K_HOME: |
| 1934 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1935 | case K_S_HOME: |
| 1936 | case K_C_HOME: |
| 1937 | ccline.cmdpos = 0; |
| 1938 | set_cmdspos(); |
| 1939 | goto cmdline_not_changed; |
| 1940 | |
| 1941 | case Ctrl_E: /* end of command line */ |
| 1942 | case K_END: |
| 1943 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1944 | case K_S_END: |
| 1945 | case K_C_END: |
| 1946 | ccline.cmdpos = ccline.cmdlen; |
| 1947 | set_cmdspos_cursor(); |
| 1948 | goto cmdline_not_changed; |
| 1949 | |
| 1950 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1951 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | goto cmdline_changed; |
| 1954 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1955 | case Ctrl_L: |
| 1956 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1957 | if (may_add_char_to_search(firstc, &c, &is_state) == OK) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1958 | goto cmdline_not_changed; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1959 | #endif |
| 1960 | |
| 1961 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1962 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | break; |
| 1964 | goto cmdline_changed; |
| 1965 | |
| 1966 | case Ctrl_N: /* next match */ |
| 1967 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 1968 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1969 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1970 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1971 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1973 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1975 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 1976 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1977 | case K_UP: |
| 1978 | case K_DOWN: |
| 1979 | case K_S_UP: |
| 1980 | case K_S_DOWN: |
| 1981 | case K_PAGEUP: |
| 1982 | case K_KPAGEUP: |
| 1983 | case K_PAGEDOWN: |
| 1984 | case K_KPAGEDOWN: |
| 1985 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1986 | goto cmdline_not_changed; |
| 1987 | |
| 1988 | i = hiscnt; |
| 1989 | |
| 1990 | /* save current command string so it can be restored later */ |
| 1991 | if (lookfor == NULL) |
| 1992 | { |
| 1993 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1994 | goto cmdline_not_changed; |
| 1995 | lookfor[ccline.cmdpos] = NUL; |
| 1996 | } |
| 1997 | |
| 1998 | j = (int)STRLEN(lookfor); |
| 1999 | for (;;) |
| 2000 | { |
| 2001 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2002 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2003 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | { |
| 2005 | if (hiscnt == hislen) /* first time */ |
| 2006 | hiscnt = hisidx[histype]; |
| 2007 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 2008 | hiscnt = hislen - 1; |
| 2009 | else if (hiscnt != hisidx[histype] + 1) |
| 2010 | --hiscnt; |
| 2011 | else /* at top of list */ |
| 2012 | { |
| 2013 | hiscnt = i; |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
| 2017 | else /* one step forwards */ |
| 2018 | { |
| 2019 | /* on last entry, clear the line */ |
| 2020 | if (hiscnt == hisidx[histype]) |
| 2021 | { |
| 2022 | hiscnt = hislen; |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | /* not on a history line, nothing to do */ |
| 2027 | if (hiscnt == hislen) |
| 2028 | break; |
| 2029 | if (hiscnt == hislen - 1) /* wrap around */ |
| 2030 | hiscnt = 0; |
| 2031 | else |
| 2032 | ++hiscnt; |
| 2033 | } |
| 2034 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 2035 | { |
| 2036 | hiscnt = i; |
| 2037 | break; |
| 2038 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2039 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2040 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2041 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 2042 | lookfor, (size_t)j) == 0) |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | if (hiscnt != i) /* jumped to other entry */ |
| 2047 | { |
| 2048 | char_u *p; |
| 2049 | int len; |
| 2050 | int old_firstc; |
| 2051 | |
| 2052 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2053 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | if (hiscnt == hislen) |
| 2055 | p = lookfor; /* back to the old one */ |
| 2056 | else |
| 2057 | p = history[histype][hiscnt].hisstr; |
| 2058 | |
| 2059 | if (histype == HIST_SEARCH |
| 2060 | && p != lookfor |
| 2061 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 2062 | { |
| 2063 | /* Correct for the separator character used when |
| 2064 | * adding the history entry vs the one used now. |
| 2065 | * First loop: count length. |
| 2066 | * Second loop: copy the characters. */ |
| 2067 | for (i = 0; i <= 1; ++i) |
| 2068 | { |
| 2069 | len = 0; |
| 2070 | for (j = 0; p[j] != NUL; ++j) |
| 2071 | { |
| 2072 | /* Replace old sep with new sep, unless it is |
| 2073 | * escaped. */ |
| 2074 | if (p[j] == old_firstc |
| 2075 | && (j == 0 || p[j - 1] != '\\')) |
| 2076 | { |
| 2077 | if (i > 0) |
| 2078 | ccline.cmdbuff[len] = firstc; |
| 2079 | } |
| 2080 | else |
| 2081 | { |
| 2082 | /* Escape new sep, unless it is already |
| 2083 | * escaped. */ |
| 2084 | if (p[j] == firstc |
| 2085 | && (j == 0 || p[j - 1] != '\\')) |
| 2086 | { |
| 2087 | if (i > 0) |
| 2088 | ccline.cmdbuff[len] = '\\'; |
| 2089 | ++len; |
| 2090 | } |
| 2091 | if (i > 0) |
| 2092 | ccline.cmdbuff[len] = p[j]; |
| 2093 | } |
| 2094 | ++len; |
| 2095 | } |
| 2096 | if (i == 0) |
| 2097 | { |
| 2098 | alloc_cmdbuff(len); |
| 2099 | if (ccline.cmdbuff == NULL) |
| 2100 | goto returncmd; |
| 2101 | } |
| 2102 | } |
| 2103 | ccline.cmdbuff[len] = NUL; |
| 2104 | } |
| 2105 | else |
| 2106 | { |
| 2107 | alloc_cmdbuff((int)STRLEN(p)); |
| 2108 | if (ccline.cmdbuff == NULL) |
| 2109 | goto returncmd; |
| 2110 | STRCPY(ccline.cmdbuff, p); |
| 2111 | } |
| 2112 | |
| 2113 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 2114 | redrawcmd(); |
| 2115 | goto cmdline_changed; |
| 2116 | } |
| 2117 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2118 | #endif |
| 2119 | goto cmdline_not_changed; |
| 2120 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2121 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2122 | case Ctrl_G: /* next match */ |
| 2123 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2124 | if (may_adjust_incsearch_highlighting( |
| 2125 | firstc, count, &is_state, c) == FAIL) |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2126 | goto cmdline_not_changed; |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2127 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | #endif |
| 2129 | |
| 2130 | case Ctrl_V: |
| 2131 | case Ctrl_Q: |
| 2132 | #ifdef FEAT_MOUSE |
| 2133 | ignore_drag_release = TRUE; |
| 2134 | #endif |
| 2135 | putcmdline('^', TRUE); |
| 2136 | c = get_literal(); /* get next (two) character(s) */ |
| 2137 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2138 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | #ifdef FEAT_MBYTE |
| 2140 | /* may need to remove ^ when composing char was typed */ |
| 2141 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 2142 | { |
| 2143 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2144 | msg_putchar(' '); |
| 2145 | cursorcmd(); |
| 2146 | } |
| 2147 | #endif |
| 2148 | break; |
| 2149 | |
| 2150 | #ifdef FEAT_DIGRAPHS |
| 2151 | case Ctrl_K: |
| 2152 | #ifdef FEAT_MOUSE |
| 2153 | ignore_drag_release = TRUE; |
| 2154 | #endif |
| 2155 | putcmdline('?', TRUE); |
| 2156 | #ifdef USE_ON_FLY_SCROLL |
| 2157 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 2158 | #endif |
| 2159 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2160 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2161 | if (c != NUL) |
| 2162 | break; |
| 2163 | |
| 2164 | redrawcmd(); |
| 2165 | goto cmdline_not_changed; |
| 2166 | #endif /* FEAT_DIGRAPHS */ |
| 2167 | |
| 2168 | #ifdef FEAT_RIGHTLEFT |
| 2169 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 2170 | if (!p_ari) |
| 2171 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2172 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2173 | if (p_altkeymap) |
| 2174 | { |
| 2175 | cmd_fkmap = !cmd_fkmap; |
| 2176 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 2177 | ccline.overstrike = FALSE; |
| 2178 | } |
| 2179 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2180 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | cmd_hkmap = !cmd_hkmap; |
| 2182 | goto cmdline_not_changed; |
| 2183 | #endif |
| 2184 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2185 | case K_PS: |
| 2186 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 2187 | goto cmdline_changed; |
| 2188 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2189 | default: |
| 2190 | #ifdef UNIX |
| 2191 | if (c == intr_char) |
| 2192 | { |
| 2193 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 2194 | putting it in history */ |
| 2195 | goto returncmd; /* back to Normal mode */ |
| 2196 | } |
| 2197 | #endif |
| 2198 | /* |
| 2199 | * Normal character with no special meaning. Just set mod_mask |
| 2200 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 2201 | * the string <S-Space>. This should only happen after ^V. |
| 2202 | */ |
| 2203 | if (!IS_SPECIAL(c)) |
| 2204 | mod_mask = 0x0; |
| 2205 | break; |
| 2206 | } |
| 2207 | /* |
| 2208 | * End of switch on command line character. |
| 2209 | * We come here if we have a normal character. |
| 2210 | */ |
| 2211 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2212 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2213 | #ifdef FEAT_MBYTE |
| 2214 | /* Add ABBR_OFF for characters above 0x100, this is |
| 2215 | * what check_abbr() expects. */ |
| 2216 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 2217 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2218 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2219 | goto cmdline_changed; |
| 2220 | |
| 2221 | /* |
| 2222 | * put the character in the command line |
| 2223 | */ |
| 2224 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 2225 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 2226 | else |
| 2227 | { |
| 2228 | #ifdef FEAT_MBYTE |
| 2229 | if (has_mbyte) |
| 2230 | { |
| 2231 | j = (*mb_char2bytes)(c, IObuff); |
| 2232 | IObuff[j] = NUL; /* exclude composing chars */ |
| 2233 | put_on_cmdline(IObuff, j, TRUE); |
| 2234 | } |
| 2235 | else |
| 2236 | #endif |
| 2237 | { |
| 2238 | IObuff[0] = c; |
| 2239 | put_on_cmdline(IObuff, 1, TRUE); |
| 2240 | } |
| 2241 | } |
| 2242 | goto cmdline_changed; |
| 2243 | |
| 2244 | /* |
| 2245 | * This part implements incremental searches for "/" and "?" |
| 2246 | * Jump to cmdline_not_changed when a character has been read but the command |
| 2247 | * line did not change. Then we only search and redraw if something changed in |
| 2248 | * the past. |
| 2249 | * Jump to cmdline_changed when the command line did change. |
| 2250 | * (Sorry for the goto's, I know it is ugly). |
| 2251 | */ |
| 2252 | cmdline_not_changed: |
| 2253 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2254 | if (!is_state.incsearch_postponed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2255 | continue; |
| 2256 | #endif |
| 2257 | |
| 2258 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2259 | /* Trigger CmdlineChanged autocommands. */ |
| 2260 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2262 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2263 | may_do_incsearch_highlighting(firstc, count, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | #endif |
| 2265 | |
| 2266 | #ifdef FEAT_RIGHTLEFT |
| 2267 | if (cmdmsg_rl |
| 2268 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2269 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | # endif |
| 2271 | ) |
| 2272 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2273 | * right-left typing. Not efficient, but it works. |
| 2274 | * Do it only when there are no characters left to read |
| 2275 | * to avoid useless intermediate redraws. */ |
| 2276 | if (vpeekc() == NUL) |
| 2277 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2278 | #endif |
| 2279 | } |
| 2280 | |
| 2281 | returncmd: |
| 2282 | |
| 2283 | #ifdef FEAT_RIGHTLEFT |
| 2284 | cmdmsg_rl = FALSE; |
| 2285 | #endif |
| 2286 | |
| 2287 | #ifdef FEAT_FKMAP |
| 2288 | cmd_fkmap = 0; |
| 2289 | #endif |
| 2290 | |
| 2291 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2292 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | |
| 2294 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2295 | finish_incsearch_highlighting(gotesc, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | #endif |
| 2297 | |
| 2298 | if (ccline.cmdbuff != NULL) |
| 2299 | { |
| 2300 | /* |
| 2301 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2302 | */ |
| 2303 | #ifdef FEAT_CMDHIST |
| 2304 | if (ccline.cmdlen && firstc != NUL |
| 2305 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2306 | { |
| 2307 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2308 | histype == HIST_SEARCH ? firstc : NUL); |
| 2309 | if (firstc == ':') |
| 2310 | { |
| 2311 | vim_free(new_last_cmdline); |
| 2312 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2313 | } |
| 2314 | } |
| 2315 | #endif |
| 2316 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2317 | if (gotesc) |
| 2318 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * If the screen was shifted up, redraw the whole screen (later). |
| 2323 | * If the line is too long, clear it, so ruler and shown command do |
| 2324 | * not get printed in the middle of it. |
| 2325 | */ |
| 2326 | msg_check(); |
| 2327 | msg_scroll = save_msg_scroll; |
| 2328 | redir_off = FALSE; |
| 2329 | |
| 2330 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2331 | if (some_key_typed) |
| 2332 | need_wait_return = FALSE; |
| 2333 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2334 | /* Trigger CmdlineLeave autocommands. */ |
| 2335 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2336 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2338 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2340 | im_save_status(b_im_ptr); |
| 2341 | im_set_active(FALSE); |
| 2342 | #endif |
| 2343 | #ifdef FEAT_MOUSE |
| 2344 | setmouse(); |
| 2345 | #endif |
| 2346 | #ifdef CURSOR_SHAPE |
| 2347 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2348 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2349 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2350 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2351 | { |
| 2352 | char_u *p = ccline.cmdbuff; |
| 2353 | |
| 2354 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2355 | ccline.cmdbuff = NULL; |
| 2356 | return p; |
| 2357 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2361 | /* |
| 2362 | * Get a command line with a prompt. |
| 2363 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2364 | * f_input() when evaluating an expression from CTRL-R =). |
| 2365 | * Returns the command line in allocated memory, or NULL. |
| 2366 | */ |
| 2367 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2368 | getcmdline_prompt( |
| 2369 | int firstc, |
| 2370 | char_u *prompt, /* command line prompt */ |
| 2371 | int attr, /* attributes for prompt */ |
| 2372 | int xp_context, /* type of expansion */ |
| 2373 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2374 | { |
| 2375 | char_u *s; |
| 2376 | struct cmdline_info save_ccline; |
| 2377 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2378 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2380 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2381 | ccline.cmdprompt = prompt; |
| 2382 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2383 | # ifdef FEAT_EVAL |
| 2384 | ccline.xp_context = xp_context; |
| 2385 | ccline.xp_arg = xp_arg; |
| 2386 | ccline.input_fn = (firstc == '@'); |
| 2387 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2388 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2389 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2390 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2391 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2392 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2393 | * But only if called recursively and the commandline is therefore being |
| 2394 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2395 | * so we need its modified msg_col left intact. */ |
| 2396 | if (ccline.cmdbuff != NULL) |
| 2397 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2398 | |
| 2399 | return s; |
| 2400 | } |
| 2401 | #endif |
| 2402 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2403 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2404 | * Return TRUE when the text must not be changed and we can't switch to |
| 2405 | * another window or buffer. Used when editing the command line, evaluating |
| 2406 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2407 | */ |
| 2408 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2409 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2410 | { |
| 2411 | #ifdef FEAT_CMDWIN |
| 2412 | if (cmdwin_type != 0) |
| 2413 | return TRUE; |
| 2414 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2415 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | /* |
| 2419 | * Give an error message for a command that isn't allowed while the cmdline |
| 2420 | * window is open or editing the cmdline in another way. |
| 2421 | */ |
| 2422 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2423 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2424 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2425 | EMSG(_(get_text_locked_msg())); |
| 2426 | } |
| 2427 | |
| 2428 | char_u * |
| 2429 | get_text_locked_msg(void) |
| 2430 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2431 | #ifdef FEAT_CMDWIN |
| 2432 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2433 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2434 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2435 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2438 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2439 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2440 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2441 | */ |
| 2442 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2443 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2444 | { |
| 2445 | if (curbuf_lock > 0) |
| 2446 | { |
| 2447 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2448 | return TRUE; |
| 2449 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2450 | return allbuf_locked(); |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2455 | * message. |
| 2456 | */ |
| 2457 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2458 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2459 | { |
| 2460 | if (allbuf_lock > 0) |
| 2461 | { |
| 2462 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2463 | return TRUE; |
| 2464 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2465 | return FALSE; |
| 2466 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2467 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2469 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | { |
| 2471 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2472 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2473 | return 1; |
| 2474 | #endif |
| 2475 | return ptr2cells(ccline.cmdbuff + idx); |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * Compute the offset of the cursor on the command line for the prompt and |
| 2480 | * indent. |
| 2481 | */ |
| 2482 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2483 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2484 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2485 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2487 | else |
| 2488 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2489 | } |
| 2490 | |
| 2491 | /* |
| 2492 | * Compute the screen position for the cursor on the command line. |
| 2493 | */ |
| 2494 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2495 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | { |
| 2497 | int i, m, c; |
| 2498 | |
| 2499 | set_cmdspos(); |
| 2500 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2501 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2503 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2504 | m = MAXCOL; |
| 2505 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2506 | else |
| 2507 | m = MAXCOL; |
| 2508 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2509 | { |
| 2510 | c = cmdline_charsize(i); |
| 2511 | #ifdef FEAT_MBYTE |
| 2512 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2513 | if (has_mbyte) |
| 2514 | correct_cmdspos(i, c); |
| 2515 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2516 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2517 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2518 | if ((ccline.cmdspos += c) >= m) |
| 2519 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | ccline.cmdspos -= c; |
| 2521 | break; |
| 2522 | } |
| 2523 | #ifdef FEAT_MBYTE |
| 2524 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2525 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2526 | #endif |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | #ifdef FEAT_MBYTE |
| 2531 | /* |
| 2532 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2533 | * character that doesn't fit, so that a ">" must be displayed. |
| 2534 | */ |
| 2535 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2536 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2537 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2538 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2540 | && ccline.cmdspos % Columns + cells > Columns) |
| 2541 | ccline.cmdspos++; |
| 2542 | } |
| 2543 | #endif |
| 2544 | |
| 2545 | /* |
| 2546 | * Get an Ex command line for the ":" command. |
| 2547 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2549 | getexline( |
| 2550 | int c, /* normally ':', NUL for ":append" */ |
| 2551 | void *cookie UNUSED, |
| 2552 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2553 | { |
| 2554 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2555 | if (exec_from_reg && vpeekc() == ':') |
| 2556 | (void)vgetc(); |
| 2557 | return getcmdline(c, 1L, indent); |
| 2558 | } |
| 2559 | |
| 2560 | /* |
| 2561 | * Get an Ex command line for Ex mode. |
| 2562 | * In Ex mode we only use the OS supplied line editing features and no |
| 2563 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2564 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2565 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2566 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2567 | getexmodeline( |
| 2568 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2569 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2570 | void *cookie UNUSED, |
| 2571 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2572 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2573 | garray_T line_ga; |
| 2574 | char_u *pend; |
| 2575 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2576 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2577 | int escaped = FALSE; /* CTRL-V typed */ |
| 2578 | int vcol = 0; |
| 2579 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2580 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2581 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2582 | |
| 2583 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2584 | * confuses the system function that computes tabstops. */ |
| 2585 | cursor_on(); |
| 2586 | |
| 2587 | /* always start in column 0; write a newline if necessary */ |
| 2588 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2589 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2590 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2591 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2592 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2593 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2594 | if (p_prompt) |
| 2595 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2596 | while (indent-- > 0) |
| 2597 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2598 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | ga_init2(&line_ga, 1, 30); |
| 2602 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2603 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2604 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2605 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2606 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2607 | while (indent >= 8) |
| 2608 | { |
| 2609 | ga_append(&line_ga, TAB); |
| 2610 | msg_puts((char_u *)" "); |
| 2611 | indent -= 8; |
| 2612 | } |
| 2613 | while (indent-- > 0) |
| 2614 | { |
| 2615 | ga_append(&line_ga, ' '); |
| 2616 | msg_putchar(' '); |
| 2617 | } |
| 2618 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2619 | ++no_mapping; |
| 2620 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2621 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | /* |
| 2623 | * Get the line, one character at a time. |
| 2624 | */ |
| 2625 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2626 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2627 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2628 | long sw; |
| 2629 | char_u *s; |
| 2630 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2632 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2634 | /* |
| 2635 | * Get one character at a time. |
| 2636 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2637 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2638 | |
| 2639 | /* Check for a ":normal" command and no more characters left. */ |
| 2640 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2641 | c1 = '\n'; |
| 2642 | else |
| 2643 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | |
| 2645 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2646 | * Handle line editing. |
| 2647 | * Previously this was left to the system, putting the terminal in |
| 2648 | * 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] | 2649 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2650 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2651 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2652 | msg_putchar('\n'); |
| 2653 | break; |
| 2654 | } |
| 2655 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2656 | if (c1 == K_PS) |
| 2657 | { |
| 2658 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2659 | goto redraw; |
| 2660 | } |
| 2661 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2662 | if (!escaped) |
| 2663 | { |
| 2664 | /* CR typed means "enter", which is NL */ |
| 2665 | if (c1 == '\r') |
| 2666 | c1 = '\n'; |
| 2667 | |
| 2668 | if (c1 == BS || c1 == K_BS |
| 2669 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2671 | if (line_ga.ga_len > 0) |
| 2672 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2673 | #ifdef FEAT_MBYTE |
| 2674 | if (has_mbyte) |
| 2675 | { |
| 2676 | p = (char_u *)line_ga.ga_data; |
| 2677 | p[line_ga.ga_len] = NUL; |
| 2678 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2679 | line_ga.ga_len -= len; |
| 2680 | } |
| 2681 | else |
| 2682 | #endif |
| 2683 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2684 | goto redraw; |
| 2685 | } |
| 2686 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2689 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2691 | msg_col = startcol; |
| 2692 | msg_clr_eos(); |
| 2693 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2694 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | if (c1 == Ctrl_T) |
| 2698 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2699 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2700 | p = (char_u *)line_ga.ga_data; |
| 2701 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2702 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2703 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2704 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2705 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2707 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2708 | p = (char_u *)line_ga.ga_data; |
| 2709 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2710 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2711 | *s = ' '; |
| 2712 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2713 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2714 | redraw: |
| 2715 | /* redraw the line */ |
| 2716 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2717 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2718 | p = (char_u *)line_ga.ga_data; |
| 2719 | p[line_ga.ga_len] = NUL; |
| 2720 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2722 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2724 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2726 | msg_putchar(' '); |
| 2727 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2728 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2729 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2730 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2732 | len = MB_PTR2LEN(p); |
| 2733 | msg_outtrans_len(p, len); |
| 2734 | vcol += ptr2cells(p); |
| 2735 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | } |
| 2737 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2738 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2739 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2740 | continue; |
| 2741 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2743 | if (c1 == Ctrl_D) |
| 2744 | { |
| 2745 | /* Delete one shiftwidth. */ |
| 2746 | p = (char_u *)line_ga.ga_data; |
| 2747 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2749 | if (prev_char == '^') |
| 2750 | ex_keep_indent = TRUE; |
| 2751 | indent = 0; |
| 2752 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2753 | } |
| 2754 | else |
| 2755 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2756 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2757 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2758 | if (indent > 0) |
| 2759 | { |
| 2760 | --indent; |
| 2761 | indent -= indent % get_sw_value(curbuf); |
| 2762 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2763 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2764 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2765 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2766 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2767 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2768 | --line_ga.ga_len; |
| 2769 | } |
| 2770 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2771 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2772 | |
| 2773 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2774 | { |
| 2775 | escaped = TRUE; |
| 2776 | continue; |
| 2777 | } |
| 2778 | |
| 2779 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2780 | if (IS_SPECIAL(c1)) |
| 2781 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2783 | |
| 2784 | if (IS_SPECIAL(c1)) |
| 2785 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2786 | #ifdef FEAT_MBYTE |
| 2787 | if (has_mbyte) |
| 2788 | len = (*mb_char2bytes)(c1, |
| 2789 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2790 | else |
| 2791 | #endif |
| 2792 | { |
| 2793 | len = 1; |
| 2794 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2795 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2796 | if (c1 == '\n') |
| 2797 | msg_putchar('\n'); |
| 2798 | else if (c1 == TAB) |
| 2799 | { |
| 2800 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2801 | do |
| 2802 | { |
| 2803 | msg_putchar(' '); |
| 2804 | } while (++vcol % 8); |
| 2805 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2807 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2808 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2809 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2810 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2812 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2813 | escaped = FALSE; |
| 2814 | |
| 2815 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2816 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2817 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2818 | /* We are done when a NL is entered, but not when it comes after an |
| 2819 | * odd number of backslashes, that results in a NUL. */ |
| 2820 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2822 | int bcount = 0; |
| 2823 | |
| 2824 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2825 | ++bcount; |
| 2826 | |
| 2827 | if (bcount > 0) |
| 2828 | { |
| 2829 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2830 | * "\NL", etc. */ |
| 2831 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2832 | pend -= (bcount + 1) / 2; |
| 2833 | pend[-1] = '\n'; |
| 2834 | } |
| 2835 | |
| 2836 | if ((bcount & 1) == 0) |
| 2837 | { |
| 2838 | --line_ga.ga_len; |
| 2839 | --pend; |
| 2840 | *pend = NUL; |
| 2841 | break; |
| 2842 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2846 | --no_mapping; |
| 2847 | --allow_keys; |
| 2848 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2849 | /* make following messages go to the next line */ |
| 2850 | msg_didout = FALSE; |
| 2851 | msg_col = 0; |
| 2852 | if (msg_row < Rows - 1) |
| 2853 | ++msg_row; |
| 2854 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2855 | |
| 2856 | if (got_int) |
| 2857 | ga_clear(&line_ga); |
| 2858 | |
| 2859 | return (char_u *)line_ga.ga_data; |
| 2860 | } |
| 2861 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2862 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2863 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2864 | /* |
| 2865 | * Return TRUE if ccline.overstrike is on. |
| 2866 | */ |
| 2867 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2868 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2869 | { |
| 2870 | return ccline.overstrike; |
| 2871 | } |
| 2872 | |
| 2873 | /* |
| 2874 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2875 | */ |
| 2876 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2877 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2878 | { |
| 2879 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2880 | } |
| 2881 | #endif |
| 2882 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2883 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2884 | /* |
| 2885 | * Return the virtual column number at the current cursor position. |
| 2886 | * This is used by the IM code to obtain the start of the preedit string. |
| 2887 | */ |
| 2888 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2889 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2890 | { |
| 2891 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2892 | return MAXCOL; |
| 2893 | |
| 2894 | # ifdef FEAT_MBYTE |
| 2895 | if (has_mbyte) |
| 2896 | { |
| 2897 | colnr_T col; |
| 2898 | int i = 0; |
| 2899 | |
| 2900 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2901 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | |
| 2903 | return col; |
| 2904 | } |
| 2905 | else |
| 2906 | # endif |
| 2907 | return ccline.cmdpos; |
| 2908 | } |
| 2909 | #endif |
| 2910 | |
| 2911 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2912 | /* |
| 2913 | * If part of the command line is an IM preedit string, redraw it with |
| 2914 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2915 | */ |
| 2916 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2917 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | { |
| 2919 | if ((State & CMDLINE) |
| 2920 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2921 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2922 | && !p_imdisable |
| 2923 | && im_is_preediting()) |
| 2924 | { |
| 2925 | int cmdpos = 0; |
| 2926 | int cmdspos; |
| 2927 | int old_row; |
| 2928 | int old_col; |
| 2929 | colnr_T col; |
| 2930 | |
| 2931 | old_row = msg_row; |
| 2932 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2933 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2934 | |
| 2935 | # ifdef FEAT_MBYTE |
| 2936 | if (has_mbyte) |
| 2937 | { |
| 2938 | for (col = 0; col < preedit_start_col |
| 2939 | && cmdpos < ccline.cmdlen; ++col) |
| 2940 | { |
| 2941 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2942 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | else |
| 2946 | # endif |
| 2947 | { |
| 2948 | cmdspos += preedit_start_col; |
| 2949 | cmdpos += preedit_start_col; |
| 2950 | } |
| 2951 | |
| 2952 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2953 | msg_col = cmdspos % (int)Columns; |
| 2954 | if (msg_row >= Rows) |
| 2955 | msg_row = Rows - 1; |
| 2956 | |
| 2957 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2958 | { |
| 2959 | int char_len; |
| 2960 | int char_attr; |
| 2961 | |
| 2962 | char_attr = im_get_feedback_attr(col); |
| 2963 | if (char_attr < 0) |
| 2964 | break; /* end of preedit string */ |
| 2965 | |
| 2966 | # ifdef FEAT_MBYTE |
| 2967 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2968 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2969 | else |
| 2970 | # endif |
| 2971 | char_len = 1; |
| 2972 | |
| 2973 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2974 | cmdpos += char_len; |
| 2975 | } |
| 2976 | |
| 2977 | msg_row = old_row; |
| 2978 | msg_col = old_col; |
| 2979 | } |
| 2980 | } |
| 2981 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2982 | |
| 2983 | /* |
| 2984 | * Allocate a new command line buffer. |
| 2985 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2986 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2987 | */ |
| 2988 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2989 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | { |
| 2991 | /* |
| 2992 | * give some extra space to avoid having to allocate all the time |
| 2993 | */ |
| 2994 | if (len < 80) |
| 2995 | len = 100; |
| 2996 | else |
| 2997 | len += 20; |
| 2998 | |
| 2999 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 3000 | ccline.cmdbufflen = len; |
| 3001 | } |
| 3002 | |
| 3003 | /* |
| 3004 | * Re-allocate the command line to length len + something extra. |
| 3005 | * return FAIL for failure, OK otherwise |
| 3006 | */ |
| 3007 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3008 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | { |
| 3010 | char_u *p; |
| 3011 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3012 | if (len < ccline.cmdbufflen) |
| 3013 | return OK; /* no need to resize */ |
| 3014 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3015 | p = ccline.cmdbuff; |
| 3016 | alloc_cmdbuff(len); /* will get some more */ |
| 3017 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 3018 | { |
| 3019 | ccline.cmdbuff = p; /* keep the old one */ |
| 3020 | return FAIL; |
| 3021 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 3022 | /* There isn't always a NUL after the command, but it may need to be |
| 3023 | * there, thus copy up to the NUL and add a NUL. */ |
| 3024 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 3025 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3027 | |
| 3028 | if (ccline.xpc != NULL |
| 3029 | && ccline.xpc->xp_pattern != NULL |
| 3030 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 3031 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 3032 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 3033 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3034 | |
| 3035 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 3036 | * to point into the newly allocated memory. */ |
| 3037 | if (i >= 0 && i <= ccline.cmdlen) |
| 3038 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 3039 | } |
| 3040 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3041 | return OK; |
| 3042 | } |
| 3043 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3044 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 3045 | static char_u *arshape_buf = NULL; |
| 3046 | |
| 3047 | # if defined(EXITFREE) || defined(PROTO) |
| 3048 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3049 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3050 | { |
| 3051 | vim_free(arshape_buf); |
| 3052 | } |
| 3053 | # endif |
| 3054 | #endif |
| 3055 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3056 | /* |
| 3057 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 3058 | * when cmdline_star is TRUE. |
| 3059 | */ |
| 3060 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3061 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | { |
| 3063 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3064 | int i; |
| 3065 | |
| 3066 | if (cmdline_star > 0) |
| 3067 | for (i = 0; i < len; ++i) |
| 3068 | { |
| 3069 | msg_putchar('*'); |
| 3070 | # ifdef FEAT_MBYTE |
| 3071 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3072 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3073 | # endif |
| 3074 | } |
| 3075 | else |
| 3076 | #endif |
| 3077 | #ifdef FEAT_ARABIC |
| 3078 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 3079 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3080 | static int buflen = 0; |
| 3081 | char_u *p; |
| 3082 | int j; |
| 3083 | int newlen = 0; |
| 3084 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3085 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3086 | int prev_c = 0; |
| 3087 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3088 | int u8c; |
| 3089 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3090 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | |
| 3092 | /* |
| 3093 | * Do arabic shaping into a temporary buffer. This is very |
| 3094 | * inefficient! |
| 3095 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3096 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3097 | { |
| 3098 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 3099 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3100 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3101 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3102 | arshape_buf = alloc(buflen); |
| 3103 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3104 | return; /* out of memory */ |
| 3105 | } |
| 3106 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3107 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 3108 | { |
| 3109 | /* Prepend a space to draw the leading composing char on. */ |
| 3110 | arshape_buf[0] = ' '; |
| 3111 | newlen = 1; |
| 3112 | } |
| 3113 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3114 | for (j = start; j < start + len; j += mb_l) |
| 3115 | { |
| 3116 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3117 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3118 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3119 | if (ARABIC_CHAR(u8c)) |
| 3120 | { |
| 3121 | /* Do Arabic shaping. */ |
| 3122 | if (cmdmsg_rl) |
| 3123 | { |
| 3124 | /* displaying from right to left */ |
| 3125 | pc = prev_c; |
| 3126 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3127 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3128 | if (j + mb_l >= start + len) |
| 3129 | nc = NUL; |
| 3130 | else |
| 3131 | nc = utf_ptr2char(p + mb_l); |
| 3132 | } |
| 3133 | else |
| 3134 | { |
| 3135 | /* displaying from left to right */ |
| 3136 | if (j + mb_l >= start + len) |
| 3137 | pc = NUL; |
| 3138 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3139 | { |
| 3140 | int pcc[MAX_MCO]; |
| 3141 | |
| 3142 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3143 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3144 | pc1 = pcc[0]; |
| 3145 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3146 | nc = prev_c; |
| 3147 | } |
| 3148 | prev_c = u8c; |
| 3149 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3150 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3151 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3152 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3153 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3154 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3155 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 3156 | if (u8cc[1] != 0) |
| 3157 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3158 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
| 3161 | else |
| 3162 | { |
| 3163 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3164 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3165 | newlen += mb_l; |
| 3166 | } |
| 3167 | } |
| 3168 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3169 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3170 | } |
| 3171 | else |
| 3172 | #endif |
| 3173 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3174 | } |
| 3175 | |
| 3176 | /* |
| 3177 | * Put a character on the command line. Shifts the following text to the |
| 3178 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3179 | * "c" must be printable (fit in one display cell)! |
| 3180 | */ |
| 3181 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3182 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3183 | { |
| 3184 | if (cmd_silent) |
| 3185 | return; |
| 3186 | msg_no_more = TRUE; |
| 3187 | msg_putchar(c); |
| 3188 | if (shift) |
| 3189 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3190 | msg_no_more = FALSE; |
| 3191 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3192 | extra_char = c; |
| 3193 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* |
| 3197 | * Undo a putcmdline(c, FALSE). |
| 3198 | */ |
| 3199 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3200 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | { |
| 3202 | if (cmd_silent) |
| 3203 | return; |
| 3204 | msg_no_more = TRUE; |
| 3205 | if (ccline.cmdlen == ccline.cmdpos) |
| 3206 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3207 | #ifdef FEAT_MBYTE |
| 3208 | else if (has_mbyte) |
| 3209 | draw_cmdline(ccline.cmdpos, |
| 3210 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3211 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3212 | else |
| 3213 | draw_cmdline(ccline.cmdpos, 1); |
| 3214 | msg_no_more = FALSE; |
| 3215 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3216 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Put the given string, of the given length, onto the command line. |
| 3221 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3222 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3223 | * part will be redrawn, otherwise it will not. If this function is called |
| 3224 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3225 | * called afterwards. |
| 3226 | */ |
| 3227 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3228 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3229 | { |
| 3230 | int retval; |
| 3231 | int i; |
| 3232 | int m; |
| 3233 | int c; |
| 3234 | |
| 3235 | if (len < 0) |
| 3236 | len = (int)STRLEN(str); |
| 3237 | |
| 3238 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3239 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3240 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3241 | else |
| 3242 | retval = OK; |
| 3243 | if (retval == OK) |
| 3244 | { |
| 3245 | if (!ccline.overstrike) |
| 3246 | { |
| 3247 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3248 | ccline.cmdbuff + ccline.cmdpos, |
| 3249 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3250 | ccline.cmdlen += len; |
| 3251 | } |
| 3252 | else |
| 3253 | { |
| 3254 | #ifdef FEAT_MBYTE |
| 3255 | if (has_mbyte) |
| 3256 | { |
| 3257 | /* Count nr of characters in the new string. */ |
| 3258 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3259 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3260 | ++m; |
| 3261 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3262 | * characters. */ |
| 3263 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3264 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | --m; |
| 3266 | if (i < ccline.cmdlen) |
| 3267 | { |
| 3268 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3269 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3270 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3271 | } |
| 3272 | else |
| 3273 | ccline.cmdlen = ccline.cmdpos + len; |
| 3274 | } |
| 3275 | else |
| 3276 | #endif |
| 3277 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3278 | ccline.cmdlen = ccline.cmdpos + len; |
| 3279 | } |
| 3280 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3281 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3282 | |
| 3283 | #ifdef FEAT_MBYTE |
| 3284 | if (enc_utf8) |
| 3285 | { |
| 3286 | /* When the inserted text starts with a composing character, |
| 3287 | * backup to the character before it. There could be two of them. |
| 3288 | */ |
| 3289 | i = 0; |
| 3290 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3291 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3292 | { |
| 3293 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3294 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3295 | ccline.cmdpos -= i; |
| 3296 | len += i; |
| 3297 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3298 | } |
| 3299 | # ifdef FEAT_ARABIC |
| 3300 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3301 | { |
| 3302 | /* Check the previous character for Arabic combining pair. */ |
| 3303 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3304 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3305 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3306 | + ccline.cmdpos - i), c)) |
| 3307 | { |
| 3308 | ccline.cmdpos -= i; |
| 3309 | len += i; |
| 3310 | } |
| 3311 | else |
| 3312 | i = 0; |
| 3313 | } |
| 3314 | # endif |
| 3315 | if (i != 0) |
| 3316 | { |
| 3317 | /* Also backup the cursor position. */ |
| 3318 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3319 | ccline.cmdspos -= i; |
| 3320 | msg_col -= i; |
| 3321 | if (msg_col < 0) |
| 3322 | { |
| 3323 | msg_col += Columns; |
| 3324 | --msg_row; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | #endif |
| 3329 | |
| 3330 | if (redraw && !cmd_silent) |
| 3331 | { |
| 3332 | msg_no_more = TRUE; |
| 3333 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3334 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3336 | /* Avoid clearing the rest of the line too often. */ |
| 3337 | if (cmdline_row != i || ccline.overstrike) |
| 3338 | msg_clr_eos(); |
| 3339 | msg_no_more = FALSE; |
| 3340 | } |
| 3341 | #ifdef FEAT_FKMAP |
| 3342 | /* |
| 3343 | * If we are in Farsi command mode, the character input must be in |
| 3344 | * Insert mode. So do not advance the cmdpos. |
| 3345 | */ |
| 3346 | if (!cmd_fkmap) |
| 3347 | #endif |
| 3348 | { |
| 3349 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3350 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3351 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3352 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3353 | m = MAXCOL; |
| 3354 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3355 | else |
| 3356 | m = MAXCOL; |
| 3357 | for (i = 0; i < len; ++i) |
| 3358 | { |
| 3359 | c = cmdline_charsize(ccline.cmdpos); |
| 3360 | #ifdef FEAT_MBYTE |
| 3361 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3362 | if (has_mbyte) |
| 3363 | correct_cmdspos(ccline.cmdpos, c); |
| 3364 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3365 | /* Stop cursor at the end of the screen, but do increment the |
| 3366 | * insert position, so that entering a very long command |
| 3367 | * works, even though you can't see it. */ |
| 3368 | if (ccline.cmdspos + c < m) |
| 3369 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | #ifdef FEAT_MBYTE |
| 3371 | if (has_mbyte) |
| 3372 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3373 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | if (c > len - i - 1) |
| 3375 | c = len - i - 1; |
| 3376 | ccline.cmdpos += c; |
| 3377 | i += c; |
| 3378 | } |
| 3379 | #endif |
| 3380 | ++ccline.cmdpos; |
| 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | if (redraw) |
| 3385 | msg_check(); |
| 3386 | return retval; |
| 3387 | } |
| 3388 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3389 | static struct cmdline_info prev_ccline; |
| 3390 | static int prev_ccline_used = FALSE; |
| 3391 | |
| 3392 | /* |
| 3393 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3394 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3395 | * available globally in prev_ccline. |
| 3396 | */ |
| 3397 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3398 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3399 | { |
| 3400 | if (!prev_ccline_used) |
| 3401 | { |
| 3402 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3403 | prev_ccline_used = TRUE; |
| 3404 | } |
| 3405 | *ccp = prev_ccline; |
| 3406 | prev_ccline = ccline; |
| 3407 | ccline.cmdbuff = NULL; |
| 3408 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3409 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3413 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3414 | */ |
| 3415 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3416 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3417 | { |
| 3418 | ccline = prev_ccline; |
| 3419 | prev_ccline = *ccp; |
| 3420 | } |
| 3421 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3422 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3423 | /* |
| 3424 | * Save the command line into allocated memory. Returns a pointer to be |
| 3425 | * passed to restore_cmdline_alloc() later. |
| 3426 | * Returns NULL when failed. |
| 3427 | */ |
| 3428 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3429 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3430 | { |
| 3431 | struct cmdline_info *p; |
| 3432 | |
| 3433 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3434 | if (p != NULL) |
| 3435 | save_cmdline(p); |
| 3436 | return (char_u *)p; |
| 3437 | } |
| 3438 | |
| 3439 | /* |
| 3440 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3441 | */ |
| 3442 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3443 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3444 | { |
| 3445 | if (p != NULL) |
| 3446 | { |
| 3447 | restore_cmdline((struct cmdline_info *)p); |
| 3448 | vim_free(p); |
| 3449 | } |
| 3450 | } |
| 3451 | #endif |
| 3452 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3453 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3454 | * Paste a yank register into the command line. |
| 3455 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3456 | * insert_reg() can't be used here, because special characters from the |
| 3457 | * register contents will be interpreted as commands. |
| 3458 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3459 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3460 | */ |
| 3461 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3462 | cmdline_paste( |
| 3463 | int regname, |
| 3464 | int literally, /* Insert text literally instead of "as typed" */ |
| 3465 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3466 | { |
| 3467 | long i; |
| 3468 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3469 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3470 | int allocated; |
| 3471 | struct cmdline_info save_ccline; |
| 3472 | |
| 3473 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3474 | * the command line */ |
| 3475 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 3476 | && regname != Ctrl_A && regname != Ctrl_L |
| 3477 | && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3478 | return FAIL; |
| 3479 | |
| 3480 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3481 | * CTRL-C to break the loop. */ |
| 3482 | line_breakcheck(); |
| 3483 | if (got_int) |
| 3484 | return FAIL; |
| 3485 | |
| 3486 | #ifdef FEAT_CLIPBOARD |
| 3487 | regname = may_get_selection(regname); |
| 3488 | #endif |
| 3489 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3490 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3491 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3492 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3493 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3494 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3495 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3496 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3497 | |
| 3498 | if (i) |
| 3499 | { |
| 3500 | /* Got the value of a special register in "arg". */ |
| 3501 | if (arg == NULL) |
| 3502 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3503 | |
| 3504 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3505 | * part of the word. */ |
| 3506 | p = arg; |
| 3507 | if (p_is && regname == Ctrl_W) |
| 3508 | { |
| 3509 | char_u *w; |
| 3510 | int len; |
| 3511 | |
| 3512 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3513 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3514 | { |
| 3515 | #ifdef FEAT_MBYTE |
| 3516 | if (has_mbyte) |
| 3517 | { |
| 3518 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3519 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3520 | break; |
| 3521 | w -= len; |
| 3522 | } |
| 3523 | else |
| 3524 | #endif |
| 3525 | { |
| 3526 | if (!vim_iswordc(w[-1])) |
| 3527 | break; |
| 3528 | --w; |
| 3529 | } |
| 3530 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3531 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3532 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3533 | p += len; |
| 3534 | } |
| 3535 | |
| 3536 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3537 | if (allocated) |
| 3538 | vim_free(arg); |
| 3539 | return OK; |
| 3540 | } |
| 3541 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3542 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | /* |
| 3546 | * Put a string on the command line. |
| 3547 | * When "literally" is TRUE, insert literally. |
| 3548 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3549 | * line. |
| 3550 | */ |
| 3551 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3552 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3553 | { |
| 3554 | int c, cv; |
| 3555 | |
| 3556 | if (literally) |
| 3557 | put_on_cmdline(s, -1, TRUE); |
| 3558 | else |
| 3559 | while (*s != NUL) |
| 3560 | { |
| 3561 | cv = *s; |
| 3562 | if (cv == Ctrl_V && s[1]) |
| 3563 | ++s; |
| 3564 | #ifdef FEAT_MBYTE |
| 3565 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3566 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3567 | else |
| 3568 | #endif |
| 3569 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3570 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3571 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3572 | #ifdef UNIX |
| 3573 | || c == intr_char |
| 3574 | #endif |
| 3575 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3576 | stuffcharReadbuff(Ctrl_V); |
| 3577 | stuffcharReadbuff(c); |
| 3578 | } |
| 3579 | } |
| 3580 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3581 | #ifdef FEAT_WILDMENU |
| 3582 | /* |
| 3583 | * Delete characters on the command line, from "from" to the current |
| 3584 | * position. |
| 3585 | */ |
| 3586 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3587 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3588 | { |
| 3589 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3590 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3591 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3592 | ccline.cmdpos = from; |
| 3593 | } |
| 3594 | #endif |
| 3595 | |
| 3596 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3597 | * This function is called when the screen size changes and with incremental |
| 3598 | * search and in other situations where the command line may have been |
| 3599 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3600 | */ |
| 3601 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3602 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3603 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3604 | redrawcmdline_ex(TRUE); |
| 3605 | } |
| 3606 | |
| 3607 | void |
| 3608 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3609 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3610 | if (cmd_silent) |
| 3611 | return; |
| 3612 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3613 | if (do_compute_cmdrow) |
| 3614 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3615 | redrawcmd(); |
| 3616 | cursorcmd(); |
| 3617 | } |
| 3618 | |
| 3619 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3620 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3621 | { |
| 3622 | int i; |
| 3623 | |
| 3624 | if (cmd_silent) |
| 3625 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3626 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3627 | msg_putchar(ccline.cmdfirstc); |
| 3628 | if (ccline.cmdprompt != NULL) |
| 3629 | { |
| 3630 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3631 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3632 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3633 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3634 | --ccline.cmdindent; |
| 3635 | } |
| 3636 | else |
| 3637 | for (i = ccline.cmdindent; i > 0; --i) |
| 3638 | msg_putchar(' '); |
| 3639 | } |
| 3640 | |
| 3641 | /* |
| 3642 | * Redraw what is currently on the command line. |
| 3643 | */ |
| 3644 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3645 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3646 | { |
| 3647 | if (cmd_silent) |
| 3648 | return; |
| 3649 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3650 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3651 | if (ccline.cmdbuff == NULL) |
| 3652 | { |
| 3653 | windgoto(cmdline_row, 0); |
| 3654 | msg_clr_eos(); |
| 3655 | return; |
| 3656 | } |
| 3657 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3658 | msg_start(); |
| 3659 | redrawcmdprompt(); |
| 3660 | |
| 3661 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3662 | msg_no_more = TRUE; |
| 3663 | draw_cmdline(0, ccline.cmdlen); |
| 3664 | msg_clr_eos(); |
| 3665 | msg_no_more = FALSE; |
| 3666 | |
| 3667 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3668 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3669 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | |
| 3671 | /* |
| 3672 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3673 | * in cmdline mode we can reset them now. |
| 3674 | */ |
| 3675 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3676 | |
| 3677 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3678 | * in cmdline mode */ |
| 3679 | skip_redraw = FALSE; |
| 3680 | } |
| 3681 | |
| 3682 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3683 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3684 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3685 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3686 | cmdline_row = Rows - 1; |
| 3687 | else |
| 3688 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3689 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3693 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3694 | { |
| 3695 | if (cmd_silent) |
| 3696 | return; |
| 3697 | |
| 3698 | #ifdef FEAT_RIGHTLEFT |
| 3699 | if (cmdmsg_rl) |
| 3700 | { |
| 3701 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3702 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3703 | if (msg_row <= 0) |
| 3704 | msg_row = Rows - 1; |
| 3705 | } |
| 3706 | else |
| 3707 | #endif |
| 3708 | { |
| 3709 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3710 | msg_col = ccline.cmdspos % (int)Columns; |
| 3711 | if (msg_row >= Rows) |
| 3712 | msg_row = Rows - 1; |
| 3713 | } |
| 3714 | |
| 3715 | windgoto(msg_row, msg_col); |
| 3716 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3717 | if (p_imst == IM_ON_THE_SPOT) |
| 3718 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3719 | #endif |
| 3720 | #ifdef MCH_CURSOR_SHAPE |
| 3721 | mch_update_cursor(); |
| 3722 | #endif |
| 3723 | } |
| 3724 | |
| 3725 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3726 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3727 | { |
| 3728 | msg_start(); |
| 3729 | #ifdef FEAT_RIGHTLEFT |
| 3730 | if (cmdmsg_rl) |
| 3731 | msg_col = Columns - 1; |
| 3732 | else |
| 3733 | #endif |
| 3734 | msg_col = 0; /* always start in column 0 */ |
| 3735 | if (clr) /* clear the bottom line(s) */ |
| 3736 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3737 | windgoto(cmdline_row, 0); |
| 3738 | } |
| 3739 | |
| 3740 | /* |
| 3741 | * Check the word in front of the cursor for an abbreviation. |
| 3742 | * Called when the non-id character "c" has been entered. |
| 3743 | * When an abbreviation is recognized it is removed from the text with |
| 3744 | * backspaces and the replacement string is inserted, followed by "c". |
| 3745 | */ |
| 3746 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3747 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3748 | { |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3749 | int spos = 0; |
| 3750 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3752 | return FALSE; |
| 3753 | |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3754 | /* Do not consider '<,'> be part of the mapping, skip leading whitespace. |
| 3755 | * Actually accepts any mark. */ |
| 3756 | while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen) |
| 3757 | spos++; |
| 3758 | if (ccline.cmdlen - spos > 5 |
| 3759 | && ccline.cmdbuff[spos] == '\'' |
| 3760 | && ccline.cmdbuff[spos + 2] == ',' |
| 3761 | && ccline.cmdbuff[spos + 3] == '\'') |
| 3762 | spos += 5; |
| 3763 | else |
| 3764 | /* check abbreviation from the beginning of the commandline */ |
| 3765 | spos = 0; |
| 3766 | |
| 3767 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3770 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3771 | static int |
| 3772 | #ifdef __BORLANDC__ |
| 3773 | _RTLENTRYF |
| 3774 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3775 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3776 | { |
| 3777 | char_u *p1 = *(char_u **)s1; |
| 3778 | char_u *p2 = *(char_u **)s2; |
| 3779 | |
| 3780 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3781 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3782 | return STRCMP(p1, p2); |
| 3783 | } |
| 3784 | #endif |
| 3785 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | /* |
| 3787 | * Return FAIL if this is not an appropriate context in which to do |
| 3788 | * completion of anything, return OK if it is (even if there are no matches). |
| 3789 | * For the caller, this means that the character is just passed through like a |
| 3790 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3791 | */ |
| 3792 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3793 | nextwild( |
| 3794 | expand_T *xp, |
| 3795 | int type, |
| 3796 | int options, /* extra options for ExpandOne() */ |
| 3797 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3798 | { |
| 3799 | int i, j; |
| 3800 | char_u *p1; |
| 3801 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3802 | int difflen; |
| 3803 | int v; |
| 3804 | |
| 3805 | if (xp->xp_numfiles == -1) |
| 3806 | { |
| 3807 | set_expand_context(xp); |
| 3808 | cmd_showtail = expand_showtail(xp); |
| 3809 | } |
| 3810 | |
| 3811 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3812 | { |
| 3813 | beep_flush(); |
| 3814 | return OK; /* Something illegal on command line */ |
| 3815 | } |
| 3816 | if (xp->xp_context == EXPAND_NOTHING) |
| 3817 | { |
| 3818 | /* Caller can use the character as a normal char instead */ |
| 3819 | return FAIL; |
| 3820 | } |
| 3821 | |
| 3822 | MSG_PUTS("..."); /* show that we are busy */ |
| 3823 | out_flush(); |
| 3824 | |
| 3825 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3826 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3827 | |
| 3828 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3829 | { |
| 3830 | /* |
| 3831 | * Get next/previous match for a previous expanded pattern. |
| 3832 | */ |
| 3833 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3834 | } |
| 3835 | else |
| 3836 | { |
| 3837 | /* |
| 3838 | * Translate string into pattern and expand it. |
| 3839 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3840 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3841 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3842 | p2 = NULL; |
| 3843 | else |
| 3844 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3845 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3846 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3847 | if (escape) |
| 3848 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3849 | |
| 3850 | if (p_wic) |
| 3851 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3852 | p2 = ExpandOne(xp, p1, |
| 3853 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3854 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3855 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3856 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | if (p2 != NULL && type == WILD_LONGEST) |
| 3858 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3859 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | if (ccline.cmdbuff[i + j] == '*' |
| 3861 | || ccline.cmdbuff[i + j] == '?') |
| 3862 | break; |
| 3863 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3864 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | if (p2 != NULL && !got_int) |
| 3870 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3871 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3872 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3873 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3874 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3875 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3876 | } |
| 3877 | else |
| 3878 | v = OK; |
| 3879 | if (v == OK) |
| 3880 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3881 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3882 | &ccline.cmdbuff[ccline.cmdpos], |
| 3883 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3884 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3885 | ccline.cmdlen += difflen; |
| 3886 | ccline.cmdpos += difflen; |
| 3887 | } |
| 3888 | } |
| 3889 | vim_free(p2); |
| 3890 | |
| 3891 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3892 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3893 | |
| 3894 | /* When expanding a ":map" command and no matches are found, assume that |
| 3895 | * the key is supposed to be inserted literally */ |
| 3896 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3897 | return FAIL; |
| 3898 | |
| 3899 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3900 | beep_flush(); |
| 3901 | else if (xp->xp_numfiles == 1) |
| 3902 | /* free expanded pattern */ |
| 3903 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3904 | |
| 3905 | return OK; |
| 3906 | } |
| 3907 | |
| 3908 | /* |
| 3909 | * Do wildcard expansion on the string 'str'. |
| 3910 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3911 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3912 | * Return NULL for failure. |
| 3913 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3914 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3915 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3916 | * WILD_PREV "orig" should be NULL. |
| 3917 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3918 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3919 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3920 | * |
| 3921 | * mode = WILD_FREE: just free previously expanded matches |
| 3922 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3923 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3924 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3925 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3926 | * mode = WILD_ALL: return all matches concatenated |
| 3927 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3928 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | * |
| 3930 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3931 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3932 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3933 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3934 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3935 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3936 | * options = WILD_SILENT: don't print warning messages |
| 3937 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3938 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3939 | * |
| 3940 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3941 | */ |
| 3942 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3943 | ExpandOne( |
| 3944 | expand_T *xp, |
| 3945 | char_u *str, |
| 3946 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3947 | int options, |
| 3948 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | { |
| 3950 | char_u *ss = NULL; |
| 3951 | static int findex; |
| 3952 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3953 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3954 | int i; |
| 3955 | long_u len; |
| 3956 | int non_suf_match; /* number without matching suffix */ |
| 3957 | |
| 3958 | /* |
| 3959 | * first handle the case of using an old match |
| 3960 | */ |
| 3961 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3962 | { |
| 3963 | if (xp->xp_numfiles > 0) |
| 3964 | { |
| 3965 | if (mode == WILD_PREV) |
| 3966 | { |
| 3967 | if (findex == -1) |
| 3968 | findex = xp->xp_numfiles; |
| 3969 | --findex; |
| 3970 | } |
| 3971 | else /* mode == WILD_NEXT */ |
| 3972 | ++findex; |
| 3973 | |
| 3974 | /* |
| 3975 | * When wrapping around, return the original string, set findex to |
| 3976 | * -1. |
| 3977 | */ |
| 3978 | if (findex < 0) |
| 3979 | { |
| 3980 | if (orig_save == NULL) |
| 3981 | findex = xp->xp_numfiles - 1; |
| 3982 | else |
| 3983 | findex = -1; |
| 3984 | } |
| 3985 | if (findex >= xp->xp_numfiles) |
| 3986 | { |
| 3987 | if (orig_save == NULL) |
| 3988 | findex = 0; |
| 3989 | else |
| 3990 | findex = -1; |
| 3991 | } |
| 3992 | #ifdef FEAT_WILDMENU |
| 3993 | if (p_wmnu) |
| 3994 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3995 | findex, cmd_showtail); |
| 3996 | #endif |
| 3997 | if (findex == -1) |
| 3998 | return vim_strsave(orig_save); |
| 3999 | return vim_strsave(xp->xp_files[findex]); |
| 4000 | } |
| 4001 | else |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4005 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4006 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 4007 | { |
| 4008 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4009 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4010 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4011 | } |
| 4012 | findex = 0; |
| 4013 | |
| 4014 | if (mode == WILD_FREE) /* only release file name */ |
| 4015 | return NULL; |
| 4016 | |
| 4017 | if (xp->xp_numfiles == -1) |
| 4018 | { |
| 4019 | vim_free(orig_save); |
| 4020 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4021 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* |
| 4024 | * Do the expansion. |
| 4025 | */ |
| 4026 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 4027 | options) == FAIL) |
| 4028 | { |
| 4029 | #ifdef FNAME_ILLEGAL |
| 4030 | /* Illegal file name has been silently skipped. But when there |
| 4031 | * are wildcards, the real problem is that there was no match, |
| 4032 | * causing the pattern to be added, which has illegal characters. |
| 4033 | */ |
| 4034 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 4035 | EMSG2(_(e_nomatch2), str); |
| 4036 | #endif |
| 4037 | } |
| 4038 | else if (xp->xp_numfiles == 0) |
| 4039 | { |
| 4040 | if (!(options & WILD_SILENT)) |
| 4041 | EMSG2(_(e_nomatch2), str); |
| 4042 | } |
| 4043 | else |
| 4044 | { |
| 4045 | /* Escape the matches for use on the command line. */ |
| 4046 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 4047 | |
| 4048 | /* |
| 4049 | * Check for matching suffixes in file names. |
| 4050 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4051 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 4052 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4053 | { |
| 4054 | if (xp->xp_numfiles) |
| 4055 | non_suf_match = xp->xp_numfiles; |
| 4056 | else |
| 4057 | non_suf_match = 1; |
| 4058 | if ((xp->xp_context == EXPAND_FILES |
| 4059 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4060 | && xp->xp_numfiles > 1) |
| 4061 | { |
| 4062 | /* |
| 4063 | * More than one match; check suffix. |
| 4064 | * The files will have been sorted on matching suffix in |
| 4065 | * expand_wildcards, only need to check the first two. |
| 4066 | */ |
| 4067 | non_suf_match = 0; |
| 4068 | for (i = 0; i < 2; ++i) |
| 4069 | if (match_suffix(xp->xp_files[i])) |
| 4070 | ++non_suf_match; |
| 4071 | } |
| 4072 | if (non_suf_match != 1) |
| 4073 | { |
| 4074 | /* Can we ever get here unless it's while expanding |
| 4075 | * interactively? If not, we can get rid of this all |
| 4076 | * together. Don't really want to wait for this message |
| 4077 | * (and possibly have to hit return to continue!). |
| 4078 | */ |
| 4079 | if (!(options & WILD_SILENT)) |
| 4080 | EMSG(_(e_toomany)); |
| 4081 | else if (!(options & WILD_NO_BEEP)) |
| 4082 | beep_flush(); |
| 4083 | } |
| 4084 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 4085 | ss = vim_strsave(xp->xp_files[0]); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | /* Find longest common part */ |
| 4091 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 4092 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4093 | int mb_len = 1; |
| 4094 | int c0, ci; |
| 4095 | |
| 4096 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4097 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4098 | #ifdef FEAT_MBYTE |
| 4099 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4100 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4101 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 4102 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 4103 | } |
| 4104 | else |
| 4105 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 4106 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4107 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 4108 | { |
| 4109 | #ifdef FEAT_MBYTE |
| 4110 | if (has_mbyte) |
| 4111 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 4112 | else |
| 4113 | #endif |
| 4114 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4115 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4117 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4118 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4119 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4120 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4121 | break; |
| 4122 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4123 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4124 | break; |
| 4125 | } |
| 4126 | if (i < xp->xp_numfiles) |
| 4127 | { |
| 4128 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 4129 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4130 | break; |
| 4131 | } |
| 4132 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4133 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4134 | ss = alloc((unsigned)len + 1); |
| 4135 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4136 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4137 | findex = -1; /* next p_wc gets first one */ |
| 4138 | } |
| 4139 | |
| 4140 | /* Concatenate all matching names */ |
| 4141 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 4142 | { |
| 4143 | len = 0; |
| 4144 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4145 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 4146 | ss = lalloc(len, TRUE); |
| 4147 | if (ss != NULL) |
| 4148 | { |
| 4149 | *ss = NUL; |
| 4150 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4151 | { |
| 4152 | STRCAT(ss, xp->xp_files[i]); |
| 4153 | if (i != xp->xp_numfiles - 1) |
| 4154 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 4160 | ExpandCleanup(xp); |
| 4161 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4162 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4163 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4164 | vim_free(orig); |
| 4165 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4166 | return ss; |
| 4167 | } |
| 4168 | |
| 4169 | /* |
| 4170 | * Prepare an expand structure for use. |
| 4171 | */ |
| 4172 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4173 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 4175 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4176 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4177 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4178 | #ifndef BACKSLASH_IN_FILENAME |
| 4179 | xp->xp_shell = FALSE; |
| 4180 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4181 | xp->xp_numfiles = -1; |
| 4182 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4183 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4184 | xp->xp_arg = NULL; |
| 4185 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4186 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | /* |
| 4190 | * Cleanup an expand structure after use. |
| 4191 | */ |
| 4192 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4193 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4194 | { |
| 4195 | if (xp->xp_numfiles >= 0) |
| 4196 | { |
| 4197 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4198 | xp->xp_numfiles = -1; |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4203 | ExpandEscape( |
| 4204 | expand_T *xp, |
| 4205 | char_u *str, |
| 4206 | int numfiles, |
| 4207 | char_u **files, |
| 4208 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4209 | { |
| 4210 | int i; |
| 4211 | char_u *p; |
| 4212 | |
| 4213 | /* |
| 4214 | * May change home directory back to "~" |
| 4215 | */ |
| 4216 | if (options & WILD_HOME_REPLACE) |
| 4217 | tilde_replace(str, numfiles, files); |
| 4218 | |
| 4219 | if (options & WILD_ESCAPE) |
| 4220 | { |
| 4221 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4222 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4223 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4224 | || xp->xp_context == EXPAND_BUFFERS |
| 4225 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4226 | { |
| 4227 | /* |
| 4228 | * Insert a backslash into a file name before a space, \, %, # |
| 4229 | * and wildmatch characters, except '~'. |
| 4230 | */ |
| 4231 | for (i = 0; i < numfiles; ++i) |
| 4232 | { |
| 4233 | /* for ":set path=" we need to escape spaces twice */ |
| 4234 | if (xp->xp_backslash == XP_BS_THREE) |
| 4235 | { |
| 4236 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4237 | if (p != NULL) |
| 4238 | { |
| 4239 | vim_free(files[i]); |
| 4240 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4241 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4242 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4243 | if (p != NULL) |
| 4244 | { |
| 4245 | vim_free(files[i]); |
| 4246 | files[i] = p; |
| 4247 | } |
| 4248 | #endif |
| 4249 | } |
| 4250 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4251 | #ifdef BACKSLASH_IN_FILENAME |
| 4252 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4253 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4254 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4255 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | if (p != NULL) |
| 4257 | { |
| 4258 | vim_free(files[i]); |
| 4259 | files[i] = p; |
| 4260 | } |
| 4261 | |
| 4262 | /* If 'str' starts with "\~", replace "~" at start of |
| 4263 | * files[i] with "\~". */ |
| 4264 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4265 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4266 | } |
| 4267 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4268 | |
| 4269 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4270 | * could be seen as "+cmd". */ |
| 4271 | if (*files[0] == '+') |
| 4272 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4273 | } |
| 4274 | else if (xp->xp_context == EXPAND_TAGS) |
| 4275 | { |
| 4276 | /* |
| 4277 | * Insert a backslash before characters in a tag name that |
| 4278 | * would terminate the ":tag" command. |
| 4279 | */ |
| 4280 | for (i = 0; i < numfiles; ++i) |
| 4281 | { |
| 4282 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4283 | if (p != NULL) |
| 4284 | { |
| 4285 | vim_free(files[i]); |
| 4286 | files[i] = p; |
| 4287 | } |
| 4288 | } |
| 4289 | } |
| 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4294 | * Escape special characters in "fname" for when used as a file name argument |
| 4295 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4296 | * Returns the result in allocated memory. |
| 4297 | */ |
| 4298 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4299 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4300 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4301 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4302 | #ifdef BACKSLASH_IN_FILENAME |
| 4303 | char_u buf[20]; |
| 4304 | int j = 0; |
| 4305 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4306 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4307 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4308 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4309 | buf[j++] = *p; |
| 4310 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4311 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4312 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4313 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4314 | if (shell && csh_like_shell() && p != NULL) |
| 4315 | { |
| 4316 | char_u *s; |
| 4317 | |
| 4318 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4319 | * One is taken by Vim, one by the shell. */ |
| 4320 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4321 | vim_free(p); |
| 4322 | p = s; |
| 4323 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4324 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4327 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4328 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4329 | escape_fname(&p); |
| 4330 | |
| 4331 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4335 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4336 | */ |
| 4337 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4338 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4339 | { |
| 4340 | char_u *p; |
| 4341 | |
| 4342 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4343 | if (p != NULL) |
| 4344 | { |
| 4345 | p[0] = '\\'; |
| 4346 | STRCPY(p + 1, *pp); |
| 4347 | vim_free(*pp); |
| 4348 | *pp = p; |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4353 | * For each file name in files[num_files]: |
| 4354 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4355 | */ |
| 4356 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4357 | tilde_replace( |
| 4358 | char_u *orig_pat, |
| 4359 | int num_files, |
| 4360 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4361 | { |
| 4362 | int i; |
| 4363 | char_u *p; |
| 4364 | |
| 4365 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4366 | { |
| 4367 | for (i = 0; i < num_files; ++i) |
| 4368 | { |
| 4369 | p = home_replace_save(NULL, files[i]); |
| 4370 | if (p != NULL) |
| 4371 | { |
| 4372 | vim_free(files[i]); |
| 4373 | files[i] = p; |
| 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | /* |
| 4380 | * Show all matches for completion on the command line. |
| 4381 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4382 | * be inserted like a normal character. |
| 4383 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4384 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4385 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4386 | { |
| 4387 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4388 | int num_files; |
| 4389 | char_u **files_found; |
| 4390 | int i, j, k; |
| 4391 | int maxlen; |
| 4392 | int lines; |
| 4393 | int columns; |
| 4394 | char_u *p; |
| 4395 | int lastlen; |
| 4396 | int attr; |
| 4397 | int showtail; |
| 4398 | |
| 4399 | if (xp->xp_numfiles == -1) |
| 4400 | { |
| 4401 | set_expand_context(xp); |
| 4402 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4403 | &num_files, &files_found); |
| 4404 | showtail = expand_showtail(xp); |
| 4405 | if (i != EXPAND_OK) |
| 4406 | return i; |
| 4407 | |
| 4408 | } |
| 4409 | else |
| 4410 | { |
| 4411 | num_files = xp->xp_numfiles; |
| 4412 | files_found = xp->xp_files; |
| 4413 | showtail = cmd_showtail; |
| 4414 | } |
| 4415 | |
| 4416 | #ifdef FEAT_WILDMENU |
| 4417 | if (!wildmenu) |
| 4418 | { |
| 4419 | #endif |
| 4420 | msg_didany = FALSE; /* lines_left will be set */ |
| 4421 | msg_start(); /* prepare for paging */ |
| 4422 | msg_putchar('\n'); |
| 4423 | out_flush(); |
| 4424 | cmdline_row = msg_row; |
| 4425 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4426 | msg_start(); /* prepare for paging */ |
| 4427 | #ifdef FEAT_WILDMENU |
| 4428 | } |
| 4429 | #endif |
| 4430 | |
| 4431 | if (got_int) |
| 4432 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4433 | #ifdef FEAT_WILDMENU |
| 4434 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4435 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4436 | #endif |
| 4437 | else |
| 4438 | { |
| 4439 | /* find the length of the longest file name */ |
| 4440 | maxlen = 0; |
| 4441 | for (i = 0; i < num_files; ++i) |
| 4442 | { |
| 4443 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4444 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4445 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4446 | { |
| 4447 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4448 | j = vim_strsize(NameBuff); |
| 4449 | } |
| 4450 | else |
| 4451 | j = vim_strsize(L_SHOWFILE(i)); |
| 4452 | if (j > maxlen) |
| 4453 | maxlen = j; |
| 4454 | } |
| 4455 | |
| 4456 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4457 | lines = num_files; |
| 4458 | else |
| 4459 | { |
| 4460 | /* compute the number of columns and lines for the listing */ |
| 4461 | maxlen += 2; /* two spaces between file names */ |
| 4462 | columns = ((int)Columns + 2) / maxlen; |
| 4463 | if (columns < 1) |
| 4464 | columns = 1; |
| 4465 | lines = (num_files + columns - 1) / columns; |
| 4466 | } |
| 4467 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4468 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4469 | |
| 4470 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4471 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4472 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4473 | msg_clr_eos(); |
| 4474 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4475 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | /* list the files line by line */ |
| 4479 | for (i = 0; i < lines; ++i) |
| 4480 | { |
| 4481 | lastlen = 999; |
| 4482 | for (k = i; k < num_files; k += lines) |
| 4483 | { |
| 4484 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4485 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4486 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4487 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4488 | msg_advance(maxlen + 1); |
| 4489 | msg_puts(p); |
| 4490 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4491 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4492 | break; |
| 4493 | } |
| 4494 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4495 | msg_putchar(' '); |
| 4496 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4497 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4498 | || xp->xp_context == EXPAND_BUFFERS) |
| 4499 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4500 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4501 | if (xp->xp_numfiles != -1) |
| 4502 | { |
| 4503 | char_u *halved_slash; |
| 4504 | char_u *exp_path; |
| 4505 | |
| 4506 | /* Expansion was done before and special characters |
| 4507 | * were escaped, need to halve backslashes. Also |
| 4508 | * $HOME has been replaced with ~/. */ |
| 4509 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4510 | halved_slash = backslash_halve_save( |
| 4511 | exp_path != NULL ? exp_path : files_found[k]); |
| 4512 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4513 | : files_found[k]); |
| 4514 | vim_free(exp_path); |
| 4515 | vim_free(halved_slash); |
| 4516 | } |
| 4517 | else |
| 4518 | /* Expansion was done here, file names are literal. */ |
| 4519 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4520 | if (showtail) |
| 4521 | p = L_SHOWFILE(k); |
| 4522 | else |
| 4523 | { |
| 4524 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4525 | TRUE); |
| 4526 | p = NameBuff; |
| 4527 | } |
| 4528 | } |
| 4529 | else |
| 4530 | { |
| 4531 | j = FALSE; |
| 4532 | p = L_SHOWFILE(k); |
| 4533 | } |
| 4534 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4535 | } |
| 4536 | if (msg_col > 0) /* when not wrapped around */ |
| 4537 | { |
| 4538 | msg_clr_eos(); |
| 4539 | msg_putchar('\n'); |
| 4540 | } |
| 4541 | out_flush(); /* show one line at a time */ |
| 4542 | if (got_int) |
| 4543 | { |
| 4544 | got_int = FALSE; |
| 4545 | break; |
| 4546 | } |
| 4547 | } |
| 4548 | |
| 4549 | /* |
| 4550 | * we redraw the command below the lines that we have just listed |
| 4551 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4552 | */ |
| 4553 | cmdline_row = msg_row; /* will put it back later */ |
| 4554 | } |
| 4555 | |
| 4556 | if (xp->xp_numfiles == -1) |
| 4557 | FreeWild(num_files, files_found); |
| 4558 | |
| 4559 | return EXPAND_OK; |
| 4560 | } |
| 4561 | |
| 4562 | /* |
| 4563 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4564 | * Find tail of file name path, but ignore trailing "/". |
| 4565 | */ |
| 4566 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4567 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | { |
| 4569 | char_u *p; |
| 4570 | char_u *t = s; |
| 4571 | int had_sep = FALSE; |
| 4572 | |
| 4573 | for (p = s; *p != NUL; ) |
| 4574 | { |
| 4575 | if (vim_ispathsep(*p) |
| 4576 | #ifdef BACKSLASH_IN_FILENAME |
| 4577 | && !rem_backslash(p) |
| 4578 | #endif |
| 4579 | ) |
| 4580 | had_sep = TRUE; |
| 4581 | else if (had_sep) |
| 4582 | { |
| 4583 | t = p; |
| 4584 | had_sep = FALSE; |
| 4585 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4586 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4587 | } |
| 4588 | return t; |
| 4589 | } |
| 4590 | |
| 4591 | /* |
| 4592 | * Return TRUE if we only need to show the tail of completion matches. |
| 4593 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4594 | * returned. |
| 4595 | */ |
| 4596 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4597 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4598 | { |
| 4599 | char_u *s; |
| 4600 | char_u *end; |
| 4601 | |
| 4602 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4603 | if (xp->xp_context != EXPAND_FILES |
| 4604 | && xp->xp_context != EXPAND_SHELLCMD |
| 4605 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4606 | return FALSE; |
| 4607 | |
| 4608 | end = gettail(xp->xp_pattern); |
| 4609 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4610 | return FALSE; |
| 4611 | |
| 4612 | for (s = xp->xp_pattern; s < end; s++) |
| 4613 | { |
| 4614 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4615 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4616 | if (rem_backslash(s)) |
| 4617 | ++s; |
| 4618 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4619 | return FALSE; |
| 4620 | } |
| 4621 | return TRUE; |
| 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Prepare a string for expansion. |
| 4626 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4627 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4628 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4629 | * the name into allocated memory and prepend "^". |
| 4630 | */ |
| 4631 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4632 | addstar( |
| 4633 | char_u *fname, |
| 4634 | int len, |
| 4635 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4636 | { |
| 4637 | char_u *retval; |
| 4638 | int i, j; |
| 4639 | int new_len; |
| 4640 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4641 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4642 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4643 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4644 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4645 | && context != EXPAND_SHELLCMD |
| 4646 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4647 | { |
| 4648 | /* |
| 4649 | * Matching will be done internally (on something other than files). |
| 4650 | * So we convert the file-matching-type wildcards into our kind for |
| 4651 | * use with vim_regcomp(). First work out how long it will be: |
| 4652 | */ |
| 4653 | |
| 4654 | /* For help tags the translation is done in find_help_tags(). |
| 4655 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4656 | if (context == EXPAND_HELP |
| 4657 | || context == EXPAND_COLORS |
| 4658 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4659 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4660 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4661 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4662 | || ((context == EXPAND_TAGS_LISTFILES |
| 4663 | || context == EXPAND_TAGS) |
| 4664 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4665 | retval = vim_strnsave(fname, len); |
| 4666 | else |
| 4667 | { |
| 4668 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4669 | for (i = 0; i < len; i++) |
| 4670 | { |
| 4671 | if (fname[i] == '*' || fname[i] == '~') |
| 4672 | new_len++; /* '*' needs to be replaced by ".*" |
| 4673 | '~' needs to be replaced by "\~" */ |
| 4674 | |
| 4675 | /* Buffer names are like file names. "." should be literal */ |
| 4676 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4677 | new_len++; /* "." becomes "\." */ |
| 4678 | |
| 4679 | /* Custom expansion takes care of special things, match |
| 4680 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4681 | if ((context == EXPAND_USER_DEFINED |
| 4682 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4683 | new_len++; /* '\' becomes "\\" */ |
| 4684 | } |
| 4685 | retval = alloc(new_len); |
| 4686 | if (retval != NULL) |
| 4687 | { |
| 4688 | retval[0] = '^'; |
| 4689 | j = 1; |
| 4690 | for (i = 0; i < len; i++, j++) |
| 4691 | { |
| 4692 | /* Skip backslash. But why? At least keep it for custom |
| 4693 | * expansion. */ |
| 4694 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4695 | && context != EXPAND_USER_LIST |
| 4696 | && fname[i] == '\\' |
| 4697 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4698 | break; |
| 4699 | |
| 4700 | switch (fname[i]) |
| 4701 | { |
| 4702 | case '*': retval[j++] = '.'; |
| 4703 | break; |
| 4704 | case '~': retval[j++] = '\\'; |
| 4705 | break; |
| 4706 | case '?': retval[j] = '.'; |
| 4707 | continue; |
| 4708 | case '.': if (context == EXPAND_BUFFERS) |
| 4709 | retval[j++] = '\\'; |
| 4710 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4711 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4712 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4713 | retval[j++] = '\\'; |
| 4714 | break; |
| 4715 | } |
| 4716 | retval[j] = fname[i]; |
| 4717 | } |
| 4718 | retval[j] = NUL; |
| 4719 | } |
| 4720 | } |
| 4721 | } |
| 4722 | else |
| 4723 | { |
| 4724 | retval = alloc(len + 4); |
| 4725 | if (retval != NULL) |
| 4726 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4727 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4728 | |
| 4729 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4730 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4731 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4732 | * ~ would be at the start of the file name, but not the tail. |
| 4733 | * $ could be anywhere in the tail. |
| 4734 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4735 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4736 | */ |
| 4737 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4738 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4739 | #ifndef BACKSLASH_IN_FILENAME |
| 4740 | for (i = len - 2; i >= 0; --i) |
| 4741 | { |
| 4742 | if (retval[i] != '\\') |
| 4743 | break; |
| 4744 | ends_in_star = !ends_in_star; |
| 4745 | } |
| 4746 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4747 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4748 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | && vim_strchr(tail, '$') == NULL |
| 4750 | && vim_strchr(retval, '`') == NULL) |
| 4751 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4752 | else if (len > 0 && retval[len - 1] == '$') |
| 4753 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | retval[len] = NUL; |
| 4755 | } |
| 4756 | } |
| 4757 | return retval; |
| 4758 | } |
| 4759 | |
| 4760 | /* |
| 4761 | * Must parse the command line so far to work out what context we are in. |
| 4762 | * Completion can then be done based on that context. |
| 4763 | * This routine sets the variables: |
| 4764 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4765 | * the command line (ends at the cursor). |
| 4766 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4767 | * |
| 4768 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4769 | * the command line, like an unknown command. Caller |
| 4770 | * should beep. |
| 4771 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4772 | * a normal char, rather than for completion. eg |
| 4773 | * :s/^I/ |
| 4774 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4775 | * it. |
| 4776 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4777 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4778 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4779 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4780 | * when we know only directories are of interest. eg |
| 4781 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4782 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4783 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4784 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4785 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4786 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4787 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4788 | * EXPAND_EVENTS Complete event names |
| 4789 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4790 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4791 | * EXPAND_AUGROUP Complete autocommand group names |
| 4792 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4793 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4794 | * eg :unmap a^I , :cunab x^I |
| 4795 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4796 | * eg :call sub^I |
| 4797 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4798 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4799 | * names in expressions, eg :while s^I |
| 4800 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4801 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4802 | */ |
| 4803 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4804 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4806 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4807 | if (ccline.cmdfirstc != ':' |
| 4808 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4809 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4810 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | #endif |
| 4812 | ) |
| 4813 | { |
| 4814 | xp->xp_context = EXPAND_NOTHING; |
| 4815 | return; |
| 4816 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4817 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4821 | set_cmd_context( |
| 4822 | expand_T *xp, |
| 4823 | char_u *str, /* start of command line */ |
| 4824 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4825 | int col, /* position of cursor */ |
| 4826 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4827 | { |
| 4828 | int old_char = NUL; |
| 4829 | char_u *nextcomm; |
| 4830 | |
| 4831 | /* |
| 4832 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4833 | * written before. |
| 4834 | */ |
| 4835 | if (col < len) |
| 4836 | old_char = str[col]; |
| 4837 | str[col] = NUL; |
| 4838 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4839 | |
| 4840 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4841 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4842 | { |
| 4843 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4844 | /* pass CMD_SIZE because there is no real command */ |
| 4845 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4846 | # endif |
| 4847 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4848 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4849 | { |
| 4850 | xp->xp_context = ccline.xp_context; |
| 4851 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4852 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4853 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4854 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4855 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4856 | else |
| 4857 | #endif |
| 4858 | while (nextcomm != NULL) |
| 4859 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4860 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4861 | /* Store the string here so that call_user_expand_func() can get to them |
| 4862 | * easily. */ |
| 4863 | xp->xp_line = str; |
| 4864 | xp->xp_col = col; |
| 4865 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | str[col] = old_char; |
| 4867 | } |
| 4868 | |
| 4869 | /* |
| 4870 | * Expand the command line "str" from context "xp". |
| 4871 | * "xp" must have been set by set_cmd_context(). |
| 4872 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4873 | * starts. |
| 4874 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4875 | * cursor. |
| 4876 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4877 | * key that triggered expansion literally. |
| 4878 | * Returns EXPAND_OK otherwise. |
| 4879 | */ |
| 4880 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4881 | expand_cmdline( |
| 4882 | expand_T *xp, |
| 4883 | char_u *str, /* start of command line */ |
| 4884 | int col, /* position of cursor */ |
| 4885 | int *matchcount, /* return: nr of matches */ |
| 4886 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4887 | { |
| 4888 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4889 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4890 | |
| 4891 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4892 | { |
| 4893 | beep_flush(); |
| 4894 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4895 | } |
| 4896 | if (xp->xp_context == EXPAND_NOTHING) |
| 4897 | { |
| 4898 | /* Caller can use the character as a normal char instead */ |
| 4899 | return EXPAND_NOTHING; |
| 4900 | } |
| 4901 | |
| 4902 | /* add star to file name, or convert to regexp if not exp. files. */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4903 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4904 | file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | if (file_str == NULL) |
| 4906 | return EXPAND_UNSUCCESSFUL; |
| 4907 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4908 | if (p_wic) |
| 4909 | options += WILD_ICASE; |
| 4910 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4911 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4912 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4913 | { |
| 4914 | *matchcount = 0; |
| 4915 | *matches = NULL; |
| 4916 | } |
| 4917 | vim_free(file_str); |
| 4918 | |
| 4919 | return EXPAND_OK; |
| 4920 | } |
| 4921 | |
| 4922 | #ifdef FEAT_MULTI_LANG |
| 4923 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4924 | * Cleanup matches for help tags: |
| 4925 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4926 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4927 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4928 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4929 | |
| 4930 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4931 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4932 | { |
| 4933 | int i, j; |
| 4934 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4935 | char_u buf[4]; |
| 4936 | char_u *p = buf; |
| 4937 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4938 | if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n')) |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4939 | { |
| 4940 | *p++ = '@'; |
| 4941 | *p++ = p_hlg[0]; |
| 4942 | *p++ = p_hlg[1]; |
| 4943 | } |
| 4944 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4945 | |
| 4946 | for (i = 0; i < num_file; ++i) |
| 4947 | { |
| 4948 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4949 | if (len <= 0) |
| 4950 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4951 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4952 | { |
| 4953 | /* Sorting on priority means the same item in another language may |
| 4954 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4955 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4956 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4957 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | break; |
| 4959 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4960 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | file[i][len] = NUL; |
| 4962 | } |
| 4963 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4964 | |
| 4965 | if (*buf != NUL) |
| 4966 | for (i = 0; i < num_file; ++i) |
| 4967 | { |
| 4968 | len = (int)STRLEN(file[i]) - 3; |
| 4969 | if (len <= 0) |
| 4970 | continue; |
| 4971 | if (STRCMP(file[i] + len, buf) == 0) |
| 4972 | { |
| 4973 | /* remove the default language */ |
| 4974 | file[i][len] = NUL; |
| 4975 | } |
| 4976 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4977 | } |
| 4978 | #endif |
| 4979 | |
| 4980 | /* |
| 4981 | * Do the expansion based on xp->xp_context and "pat". |
| 4982 | */ |
| 4983 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4984 | ExpandFromContext( |
| 4985 | expand_T *xp, |
| 4986 | char_u *pat, |
| 4987 | int *num_file, |
| 4988 | char_u ***file, |
| 4989 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4990 | { |
| 4991 | #ifdef FEAT_CMDL_COMPL |
| 4992 | regmatch_T regmatch; |
| 4993 | #endif |
| 4994 | int ret; |
| 4995 | int flags; |
| 4996 | |
| 4997 | flags = EW_DIR; /* include directories */ |
| 4998 | if (options & WILD_LIST_NOTFOUND) |
| 4999 | flags |= EW_NOTFOUND; |
| 5000 | if (options & WILD_ADD_SLASH) |
| 5001 | flags |= EW_ADDSLASH; |
| 5002 | if (options & WILD_KEEP_ALL) |
| 5003 | flags |= EW_KEEPALL; |
| 5004 | if (options & WILD_SILENT) |
| 5005 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 5006 | if (options & WILD_ALLLINKS) |
| 5007 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5009 | if (xp->xp_context == EXPAND_FILES |
| 5010 | || xp->xp_context == EXPAND_DIRECTORIES |
| 5011 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | { |
| 5013 | /* |
| 5014 | * Expand file or directory names. |
| 5015 | */ |
| 5016 | int free_pat = FALSE; |
| 5017 | int i; |
| 5018 | |
| 5019 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5020 | * space */ |
| 5021 | if (xp->xp_backslash != XP_BS_NONE) |
| 5022 | { |
| 5023 | free_pat = TRUE; |
| 5024 | pat = vim_strsave(pat); |
| 5025 | for (i = 0; pat[i]; ++i) |
| 5026 | if (pat[i] == '\\') |
| 5027 | { |
| 5028 | if (xp->xp_backslash == XP_BS_THREE |
| 5029 | && pat[i + 1] == '\\' |
| 5030 | && pat[i + 2] == '\\' |
| 5031 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5032 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5033 | if (xp->xp_backslash == XP_BS_ONE |
| 5034 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5035 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | if (xp->xp_context == EXPAND_FILES) |
| 5040 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5041 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 5042 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5043 | else |
| 5044 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5045 | if (options & WILD_ICASE) |
| 5046 | flags |= EW_ICASE; |
| 5047 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5048 | /* Expand wildcards, supporting %:h and the like. */ |
| 5049 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | if (free_pat) |
| 5051 | vim_free(pat); |
| 5052 | return ret; |
| 5053 | } |
| 5054 | |
| 5055 | *file = (char_u **)""; |
| 5056 | *num_file = 0; |
| 5057 | if (xp->xp_context == EXPAND_HELP) |
| 5058 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 5059 | /* With an empty argument we would get all the help tags, which is |
| 5060 | * very slow. Get matches for "help" instead. */ |
| 5061 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 5062 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5063 | { |
| 5064 | #ifdef FEAT_MULTI_LANG |
| 5065 | cleanup_help_tags(*num_file, *file); |
| 5066 | #endif |
| 5067 | return OK; |
| 5068 | } |
| 5069 | return FAIL; |
| 5070 | } |
| 5071 | |
| 5072 | #ifndef FEAT_CMDL_COMPL |
| 5073 | return FAIL; |
| 5074 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5075 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 5076 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 5078 | return ExpandOldSetting(num_file, file); |
| 5079 | if (xp->xp_context == EXPAND_BUFFERS) |
| 5080 | return ExpandBufnames(pat, num_file, file, options); |
| 5081 | if (xp->xp_context == EXPAND_TAGS |
| 5082 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 5083 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 5084 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5085 | { |
| 5086 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5087 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 5088 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5089 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5091 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 5092 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5093 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5094 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5095 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5096 | { |
| 5097 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5098 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5099 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5100 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5101 | { |
| 5102 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5103 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5104 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5105 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5106 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5107 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5108 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5109 | if (xp->xp_context == EXPAND_PACKADD) |
| 5110 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5111 | |
| 5112 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5113 | if (regmatch.regprog == NULL) |
| 5114 | return FAIL; |
| 5115 | |
| 5116 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 5117 | regmatch.rm_ic = ignorecase(pat); |
| 5118 | |
| 5119 | if (xp->xp_context == EXPAND_SETTINGS |
| 5120 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 5121 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 5122 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 5123 | ret = ExpandMappings(®match, num_file, file); |
| 5124 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5125 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 5126 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 5127 | # endif |
| 5128 | else |
| 5129 | { |
| 5130 | static struct expgen |
| 5131 | { |
| 5132 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 5133 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5134 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5135 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5136 | } tab[] = |
| 5137 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5138 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 5139 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 5140 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 5141 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5142 | #ifdef FEAT_CMDHIST |
| 5143 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 5144 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5145 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5146 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 5147 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5148 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 5149 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 5150 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5151 | #endif |
| 5152 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5153 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 5154 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 5155 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 5156 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5157 | #endif |
| 5158 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5159 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 5160 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5161 | #endif |
| 5162 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5163 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5164 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 5165 | #ifdef FEAT_PROFILE |
| 5166 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 5167 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5168 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5169 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 5170 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5171 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5172 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5173 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5174 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5175 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5176 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5177 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5178 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5179 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5180 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5181 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5182 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5183 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5184 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5185 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5186 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5187 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5188 | }; |
| 5189 | int i; |
| 5190 | |
| 5191 | /* |
| 5192 | * Find a context in the table and call the ExpandGeneric() with the |
| 5193 | * right function to do the expansion. |
| 5194 | */ |
| 5195 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5196 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5197 | if (xp->xp_context == tab[i].context) |
| 5198 | { |
| 5199 | if (tab[i].ic) |
| 5200 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5201 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5202 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | break; |
| 5204 | } |
| 5205 | } |
| 5206 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5207 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | |
| 5209 | return ret; |
| 5210 | #endif /* FEAT_CMDL_COMPL */ |
| 5211 | } |
| 5212 | |
| 5213 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5214 | /* |
| 5215 | * Expand a list of names. |
| 5216 | * |
| 5217 | * Generic function for command line completion. It calls a function to |
| 5218 | * obtain strings, one by one. The strings are matched against a regexp |
| 5219 | * program. Matching strings are copied into an array, which is returned. |
| 5220 | * |
| 5221 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5222 | */ |
| 5223 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5224 | ExpandGeneric( |
| 5225 | expand_T *xp, |
| 5226 | regmatch_T *regmatch, |
| 5227 | int *num_file, |
| 5228 | char_u ***file, |
| 5229 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5230 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5231 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | { |
| 5233 | int i; |
| 5234 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5235 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5236 | char_u *str; |
| 5237 | |
| 5238 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5239 | * round == 0: count the number of matching names |
| 5240 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5241 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5242 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | { |
| 5244 | for (i = 0; ; ++i) |
| 5245 | { |
| 5246 | str = (*func)(xp, i); |
| 5247 | if (str == NULL) /* end of list */ |
| 5248 | break; |
| 5249 | if (*str == NUL) /* skip empty strings */ |
| 5250 | continue; |
| 5251 | |
| 5252 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5253 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5254 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5255 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5256 | if (escaped) |
| 5257 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5258 | else |
| 5259 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5260 | (*file)[count] = str; |
| 5261 | #ifdef FEAT_MENU |
| 5262 | if (func == get_menu_names && str != NULL) |
| 5263 | { |
| 5264 | /* test for separator added by get_menu_names() */ |
| 5265 | str += STRLEN(str) - 1; |
| 5266 | if (*str == '\001') |
| 5267 | *str = '.'; |
| 5268 | } |
| 5269 | #endif |
| 5270 | } |
| 5271 | ++count; |
| 5272 | } |
| 5273 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5274 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5275 | { |
| 5276 | if (count == 0) |
| 5277 | return OK; |
| 5278 | *num_file = count; |
| 5279 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5280 | if (*file == NULL) |
| 5281 | { |
| 5282 | *file = (char_u **)""; |
| 5283 | return FAIL; |
| 5284 | } |
| 5285 | count = 0; |
| 5286 | } |
| 5287 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5288 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5289 | /* Sort the results. Keep menu's in the specified order. */ |
| 5290 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5291 | { |
| 5292 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5293 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5294 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5295 | /* <SNR> functions should be sorted to the end. */ |
| 5296 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5297 | sort_func_compare); |
| 5298 | else |
| 5299 | sort_strings(*file, *num_file); |
| 5300 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5301 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5302 | #ifdef FEAT_CMDL_COMPL |
| 5303 | /* Reset the variables used for special highlight names expansion, so that |
| 5304 | * they don't show up when getting normal highlight names by ID. */ |
| 5305 | reset_expand_highlight(); |
| 5306 | #endif |
| 5307 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5308 | return OK; |
| 5309 | } |
| 5310 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5311 | /* |
| 5312 | * Complete a shell command. |
| 5313 | * Returns FAIL or OK; |
| 5314 | */ |
| 5315 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5316 | expand_shellcmd( |
| 5317 | char_u *filepat, /* pattern to match with command names */ |
| 5318 | int *num_file, /* return: number of matches */ |
| 5319 | char_u ***file, /* return: array with matches */ |
| 5320 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5321 | { |
| 5322 | char_u *pat; |
| 5323 | int i; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5324 | char_u *path = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5325 | int mustfree = FALSE; |
| 5326 | garray_T ga; |
| 5327 | char_u *buf = alloc(MAXPATHL); |
| 5328 | size_t l; |
| 5329 | char_u *s, *e; |
| 5330 | int flags = flagsarg; |
| 5331 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5332 | int did_curdir = FALSE; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5333 | hashtab_T found_ht; |
| 5334 | hashitem_T *hi; |
| 5335 | hash_T hash; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5336 | |
| 5337 | if (buf == NULL) |
| 5338 | return FAIL; |
| 5339 | |
| 5340 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5341 | * space */ |
| 5342 | pat = vim_strsave(filepat); |
| 5343 | for (i = 0; pat[i]; ++i) |
| 5344 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5345 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5346 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5347 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5348 | |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5349 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 5350 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5351 | path = (char_u *)"."; |
| 5352 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5353 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5354 | /* For an absolute name we don't use $PATH. */ |
| 5355 | if (!mch_isFullName(pat)) |
| 5356 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5357 | if (path == NULL) |
| 5358 | path = (char_u *)""; |
| 5359 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5360 | |
| 5361 | /* |
| 5362 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5363 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5364 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5365 | */ |
| 5366 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5367 | hash_init(&found_ht); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5368 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5369 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5370 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5371 | e = vim_strchr(s, ';'); |
| 5372 | #else |
| 5373 | e = vim_strchr(s, ':'); |
| 5374 | #endif |
| 5375 | if (e == NULL) |
| 5376 | e = s + STRLEN(s); |
| 5377 | |
Bram Moolenaar | 6ab9e42 | 2018-07-28 19:20:13 +0200 | [diff] [blame] | 5378 | if (*s == NUL) |
| 5379 | { |
| 5380 | if (did_curdir) |
| 5381 | break; |
| 5382 | // Find directories in the current directory, path is empty. |
| 5383 | did_curdir = TRUE; |
| 5384 | flags |= EW_DIR; |
| 5385 | } |
| 5386 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 5387 | { |
| 5388 | did_curdir = TRUE; |
| 5389 | flags |= EW_DIR; |
| 5390 | } |
| 5391 | else |
| 5392 | // Do not match directories inside a $PATH item. |
| 5393 | flags &= ~EW_DIR; |
| 5394 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5395 | l = e - s; |
| 5396 | if (l > MAXPATHL - 5) |
| 5397 | break; |
| 5398 | vim_strncpy(buf, s, l); |
| 5399 | add_pathsep(buf); |
| 5400 | l = STRLEN(buf); |
| 5401 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5402 | |
| 5403 | /* Expand matches in one directory of $PATH. */ |
| 5404 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5405 | if (ret == OK) |
| 5406 | { |
| 5407 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5408 | FreeWild(*num_file, *file); |
| 5409 | else |
| 5410 | { |
| 5411 | for (i = 0; i < *num_file; ++i) |
| 5412 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5413 | char_u *name = (*file)[i]; |
| 5414 | |
| 5415 | if (STRLEN(name) > l) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5416 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5417 | // Check if this name was already found. |
| 5418 | hash = hash_hash(name + l); |
| 5419 | hi = hash_lookup(&found_ht, name + l, hash); |
| 5420 | if (HASHITEM_EMPTY(hi)) |
| 5421 | { |
| 5422 | // Remove the path that was prepended. |
| 5423 | STRMOVE(name, name + l); |
| 5424 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 5425 | hash_add_item(&found_ht, hi, name, hash); |
| 5426 | name = NULL; |
| 5427 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5428 | } |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5429 | vim_free(name); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5430 | } |
| 5431 | vim_free(*file); |
| 5432 | } |
| 5433 | } |
| 5434 | if (*e != NUL) |
| 5435 | ++e; |
| 5436 | } |
| 5437 | *file = ga.ga_data; |
| 5438 | *num_file = ga.ga_len; |
| 5439 | |
| 5440 | vim_free(buf); |
| 5441 | vim_free(pat); |
| 5442 | if (mustfree) |
| 5443 | vim_free(path); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5444 | hash_clear(&found_ht); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5445 | return OK; |
| 5446 | } |
| 5447 | |
| 5448 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5449 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5450 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5451 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5452 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5453 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5454 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5455 | call_user_expand_func( |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5456 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5457 | expand_T *xp, |
| 5458 | int *num_file, |
| 5459 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5461 | int keep = 0; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5462 | typval_T args[4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5463 | int save_current_SID = current_SID; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5464 | char_u *pat = NULL; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5465 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5466 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5467 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5468 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5469 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5470 | *num_file = 0; |
| 5471 | *file = NULL; |
| 5472 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5473 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5474 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5475 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5476 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5477 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5478 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5479 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 5480 | |
| 5481 | args[0].v_type = VAR_STRING; |
| 5482 | args[0].vval.v_string = pat; |
| 5483 | args[1].v_type = VAR_STRING; |
| 5484 | args[1].vval.v_string = xp->xp_line; |
| 5485 | args[2].v_type = VAR_NUMBER; |
| 5486 | args[2].vval.v_number = xp->xp_col; |
| 5487 | args[3].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5489 | /* Save the cmdline, we don't know what the function may do. */ |
| 5490 | save_ccline = ccline; |
| 5491 | ccline.cmdbuff = NULL; |
| 5492 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5493 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5494 | |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5495 | ret = user_expand_func(xp->xp_arg, 3, args); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5496 | |
| 5497 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5498 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5499 | if (ccline.cmdbuff != NULL) |
| 5500 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5501 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5502 | vim_free(pat); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5503 | return ret; |
| 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | * Expand names with a function defined by the user. |
| 5508 | */ |
| 5509 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5510 | ExpandUserDefined( |
| 5511 | expand_T *xp, |
| 5512 | regmatch_T *regmatch, |
| 5513 | int *num_file, |
| 5514 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5515 | { |
| 5516 | char_u *retstr; |
| 5517 | char_u *s; |
| 5518 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5519 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5520 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5521 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5522 | |
| 5523 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5524 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | return FAIL; |
| 5526 | |
| 5527 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5528 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5529 | { |
| 5530 | e = vim_strchr(s, '\n'); |
| 5531 | if (e == NULL) |
| 5532 | e = s + STRLEN(s); |
| 5533 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5534 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5535 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5536 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5537 | *e = keep; |
| 5538 | |
| 5539 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5540 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5541 | if (ga_grow(&ga, 1) == FAIL) |
| 5542 | break; |
| 5543 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5544 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5547 | if (*e != NUL) |
| 5548 | ++e; |
| 5549 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5550 | vim_free(retstr); |
| 5551 | *file = ga.ga_data; |
| 5552 | *num_file = ga.ga_len; |
| 5553 | return OK; |
| 5554 | } |
| 5555 | |
| 5556 | /* |
| 5557 | * Expand names with a list returned by a function defined by the user. |
| 5558 | */ |
| 5559 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5560 | ExpandUserList( |
| 5561 | expand_T *xp, |
| 5562 | int *num_file, |
| 5563 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5564 | { |
| 5565 | list_T *retlist; |
| 5566 | listitem_T *li; |
| 5567 | garray_T ga; |
| 5568 | |
| 5569 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5570 | if (retlist == NULL) |
| 5571 | return FAIL; |
| 5572 | |
| 5573 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5574 | /* Loop over the items in the list. */ |
| 5575 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5576 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5577 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5578 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5579 | |
| 5580 | if (ga_grow(&ga, 1) == FAIL) |
| 5581 | break; |
| 5582 | |
| 5583 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5584 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5585 | ++ga.ga_len; |
| 5586 | } |
| 5587 | list_unref(retlist); |
| 5588 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5589 | *file = ga.ga_data; |
| 5590 | *num_file = ga.ga_len; |
| 5591 | return OK; |
| 5592 | } |
| 5593 | #endif |
| 5594 | |
| 5595 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5596 | * Expand color scheme, compiler or filetype names. |
| 5597 | * Search from 'runtimepath': |
| 5598 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5599 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5600 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5601 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5602 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5603 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5604 | */ |
| 5605 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5606 | ExpandRTDir( |
| 5607 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5608 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5609 | int *num_file, |
| 5610 | char_u ***file, |
| 5611 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5612 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5613 | char_u *s; |
| 5614 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5615 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5616 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5617 | int i; |
| 5618 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5619 | |
| 5620 | *num_file = 0; |
| 5621 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5622 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5623 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5625 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5626 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5627 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5628 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5629 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5630 | ga_clear_strings(&ga); |
| 5631 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5632 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5633 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5634 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5635 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5636 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5637 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5638 | if (flags & DIP_START) { |
| 5639 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5640 | { |
| 5641 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5642 | if (s == NULL) |
| 5643 | { |
| 5644 | ga_clear_strings(&ga); |
| 5645 | return FAIL; |
| 5646 | } |
| 5647 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5648 | globpath(p_pp, s, &ga, 0); |
| 5649 | vim_free(s); |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | if (flags & DIP_OPT) { |
| 5654 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5655 | { |
| 5656 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5657 | if (s == NULL) |
| 5658 | { |
| 5659 | ga_clear_strings(&ga); |
| 5660 | return FAIL; |
| 5661 | } |
| 5662 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5663 | globpath(p_pp, s, &ga, 0); |
| 5664 | vim_free(s); |
| 5665 | } |
| 5666 | } |
| 5667 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5668 | for (i = 0; i < ga.ga_len; ++i) |
| 5669 | { |
| 5670 | match = ((char_u **)ga.ga_data)[i]; |
| 5671 | s = match; |
| 5672 | e = s + STRLEN(s); |
| 5673 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5674 | { |
| 5675 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5676 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5677 | if (s < match || vim_ispathsep(*s)) |
| 5678 | break; |
| 5679 | ++s; |
| 5680 | *e = NUL; |
| 5681 | mch_memmove(match, s, e - s + 1); |
| 5682 | } |
| 5683 | } |
| 5684 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5685 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5686 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5687 | |
| 5688 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5689 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5690 | remove_duplicates(&ga); |
| 5691 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5692 | *file = ga.ga_data; |
| 5693 | *num_file = ga.ga_len; |
| 5694 | return OK; |
| 5695 | } |
| 5696 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5697 | /* |
| 5698 | * Expand loadplugin names: |
| 5699 | * 'packpath'/pack/ * /opt/{pat} |
| 5700 | */ |
| 5701 | static int |
| 5702 | ExpandPackAddDir( |
| 5703 | char_u *pat, |
| 5704 | int *num_file, |
| 5705 | char_u ***file) |
| 5706 | { |
| 5707 | char_u *s; |
| 5708 | char_u *e; |
| 5709 | char_u *match; |
| 5710 | garray_T ga; |
| 5711 | int i; |
| 5712 | int pat_len; |
| 5713 | |
| 5714 | *num_file = 0; |
| 5715 | *file = NULL; |
| 5716 | pat_len = (int)STRLEN(pat); |
| 5717 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5718 | |
| 5719 | s = alloc((unsigned)(pat_len + 26)); |
| 5720 | if (s == NULL) |
| 5721 | { |
| 5722 | ga_clear_strings(&ga); |
| 5723 | return FAIL; |
| 5724 | } |
| 5725 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5726 | globpath(p_pp, s, &ga, 0); |
| 5727 | vim_free(s); |
| 5728 | |
| 5729 | for (i = 0; i < ga.ga_len; ++i) |
| 5730 | { |
| 5731 | match = ((char_u **)ga.ga_data)[i]; |
| 5732 | s = gettail(match); |
| 5733 | e = s + STRLEN(s); |
| 5734 | mch_memmove(match, s, e - s + 1); |
| 5735 | } |
| 5736 | |
| 5737 | if (ga.ga_len == 0) |
| 5738 | return FAIL; |
| 5739 | |
| 5740 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5741 | * directories in dirnames. */ |
| 5742 | remove_duplicates(&ga); |
| 5743 | |
| 5744 | *file = ga.ga_data; |
| 5745 | *num_file = ga.ga_len; |
| 5746 | return OK; |
| 5747 | } |
| 5748 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5749 | #endif |
| 5750 | |
| 5751 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5752 | /* |
| 5753 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5754 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5755 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5756 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5757 | globpath( |
| 5758 | char_u *path, |
| 5759 | char_u *file, |
| 5760 | garray_T *ga, |
| 5761 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5762 | { |
| 5763 | expand_T xpc; |
| 5764 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5765 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5766 | int num_p; |
| 5767 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5768 | |
| 5769 | buf = alloc(MAXPATHL); |
| 5770 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5771 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5772 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5773 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5774 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5775 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5776 | /* Loop over all entries in {path}. */ |
| 5777 | while (*path != NUL) |
| 5778 | { |
| 5779 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5780 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5781 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5782 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5783 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5784 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5785 | * treat it as an escape character, use '/' instead. */ |
| 5786 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5787 | STRCAT(buf, "/"); |
| 5788 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5790 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5791 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5792 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5793 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5794 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5795 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5796 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5797 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5798 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5799 | for (i = 0; i < num_p; ++i) |
| 5800 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5801 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5802 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5803 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5804 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5805 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5807 | FreeWild(num_p, p); |
| 5808 | } |
| 5809 | } |
| 5810 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5811 | |
| 5812 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | #endif |
| 5816 | |
| 5817 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5818 | |
| 5819 | /********************************* |
| 5820 | * Command line history stuff * |
| 5821 | *********************************/ |
| 5822 | |
| 5823 | /* |
| 5824 | * Translate a history character to the associated type number. |
| 5825 | */ |
| 5826 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5827 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5828 | { |
| 5829 | if (c == ':') |
| 5830 | return HIST_CMD; |
| 5831 | if (c == '=') |
| 5832 | return HIST_EXPR; |
| 5833 | if (c == '@') |
| 5834 | return HIST_INPUT; |
| 5835 | if (c == '>') |
| 5836 | return HIST_DEBUG; |
| 5837 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Table of history names. |
| 5842 | * These names are used in :history and various hist...() functions. |
| 5843 | * It is sufficient to give the significant prefix of a history name. |
| 5844 | */ |
| 5845 | |
| 5846 | static char *(history_names[]) = |
| 5847 | { |
| 5848 | "cmd", |
| 5849 | "search", |
| 5850 | "expr", |
| 5851 | "input", |
| 5852 | "debug", |
| 5853 | NULL |
| 5854 | }; |
| 5855 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5856 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5857 | /* |
| 5858 | * Function given to ExpandGeneric() to obtain the possible first |
| 5859 | * arguments of the ":history command. |
| 5860 | */ |
| 5861 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5862 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5863 | { |
| 5864 | static char_u compl[2] = { NUL, NUL }; |
| 5865 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5866 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5867 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5868 | |
| 5869 | if (idx < short_names_count) |
| 5870 | { |
| 5871 | compl[0] = (char_u)short_names[idx]; |
| 5872 | return compl; |
| 5873 | } |
| 5874 | if (idx < short_names_count + history_name_count) |
| 5875 | return (char_u *)history_names[idx - short_names_count]; |
| 5876 | if (idx == short_names_count + history_name_count) |
| 5877 | return (char_u *)"all"; |
| 5878 | return NULL; |
| 5879 | } |
| 5880 | #endif |
| 5881 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5882 | /* |
| 5883 | * init_history() - Initialize the command line history. |
| 5884 | * Also used to re-allocate the history when the size changes. |
| 5885 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5886 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5887 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5888 | { |
| 5889 | int newlen; /* new length of history table */ |
| 5890 | histentry_T *temp; |
| 5891 | int i; |
| 5892 | int j; |
| 5893 | int type; |
| 5894 | |
| 5895 | /* |
| 5896 | * If size of history table changed, reallocate it |
| 5897 | */ |
| 5898 | newlen = (int)p_hi; |
| 5899 | if (newlen != hislen) /* history length changed */ |
| 5900 | { |
| 5901 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5902 | { |
| 5903 | if (newlen) |
| 5904 | { |
| 5905 | temp = (histentry_T *)lalloc( |
| 5906 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5907 | if (temp == NULL) /* out of memory! */ |
| 5908 | { |
| 5909 | if (type == 0) /* first one: just keep the old length */ |
| 5910 | { |
| 5911 | newlen = hislen; |
| 5912 | break; |
| 5913 | } |
| 5914 | /* Already changed one table, now we can only have zero |
| 5915 | * length for all tables. */ |
| 5916 | newlen = 0; |
| 5917 | type = -1; |
| 5918 | continue; |
| 5919 | } |
| 5920 | } |
| 5921 | else |
| 5922 | temp = NULL; |
| 5923 | if (newlen == 0 || temp != NULL) |
| 5924 | { |
| 5925 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5926 | { |
| 5927 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5928 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5929 | } |
| 5930 | else if (newlen > hislen) /* array becomes bigger */ |
| 5931 | { |
| 5932 | for (i = 0; i <= hisidx[type]; ++i) |
| 5933 | temp[i] = history[type][i]; |
| 5934 | j = i; |
| 5935 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5936 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5937 | for ( ; j < hislen; ++i, ++j) |
| 5938 | temp[i] = history[type][j]; |
| 5939 | } |
| 5940 | else /* array becomes smaller or 0 */ |
| 5941 | { |
| 5942 | j = hisidx[type]; |
| 5943 | for (i = newlen - 1; ; --i) |
| 5944 | { |
| 5945 | if (i >= 0) /* copy newest entries */ |
| 5946 | temp[i] = history[type][j]; |
| 5947 | else /* remove older entries */ |
| 5948 | vim_free(history[type][j].hisstr); |
| 5949 | if (--j < 0) |
| 5950 | j = hislen - 1; |
| 5951 | if (j == hisidx[type]) |
| 5952 | break; |
| 5953 | } |
| 5954 | hisidx[type] = newlen - 1; |
| 5955 | } |
| 5956 | vim_free(history[type]); |
| 5957 | history[type] = temp; |
| 5958 | } |
| 5959 | } |
| 5960 | hislen = newlen; |
| 5961 | } |
| 5962 | } |
| 5963 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5964 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5965 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5966 | { |
| 5967 | hisptr->hisnum = 0; |
| 5968 | hisptr->viminfo = FALSE; |
| 5969 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5970 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5971 | } |
| 5972 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5973 | /* |
| 5974 | * Check if command line 'str' is already in history. |
| 5975 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5976 | */ |
| 5977 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5978 | in_history( |
| 5979 | int type, |
| 5980 | char_u *str, |
| 5981 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5982 | int sep, |
| 5983 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5984 | { |
| 5985 | int i; |
| 5986 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5987 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5988 | |
| 5989 | if (hisidx[type] < 0) |
| 5990 | return FALSE; |
| 5991 | i = hisidx[type]; |
| 5992 | do |
| 5993 | { |
| 5994 | if (history[type][i].hisstr == NULL) |
| 5995 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5996 | |
| 5997 | /* For search history, check that the separator character matches as |
| 5998 | * well. */ |
| 5999 | p = history[type][i].hisstr; |
| 6000 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6001 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6002 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6003 | { |
| 6004 | if (!move_to_front) |
| 6005 | return TRUE; |
| 6006 | last_i = i; |
| 6007 | break; |
| 6008 | } |
| 6009 | if (--i < 0) |
| 6010 | i = hislen - 1; |
| 6011 | } while (i != hisidx[type]); |
| 6012 | |
| 6013 | if (last_i >= 0) |
| 6014 | { |
| 6015 | str = history[type][i].hisstr; |
| 6016 | while (i != hisidx[type]) |
| 6017 | { |
| 6018 | if (++i >= hislen) |
| 6019 | i = 0; |
| 6020 | history[type][last_i] = history[type][i]; |
| 6021 | last_i = i; |
| 6022 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6023 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6024 | history[type][i].viminfo = FALSE; |
| 6025 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6026 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6027 | return TRUE; |
| 6028 | } |
| 6029 | return FALSE; |
| 6030 | } |
| 6031 | |
| 6032 | /* |
| 6033 | * Convert history name (from table above) to its HIST_ equivalent. |
| 6034 | * When "name" is empty, return "cmd" history. |
| 6035 | * Returns -1 for unknown history name. |
| 6036 | */ |
| 6037 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6038 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6039 | { |
| 6040 | int i; |
| 6041 | int len = (int)STRLEN(name); |
| 6042 | |
| 6043 | /* No argument: use current history. */ |
| 6044 | if (len == 0) |
| 6045 | return hist_char2type(ccline.cmdfirstc); |
| 6046 | |
| 6047 | for (i = 0; history_names[i] != NULL; ++i) |
| 6048 | if (STRNICMP(name, history_names[i], len) == 0) |
| 6049 | return i; |
| 6050 | |
| 6051 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 6052 | return hist_char2type(name[0]); |
| 6053 | |
| 6054 | return -1; |
| 6055 | } |
| 6056 | |
| 6057 | static int last_maptick = -1; /* last seen maptick */ |
| 6058 | |
| 6059 | /* |
| 6060 | * Add the given string to the given history. If the string is already in the |
| 6061 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 6062 | * values. |
| 6063 | */ |
| 6064 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6065 | add_to_history( |
| 6066 | int histype, |
| 6067 | char_u *new_entry, |
| 6068 | int in_map, /* consider maptick when inside a mapping */ |
| 6069 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | { |
| 6071 | histentry_T *hisptr; |
| 6072 | int len; |
| 6073 | |
| 6074 | if (hislen == 0) /* no history */ |
| 6075 | return; |
| 6076 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 6077 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 6078 | return; |
| 6079 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6080 | /* |
| 6081 | * Searches inside the same mapping overwrite each other, so that only |
| 6082 | * the last line is kept. Be careful not to remove a line that was moved |
| 6083 | * down, only lines that were added. |
| 6084 | */ |
| 6085 | if (histype == HIST_SEARCH && in_map) |
| 6086 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 6087 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6088 | { |
| 6089 | /* Current line is from the same mapping, remove it */ |
| 6090 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 6091 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6092 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6093 | --hisnum[histype]; |
| 6094 | if (--hisidx[HIST_SEARCH] < 0) |
| 6095 | hisidx[HIST_SEARCH] = hislen - 1; |
| 6096 | } |
| 6097 | last_maptick = -1; |
| 6098 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6099 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6100 | { |
| 6101 | if (++hisidx[histype] == hislen) |
| 6102 | hisidx[histype] = 0; |
| 6103 | hisptr = &history[histype][hisidx[histype]]; |
| 6104 | vim_free(hisptr->hisstr); |
| 6105 | |
| 6106 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6107 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6108 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 6109 | if (hisptr->hisstr != NULL) |
| 6110 | hisptr->hisstr[len + 1] = sep; |
| 6111 | |
| 6112 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6113 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6114 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | if (histype == HIST_SEARCH && in_map) |
| 6116 | last_maptick = maptick; |
| 6117 | } |
| 6118 | } |
| 6119 | |
| 6120 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6121 | |
| 6122 | /* |
| 6123 | * Get identifier of newest history entry. |
| 6124 | * "histype" may be one of the HIST_ values. |
| 6125 | */ |
| 6126 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6127 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6128 | { |
| 6129 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6130 | || hisidx[histype] < 0) |
| 6131 | return -1; |
| 6132 | |
| 6133 | return history[histype][hisidx[histype]].hisnum; |
| 6134 | } |
| 6135 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6136 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6137 | * Calculate history index from a number: |
| 6138 | * num > 0: seen as identifying number of a history entry |
| 6139 | * num < 0: relative position in history wrt newest entry |
| 6140 | * "histype" may be one of the HIST_ values. |
| 6141 | */ |
| 6142 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6143 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6144 | { |
| 6145 | int i; |
| 6146 | histentry_T *hist; |
| 6147 | int wrapped = FALSE; |
| 6148 | |
| 6149 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6150 | || (i = hisidx[histype]) < 0 || num == 0) |
| 6151 | return -1; |
| 6152 | |
| 6153 | hist = history[histype]; |
| 6154 | if (num > 0) |
| 6155 | { |
| 6156 | while (hist[i].hisnum > num) |
| 6157 | if (--i < 0) |
| 6158 | { |
| 6159 | if (wrapped) |
| 6160 | break; |
| 6161 | i += hislen; |
| 6162 | wrapped = TRUE; |
| 6163 | } |
| 6164 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 6165 | return i; |
| 6166 | } |
| 6167 | else if (-num <= hislen) |
| 6168 | { |
| 6169 | i += num + 1; |
| 6170 | if (i < 0) |
| 6171 | i += hislen; |
| 6172 | if (hist[i].hisstr != NULL) |
| 6173 | return i; |
| 6174 | } |
| 6175 | return -1; |
| 6176 | } |
| 6177 | |
| 6178 | /* |
| 6179 | * Get a history entry by its index. |
| 6180 | * "histype" may be one of the HIST_ values. |
| 6181 | */ |
| 6182 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6183 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6184 | { |
| 6185 | idx = calc_hist_idx(histype, idx); |
| 6186 | if (idx >= 0) |
| 6187 | return history[histype][idx].hisstr; |
| 6188 | else |
| 6189 | return (char_u *)""; |
| 6190 | } |
| 6191 | |
| 6192 | /* |
| 6193 | * Clear all entries of a history. |
| 6194 | * "histype" may be one of the HIST_ values. |
| 6195 | */ |
| 6196 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6197 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6198 | { |
| 6199 | int i; |
| 6200 | histentry_T *hisptr; |
| 6201 | |
| 6202 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6203 | { |
| 6204 | hisptr = history[histype]; |
| 6205 | for (i = hislen; i--;) |
| 6206 | { |
| 6207 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6208 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6209 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6210 | } |
| 6211 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6212 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6213 | return OK; |
| 6214 | } |
| 6215 | return FAIL; |
| 6216 | } |
| 6217 | |
| 6218 | /* |
| 6219 | * Remove all entries matching {str} from a history. |
| 6220 | * "histype" may be one of the HIST_ values. |
| 6221 | */ |
| 6222 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6223 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6224 | { |
| 6225 | regmatch_T regmatch; |
| 6226 | histentry_T *hisptr; |
| 6227 | int idx; |
| 6228 | int i; |
| 6229 | int last; |
| 6230 | int found = FALSE; |
| 6231 | |
| 6232 | regmatch.regprog = NULL; |
| 6233 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6234 | if (hislen != 0 |
| 6235 | && histype >= 0 |
| 6236 | && histype < HIST_COUNT |
| 6237 | && *str != NUL |
| 6238 | && (idx = hisidx[histype]) >= 0 |
| 6239 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6240 | != NULL) |
| 6241 | { |
| 6242 | i = last = idx; |
| 6243 | do |
| 6244 | { |
| 6245 | hisptr = &history[histype][i]; |
| 6246 | if (hisptr->hisstr == NULL) |
| 6247 | break; |
| 6248 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6249 | { |
| 6250 | found = TRUE; |
| 6251 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6252 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6253 | } |
| 6254 | else |
| 6255 | { |
| 6256 | if (i != last) |
| 6257 | { |
| 6258 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6259 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6260 | } |
| 6261 | if (--last < 0) |
| 6262 | last += hislen; |
| 6263 | } |
| 6264 | if (--i < 0) |
| 6265 | i += hislen; |
| 6266 | } while (i != idx); |
| 6267 | if (history[histype][idx].hisstr == NULL) |
| 6268 | hisidx[histype] = -1; |
| 6269 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6270 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6271 | return found; |
| 6272 | } |
| 6273 | |
| 6274 | /* |
| 6275 | * Remove an indexed entry from a history. |
| 6276 | * "histype" may be one of the HIST_ values. |
| 6277 | */ |
| 6278 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6279 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6280 | { |
| 6281 | int i, j; |
| 6282 | |
| 6283 | i = calc_hist_idx(histype, idx); |
| 6284 | if (i < 0) |
| 6285 | return FALSE; |
| 6286 | idx = hisidx[histype]; |
| 6287 | vim_free(history[histype][i].hisstr); |
| 6288 | |
| 6289 | /* When deleting the last added search string in a mapping, reset |
| 6290 | * last_maptick, so that the last added search string isn't deleted again. |
| 6291 | */ |
| 6292 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6293 | last_maptick = -1; |
| 6294 | |
| 6295 | while (i != idx) |
| 6296 | { |
| 6297 | j = (i + 1) % hislen; |
| 6298 | history[histype][i] = history[histype][j]; |
| 6299 | i = j; |
| 6300 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6301 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6302 | if (--i < 0) |
| 6303 | i += hislen; |
| 6304 | hisidx[histype] = i; |
| 6305 | return TRUE; |
| 6306 | } |
| 6307 | |
| 6308 | #endif /* FEAT_EVAL */ |
| 6309 | |
| 6310 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6311 | /* |
| 6312 | * Very specific function to remove the value in ":set key=val" from the |
| 6313 | * history. |
| 6314 | */ |
| 6315 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6316 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6317 | { |
| 6318 | char_u *p; |
| 6319 | int i; |
| 6320 | |
| 6321 | i = hisidx[HIST_CMD]; |
| 6322 | if (i < 0) |
| 6323 | return; |
| 6324 | p = history[HIST_CMD][i].hisstr; |
| 6325 | if (p != NULL) |
| 6326 | for ( ; *p; ++p) |
| 6327 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6328 | { |
| 6329 | p = vim_strchr(p + 3, '='); |
| 6330 | if (p == NULL) |
| 6331 | break; |
| 6332 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6333 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | if (p[i] == '\\' && p[i + 1]) |
| 6335 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6336 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6337 | --p; |
| 6338 | } |
| 6339 | } |
| 6340 | #endif |
| 6341 | |
| 6342 | #endif /* FEAT_CMDHIST */ |
| 6343 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6344 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6345 | /* |
| 6346 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6347 | * ccline and put the previous value in prev_ccline. |
| 6348 | */ |
| 6349 | static struct cmdline_info * |
| 6350 | get_ccline_ptr(void) |
| 6351 | { |
| 6352 | if ((State & CMDLINE) == 0) |
| 6353 | return NULL; |
| 6354 | if (ccline.cmdbuff != NULL) |
| 6355 | return &ccline; |
| 6356 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6357 | return &prev_ccline; |
| 6358 | return NULL; |
| 6359 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6360 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6361 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6362 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6363 | /* |
| 6364 | * Get the current command line in allocated memory. |
| 6365 | * Only works when the command line is being edited. |
| 6366 | * Returns NULL when something is wrong. |
| 6367 | */ |
| 6368 | char_u * |
| 6369 | get_cmdline_str(void) |
| 6370 | { |
| 6371 | struct cmdline_info *p = get_ccline_ptr(); |
| 6372 | |
| 6373 | if (p == NULL) |
| 6374 | return NULL; |
| 6375 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6376 | } |
| 6377 | |
| 6378 | /* |
| 6379 | * Get the current command line position, counted in bytes. |
| 6380 | * Zero is the first position. |
| 6381 | * Only works when the command line is being edited. |
| 6382 | * Returns -1 when something is wrong. |
| 6383 | */ |
| 6384 | int |
| 6385 | get_cmdline_pos(void) |
| 6386 | { |
| 6387 | struct cmdline_info *p = get_ccline_ptr(); |
| 6388 | |
| 6389 | if (p == NULL) |
| 6390 | return -1; |
| 6391 | return p->cmdpos; |
| 6392 | } |
| 6393 | |
| 6394 | /* |
| 6395 | * Set the command line byte position to "pos". Zero is the first position. |
| 6396 | * Only works when the command line is being edited. |
| 6397 | * Returns 1 when failed, 0 when OK. |
| 6398 | */ |
| 6399 | int |
| 6400 | set_cmdline_pos( |
| 6401 | int pos) |
| 6402 | { |
| 6403 | struct cmdline_info *p = get_ccline_ptr(); |
| 6404 | |
| 6405 | if (p == NULL) |
| 6406 | return 1; |
| 6407 | |
| 6408 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6409 | * changed the command line. */ |
| 6410 | if (pos < 0) |
| 6411 | new_cmdpos = 0; |
| 6412 | else |
| 6413 | new_cmdpos = pos; |
| 6414 | return 0; |
| 6415 | } |
| 6416 | #endif |
| 6417 | |
| 6418 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6419 | /* |
| 6420 | * Get the current command-line type. |
| 6421 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6422 | * Only works when the command line is being edited. |
| 6423 | * Returns NUL when something is wrong. |
| 6424 | */ |
| 6425 | int |
| 6426 | get_cmdline_type(void) |
| 6427 | { |
| 6428 | struct cmdline_info *p = get_ccline_ptr(); |
| 6429 | |
| 6430 | if (p == NULL) |
| 6431 | return NUL; |
| 6432 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6433 | return |
| 6434 | # ifdef FEAT_EVAL |
| 6435 | (p->input_fn) ? '@' : |
| 6436 | # endif |
| 6437 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6438 | return p->cmdfirstc; |
| 6439 | } |
| 6440 | #endif |
| 6441 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6442 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6443 | /* |
| 6444 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6445 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6446 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6447 | */ |
| 6448 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6449 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6450 | { |
| 6451 | int len; |
| 6452 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6453 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6454 | |
| 6455 | *str = skipwhite(*str); |
| 6456 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6457 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6458 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6459 | *str += len; |
| 6460 | *num1 = (int)num; |
| 6461 | first = TRUE; |
| 6462 | } |
| 6463 | *str = skipwhite(*str); |
| 6464 | if (**str == ',') /* parse "to" part of range */ |
| 6465 | { |
| 6466 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6467 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6468 | if (len > 0) |
| 6469 | { |
| 6470 | *num2 = (int)num; |
| 6471 | *str = skipwhite(*str + len); |
| 6472 | } |
| 6473 | else if (!first) /* no number given at all */ |
| 6474 | return FAIL; |
| 6475 | } |
| 6476 | else if (first) /* only one number given */ |
| 6477 | *num2 = *num1; |
| 6478 | return OK; |
| 6479 | } |
| 6480 | #endif |
| 6481 | |
| 6482 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6483 | /* |
| 6484 | * :history command - print a history |
| 6485 | */ |
| 6486 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6487 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6488 | { |
| 6489 | histentry_T *hist; |
| 6490 | int histype1 = HIST_CMD; |
| 6491 | int histype2 = HIST_CMD; |
| 6492 | int hisidx1 = 1; |
| 6493 | int hisidx2 = -1; |
| 6494 | int idx; |
| 6495 | int i, j, k; |
| 6496 | char_u *end; |
| 6497 | char_u *arg = eap->arg; |
| 6498 | |
| 6499 | if (hislen == 0) |
| 6500 | { |
| 6501 | MSG(_("'history' option is zero")); |
| 6502 | return; |
| 6503 | } |
| 6504 | |
| 6505 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6506 | { |
| 6507 | end = arg; |
| 6508 | while (ASCII_ISALPHA(*end) |
| 6509 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6510 | end++; |
| 6511 | i = *end; |
| 6512 | *end = NUL; |
| 6513 | histype1 = get_histtype(arg); |
| 6514 | if (histype1 == -1) |
| 6515 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6516 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6517 | { |
| 6518 | histype1 = 0; |
| 6519 | histype2 = HIST_COUNT-1; |
| 6520 | } |
| 6521 | else |
| 6522 | { |
| 6523 | *end = i; |
| 6524 | EMSG(_(e_trailing)); |
| 6525 | return; |
| 6526 | } |
| 6527 | } |
| 6528 | else |
| 6529 | histype2 = histype1; |
| 6530 | *end = i; |
| 6531 | } |
| 6532 | else |
| 6533 | end = arg; |
| 6534 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6535 | { |
| 6536 | EMSG(_(e_trailing)); |
| 6537 | return; |
| 6538 | } |
| 6539 | |
| 6540 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6541 | { |
| 6542 | STRCPY(IObuff, "\n # "); |
| 6543 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6544 | MSG_PUTS_TITLE(IObuff); |
| 6545 | idx = hisidx[histype1]; |
| 6546 | hist = history[histype1]; |
| 6547 | j = hisidx1; |
| 6548 | k = hisidx2; |
| 6549 | if (j < 0) |
| 6550 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6551 | if (k < 0) |
| 6552 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6553 | if (idx >= 0 && j <= k) |
| 6554 | for (i = idx + 1; !got_int; ++i) |
| 6555 | { |
| 6556 | if (i == hislen) |
| 6557 | i = 0; |
| 6558 | if (hist[i].hisstr != NULL |
| 6559 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6560 | { |
| 6561 | msg_putchar('\n'); |
| 6562 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6563 | hist[i].hisnum); |
| 6564 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6565 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6566 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6567 | else |
| 6568 | STRCAT(IObuff, hist[i].hisstr); |
| 6569 | msg_outtrans(IObuff); |
| 6570 | out_flush(); |
| 6571 | } |
| 6572 | if (i == idx) |
| 6573 | break; |
| 6574 | } |
| 6575 | } |
| 6576 | } |
| 6577 | #endif |
| 6578 | |
| 6579 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6580 | /* |
| 6581 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6582 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6583 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6584 | {NULL, NULL, NULL, NULL, NULL}; |
| 6585 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6586 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6587 | static int viminfo_add_at_front = FALSE; |
| 6588 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6589 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6590 | |
| 6591 | /* |
| 6592 | * Translate a history type number to the associated character. |
| 6593 | */ |
| 6594 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6595 | hist_type2char( |
| 6596 | int type, |
| 6597 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | { |
| 6599 | if (type == HIST_CMD) |
| 6600 | return ':'; |
| 6601 | if (type == HIST_SEARCH) |
| 6602 | { |
| 6603 | if (use_question) |
| 6604 | return '?'; |
| 6605 | else |
| 6606 | return '/'; |
| 6607 | } |
| 6608 | if (type == HIST_EXPR) |
| 6609 | return '='; |
| 6610 | return '@'; |
| 6611 | } |
| 6612 | |
| 6613 | /* |
| 6614 | * Prepare for reading the history from the viminfo file. |
| 6615 | * This allocates history arrays to store the read history lines. |
| 6616 | */ |
| 6617 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6618 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6619 | { |
| 6620 | int i; |
| 6621 | int num; |
| 6622 | int type; |
| 6623 | int len; |
| 6624 | |
| 6625 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6626 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6627 | if (asklen > hislen) |
| 6628 | asklen = hislen; |
| 6629 | |
| 6630 | for (type = 0; type < HIST_COUNT; ++type) |
| 6631 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6632 | /* Count the number of empty spaces in the history list. Entries read |
| 6633 | * from viminfo previously are also considered empty. If there are |
| 6634 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6635 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6636 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6637 | num++; |
| 6638 | len = asklen; |
| 6639 | if (num > len) |
| 6640 | len = num; |
| 6641 | if (len <= 0) |
| 6642 | viminfo_history[type] = NULL; |
| 6643 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6644 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6645 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6646 | if (viminfo_history[type] == NULL) |
| 6647 | len = 0; |
| 6648 | viminfo_hislen[type] = len; |
| 6649 | viminfo_hisidx[type] = 0; |
| 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | /* |
| 6654 | * Accept a line from the viminfo, store it in the history array when it's |
| 6655 | * new. |
| 6656 | */ |
| 6657 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6658 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | { |
| 6660 | int type; |
| 6661 | long_u len; |
| 6662 | char_u *val; |
| 6663 | char_u *p; |
| 6664 | |
| 6665 | type = hist_char2type(virp->vir_line[0]); |
| 6666 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6667 | { |
| 6668 | val = viminfo_readstring(virp, 1, TRUE); |
| 6669 | if (val != NULL && *val != NUL) |
| 6670 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6671 | int sep = (*val == ' ' ? NUL : *val); |
| 6672 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6673 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6674 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6675 | { |
| 6676 | /* Need to re-allocate to append the separator byte. */ |
| 6677 | len = STRLEN(val); |
| 6678 | p = lalloc(len + 2, TRUE); |
| 6679 | if (p != NULL) |
| 6680 | { |
| 6681 | if (type == HIST_SEARCH) |
| 6682 | { |
| 6683 | /* Search entry: Move the separator from the first |
| 6684 | * column to after the NUL. */ |
| 6685 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6686 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6687 | } |
| 6688 | else |
| 6689 | { |
| 6690 | /* Not a search entry: No separator in the viminfo |
| 6691 | * file, add a NUL separator. */ |
| 6692 | mch_memmove(p, val, (size_t)len + 1); |
| 6693 | p[len + 1] = NUL; |
| 6694 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6695 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6696 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6697 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6698 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6699 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6700 | } |
| 6701 | } |
| 6702 | } |
| 6703 | vim_free(val); |
| 6704 | } |
| 6705 | return viminfo_readline(virp); |
| 6706 | } |
| 6707 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6708 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6709 | * Accept a new style history line from the viminfo, store it in the history |
| 6710 | * array when it's new. |
| 6711 | */ |
| 6712 | void |
| 6713 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6714 | garray_T *values, |
| 6715 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6716 | { |
| 6717 | int type; |
| 6718 | long_u len; |
| 6719 | char_u *val; |
| 6720 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6721 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6722 | |
| 6723 | /* Check the format: |
| 6724 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6725 | if (values->ga_len < 4 |
| 6726 | || vp[0].bv_type != BVAL_NR |
| 6727 | || vp[1].bv_type != BVAL_NR |
| 6728 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6729 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6730 | return; |
| 6731 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6732 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6733 | if (type >= HIST_COUNT) |
| 6734 | return; |
| 6735 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6736 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6737 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6738 | if (val != NULL && *val != NUL) |
| 6739 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6740 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6741 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6742 | int idx; |
| 6743 | int overwrite = FALSE; |
| 6744 | |
| 6745 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6746 | { |
| 6747 | /* If lines were written by an older Vim we need to avoid |
| 6748 | * getting duplicates. See if the entry already exists. */ |
| 6749 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6750 | { |
| 6751 | p = viminfo_history[type][idx].hisstr; |
| 6752 | if (STRCMP(val, p) == 0 |
| 6753 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6754 | { |
| 6755 | overwrite = TRUE; |
| 6756 | break; |
| 6757 | } |
| 6758 | } |
| 6759 | |
| 6760 | if (!overwrite) |
| 6761 | { |
| 6762 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6763 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6764 | p = lalloc(len + 2, TRUE); |
| 6765 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6766 | else |
| 6767 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6768 | if (p != NULL) |
| 6769 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6770 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6771 | if (!overwrite) |
| 6772 | { |
| 6773 | mch_memmove(p, val, (size_t)len + 1); |
| 6774 | /* Put the separator after the NUL. */ |
| 6775 | p[len + 1] = sep; |
| 6776 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6777 | viminfo_history[type][idx].hisnum = 0; |
| 6778 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6779 | viminfo_hisidx[type]++; |
| 6780 | } |
| 6781 | } |
| 6782 | } |
| 6783 | } |
| 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6788 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6789 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6790 | static void |
| 6791 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6792 | { |
| 6793 | int idx; |
| 6794 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6795 | |
| 6796 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6797 | if (idx >= hislen) |
| 6798 | idx -= hislen; |
| 6799 | else if (idx < 0) |
| 6800 | idx = hislen - 1; |
| 6801 | if (viminfo_add_at_front) |
| 6802 | hisidx[type] = idx; |
| 6803 | else |
| 6804 | { |
| 6805 | if (hisidx[type] == -1) |
| 6806 | hisidx[type] = hislen - 1; |
| 6807 | do |
| 6808 | { |
| 6809 | if (history[type][idx].hisstr != NULL |
| 6810 | || history[type][idx].viminfo) |
| 6811 | break; |
| 6812 | if (++idx == hislen) |
| 6813 | idx = 0; |
| 6814 | } while (idx != hisidx[type]); |
| 6815 | if (idx != hisidx[type] && --idx < 0) |
| 6816 | idx = hislen - 1; |
| 6817 | } |
| 6818 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6819 | { |
| 6820 | vim_free(history[type][idx].hisstr); |
| 6821 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6822 | history[type][idx].viminfo = TRUE; |
| 6823 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6824 | if (--idx < 0) |
| 6825 | idx = hislen - 1; |
| 6826 | } |
| 6827 | idx += 1; |
| 6828 | idx %= hislen; |
| 6829 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6830 | { |
| 6831 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6832 | idx %= hislen; |
| 6833 | } |
| 6834 | } |
| 6835 | |
| 6836 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6837 | static int |
| 6838 | #ifdef __BORLANDC__ |
| 6839 | _RTLENTRYF |
| 6840 | #endif |
| 6841 | sort_hist(const void *s1, const void *s2) |
| 6842 | { |
| 6843 | histentry_T *p1 = *(histentry_T **)s1; |
| 6844 | histentry_T *p2 = *(histentry_T **)s2; |
| 6845 | |
| 6846 | if (p1->time_set < p2->time_set) return -1; |
| 6847 | if (p1->time_set > p2->time_set) return 1; |
| 6848 | return 0; |
| 6849 | } |
| 6850 | #endif |
| 6851 | |
| 6852 | /* |
| 6853 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6854 | * timestamp; |
| 6855 | */ |
| 6856 | static void |
| 6857 | merge_history(int type) |
| 6858 | { |
| 6859 | int max_len; |
| 6860 | histentry_T **tot_hist; |
| 6861 | histentry_T *new_hist; |
| 6862 | int i; |
| 6863 | int len; |
| 6864 | |
| 6865 | /* Make one long list with all entries. */ |
| 6866 | max_len = hislen + viminfo_hisidx[type]; |
| 6867 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6868 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6869 | if (tot_hist == NULL || new_hist == NULL) |
| 6870 | { |
| 6871 | vim_free(tot_hist); |
| 6872 | vim_free(new_hist); |
| 6873 | return; |
| 6874 | } |
| 6875 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6876 | tot_hist[i] = &viminfo_history[type][i]; |
| 6877 | len = i; |
| 6878 | for (i = 0; i < hislen; i++) |
| 6879 | if (history[type][i].hisstr != NULL) |
| 6880 | tot_hist[len++] = &history[type][i]; |
| 6881 | |
| 6882 | /* Sort the list on timestamp. */ |
| 6883 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6884 | |
| 6885 | /* Keep the newest ones. */ |
| 6886 | for (i = 0; i < hislen; i++) |
| 6887 | { |
| 6888 | if (i < len) |
| 6889 | { |
| 6890 | new_hist[i] = *tot_hist[i]; |
| 6891 | tot_hist[i]->hisstr = NULL; |
| 6892 | if (new_hist[i].hisnum == 0) |
| 6893 | new_hist[i].hisnum = ++hisnum[type]; |
| 6894 | } |
| 6895 | else |
| 6896 | clear_hist_entry(&new_hist[i]); |
| 6897 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6898 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6899 | |
| 6900 | /* Free what is not kept. */ |
| 6901 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6902 | vim_free(viminfo_history[type][i].hisstr); |
| 6903 | for (i = 0; i < hislen; i++) |
| 6904 | vim_free(history[type][i].hisstr); |
| 6905 | vim_free(history[type]); |
| 6906 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6907 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | /* |
| 6911 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6912 | */ |
| 6913 | void |
| 6914 | finish_viminfo_history(vir_T *virp) |
| 6915 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6916 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6917 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6918 | |
| 6919 | for (type = 0; type < HIST_COUNT; ++type) |
| 6920 | { |
| 6921 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6922 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6923 | |
| 6924 | if (merge) |
| 6925 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6927 | concat_history(type); |
| 6928 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6929 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6930 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6931 | } |
| 6932 | } |
| 6933 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6934 | /* |
| 6935 | * Write history to viminfo file in "fp". |
| 6936 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6937 | * file, data is in viminfo_history[]. |
| 6938 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6939 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6940 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6941 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6942 | { |
| 6943 | int i; |
| 6944 | int type; |
| 6945 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6946 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6947 | |
| 6948 | init_history(); |
| 6949 | if (hislen == 0) |
| 6950 | return; |
| 6951 | for (type = 0; type < HIST_COUNT; ++type) |
| 6952 | { |
| 6953 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6954 | if (num_saved == 0) |
| 6955 | continue; |
| 6956 | if (num_saved < 0) /* Use default */ |
| 6957 | num_saved = hislen; |
| 6958 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6959 | type == HIST_CMD ? _("Command Line") : |
| 6960 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6961 | type == HIST_EXPR ? _("Expression") : |
| 6962 | type == HIST_INPUT ? _("Input Line") : |
| 6963 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6964 | if (num_saved > hislen) |
| 6965 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6966 | |
| 6967 | /* |
| 6968 | * Merge typed and viminfo history: |
| 6969 | * round 1: history of typed commands. |
| 6970 | * round 2: history from recently read viminfo. |
| 6971 | */ |
| 6972 | for (round = 1; round <= 2; ++round) |
| 6973 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6974 | if (round == 1) |
| 6975 | /* start at newest entry, somewhere in the list */ |
| 6976 | i = hisidx[type]; |
| 6977 | else if (viminfo_hisidx[type] > 0) |
| 6978 | /* start at newest entry, first in the list */ |
| 6979 | i = 0; |
| 6980 | else |
| 6981 | /* empty list */ |
| 6982 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6983 | if (i >= 0) |
| 6984 | while (num_saved > 0 |
| 6985 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6986 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6987 | char_u *p; |
| 6988 | time_t timestamp; |
| 6989 | int c = NUL; |
| 6990 | |
| 6991 | if (round == 1) |
| 6992 | { |
| 6993 | p = history[type][i].hisstr; |
| 6994 | timestamp = history[type][i].time_set; |
| 6995 | } |
| 6996 | else |
| 6997 | { |
| 6998 | p = viminfo_history[type] == NULL ? NULL |
| 6999 | : viminfo_history[type][i].hisstr; |
| 7000 | timestamp = viminfo_history[type] == NULL ? 0 |
| 7001 | : viminfo_history[type][i].time_set; |
| 7002 | } |
| 7003 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7004 | if (p != NULL && (round == 2 |
| 7005 | || !merge |
| 7006 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7007 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7008 | --num_saved; |
| 7009 | fputc(hist_type2char(type, TRUE), fp); |
| 7010 | /* For the search history: put the separator in the |
| 7011 | * second column; use a space if there isn't one. */ |
| 7012 | if (type == HIST_SEARCH) |
| 7013 | { |
| 7014 | c = p[STRLEN(p) + 1]; |
| 7015 | putc(c == NUL ? ' ' : c, fp); |
| 7016 | } |
| 7017 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7018 | |
| 7019 | { |
| 7020 | char cbuf[NUMBUFLEN]; |
| 7021 | |
| 7022 | /* New style history with a bar line. Format: |
| 7023 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 7024 | if (c == NUL) |
| 7025 | cbuf[0] = NUL; |
| 7026 | else |
| 7027 | sprintf(cbuf, "%d", c); |
| 7028 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 7029 | type, (long)timestamp, cbuf); |
| 7030 | barline_writestring(fp, p, LSIZE - 20); |
| 7031 | putc('\n', fp); |
| 7032 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7033 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7034 | if (round == 1) |
| 7035 | { |
| 7036 | /* Decrement index, loop around and stop when back at |
| 7037 | * the start. */ |
| 7038 | if (--i < 0) |
| 7039 | i = hislen - 1; |
| 7040 | if (i == hisidx[type]) |
| 7041 | break; |
| 7042 | } |
| 7043 | else |
| 7044 | { |
| 7045 | /* Increment index. Stop at the end in the while. */ |
| 7046 | ++i; |
| 7047 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7048 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7049 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 7050 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7051 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7052 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7053 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 7054 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7055 | } |
| 7056 | } |
| 7057 | #endif /* FEAT_VIMINFO */ |
| 7058 | |
| 7059 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 7060 | /* |
| 7061 | * Write a character at the current cursor+offset position. |
| 7062 | * It is directly written into the command buffer block. |
| 7063 | */ |
| 7064 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7065 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7066 | { |
| 7067 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7068 | { |
| 7069 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 7070 | return; |
| 7071 | } |
| 7072 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 7073 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 7074 | } |
| 7075 | |
| 7076 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7077 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7078 | { |
| 7079 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7080 | { |
| 7081 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 7082 | return NUL; |
| 7083 | } |
| 7084 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 7085 | } |
| 7086 | #endif |
| 7087 | |
| 7088 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 7089 | /* |
| 7090 | * Open a window on the current command line and history. Allow editing in |
| 7091 | * the window. Returns when the window is closed. |
| 7092 | * Returns: |
| 7093 | * CR if the command is to be executed |
| 7094 | * Ctrl_C if it is to be abandoned |
| 7095 | * K_IGNORE if editing continues |
| 7096 | */ |
| 7097 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7098 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7099 | { |
| 7100 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7101 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7102 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7103 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7104 | win_T *wp; |
| 7105 | int i; |
| 7106 | linenr_T lnum; |
| 7107 | int histtype; |
| 7108 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7109 | int save_restart_edit = restart_edit; |
| 7110 | int save_State = State; |
| 7111 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7112 | #ifdef FEAT_RIGHTLEFT |
| 7113 | int save_cmdmsg_rl = cmdmsg_rl; |
| 7114 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7115 | #ifdef FEAT_FOLDING |
| 7116 | int save_KeyTyped; |
| 7117 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7118 | |
| 7119 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 7120 | if (cmdwin_type != 0 |
| 7121 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 7122 | || cmdline_star > 0 |
| 7123 | # endif |
| 7124 | ) |
| 7125 | { |
| 7126 | beep_flush(); |
| 7127 | return K_IGNORE; |
| 7128 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7129 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7130 | |
| 7131 | /* Save current window sizes. */ |
| 7132 | win_size_save(&winsizes); |
| 7133 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7134 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7135 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7136 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7137 | /* don't use a new tab page */ |
| 7138 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7139 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7140 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7141 | /* Create a window for the command-line buffer. */ |
| 7142 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 7143 | { |
| 7144 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7145 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7146 | return K_IGNORE; |
| 7147 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7148 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7149 | |
| 7150 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 7151 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7152 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7153 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7154 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 7155 | #ifdef FEAT_FOLDING |
| 7156 | curwin->w_p_fen = FALSE; |
| 7157 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7158 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7159 | curwin->w_p_rl = cmdmsg_rl; |
| 7160 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7161 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 7162 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7163 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7164 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7165 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7166 | /* But don't allow switching to another buffer. */ |
| 7167 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7168 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7169 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 7170 | need_wait_return = FALSE; |
| 7171 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7172 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7173 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 7174 | { |
| 7175 | if (p_wc == TAB) |
| 7176 | { |
| 7177 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 7178 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 7179 | } |
| 7180 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 7181 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7182 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7183 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 7184 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7185 | * sets 'textwidth' to 78). */ |
| 7186 | curbuf->b_p_tw = 0; |
| 7187 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7188 | /* Fill the buffer with the history. */ |
| 7189 | init_history(); |
| 7190 | if (hislen > 0) |
| 7191 | { |
| 7192 | i = hisidx[histtype]; |
| 7193 | if (i >= 0) |
| 7194 | { |
| 7195 | lnum = 0; |
| 7196 | do |
| 7197 | { |
| 7198 | if (++i == hislen) |
| 7199 | i = 0; |
| 7200 | if (history[histtype][i].hisstr != NULL) |
| 7201 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7202 | (colnr_T)0, FALSE); |
| 7203 | } |
| 7204 | while (i != hisidx[histtype]); |
| 7205 | } |
| 7206 | } |
| 7207 | |
| 7208 | /* Replace the empty last line with the current command-line and put the |
| 7209 | * cursor there. */ |
| 7210 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7211 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7212 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7213 | changed_line_abv_curs(); |
| 7214 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7215 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7216 | |
| 7217 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7218 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | |
| 7220 | /* No Ex mode here! */ |
| 7221 | exmode_active = 0; |
| 7222 | |
| 7223 | State = NORMAL; |
| 7224 | # ifdef FEAT_MOUSE |
| 7225 | setmouse(); |
| 7226 | # endif |
| 7227 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7228 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7229 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7230 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7231 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7232 | |
| 7233 | i = RedrawingDisabled; |
| 7234 | RedrawingDisabled = 0; |
| 7235 | |
| 7236 | /* |
| 7237 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7238 | */ |
| 7239 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7240 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | |
| 7242 | RedrawingDisabled = i; |
| 7243 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7244 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7245 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7246 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7249 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7250 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7251 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7252 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7253 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7254 | # endif |
| 7255 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7256 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7257 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7258 | cmdwin_type = 0; |
| 7259 | |
| 7260 | exmode_active = save_exmode; |
| 7261 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7262 | /* 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] | 7263 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7264 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7265 | { |
| 7266 | cmdwin_result = Ctrl_C; |
| 7267 | EMSG(_("E199: Active window or buffer deleted")); |
| 7268 | } |
| 7269 | else |
| 7270 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7271 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7272 | /* autocmds may abort script processing */ |
| 7273 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7274 | cmdwin_result = Ctrl_C; |
| 7275 | # endif |
| 7276 | /* Set the new command line from the cmdline buffer. */ |
| 7277 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7278 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7279 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7280 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7281 | |
| 7282 | if (histtype == HIST_CMD) |
| 7283 | { |
| 7284 | /* Execute the command directly. */ |
| 7285 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7286 | cmdwin_result = CAR; |
| 7287 | } |
| 7288 | else |
| 7289 | { |
| 7290 | /* First need to cancel what we were doing. */ |
| 7291 | ccline.cmdbuff = NULL; |
| 7292 | stuffcharReadbuff(':'); |
| 7293 | stuffReadbuff((char_u *)p); |
| 7294 | stuffcharReadbuff(CAR); |
| 7295 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7296 | } |
| 7297 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7298 | { |
| 7299 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7300 | cmdwin_result = CAR; |
| 7301 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7302 | else if (cmdwin_result == Ctrl_C) |
| 7303 | { |
| 7304 | /* :q or :close, don't execute any command |
| 7305 | * and don't modify the cmd window. */ |
| 7306 | ccline.cmdbuff = NULL; |
| 7307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7308 | else |
| 7309 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7310 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7311 | { |
| 7312 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7313 | ccline.cmdlen = 0; |
| 7314 | ccline.cmdbufflen = 1; |
| 7315 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7316 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7317 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7318 | else |
| 7319 | { |
| 7320 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7321 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7322 | ccline.cmdpos = curwin->w_cursor.col; |
| 7323 | if (ccline.cmdpos > ccline.cmdlen) |
| 7324 | ccline.cmdpos = ccline.cmdlen; |
| 7325 | if (cmdwin_result == K_IGNORE) |
| 7326 | { |
| 7327 | set_cmdspos_cursor(); |
| 7328 | redrawcmd(); |
| 7329 | } |
| 7330 | } |
| 7331 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7332 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7333 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7334 | # ifdef FEAT_CONCEAL |
| 7335 | /* Avoid command-line window first character being concealed. */ |
| 7336 | curwin->w_p_cole = 0; |
| 7337 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7338 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7339 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7340 | win_goto(old_curwin); |
| 7341 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7342 | |
| 7343 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7344 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7345 | if (bufref_valid(&bufref)) |
| 7346 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7347 | |
| 7348 | /* Restore window sizes. */ |
| 7349 | win_size_restore(&winsizes); |
| 7350 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7351 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
| 7354 | ga_clear(&winsizes); |
| 7355 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7356 | # ifdef FEAT_RIGHTLEFT |
| 7357 | cmdmsg_rl = save_cmdmsg_rl; |
| 7358 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7359 | |
| 7360 | State = save_State; |
| 7361 | # ifdef FEAT_MOUSE |
| 7362 | setmouse(); |
| 7363 | # endif |
| 7364 | |
| 7365 | return cmdwin_result; |
| 7366 | } |
| 7367 | #endif /* FEAT_CMDWIN */ |
| 7368 | |
| 7369 | /* |
| 7370 | * Used for commands that either take a simple command string argument, or: |
| 7371 | * cmd << endmarker |
| 7372 | * {script} |
| 7373 | * endmarker |
| 7374 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7375 | */ |
| 7376 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7377 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7378 | { |
| 7379 | char_u *theline; |
| 7380 | char *end_pattern = NULL; |
| 7381 | char dot[] = "."; |
| 7382 | garray_T ga; |
| 7383 | |
| 7384 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7385 | return NULL; |
| 7386 | |
| 7387 | ga_init2(&ga, 1, 0x400); |
| 7388 | |
| 7389 | if (cmd[2] != NUL) |
| 7390 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7391 | else |
| 7392 | end_pattern = dot; |
| 7393 | |
| 7394 | for (;;) |
| 7395 | { |
| 7396 | theline = eap->getline( |
| 7397 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7398 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7399 | #endif |
| 7400 | NUL, eap->cookie, 0); |
| 7401 | |
| 7402 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7403 | { |
| 7404 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7405 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7406 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | |
| 7408 | ga_concat(&ga, theline); |
| 7409 | ga_append(&ga, '\n'); |
| 7410 | vim_free(theline); |
| 7411 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7412 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7413 | |
| 7414 | return (char_u *)ga.ga_data; |
| 7415 | } |