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 | |
Bram Moolenaar | 37b1556 | 2018-08-14 22:08:25 +0200 | [diff] [blame] | 16 | #ifndef MAX |
| 17 | # define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Variables shared between getcmdline(), redrawcmdline() and others. |
| 22 | * These need to be saved when using CTRL-R |, that's why they are in a |
| 23 | * structure. |
| 24 | */ |
| 25 | struct cmdline_info |
| 26 | { |
| 27 | char_u *cmdbuff; /* pointer to command line buffer */ |
| 28 | int cmdbufflen; /* length of cmdbuff */ |
| 29 | int cmdlen; /* number of chars in command line */ |
| 30 | int cmdpos; /* current cursor position */ |
| 31 | int cmdspos; /* cursor column on screen */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 32 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | int cmdindent; /* number of spaces before cmdline */ |
| 34 | char_u *cmdprompt; /* message in front of cmdline */ |
| 35 | int cmdattr; /* attributes for prompt */ |
| 36 | int overstrike; /* Typing mode on the command line. Shared by |
| 37 | getcmdline() and put_on_cmdline(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 38 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 39 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | int xp_context; /* type of expansion */ |
| 41 | # ifdef FEAT_EVAL |
| 42 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 43 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 44 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 47 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 48 | * used by other functions. When invoking getcmdline() recursively it needs |
| 49 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 50 | * TODO: make it local to getcmdline() and pass it around. */ |
| 51 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | |
| 53 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 54 | |
| 55 | #ifdef FEAT_EVAL |
| 56 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 57 | #endif |
| 58 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 59 | static int extra_char = NUL; /* extra character to display when redrawing |
| 60 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 61 | static int extra_char_shift; |
| 62 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | #ifdef FEAT_CMDHIST |
| 64 | typedef struct hist_entry |
| 65 | { |
| 66 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 67 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 69 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | } histentry_T; |
| 71 | |
| 72 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 73 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 74 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 75 | /* identifying (unique) number of newest history entry */ |
| 76 | static int hislen = 0; /* actual length of history tables */ |
| 77 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 78 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 80 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 82 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | # endif |
| 84 | #endif |
| 85 | |
| 86 | #ifdef FEAT_RIGHTLEFT |
| 87 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 88 | #endif |
| 89 | |
| 90 | #ifdef FEAT_FKMAP |
| 91 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 92 | #endif |
| 93 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 94 | static int cmdline_charsize(int idx); |
| 95 | static void set_cmdspos(void); |
| 96 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 98 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 100 | static void alloc_cmdbuff(int len); |
| 101 | static int realloc_cmdbuff(int len); |
| 102 | static void draw_cmdline(int start, int len); |
| 103 | static void save_cmdline(struct cmdline_info *ccp); |
| 104 | static void restore_cmdline(struct cmdline_info *ccp); |
| 105 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 107 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | #endif |
| 109 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 110 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 112 | static void redrawcmdprompt(void); |
| 113 | static void cursorcmd(void); |
| 114 | static int ccheck_abbr(int); |
| 115 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 116 | static void escape_fname(char_u **pp); |
| 117 | static int showmatches(expand_T *xp, int wildmenu); |
| 118 | static void set_expand_context(expand_T *xp); |
| 119 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 120 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 122 | 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] | 123 | 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] | 124 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 125 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 127 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 129 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 130 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | # endif |
| 132 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 133 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 134 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 135 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
| 137 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 138 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 141 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 142 | static int |
| 143 | #ifdef __BORLANDC__ |
| 144 | _RTLENTRYF |
| 145 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 146 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 150 | static void |
| 151 | trigger_cmd_autocmd(int typechar, int evt) |
| 152 | { |
| 153 | char_u typestr[2]; |
| 154 | |
| 155 | typestr[0] = typechar; |
| 156 | typestr[1] = NUL; |
| 157 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 158 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 161 | * Abandon the command line. |
| 162 | */ |
| 163 | static void |
| 164 | abandon_cmdline(void) |
| 165 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 166 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 167 | if (msg_scrolled == 0) |
| 168 | compute_cmdrow(); |
| 169 | MSG(""); |
| 170 | redraw_cmdline = TRUE; |
| 171 | } |
| 172 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 173 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 174 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 175 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 176 | * as a trailing \|, which can happen while typing a pattern. |
| 177 | */ |
| 178 | static int |
| 179 | empty_pattern(char_u *p) |
| 180 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 181 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 182 | |
| 183 | /* remove trailing \v and the like */ |
| 184 | while (n >= 2 && p[n - 2] == '\\' |
| 185 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 186 | n -= 2; |
| 187 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 188 | } |
| 189 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 190 | // Struct to store the viewstate during 'incsearch' highlighting. |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 191 | typedef struct { |
| 192 | colnr_T vs_curswant; |
| 193 | colnr_T vs_leftcol; |
| 194 | linenr_T vs_topline; |
| 195 | # ifdef FEAT_DIFF |
| 196 | int vs_topfill; |
| 197 | # endif |
| 198 | linenr_T vs_botline; |
| 199 | linenr_T vs_empty_rows; |
| 200 | } viewstate_T; |
| 201 | |
| 202 | static void |
| 203 | save_viewstate(viewstate_T *vs) |
| 204 | { |
| 205 | vs->vs_curswant = curwin->w_curswant; |
| 206 | vs->vs_leftcol = curwin->w_leftcol; |
| 207 | vs->vs_topline = curwin->w_topline; |
| 208 | # ifdef FEAT_DIFF |
| 209 | vs->vs_topfill = curwin->w_topfill; |
| 210 | # endif |
| 211 | vs->vs_botline = curwin->w_botline; |
| 212 | vs->vs_empty_rows = curwin->w_empty_rows; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | restore_viewstate(viewstate_T *vs) |
| 217 | { |
| 218 | curwin->w_curswant = vs->vs_curswant; |
| 219 | curwin->w_leftcol = vs->vs_leftcol; |
| 220 | curwin->w_topline = vs->vs_topline; |
| 221 | # ifdef FEAT_DIFF |
| 222 | curwin->w_topfill = vs->vs_topfill; |
| 223 | # endif |
| 224 | curwin->w_botline = vs->vs_botline; |
| 225 | curwin->w_empty_rows = vs->vs_empty_rows; |
| 226 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 227 | |
| 228 | // Struct to store the state of 'incsearch' highlighting. |
| 229 | typedef struct { |
| 230 | pos_T search_start; // where 'incsearch' starts searching |
| 231 | pos_T save_cursor; |
| 232 | viewstate_T init_viewstate; |
| 233 | viewstate_T old_viewstate; |
| 234 | pos_T match_start; |
| 235 | pos_T match_end; |
| 236 | int did_incsearch; |
| 237 | int incsearch_postponed; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 238 | int magic_save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 239 | } incsearch_state_T; |
| 240 | |
| 241 | static void |
| 242 | init_incsearch_state(incsearch_state_T *is_state) |
| 243 | { |
| 244 | is_state->match_start = curwin->w_cursor; |
| 245 | is_state->did_incsearch = FALSE; |
| 246 | is_state->incsearch_postponed = FALSE; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 247 | is_state->magic_save = p_magic; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 248 | CLEAR_POS(&is_state->match_end); |
| 249 | is_state->save_cursor = curwin->w_cursor; // may be restored later |
| 250 | is_state->search_start = curwin->w_cursor; |
| 251 | save_viewstate(&is_state->init_viewstate); |
| 252 | save_viewstate(&is_state->old_viewstate); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * First move cursor to end of match, then to the start. This |
| 257 | * moves the whole match onto the screen when 'nowrap' is set. |
| 258 | */ |
| 259 | static void |
| 260 | set_search_match(pos_T *t) |
| 261 | { |
| 262 | t->lnum += search_match_lines; |
| 263 | t->col = search_match_endcol; |
| 264 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 265 | { |
| 266 | t->lnum = curbuf->b_ml.ml_line_count; |
| 267 | coladvance((colnr_T)MAXCOL); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Return TRUE when 'incsearch' highlighting is to be done. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 273 | * Sets search_first_line and search_last_line to the address range. |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 274 | */ |
| 275 | static int |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 276 | do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, |
| 277 | int *skiplen, int *patlen) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 278 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 279 | char_u *cmd; |
| 280 | cmdmod_T save_cmdmod = cmdmod; |
| 281 | char_u *p; |
| 282 | int delim_optional = FALSE; |
| 283 | int delim; |
| 284 | char_u *end; |
| 285 | char_u *dummy; |
| 286 | exarg_T ea; |
| 287 | pos_T save_cursor; |
| 288 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 289 | *skiplen = 0; |
| 290 | *patlen = ccline.cmdlen; |
| 291 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 292 | if (!p_is || cmd_silent) |
| 293 | return FALSE; |
| 294 | |
| 295 | // by default search all lines |
| 296 | search_first_line = 0; |
| 297 | search_last_line = MAXLNUM; |
| 298 | |
| 299 | if (firstc == '/' || firstc == '?') |
| 300 | return TRUE; |
| 301 | if (firstc != ':') |
| 302 | return FALSE; |
| 303 | |
| 304 | vim_memset(&ea, 0, sizeof(ea)); |
| 305 | ea.line1 = 1; |
| 306 | ea.line2 = 1; |
| 307 | ea.cmd = ccline.cmdbuff; |
| 308 | ea.addr_type = ADDR_LINES; |
| 309 | |
| 310 | parse_command_modifiers(&ea, &dummy, TRUE); |
| 311 | cmdmod = save_cmdmod; |
| 312 | |
| 313 | cmd = skip_range(ea.cmd, NULL); |
| 314 | if (vim_strchr((char_u *)"sgvl", *cmd) == NULL) |
| 315 | return FALSE; |
| 316 | |
| 317 | // Skip over "substitute" to find the pattern separator. |
| 318 | for (p = cmd; ASCII_ISALPHA(*p); ++p) |
| 319 | ; |
| 320 | if (*skipwhite(p) == NUL) |
| 321 | return FALSE; |
| 322 | |
| 323 | if (STRNCMP(cmd, "substitute", p - cmd) == 0 |
| 324 | || STRNCMP(cmd, "smagic", p - cmd) == 0 |
| 325 | || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 |
| 326 | || STRNCMP(cmd, "vglobal", p - cmd) == 0) |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 327 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 328 | if (*cmd == 's' && cmd[1] == 'm') |
| 329 | p_magic = TRUE; |
| 330 | else if (*cmd == 's' && cmd[1] == 'n') |
| 331 | p_magic = FALSE; |
| 332 | } |
| 333 | else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0) |
| 334 | { |
| 335 | // skip over flags |
| 336 | while (ASCII_ISALPHA(*(p = skipwhite(p)))) |
| 337 | ++p; |
| 338 | if (*p == NUL) |
| 339 | return FALSE; |
| 340 | } |
| 341 | else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0 |
| 342 | || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0 |
| 343 | || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0 |
| 344 | || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0 |
| 345 | || STRNCMP(cmd, "global", p - cmd) == 0) |
| 346 | { |
| 347 | // skip over "!" |
| 348 | if (*p == '!') |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 349 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 350 | p++; |
| 351 | if (*skipwhite(p) == NUL) |
| 352 | return FALSE; |
| 353 | } |
| 354 | if (*cmd != 'g') |
| 355 | delim_optional = TRUE; |
| 356 | } |
| 357 | else |
| 358 | return FALSE; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 359 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 360 | p = skipwhite(p); |
| 361 | delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++; |
| 362 | end = skip_regexp(p, delim, p_magic, NULL); |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 363 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 364 | if (end == p && *end != delim) |
| 365 | return FALSE; |
| 366 | // found a non-empty pattern or // |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 367 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 368 | *skiplen = (int)(p - ccline.cmdbuff); |
| 369 | *patlen = (int)(end - p); |
Bram Moolenaar | 264cf5c | 2018-08-18 21:05:31 +0200 | [diff] [blame] | 370 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 371 | // parse the address range |
| 372 | save_cursor = curwin->w_cursor; |
| 373 | curwin->w_cursor = is_state->search_start; |
| 374 | parse_cmd_address(&ea, &dummy); |
| 375 | if (ea.addr_count > 0) |
| 376 | { |
| 377 | // Allow for reverse match. |
| 378 | if (ea.line2 < ea.line1) |
| 379 | { |
| 380 | search_first_line = ea.line2; |
| 381 | search_last_line = ea.line1; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | search_first_line = ea.line1; |
| 386 | search_last_line = ea.line2; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 387 | } |
| 388 | } |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 389 | else if (cmd[0] == 's' && cmd[1] != 'o') |
| 390 | { |
| 391 | // :s defaults to the current line |
| 392 | search_first_line = curwin->w_cursor.lnum; |
| 393 | search_last_line = curwin->w_cursor.lnum; |
| 394 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 395 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 396 | curwin->w_cursor = save_cursor; |
| 397 | return TRUE; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 400 | static void |
| 401 | finish_incsearch_highlighting( |
| 402 | int gotesc, |
| 403 | incsearch_state_T *is_state, |
| 404 | int call_update_screen) |
| 405 | { |
| 406 | if (is_state->did_incsearch) |
| 407 | { |
| 408 | is_state->did_incsearch = FALSE; |
| 409 | if (gotesc) |
| 410 | curwin->w_cursor = is_state->save_cursor; |
| 411 | else |
| 412 | { |
| 413 | if (!EQUAL_POS(is_state->save_cursor, is_state->search_start)) |
| 414 | { |
| 415 | // put the '" mark at the original position |
| 416 | curwin->w_cursor = is_state->save_cursor; |
| 417 | setpcmark(); |
| 418 | } |
| 419 | curwin->w_cursor = is_state->search_start; |
| 420 | } |
| 421 | restore_viewstate(&is_state->old_viewstate); |
| 422 | highlight_match = FALSE; |
| 423 | validate_cursor(); /* needed for TAB */ |
| 424 | if (call_update_screen) |
| 425 | update_screen(SOME_VALID); |
| 426 | else |
| 427 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 428 | p_magic = is_state->magic_save; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 432 | /* |
| 433 | * Do 'incsearch' highlighting if desired. |
| 434 | */ |
| 435 | static void |
| 436 | may_do_incsearch_highlighting( |
| 437 | int firstc, |
| 438 | long count, |
| 439 | incsearch_state_T *is_state) |
| 440 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 441 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 442 | int i; |
| 443 | pos_T end_pos; |
| 444 | struct cmdline_info save_ccline; |
| 445 | #ifdef FEAT_RELTIME |
| 446 | proftime_T tm; |
| 447 | #endif |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 448 | int next_char; |
| 449 | int use_last_pat; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 450 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 451 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 452 | { |
| 453 | finish_incsearch_highlighting(FALSE, is_state, TRUE); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 454 | return; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 455 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 456 | |
| 457 | // If there is a character waiting, search and redraw later. |
| 458 | if (char_avail()) |
| 459 | { |
| 460 | is_state->incsearch_postponed = TRUE; |
| 461 | return; |
| 462 | } |
| 463 | is_state->incsearch_postponed = FALSE; |
| 464 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 465 | if (search_first_line == 0) |
| 466 | // start at the original cursor position |
| 467 | curwin->w_cursor = is_state->search_start; |
| 468 | else |
| 469 | { |
| 470 | // start at the first line in the range |
| 471 | curwin->w_cursor.lnum = search_first_line; |
| 472 | curwin->w_cursor.col = 0; |
| 473 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 474 | save_last_search_pattern(); |
| 475 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 476 | // Use the previous pattern for ":s//". |
| 477 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 478 | use_last_pat = patlen == 0 && skiplen > 0 |
| 479 | && ccline.cmdbuff[skiplen - 1] == next_char; |
| 480 | |
| 481 | // If there is no pattern, don't do anything. |
| 482 | if (patlen == 0 && !use_last_pat) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 483 | { |
| 484 | i = 0; |
| 485 | set_no_hlsearch(TRUE); // turn off previous highlight |
| 486 | redraw_all_later(SOME_VALID); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
| 491 | |
| 492 | cursor_off(); // so the user knows we're busy |
| 493 | out_flush(); |
| 494 | ++emsg_off; // so it doesn't beep if bad expr |
| 495 | #ifdef FEAT_RELTIME |
| 496 | // Set the time limit to half a second. |
| 497 | profile_setlimit(500L, &tm); |
| 498 | #endif |
| 499 | if (!p_hls) |
| 500 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 976b847 | 2018-08-12 15:49:47 +0200 | [diff] [blame] | 501 | if (search_first_line != 0) |
| 502 | search_flags += SEARCH_START; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 503 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 504 | i = do_search(NULL, firstc == ':' ? '/' : firstc, |
| 505 | ccline.cmdbuff + skiplen, count, search_flags, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 506 | #ifdef FEAT_RELTIME |
| 507 | &tm, NULL |
| 508 | #else |
| 509 | NULL, NULL |
| 510 | #endif |
| 511 | ); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 512 | ccline.cmdbuff[skiplen + patlen] = next_char; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 513 | --emsg_off; |
| 514 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 515 | if (curwin->w_cursor.lnum < search_first_line |
| 516 | || curwin->w_cursor.lnum > search_last_line) |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 517 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 518 | // match outside of address range |
| 519 | i = 0; |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 520 | curwin->w_cursor = is_state->search_start; |
| 521 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 522 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 523 | // if interrupted while searching, behave like it failed |
| 524 | if (got_int) |
| 525 | { |
| 526 | (void)vpeekc(); // remove <C-C> from input stream |
| 527 | got_int = FALSE; // don't abandon the command line |
| 528 | i = 0; |
| 529 | } |
| 530 | else if (char_avail()) |
| 531 | // cancelled searching because a char was typed |
| 532 | is_state->incsearch_postponed = TRUE; |
| 533 | } |
| 534 | if (i != 0) |
| 535 | highlight_match = TRUE; // highlight position |
| 536 | else |
| 537 | highlight_match = FALSE; // remove highlight |
| 538 | |
| 539 | // First restore the old curwin values, so the screen is positioned in the |
| 540 | // same way as the actual search command. |
| 541 | restore_viewstate(&is_state->old_viewstate); |
| 542 | changed_cline_bef_curs(); |
| 543 | update_topline(); |
| 544 | |
| 545 | if (i != 0) |
| 546 | { |
| 547 | pos_T save_pos = curwin->w_cursor; |
| 548 | |
| 549 | is_state->match_start = curwin->w_cursor; |
| 550 | set_search_match(&curwin->w_cursor); |
| 551 | validate_cursor(); |
| 552 | end_pos = curwin->w_cursor; |
| 553 | is_state->match_end = end_pos; |
| 554 | curwin->w_cursor = save_pos; |
| 555 | } |
| 556 | else |
| 557 | end_pos = curwin->w_cursor; // shutup gcc 4 |
| 558 | |
| 559 | // Disable 'hlsearch' highlighting if the pattern matches everything. |
| 560 | // Avoids a flash when typing "foo\|". |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 561 | if (!use_last_pat) |
| 562 | { |
| 563 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 564 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 565 | if (empty_pattern(ccline.cmdbuff)) |
| 566 | set_no_hlsearch(TRUE); |
| 567 | ccline.cmdbuff[skiplen + patlen] = next_char; |
| 568 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 569 | |
| 570 | validate_cursor(); |
| 571 | // May redraw the status line to show the cursor position. |
| 572 | if (p_ru && curwin->w_status_height > 0) |
| 573 | curwin->w_redr_status = TRUE; |
| 574 | |
| 575 | save_cmdline(&save_ccline); |
| 576 | update_screen(SOME_VALID); |
| 577 | restore_cmdline(&save_ccline); |
| 578 | restore_last_search_pattern(); |
| 579 | |
| 580 | // Leave it at the end to make CTRL-R CTRL-W work. |
| 581 | if (i != 0) |
| 582 | curwin->w_cursor = end_pos; |
| 583 | |
| 584 | msg_starthere(); |
| 585 | redrawcmdline(); |
| 586 | is_state->did_incsearch = TRUE; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next |
| 591 | * or previous match. |
| 592 | * Returns FAIL when jumping to cmdline_not_changed; |
| 593 | */ |
| 594 | static int |
| 595 | may_adjust_incsearch_highlighting( |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 596 | int firstc, |
| 597 | long count, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 598 | incsearch_state_T *is_state, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 599 | int c) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 600 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 601 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 602 | pos_T t; |
| 603 | char_u *pat; |
| 604 | int search_flags = SEARCH_NOOF; |
| 605 | int i; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 606 | int save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 607 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 608 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 609 | return OK; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 610 | if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 611 | return FAIL; |
| 612 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 613 | if (firstc == ccline.cmdbuff[skiplen]) |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 614 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 615 | pat = last_search_pattern(); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 616 | skiplen = 0; |
Bram Moolenaar | d7cc163 | 2018-08-14 20:18:26 +0200 | [diff] [blame] | 617 | patlen = (int)STRLEN(pat); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 618 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 619 | else |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 620 | pat = ccline.cmdbuff + skiplen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 621 | |
| 622 | save_last_search_pattern(); |
| 623 | cursor_off(); |
| 624 | out_flush(); |
| 625 | if (c == Ctrl_G) |
| 626 | { |
| 627 | t = is_state->match_end; |
| 628 | if (LT_POS(is_state->match_start, is_state->match_end)) |
| 629 | // Start searching at the end of the match not at the beginning of |
| 630 | // the next column. |
| 631 | (void)decl(&t); |
| 632 | search_flags += SEARCH_COL; |
| 633 | } |
| 634 | else |
| 635 | t = is_state->match_start; |
| 636 | if (!p_hls) |
| 637 | search_flags += SEARCH_KEEP; |
| 638 | ++emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 639 | save = pat[patlen]; |
| 640 | pat[patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 641 | i = searchit(curwin, curbuf, &t, |
| 642 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 643 | pat, count, search_flags, |
| 644 | RE_SEARCH, 0, NULL, NULL); |
| 645 | --emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 646 | pat[patlen] = save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 647 | if (i) |
| 648 | { |
| 649 | is_state->search_start = is_state->match_start; |
| 650 | is_state->match_end = t; |
| 651 | is_state->match_start = t; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 652 | if (c == Ctrl_T && firstc != '?') |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 653 | { |
| 654 | // Move just before the current match, so that when nv_search |
| 655 | // finishes the cursor will be put back on the match. |
| 656 | is_state->search_start = t; |
| 657 | (void)decl(&is_state->search_start); |
| 658 | } |
| 659 | else if (c == Ctrl_G && firstc == '?') |
| 660 | { |
| 661 | // Move just after the current match, so that when nv_search |
| 662 | // finishes the cursor will be put back on the match. |
| 663 | is_state->search_start = t; |
| 664 | (void)incl(&is_state->search_start); |
| 665 | } |
| 666 | if (LT_POS(t, is_state->search_start) && c == Ctrl_G) |
| 667 | { |
| 668 | // wrap around |
| 669 | is_state->search_start = t; |
| 670 | if (firstc == '?') |
| 671 | (void)incl(&is_state->search_start); |
| 672 | else |
| 673 | (void)decl(&is_state->search_start); |
| 674 | } |
| 675 | |
| 676 | set_search_match(&is_state->match_end); |
| 677 | curwin->w_cursor = is_state->match_start; |
| 678 | changed_cline_bef_curs(); |
| 679 | update_topline(); |
| 680 | validate_cursor(); |
| 681 | highlight_match = TRUE; |
| 682 | save_viewstate(&is_state->old_viewstate); |
| 683 | update_screen(NOT_VALID); |
| 684 | redrawcmdline(); |
| 685 | } |
| 686 | else |
| 687 | vim_beep(BO_ERROR); |
| 688 | restore_last_search_pattern(); |
| 689 | return FAIL; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * When CTRL-L typed: add character from the match to the pattern. |
| 694 | * May set "*c" to the added character. |
| 695 | * Return OK when jumping to cmdline_not_changed. |
| 696 | */ |
| 697 | static int |
| 698 | may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state) |
| 699 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 700 | int skiplen, patlen; |
| 701 | |
| 702 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 703 | return FAIL; |
| 704 | |
| 705 | // Add a character from under the cursor for 'incsearch'. |
| 706 | if (is_state->did_incsearch) |
| 707 | { |
| 708 | curwin->w_cursor = is_state->match_end; |
| 709 | if (!EQUAL_POS(curwin->w_cursor, is_state->search_start)) |
| 710 | { |
| 711 | *c = gchar_cursor(); |
| 712 | |
| 713 | // If 'ignorecase' and 'smartcase' are set and the |
| 714 | // command line has no uppercase characters, convert |
| 715 | // the character to lowercase. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 716 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 717 | *c = MB_TOLOWER(*c); |
| 718 | if (*c != NUL) |
| 719 | { |
| 720 | if (*c == firstc || vim_strchr((char_u *)( |
| 721 | p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) |
| 722 | { |
| 723 | // put a backslash before special characters |
| 724 | stuffcharReadbuff(*c); |
| 725 | *c = '\\'; |
| 726 | } |
| 727 | return FAIL; |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | return OK; |
| 732 | } |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 733 | #endif |
| 734 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 735 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 736 | * getcmdline() - accept a command line starting with firstc. |
| 737 | * |
| 738 | * firstc == ':' get ":" command line. |
| 739 | * firstc == '/' or '?' get search pattern |
| 740 | * firstc == '=' get expression |
| 741 | * firstc == '@' get text for input() function |
| 742 | * firstc == '>' get text for debug mode |
| 743 | * firstc == NUL get text for :insert command |
| 744 | * firstc == -1 like NUL, and break on CTRL-C |
| 745 | * |
| 746 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 747 | * command line. |
| 748 | * |
| 749 | * Careful: getcmdline() can be called recursively! |
| 750 | * |
| 751 | * Return pointer to allocated string if there is a commandline, NULL |
| 752 | * otherwise. |
| 753 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 755 | getcmdline( |
| 756 | int firstc, |
| 757 | long count UNUSED, /* only used for incremental search */ |
| 758 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | { |
| 760 | int c; |
| 761 | int i; |
| 762 | int j; |
| 763 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 764 | int do_abbr; /* when TRUE check for abbr. */ |
| 765 | #ifdef FEAT_CMDHIST |
| 766 | char_u *lookfor = NULL; /* string to match */ |
| 767 | int hiscnt; /* current history line in use */ |
| 768 | int histype; /* history type to be used */ |
| 769 | #endif |
| 770 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 771 | incsearch_state_T is_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 772 | #endif |
| 773 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 774 | int wim_index = 0; /* index in wim_flags[] */ |
| 775 | int res; |
| 776 | int save_msg_scroll = msg_scroll; |
| 777 | int save_State = State; /* remember State when called */ |
| 778 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 779 | #ifdef FEAT_MOUSE |
| 780 | /* mouse drag and release events are ignored, unless they are |
| 781 | * preceded with a mouse down event */ |
| 782 | int ignore_drag_release = TRUE; |
| 783 | #endif |
| 784 | #ifdef FEAT_EVAL |
| 785 | int break_ctrl_c = FALSE; |
| 786 | #endif |
| 787 | expand_T xpc; |
| 788 | long *b_im_ptr = NULL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 789 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 790 | /* Everything that may work recursively should save and restore the |
| 791 | * current command line in save_ccline. That includes update_screen(), a |
| 792 | * custom status line may invoke ":normal". */ |
| 793 | struct cmdline_info save_ccline; |
| 794 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 795 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 797 | #ifdef FEAT_EVAL |
| 798 | if (firstc == -1) |
| 799 | { |
| 800 | firstc = NUL; |
| 801 | break_ctrl_c = TRUE; |
| 802 | } |
| 803 | #endif |
| 804 | #ifdef FEAT_RIGHTLEFT |
| 805 | /* start without Hebrew mapping for a command line */ |
| 806 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 807 | cmd_hkmap = 0; |
| 808 | #endif |
| 809 | |
| 810 | ccline.overstrike = FALSE; /* always start in insert mode */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 811 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 812 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 813 | init_incsearch_state(&is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | #endif |
| 815 | |
| 816 | /* |
| 817 | * set some variables for redrawcmd() |
| 818 | */ |
| 819 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 820 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 821 | |
| 822 | /* alloc initial ccline.cmdbuff */ |
| 823 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | if (ccline.cmdbuff == NULL) |
| 825 | return NULL; /* out of memory */ |
| 826 | ccline.cmdlen = ccline.cmdpos = 0; |
| 827 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 828 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 830 | /* autoindent for :insert and :append */ |
| 831 | if (firstc <= 0) |
| 832 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 833 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 834 | ccline.cmdbuff[indent] = NUL; |
| 835 | ccline.cmdpos = indent; |
| 836 | ccline.cmdspos = indent; |
| 837 | ccline.cmdlen = indent; |
| 838 | } |
| 839 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 841 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | |
| 843 | #ifdef FEAT_RIGHTLEFT |
| 844 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 845 | && (firstc == '/' || firstc == '?')) |
| 846 | cmdmsg_rl = TRUE; |
| 847 | else |
| 848 | cmdmsg_rl = FALSE; |
| 849 | #endif |
| 850 | |
| 851 | redir_off = TRUE; /* don't redirect the typed command */ |
| 852 | if (!cmd_silent) |
| 853 | { |
| 854 | i = msg_scrolled; |
| 855 | msg_scrolled = 0; /* avoid wait_return message */ |
| 856 | gotocmdline(TRUE); |
| 857 | msg_scrolled += i; |
| 858 | redrawcmdprompt(); /* draw prompt or indent */ |
| 859 | set_cmdspos(); |
| 860 | } |
| 861 | xpc.xp_context = EXPAND_NOTHING; |
| 862 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 863 | #ifndef BACKSLASH_IN_FILENAME |
| 864 | xpc.xp_shell = FALSE; |
| 865 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 867 | #if defined(FEAT_EVAL) |
| 868 | if (ccline.input_fn) |
| 869 | { |
| 870 | xpc.xp_context = ccline.xp_context; |
| 871 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 872 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 873 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 874 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 875 | } |
| 876 | #endif |
| 877 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 878 | /* |
| 879 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 880 | * doing ":@0" when register 0 doesn't contain a CR. |
| 881 | */ |
| 882 | msg_scroll = FALSE; |
| 883 | |
| 884 | State = CMDLINE; |
| 885 | |
| 886 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 887 | { |
| 888 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 889 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 890 | b_im_ptr = &curbuf->b_p_iminsert; |
| 891 | else |
| 892 | b_im_ptr = &curbuf->b_p_imsearch; |
| 893 | if (*b_im_ptr == B_IMODE_LMAP) |
| 894 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 895 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 896 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 897 | #endif |
| 898 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 899 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 900 | else if (p_imcmdline) |
| 901 | im_set_active(TRUE); |
| 902 | #endif |
| 903 | |
| 904 | #ifdef FEAT_MOUSE |
| 905 | setmouse(); |
| 906 | #endif |
| 907 | #ifdef CURSOR_SHAPE |
| 908 | ui_cursor_shape(); /* may show different cursor shape */ |
| 909 | #endif |
| 910 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 911 | /* When inside an autocommand for writing "exiting" may be set and |
| 912 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 913 | settmode(TMODE_RAW); |
| 914 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 915 | /* Trigger CmdlineEnter autocommands. */ |
| 916 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 917 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 918 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 919 | #ifdef FEAT_CMDHIST |
| 920 | init_history(); |
| 921 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 922 | histype = hist_char2type(firstc); |
| 923 | #endif |
| 924 | |
| 925 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 926 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | #endif |
| 928 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 929 | /* If something above caused an error, reset the flags, we do want to type |
| 930 | * and execute commands. Display may be messed up a bit. */ |
| 931 | if (did_emsg) |
| 932 | redrawcmd(); |
| 933 | did_emsg = FALSE; |
| 934 | got_int = FALSE; |
| 935 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 936 | /* |
| 937 | * Collect the command string, handling editing keys. |
| 938 | */ |
| 939 | for (;;) |
| 940 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 941 | redir_off = TRUE; /* Don't redirect the typed command. |
| 942 | Repeated, because a ":redir" inside |
| 943 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 944 | #ifdef USE_ON_FLY_SCROLL |
| 945 | dont_scroll = FALSE; /* allow scrolling here */ |
| 946 | #endif |
| 947 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 948 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 949 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 950 | that occurs while typing a command should |
| 951 | cause the command not to be executed. */ |
| 952 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 954 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 955 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 956 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 957 | do |
| 958 | { |
| 959 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 960 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 961 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 962 | if (KeyTyped) |
| 963 | { |
| 964 | some_key_typed = TRUE; |
| 965 | #ifdef FEAT_RIGHTLEFT |
| 966 | if (cmd_hkmap) |
| 967 | c = hkmap(c); |
| 968 | # ifdef FEAT_FKMAP |
| 969 | if (cmd_fkmap) |
| 970 | c = cmdl_fkmap(c); |
| 971 | # endif |
| 972 | if (cmdmsg_rl && !KeyStuffed) |
| 973 | { |
| 974 | /* Invert horizontal movements and operations. Only when |
| 975 | * typed by the user directly, not when the result of a |
| 976 | * mapping. */ |
| 977 | switch (c) |
| 978 | { |
| 979 | case K_RIGHT: c = K_LEFT; break; |
| 980 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 981 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 982 | case K_LEFT: c = K_RIGHT; break; |
| 983 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 984 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 985 | } |
| 986 | } |
| 987 | #endif |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Ignore got_int when CTRL-C was typed here. |
| 992 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 993 | * ":g/pat/normal /pat" (without the <CR>). |
| 994 | * Don't ignore it for the input() function. |
| 995 | */ |
| 996 | if ((c == Ctrl_C |
| 997 | #ifdef UNIX |
| 998 | || c == intr_char |
| 999 | #endif |
| 1000 | ) |
| 1001 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 1002 | && firstc != '@' |
| 1003 | #endif |
| 1004 | #ifdef FEAT_EVAL |
| 1005 | && !break_ctrl_c |
| 1006 | #endif |
| 1007 | && !global_busy) |
| 1008 | got_int = FALSE; |
| 1009 | |
| 1010 | #ifdef FEAT_CMDHIST |
| 1011 | /* free old command line when finished moving around in the history |
| 1012 | * list */ |
| 1013 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1014 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1015 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 1017 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1018 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1019 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1020 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | #endif |
| 1022 | |
| 1023 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1024 | * When there are matching completions to select <S-Tab> works like |
| 1025 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1027 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1028 | c = Ctrl_P; |
| 1029 | |
| 1030 | #ifdef FEAT_WILDMENU |
| 1031 | /* Special translations for 'wildmenu' */ |
| 1032 | if (did_wild_list && p_wmnu) |
| 1033 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1034 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1035 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1036 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | c = Ctrl_N; |
| 1038 | } |
| 1039 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 1040 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 1041 | && ccline.cmdpos > 1 |
| 1042 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 1043 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 1044 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 1045 | c = K_DOWN; |
| 1046 | #endif |
| 1047 | |
| 1048 | /* free expanded names when finished walking through matches */ |
| 1049 | if (xpc.xp_numfiles != -1 |
| 1050 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 1051 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 1052 | && c != Ctrl_L) |
| 1053 | { |
| 1054 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1055 | did_wild_list = FALSE; |
| 1056 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1057 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1058 | #endif |
| 1059 | xpc.xp_context = EXPAND_NOTHING; |
| 1060 | wim_index = 0; |
| 1061 | #ifdef FEAT_WILDMENU |
| 1062 | if (p_wmnu && wild_menu_showing != 0) |
| 1063 | { |
| 1064 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1065 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1068 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (wild_menu_showing == WM_SCROLLED) |
| 1071 | { |
| 1072 | /* Entered command line, move it up */ |
| 1073 | cmdline_row--; |
| 1074 | redrawcmd(); |
| 1075 | } |
| 1076 | else if (save_p_ls != -1) |
| 1077 | { |
| 1078 | /* restore 'laststatus' and 'winminheight' */ |
| 1079 | p_ls = save_p_ls; |
| 1080 | p_wmh = save_p_wmh; |
| 1081 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1082 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1084 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1085 | redrawcmd(); |
| 1086 | save_p_ls = -1; |
| 1087 | } |
| 1088 | else |
| 1089 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1091 | redraw_statuslines(); |
| 1092 | } |
| 1093 | KeyTyped = skt; |
| 1094 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1095 | if (ccline.input_fn) |
| 1096 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1097 | } |
| 1098 | #endif |
| 1099 | } |
| 1100 | |
| 1101 | #ifdef FEAT_WILDMENU |
| 1102 | /* Special translations for 'wildmenu' */ |
| 1103 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 1104 | { |
| 1105 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1106 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 1107 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1109 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | { |
| 1111 | /* Hitting <Up>: Remove one submenu name in front of the |
| 1112 | * cursor */ |
| 1113 | int found = FALSE; |
| 1114 | |
| 1115 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1116 | i = 0; |
| 1117 | while (--j > 0) |
| 1118 | { |
| 1119 | /* check for start of menu name */ |
| 1120 | if (ccline.cmdbuff[j] == ' ' |
| 1121 | && ccline.cmdbuff[j - 1] != '\\') |
| 1122 | { |
| 1123 | i = j + 1; |
| 1124 | break; |
| 1125 | } |
| 1126 | /* check for start of submenu name */ |
| 1127 | if (ccline.cmdbuff[j] == '.' |
| 1128 | && ccline.cmdbuff[j - 1] != '\\') |
| 1129 | { |
| 1130 | if (found) |
| 1131 | { |
| 1132 | i = j + 1; |
| 1133 | break; |
| 1134 | } |
| 1135 | else |
| 1136 | found = TRUE; |
| 1137 | } |
| 1138 | } |
| 1139 | if (i > 0) |
| 1140 | cmdline_del(i); |
| 1141 | c = p_wc; |
| 1142 | xpc.xp_context = EXPAND_NOTHING; |
| 1143 | } |
| 1144 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1145 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1146 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1147 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1148 | { |
| 1149 | char_u upseg[5]; |
| 1150 | |
| 1151 | upseg[0] = PATHSEP; |
| 1152 | upseg[1] = '.'; |
| 1153 | upseg[2] = '.'; |
| 1154 | upseg[3] = PATHSEP; |
| 1155 | upseg[4] = NUL; |
| 1156 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1157 | if (c == K_DOWN |
| 1158 | && ccline.cmdpos > 0 |
| 1159 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 1160 | && (ccline.cmdpos < 3 |
| 1161 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1162 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 1163 | { |
| 1164 | /* go down a directory */ |
| 1165 | c = p_wc; |
| 1166 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1167 | 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] | 1168 | { |
| 1169 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 1170 | int found = FALSE; |
| 1171 | |
| 1172 | j = ccline.cmdpos; |
| 1173 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1174 | while (--j > i) |
| 1175 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1176 | #ifdef FEAT_MBYTE |
| 1177 | if (has_mbyte) |
| 1178 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1179 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1180 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 1181 | { |
| 1182 | found = TRUE; |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | if (found |
| 1187 | && ccline.cmdbuff[j - 1] == '.' |
| 1188 | && ccline.cmdbuff[j - 2] == '.' |
| 1189 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 1190 | { |
| 1191 | cmdline_del(j - 2); |
| 1192 | c = p_wc; |
| 1193 | } |
| 1194 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1195 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1196 | { |
| 1197 | /* go up a directory */ |
| 1198 | int found = FALSE; |
| 1199 | |
| 1200 | j = ccline.cmdpos - 1; |
| 1201 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1202 | while (--j > i) |
| 1203 | { |
| 1204 | #ifdef FEAT_MBYTE |
| 1205 | if (has_mbyte) |
| 1206 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1207 | #endif |
| 1208 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 1209 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1210 | && vim_strchr((char_u *)" *?[{`$%#", |
| 1211 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1212 | #endif |
| 1213 | ) |
| 1214 | { |
| 1215 | if (found) |
| 1216 | { |
| 1217 | i = j + 1; |
| 1218 | break; |
| 1219 | } |
| 1220 | else |
| 1221 | found = TRUE; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (!found) |
| 1226 | j = i; |
| 1227 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 1228 | j += 4; |
| 1229 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 1230 | && j == i) |
| 1231 | j += 3; |
| 1232 | else |
| 1233 | j = 0; |
| 1234 | if (j > 0) |
| 1235 | { |
| 1236 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 1237 | * machine-specific stuff here and in upseg init */ |
| 1238 | cmdline_del(j); |
| 1239 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 1240 | } |
| 1241 | else if (ccline.cmdpos > i) |
| 1242 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1243 | |
| 1244 | /* Now complete in the new directory. Set KeyTyped in case the |
| 1245 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1247 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | |
| 1251 | #endif /* FEAT_WILDMENU */ |
| 1252 | |
| 1253 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 1254 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 1255 | if (c == Ctrl_BSL) |
| 1256 | { |
| 1257 | ++no_mapping; |
| 1258 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1259 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | --no_mapping; |
| 1261 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 1262 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 1263 | * is in a mapping. */ |
| 1264 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 1265 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | { |
| 1267 | vungetc(c); |
| 1268 | c = Ctrl_BSL; |
| 1269 | } |
| 1270 | #ifdef FEAT_EVAL |
| 1271 | else if (c == 'e') |
| 1272 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 1273 | char_u *p = NULL; |
| 1274 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | |
| 1276 | /* |
| 1277 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1278 | * Need to save and restore the current command line, to be |
| 1279 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1280 | */ |
| 1281 | if (ccline.cmdpos == ccline.cmdlen) |
| 1282 | new_cmdpos = 99999; /* keep it at the end */ |
| 1283 | else |
| 1284 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1285 | |
| 1286 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1287 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1288 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | if (c == '=') |
| 1290 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1291 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1292 | * to avoid nasty things like going to another buffer when |
| 1293 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1294 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1295 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1296 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1297 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1298 | restore_cmdline(&save_ccline); |
| 1299 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1300 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1302 | len = (int)STRLEN(p); |
| 1303 | if (realloc_cmdbuff(len + 1) == OK) |
| 1304 | { |
| 1305 | ccline.cmdlen = len; |
| 1306 | STRCPY(ccline.cmdbuff, p); |
| 1307 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1309 | /* Restore the cursor or use the position set with |
| 1310 | * set_cmdline_pos(). */ |
| 1311 | if (new_cmdpos > ccline.cmdlen) |
| 1312 | ccline.cmdpos = ccline.cmdlen; |
| 1313 | else |
| 1314 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1315 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1316 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1317 | redrawcmd(); |
| 1318 | goto cmdline_changed; |
| 1319 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 1323 | got_int = FALSE; /* don't abandon the command line */ |
| 1324 | did_emsg = FALSE; |
| 1325 | emsg_on_display = FALSE; |
| 1326 | redrawcmd(); |
| 1327 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | } |
| 1329 | #endif |
| 1330 | else |
| 1331 | { |
| 1332 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 1333 | restart_edit = 'a'; |
| 1334 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 1335 | in history */ |
| 1336 | goto returncmd; /* back to Normal mode */ |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | #ifdef FEAT_CMDWIN |
| 1341 | if (c == cedit_key || c == K_CMDWIN) |
| 1342 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1343 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 1344 | { |
| 1345 | /* |
| 1346 | * Open a window to edit the command line (and history). |
| 1347 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1348 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1349 | some_key_typed = TRUE; |
| 1350 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1351 | } |
| 1352 | # ifdef FEAT_DIGRAPHS |
| 1353 | else |
| 1354 | # endif |
| 1355 | #endif |
| 1356 | #ifdef FEAT_DIGRAPHS |
| 1357 | c = do_digraph(c); |
| 1358 | #endif |
| 1359 | |
| 1360 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 1361 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 1362 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1363 | /* In Ex mode a backslash escapes a newline. */ |
| 1364 | if (exmode_active |
| 1365 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1366 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1367 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1368 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1369 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1370 | if (c == K_KENTER) |
| 1371 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1373 | else |
| 1374 | { |
| 1375 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 1376 | truncate the cmdline now. */ |
| 1377 | if (ccheck_abbr(c + ABBR_OFF)) |
| 1378 | goto cmdline_changed; |
| 1379 | if (!cmd_silent) |
| 1380 | { |
| 1381 | windgoto(msg_row, 0); |
| 1382 | out_flush(); |
| 1383 | } |
| 1384 | break; |
| 1385 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Completion for 'wildchar' or 'wildcharm' key. |
| 1390 | * - hitting <ESC> twice means: abandon command line. |
| 1391 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 1392 | * typed, not when it comes from a macro |
| 1393 | */ |
| 1394 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 1395 | { |
| 1396 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 1397 | { |
| 1398 | /* if 'wildmode' contains "list" may still need to list */ |
| 1399 | if (xpc.xp_numfiles > 1 |
| 1400 | && !did_wild_list |
| 1401 | && (wim_flags[wim_index] & WIM_LIST)) |
| 1402 | { |
| 1403 | (void)showmatches(&xpc, FALSE); |
| 1404 | redrawcmd(); |
| 1405 | did_wild_list = TRUE; |
| 1406 | } |
| 1407 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1408 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1409 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1410 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1411 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1412 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | else |
| 1414 | res = OK; /* don't insert 'wildchar' now */ |
| 1415 | } |
| 1416 | else /* typed p_wc first time */ |
| 1417 | { |
| 1418 | wim_index = 0; |
| 1419 | j = ccline.cmdpos; |
| 1420 | /* if 'wildmode' first contains "longest", get longest |
| 1421 | * common part */ |
| 1422 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1423 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1424 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1426 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 1427 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1428 | |
| 1429 | /* if interrupted while completing, behave like it failed */ |
| 1430 | if (got_int) |
| 1431 | { |
| 1432 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1433 | got_int = FALSE; /* don't abandon the command line */ |
| 1434 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1435 | #ifdef FEAT_WILDMENU |
| 1436 | xpc.xp_context = EXPAND_NOTHING; |
| 1437 | #endif |
| 1438 | goto cmdline_changed; |
| 1439 | } |
| 1440 | |
| 1441 | /* when more than one match, and 'wildmode' first contains |
| 1442 | * "list", or no change and 'wildmode' contains "longest,list", |
| 1443 | * list all matches */ |
| 1444 | if (res == OK && xpc.xp_numfiles > 1) |
| 1445 | { |
| 1446 | /* a "longest" that didn't do anything is skipped (but not |
| 1447 | * "list:longest") */ |
| 1448 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 1449 | wim_index = 1; |
| 1450 | if ((wim_flags[wim_index] & WIM_LIST) |
| 1451 | #ifdef FEAT_WILDMENU |
| 1452 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 1453 | #endif |
| 1454 | ) |
| 1455 | { |
| 1456 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 1457 | { |
| 1458 | #ifdef FEAT_WILDMENU |
| 1459 | int p_wmnu_save = p_wmnu; |
| 1460 | p_wmnu = 0; |
| 1461 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1462 | /* remove match */ |
| 1463 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1464 | #ifdef FEAT_WILDMENU |
| 1465 | p_wmnu = p_wmnu_save; |
| 1466 | #endif |
| 1467 | } |
| 1468 | #ifdef FEAT_WILDMENU |
| 1469 | (void)showmatches(&xpc, p_wmnu |
| 1470 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 1471 | #else |
| 1472 | (void)showmatches(&xpc, FALSE); |
| 1473 | #endif |
| 1474 | redrawcmd(); |
| 1475 | did_wild_list = TRUE; |
| 1476 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1477 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1478 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1479 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1480 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1481 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1482 | } |
| 1483 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1484 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | } |
| 1486 | #ifdef FEAT_WILDMENU |
| 1487 | else if (xpc.xp_numfiles == -1) |
| 1488 | xpc.xp_context = EXPAND_NOTHING; |
| 1489 | #endif |
| 1490 | } |
| 1491 | if (wim_index < 3) |
| 1492 | ++wim_index; |
| 1493 | if (c == ESC) |
| 1494 | gotesc = TRUE; |
| 1495 | if (res == OK) |
| 1496 | goto cmdline_changed; |
| 1497 | } |
| 1498 | |
| 1499 | gotesc = FALSE; |
| 1500 | |
| 1501 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 1502 | if (c == K_S_TAB && KeyTyped) |
| 1503 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1504 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 1505 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 1506 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1507 | goto cmdline_changed; |
| 1508 | } |
| 1509 | |
| 1510 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 1511 | c = NL; |
| 1512 | |
| 1513 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1514 | |
| 1515 | /* |
| 1516 | * Big switch for a typed command line character. |
| 1517 | */ |
| 1518 | switch (c) |
| 1519 | { |
| 1520 | case K_BS: |
| 1521 | case Ctrl_H: |
| 1522 | case K_DEL: |
| 1523 | case K_KDEL: |
| 1524 | case Ctrl_W: |
| 1525 | #ifdef FEAT_FKMAP |
| 1526 | if (cmd_fkmap && c == K_BS) |
| 1527 | c = K_DEL; |
| 1528 | #endif |
| 1529 | if (c == K_KDEL) |
| 1530 | c = K_DEL; |
| 1531 | |
| 1532 | /* |
| 1533 | * delete current character is the same as backspace on next |
| 1534 | * character, except at end of line |
| 1535 | */ |
| 1536 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1537 | ++ccline.cmdpos; |
| 1538 | #ifdef FEAT_MBYTE |
| 1539 | if (has_mbyte && c == K_DEL) |
| 1540 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1541 | ccline.cmdbuff + ccline.cmdpos); |
| 1542 | #endif |
| 1543 | if (ccline.cmdpos > 0) |
| 1544 | { |
| 1545 | char_u *p; |
| 1546 | |
| 1547 | j = ccline.cmdpos; |
| 1548 | p = ccline.cmdbuff + j; |
| 1549 | #ifdef FEAT_MBYTE |
| 1550 | if (has_mbyte) |
| 1551 | { |
| 1552 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1553 | if (c == Ctrl_W) |
| 1554 | { |
| 1555 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1556 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1557 | i = mb_get_class(p); |
| 1558 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1559 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1560 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1561 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | else |
| 1565 | #endif |
| 1566 | if (c == Ctrl_W) |
| 1567 | { |
| 1568 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1569 | --p; |
| 1570 | i = vim_iswordc(p[-1]); |
| 1571 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1572 | && vim_iswordc(p[-1]) == i) |
| 1573 | --p; |
| 1574 | } |
| 1575 | else |
| 1576 | --p; |
| 1577 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1578 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1579 | i = ccline.cmdpos; |
| 1580 | while (i < ccline.cmdlen) |
| 1581 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1582 | |
| 1583 | /* Truncate at the end, required for multi-byte chars. */ |
| 1584 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1585 | #ifdef FEAT_SEARCH_EXTRA |
| 1586 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1587 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1588 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1589 | /* save view settings, so that the screen |
| 1590 | * won't be restored at the wrong position */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1591 | is_state.old_viewstate = is_state.init_viewstate; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1592 | } |
| 1593 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1594 | redrawcmd(); |
| 1595 | } |
| 1596 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1597 | && ccline.cmdprompt == NULL && indent == 0) |
| 1598 | { |
| 1599 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1600 | if (exmode_active |
| 1601 | #ifdef FEAT_EVAL |
| 1602 | || ccline.cmdfirstc == '>' |
| 1603 | #endif |
| 1604 | ) |
| 1605 | goto cmdline_not_changed; |
| 1606 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1607 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1608 | if (!cmd_silent) |
| 1609 | { |
| 1610 | #ifdef FEAT_RIGHTLEFT |
| 1611 | if (cmdmsg_rl) |
| 1612 | msg_col = Columns; |
| 1613 | else |
| 1614 | #endif |
| 1615 | msg_col = 0; |
| 1616 | msg_putchar(' '); /* delete ':' */ |
| 1617 | } |
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 | redraw_cmdline = TRUE; |
| 1623 | goto returncmd; /* back to cmd mode */ |
| 1624 | } |
| 1625 | goto cmdline_changed; |
| 1626 | |
| 1627 | case K_INS: |
| 1628 | case K_KINS: |
| 1629 | #ifdef FEAT_FKMAP |
| 1630 | /* if Farsi mode set, we are in reverse insert mode - |
| 1631 | Do not change the mode */ |
| 1632 | if (cmd_fkmap) |
| 1633 | beep_flush(); |
| 1634 | else |
| 1635 | #endif |
| 1636 | ccline.overstrike = !ccline.overstrike; |
| 1637 | #ifdef CURSOR_SHAPE |
| 1638 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1639 | #endif |
| 1640 | goto cmdline_not_changed; |
| 1641 | |
| 1642 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1643 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1644 | { |
| 1645 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1646 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1647 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1648 | im_set_active(FALSE); /* Disable input method */ |
| 1649 | #endif |
| 1650 | if (b_im_ptr != NULL) |
| 1651 | { |
| 1652 | if (State & LANGMAP) |
| 1653 | *b_im_ptr = B_IMODE_LMAP; |
| 1654 | else |
| 1655 | *b_im_ptr = B_IMODE_NONE; |
| 1656 | } |
| 1657 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1658 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1659 | else |
| 1660 | { |
| 1661 | /* There are no ":lmap" mappings, toggle IM. When |
| 1662 | * 'imdisable' is set don't try getting the status, it's |
| 1663 | * always off. */ |
| 1664 | if ((p_imdisable && b_im_ptr != NULL) |
| 1665 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1666 | { |
| 1667 | im_set_active(FALSE); /* Disable input method */ |
| 1668 | if (b_im_ptr != NULL) |
| 1669 | *b_im_ptr = B_IMODE_NONE; |
| 1670 | } |
| 1671 | else |
| 1672 | { |
| 1673 | im_set_active(TRUE); /* Enable input method */ |
| 1674 | if (b_im_ptr != NULL) |
| 1675 | *b_im_ptr = B_IMODE_IM; |
| 1676 | } |
| 1677 | } |
| 1678 | #endif |
| 1679 | if (b_im_ptr != NULL) |
| 1680 | { |
| 1681 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1682 | set_iminsert_global(); |
| 1683 | else |
| 1684 | set_imsearch_global(); |
| 1685 | } |
| 1686 | #ifdef CURSOR_SHAPE |
| 1687 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1688 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1689 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1690 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1691 | status_redraw_curbuf(); |
| 1692 | #endif |
| 1693 | goto cmdline_not_changed; |
| 1694 | |
| 1695 | /* case '@': only in very old vi */ |
| 1696 | case Ctrl_U: |
| 1697 | /* delete all characters left of the cursor */ |
| 1698 | j = ccline.cmdpos; |
| 1699 | ccline.cmdlen -= j; |
| 1700 | i = ccline.cmdpos = 0; |
| 1701 | while (i < ccline.cmdlen) |
| 1702 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1703 | /* Truncate at the end, required for multi-byte chars. */ |
| 1704 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1705 | #ifdef FEAT_SEARCH_EXTRA |
| 1706 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1707 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1708 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1709 | redrawcmd(); |
| 1710 | goto cmdline_changed; |
| 1711 | |
| 1712 | #ifdef FEAT_CLIPBOARD |
| 1713 | case Ctrl_Y: |
| 1714 | /* Copy the modeless selection, if there is one. */ |
| 1715 | if (clip_star.state != SELECT_CLEARED) |
| 1716 | { |
| 1717 | if (clip_star.state == SELECT_DONE) |
| 1718 | clip_copy_modeless_selection(TRUE); |
| 1719 | goto cmdline_not_changed; |
| 1720 | } |
| 1721 | break; |
| 1722 | #endif |
| 1723 | |
| 1724 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1725 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1726 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1727 | * ":normal" runs out of characters. */ |
| 1728 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1729 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | goto cmdline_not_changed; |
| 1731 | |
| 1732 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1733 | putting it in history */ |
| 1734 | goto returncmd; /* back to cmd mode */ |
| 1735 | |
| 1736 | case Ctrl_R: /* insert register */ |
| 1737 | #ifdef USE_ON_FLY_SCROLL |
| 1738 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1739 | #endif |
| 1740 | putcmdline('"', TRUE); |
| 1741 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1742 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1743 | if (i == Ctrl_O) |
| 1744 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1745 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1746 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1747 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | --no_mapping; |
| 1749 | #ifdef FEAT_EVAL |
| 1750 | /* |
| 1751 | * Insert the result of an expression. |
| 1752 | * Need to save the current command line, to be able to enter |
| 1753 | * a new one... |
| 1754 | */ |
| 1755 | new_cmdpos = -1; |
| 1756 | if (c == '=') |
| 1757 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1758 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1759 | { |
| 1760 | beep_flush(); |
| 1761 | c = ESC; |
| 1762 | } |
| 1763 | else |
| 1764 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1765 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1767 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | #endif |
| 1771 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1772 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1773 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1774 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1775 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1776 | /* When there was a serious error abort getting the |
| 1777 | * command line. */ |
| 1778 | if (aborting()) |
| 1779 | { |
| 1780 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1781 | putting it in history */ |
| 1782 | goto returncmd; /* back to cmd mode */ |
| 1783 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1784 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1786 | #ifdef FEAT_EVAL |
| 1787 | if (new_cmdpos >= 0) |
| 1788 | { |
| 1789 | /* set_cmdline_pos() was used */ |
| 1790 | if (new_cmdpos > ccline.cmdlen) |
| 1791 | ccline.cmdpos = ccline.cmdlen; |
| 1792 | else |
| 1793 | ccline.cmdpos = new_cmdpos; |
| 1794 | } |
| 1795 | #endif |
| 1796 | } |
| 1797 | redrawcmd(); |
| 1798 | goto cmdline_changed; |
| 1799 | |
| 1800 | case Ctrl_D: |
| 1801 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1802 | break; /* Use ^D as normal char instead */ |
| 1803 | |
| 1804 | redrawcmd(); |
| 1805 | continue; /* don't do incremental search now */ |
| 1806 | |
| 1807 | case K_RIGHT: |
| 1808 | case K_S_RIGHT: |
| 1809 | case K_C_RIGHT: |
| 1810 | do |
| 1811 | { |
| 1812 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1813 | break; |
| 1814 | i = cmdline_charsize(ccline.cmdpos); |
| 1815 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1816 | break; |
| 1817 | ccline.cmdspos += i; |
| 1818 | #ifdef FEAT_MBYTE |
| 1819 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1820 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | + ccline.cmdpos); |
| 1822 | else |
| 1823 | #endif |
| 1824 | ++ccline.cmdpos; |
| 1825 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1826 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1827 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1828 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1829 | #ifdef FEAT_MBYTE |
| 1830 | if (has_mbyte) |
| 1831 | set_cmdspos_cursor(); |
| 1832 | #endif |
| 1833 | goto cmdline_not_changed; |
| 1834 | |
| 1835 | case K_LEFT: |
| 1836 | case K_S_LEFT: |
| 1837 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1838 | if (ccline.cmdpos == 0) |
| 1839 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1840 | do |
| 1841 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | --ccline.cmdpos; |
| 1843 | #ifdef FEAT_MBYTE |
| 1844 | if (has_mbyte) /* move to first byte of char */ |
| 1845 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1846 | ccline.cmdbuff + ccline.cmdpos); |
| 1847 | #endif |
| 1848 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1849 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1850 | while (ccline.cmdpos > 0 |
| 1851 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1852 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1854 | #ifdef FEAT_MBYTE |
| 1855 | if (has_mbyte) |
| 1856 | set_cmdspos_cursor(); |
| 1857 | #endif |
| 1858 | goto cmdline_not_changed; |
| 1859 | |
| 1860 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1861 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1862 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1863 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1864 | #ifdef FEAT_GUI_W32 |
| 1865 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1866 | * cancelled. */ |
| 1867 | case K_F4: |
| 1868 | if (mod_mask == MOD_MASK_ALT) |
| 1869 | { |
| 1870 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1871 | goto cmdline_not_changed; |
| 1872 | } |
| 1873 | break; |
| 1874 | #endif |
| 1875 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1876 | #ifdef FEAT_MOUSE |
| 1877 | case K_MIDDLEDRAG: |
| 1878 | case K_MIDDLERELEASE: |
| 1879 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1880 | |
| 1881 | case K_MIDDLEMOUSE: |
| 1882 | # ifdef FEAT_GUI |
| 1883 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1884 | if (!gui.in_use) |
| 1885 | # endif |
| 1886 | if (!mouse_has(MOUSE_COMMAND)) |
| 1887 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1888 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1889 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1890 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1891 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1892 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1893 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | redrawcmd(); |
| 1895 | goto cmdline_changed; |
| 1896 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1897 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1898 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1899 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1900 | redrawcmd(); |
| 1901 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1902 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1903 | |
| 1904 | case K_LEFTDRAG: |
| 1905 | case K_LEFTRELEASE: |
| 1906 | case K_RIGHTDRAG: |
| 1907 | case K_RIGHTRELEASE: |
| 1908 | /* Ignore drag and release events when the button-down wasn't |
| 1909 | * seen before. */ |
| 1910 | if (ignore_drag_release) |
| 1911 | goto cmdline_not_changed; |
| 1912 | /* FALLTHROUGH */ |
| 1913 | case K_LEFTMOUSE: |
| 1914 | case K_RIGHTMOUSE: |
| 1915 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1916 | ignore_drag_release = TRUE; |
| 1917 | else |
| 1918 | ignore_drag_release = FALSE; |
| 1919 | # ifdef FEAT_GUI |
| 1920 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1921 | if (!gui.in_use) |
| 1922 | # endif |
| 1923 | if (!mouse_has(MOUSE_COMMAND)) |
| 1924 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1925 | # ifdef FEAT_CLIPBOARD |
| 1926 | if (mouse_row < cmdline_row && clip_star.available) |
| 1927 | { |
| 1928 | int button, is_click, is_drag; |
| 1929 | |
| 1930 | /* |
| 1931 | * Handle modeless selection. |
| 1932 | */ |
| 1933 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1934 | &is_click, &is_drag); |
| 1935 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1936 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1937 | { |
| 1938 | /* Translate shift-left to right button. */ |
| 1939 | button = MOUSE_RIGHT; |
| 1940 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1941 | } |
| 1942 | clip_modeless(button, is_click, is_drag); |
| 1943 | goto cmdline_not_changed; |
| 1944 | } |
| 1945 | # endif |
| 1946 | |
| 1947 | set_cmdspos(); |
| 1948 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1949 | ++ccline.cmdpos) |
| 1950 | { |
| 1951 | i = cmdline_charsize(ccline.cmdpos); |
| 1952 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1953 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1954 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1955 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1956 | if (has_mbyte) |
| 1957 | { |
| 1958 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1959 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1960 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | + ccline.cmdpos) - 1; |
| 1962 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1963 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1964 | ccline.cmdspos += i; |
| 1965 | } |
| 1966 | goto cmdline_not_changed; |
| 1967 | |
| 1968 | /* Mouse scroll wheel: ignored here */ |
| 1969 | case K_MOUSEDOWN: |
| 1970 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1971 | case K_MOUSELEFT: |
| 1972 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1973 | /* Alternate buttons ignored here */ |
| 1974 | case K_X1MOUSE: |
| 1975 | case K_X1DRAG: |
| 1976 | case K_X1RELEASE: |
| 1977 | case K_X2MOUSE: |
| 1978 | case K_X2DRAG: |
| 1979 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1980 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1981 | goto cmdline_not_changed; |
| 1982 | |
| 1983 | #endif /* FEAT_MOUSE */ |
| 1984 | |
| 1985 | #ifdef FEAT_GUI |
| 1986 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1987 | case K_LEFTRELEASE_NM: |
| 1988 | goto cmdline_not_changed; |
| 1989 | |
| 1990 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1991 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1992 | { |
| 1993 | gui_do_scroll(); |
| 1994 | redrawcmd(); |
| 1995 | } |
| 1996 | goto cmdline_not_changed; |
| 1997 | |
| 1998 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1999 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2000 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2001 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2002 | redrawcmd(); |
| 2003 | } |
| 2004 | goto cmdline_not_changed; |
| 2005 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2006 | #ifdef FEAT_GUI_TABLINE |
| 2007 | case K_TABLINE: |
| 2008 | case K_TABMENU: |
| 2009 | /* Don't want to change any tabs here. Make sure the same tab |
| 2010 | * is still selected. */ |
| 2011 | if (gui_use_tabline()) |
| 2012 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 2013 | goto cmdline_not_changed; |
| 2014 | #endif |
| 2015 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2016 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 2017 | goto cmdline_not_changed; |
| 2018 | |
| 2019 | case Ctrl_B: /* begin of command line */ |
| 2020 | case K_HOME: |
| 2021 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2022 | case K_S_HOME: |
| 2023 | case K_C_HOME: |
| 2024 | ccline.cmdpos = 0; |
| 2025 | set_cmdspos(); |
| 2026 | goto cmdline_not_changed; |
| 2027 | |
| 2028 | case Ctrl_E: /* end of command line */ |
| 2029 | case K_END: |
| 2030 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | case K_S_END: |
| 2032 | case K_C_END: |
| 2033 | ccline.cmdpos = ccline.cmdlen; |
| 2034 | set_cmdspos_cursor(); |
| 2035 | goto cmdline_not_changed; |
| 2036 | |
| 2037 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2038 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | break; |
| 2040 | goto cmdline_changed; |
| 2041 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2042 | case Ctrl_L: |
| 2043 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2044 | if (may_add_char_to_search(firstc, &c, &is_state) == OK) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2045 | goto cmdline_not_changed; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2046 | #endif |
| 2047 | |
| 2048 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2049 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2050 | break; |
| 2051 | goto cmdline_changed; |
| 2052 | |
| 2053 | case Ctrl_N: /* next match */ |
| 2054 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 2055 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2057 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 2058 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2059 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2060 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2062 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 2063 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | case K_UP: |
| 2065 | case K_DOWN: |
| 2066 | case K_S_UP: |
| 2067 | case K_S_DOWN: |
| 2068 | case K_PAGEUP: |
| 2069 | case K_KPAGEUP: |
| 2070 | case K_PAGEDOWN: |
| 2071 | case K_KPAGEDOWN: |
| 2072 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 2073 | goto cmdline_not_changed; |
| 2074 | |
| 2075 | i = hiscnt; |
| 2076 | |
| 2077 | /* save current command string so it can be restored later */ |
| 2078 | if (lookfor == NULL) |
| 2079 | { |
| 2080 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 2081 | goto cmdline_not_changed; |
| 2082 | lookfor[ccline.cmdpos] = NUL; |
| 2083 | } |
| 2084 | |
| 2085 | j = (int)STRLEN(lookfor); |
| 2086 | for (;;) |
| 2087 | { |
| 2088 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2089 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2090 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2091 | { |
| 2092 | if (hiscnt == hislen) /* first time */ |
| 2093 | hiscnt = hisidx[histype]; |
| 2094 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 2095 | hiscnt = hislen - 1; |
| 2096 | else if (hiscnt != hisidx[histype] + 1) |
| 2097 | --hiscnt; |
| 2098 | else /* at top of list */ |
| 2099 | { |
| 2100 | hiscnt = i; |
| 2101 | break; |
| 2102 | } |
| 2103 | } |
| 2104 | else /* one step forwards */ |
| 2105 | { |
| 2106 | /* on last entry, clear the line */ |
| 2107 | if (hiscnt == hisidx[histype]) |
| 2108 | { |
| 2109 | hiscnt = hislen; |
| 2110 | break; |
| 2111 | } |
| 2112 | |
| 2113 | /* not on a history line, nothing to do */ |
| 2114 | if (hiscnt == hislen) |
| 2115 | break; |
| 2116 | if (hiscnt == hislen - 1) /* wrap around */ |
| 2117 | hiscnt = 0; |
| 2118 | else |
| 2119 | ++hiscnt; |
| 2120 | } |
| 2121 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 2122 | { |
| 2123 | hiscnt = i; |
| 2124 | break; |
| 2125 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2126 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2127 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 2129 | lookfor, (size_t)j) == 0) |
| 2130 | break; |
| 2131 | } |
| 2132 | |
| 2133 | if (hiscnt != i) /* jumped to other entry */ |
| 2134 | { |
| 2135 | char_u *p; |
| 2136 | int len; |
| 2137 | int old_firstc; |
| 2138 | |
| 2139 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2140 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2141 | if (hiscnt == hislen) |
| 2142 | p = lookfor; /* back to the old one */ |
| 2143 | else |
| 2144 | p = history[histype][hiscnt].hisstr; |
| 2145 | |
| 2146 | if (histype == HIST_SEARCH |
| 2147 | && p != lookfor |
| 2148 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 2149 | { |
| 2150 | /* Correct for the separator character used when |
| 2151 | * adding the history entry vs the one used now. |
| 2152 | * First loop: count length. |
| 2153 | * Second loop: copy the characters. */ |
| 2154 | for (i = 0; i <= 1; ++i) |
| 2155 | { |
| 2156 | len = 0; |
| 2157 | for (j = 0; p[j] != NUL; ++j) |
| 2158 | { |
| 2159 | /* Replace old sep with new sep, unless it is |
| 2160 | * escaped. */ |
| 2161 | if (p[j] == old_firstc |
| 2162 | && (j == 0 || p[j - 1] != '\\')) |
| 2163 | { |
| 2164 | if (i > 0) |
| 2165 | ccline.cmdbuff[len] = firstc; |
| 2166 | } |
| 2167 | else |
| 2168 | { |
| 2169 | /* Escape new sep, unless it is already |
| 2170 | * escaped. */ |
| 2171 | if (p[j] == firstc |
| 2172 | && (j == 0 || p[j - 1] != '\\')) |
| 2173 | { |
| 2174 | if (i > 0) |
| 2175 | ccline.cmdbuff[len] = '\\'; |
| 2176 | ++len; |
| 2177 | } |
| 2178 | if (i > 0) |
| 2179 | ccline.cmdbuff[len] = p[j]; |
| 2180 | } |
| 2181 | ++len; |
| 2182 | } |
| 2183 | if (i == 0) |
| 2184 | { |
| 2185 | alloc_cmdbuff(len); |
| 2186 | if (ccline.cmdbuff == NULL) |
| 2187 | goto returncmd; |
| 2188 | } |
| 2189 | } |
| 2190 | ccline.cmdbuff[len] = NUL; |
| 2191 | } |
| 2192 | else |
| 2193 | { |
| 2194 | alloc_cmdbuff((int)STRLEN(p)); |
| 2195 | if (ccline.cmdbuff == NULL) |
| 2196 | goto returncmd; |
| 2197 | STRCPY(ccline.cmdbuff, p); |
| 2198 | } |
| 2199 | |
| 2200 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 2201 | redrawcmd(); |
| 2202 | goto cmdline_changed; |
| 2203 | } |
| 2204 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2205 | #endif |
| 2206 | goto cmdline_not_changed; |
| 2207 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2208 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2209 | case Ctrl_G: /* next match */ |
| 2210 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2211 | if (may_adjust_incsearch_highlighting( |
| 2212 | firstc, count, &is_state, c) == FAIL) |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2213 | goto cmdline_not_changed; |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2214 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2215 | #endif |
| 2216 | |
| 2217 | case Ctrl_V: |
| 2218 | case Ctrl_Q: |
| 2219 | #ifdef FEAT_MOUSE |
| 2220 | ignore_drag_release = TRUE; |
| 2221 | #endif |
| 2222 | putcmdline('^', TRUE); |
| 2223 | c = get_literal(); /* get next (two) character(s) */ |
| 2224 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2225 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | #ifdef FEAT_MBYTE |
| 2227 | /* may need to remove ^ when composing char was typed */ |
| 2228 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 2229 | { |
| 2230 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2231 | msg_putchar(' '); |
| 2232 | cursorcmd(); |
| 2233 | } |
| 2234 | #endif |
| 2235 | break; |
| 2236 | |
| 2237 | #ifdef FEAT_DIGRAPHS |
| 2238 | case Ctrl_K: |
| 2239 | #ifdef FEAT_MOUSE |
| 2240 | ignore_drag_release = TRUE; |
| 2241 | #endif |
| 2242 | putcmdline('?', TRUE); |
| 2243 | #ifdef USE_ON_FLY_SCROLL |
| 2244 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 2245 | #endif |
| 2246 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2247 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | if (c != NUL) |
| 2249 | break; |
| 2250 | |
| 2251 | redrawcmd(); |
| 2252 | goto cmdline_not_changed; |
| 2253 | #endif /* FEAT_DIGRAPHS */ |
| 2254 | |
| 2255 | #ifdef FEAT_RIGHTLEFT |
| 2256 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 2257 | if (!p_ari) |
| 2258 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2259 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | if (p_altkeymap) |
| 2261 | { |
| 2262 | cmd_fkmap = !cmd_fkmap; |
| 2263 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 2264 | ccline.overstrike = FALSE; |
| 2265 | } |
| 2266 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2267 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2268 | cmd_hkmap = !cmd_hkmap; |
| 2269 | goto cmdline_not_changed; |
| 2270 | #endif |
| 2271 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2272 | case K_PS: |
| 2273 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 2274 | goto cmdline_changed; |
| 2275 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | default: |
| 2277 | #ifdef UNIX |
| 2278 | if (c == intr_char) |
| 2279 | { |
| 2280 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 2281 | putting it in history */ |
| 2282 | goto returncmd; /* back to Normal mode */ |
| 2283 | } |
| 2284 | #endif |
| 2285 | /* |
| 2286 | * Normal character with no special meaning. Just set mod_mask |
| 2287 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 2288 | * the string <S-Space>. This should only happen after ^V. |
| 2289 | */ |
| 2290 | if (!IS_SPECIAL(c)) |
| 2291 | mod_mask = 0x0; |
| 2292 | break; |
| 2293 | } |
| 2294 | /* |
| 2295 | * End of switch on command line character. |
| 2296 | * We come here if we have a normal character. |
| 2297 | */ |
| 2298 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2299 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | #ifdef FEAT_MBYTE |
| 2301 | /* Add ABBR_OFF for characters above 0x100, this is |
| 2302 | * what check_abbr() expects. */ |
| 2303 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 2304 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2305 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | goto cmdline_changed; |
| 2307 | |
| 2308 | /* |
| 2309 | * put the character in the command line |
| 2310 | */ |
| 2311 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 2312 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 2313 | else |
| 2314 | { |
| 2315 | #ifdef FEAT_MBYTE |
| 2316 | if (has_mbyte) |
| 2317 | { |
| 2318 | j = (*mb_char2bytes)(c, IObuff); |
| 2319 | IObuff[j] = NUL; /* exclude composing chars */ |
| 2320 | put_on_cmdline(IObuff, j, TRUE); |
| 2321 | } |
| 2322 | else |
| 2323 | #endif |
| 2324 | { |
| 2325 | IObuff[0] = c; |
| 2326 | put_on_cmdline(IObuff, 1, TRUE); |
| 2327 | } |
| 2328 | } |
| 2329 | goto cmdline_changed; |
| 2330 | |
| 2331 | /* |
| 2332 | * This part implements incremental searches for "/" and "?" |
| 2333 | * Jump to cmdline_not_changed when a character has been read but the command |
| 2334 | * line did not change. Then we only search and redraw if something changed in |
| 2335 | * the past. |
| 2336 | * Jump to cmdline_changed when the command line did change. |
| 2337 | * (Sorry for the goto's, I know it is ugly). |
| 2338 | */ |
| 2339 | cmdline_not_changed: |
| 2340 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2341 | if (!is_state.incsearch_postponed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | continue; |
| 2343 | #endif |
| 2344 | |
| 2345 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2346 | /* Trigger CmdlineChanged autocommands. */ |
| 2347 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2348 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2350 | may_do_incsearch_highlighting(firstc, count, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | #endif |
| 2352 | |
| 2353 | #ifdef FEAT_RIGHTLEFT |
| 2354 | if (cmdmsg_rl |
| 2355 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2356 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | # endif |
| 2358 | ) |
| 2359 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2360 | * right-left typing. Not efficient, but it works. |
| 2361 | * Do it only when there are no characters left to read |
| 2362 | * to avoid useless intermediate redraws. */ |
| 2363 | if (vpeekc() == NUL) |
| 2364 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | #endif |
| 2366 | } |
| 2367 | |
| 2368 | returncmd: |
| 2369 | |
| 2370 | #ifdef FEAT_RIGHTLEFT |
| 2371 | cmdmsg_rl = FALSE; |
| 2372 | #endif |
| 2373 | |
| 2374 | #ifdef FEAT_FKMAP |
| 2375 | cmd_fkmap = 0; |
| 2376 | #endif |
| 2377 | |
| 2378 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2379 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2380 | |
| 2381 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 2382 | finish_incsearch_highlighting(gotesc, &is_state, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2383 | #endif |
| 2384 | |
| 2385 | if (ccline.cmdbuff != NULL) |
| 2386 | { |
| 2387 | /* |
| 2388 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2389 | */ |
| 2390 | #ifdef FEAT_CMDHIST |
| 2391 | if (ccline.cmdlen && firstc != NUL |
| 2392 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2393 | { |
| 2394 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2395 | histype == HIST_SEARCH ? firstc : NUL); |
| 2396 | if (firstc == ':') |
| 2397 | { |
| 2398 | vim_free(new_last_cmdline); |
| 2399 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2400 | } |
| 2401 | } |
| 2402 | #endif |
| 2403 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2404 | if (gotesc) |
| 2405 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * If the screen was shifted up, redraw the whole screen (later). |
| 2410 | * If the line is too long, clear it, so ruler and shown command do |
| 2411 | * not get printed in the middle of it. |
| 2412 | */ |
| 2413 | msg_check(); |
| 2414 | msg_scroll = save_msg_scroll; |
| 2415 | redir_off = FALSE; |
| 2416 | |
| 2417 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2418 | if (some_key_typed) |
| 2419 | need_wait_return = FALSE; |
| 2420 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2421 | /* Trigger CmdlineLeave autocommands. */ |
| 2422 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2423 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2424 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2425 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2427 | im_save_status(b_im_ptr); |
| 2428 | im_set_active(FALSE); |
| 2429 | #endif |
| 2430 | #ifdef FEAT_MOUSE |
| 2431 | setmouse(); |
| 2432 | #endif |
| 2433 | #ifdef CURSOR_SHAPE |
| 2434 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2435 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2436 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2437 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2438 | { |
| 2439 | char_u *p = ccline.cmdbuff; |
| 2440 | |
| 2441 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2442 | ccline.cmdbuff = NULL; |
| 2443 | return p; |
| 2444 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2448 | /* |
| 2449 | * Get a command line with a prompt. |
| 2450 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2451 | * f_input() when evaluating an expression from CTRL-R =). |
| 2452 | * Returns the command line in allocated memory, or NULL. |
| 2453 | */ |
| 2454 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2455 | getcmdline_prompt( |
| 2456 | int firstc, |
| 2457 | char_u *prompt, /* command line prompt */ |
| 2458 | int attr, /* attributes for prompt */ |
| 2459 | int xp_context, /* type of expansion */ |
| 2460 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2461 | { |
| 2462 | char_u *s; |
| 2463 | struct cmdline_info save_ccline; |
| 2464 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2465 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2466 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2467 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | ccline.cmdprompt = prompt; |
| 2469 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2470 | # ifdef FEAT_EVAL |
| 2471 | ccline.xp_context = xp_context; |
| 2472 | ccline.xp_arg = xp_arg; |
| 2473 | ccline.input_fn = (firstc == '@'); |
| 2474 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2475 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2476 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2477 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2478 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2479 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2480 | * But only if called recursively and the commandline is therefore being |
| 2481 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2482 | * so we need its modified msg_col left intact. */ |
| 2483 | if (ccline.cmdbuff != NULL) |
| 2484 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | |
| 2486 | return s; |
| 2487 | } |
| 2488 | #endif |
| 2489 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2490 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2491 | * Return TRUE when the text must not be changed and we can't switch to |
| 2492 | * another window or buffer. Used when editing the command line, evaluating |
| 2493 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2494 | */ |
| 2495 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2496 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2497 | { |
| 2498 | #ifdef FEAT_CMDWIN |
| 2499 | if (cmdwin_type != 0) |
| 2500 | return TRUE; |
| 2501 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2502 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | * Give an error message for a command that isn't allowed while the cmdline |
| 2507 | * window is open or editing the cmdline in another way. |
| 2508 | */ |
| 2509 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2510 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2511 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2512 | EMSG(_(get_text_locked_msg())); |
| 2513 | } |
| 2514 | |
| 2515 | char_u * |
| 2516 | get_text_locked_msg(void) |
| 2517 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2518 | #ifdef FEAT_CMDWIN |
| 2519 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2520 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2521 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2522 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2525 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2526 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2527 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2528 | */ |
| 2529 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2530 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2531 | { |
| 2532 | if (curbuf_lock > 0) |
| 2533 | { |
| 2534 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2535 | return TRUE; |
| 2536 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2537 | return allbuf_locked(); |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2542 | * message. |
| 2543 | */ |
| 2544 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2545 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2546 | { |
| 2547 | if (allbuf_lock > 0) |
| 2548 | { |
| 2549 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2550 | return TRUE; |
| 2551 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2552 | return FALSE; |
| 2553 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2554 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2555 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2556 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2557 | { |
| 2558 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2559 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2560 | return 1; |
| 2561 | #endif |
| 2562 | return ptr2cells(ccline.cmdbuff + idx); |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Compute the offset of the cursor on the command line for the prompt and |
| 2567 | * indent. |
| 2568 | */ |
| 2569 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2570 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2571 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2572 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2574 | else |
| 2575 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Compute the screen position for the cursor on the command line. |
| 2580 | */ |
| 2581 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2582 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2583 | { |
| 2584 | int i, m, c; |
| 2585 | |
| 2586 | set_cmdspos(); |
| 2587 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2588 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2590 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2591 | m = MAXCOL; |
| 2592 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2593 | else |
| 2594 | m = MAXCOL; |
| 2595 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2596 | { |
| 2597 | c = cmdline_charsize(i); |
| 2598 | #ifdef FEAT_MBYTE |
| 2599 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2600 | if (has_mbyte) |
| 2601 | correct_cmdspos(i, c); |
| 2602 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2603 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2604 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | if ((ccline.cmdspos += c) >= m) |
| 2606 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | ccline.cmdspos -= c; |
| 2608 | break; |
| 2609 | } |
| 2610 | #ifdef FEAT_MBYTE |
| 2611 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2612 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | #endif |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | #ifdef FEAT_MBYTE |
| 2618 | /* |
| 2619 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2620 | * character that doesn't fit, so that a ">" must be displayed. |
| 2621 | */ |
| 2622 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2623 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2624 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2625 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2627 | && ccline.cmdspos % Columns + cells > Columns) |
| 2628 | ccline.cmdspos++; |
| 2629 | } |
| 2630 | #endif |
| 2631 | |
| 2632 | /* |
| 2633 | * Get an Ex command line for the ":" command. |
| 2634 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2635 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2636 | getexline( |
| 2637 | int c, /* normally ':', NUL for ":append" */ |
| 2638 | void *cookie UNUSED, |
| 2639 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | { |
| 2641 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2642 | if (exec_from_reg && vpeekc() == ':') |
| 2643 | (void)vgetc(); |
| 2644 | return getcmdline(c, 1L, indent); |
| 2645 | } |
| 2646 | |
| 2647 | /* |
| 2648 | * Get an Ex command line for Ex mode. |
| 2649 | * In Ex mode we only use the OS supplied line editing features and no |
| 2650 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2651 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2652 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2654 | getexmodeline( |
| 2655 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2656 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2657 | void *cookie UNUSED, |
| 2658 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2659 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2660 | garray_T line_ga; |
| 2661 | char_u *pend; |
| 2662 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2663 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2664 | int escaped = FALSE; /* CTRL-V typed */ |
| 2665 | int vcol = 0; |
| 2666 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2667 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2668 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2669 | |
| 2670 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2671 | * confuses the system function that computes tabstops. */ |
| 2672 | cursor_on(); |
| 2673 | |
| 2674 | /* always start in column 0; write a newline if necessary */ |
| 2675 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2676 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2678 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2679 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2680 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2681 | if (p_prompt) |
| 2682 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2683 | while (indent-- > 0) |
| 2684 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | ga_init2(&line_ga, 1, 30); |
| 2689 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2690 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2691 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2692 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2693 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2694 | while (indent >= 8) |
| 2695 | { |
| 2696 | ga_append(&line_ga, TAB); |
| 2697 | msg_puts((char_u *)" "); |
| 2698 | indent -= 8; |
| 2699 | } |
| 2700 | while (indent-- > 0) |
| 2701 | { |
| 2702 | ga_append(&line_ga, ' '); |
| 2703 | msg_putchar(' '); |
| 2704 | } |
| 2705 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2706 | ++no_mapping; |
| 2707 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2708 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | /* |
| 2710 | * Get the line, one character at a time. |
| 2711 | */ |
| 2712 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2713 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2715 | long sw; |
| 2716 | char_u *s; |
| 2717 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2718 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2719 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2721 | /* |
| 2722 | * Get one character at a time. |
| 2723 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2724 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* Check for a ":normal" command and no more characters left. */ |
| 2727 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2728 | c1 = '\n'; |
| 2729 | else |
| 2730 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | |
| 2732 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2733 | * Handle line editing. |
| 2734 | * Previously this was left to the system, putting the terminal in |
| 2735 | * 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] | 2736 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2737 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2738 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2739 | msg_putchar('\n'); |
| 2740 | break; |
| 2741 | } |
| 2742 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2743 | if (c1 == K_PS) |
| 2744 | { |
| 2745 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2746 | goto redraw; |
| 2747 | } |
| 2748 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2749 | if (!escaped) |
| 2750 | { |
| 2751 | /* CR typed means "enter", which is NL */ |
| 2752 | if (c1 == '\r') |
| 2753 | c1 = '\n'; |
| 2754 | |
| 2755 | if (c1 == BS || c1 == K_BS |
| 2756 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2757 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2758 | if (line_ga.ga_len > 0) |
| 2759 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2760 | #ifdef FEAT_MBYTE |
| 2761 | if (has_mbyte) |
| 2762 | { |
| 2763 | p = (char_u *)line_ga.ga_data; |
| 2764 | p[line_ga.ga_len] = NUL; |
| 2765 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2766 | line_ga.ga_len -= len; |
| 2767 | } |
| 2768 | else |
| 2769 | #endif |
| 2770 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2771 | goto redraw; |
| 2772 | } |
| 2773 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2776 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2777 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2778 | msg_col = startcol; |
| 2779 | msg_clr_eos(); |
| 2780 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2781 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | if (c1 == Ctrl_T) |
| 2785 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2786 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2787 | p = (char_u *)line_ga.ga_data; |
| 2788 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2789 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2790 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2791 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2792 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2793 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2794 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2795 | p = (char_u *)line_ga.ga_data; |
| 2796 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2797 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2798 | *s = ' '; |
| 2799 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2800 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2801 | redraw: |
| 2802 | /* redraw the line */ |
| 2803 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2804 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2805 | p = (char_u *)line_ga.ga_data; |
| 2806 | p[line_ga.ga_len] = NUL; |
| 2807 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2808 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2809 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2811 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2813 | msg_putchar(' '); |
| 2814 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2815 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2816 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2817 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2819 | len = MB_PTR2LEN(p); |
| 2820 | msg_outtrans_len(p, len); |
| 2821 | vcol += ptr2cells(p); |
| 2822 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | } |
| 2824 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2825 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2826 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2827 | continue; |
| 2828 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2829 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2830 | if (c1 == Ctrl_D) |
| 2831 | { |
| 2832 | /* Delete one shiftwidth. */ |
| 2833 | p = (char_u *)line_ga.ga_data; |
| 2834 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2836 | if (prev_char == '^') |
| 2837 | ex_keep_indent = TRUE; |
| 2838 | indent = 0; |
| 2839 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2840 | } |
| 2841 | else |
| 2842 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2843 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2844 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2845 | if (indent > 0) |
| 2846 | { |
| 2847 | --indent; |
| 2848 | indent -= indent % get_sw_value(curbuf); |
| 2849 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2850 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2851 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2852 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2853 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2854 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2855 | --line_ga.ga_len; |
| 2856 | } |
| 2857 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2858 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2859 | |
| 2860 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2861 | { |
| 2862 | escaped = TRUE; |
| 2863 | continue; |
| 2864 | } |
| 2865 | |
| 2866 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2867 | if (IS_SPECIAL(c1)) |
| 2868 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2869 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2870 | |
| 2871 | if (IS_SPECIAL(c1)) |
| 2872 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2873 | #ifdef FEAT_MBYTE |
| 2874 | if (has_mbyte) |
| 2875 | len = (*mb_char2bytes)(c1, |
| 2876 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2877 | else |
| 2878 | #endif |
| 2879 | { |
| 2880 | len = 1; |
| 2881 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2882 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2883 | if (c1 == '\n') |
| 2884 | msg_putchar('\n'); |
| 2885 | else if (c1 == TAB) |
| 2886 | { |
| 2887 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2888 | do |
| 2889 | { |
| 2890 | msg_putchar(' '); |
| 2891 | } while (++vcol % 8); |
| 2892 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2893 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2894 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2895 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2896 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2897 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2899 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2900 | escaped = FALSE; |
| 2901 | |
| 2902 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2903 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2904 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2905 | /* We are done when a NL is entered, but not when it comes after an |
| 2906 | * odd number of backslashes, that results in a NUL. */ |
| 2907 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2909 | int bcount = 0; |
| 2910 | |
| 2911 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2912 | ++bcount; |
| 2913 | |
| 2914 | if (bcount > 0) |
| 2915 | { |
| 2916 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2917 | * "\NL", etc. */ |
| 2918 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2919 | pend -= (bcount + 1) / 2; |
| 2920 | pend[-1] = '\n'; |
| 2921 | } |
| 2922 | |
| 2923 | if ((bcount & 1) == 0) |
| 2924 | { |
| 2925 | --line_ga.ga_len; |
| 2926 | --pend; |
| 2927 | *pend = NUL; |
| 2928 | break; |
| 2929 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2933 | --no_mapping; |
| 2934 | --allow_keys; |
| 2935 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | /* make following messages go to the next line */ |
| 2937 | msg_didout = FALSE; |
| 2938 | msg_col = 0; |
| 2939 | if (msg_row < Rows - 1) |
| 2940 | ++msg_row; |
| 2941 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2942 | |
| 2943 | if (got_int) |
| 2944 | ga_clear(&line_ga); |
| 2945 | |
| 2946 | return (char_u *)line_ga.ga_data; |
| 2947 | } |
| 2948 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2949 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2950 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2951 | /* |
| 2952 | * Return TRUE if ccline.overstrike is on. |
| 2953 | */ |
| 2954 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2955 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | { |
| 2957 | return ccline.overstrike; |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2962 | */ |
| 2963 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2964 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2965 | { |
| 2966 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2967 | } |
| 2968 | #endif |
| 2969 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2970 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | /* |
| 2972 | * Return the virtual column number at the current cursor position. |
| 2973 | * This is used by the IM code to obtain the start of the preedit string. |
| 2974 | */ |
| 2975 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2976 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2977 | { |
| 2978 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2979 | return MAXCOL; |
| 2980 | |
| 2981 | # ifdef FEAT_MBYTE |
| 2982 | if (has_mbyte) |
| 2983 | { |
| 2984 | colnr_T col; |
| 2985 | int i = 0; |
| 2986 | |
| 2987 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2988 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2989 | |
| 2990 | return col; |
| 2991 | } |
| 2992 | else |
| 2993 | # endif |
| 2994 | return ccline.cmdpos; |
| 2995 | } |
| 2996 | #endif |
| 2997 | |
| 2998 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2999 | /* |
| 3000 | * If part of the command line is an IM preedit string, redraw it with |
| 3001 | * IM feedback attributes. The cursor position is restored after drawing. |
| 3002 | */ |
| 3003 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3004 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3005 | { |
| 3006 | if ((State & CMDLINE) |
| 3007 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 3008 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | && !p_imdisable |
| 3010 | && im_is_preediting()) |
| 3011 | { |
| 3012 | int cmdpos = 0; |
| 3013 | int cmdspos; |
| 3014 | int old_row; |
| 3015 | int old_col; |
| 3016 | colnr_T col; |
| 3017 | |
| 3018 | old_row = msg_row; |
| 3019 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3020 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | |
| 3022 | # ifdef FEAT_MBYTE |
| 3023 | if (has_mbyte) |
| 3024 | { |
| 3025 | for (col = 0; col < preedit_start_col |
| 3026 | && cmdpos < ccline.cmdlen; ++col) |
| 3027 | { |
| 3028 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3029 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | else |
| 3033 | # endif |
| 3034 | { |
| 3035 | cmdspos += preedit_start_col; |
| 3036 | cmdpos += preedit_start_col; |
| 3037 | } |
| 3038 | |
| 3039 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 3040 | msg_col = cmdspos % (int)Columns; |
| 3041 | if (msg_row >= Rows) |
| 3042 | msg_row = Rows - 1; |
| 3043 | |
| 3044 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 3045 | { |
| 3046 | int char_len; |
| 3047 | int char_attr; |
| 3048 | |
| 3049 | char_attr = im_get_feedback_attr(col); |
| 3050 | if (char_attr < 0) |
| 3051 | break; /* end of preedit string */ |
| 3052 | |
| 3053 | # ifdef FEAT_MBYTE |
| 3054 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3055 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3056 | else |
| 3057 | # endif |
| 3058 | char_len = 1; |
| 3059 | |
| 3060 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 3061 | cmdpos += char_len; |
| 3062 | } |
| 3063 | |
| 3064 | msg_row = old_row; |
| 3065 | msg_col = old_col; |
| 3066 | } |
| 3067 | } |
| 3068 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 3069 | |
| 3070 | /* |
| 3071 | * Allocate a new command line buffer. |
| 3072 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 3073 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 3074 | */ |
| 3075 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3076 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3077 | { |
| 3078 | /* |
| 3079 | * give some extra space to avoid having to allocate all the time |
| 3080 | */ |
| 3081 | if (len < 80) |
| 3082 | len = 100; |
| 3083 | else |
| 3084 | len += 20; |
| 3085 | |
| 3086 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 3087 | ccline.cmdbufflen = len; |
| 3088 | } |
| 3089 | |
| 3090 | /* |
| 3091 | * Re-allocate the command line to length len + something extra. |
| 3092 | * return FAIL for failure, OK otherwise |
| 3093 | */ |
| 3094 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3095 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3096 | { |
| 3097 | char_u *p; |
| 3098 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3099 | if (len < ccline.cmdbufflen) |
| 3100 | return OK; /* no need to resize */ |
| 3101 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3102 | p = ccline.cmdbuff; |
| 3103 | alloc_cmdbuff(len); /* will get some more */ |
| 3104 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 3105 | { |
| 3106 | ccline.cmdbuff = p; /* keep the old one */ |
| 3107 | return FAIL; |
| 3108 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 3109 | /* There isn't always a NUL after the command, but it may need to be |
| 3110 | * there, thus copy up to the NUL and add a NUL. */ |
| 3111 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 3112 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3113 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3114 | |
| 3115 | if (ccline.xpc != NULL |
| 3116 | && ccline.xpc->xp_pattern != NULL |
| 3117 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 3118 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 3119 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 3120 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3121 | |
| 3122 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 3123 | * to point into the newly allocated memory. */ |
| 3124 | if (i >= 0 && i <= ccline.cmdlen) |
| 3125 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 3126 | } |
| 3127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3128 | return OK; |
| 3129 | } |
| 3130 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3131 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 3132 | static char_u *arshape_buf = NULL; |
| 3133 | |
| 3134 | # if defined(EXITFREE) || defined(PROTO) |
| 3135 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3136 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3137 | { |
| 3138 | vim_free(arshape_buf); |
| 3139 | } |
| 3140 | # endif |
| 3141 | #endif |
| 3142 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3143 | /* |
| 3144 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 3145 | * when cmdline_star is TRUE. |
| 3146 | */ |
| 3147 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3148 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3149 | { |
| 3150 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3151 | int i; |
| 3152 | |
| 3153 | if (cmdline_star > 0) |
| 3154 | for (i = 0; i < len; ++i) |
| 3155 | { |
| 3156 | msg_putchar('*'); |
| 3157 | # ifdef FEAT_MBYTE |
| 3158 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3159 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3160 | # endif |
| 3161 | } |
| 3162 | else |
| 3163 | #endif |
| 3164 | #ifdef FEAT_ARABIC |
| 3165 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 3166 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3167 | static int buflen = 0; |
| 3168 | char_u *p; |
| 3169 | int j; |
| 3170 | int newlen = 0; |
| 3171 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3172 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3173 | int prev_c = 0; |
| 3174 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3175 | int u8c; |
| 3176 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3177 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3178 | |
| 3179 | /* |
| 3180 | * Do arabic shaping into a temporary buffer. This is very |
| 3181 | * inefficient! |
| 3182 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3183 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3184 | { |
| 3185 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 3186 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3187 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3188 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3189 | arshape_buf = alloc(buflen); |
| 3190 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3191 | return; /* out of memory */ |
| 3192 | } |
| 3193 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3194 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 3195 | { |
| 3196 | /* Prepend a space to draw the leading composing char on. */ |
| 3197 | arshape_buf[0] = ' '; |
| 3198 | newlen = 1; |
| 3199 | } |
| 3200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | for (j = start; j < start + len; j += mb_l) |
| 3202 | { |
| 3203 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3204 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3205 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3206 | if (ARABIC_CHAR(u8c)) |
| 3207 | { |
| 3208 | /* Do Arabic shaping. */ |
| 3209 | if (cmdmsg_rl) |
| 3210 | { |
| 3211 | /* displaying from right to left */ |
| 3212 | pc = prev_c; |
| 3213 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3214 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3215 | if (j + mb_l >= start + len) |
| 3216 | nc = NUL; |
| 3217 | else |
| 3218 | nc = utf_ptr2char(p + mb_l); |
| 3219 | } |
| 3220 | else |
| 3221 | { |
| 3222 | /* displaying from left to right */ |
| 3223 | if (j + mb_l >= start + len) |
| 3224 | pc = NUL; |
| 3225 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3226 | { |
| 3227 | int pcc[MAX_MCO]; |
| 3228 | |
| 3229 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3230 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3231 | pc1 = pcc[0]; |
| 3232 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3233 | nc = prev_c; |
| 3234 | } |
| 3235 | prev_c = u8c; |
| 3236 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3237 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3238 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3239 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3240 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3241 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3242 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 3243 | if (u8cc[1] != 0) |
| 3244 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3245 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | else |
| 3249 | { |
| 3250 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3251 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3252 | newlen += mb_l; |
| 3253 | } |
| 3254 | } |
| 3255 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3256 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3257 | } |
| 3258 | else |
| 3259 | #endif |
| 3260 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3261 | } |
| 3262 | |
| 3263 | /* |
| 3264 | * Put a character on the command line. Shifts the following text to the |
| 3265 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3266 | * "c" must be printable (fit in one display cell)! |
| 3267 | */ |
| 3268 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3269 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3270 | { |
| 3271 | if (cmd_silent) |
| 3272 | return; |
| 3273 | msg_no_more = TRUE; |
| 3274 | msg_putchar(c); |
| 3275 | if (shift) |
| 3276 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3277 | msg_no_more = FALSE; |
| 3278 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3279 | extra_char = c; |
| 3280 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | /* |
| 3284 | * Undo a putcmdline(c, FALSE). |
| 3285 | */ |
| 3286 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3287 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3288 | { |
| 3289 | if (cmd_silent) |
| 3290 | return; |
| 3291 | msg_no_more = TRUE; |
| 3292 | if (ccline.cmdlen == ccline.cmdpos) |
| 3293 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3294 | #ifdef FEAT_MBYTE |
| 3295 | else if (has_mbyte) |
| 3296 | draw_cmdline(ccline.cmdpos, |
| 3297 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3298 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3299 | else |
| 3300 | draw_cmdline(ccline.cmdpos, 1); |
| 3301 | msg_no_more = FALSE; |
| 3302 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3303 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* |
| 3307 | * Put the given string, of the given length, onto the command line. |
| 3308 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3309 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3310 | * part will be redrawn, otherwise it will not. If this function is called |
| 3311 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3312 | * called afterwards. |
| 3313 | */ |
| 3314 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3315 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3316 | { |
| 3317 | int retval; |
| 3318 | int i; |
| 3319 | int m; |
| 3320 | int c; |
| 3321 | |
| 3322 | if (len < 0) |
| 3323 | len = (int)STRLEN(str); |
| 3324 | |
| 3325 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3326 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3327 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3328 | else |
| 3329 | retval = OK; |
| 3330 | if (retval == OK) |
| 3331 | { |
| 3332 | if (!ccline.overstrike) |
| 3333 | { |
| 3334 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3335 | ccline.cmdbuff + ccline.cmdpos, |
| 3336 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3337 | ccline.cmdlen += len; |
| 3338 | } |
| 3339 | else |
| 3340 | { |
| 3341 | #ifdef FEAT_MBYTE |
| 3342 | if (has_mbyte) |
| 3343 | { |
| 3344 | /* Count nr of characters in the new string. */ |
| 3345 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3346 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3347 | ++m; |
| 3348 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3349 | * characters. */ |
| 3350 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3351 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | --m; |
| 3353 | if (i < ccline.cmdlen) |
| 3354 | { |
| 3355 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3356 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3357 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3358 | } |
| 3359 | else |
| 3360 | ccline.cmdlen = ccline.cmdpos + len; |
| 3361 | } |
| 3362 | else |
| 3363 | #endif |
| 3364 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3365 | ccline.cmdlen = ccline.cmdpos + len; |
| 3366 | } |
| 3367 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3368 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3369 | |
| 3370 | #ifdef FEAT_MBYTE |
| 3371 | if (enc_utf8) |
| 3372 | { |
| 3373 | /* When the inserted text starts with a composing character, |
| 3374 | * backup to the character before it. There could be two of them. |
| 3375 | */ |
| 3376 | i = 0; |
| 3377 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3378 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3379 | { |
| 3380 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3381 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3382 | ccline.cmdpos -= i; |
| 3383 | len += i; |
| 3384 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3385 | } |
| 3386 | # ifdef FEAT_ARABIC |
| 3387 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3388 | { |
| 3389 | /* Check the previous character for Arabic combining pair. */ |
| 3390 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3391 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3392 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3393 | + ccline.cmdpos - i), c)) |
| 3394 | { |
| 3395 | ccline.cmdpos -= i; |
| 3396 | len += i; |
| 3397 | } |
| 3398 | else |
| 3399 | i = 0; |
| 3400 | } |
| 3401 | # endif |
| 3402 | if (i != 0) |
| 3403 | { |
| 3404 | /* Also backup the cursor position. */ |
| 3405 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3406 | ccline.cmdspos -= i; |
| 3407 | msg_col -= i; |
| 3408 | if (msg_col < 0) |
| 3409 | { |
| 3410 | msg_col += Columns; |
| 3411 | --msg_row; |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | #endif |
| 3416 | |
| 3417 | if (redraw && !cmd_silent) |
| 3418 | { |
| 3419 | msg_no_more = TRUE; |
| 3420 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3421 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3423 | /* Avoid clearing the rest of the line too often. */ |
| 3424 | if (cmdline_row != i || ccline.overstrike) |
| 3425 | msg_clr_eos(); |
| 3426 | msg_no_more = FALSE; |
| 3427 | } |
| 3428 | #ifdef FEAT_FKMAP |
| 3429 | /* |
| 3430 | * If we are in Farsi command mode, the character input must be in |
| 3431 | * Insert mode. So do not advance the cmdpos. |
| 3432 | */ |
| 3433 | if (!cmd_fkmap) |
| 3434 | #endif |
| 3435 | { |
| 3436 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3437 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3438 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3439 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3440 | m = MAXCOL; |
| 3441 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3442 | else |
| 3443 | m = MAXCOL; |
| 3444 | for (i = 0; i < len; ++i) |
| 3445 | { |
| 3446 | c = cmdline_charsize(ccline.cmdpos); |
| 3447 | #ifdef FEAT_MBYTE |
| 3448 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3449 | if (has_mbyte) |
| 3450 | correct_cmdspos(ccline.cmdpos, c); |
| 3451 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3452 | /* Stop cursor at the end of the screen, but do increment the |
| 3453 | * insert position, so that entering a very long command |
| 3454 | * works, even though you can't see it. */ |
| 3455 | if (ccline.cmdspos + c < m) |
| 3456 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3457 | #ifdef FEAT_MBYTE |
| 3458 | if (has_mbyte) |
| 3459 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3460 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | if (c > len - i - 1) |
| 3462 | c = len - i - 1; |
| 3463 | ccline.cmdpos += c; |
| 3464 | i += c; |
| 3465 | } |
| 3466 | #endif |
| 3467 | ++ccline.cmdpos; |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | if (redraw) |
| 3472 | msg_check(); |
| 3473 | return retval; |
| 3474 | } |
| 3475 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3476 | static struct cmdline_info prev_ccline; |
| 3477 | static int prev_ccline_used = FALSE; |
| 3478 | |
| 3479 | /* |
| 3480 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3481 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3482 | * available globally in prev_ccline. |
| 3483 | */ |
| 3484 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3485 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3486 | { |
| 3487 | if (!prev_ccline_used) |
| 3488 | { |
| 3489 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3490 | prev_ccline_used = TRUE; |
| 3491 | } |
| 3492 | *ccp = prev_ccline; |
| 3493 | prev_ccline = ccline; |
| 3494 | ccline.cmdbuff = NULL; |
| 3495 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3496 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3500 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3501 | */ |
| 3502 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3503 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3504 | { |
| 3505 | ccline = prev_ccline; |
| 3506 | prev_ccline = *ccp; |
| 3507 | } |
| 3508 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3509 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3510 | /* |
| 3511 | * Save the command line into allocated memory. Returns a pointer to be |
| 3512 | * passed to restore_cmdline_alloc() later. |
| 3513 | * Returns NULL when failed. |
| 3514 | */ |
| 3515 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3516 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3517 | { |
| 3518 | struct cmdline_info *p; |
| 3519 | |
| 3520 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3521 | if (p != NULL) |
| 3522 | save_cmdline(p); |
| 3523 | return (char_u *)p; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3528 | */ |
| 3529 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3530 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3531 | { |
| 3532 | if (p != NULL) |
| 3533 | { |
| 3534 | restore_cmdline((struct cmdline_info *)p); |
| 3535 | vim_free(p); |
| 3536 | } |
| 3537 | } |
| 3538 | #endif |
| 3539 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3540 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3541 | * Paste a yank register into the command line. |
| 3542 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3543 | * insert_reg() can't be used here, because special characters from the |
| 3544 | * register contents will be interpreted as commands. |
| 3545 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3546 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3547 | */ |
| 3548 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3549 | cmdline_paste( |
| 3550 | int regname, |
| 3551 | int literally, /* Insert text literally instead of "as typed" */ |
| 3552 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3553 | { |
| 3554 | long i; |
| 3555 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3556 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3557 | int allocated; |
| 3558 | struct cmdline_info save_ccline; |
| 3559 | |
| 3560 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3561 | * the command line */ |
| 3562 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 3563 | && regname != Ctrl_A && regname != Ctrl_L |
| 3564 | && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3565 | return FAIL; |
| 3566 | |
| 3567 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3568 | * CTRL-C to break the loop. */ |
| 3569 | line_breakcheck(); |
| 3570 | if (got_int) |
| 3571 | return FAIL; |
| 3572 | |
| 3573 | #ifdef FEAT_CLIPBOARD |
| 3574 | regname = may_get_selection(regname); |
| 3575 | #endif |
| 3576 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3577 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3578 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3579 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3580 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3581 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3582 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3583 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3584 | |
| 3585 | if (i) |
| 3586 | { |
| 3587 | /* Got the value of a special register in "arg". */ |
| 3588 | if (arg == NULL) |
| 3589 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3590 | |
| 3591 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3592 | * part of the word. */ |
| 3593 | p = arg; |
| 3594 | if (p_is && regname == Ctrl_W) |
| 3595 | { |
| 3596 | char_u *w; |
| 3597 | int len; |
| 3598 | |
| 3599 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3600 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3601 | { |
| 3602 | #ifdef FEAT_MBYTE |
| 3603 | if (has_mbyte) |
| 3604 | { |
| 3605 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3606 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3607 | break; |
| 3608 | w -= len; |
| 3609 | } |
| 3610 | else |
| 3611 | #endif |
| 3612 | { |
| 3613 | if (!vim_iswordc(w[-1])) |
| 3614 | break; |
| 3615 | --w; |
| 3616 | } |
| 3617 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3618 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3619 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3620 | p += len; |
| 3621 | } |
| 3622 | |
| 3623 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3624 | if (allocated) |
| 3625 | vim_free(arg); |
| 3626 | return OK; |
| 3627 | } |
| 3628 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3629 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | /* |
| 3633 | * Put a string on the command line. |
| 3634 | * When "literally" is TRUE, insert literally. |
| 3635 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3636 | * line. |
| 3637 | */ |
| 3638 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3639 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3640 | { |
| 3641 | int c, cv; |
| 3642 | |
| 3643 | if (literally) |
| 3644 | put_on_cmdline(s, -1, TRUE); |
| 3645 | else |
| 3646 | while (*s != NUL) |
| 3647 | { |
| 3648 | cv = *s; |
| 3649 | if (cv == Ctrl_V && s[1]) |
| 3650 | ++s; |
| 3651 | #ifdef FEAT_MBYTE |
| 3652 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3653 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3654 | else |
| 3655 | #endif |
| 3656 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3657 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3658 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3659 | #ifdef UNIX |
| 3660 | || c == intr_char |
| 3661 | #endif |
| 3662 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3663 | stuffcharReadbuff(Ctrl_V); |
| 3664 | stuffcharReadbuff(c); |
| 3665 | } |
| 3666 | } |
| 3667 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3668 | #ifdef FEAT_WILDMENU |
| 3669 | /* |
| 3670 | * Delete characters on the command line, from "from" to the current |
| 3671 | * position. |
| 3672 | */ |
| 3673 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3674 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3675 | { |
| 3676 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3677 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3678 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3679 | ccline.cmdpos = from; |
| 3680 | } |
| 3681 | #endif |
| 3682 | |
| 3683 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3684 | * This function is called when the screen size changes and with incremental |
| 3685 | * search and in other situations where the command line may have been |
| 3686 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3687 | */ |
| 3688 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3689 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3691 | redrawcmdline_ex(TRUE); |
| 3692 | } |
| 3693 | |
| 3694 | void |
| 3695 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3696 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | if (cmd_silent) |
| 3698 | return; |
| 3699 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3700 | if (do_compute_cmdrow) |
| 3701 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3702 | redrawcmd(); |
| 3703 | cursorcmd(); |
| 3704 | } |
| 3705 | |
| 3706 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3707 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3708 | { |
| 3709 | int i; |
| 3710 | |
| 3711 | if (cmd_silent) |
| 3712 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3713 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3714 | msg_putchar(ccline.cmdfirstc); |
| 3715 | if (ccline.cmdprompt != NULL) |
| 3716 | { |
| 3717 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3718 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3719 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3720 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3721 | --ccline.cmdindent; |
| 3722 | } |
| 3723 | else |
| 3724 | for (i = ccline.cmdindent; i > 0; --i) |
| 3725 | msg_putchar(' '); |
| 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Redraw what is currently on the command line. |
| 3730 | */ |
| 3731 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3732 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3733 | { |
| 3734 | if (cmd_silent) |
| 3735 | return; |
| 3736 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3737 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3738 | if (ccline.cmdbuff == NULL) |
| 3739 | { |
| 3740 | windgoto(cmdline_row, 0); |
| 3741 | msg_clr_eos(); |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3745 | msg_start(); |
| 3746 | redrawcmdprompt(); |
| 3747 | |
| 3748 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3749 | msg_no_more = TRUE; |
| 3750 | draw_cmdline(0, ccline.cmdlen); |
| 3751 | msg_clr_eos(); |
| 3752 | msg_no_more = FALSE; |
| 3753 | |
| 3754 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3755 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3756 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3757 | |
| 3758 | /* |
| 3759 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3760 | * in cmdline mode we can reset them now. |
| 3761 | */ |
| 3762 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3763 | |
| 3764 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3765 | * in cmdline mode */ |
| 3766 | skip_redraw = FALSE; |
| 3767 | } |
| 3768 | |
| 3769 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3770 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3771 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3772 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | cmdline_row = Rows - 1; |
| 3774 | else |
| 3775 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3776 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3780 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3781 | { |
| 3782 | if (cmd_silent) |
| 3783 | return; |
| 3784 | |
| 3785 | #ifdef FEAT_RIGHTLEFT |
| 3786 | if (cmdmsg_rl) |
| 3787 | { |
| 3788 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3789 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3790 | if (msg_row <= 0) |
| 3791 | msg_row = Rows - 1; |
| 3792 | } |
| 3793 | else |
| 3794 | #endif |
| 3795 | { |
| 3796 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3797 | msg_col = ccline.cmdspos % (int)Columns; |
| 3798 | if (msg_row >= Rows) |
| 3799 | msg_row = Rows - 1; |
| 3800 | } |
| 3801 | |
| 3802 | windgoto(msg_row, msg_col); |
| 3803 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3804 | if (p_imst == IM_ON_THE_SPOT) |
| 3805 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3806 | #endif |
| 3807 | #ifdef MCH_CURSOR_SHAPE |
| 3808 | mch_update_cursor(); |
| 3809 | #endif |
| 3810 | } |
| 3811 | |
| 3812 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3813 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3814 | { |
| 3815 | msg_start(); |
| 3816 | #ifdef FEAT_RIGHTLEFT |
| 3817 | if (cmdmsg_rl) |
| 3818 | msg_col = Columns - 1; |
| 3819 | else |
| 3820 | #endif |
| 3821 | msg_col = 0; /* always start in column 0 */ |
| 3822 | if (clr) /* clear the bottom line(s) */ |
| 3823 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3824 | windgoto(cmdline_row, 0); |
| 3825 | } |
| 3826 | |
| 3827 | /* |
| 3828 | * Check the word in front of the cursor for an abbreviation. |
| 3829 | * Called when the non-id character "c" has been entered. |
| 3830 | * When an abbreviation is recognized it is removed from the text with |
| 3831 | * backspaces and the replacement string is inserted, followed by "c". |
| 3832 | */ |
| 3833 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3834 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | { |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3836 | int spos = 0; |
| 3837 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3838 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3839 | return FALSE; |
| 3840 | |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3841 | /* Do not consider '<,'> be part of the mapping, skip leading whitespace. |
| 3842 | * Actually accepts any mark. */ |
| 3843 | while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen) |
| 3844 | spos++; |
| 3845 | if (ccline.cmdlen - spos > 5 |
| 3846 | && ccline.cmdbuff[spos] == '\'' |
| 3847 | && ccline.cmdbuff[spos + 2] == ',' |
| 3848 | && ccline.cmdbuff[spos + 3] == '\'') |
| 3849 | spos += 5; |
| 3850 | else |
| 3851 | /* check abbreviation from the beginning of the commandline */ |
| 3852 | spos = 0; |
| 3853 | |
| 3854 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3857 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3858 | static int |
| 3859 | #ifdef __BORLANDC__ |
| 3860 | _RTLENTRYF |
| 3861 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3862 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3863 | { |
| 3864 | char_u *p1 = *(char_u **)s1; |
| 3865 | char_u *p2 = *(char_u **)s2; |
| 3866 | |
| 3867 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3868 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3869 | return STRCMP(p1, p2); |
| 3870 | } |
| 3871 | #endif |
| 3872 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3873 | /* |
| 3874 | * Return FAIL if this is not an appropriate context in which to do |
| 3875 | * completion of anything, return OK if it is (even if there are no matches). |
| 3876 | * For the caller, this means that the character is just passed through like a |
| 3877 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3878 | */ |
| 3879 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3880 | nextwild( |
| 3881 | expand_T *xp, |
| 3882 | int type, |
| 3883 | int options, /* extra options for ExpandOne() */ |
| 3884 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3885 | { |
| 3886 | int i, j; |
| 3887 | char_u *p1; |
| 3888 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3889 | int difflen; |
| 3890 | int v; |
| 3891 | |
| 3892 | if (xp->xp_numfiles == -1) |
| 3893 | { |
| 3894 | set_expand_context(xp); |
| 3895 | cmd_showtail = expand_showtail(xp); |
| 3896 | } |
| 3897 | |
| 3898 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3899 | { |
| 3900 | beep_flush(); |
| 3901 | return OK; /* Something illegal on command line */ |
| 3902 | } |
| 3903 | if (xp->xp_context == EXPAND_NOTHING) |
| 3904 | { |
| 3905 | /* Caller can use the character as a normal char instead */ |
| 3906 | return FAIL; |
| 3907 | } |
| 3908 | |
| 3909 | MSG_PUTS("..."); /* show that we are busy */ |
| 3910 | out_flush(); |
| 3911 | |
| 3912 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3913 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3914 | |
| 3915 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3916 | { |
| 3917 | /* |
| 3918 | * Get next/previous match for a previous expanded pattern. |
| 3919 | */ |
| 3920 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3921 | } |
| 3922 | else |
| 3923 | { |
| 3924 | /* |
| 3925 | * Translate string into pattern and expand it. |
| 3926 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3927 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3928 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | p2 = NULL; |
| 3930 | else |
| 3931 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3932 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3933 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3934 | if (escape) |
| 3935 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3936 | |
| 3937 | if (p_wic) |
| 3938 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3939 | p2 = ExpandOne(xp, p1, |
| 3940 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3941 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3942 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3943 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3944 | if (p2 != NULL && type == WILD_LONGEST) |
| 3945 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3946 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3947 | if (ccline.cmdbuff[i + j] == '*' |
| 3948 | || ccline.cmdbuff[i + j] == '?') |
| 3949 | break; |
| 3950 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3951 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | } |
| 3953 | } |
| 3954 | } |
| 3955 | |
| 3956 | if (p2 != NULL && !got_int) |
| 3957 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3958 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3959 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3960 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3961 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3963 | } |
| 3964 | else |
| 3965 | v = OK; |
| 3966 | if (v == OK) |
| 3967 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3968 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3969 | &ccline.cmdbuff[ccline.cmdpos], |
| 3970 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3971 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3972 | ccline.cmdlen += difflen; |
| 3973 | ccline.cmdpos += difflen; |
| 3974 | } |
| 3975 | } |
| 3976 | vim_free(p2); |
| 3977 | |
| 3978 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3979 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3980 | |
| 3981 | /* When expanding a ":map" command and no matches are found, assume that |
| 3982 | * the key is supposed to be inserted literally */ |
| 3983 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3984 | return FAIL; |
| 3985 | |
| 3986 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3987 | beep_flush(); |
| 3988 | else if (xp->xp_numfiles == 1) |
| 3989 | /* free expanded pattern */ |
| 3990 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3991 | |
| 3992 | return OK; |
| 3993 | } |
| 3994 | |
| 3995 | /* |
| 3996 | * Do wildcard expansion on the string 'str'. |
| 3997 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3998 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | * Return NULL for failure. |
| 4000 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4001 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 4002 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 4003 | * WILD_PREV "orig" should be NULL. |
| 4004 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 4005 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 4006 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4007 | * |
| 4008 | * mode = WILD_FREE: just free previously expanded matches |
| 4009 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 4010 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 4011 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 4012 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 4013 | * mode = WILD_ALL: return all matches concatenated |
| 4014 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4015 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | * |
| 4017 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 4018 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 4019 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 4020 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 4021 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 4022 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 4023 | * options = WILD_SILENT: don't print warning messages |
| 4024 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4025 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4026 | * |
| 4027 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 4028 | */ |
| 4029 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4030 | ExpandOne( |
| 4031 | expand_T *xp, |
| 4032 | char_u *str, |
| 4033 | char_u *orig, /* allocated copy of original of expanded string */ |
| 4034 | int options, |
| 4035 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4036 | { |
| 4037 | char_u *ss = NULL; |
| 4038 | static int findex; |
| 4039 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4040 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | int i; |
| 4042 | long_u len; |
| 4043 | int non_suf_match; /* number without matching suffix */ |
| 4044 | |
| 4045 | /* |
| 4046 | * first handle the case of using an old match |
| 4047 | */ |
| 4048 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 4049 | { |
| 4050 | if (xp->xp_numfiles > 0) |
| 4051 | { |
| 4052 | if (mode == WILD_PREV) |
| 4053 | { |
| 4054 | if (findex == -1) |
| 4055 | findex = xp->xp_numfiles; |
| 4056 | --findex; |
| 4057 | } |
| 4058 | else /* mode == WILD_NEXT */ |
| 4059 | ++findex; |
| 4060 | |
| 4061 | /* |
| 4062 | * When wrapping around, return the original string, set findex to |
| 4063 | * -1. |
| 4064 | */ |
| 4065 | if (findex < 0) |
| 4066 | { |
| 4067 | if (orig_save == NULL) |
| 4068 | findex = xp->xp_numfiles - 1; |
| 4069 | else |
| 4070 | findex = -1; |
| 4071 | } |
| 4072 | if (findex >= xp->xp_numfiles) |
| 4073 | { |
| 4074 | if (orig_save == NULL) |
| 4075 | findex = 0; |
| 4076 | else |
| 4077 | findex = -1; |
| 4078 | } |
| 4079 | #ifdef FEAT_WILDMENU |
| 4080 | if (p_wmnu) |
| 4081 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 4082 | findex, cmd_showtail); |
| 4083 | #endif |
| 4084 | if (findex == -1) |
| 4085 | return vim_strsave(orig_save); |
| 4086 | return vim_strsave(xp->xp_files[findex]); |
| 4087 | } |
| 4088 | else |
| 4089 | return NULL; |
| 4090 | } |
| 4091 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4092 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4093 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 4094 | { |
| 4095 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4096 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4097 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4098 | } |
| 4099 | findex = 0; |
| 4100 | |
| 4101 | if (mode == WILD_FREE) /* only release file name */ |
| 4102 | return NULL; |
| 4103 | |
| 4104 | if (xp->xp_numfiles == -1) |
| 4105 | { |
| 4106 | vim_free(orig_save); |
| 4107 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4108 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4109 | |
| 4110 | /* |
| 4111 | * Do the expansion. |
| 4112 | */ |
| 4113 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 4114 | options) == FAIL) |
| 4115 | { |
| 4116 | #ifdef FNAME_ILLEGAL |
| 4117 | /* Illegal file name has been silently skipped. But when there |
| 4118 | * are wildcards, the real problem is that there was no match, |
| 4119 | * causing the pattern to be added, which has illegal characters. |
| 4120 | */ |
| 4121 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 4122 | EMSG2(_(e_nomatch2), str); |
| 4123 | #endif |
| 4124 | } |
| 4125 | else if (xp->xp_numfiles == 0) |
| 4126 | { |
| 4127 | if (!(options & WILD_SILENT)) |
| 4128 | EMSG2(_(e_nomatch2), str); |
| 4129 | } |
| 4130 | else |
| 4131 | { |
| 4132 | /* Escape the matches for use on the command line. */ |
| 4133 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 4134 | |
| 4135 | /* |
| 4136 | * Check for matching suffixes in file names. |
| 4137 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4138 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 4139 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4140 | { |
| 4141 | if (xp->xp_numfiles) |
| 4142 | non_suf_match = xp->xp_numfiles; |
| 4143 | else |
| 4144 | non_suf_match = 1; |
| 4145 | if ((xp->xp_context == EXPAND_FILES |
| 4146 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4147 | && xp->xp_numfiles > 1) |
| 4148 | { |
| 4149 | /* |
| 4150 | * More than one match; check suffix. |
| 4151 | * The files will have been sorted on matching suffix in |
| 4152 | * expand_wildcards, only need to check the first two. |
| 4153 | */ |
| 4154 | non_suf_match = 0; |
| 4155 | for (i = 0; i < 2; ++i) |
| 4156 | if (match_suffix(xp->xp_files[i])) |
| 4157 | ++non_suf_match; |
| 4158 | } |
| 4159 | if (non_suf_match != 1) |
| 4160 | { |
| 4161 | /* Can we ever get here unless it's while expanding |
| 4162 | * interactively? If not, we can get rid of this all |
| 4163 | * together. Don't really want to wait for this message |
| 4164 | * (and possibly have to hit return to continue!). |
| 4165 | */ |
| 4166 | if (!(options & WILD_SILENT)) |
| 4167 | EMSG(_(e_toomany)); |
| 4168 | else if (!(options & WILD_NO_BEEP)) |
| 4169 | beep_flush(); |
| 4170 | } |
| 4171 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 4172 | ss = vim_strsave(xp->xp_files[0]); |
| 4173 | } |
| 4174 | } |
| 4175 | } |
| 4176 | |
| 4177 | /* Find longest common part */ |
| 4178 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 4179 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4180 | int mb_len = 1; |
| 4181 | int c0, ci; |
| 4182 | |
| 4183 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4184 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4185 | #ifdef FEAT_MBYTE |
| 4186 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4188 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 4189 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 4190 | } |
| 4191 | else |
| 4192 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 4193 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4194 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 4195 | { |
| 4196 | #ifdef FEAT_MBYTE |
| 4197 | if (has_mbyte) |
| 4198 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 4199 | else |
| 4200 | #endif |
| 4201 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4202 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4203 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4204 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4205 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4206 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4207 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4208 | break; |
| 4209 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4210 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | break; |
| 4212 | } |
| 4213 | if (i < xp->xp_numfiles) |
| 4214 | { |
| 4215 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 4216 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | break; |
| 4218 | } |
| 4219 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | ss = alloc((unsigned)len + 1); |
| 4222 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4223 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4224 | findex = -1; /* next p_wc gets first one */ |
| 4225 | } |
| 4226 | |
| 4227 | /* Concatenate all matching names */ |
| 4228 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 4229 | { |
| 4230 | len = 0; |
| 4231 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4232 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 4233 | ss = lalloc(len, TRUE); |
| 4234 | if (ss != NULL) |
| 4235 | { |
| 4236 | *ss = NUL; |
| 4237 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4238 | { |
| 4239 | STRCAT(ss, xp->xp_files[i]); |
| 4240 | if (i != xp->xp_numfiles - 1) |
| 4241 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 4242 | } |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 4247 | ExpandCleanup(xp); |
| 4248 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4249 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4250 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4251 | vim_free(orig); |
| 4252 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4253 | return ss; |
| 4254 | } |
| 4255 | |
| 4256 | /* |
| 4257 | * Prepare an expand structure for use. |
| 4258 | */ |
| 4259 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4260 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4261 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 4262 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4263 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4264 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4265 | #ifndef BACKSLASH_IN_FILENAME |
| 4266 | xp->xp_shell = FALSE; |
| 4267 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4268 | xp->xp_numfiles = -1; |
| 4269 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4270 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4271 | xp->xp_arg = NULL; |
| 4272 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4273 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Cleanup an expand structure after use. |
| 4278 | */ |
| 4279 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4280 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4281 | { |
| 4282 | if (xp->xp_numfiles >= 0) |
| 4283 | { |
| 4284 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4285 | xp->xp_numfiles = -1; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4290 | ExpandEscape( |
| 4291 | expand_T *xp, |
| 4292 | char_u *str, |
| 4293 | int numfiles, |
| 4294 | char_u **files, |
| 4295 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4296 | { |
| 4297 | int i; |
| 4298 | char_u *p; |
| 4299 | |
| 4300 | /* |
| 4301 | * May change home directory back to "~" |
| 4302 | */ |
| 4303 | if (options & WILD_HOME_REPLACE) |
| 4304 | tilde_replace(str, numfiles, files); |
| 4305 | |
| 4306 | if (options & WILD_ESCAPE) |
| 4307 | { |
| 4308 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4309 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4310 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4311 | || xp->xp_context == EXPAND_BUFFERS |
| 4312 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4313 | { |
| 4314 | /* |
| 4315 | * Insert a backslash into a file name before a space, \, %, # |
| 4316 | * and wildmatch characters, except '~'. |
| 4317 | */ |
| 4318 | for (i = 0; i < numfiles; ++i) |
| 4319 | { |
| 4320 | /* for ":set path=" we need to escape spaces twice */ |
| 4321 | if (xp->xp_backslash == XP_BS_THREE) |
| 4322 | { |
| 4323 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4324 | if (p != NULL) |
| 4325 | { |
| 4326 | vim_free(files[i]); |
| 4327 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4328 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4329 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4330 | if (p != NULL) |
| 4331 | { |
| 4332 | vim_free(files[i]); |
| 4333 | files[i] = p; |
| 4334 | } |
| 4335 | #endif |
| 4336 | } |
| 4337 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4338 | #ifdef BACKSLASH_IN_FILENAME |
| 4339 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4340 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4341 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4342 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4343 | if (p != NULL) |
| 4344 | { |
| 4345 | vim_free(files[i]); |
| 4346 | files[i] = p; |
| 4347 | } |
| 4348 | |
| 4349 | /* If 'str' starts with "\~", replace "~" at start of |
| 4350 | * files[i] with "\~". */ |
| 4351 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4352 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4353 | } |
| 4354 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4355 | |
| 4356 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4357 | * could be seen as "+cmd". */ |
| 4358 | if (*files[0] == '+') |
| 4359 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4360 | } |
| 4361 | else if (xp->xp_context == EXPAND_TAGS) |
| 4362 | { |
| 4363 | /* |
| 4364 | * Insert a backslash before characters in a tag name that |
| 4365 | * would terminate the ":tag" command. |
| 4366 | */ |
| 4367 | for (i = 0; i < numfiles; ++i) |
| 4368 | { |
| 4369 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4370 | if (p != NULL) |
| 4371 | { |
| 4372 | vim_free(files[i]); |
| 4373 | files[i] = p; |
| 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4381 | * Escape special characters in "fname" for when used as a file name argument |
| 4382 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4383 | * Returns the result in allocated memory. |
| 4384 | */ |
| 4385 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4386 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4387 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4388 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4389 | #ifdef BACKSLASH_IN_FILENAME |
| 4390 | char_u buf[20]; |
| 4391 | int j = 0; |
| 4392 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4393 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4394 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4395 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4396 | buf[j++] = *p; |
| 4397 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4398 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4399 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4400 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4401 | if (shell && csh_like_shell() && p != NULL) |
| 4402 | { |
| 4403 | char_u *s; |
| 4404 | |
| 4405 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4406 | * One is taken by Vim, one by the shell. */ |
| 4407 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4408 | vim_free(p); |
| 4409 | p = s; |
| 4410 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4411 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4412 | |
| 4413 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4414 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4415 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4416 | escape_fname(&p); |
| 4417 | |
| 4418 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4422 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4423 | */ |
| 4424 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4425 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4426 | { |
| 4427 | char_u *p; |
| 4428 | |
| 4429 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4430 | if (p != NULL) |
| 4431 | { |
| 4432 | p[0] = '\\'; |
| 4433 | STRCPY(p + 1, *pp); |
| 4434 | vim_free(*pp); |
| 4435 | *pp = p; |
| 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4440 | * For each file name in files[num_files]: |
| 4441 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4442 | */ |
| 4443 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4444 | tilde_replace( |
| 4445 | char_u *orig_pat, |
| 4446 | int num_files, |
| 4447 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4448 | { |
| 4449 | int i; |
| 4450 | char_u *p; |
| 4451 | |
| 4452 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4453 | { |
| 4454 | for (i = 0; i < num_files; ++i) |
| 4455 | { |
| 4456 | p = home_replace_save(NULL, files[i]); |
| 4457 | if (p != NULL) |
| 4458 | { |
| 4459 | vim_free(files[i]); |
| 4460 | files[i] = p; |
| 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | /* |
| 4467 | * Show all matches for completion on the command line. |
| 4468 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4469 | * be inserted like a normal character. |
| 4470 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4472 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4473 | { |
| 4474 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4475 | int num_files; |
| 4476 | char_u **files_found; |
| 4477 | int i, j, k; |
| 4478 | int maxlen; |
| 4479 | int lines; |
| 4480 | int columns; |
| 4481 | char_u *p; |
| 4482 | int lastlen; |
| 4483 | int attr; |
| 4484 | int showtail; |
| 4485 | |
| 4486 | if (xp->xp_numfiles == -1) |
| 4487 | { |
| 4488 | set_expand_context(xp); |
| 4489 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4490 | &num_files, &files_found); |
| 4491 | showtail = expand_showtail(xp); |
| 4492 | if (i != EXPAND_OK) |
| 4493 | return i; |
| 4494 | |
| 4495 | } |
| 4496 | else |
| 4497 | { |
| 4498 | num_files = xp->xp_numfiles; |
| 4499 | files_found = xp->xp_files; |
| 4500 | showtail = cmd_showtail; |
| 4501 | } |
| 4502 | |
| 4503 | #ifdef FEAT_WILDMENU |
| 4504 | if (!wildmenu) |
| 4505 | { |
| 4506 | #endif |
| 4507 | msg_didany = FALSE; /* lines_left will be set */ |
| 4508 | msg_start(); /* prepare for paging */ |
| 4509 | msg_putchar('\n'); |
| 4510 | out_flush(); |
| 4511 | cmdline_row = msg_row; |
| 4512 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4513 | msg_start(); /* prepare for paging */ |
| 4514 | #ifdef FEAT_WILDMENU |
| 4515 | } |
| 4516 | #endif |
| 4517 | |
| 4518 | if (got_int) |
| 4519 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4520 | #ifdef FEAT_WILDMENU |
| 4521 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4522 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4523 | #endif |
| 4524 | else |
| 4525 | { |
| 4526 | /* find the length of the longest file name */ |
| 4527 | maxlen = 0; |
| 4528 | for (i = 0; i < num_files; ++i) |
| 4529 | { |
| 4530 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4531 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4532 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4533 | { |
| 4534 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4535 | j = vim_strsize(NameBuff); |
| 4536 | } |
| 4537 | else |
| 4538 | j = vim_strsize(L_SHOWFILE(i)); |
| 4539 | if (j > maxlen) |
| 4540 | maxlen = j; |
| 4541 | } |
| 4542 | |
| 4543 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4544 | lines = num_files; |
| 4545 | else |
| 4546 | { |
| 4547 | /* compute the number of columns and lines for the listing */ |
| 4548 | maxlen += 2; /* two spaces between file names */ |
| 4549 | columns = ((int)Columns + 2) / maxlen; |
| 4550 | if (columns < 1) |
| 4551 | columns = 1; |
| 4552 | lines = (num_files + columns - 1) / columns; |
| 4553 | } |
| 4554 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4555 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4556 | |
| 4557 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4558 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4559 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4560 | msg_clr_eos(); |
| 4561 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4562 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | /* list the files line by line */ |
| 4566 | for (i = 0; i < lines; ++i) |
| 4567 | { |
| 4568 | lastlen = 999; |
| 4569 | for (k = i; k < num_files; k += lines) |
| 4570 | { |
| 4571 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4572 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4573 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4574 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4575 | msg_advance(maxlen + 1); |
| 4576 | msg_puts(p); |
| 4577 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4578 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | break; |
| 4580 | } |
| 4581 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4582 | msg_putchar(' '); |
| 4583 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4584 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | || xp->xp_context == EXPAND_BUFFERS) |
| 4586 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4587 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4588 | if (xp->xp_numfiles != -1) |
| 4589 | { |
| 4590 | char_u *halved_slash; |
| 4591 | char_u *exp_path; |
| 4592 | |
| 4593 | /* Expansion was done before and special characters |
| 4594 | * were escaped, need to halve backslashes. Also |
| 4595 | * $HOME has been replaced with ~/. */ |
| 4596 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4597 | halved_slash = backslash_halve_save( |
| 4598 | exp_path != NULL ? exp_path : files_found[k]); |
| 4599 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4600 | : files_found[k]); |
| 4601 | vim_free(exp_path); |
| 4602 | vim_free(halved_slash); |
| 4603 | } |
| 4604 | else |
| 4605 | /* Expansion was done here, file names are literal. */ |
| 4606 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4607 | if (showtail) |
| 4608 | p = L_SHOWFILE(k); |
| 4609 | else |
| 4610 | { |
| 4611 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4612 | TRUE); |
| 4613 | p = NameBuff; |
| 4614 | } |
| 4615 | } |
| 4616 | else |
| 4617 | { |
| 4618 | j = FALSE; |
| 4619 | p = L_SHOWFILE(k); |
| 4620 | } |
| 4621 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4622 | } |
| 4623 | if (msg_col > 0) /* when not wrapped around */ |
| 4624 | { |
| 4625 | msg_clr_eos(); |
| 4626 | msg_putchar('\n'); |
| 4627 | } |
| 4628 | out_flush(); /* show one line at a time */ |
| 4629 | if (got_int) |
| 4630 | { |
| 4631 | got_int = FALSE; |
| 4632 | break; |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | /* |
| 4637 | * we redraw the command below the lines that we have just listed |
| 4638 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4639 | */ |
| 4640 | cmdline_row = msg_row; /* will put it back later */ |
| 4641 | } |
| 4642 | |
| 4643 | if (xp->xp_numfiles == -1) |
| 4644 | FreeWild(num_files, files_found); |
| 4645 | |
| 4646 | return EXPAND_OK; |
| 4647 | } |
| 4648 | |
| 4649 | /* |
| 4650 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4651 | * Find tail of file name path, but ignore trailing "/". |
| 4652 | */ |
| 4653 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4654 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4655 | { |
| 4656 | char_u *p; |
| 4657 | char_u *t = s; |
| 4658 | int had_sep = FALSE; |
| 4659 | |
| 4660 | for (p = s; *p != NUL; ) |
| 4661 | { |
| 4662 | if (vim_ispathsep(*p) |
| 4663 | #ifdef BACKSLASH_IN_FILENAME |
| 4664 | && !rem_backslash(p) |
| 4665 | #endif |
| 4666 | ) |
| 4667 | had_sep = TRUE; |
| 4668 | else if (had_sep) |
| 4669 | { |
| 4670 | t = p; |
| 4671 | had_sep = FALSE; |
| 4672 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4673 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4674 | } |
| 4675 | return t; |
| 4676 | } |
| 4677 | |
| 4678 | /* |
| 4679 | * Return TRUE if we only need to show the tail of completion matches. |
| 4680 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4681 | * returned. |
| 4682 | */ |
| 4683 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4684 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4685 | { |
| 4686 | char_u *s; |
| 4687 | char_u *end; |
| 4688 | |
| 4689 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4690 | if (xp->xp_context != EXPAND_FILES |
| 4691 | && xp->xp_context != EXPAND_SHELLCMD |
| 4692 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4693 | return FALSE; |
| 4694 | |
| 4695 | end = gettail(xp->xp_pattern); |
| 4696 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4697 | return FALSE; |
| 4698 | |
| 4699 | for (s = xp->xp_pattern; s < end; s++) |
| 4700 | { |
| 4701 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4702 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4703 | if (rem_backslash(s)) |
| 4704 | ++s; |
| 4705 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4706 | return FALSE; |
| 4707 | } |
| 4708 | return TRUE; |
| 4709 | } |
| 4710 | |
| 4711 | /* |
| 4712 | * Prepare a string for expansion. |
| 4713 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4714 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4715 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4716 | * the name into allocated memory and prepend "^". |
| 4717 | */ |
| 4718 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4719 | addstar( |
| 4720 | char_u *fname, |
| 4721 | int len, |
| 4722 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4723 | { |
| 4724 | char_u *retval; |
| 4725 | int i, j; |
| 4726 | int new_len; |
| 4727 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4728 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4730 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4731 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4732 | && context != EXPAND_SHELLCMD |
| 4733 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4734 | { |
| 4735 | /* |
| 4736 | * Matching will be done internally (on something other than files). |
| 4737 | * So we convert the file-matching-type wildcards into our kind for |
| 4738 | * use with vim_regcomp(). First work out how long it will be: |
| 4739 | */ |
| 4740 | |
| 4741 | /* For help tags the translation is done in find_help_tags(). |
| 4742 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4743 | if (context == EXPAND_HELP |
| 4744 | || context == EXPAND_COLORS |
| 4745 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4746 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4747 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4748 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4749 | || ((context == EXPAND_TAGS_LISTFILES |
| 4750 | || context == EXPAND_TAGS) |
| 4751 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4752 | retval = vim_strnsave(fname, len); |
| 4753 | else |
| 4754 | { |
| 4755 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4756 | for (i = 0; i < len; i++) |
| 4757 | { |
| 4758 | if (fname[i] == '*' || fname[i] == '~') |
| 4759 | new_len++; /* '*' needs to be replaced by ".*" |
| 4760 | '~' needs to be replaced by "\~" */ |
| 4761 | |
| 4762 | /* Buffer names are like file names. "." should be literal */ |
| 4763 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4764 | new_len++; /* "." becomes "\." */ |
| 4765 | |
| 4766 | /* Custom expansion takes care of special things, match |
| 4767 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4768 | if ((context == EXPAND_USER_DEFINED |
| 4769 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | new_len++; /* '\' becomes "\\" */ |
| 4771 | } |
| 4772 | retval = alloc(new_len); |
| 4773 | if (retval != NULL) |
| 4774 | { |
| 4775 | retval[0] = '^'; |
| 4776 | j = 1; |
| 4777 | for (i = 0; i < len; i++, j++) |
| 4778 | { |
| 4779 | /* Skip backslash. But why? At least keep it for custom |
| 4780 | * expansion. */ |
| 4781 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4782 | && context != EXPAND_USER_LIST |
| 4783 | && fname[i] == '\\' |
| 4784 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4785 | break; |
| 4786 | |
| 4787 | switch (fname[i]) |
| 4788 | { |
| 4789 | case '*': retval[j++] = '.'; |
| 4790 | break; |
| 4791 | case '~': retval[j++] = '\\'; |
| 4792 | break; |
| 4793 | case '?': retval[j] = '.'; |
| 4794 | continue; |
| 4795 | case '.': if (context == EXPAND_BUFFERS) |
| 4796 | retval[j++] = '\\'; |
| 4797 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4798 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4799 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4800 | retval[j++] = '\\'; |
| 4801 | break; |
| 4802 | } |
| 4803 | retval[j] = fname[i]; |
| 4804 | } |
| 4805 | retval[j] = NUL; |
| 4806 | } |
| 4807 | } |
| 4808 | } |
| 4809 | else |
| 4810 | { |
| 4811 | retval = alloc(len + 4); |
| 4812 | if (retval != NULL) |
| 4813 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4814 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | |
| 4816 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4817 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4818 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4819 | * ~ would be at the start of the file name, but not the tail. |
| 4820 | * $ could be anywhere in the tail. |
| 4821 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4822 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | */ |
| 4824 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4825 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4826 | #ifndef BACKSLASH_IN_FILENAME |
| 4827 | for (i = len - 2; i >= 0; --i) |
| 4828 | { |
| 4829 | if (retval[i] != '\\') |
| 4830 | break; |
| 4831 | ends_in_star = !ends_in_star; |
| 4832 | } |
| 4833 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4834 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4835 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4836 | && vim_strchr(tail, '$') == NULL |
| 4837 | && vim_strchr(retval, '`') == NULL) |
| 4838 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4839 | else if (len > 0 && retval[len - 1] == '$') |
| 4840 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4841 | retval[len] = NUL; |
| 4842 | } |
| 4843 | } |
| 4844 | return retval; |
| 4845 | } |
| 4846 | |
| 4847 | /* |
| 4848 | * Must parse the command line so far to work out what context we are in. |
| 4849 | * Completion can then be done based on that context. |
| 4850 | * This routine sets the variables: |
| 4851 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4852 | * the command line (ends at the cursor). |
| 4853 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4854 | * |
| 4855 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4856 | * the command line, like an unknown command. Caller |
| 4857 | * should beep. |
| 4858 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4859 | * a normal char, rather than for completion. eg |
| 4860 | * :s/^I/ |
| 4861 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4862 | * it. |
| 4863 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4864 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4865 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4866 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4867 | * when we know only directories are of interest. eg |
| 4868 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4869 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4870 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4871 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4872 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4873 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4874 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4875 | * EXPAND_EVENTS Complete event names |
| 4876 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4877 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4878 | * EXPAND_AUGROUP Complete autocommand group names |
| 4879 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4880 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4881 | * eg :unmap a^I , :cunab x^I |
| 4882 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4883 | * eg :call sub^I |
| 4884 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4885 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4886 | * names in expressions, eg :while s^I |
| 4887 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4888 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4889 | */ |
| 4890 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4891 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4892 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4893 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4894 | if (ccline.cmdfirstc != ':' |
| 4895 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4896 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4897 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | #endif |
| 4899 | ) |
| 4900 | { |
| 4901 | xp->xp_context = EXPAND_NOTHING; |
| 4902 | return; |
| 4903 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4904 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4908 | set_cmd_context( |
| 4909 | expand_T *xp, |
| 4910 | char_u *str, /* start of command line */ |
| 4911 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4912 | int col, /* position of cursor */ |
| 4913 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4914 | { |
| 4915 | int old_char = NUL; |
| 4916 | char_u *nextcomm; |
| 4917 | |
| 4918 | /* |
| 4919 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4920 | * written before. |
| 4921 | */ |
| 4922 | if (col < len) |
| 4923 | old_char = str[col]; |
| 4924 | str[col] = NUL; |
| 4925 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4926 | |
| 4927 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4928 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4929 | { |
| 4930 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4931 | /* pass CMD_SIZE because there is no real command */ |
| 4932 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4933 | # endif |
| 4934 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4935 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4936 | { |
| 4937 | xp->xp_context = ccline.xp_context; |
| 4938 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4939 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4940 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4941 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4942 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4943 | else |
| 4944 | #endif |
| 4945 | while (nextcomm != NULL) |
| 4946 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4947 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4948 | /* Store the string here so that call_user_expand_func() can get to them |
| 4949 | * easily. */ |
| 4950 | xp->xp_line = str; |
| 4951 | xp->xp_col = col; |
| 4952 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4953 | str[col] = old_char; |
| 4954 | } |
| 4955 | |
| 4956 | /* |
| 4957 | * Expand the command line "str" from context "xp". |
| 4958 | * "xp" must have been set by set_cmd_context(). |
| 4959 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4960 | * starts. |
| 4961 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4962 | * cursor. |
| 4963 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4964 | * key that triggered expansion literally. |
| 4965 | * Returns EXPAND_OK otherwise. |
| 4966 | */ |
| 4967 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4968 | expand_cmdline( |
| 4969 | expand_T *xp, |
| 4970 | char_u *str, /* start of command line */ |
| 4971 | int col, /* position of cursor */ |
| 4972 | int *matchcount, /* return: nr of matches */ |
| 4973 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4974 | { |
| 4975 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4976 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4977 | |
| 4978 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4979 | { |
| 4980 | beep_flush(); |
| 4981 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4982 | } |
| 4983 | if (xp->xp_context == EXPAND_NOTHING) |
| 4984 | { |
| 4985 | /* Caller can use the character as a normal char instead */ |
| 4986 | return EXPAND_NOTHING; |
| 4987 | } |
| 4988 | |
| 4989 | /* 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] | 4990 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4991 | 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] | 4992 | if (file_str == NULL) |
| 4993 | return EXPAND_UNSUCCESSFUL; |
| 4994 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4995 | if (p_wic) |
| 4996 | options += WILD_ICASE; |
| 4997 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4998 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4999 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | { |
| 5001 | *matchcount = 0; |
| 5002 | *matches = NULL; |
| 5003 | } |
| 5004 | vim_free(file_str); |
| 5005 | |
| 5006 | return EXPAND_OK; |
| 5007 | } |
| 5008 | |
| 5009 | #ifdef FEAT_MULTI_LANG |
| 5010 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5011 | * Cleanup matches for help tags: |
| 5012 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 5013 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5014 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5015 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5016 | |
| 5017 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5018 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | { |
| 5020 | int i, j; |
| 5021 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5022 | char_u buf[4]; |
| 5023 | char_u *p = buf; |
| 5024 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5025 | 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] | 5026 | { |
| 5027 | *p++ = '@'; |
| 5028 | *p++ = p_hlg[0]; |
| 5029 | *p++ = p_hlg[1]; |
| 5030 | } |
| 5031 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5032 | |
| 5033 | for (i = 0; i < num_file; ++i) |
| 5034 | { |
| 5035 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5036 | if (len <= 0) |
| 5037 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5038 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5039 | { |
| 5040 | /* Sorting on priority means the same item in another language may |
| 5041 | * be anywhere. Search all items for a match up to the "@en". */ |
| 5042 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5043 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 5044 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5045 | break; |
| 5046 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5047 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5048 | file[i][len] = NUL; |
| 5049 | } |
| 5050 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5051 | |
| 5052 | if (*buf != NUL) |
| 5053 | for (i = 0; i < num_file; ++i) |
| 5054 | { |
| 5055 | len = (int)STRLEN(file[i]) - 3; |
| 5056 | if (len <= 0) |
| 5057 | continue; |
| 5058 | if (STRCMP(file[i] + len, buf) == 0) |
| 5059 | { |
| 5060 | /* remove the default language */ |
| 5061 | file[i][len] = NUL; |
| 5062 | } |
| 5063 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5064 | } |
| 5065 | #endif |
| 5066 | |
| 5067 | /* |
| 5068 | * Do the expansion based on xp->xp_context and "pat". |
| 5069 | */ |
| 5070 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5071 | ExpandFromContext( |
| 5072 | expand_T *xp, |
| 5073 | char_u *pat, |
| 5074 | int *num_file, |
| 5075 | char_u ***file, |
| 5076 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | { |
| 5078 | #ifdef FEAT_CMDL_COMPL |
| 5079 | regmatch_T regmatch; |
| 5080 | #endif |
| 5081 | int ret; |
| 5082 | int flags; |
| 5083 | |
| 5084 | flags = EW_DIR; /* include directories */ |
| 5085 | if (options & WILD_LIST_NOTFOUND) |
| 5086 | flags |= EW_NOTFOUND; |
| 5087 | if (options & WILD_ADD_SLASH) |
| 5088 | flags |= EW_ADDSLASH; |
| 5089 | if (options & WILD_KEEP_ALL) |
| 5090 | flags |= EW_KEEPALL; |
| 5091 | if (options & WILD_SILENT) |
| 5092 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 5093 | if (options & WILD_ALLLINKS) |
| 5094 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5095 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5096 | if (xp->xp_context == EXPAND_FILES |
| 5097 | || xp->xp_context == EXPAND_DIRECTORIES |
| 5098 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | { |
| 5100 | /* |
| 5101 | * Expand file or directory names. |
| 5102 | */ |
| 5103 | int free_pat = FALSE; |
| 5104 | int i; |
| 5105 | |
| 5106 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5107 | * space */ |
| 5108 | if (xp->xp_backslash != XP_BS_NONE) |
| 5109 | { |
| 5110 | free_pat = TRUE; |
| 5111 | pat = vim_strsave(pat); |
| 5112 | for (i = 0; pat[i]; ++i) |
| 5113 | if (pat[i] == '\\') |
| 5114 | { |
| 5115 | if (xp->xp_backslash == XP_BS_THREE |
| 5116 | && pat[i + 1] == '\\' |
| 5117 | && pat[i + 2] == '\\' |
| 5118 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5119 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5120 | if (xp->xp_backslash == XP_BS_ONE |
| 5121 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5122 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | if (xp->xp_context == EXPAND_FILES) |
| 5127 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5128 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 5129 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5130 | else |
| 5131 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5132 | if (options & WILD_ICASE) |
| 5133 | flags |= EW_ICASE; |
| 5134 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5135 | /* Expand wildcards, supporting %:h and the like. */ |
| 5136 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5137 | if (free_pat) |
| 5138 | vim_free(pat); |
| 5139 | return ret; |
| 5140 | } |
| 5141 | |
| 5142 | *file = (char_u **)""; |
| 5143 | *num_file = 0; |
| 5144 | if (xp->xp_context == EXPAND_HELP) |
| 5145 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 5146 | /* With an empty argument we would get all the help tags, which is |
| 5147 | * very slow. Get matches for "help" instead. */ |
| 5148 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 5149 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5150 | { |
| 5151 | #ifdef FEAT_MULTI_LANG |
| 5152 | cleanup_help_tags(*num_file, *file); |
| 5153 | #endif |
| 5154 | return OK; |
| 5155 | } |
| 5156 | return FAIL; |
| 5157 | } |
| 5158 | |
| 5159 | #ifndef FEAT_CMDL_COMPL |
| 5160 | return FAIL; |
| 5161 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5162 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 5163 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5164 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 5165 | return ExpandOldSetting(num_file, file); |
| 5166 | if (xp->xp_context == EXPAND_BUFFERS) |
| 5167 | return ExpandBufnames(pat, num_file, file, options); |
| 5168 | if (xp->xp_context == EXPAND_TAGS |
| 5169 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 5170 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 5171 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5172 | { |
| 5173 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5174 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 5175 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5176 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5177 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5178 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 5179 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5180 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5181 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5182 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5183 | { |
| 5184 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5185 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5186 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5187 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5188 | { |
| 5189 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5190 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5191 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5192 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5193 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5194 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5195 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5196 | if (xp->xp_context == EXPAND_PACKADD) |
| 5197 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5198 | |
| 5199 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5200 | if (regmatch.regprog == NULL) |
| 5201 | return FAIL; |
| 5202 | |
| 5203 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 5204 | regmatch.rm_ic = ignorecase(pat); |
| 5205 | |
| 5206 | if (xp->xp_context == EXPAND_SETTINGS |
| 5207 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 5208 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 5209 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 5210 | ret = ExpandMappings(®match, num_file, file); |
| 5211 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5212 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 5213 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 5214 | # endif |
| 5215 | else |
| 5216 | { |
| 5217 | static struct expgen |
| 5218 | { |
| 5219 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 5220 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5221 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5222 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5223 | } tab[] = |
| 5224 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5225 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 5226 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 5227 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 5228 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5229 | #ifdef FEAT_CMDHIST |
| 5230 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 5231 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5233 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 5234 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5235 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 5236 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 5237 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5238 | #endif |
| 5239 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5240 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 5241 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 5242 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 5243 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5244 | #endif |
| 5245 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5246 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 5247 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5248 | #endif |
| 5249 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5250 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 5252 | #ifdef FEAT_PROFILE |
| 5253 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 5254 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5255 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5256 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 5257 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5258 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5259 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5260 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5261 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5262 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5263 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5264 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5265 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5266 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5267 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5268 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5269 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5270 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5272 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5273 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5274 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5275 | }; |
| 5276 | int i; |
| 5277 | |
| 5278 | /* |
| 5279 | * Find a context in the table and call the ExpandGeneric() with the |
| 5280 | * right function to do the expansion. |
| 5281 | */ |
| 5282 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5283 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | if (xp->xp_context == tab[i].context) |
| 5285 | { |
| 5286 | if (tab[i].ic) |
| 5287 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5288 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5289 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5290 | break; |
| 5291 | } |
| 5292 | } |
| 5293 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5294 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | |
| 5296 | return ret; |
| 5297 | #endif /* FEAT_CMDL_COMPL */ |
| 5298 | } |
| 5299 | |
| 5300 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5301 | /* |
| 5302 | * Expand a list of names. |
| 5303 | * |
| 5304 | * Generic function for command line completion. It calls a function to |
| 5305 | * obtain strings, one by one. The strings are matched against a regexp |
| 5306 | * program. Matching strings are copied into an array, which is returned. |
| 5307 | * |
| 5308 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5309 | */ |
| 5310 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5311 | ExpandGeneric( |
| 5312 | expand_T *xp, |
| 5313 | regmatch_T *regmatch, |
| 5314 | int *num_file, |
| 5315 | char_u ***file, |
| 5316 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5317 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5318 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5319 | { |
| 5320 | int i; |
| 5321 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5322 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5323 | char_u *str; |
| 5324 | |
| 5325 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5326 | * round == 0: count the number of matching names |
| 5327 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5329 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5330 | { |
| 5331 | for (i = 0; ; ++i) |
| 5332 | { |
| 5333 | str = (*func)(xp, i); |
| 5334 | if (str == NULL) /* end of list */ |
| 5335 | break; |
| 5336 | if (*str == NUL) /* skip empty strings */ |
| 5337 | continue; |
| 5338 | |
| 5339 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5340 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5341 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5342 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5343 | if (escaped) |
| 5344 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5345 | else |
| 5346 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | (*file)[count] = str; |
| 5348 | #ifdef FEAT_MENU |
| 5349 | if (func == get_menu_names && str != NULL) |
| 5350 | { |
| 5351 | /* test for separator added by get_menu_names() */ |
| 5352 | str += STRLEN(str) - 1; |
| 5353 | if (*str == '\001') |
| 5354 | *str = '.'; |
| 5355 | } |
| 5356 | #endif |
| 5357 | } |
| 5358 | ++count; |
| 5359 | } |
| 5360 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5361 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5362 | { |
| 5363 | if (count == 0) |
| 5364 | return OK; |
| 5365 | *num_file = count; |
| 5366 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5367 | if (*file == NULL) |
| 5368 | { |
| 5369 | *file = (char_u **)""; |
| 5370 | return FAIL; |
| 5371 | } |
| 5372 | count = 0; |
| 5373 | } |
| 5374 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5375 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5376 | /* Sort the results. Keep menu's in the specified order. */ |
| 5377 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5378 | { |
| 5379 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5380 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5381 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5382 | /* <SNR> functions should be sorted to the end. */ |
| 5383 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5384 | sort_func_compare); |
| 5385 | else |
| 5386 | sort_strings(*file, *num_file); |
| 5387 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5388 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5389 | #ifdef FEAT_CMDL_COMPL |
| 5390 | /* Reset the variables used for special highlight names expansion, so that |
| 5391 | * they don't show up when getting normal highlight names by ID. */ |
| 5392 | reset_expand_highlight(); |
| 5393 | #endif |
| 5394 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5395 | return OK; |
| 5396 | } |
| 5397 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5398 | /* |
| 5399 | * Complete a shell command. |
| 5400 | * Returns FAIL or OK; |
| 5401 | */ |
| 5402 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5403 | expand_shellcmd( |
| 5404 | char_u *filepat, /* pattern to match with command names */ |
| 5405 | int *num_file, /* return: number of matches */ |
| 5406 | char_u ***file, /* return: array with matches */ |
| 5407 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5408 | { |
| 5409 | char_u *pat; |
| 5410 | int i; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5411 | char_u *path = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5412 | int mustfree = FALSE; |
| 5413 | garray_T ga; |
| 5414 | char_u *buf = alloc(MAXPATHL); |
| 5415 | size_t l; |
| 5416 | char_u *s, *e; |
| 5417 | int flags = flagsarg; |
| 5418 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5419 | int did_curdir = FALSE; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5420 | hashtab_T found_ht; |
| 5421 | hashitem_T *hi; |
| 5422 | hash_T hash; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5423 | |
| 5424 | if (buf == NULL) |
| 5425 | return FAIL; |
| 5426 | |
| 5427 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5428 | * space */ |
| 5429 | pat = vim_strsave(filepat); |
| 5430 | for (i = 0; pat[i]; ++i) |
| 5431 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5432 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5433 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5434 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5435 | |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5436 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 5437 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5438 | path = (char_u *)"."; |
| 5439 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5440 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5441 | /* For an absolute name we don't use $PATH. */ |
| 5442 | if (!mch_isFullName(pat)) |
| 5443 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5444 | if (path == NULL) |
| 5445 | path = (char_u *)""; |
| 5446 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5447 | |
| 5448 | /* |
| 5449 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5450 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5451 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5452 | */ |
| 5453 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5454 | hash_init(&found_ht); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5455 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5456 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5457 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5458 | e = vim_strchr(s, ';'); |
| 5459 | #else |
| 5460 | e = vim_strchr(s, ':'); |
| 5461 | #endif |
| 5462 | if (e == NULL) |
| 5463 | e = s + STRLEN(s); |
| 5464 | |
Bram Moolenaar | 6ab9e42 | 2018-07-28 19:20:13 +0200 | [diff] [blame] | 5465 | if (*s == NUL) |
| 5466 | { |
| 5467 | if (did_curdir) |
| 5468 | break; |
| 5469 | // Find directories in the current directory, path is empty. |
| 5470 | did_curdir = TRUE; |
| 5471 | flags |= EW_DIR; |
| 5472 | } |
| 5473 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 5474 | { |
| 5475 | did_curdir = TRUE; |
| 5476 | flags |= EW_DIR; |
| 5477 | } |
| 5478 | else |
| 5479 | // Do not match directories inside a $PATH item. |
| 5480 | flags &= ~EW_DIR; |
| 5481 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5482 | l = e - s; |
| 5483 | if (l > MAXPATHL - 5) |
| 5484 | break; |
| 5485 | vim_strncpy(buf, s, l); |
| 5486 | add_pathsep(buf); |
| 5487 | l = STRLEN(buf); |
| 5488 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5489 | |
| 5490 | /* Expand matches in one directory of $PATH. */ |
| 5491 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5492 | if (ret == OK) |
| 5493 | { |
| 5494 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5495 | FreeWild(*num_file, *file); |
| 5496 | else |
| 5497 | { |
| 5498 | for (i = 0; i < *num_file; ++i) |
| 5499 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5500 | char_u *name = (*file)[i]; |
| 5501 | |
| 5502 | if (STRLEN(name) > l) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5503 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5504 | // Check if this name was already found. |
| 5505 | hash = hash_hash(name + l); |
| 5506 | hi = hash_lookup(&found_ht, name + l, hash); |
| 5507 | if (HASHITEM_EMPTY(hi)) |
| 5508 | { |
| 5509 | // Remove the path that was prepended. |
| 5510 | STRMOVE(name, name + l); |
| 5511 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 5512 | hash_add_item(&found_ht, hi, name, hash); |
| 5513 | name = NULL; |
| 5514 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5515 | } |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5516 | vim_free(name); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5517 | } |
| 5518 | vim_free(*file); |
| 5519 | } |
| 5520 | } |
| 5521 | if (*e != NUL) |
| 5522 | ++e; |
| 5523 | } |
| 5524 | *file = ga.ga_data; |
| 5525 | *num_file = ga.ga_len; |
| 5526 | |
| 5527 | vim_free(buf); |
| 5528 | vim_free(pat); |
| 5529 | if (mustfree) |
| 5530 | vim_free(path); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5531 | hash_clear(&found_ht); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5532 | return OK; |
| 5533 | } |
| 5534 | |
| 5535 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5536 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5537 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5538 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5539 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5540 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5541 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5542 | call_user_expand_func( |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5543 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5544 | expand_T *xp, |
| 5545 | int *num_file, |
| 5546 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5547 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5548 | int keep = 0; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5549 | typval_T args[4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5550 | int save_current_SID = current_SID; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5551 | char_u *pat = NULL; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5552 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5553 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5554 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5555 | 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] | 5556 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5557 | *num_file = 0; |
| 5558 | *file = NULL; |
| 5559 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5560 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5561 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5562 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5563 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5564 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5565 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5566 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 5567 | |
| 5568 | args[0].v_type = VAR_STRING; |
| 5569 | args[0].vval.v_string = pat; |
| 5570 | args[1].v_type = VAR_STRING; |
| 5571 | args[1].vval.v_string = xp->xp_line; |
| 5572 | args[2].v_type = VAR_NUMBER; |
| 5573 | args[2].vval.v_number = xp->xp_col; |
| 5574 | args[3].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5575 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5576 | /* Save the cmdline, we don't know what the function may do. */ |
| 5577 | save_ccline = ccline; |
| 5578 | ccline.cmdbuff = NULL; |
| 5579 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5580 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5581 | |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5582 | ret = user_expand_func(xp->xp_arg, 3, args); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5583 | |
| 5584 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5585 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5586 | if (ccline.cmdbuff != NULL) |
| 5587 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5588 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5589 | vim_free(pat); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5590 | return ret; |
| 5591 | } |
| 5592 | |
| 5593 | /* |
| 5594 | * Expand names with a function defined by the user. |
| 5595 | */ |
| 5596 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5597 | ExpandUserDefined( |
| 5598 | expand_T *xp, |
| 5599 | regmatch_T *regmatch, |
| 5600 | int *num_file, |
| 5601 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5602 | { |
| 5603 | char_u *retstr; |
| 5604 | char_u *s; |
| 5605 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5606 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5607 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5608 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5609 | |
| 5610 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5611 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5612 | return FAIL; |
| 5613 | |
| 5614 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5615 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5616 | { |
| 5617 | e = vim_strchr(s, '\n'); |
| 5618 | if (e == NULL) |
| 5619 | e = s + STRLEN(s); |
| 5620 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5621 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5622 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5623 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5624 | *e = keep; |
| 5625 | |
| 5626 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5627 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5628 | if (ga_grow(&ga, 1) == FAIL) |
| 5629 | break; |
| 5630 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5631 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | if (*e != NUL) |
| 5635 | ++e; |
| 5636 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5637 | vim_free(retstr); |
| 5638 | *file = ga.ga_data; |
| 5639 | *num_file = ga.ga_len; |
| 5640 | return OK; |
| 5641 | } |
| 5642 | |
| 5643 | /* |
| 5644 | * Expand names with a list returned by a function defined by the user. |
| 5645 | */ |
| 5646 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5647 | ExpandUserList( |
| 5648 | expand_T *xp, |
| 5649 | int *num_file, |
| 5650 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5651 | { |
| 5652 | list_T *retlist; |
| 5653 | listitem_T *li; |
| 5654 | garray_T ga; |
| 5655 | |
| 5656 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5657 | if (retlist == NULL) |
| 5658 | return FAIL; |
| 5659 | |
| 5660 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5661 | /* Loop over the items in the list. */ |
| 5662 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5663 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5664 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5665 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5666 | |
| 5667 | if (ga_grow(&ga, 1) == FAIL) |
| 5668 | break; |
| 5669 | |
| 5670 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5671 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5672 | ++ga.ga_len; |
| 5673 | } |
| 5674 | list_unref(retlist); |
| 5675 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5676 | *file = ga.ga_data; |
| 5677 | *num_file = ga.ga_len; |
| 5678 | return OK; |
| 5679 | } |
| 5680 | #endif |
| 5681 | |
| 5682 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5683 | * Expand color scheme, compiler or filetype names. |
| 5684 | * Search from 'runtimepath': |
| 5685 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5686 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5687 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5688 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5689 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5690 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | */ |
| 5692 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5693 | ExpandRTDir( |
| 5694 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5695 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5696 | int *num_file, |
| 5697 | char_u ***file, |
| 5698 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5699 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5700 | char_u *s; |
| 5701 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5702 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5703 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5704 | int i; |
| 5705 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5706 | |
| 5707 | *num_file = 0; |
| 5708 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5709 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5710 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5711 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5712 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5713 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5714 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5715 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5716 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5717 | ga_clear_strings(&ga); |
| 5718 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5719 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5720 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5721 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5722 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5723 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5724 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5725 | if (flags & DIP_START) { |
| 5726 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5727 | { |
| 5728 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5729 | if (s == NULL) |
| 5730 | { |
| 5731 | ga_clear_strings(&ga); |
| 5732 | return FAIL; |
| 5733 | } |
| 5734 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5735 | globpath(p_pp, s, &ga, 0); |
| 5736 | vim_free(s); |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | if (flags & DIP_OPT) { |
| 5741 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5742 | { |
| 5743 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5744 | if (s == NULL) |
| 5745 | { |
| 5746 | ga_clear_strings(&ga); |
| 5747 | return FAIL; |
| 5748 | } |
| 5749 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5750 | globpath(p_pp, s, &ga, 0); |
| 5751 | vim_free(s); |
| 5752 | } |
| 5753 | } |
| 5754 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5755 | for (i = 0; i < ga.ga_len; ++i) |
| 5756 | { |
| 5757 | match = ((char_u **)ga.ga_data)[i]; |
| 5758 | s = match; |
| 5759 | e = s + STRLEN(s); |
| 5760 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5761 | { |
| 5762 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5763 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5764 | if (s < match || vim_ispathsep(*s)) |
| 5765 | break; |
| 5766 | ++s; |
| 5767 | *e = NUL; |
| 5768 | mch_memmove(match, s, e - s + 1); |
| 5769 | } |
| 5770 | } |
| 5771 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5772 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5773 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5774 | |
| 5775 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5776 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5777 | remove_duplicates(&ga); |
| 5778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | *file = ga.ga_data; |
| 5780 | *num_file = ga.ga_len; |
| 5781 | return OK; |
| 5782 | } |
| 5783 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5784 | /* |
| 5785 | * Expand loadplugin names: |
| 5786 | * 'packpath'/pack/ * /opt/{pat} |
| 5787 | */ |
| 5788 | static int |
| 5789 | ExpandPackAddDir( |
| 5790 | char_u *pat, |
| 5791 | int *num_file, |
| 5792 | char_u ***file) |
| 5793 | { |
| 5794 | char_u *s; |
| 5795 | char_u *e; |
| 5796 | char_u *match; |
| 5797 | garray_T ga; |
| 5798 | int i; |
| 5799 | int pat_len; |
| 5800 | |
| 5801 | *num_file = 0; |
| 5802 | *file = NULL; |
| 5803 | pat_len = (int)STRLEN(pat); |
| 5804 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5805 | |
| 5806 | s = alloc((unsigned)(pat_len + 26)); |
| 5807 | if (s == NULL) |
| 5808 | { |
| 5809 | ga_clear_strings(&ga); |
| 5810 | return FAIL; |
| 5811 | } |
| 5812 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5813 | globpath(p_pp, s, &ga, 0); |
| 5814 | vim_free(s); |
| 5815 | |
| 5816 | for (i = 0; i < ga.ga_len; ++i) |
| 5817 | { |
| 5818 | match = ((char_u **)ga.ga_data)[i]; |
| 5819 | s = gettail(match); |
| 5820 | e = s + STRLEN(s); |
| 5821 | mch_memmove(match, s, e - s + 1); |
| 5822 | } |
| 5823 | |
| 5824 | if (ga.ga_len == 0) |
| 5825 | return FAIL; |
| 5826 | |
| 5827 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5828 | * directories in dirnames. */ |
| 5829 | remove_duplicates(&ga); |
| 5830 | |
| 5831 | *file = ga.ga_data; |
| 5832 | *num_file = ga.ga_len; |
| 5833 | return OK; |
| 5834 | } |
| 5835 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5836 | #endif |
| 5837 | |
| 5838 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5839 | /* |
| 5840 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5841 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5842 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5843 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5844 | globpath( |
| 5845 | char_u *path, |
| 5846 | char_u *file, |
| 5847 | garray_T *ga, |
| 5848 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5849 | { |
| 5850 | expand_T xpc; |
| 5851 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5852 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5853 | int num_p; |
| 5854 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5855 | |
| 5856 | buf = alloc(MAXPATHL); |
| 5857 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5858 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5859 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5860 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5861 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5862 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5863 | /* Loop over all entries in {path}. */ |
| 5864 | while (*path != NUL) |
| 5865 | { |
| 5866 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5867 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5868 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5869 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5870 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5871 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5872 | * treat it as an escape character, use '/' instead. */ |
| 5873 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5874 | STRCAT(buf, "/"); |
| 5875 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5876 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5877 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5878 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5879 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5880 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5882 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5883 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5884 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5885 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | for (i = 0; i < num_p; ++i) |
| 5887 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5888 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5889 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5890 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5892 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5893 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5894 | FreeWild(num_p, p); |
| 5895 | } |
| 5896 | } |
| 5897 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5898 | |
| 5899 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | #endif |
| 5903 | |
| 5904 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5905 | |
| 5906 | /********************************* |
| 5907 | * Command line history stuff * |
| 5908 | *********************************/ |
| 5909 | |
| 5910 | /* |
| 5911 | * Translate a history character to the associated type number. |
| 5912 | */ |
| 5913 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5914 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | { |
| 5916 | if (c == ':') |
| 5917 | return HIST_CMD; |
| 5918 | if (c == '=') |
| 5919 | return HIST_EXPR; |
| 5920 | if (c == '@') |
| 5921 | return HIST_INPUT; |
| 5922 | if (c == '>') |
| 5923 | return HIST_DEBUG; |
| 5924 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5925 | } |
| 5926 | |
| 5927 | /* |
| 5928 | * Table of history names. |
| 5929 | * These names are used in :history and various hist...() functions. |
| 5930 | * It is sufficient to give the significant prefix of a history name. |
| 5931 | */ |
| 5932 | |
| 5933 | static char *(history_names[]) = |
| 5934 | { |
| 5935 | "cmd", |
| 5936 | "search", |
| 5937 | "expr", |
| 5938 | "input", |
| 5939 | "debug", |
| 5940 | NULL |
| 5941 | }; |
| 5942 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5943 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5944 | /* |
| 5945 | * Function given to ExpandGeneric() to obtain the possible first |
| 5946 | * arguments of the ":history command. |
| 5947 | */ |
| 5948 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5949 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5950 | { |
| 5951 | static char_u compl[2] = { NUL, NUL }; |
| 5952 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5953 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5954 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5955 | |
| 5956 | if (idx < short_names_count) |
| 5957 | { |
| 5958 | compl[0] = (char_u)short_names[idx]; |
| 5959 | return compl; |
| 5960 | } |
| 5961 | if (idx < short_names_count + history_name_count) |
| 5962 | return (char_u *)history_names[idx - short_names_count]; |
| 5963 | if (idx == short_names_count + history_name_count) |
| 5964 | return (char_u *)"all"; |
| 5965 | return NULL; |
| 5966 | } |
| 5967 | #endif |
| 5968 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5969 | /* |
| 5970 | * init_history() - Initialize the command line history. |
| 5971 | * Also used to re-allocate the history when the size changes. |
| 5972 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5973 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5974 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5975 | { |
| 5976 | int newlen; /* new length of history table */ |
| 5977 | histentry_T *temp; |
| 5978 | int i; |
| 5979 | int j; |
| 5980 | int type; |
| 5981 | |
| 5982 | /* |
| 5983 | * If size of history table changed, reallocate it |
| 5984 | */ |
| 5985 | newlen = (int)p_hi; |
| 5986 | if (newlen != hislen) /* history length changed */ |
| 5987 | { |
| 5988 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5989 | { |
| 5990 | if (newlen) |
| 5991 | { |
| 5992 | temp = (histentry_T *)lalloc( |
| 5993 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5994 | if (temp == NULL) /* out of memory! */ |
| 5995 | { |
| 5996 | if (type == 0) /* first one: just keep the old length */ |
| 5997 | { |
| 5998 | newlen = hislen; |
| 5999 | break; |
| 6000 | } |
| 6001 | /* Already changed one table, now we can only have zero |
| 6002 | * length for all tables. */ |
| 6003 | newlen = 0; |
| 6004 | type = -1; |
| 6005 | continue; |
| 6006 | } |
| 6007 | } |
| 6008 | else |
| 6009 | temp = NULL; |
| 6010 | if (newlen == 0 || temp != NULL) |
| 6011 | { |
| 6012 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 6013 | { |
| 6014 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6015 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6016 | } |
| 6017 | else if (newlen > hislen) /* array becomes bigger */ |
| 6018 | { |
| 6019 | for (i = 0; i <= hisidx[type]; ++i) |
| 6020 | temp[i] = history[type][i]; |
| 6021 | j = i; |
| 6022 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6023 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6024 | for ( ; j < hislen; ++i, ++j) |
| 6025 | temp[i] = history[type][j]; |
| 6026 | } |
| 6027 | else /* array becomes smaller or 0 */ |
| 6028 | { |
| 6029 | j = hisidx[type]; |
| 6030 | for (i = newlen - 1; ; --i) |
| 6031 | { |
| 6032 | if (i >= 0) /* copy newest entries */ |
| 6033 | temp[i] = history[type][j]; |
| 6034 | else /* remove older entries */ |
| 6035 | vim_free(history[type][j].hisstr); |
| 6036 | if (--j < 0) |
| 6037 | j = hislen - 1; |
| 6038 | if (j == hisidx[type]) |
| 6039 | break; |
| 6040 | } |
| 6041 | hisidx[type] = newlen - 1; |
| 6042 | } |
| 6043 | vim_free(history[type]); |
| 6044 | history[type] = temp; |
| 6045 | } |
| 6046 | } |
| 6047 | hislen = newlen; |
| 6048 | } |
| 6049 | } |
| 6050 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6051 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6052 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6053 | { |
| 6054 | hisptr->hisnum = 0; |
| 6055 | hisptr->viminfo = FALSE; |
| 6056 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6057 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6058 | } |
| 6059 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6060 | /* |
| 6061 | * Check if command line 'str' is already in history. |
| 6062 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 6063 | */ |
| 6064 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6065 | in_history( |
| 6066 | int type, |
| 6067 | char_u *str, |
| 6068 | int move_to_front, /* Move the entry to the front if it exists */ |
| 6069 | int sep, |
| 6070 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6071 | { |
| 6072 | int i; |
| 6073 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6074 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6075 | |
| 6076 | if (hisidx[type] < 0) |
| 6077 | return FALSE; |
| 6078 | i = hisidx[type]; |
| 6079 | do |
| 6080 | { |
| 6081 | if (history[type][i].hisstr == NULL) |
| 6082 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6083 | |
| 6084 | /* For search history, check that the separator character matches as |
| 6085 | * well. */ |
| 6086 | p = history[type][i].hisstr; |
| 6087 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6088 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6089 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | { |
| 6091 | if (!move_to_front) |
| 6092 | return TRUE; |
| 6093 | last_i = i; |
| 6094 | break; |
| 6095 | } |
| 6096 | if (--i < 0) |
| 6097 | i = hislen - 1; |
| 6098 | } while (i != hisidx[type]); |
| 6099 | |
| 6100 | if (last_i >= 0) |
| 6101 | { |
| 6102 | str = history[type][i].hisstr; |
| 6103 | while (i != hisidx[type]) |
| 6104 | { |
| 6105 | if (++i >= hislen) |
| 6106 | i = 0; |
| 6107 | history[type][last_i] = history[type][i]; |
| 6108 | last_i = i; |
| 6109 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6110 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6111 | history[type][i].viminfo = FALSE; |
| 6112 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6113 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6114 | return TRUE; |
| 6115 | } |
| 6116 | return FALSE; |
| 6117 | } |
| 6118 | |
| 6119 | /* |
| 6120 | * Convert history name (from table above) to its HIST_ equivalent. |
| 6121 | * When "name" is empty, return "cmd" history. |
| 6122 | * Returns -1 for unknown history name. |
| 6123 | */ |
| 6124 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6125 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6126 | { |
| 6127 | int i; |
| 6128 | int len = (int)STRLEN(name); |
| 6129 | |
| 6130 | /* No argument: use current history. */ |
| 6131 | if (len == 0) |
| 6132 | return hist_char2type(ccline.cmdfirstc); |
| 6133 | |
| 6134 | for (i = 0; history_names[i] != NULL; ++i) |
| 6135 | if (STRNICMP(name, history_names[i], len) == 0) |
| 6136 | return i; |
| 6137 | |
| 6138 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 6139 | return hist_char2type(name[0]); |
| 6140 | |
| 6141 | return -1; |
| 6142 | } |
| 6143 | |
| 6144 | static int last_maptick = -1; /* last seen maptick */ |
| 6145 | |
| 6146 | /* |
| 6147 | * Add the given string to the given history. If the string is already in the |
| 6148 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 6149 | * values. |
| 6150 | */ |
| 6151 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6152 | add_to_history( |
| 6153 | int histype, |
| 6154 | char_u *new_entry, |
| 6155 | int in_map, /* consider maptick when inside a mapping */ |
| 6156 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6157 | { |
| 6158 | histentry_T *hisptr; |
| 6159 | int len; |
| 6160 | |
| 6161 | if (hislen == 0) /* no history */ |
| 6162 | return; |
| 6163 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 6164 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 6165 | return; |
| 6166 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6167 | /* |
| 6168 | * Searches inside the same mapping overwrite each other, so that only |
| 6169 | * the last line is kept. Be careful not to remove a line that was moved |
| 6170 | * down, only lines that were added. |
| 6171 | */ |
| 6172 | if (histype == HIST_SEARCH && in_map) |
| 6173 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 6174 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6175 | { |
| 6176 | /* Current line is from the same mapping, remove it */ |
| 6177 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 6178 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6179 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6180 | --hisnum[histype]; |
| 6181 | if (--hisidx[HIST_SEARCH] < 0) |
| 6182 | hisidx[HIST_SEARCH] = hislen - 1; |
| 6183 | } |
| 6184 | last_maptick = -1; |
| 6185 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6186 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6187 | { |
| 6188 | if (++hisidx[histype] == hislen) |
| 6189 | hisidx[histype] = 0; |
| 6190 | hisptr = &history[histype][hisidx[histype]]; |
| 6191 | vim_free(hisptr->hisstr); |
| 6192 | |
| 6193 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6194 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6195 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 6196 | if (hisptr->hisstr != NULL) |
| 6197 | hisptr->hisstr[len + 1] = sep; |
| 6198 | |
| 6199 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6200 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6201 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6202 | if (histype == HIST_SEARCH && in_map) |
| 6203 | last_maptick = maptick; |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6208 | |
| 6209 | /* |
| 6210 | * Get identifier of newest history entry. |
| 6211 | * "histype" may be one of the HIST_ values. |
| 6212 | */ |
| 6213 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6214 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6215 | { |
| 6216 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6217 | || hisidx[histype] < 0) |
| 6218 | return -1; |
| 6219 | |
| 6220 | return history[histype][hisidx[histype]].hisnum; |
| 6221 | } |
| 6222 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6223 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6224 | * Calculate history index from a number: |
| 6225 | * num > 0: seen as identifying number of a history entry |
| 6226 | * num < 0: relative position in history wrt newest entry |
| 6227 | * "histype" may be one of the HIST_ values. |
| 6228 | */ |
| 6229 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6230 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6231 | { |
| 6232 | int i; |
| 6233 | histentry_T *hist; |
| 6234 | int wrapped = FALSE; |
| 6235 | |
| 6236 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6237 | || (i = hisidx[histype]) < 0 || num == 0) |
| 6238 | return -1; |
| 6239 | |
| 6240 | hist = history[histype]; |
| 6241 | if (num > 0) |
| 6242 | { |
| 6243 | while (hist[i].hisnum > num) |
| 6244 | if (--i < 0) |
| 6245 | { |
| 6246 | if (wrapped) |
| 6247 | break; |
| 6248 | i += hislen; |
| 6249 | wrapped = TRUE; |
| 6250 | } |
| 6251 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 6252 | return i; |
| 6253 | } |
| 6254 | else if (-num <= hislen) |
| 6255 | { |
| 6256 | i += num + 1; |
| 6257 | if (i < 0) |
| 6258 | i += hislen; |
| 6259 | if (hist[i].hisstr != NULL) |
| 6260 | return i; |
| 6261 | } |
| 6262 | return -1; |
| 6263 | } |
| 6264 | |
| 6265 | /* |
| 6266 | * Get a history entry by its index. |
| 6267 | * "histype" may be one of the HIST_ values. |
| 6268 | */ |
| 6269 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6270 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6271 | { |
| 6272 | idx = calc_hist_idx(histype, idx); |
| 6273 | if (idx >= 0) |
| 6274 | return history[histype][idx].hisstr; |
| 6275 | else |
| 6276 | return (char_u *)""; |
| 6277 | } |
| 6278 | |
| 6279 | /* |
| 6280 | * Clear all entries of a history. |
| 6281 | * "histype" may be one of the HIST_ values. |
| 6282 | */ |
| 6283 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6284 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6285 | { |
| 6286 | int i; |
| 6287 | histentry_T *hisptr; |
| 6288 | |
| 6289 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6290 | { |
| 6291 | hisptr = history[histype]; |
| 6292 | for (i = hislen; i--;) |
| 6293 | { |
| 6294 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6295 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6296 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6297 | } |
| 6298 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6299 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6300 | return OK; |
| 6301 | } |
| 6302 | return FAIL; |
| 6303 | } |
| 6304 | |
| 6305 | /* |
| 6306 | * Remove all entries matching {str} from a history. |
| 6307 | * "histype" may be one of the HIST_ values. |
| 6308 | */ |
| 6309 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6310 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6311 | { |
| 6312 | regmatch_T regmatch; |
| 6313 | histentry_T *hisptr; |
| 6314 | int idx; |
| 6315 | int i; |
| 6316 | int last; |
| 6317 | int found = FALSE; |
| 6318 | |
| 6319 | regmatch.regprog = NULL; |
| 6320 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6321 | if (hislen != 0 |
| 6322 | && histype >= 0 |
| 6323 | && histype < HIST_COUNT |
| 6324 | && *str != NUL |
| 6325 | && (idx = hisidx[histype]) >= 0 |
| 6326 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6327 | != NULL) |
| 6328 | { |
| 6329 | i = last = idx; |
| 6330 | do |
| 6331 | { |
| 6332 | hisptr = &history[histype][i]; |
| 6333 | if (hisptr->hisstr == NULL) |
| 6334 | break; |
| 6335 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6336 | { |
| 6337 | found = TRUE; |
| 6338 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6339 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6340 | } |
| 6341 | else |
| 6342 | { |
| 6343 | if (i != last) |
| 6344 | { |
| 6345 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6346 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | } |
| 6348 | if (--last < 0) |
| 6349 | last += hislen; |
| 6350 | } |
| 6351 | if (--i < 0) |
| 6352 | i += hislen; |
| 6353 | } while (i != idx); |
| 6354 | if (history[histype][idx].hisstr == NULL) |
| 6355 | hisidx[histype] = -1; |
| 6356 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6357 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6358 | return found; |
| 6359 | } |
| 6360 | |
| 6361 | /* |
| 6362 | * Remove an indexed entry from a history. |
| 6363 | * "histype" may be one of the HIST_ values. |
| 6364 | */ |
| 6365 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6366 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6367 | { |
| 6368 | int i, j; |
| 6369 | |
| 6370 | i = calc_hist_idx(histype, idx); |
| 6371 | if (i < 0) |
| 6372 | return FALSE; |
| 6373 | idx = hisidx[histype]; |
| 6374 | vim_free(history[histype][i].hisstr); |
| 6375 | |
| 6376 | /* When deleting the last added search string in a mapping, reset |
| 6377 | * last_maptick, so that the last added search string isn't deleted again. |
| 6378 | */ |
| 6379 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6380 | last_maptick = -1; |
| 6381 | |
| 6382 | while (i != idx) |
| 6383 | { |
| 6384 | j = (i + 1) % hislen; |
| 6385 | history[histype][i] = history[histype][j]; |
| 6386 | i = j; |
| 6387 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6388 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6389 | if (--i < 0) |
| 6390 | i += hislen; |
| 6391 | hisidx[histype] = i; |
| 6392 | return TRUE; |
| 6393 | } |
| 6394 | |
| 6395 | #endif /* FEAT_EVAL */ |
| 6396 | |
| 6397 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6398 | /* |
| 6399 | * Very specific function to remove the value in ":set key=val" from the |
| 6400 | * history. |
| 6401 | */ |
| 6402 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6403 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | { |
| 6405 | char_u *p; |
| 6406 | int i; |
| 6407 | |
| 6408 | i = hisidx[HIST_CMD]; |
| 6409 | if (i < 0) |
| 6410 | return; |
| 6411 | p = history[HIST_CMD][i].hisstr; |
| 6412 | if (p != NULL) |
| 6413 | for ( ; *p; ++p) |
| 6414 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6415 | { |
| 6416 | p = vim_strchr(p + 3, '='); |
| 6417 | if (p == NULL) |
| 6418 | break; |
| 6419 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6420 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6421 | if (p[i] == '\\' && p[i + 1]) |
| 6422 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6423 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | --p; |
| 6425 | } |
| 6426 | } |
| 6427 | #endif |
| 6428 | |
| 6429 | #endif /* FEAT_CMDHIST */ |
| 6430 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6431 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6432 | /* |
| 6433 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6434 | * ccline and put the previous value in prev_ccline. |
| 6435 | */ |
| 6436 | static struct cmdline_info * |
| 6437 | get_ccline_ptr(void) |
| 6438 | { |
| 6439 | if ((State & CMDLINE) == 0) |
| 6440 | return NULL; |
| 6441 | if (ccline.cmdbuff != NULL) |
| 6442 | return &ccline; |
| 6443 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6444 | return &prev_ccline; |
| 6445 | return NULL; |
| 6446 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6447 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6448 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6449 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6450 | /* |
| 6451 | * Get the current command line in allocated memory. |
| 6452 | * Only works when the command line is being edited. |
| 6453 | * Returns NULL when something is wrong. |
| 6454 | */ |
| 6455 | char_u * |
| 6456 | get_cmdline_str(void) |
| 6457 | { |
| 6458 | struct cmdline_info *p = get_ccline_ptr(); |
| 6459 | |
| 6460 | if (p == NULL) |
| 6461 | return NULL; |
| 6462 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6463 | } |
| 6464 | |
| 6465 | /* |
| 6466 | * Get the current command line position, counted in bytes. |
| 6467 | * Zero is the first position. |
| 6468 | * Only works when the command line is being edited. |
| 6469 | * Returns -1 when something is wrong. |
| 6470 | */ |
| 6471 | int |
| 6472 | get_cmdline_pos(void) |
| 6473 | { |
| 6474 | struct cmdline_info *p = get_ccline_ptr(); |
| 6475 | |
| 6476 | if (p == NULL) |
| 6477 | return -1; |
| 6478 | return p->cmdpos; |
| 6479 | } |
| 6480 | |
| 6481 | /* |
| 6482 | * Set the command line byte position to "pos". Zero is the first position. |
| 6483 | * Only works when the command line is being edited. |
| 6484 | * Returns 1 when failed, 0 when OK. |
| 6485 | */ |
| 6486 | int |
| 6487 | set_cmdline_pos( |
| 6488 | int pos) |
| 6489 | { |
| 6490 | struct cmdline_info *p = get_ccline_ptr(); |
| 6491 | |
| 6492 | if (p == NULL) |
| 6493 | return 1; |
| 6494 | |
| 6495 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6496 | * changed the command line. */ |
| 6497 | if (pos < 0) |
| 6498 | new_cmdpos = 0; |
| 6499 | else |
| 6500 | new_cmdpos = pos; |
| 6501 | return 0; |
| 6502 | } |
| 6503 | #endif |
| 6504 | |
| 6505 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6506 | /* |
| 6507 | * Get the current command-line type. |
| 6508 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6509 | * Only works when the command line is being edited. |
| 6510 | * Returns NUL when something is wrong. |
| 6511 | */ |
| 6512 | int |
| 6513 | get_cmdline_type(void) |
| 6514 | { |
| 6515 | struct cmdline_info *p = get_ccline_ptr(); |
| 6516 | |
| 6517 | if (p == NULL) |
| 6518 | return NUL; |
| 6519 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6520 | return |
| 6521 | # ifdef FEAT_EVAL |
| 6522 | (p->input_fn) ? '@' : |
| 6523 | # endif |
| 6524 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6525 | return p->cmdfirstc; |
| 6526 | } |
| 6527 | #endif |
| 6528 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6529 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6530 | /* |
| 6531 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6532 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6533 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6534 | */ |
| 6535 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6536 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6537 | { |
| 6538 | int len; |
| 6539 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6540 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6541 | |
| 6542 | *str = skipwhite(*str); |
| 6543 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6544 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6545 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6546 | *str += len; |
| 6547 | *num1 = (int)num; |
| 6548 | first = TRUE; |
| 6549 | } |
| 6550 | *str = skipwhite(*str); |
| 6551 | if (**str == ',') /* parse "to" part of range */ |
| 6552 | { |
| 6553 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6554 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6555 | if (len > 0) |
| 6556 | { |
| 6557 | *num2 = (int)num; |
| 6558 | *str = skipwhite(*str + len); |
| 6559 | } |
| 6560 | else if (!first) /* no number given at all */ |
| 6561 | return FAIL; |
| 6562 | } |
| 6563 | else if (first) /* only one number given */ |
| 6564 | *num2 = *num1; |
| 6565 | return OK; |
| 6566 | } |
| 6567 | #endif |
| 6568 | |
| 6569 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6570 | /* |
| 6571 | * :history command - print a history |
| 6572 | */ |
| 6573 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6574 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6575 | { |
| 6576 | histentry_T *hist; |
| 6577 | int histype1 = HIST_CMD; |
| 6578 | int histype2 = HIST_CMD; |
| 6579 | int hisidx1 = 1; |
| 6580 | int hisidx2 = -1; |
| 6581 | int idx; |
| 6582 | int i, j, k; |
| 6583 | char_u *end; |
| 6584 | char_u *arg = eap->arg; |
| 6585 | |
| 6586 | if (hislen == 0) |
| 6587 | { |
| 6588 | MSG(_("'history' option is zero")); |
| 6589 | return; |
| 6590 | } |
| 6591 | |
| 6592 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6593 | { |
| 6594 | end = arg; |
| 6595 | while (ASCII_ISALPHA(*end) |
| 6596 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6597 | end++; |
| 6598 | i = *end; |
| 6599 | *end = NUL; |
| 6600 | histype1 = get_histtype(arg); |
| 6601 | if (histype1 == -1) |
| 6602 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6603 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6604 | { |
| 6605 | histype1 = 0; |
| 6606 | histype2 = HIST_COUNT-1; |
| 6607 | } |
| 6608 | else |
| 6609 | { |
| 6610 | *end = i; |
| 6611 | EMSG(_(e_trailing)); |
| 6612 | return; |
| 6613 | } |
| 6614 | } |
| 6615 | else |
| 6616 | histype2 = histype1; |
| 6617 | *end = i; |
| 6618 | } |
| 6619 | else |
| 6620 | end = arg; |
| 6621 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6622 | { |
| 6623 | EMSG(_(e_trailing)); |
| 6624 | return; |
| 6625 | } |
| 6626 | |
| 6627 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6628 | { |
| 6629 | STRCPY(IObuff, "\n # "); |
| 6630 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6631 | MSG_PUTS_TITLE(IObuff); |
| 6632 | idx = hisidx[histype1]; |
| 6633 | hist = history[histype1]; |
| 6634 | j = hisidx1; |
| 6635 | k = hisidx2; |
| 6636 | if (j < 0) |
| 6637 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6638 | if (k < 0) |
| 6639 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6640 | if (idx >= 0 && j <= k) |
| 6641 | for (i = idx + 1; !got_int; ++i) |
| 6642 | { |
| 6643 | if (i == hislen) |
| 6644 | i = 0; |
| 6645 | if (hist[i].hisstr != NULL |
| 6646 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6647 | { |
| 6648 | msg_putchar('\n'); |
| 6649 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6650 | hist[i].hisnum); |
| 6651 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6652 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6653 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6654 | else |
| 6655 | STRCAT(IObuff, hist[i].hisstr); |
| 6656 | msg_outtrans(IObuff); |
| 6657 | out_flush(); |
| 6658 | } |
| 6659 | if (i == idx) |
| 6660 | break; |
| 6661 | } |
| 6662 | } |
| 6663 | } |
| 6664 | #endif |
| 6665 | |
| 6666 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6667 | /* |
| 6668 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6669 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6670 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6671 | {NULL, NULL, NULL, NULL, NULL}; |
| 6672 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6673 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6674 | static int viminfo_add_at_front = FALSE; |
| 6675 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6676 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6677 | |
| 6678 | /* |
| 6679 | * Translate a history type number to the associated character. |
| 6680 | */ |
| 6681 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6682 | hist_type2char( |
| 6683 | int type, |
| 6684 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | { |
| 6686 | if (type == HIST_CMD) |
| 6687 | return ':'; |
| 6688 | if (type == HIST_SEARCH) |
| 6689 | { |
| 6690 | if (use_question) |
| 6691 | return '?'; |
| 6692 | else |
| 6693 | return '/'; |
| 6694 | } |
| 6695 | if (type == HIST_EXPR) |
| 6696 | return '='; |
| 6697 | return '@'; |
| 6698 | } |
| 6699 | |
| 6700 | /* |
| 6701 | * Prepare for reading the history from the viminfo file. |
| 6702 | * This allocates history arrays to store the read history lines. |
| 6703 | */ |
| 6704 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6705 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6706 | { |
| 6707 | int i; |
| 6708 | int num; |
| 6709 | int type; |
| 6710 | int len; |
| 6711 | |
| 6712 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6713 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6714 | if (asklen > hislen) |
| 6715 | asklen = hislen; |
| 6716 | |
| 6717 | for (type = 0; type < HIST_COUNT; ++type) |
| 6718 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6719 | /* Count the number of empty spaces in the history list. Entries read |
| 6720 | * from viminfo previously are also considered empty. If there are |
| 6721 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6722 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6723 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6724 | num++; |
| 6725 | len = asklen; |
| 6726 | if (num > len) |
| 6727 | len = num; |
| 6728 | if (len <= 0) |
| 6729 | viminfo_history[type] = NULL; |
| 6730 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6731 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6732 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6733 | if (viminfo_history[type] == NULL) |
| 6734 | len = 0; |
| 6735 | viminfo_hislen[type] = len; |
| 6736 | viminfo_hisidx[type] = 0; |
| 6737 | } |
| 6738 | } |
| 6739 | |
| 6740 | /* |
| 6741 | * Accept a line from the viminfo, store it in the history array when it's |
| 6742 | * new. |
| 6743 | */ |
| 6744 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6745 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6746 | { |
| 6747 | int type; |
| 6748 | long_u len; |
| 6749 | char_u *val; |
| 6750 | char_u *p; |
| 6751 | |
| 6752 | type = hist_char2type(virp->vir_line[0]); |
| 6753 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6754 | { |
| 6755 | val = viminfo_readstring(virp, 1, TRUE); |
| 6756 | if (val != NULL && *val != NUL) |
| 6757 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6758 | int sep = (*val == ' ' ? NUL : *val); |
| 6759 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6760 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6761 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6762 | { |
| 6763 | /* Need to re-allocate to append the separator byte. */ |
| 6764 | len = STRLEN(val); |
| 6765 | p = lalloc(len + 2, TRUE); |
| 6766 | if (p != NULL) |
| 6767 | { |
| 6768 | if (type == HIST_SEARCH) |
| 6769 | { |
| 6770 | /* Search entry: Move the separator from the first |
| 6771 | * column to after the NUL. */ |
| 6772 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6773 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6774 | } |
| 6775 | else |
| 6776 | { |
| 6777 | /* Not a search entry: No separator in the viminfo |
| 6778 | * file, add a NUL separator. */ |
| 6779 | mch_memmove(p, val, (size_t)len + 1); |
| 6780 | p[len + 1] = NUL; |
| 6781 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6782 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6783 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6784 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6785 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6786 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6787 | } |
| 6788 | } |
| 6789 | } |
| 6790 | vim_free(val); |
| 6791 | } |
| 6792 | return viminfo_readline(virp); |
| 6793 | } |
| 6794 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6795 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6796 | * Accept a new style history line from the viminfo, store it in the history |
| 6797 | * array when it's new. |
| 6798 | */ |
| 6799 | void |
| 6800 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6801 | garray_T *values, |
| 6802 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6803 | { |
| 6804 | int type; |
| 6805 | long_u len; |
| 6806 | char_u *val; |
| 6807 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6808 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6809 | |
| 6810 | /* Check the format: |
| 6811 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6812 | if (values->ga_len < 4 |
| 6813 | || vp[0].bv_type != BVAL_NR |
| 6814 | || vp[1].bv_type != BVAL_NR |
| 6815 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6816 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6817 | return; |
| 6818 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6819 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6820 | if (type >= HIST_COUNT) |
| 6821 | return; |
| 6822 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6823 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6824 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6825 | if (val != NULL && *val != NUL) |
| 6826 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6827 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6828 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6829 | int idx; |
| 6830 | int overwrite = FALSE; |
| 6831 | |
| 6832 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6833 | { |
| 6834 | /* If lines were written by an older Vim we need to avoid |
| 6835 | * getting duplicates. See if the entry already exists. */ |
| 6836 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6837 | { |
| 6838 | p = viminfo_history[type][idx].hisstr; |
| 6839 | if (STRCMP(val, p) == 0 |
| 6840 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6841 | { |
| 6842 | overwrite = TRUE; |
| 6843 | break; |
| 6844 | } |
| 6845 | } |
| 6846 | |
| 6847 | if (!overwrite) |
| 6848 | { |
| 6849 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6850 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6851 | p = lalloc(len + 2, TRUE); |
| 6852 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6853 | else |
| 6854 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6855 | if (p != NULL) |
| 6856 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6857 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6858 | if (!overwrite) |
| 6859 | { |
| 6860 | mch_memmove(p, val, (size_t)len + 1); |
| 6861 | /* Put the separator after the NUL. */ |
| 6862 | p[len + 1] = sep; |
| 6863 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6864 | viminfo_history[type][idx].hisnum = 0; |
| 6865 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6866 | viminfo_hisidx[type]++; |
| 6867 | } |
| 6868 | } |
| 6869 | } |
| 6870 | } |
| 6871 | } |
| 6872 | } |
| 6873 | |
| 6874 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6875 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6876 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6877 | static void |
| 6878 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6879 | { |
| 6880 | int idx; |
| 6881 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6882 | |
| 6883 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6884 | if (idx >= hislen) |
| 6885 | idx -= hislen; |
| 6886 | else if (idx < 0) |
| 6887 | idx = hislen - 1; |
| 6888 | if (viminfo_add_at_front) |
| 6889 | hisidx[type] = idx; |
| 6890 | else |
| 6891 | { |
| 6892 | if (hisidx[type] == -1) |
| 6893 | hisidx[type] = hislen - 1; |
| 6894 | do |
| 6895 | { |
| 6896 | if (history[type][idx].hisstr != NULL |
| 6897 | || history[type][idx].viminfo) |
| 6898 | break; |
| 6899 | if (++idx == hislen) |
| 6900 | idx = 0; |
| 6901 | } while (idx != hisidx[type]); |
| 6902 | if (idx != hisidx[type] && --idx < 0) |
| 6903 | idx = hislen - 1; |
| 6904 | } |
| 6905 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6906 | { |
| 6907 | vim_free(history[type][idx].hisstr); |
| 6908 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6909 | history[type][idx].viminfo = TRUE; |
| 6910 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6911 | if (--idx < 0) |
| 6912 | idx = hislen - 1; |
| 6913 | } |
| 6914 | idx += 1; |
| 6915 | idx %= hislen; |
| 6916 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6917 | { |
| 6918 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6919 | idx %= hislen; |
| 6920 | } |
| 6921 | } |
| 6922 | |
| 6923 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6924 | static int |
| 6925 | #ifdef __BORLANDC__ |
| 6926 | _RTLENTRYF |
| 6927 | #endif |
| 6928 | sort_hist(const void *s1, const void *s2) |
| 6929 | { |
| 6930 | histentry_T *p1 = *(histentry_T **)s1; |
| 6931 | histentry_T *p2 = *(histentry_T **)s2; |
| 6932 | |
| 6933 | if (p1->time_set < p2->time_set) return -1; |
| 6934 | if (p1->time_set > p2->time_set) return 1; |
| 6935 | return 0; |
| 6936 | } |
| 6937 | #endif |
| 6938 | |
| 6939 | /* |
| 6940 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6941 | * timestamp; |
| 6942 | */ |
| 6943 | static void |
| 6944 | merge_history(int type) |
| 6945 | { |
| 6946 | int max_len; |
| 6947 | histentry_T **tot_hist; |
| 6948 | histentry_T *new_hist; |
| 6949 | int i; |
| 6950 | int len; |
| 6951 | |
| 6952 | /* Make one long list with all entries. */ |
| 6953 | max_len = hislen + viminfo_hisidx[type]; |
| 6954 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6955 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6956 | if (tot_hist == NULL || new_hist == NULL) |
| 6957 | { |
| 6958 | vim_free(tot_hist); |
| 6959 | vim_free(new_hist); |
| 6960 | return; |
| 6961 | } |
| 6962 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6963 | tot_hist[i] = &viminfo_history[type][i]; |
| 6964 | len = i; |
| 6965 | for (i = 0; i < hislen; i++) |
| 6966 | if (history[type][i].hisstr != NULL) |
| 6967 | tot_hist[len++] = &history[type][i]; |
| 6968 | |
| 6969 | /* Sort the list on timestamp. */ |
| 6970 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6971 | |
| 6972 | /* Keep the newest ones. */ |
| 6973 | for (i = 0; i < hislen; i++) |
| 6974 | { |
| 6975 | if (i < len) |
| 6976 | { |
| 6977 | new_hist[i] = *tot_hist[i]; |
| 6978 | tot_hist[i]->hisstr = NULL; |
| 6979 | if (new_hist[i].hisnum == 0) |
| 6980 | new_hist[i].hisnum = ++hisnum[type]; |
| 6981 | } |
| 6982 | else |
| 6983 | clear_hist_entry(&new_hist[i]); |
| 6984 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6985 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6986 | |
| 6987 | /* Free what is not kept. */ |
| 6988 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6989 | vim_free(viminfo_history[type][i].hisstr); |
| 6990 | for (i = 0; i < hislen; i++) |
| 6991 | vim_free(history[type][i].hisstr); |
| 6992 | vim_free(history[type]); |
| 6993 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6994 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | /* |
| 6998 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6999 | */ |
| 7000 | void |
| 7001 | finish_viminfo_history(vir_T *virp) |
| 7002 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7003 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7004 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7005 | |
| 7006 | for (type = 0; type < HIST_COUNT; ++type) |
| 7007 | { |
| 7008 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7009 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7010 | |
| 7011 | if (merge) |
| 7012 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7013 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7014 | concat_history(type); |
| 7015 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7016 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7017 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | } |
| 7019 | } |
| 7020 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7021 | /* |
| 7022 | * Write history to viminfo file in "fp". |
| 7023 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 7024 | * file, data is in viminfo_history[]. |
| 7025 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 7026 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7027 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7028 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7029 | { |
| 7030 | int i; |
| 7031 | int type; |
| 7032 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7033 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | |
| 7035 | init_history(); |
| 7036 | if (hislen == 0) |
| 7037 | return; |
| 7038 | for (type = 0; type < HIST_COUNT; ++type) |
| 7039 | { |
| 7040 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 7041 | if (num_saved == 0) |
| 7042 | continue; |
| 7043 | if (num_saved < 0) /* Use default */ |
| 7044 | num_saved = hislen; |
| 7045 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 7046 | type == HIST_CMD ? _("Command Line") : |
| 7047 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7048 | type == HIST_EXPR ? _("Expression") : |
| 7049 | type == HIST_INPUT ? _("Input Line") : |
| 7050 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7051 | if (num_saved > hislen) |
| 7052 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7053 | |
| 7054 | /* |
| 7055 | * Merge typed and viminfo history: |
| 7056 | * round 1: history of typed commands. |
| 7057 | * round 2: history from recently read viminfo. |
| 7058 | */ |
| 7059 | for (round = 1; round <= 2; ++round) |
| 7060 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 7061 | if (round == 1) |
| 7062 | /* start at newest entry, somewhere in the list */ |
| 7063 | i = hisidx[type]; |
| 7064 | else if (viminfo_hisidx[type] > 0) |
| 7065 | /* start at newest entry, first in the list */ |
| 7066 | i = 0; |
| 7067 | else |
| 7068 | /* empty list */ |
| 7069 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7070 | if (i >= 0) |
| 7071 | while (num_saved > 0 |
| 7072 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7073 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7074 | char_u *p; |
| 7075 | time_t timestamp; |
| 7076 | int c = NUL; |
| 7077 | |
| 7078 | if (round == 1) |
| 7079 | { |
| 7080 | p = history[type][i].hisstr; |
| 7081 | timestamp = history[type][i].time_set; |
| 7082 | } |
| 7083 | else |
| 7084 | { |
| 7085 | p = viminfo_history[type] == NULL ? NULL |
| 7086 | : viminfo_history[type][i].hisstr; |
| 7087 | timestamp = viminfo_history[type] == NULL ? 0 |
| 7088 | : viminfo_history[type][i].time_set; |
| 7089 | } |
| 7090 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7091 | if (p != NULL && (round == 2 |
| 7092 | || !merge |
| 7093 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7094 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7095 | --num_saved; |
| 7096 | fputc(hist_type2char(type, TRUE), fp); |
| 7097 | /* For the search history: put the separator in the |
| 7098 | * second column; use a space if there isn't one. */ |
| 7099 | if (type == HIST_SEARCH) |
| 7100 | { |
| 7101 | c = p[STRLEN(p) + 1]; |
| 7102 | putc(c == NUL ? ' ' : c, fp); |
| 7103 | } |
| 7104 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7105 | |
| 7106 | { |
| 7107 | char cbuf[NUMBUFLEN]; |
| 7108 | |
| 7109 | /* New style history with a bar line. Format: |
| 7110 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 7111 | if (c == NUL) |
| 7112 | cbuf[0] = NUL; |
| 7113 | else |
| 7114 | sprintf(cbuf, "%d", c); |
| 7115 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 7116 | type, (long)timestamp, cbuf); |
| 7117 | barline_writestring(fp, p, LSIZE - 20); |
| 7118 | putc('\n', fp); |
| 7119 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7120 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7121 | if (round == 1) |
| 7122 | { |
| 7123 | /* Decrement index, loop around and stop when back at |
| 7124 | * the start. */ |
| 7125 | if (--i < 0) |
| 7126 | i = hislen - 1; |
| 7127 | if (i == hisidx[type]) |
| 7128 | break; |
| 7129 | } |
| 7130 | else |
| 7131 | { |
| 7132 | /* Increment index. Stop at the end in the while. */ |
| 7133 | ++i; |
| 7134 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7135 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7136 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 7137 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7138 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7139 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7140 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 7141 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7142 | } |
| 7143 | } |
| 7144 | #endif /* FEAT_VIMINFO */ |
| 7145 | |
| 7146 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 7147 | /* |
| 7148 | * Write a character at the current cursor+offset position. |
| 7149 | * It is directly written into the command buffer block. |
| 7150 | */ |
| 7151 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7152 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7153 | { |
| 7154 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7155 | { |
| 7156 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 7157 | return; |
| 7158 | } |
| 7159 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 7160 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 7161 | } |
| 7162 | |
| 7163 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7164 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7165 | { |
| 7166 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7167 | { |
| 7168 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 7169 | return NUL; |
| 7170 | } |
| 7171 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 7172 | } |
| 7173 | #endif |
| 7174 | |
| 7175 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 7176 | /* |
| 7177 | * Open a window on the current command line and history. Allow editing in |
| 7178 | * the window. Returns when the window is closed. |
| 7179 | * Returns: |
| 7180 | * CR if the command is to be executed |
| 7181 | * Ctrl_C if it is to be abandoned |
| 7182 | * K_IGNORE if editing continues |
| 7183 | */ |
| 7184 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7185 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7186 | { |
| 7187 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7188 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7189 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7190 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7191 | win_T *wp; |
| 7192 | int i; |
| 7193 | linenr_T lnum; |
| 7194 | int histtype; |
| 7195 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7196 | int save_restart_edit = restart_edit; |
| 7197 | int save_State = State; |
| 7198 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7199 | #ifdef FEAT_RIGHTLEFT |
| 7200 | int save_cmdmsg_rl = cmdmsg_rl; |
| 7201 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7202 | #ifdef FEAT_FOLDING |
| 7203 | int save_KeyTyped; |
| 7204 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7205 | |
| 7206 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 7207 | if (cmdwin_type != 0 |
| 7208 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 7209 | || cmdline_star > 0 |
| 7210 | # endif |
| 7211 | ) |
| 7212 | { |
| 7213 | beep_flush(); |
| 7214 | return K_IGNORE; |
| 7215 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7216 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | |
| 7218 | /* Save current window sizes. */ |
| 7219 | win_size_save(&winsizes); |
| 7220 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7222 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7223 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7224 | /* don't use a new tab page */ |
| 7225 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7226 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7227 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7228 | /* Create a window for the command-line buffer. */ |
| 7229 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 7230 | { |
| 7231 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7232 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7233 | return K_IGNORE; |
| 7234 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7235 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7236 | |
| 7237 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 7238 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7239 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7240 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 7242 | #ifdef FEAT_FOLDING |
| 7243 | curwin->w_p_fen = FALSE; |
| 7244 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7245 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7246 | curwin->w_p_rl = cmdmsg_rl; |
| 7247 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 7249 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7251 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7252 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7253 | /* But don't allow switching to another buffer. */ |
| 7254 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7255 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7256 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 7257 | need_wait_return = FALSE; |
| 7258 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7259 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7260 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 7261 | { |
| 7262 | if (p_wc == TAB) |
| 7263 | { |
| 7264 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 7265 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 7266 | } |
| 7267 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 7268 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7269 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7270 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 7271 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7272 | * sets 'textwidth' to 78). */ |
| 7273 | curbuf->b_p_tw = 0; |
| 7274 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7275 | /* Fill the buffer with the history. */ |
| 7276 | init_history(); |
| 7277 | if (hislen > 0) |
| 7278 | { |
| 7279 | i = hisidx[histtype]; |
| 7280 | if (i >= 0) |
| 7281 | { |
| 7282 | lnum = 0; |
| 7283 | do |
| 7284 | { |
| 7285 | if (++i == hislen) |
| 7286 | i = 0; |
| 7287 | if (history[histtype][i].hisstr != NULL) |
| 7288 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7289 | (colnr_T)0, FALSE); |
| 7290 | } |
| 7291 | while (i != hisidx[histtype]); |
| 7292 | } |
| 7293 | } |
| 7294 | |
| 7295 | /* Replace the empty last line with the current command-line and put the |
| 7296 | * cursor there. */ |
| 7297 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7298 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7299 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7300 | changed_line_abv_curs(); |
| 7301 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7302 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7303 | |
| 7304 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7305 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7306 | |
| 7307 | /* No Ex mode here! */ |
| 7308 | exmode_active = 0; |
| 7309 | |
| 7310 | State = NORMAL; |
| 7311 | # ifdef FEAT_MOUSE |
| 7312 | setmouse(); |
| 7313 | # endif |
| 7314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7315 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7316 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7317 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7318 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7319 | |
| 7320 | i = RedrawingDisabled; |
| 7321 | RedrawingDisabled = 0; |
| 7322 | |
| 7323 | /* |
| 7324 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7325 | */ |
| 7326 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7327 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7328 | |
| 7329 | RedrawingDisabled = i; |
| 7330 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7331 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7332 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7333 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7335 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7336 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7337 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7338 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7339 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7340 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7341 | # endif |
| 7342 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7343 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7344 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7345 | cmdwin_type = 0; |
| 7346 | |
| 7347 | exmode_active = save_exmode; |
| 7348 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7349 | /* 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] | 7350 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7351 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | { |
| 7353 | cmdwin_result = Ctrl_C; |
| 7354 | EMSG(_("E199: Active window or buffer deleted")); |
| 7355 | } |
| 7356 | else |
| 7357 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7358 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7359 | /* autocmds may abort script processing */ |
| 7360 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7361 | cmdwin_result = Ctrl_C; |
| 7362 | # endif |
| 7363 | /* Set the new command line from the cmdline buffer. */ |
| 7364 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7365 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7366 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7367 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7368 | |
| 7369 | if (histtype == HIST_CMD) |
| 7370 | { |
| 7371 | /* Execute the command directly. */ |
| 7372 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7373 | cmdwin_result = CAR; |
| 7374 | } |
| 7375 | else |
| 7376 | { |
| 7377 | /* First need to cancel what we were doing. */ |
| 7378 | ccline.cmdbuff = NULL; |
| 7379 | stuffcharReadbuff(':'); |
| 7380 | stuffReadbuff((char_u *)p); |
| 7381 | stuffcharReadbuff(CAR); |
| 7382 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7383 | } |
| 7384 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7385 | { |
| 7386 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7387 | cmdwin_result = CAR; |
| 7388 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7389 | else if (cmdwin_result == Ctrl_C) |
| 7390 | { |
| 7391 | /* :q or :close, don't execute any command |
| 7392 | * and don't modify the cmd window. */ |
| 7393 | ccline.cmdbuff = NULL; |
| 7394 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7395 | else |
| 7396 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7397 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7398 | { |
| 7399 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7400 | ccline.cmdlen = 0; |
| 7401 | ccline.cmdbufflen = 1; |
| 7402 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7403 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7404 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7405 | else |
| 7406 | { |
| 7407 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7408 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7409 | ccline.cmdpos = curwin->w_cursor.col; |
| 7410 | if (ccline.cmdpos > ccline.cmdlen) |
| 7411 | ccline.cmdpos = ccline.cmdlen; |
| 7412 | if (cmdwin_result == K_IGNORE) |
| 7413 | { |
| 7414 | set_cmdspos_cursor(); |
| 7415 | redrawcmd(); |
| 7416 | } |
| 7417 | } |
| 7418 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7420 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7421 | # ifdef FEAT_CONCEAL |
| 7422 | /* Avoid command-line window first character being concealed. */ |
| 7423 | curwin->w_p_cole = 0; |
| 7424 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7425 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7426 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7427 | win_goto(old_curwin); |
| 7428 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7429 | |
| 7430 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7431 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7432 | if (bufref_valid(&bufref)) |
| 7433 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7434 | |
| 7435 | /* Restore window sizes. */ |
| 7436 | win_size_restore(&winsizes); |
| 7437 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7438 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7439 | } |
| 7440 | |
| 7441 | ga_clear(&winsizes); |
| 7442 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7443 | # ifdef FEAT_RIGHTLEFT |
| 7444 | cmdmsg_rl = save_cmdmsg_rl; |
| 7445 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7446 | |
| 7447 | State = save_State; |
| 7448 | # ifdef FEAT_MOUSE |
| 7449 | setmouse(); |
| 7450 | # endif |
| 7451 | |
| 7452 | return cmdwin_result; |
| 7453 | } |
| 7454 | #endif /* FEAT_CMDWIN */ |
| 7455 | |
| 7456 | /* |
| 7457 | * Used for commands that either take a simple command string argument, or: |
| 7458 | * cmd << endmarker |
| 7459 | * {script} |
| 7460 | * endmarker |
| 7461 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7462 | */ |
| 7463 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7464 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7465 | { |
| 7466 | char_u *theline; |
| 7467 | char *end_pattern = NULL; |
| 7468 | char dot[] = "."; |
| 7469 | garray_T ga; |
| 7470 | |
| 7471 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7472 | return NULL; |
| 7473 | |
| 7474 | ga_init2(&ga, 1, 0x400); |
| 7475 | |
| 7476 | if (cmd[2] != NUL) |
| 7477 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7478 | else |
| 7479 | end_pattern = dot; |
| 7480 | |
| 7481 | for (;;) |
| 7482 | { |
| 7483 | theline = eap->getline( |
| 7484 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7485 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7486 | #endif |
| 7487 | NUL, eap->cookie, 0); |
| 7488 | |
| 7489 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7490 | { |
| 7491 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7492 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7493 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7494 | |
| 7495 | ga_concat(&ga, theline); |
| 7496 | ga_append(&ga, '\n'); |
| 7497 | vim_free(theline); |
| 7498 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7499 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7500 | |
| 7501 | return (char_u *)ga.ga_data; |
| 7502 | } |