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 | 438d176 | 2018-09-30 17:11:48 +0200 | [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(). |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 50 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 52 | static int cmd_showtail; /* Only show path tail in lists ? */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 53 | |
| 54 | #ifdef FEAT_EVAL |
| 55 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 56 | #endif |
| 57 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 58 | static int extra_char = NUL; /* extra character to display when redrawing |
| 59 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 60 | static int extra_char_shift; |
| 61 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | #ifdef FEAT_CMDHIST |
| 63 | typedef struct hist_entry |
| 64 | { |
| 65 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 66 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 67 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 68 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | } histentry_T; |
| 70 | |
| 71 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 72 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 73 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 74 | /* identifying (unique) number of newest history entry */ |
| 75 | static int hislen = 0; /* actual length of history tables */ |
| 76 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 77 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
| 80 | #ifdef FEAT_RIGHTLEFT |
| 81 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 82 | #endif |
| 83 | |
| 84 | #ifdef FEAT_FKMAP |
| 85 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 86 | #endif |
| 87 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 88 | static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 89 | static int cmdline_charsize(int idx); |
| 90 | static void set_cmdspos(void); |
| 91 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 93 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 95 | static void alloc_cmdbuff(int len); |
| 96 | static int realloc_cmdbuff(int len); |
| 97 | static void draw_cmdline(int start, int len); |
| 98 | static void save_cmdline(struct cmdline_info *ccp); |
| 99 | static void restore_cmdline(struct cmdline_info *ccp); |
| 100 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 102 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 104 | static void redrawcmdprompt(void); |
| 105 | static void cursorcmd(void); |
| 106 | static int ccheck_abbr(int); |
| 107 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 108 | static void escape_fname(char_u **pp); |
| 109 | static int showmatches(expand_T *xp, int wildmenu); |
| 110 | static void set_expand_context(expand_T *xp); |
| 111 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 112 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 114 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 115 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 116 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 117 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 119 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 121 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 122 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | # endif |
| 124 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 125 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 127 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 130 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 133 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 134 | static int |
| 135 | #ifdef __BORLANDC__ |
| 136 | _RTLENTRYF |
| 137 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 138 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 139 | #endif |
| 140 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 142 | static void |
| 143 | trigger_cmd_autocmd(int typechar, int evt) |
| 144 | { |
| 145 | char_u typestr[2]; |
| 146 | |
| 147 | typestr[0] = typechar; |
| 148 | typestr[1] = NUL; |
| 149 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 150 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 151 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 153 | * Abandon the command line. |
| 154 | */ |
| 155 | static void |
| 156 | abandon_cmdline(void) |
| 157 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 158 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 159 | if (msg_scrolled == 0) |
| 160 | compute_cmdrow(); |
| 161 | MSG(""); |
| 162 | redraw_cmdline = TRUE; |
| 163 | } |
| 164 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 165 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 166 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 167 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 168 | * as a trailing \|, which can happen while typing a pattern. |
| 169 | */ |
| 170 | static int |
| 171 | empty_pattern(char_u *p) |
| 172 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 173 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 174 | |
| 175 | /* remove trailing \v and the like */ |
| 176 | while (n >= 2 && p[n - 2] == '\\' |
| 177 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 178 | n -= 2; |
| 179 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 180 | } |
| 181 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 182 | // Struct to store the viewstate during 'incsearch' highlighting. |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 183 | typedef struct { |
| 184 | colnr_T vs_curswant; |
| 185 | colnr_T vs_leftcol; |
| 186 | linenr_T vs_topline; |
| 187 | # ifdef FEAT_DIFF |
| 188 | int vs_topfill; |
| 189 | # endif |
| 190 | linenr_T vs_botline; |
| 191 | linenr_T vs_empty_rows; |
| 192 | } viewstate_T; |
| 193 | |
| 194 | static void |
| 195 | save_viewstate(viewstate_T *vs) |
| 196 | { |
| 197 | vs->vs_curswant = curwin->w_curswant; |
| 198 | vs->vs_leftcol = curwin->w_leftcol; |
| 199 | vs->vs_topline = curwin->w_topline; |
| 200 | # ifdef FEAT_DIFF |
| 201 | vs->vs_topfill = curwin->w_topfill; |
| 202 | # endif |
| 203 | vs->vs_botline = curwin->w_botline; |
| 204 | vs->vs_empty_rows = curwin->w_empty_rows; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | restore_viewstate(viewstate_T *vs) |
| 209 | { |
| 210 | curwin->w_curswant = vs->vs_curswant; |
| 211 | curwin->w_leftcol = vs->vs_leftcol; |
| 212 | curwin->w_topline = vs->vs_topline; |
| 213 | # ifdef FEAT_DIFF |
| 214 | curwin->w_topfill = vs->vs_topfill; |
| 215 | # endif |
| 216 | curwin->w_botline = vs->vs_botline; |
| 217 | curwin->w_empty_rows = vs->vs_empty_rows; |
| 218 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 219 | |
| 220 | // Struct to store the state of 'incsearch' highlighting. |
| 221 | typedef struct { |
| 222 | pos_T search_start; // where 'incsearch' starts searching |
| 223 | pos_T save_cursor; |
| 224 | viewstate_T init_viewstate; |
| 225 | viewstate_T old_viewstate; |
| 226 | pos_T match_start; |
| 227 | pos_T match_end; |
| 228 | int did_incsearch; |
| 229 | int incsearch_postponed; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 230 | int magic_save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 231 | } incsearch_state_T; |
| 232 | |
| 233 | static void |
| 234 | init_incsearch_state(incsearch_state_T *is_state) |
| 235 | { |
| 236 | is_state->match_start = curwin->w_cursor; |
| 237 | is_state->did_incsearch = FALSE; |
| 238 | is_state->incsearch_postponed = FALSE; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 239 | is_state->magic_save = p_magic; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 240 | CLEAR_POS(&is_state->match_end); |
| 241 | is_state->save_cursor = curwin->w_cursor; // may be restored later |
| 242 | is_state->search_start = curwin->w_cursor; |
| 243 | save_viewstate(&is_state->init_viewstate); |
| 244 | save_viewstate(&is_state->old_viewstate); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * First move cursor to end of match, then to the start. This |
| 249 | * moves the whole match onto the screen when 'nowrap' is set. |
| 250 | */ |
| 251 | static void |
| 252 | set_search_match(pos_T *t) |
| 253 | { |
| 254 | t->lnum += search_match_lines; |
| 255 | t->col = search_match_endcol; |
| 256 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 257 | { |
| 258 | t->lnum = curbuf->b_ml.ml_line_count; |
| 259 | coladvance((colnr_T)MAXCOL); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Return TRUE when 'incsearch' highlighting is to be done. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 265 | * Sets search_first_line and search_last_line to the address range. |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 266 | * May change the last search pattern. |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 267 | */ |
| 268 | static int |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 269 | do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, |
| 270 | int *skiplen, int *patlen) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 271 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 272 | char_u *cmd; |
| 273 | cmdmod_T save_cmdmod = cmdmod; |
| 274 | char_u *p; |
| 275 | int delim_optional = FALSE; |
| 276 | int delim; |
| 277 | char_u *end; |
| 278 | char_u *dummy; |
| 279 | exarg_T ea; |
| 280 | pos_T save_cursor; |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame] | 281 | int use_last_pat; |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 282 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 283 | *skiplen = 0; |
| 284 | *patlen = ccline.cmdlen; |
| 285 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 286 | if (!p_is || cmd_silent) |
| 287 | return FALSE; |
| 288 | |
| 289 | // by default search all lines |
| 290 | search_first_line = 0; |
| 291 | search_last_line = MAXLNUM; |
| 292 | |
| 293 | if (firstc == '/' || firstc == '?') |
| 294 | return TRUE; |
| 295 | if (firstc != ':') |
| 296 | return FALSE; |
| 297 | |
| 298 | vim_memset(&ea, 0, sizeof(ea)); |
| 299 | ea.line1 = 1; |
| 300 | ea.line2 = 1; |
| 301 | ea.cmd = ccline.cmdbuff; |
| 302 | ea.addr_type = ADDR_LINES; |
| 303 | |
| 304 | parse_command_modifiers(&ea, &dummy, TRUE); |
| 305 | cmdmod = save_cmdmod; |
| 306 | |
| 307 | cmd = skip_range(ea.cmd, NULL); |
| 308 | if (vim_strchr((char_u *)"sgvl", *cmd) == NULL) |
| 309 | return FALSE; |
| 310 | |
| 311 | // Skip over "substitute" to find the pattern separator. |
| 312 | for (p = cmd; ASCII_ISALPHA(*p); ++p) |
| 313 | ; |
| 314 | if (*skipwhite(p) == NUL) |
| 315 | return FALSE; |
| 316 | |
| 317 | if (STRNCMP(cmd, "substitute", p - cmd) == 0 |
| 318 | || STRNCMP(cmd, "smagic", p - cmd) == 0 |
| 319 | || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 |
| 320 | || STRNCMP(cmd, "vglobal", p - cmd) == 0) |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 321 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 322 | if (*cmd == 's' && cmd[1] == 'm') |
| 323 | p_magic = TRUE; |
| 324 | else if (*cmd == 's' && cmd[1] == 'n') |
| 325 | p_magic = FALSE; |
| 326 | } |
| 327 | else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0) |
| 328 | { |
| 329 | // skip over flags |
| 330 | while (ASCII_ISALPHA(*(p = skipwhite(p)))) |
| 331 | ++p; |
| 332 | if (*p == NUL) |
| 333 | return FALSE; |
| 334 | } |
| 335 | else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0 |
| 336 | || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0 |
| 337 | || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0 |
| 338 | || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0 |
| 339 | || STRNCMP(cmd, "global", p - cmd) == 0) |
| 340 | { |
| 341 | // skip over "!" |
| 342 | if (*p == '!') |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 343 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 344 | p++; |
| 345 | if (*skipwhite(p) == NUL) |
| 346 | return FALSE; |
| 347 | } |
| 348 | if (*cmd != 'g') |
| 349 | delim_optional = TRUE; |
| 350 | } |
| 351 | else |
| 352 | return FALSE; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 353 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 354 | p = skipwhite(p); |
| 355 | delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++; |
| 356 | end = skip_regexp(p, delim, p_magic, NULL); |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 357 | |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame] | 358 | use_last_pat = end == p && *end == delim; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 359 | |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame] | 360 | if (end == p && !use_last_pat) |
| 361 | return FALSE; |
| 362 | |
| 363 | // Don't do 'hlsearch' highlighting if the pattern matches everything. |
| 364 | if (!use_last_pat) |
| 365 | { |
| 366 | char c = *end; |
| 367 | int empty; |
| 368 | |
| 369 | *end = NUL; |
| 370 | empty = empty_pattern(p); |
| 371 | *end = c; |
| 372 | if (empty) |
| 373 | return FALSE; |
| 374 | } |
| 375 | |
| 376 | // found a non-empty pattern or // |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 377 | *skiplen = (int)(p - ccline.cmdbuff); |
| 378 | *patlen = (int)(end - p); |
Bram Moolenaar | 264cf5c | 2018-08-18 21:05:31 +0200 | [diff] [blame] | 379 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 380 | // parse the address range |
| 381 | save_cursor = curwin->w_cursor; |
| 382 | curwin->w_cursor = is_state->search_start; |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 383 | parse_cmd_address(&ea, &dummy, TRUE); |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 384 | if (ea.addr_count > 0) |
| 385 | { |
| 386 | // Allow for reverse match. |
| 387 | if (ea.line2 < ea.line1) |
| 388 | { |
| 389 | search_first_line = ea.line2; |
| 390 | search_last_line = ea.line1; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | search_first_line = ea.line1; |
| 395 | search_last_line = ea.line2; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 396 | } |
| 397 | } |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 398 | else if (cmd[0] == 's' && cmd[1] != 'o') |
| 399 | { |
| 400 | // :s defaults to the current line |
| 401 | search_first_line = curwin->w_cursor.lnum; |
| 402 | search_last_line = curwin->w_cursor.lnum; |
| 403 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 404 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 405 | curwin->w_cursor = save_cursor; |
| 406 | return TRUE; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 409 | static void |
| 410 | finish_incsearch_highlighting( |
| 411 | int gotesc, |
| 412 | incsearch_state_T *is_state, |
| 413 | int call_update_screen) |
| 414 | { |
| 415 | if (is_state->did_incsearch) |
| 416 | { |
| 417 | is_state->did_incsearch = FALSE; |
| 418 | if (gotesc) |
| 419 | curwin->w_cursor = is_state->save_cursor; |
| 420 | else |
| 421 | { |
| 422 | if (!EQUAL_POS(is_state->save_cursor, is_state->search_start)) |
| 423 | { |
| 424 | // put the '" mark at the original position |
| 425 | curwin->w_cursor = is_state->save_cursor; |
| 426 | setpcmark(); |
| 427 | } |
| 428 | curwin->w_cursor = is_state->search_start; |
| 429 | } |
| 430 | restore_viewstate(&is_state->old_viewstate); |
| 431 | highlight_match = FALSE; |
Bram Moolenaar | f13daa4 | 2018-08-31 22:09:54 +0200 | [diff] [blame] | 432 | |
| 433 | // by default search all lines |
| 434 | search_first_line = 0; |
| 435 | search_last_line = MAXLNUM; |
| 436 | |
| 437 | p_magic = is_state->magic_save; |
| 438 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 439 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 65985ac | 2018-09-16 17:08:04 +0200 | [diff] [blame] | 440 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 441 | if (call_update_screen) |
| 442 | update_screen(SOME_VALID); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 446 | /* |
| 447 | * Do 'incsearch' highlighting if desired. |
| 448 | */ |
| 449 | static void |
| 450 | may_do_incsearch_highlighting( |
| 451 | int firstc, |
| 452 | long count, |
| 453 | incsearch_state_T *is_state) |
| 454 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 455 | int skiplen, patlen; |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 456 | int found; // do_search() result |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 457 | pos_T end_pos; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 458 | #ifdef FEAT_RELTIME |
| 459 | proftime_T tm; |
| 460 | #endif |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 461 | int next_char; |
| 462 | int use_last_pat; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 463 | |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 464 | // Parsing range may already set the last search pattern. |
| 465 | save_last_search_pattern(); |
| 466 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 467 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 468 | { |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 469 | restore_last_search_pattern(); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 470 | finish_incsearch_highlighting(FALSE, is_state, TRUE); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 471 | return; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 472 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 473 | |
| 474 | // If there is a character waiting, search and redraw later. |
| 475 | if (char_avail()) |
| 476 | { |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 477 | restore_last_search_pattern(); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 478 | is_state->incsearch_postponed = TRUE; |
| 479 | return; |
| 480 | } |
| 481 | is_state->incsearch_postponed = FALSE; |
| 482 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 483 | if (search_first_line == 0) |
| 484 | // start at the original cursor position |
| 485 | curwin->w_cursor = is_state->search_start; |
Bram Moolenaar | 1c29943 | 2018-10-28 14:36:09 +0100 | [diff] [blame] | 486 | else if (search_first_line > curbuf->b_ml.ml_line_count) |
| 487 | { |
| 488 | // start after the last line |
| 489 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 490 | curwin->w_cursor.col = MAXCOL; |
| 491 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 492 | else |
| 493 | { |
| 494 | // start at the first line in the range |
| 495 | curwin->w_cursor.lnum = search_first_line; |
| 496 | curwin->w_cursor.col = 0; |
| 497 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 498 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 499 | // Use the previous pattern for ":s//". |
| 500 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 501 | use_last_pat = patlen == 0 && skiplen > 0 |
| 502 | && ccline.cmdbuff[skiplen - 1] == next_char; |
| 503 | |
| 504 | // If there is no pattern, don't do anything. |
| 505 | if (patlen == 0 && !use_last_pat) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 506 | { |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 507 | found = 0; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 508 | set_no_hlsearch(TRUE); // turn off previous highlight |
| 509 | redraw_all_later(SOME_VALID); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
| 514 | |
| 515 | cursor_off(); // so the user knows we're busy |
| 516 | out_flush(); |
| 517 | ++emsg_off; // so it doesn't beep if bad expr |
| 518 | #ifdef FEAT_RELTIME |
| 519 | // Set the time limit to half a second. |
| 520 | profile_setlimit(500L, &tm); |
| 521 | #endif |
| 522 | if (!p_hls) |
| 523 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 976b847 | 2018-08-12 15:49:47 +0200 | [diff] [blame] | 524 | if (search_first_line != 0) |
| 525 | search_flags += SEARCH_START; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 526 | ccline.cmdbuff[skiplen + patlen] = NUL; |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 527 | found = do_search(NULL, firstc == ':' ? '/' : firstc, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 528 | ccline.cmdbuff + skiplen, count, search_flags, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 529 | #ifdef FEAT_RELTIME |
| 530 | &tm, NULL |
| 531 | #else |
| 532 | NULL, NULL |
| 533 | #endif |
| 534 | ); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 535 | ccline.cmdbuff[skiplen + patlen] = next_char; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 536 | --emsg_off; |
| 537 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 538 | if (curwin->w_cursor.lnum < search_first_line |
| 539 | || curwin->w_cursor.lnum > search_last_line) |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 540 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 541 | // match outside of address range |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 542 | found = 0; |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 543 | curwin->w_cursor = is_state->search_start; |
| 544 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 545 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 546 | // if interrupted while searching, behave like it failed |
| 547 | if (got_int) |
| 548 | { |
| 549 | (void)vpeekc(); // remove <C-C> from input stream |
| 550 | got_int = FALSE; // don't abandon the command line |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 551 | found = 0; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 552 | } |
| 553 | else if (char_avail()) |
| 554 | // cancelled searching because a char was typed |
| 555 | is_state->incsearch_postponed = TRUE; |
| 556 | } |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 557 | if (found != 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 558 | highlight_match = TRUE; // highlight position |
| 559 | else |
| 560 | highlight_match = FALSE; // remove highlight |
| 561 | |
| 562 | // First restore the old curwin values, so the screen is positioned in the |
| 563 | // same way as the actual search command. |
| 564 | restore_viewstate(&is_state->old_viewstate); |
| 565 | changed_cline_bef_curs(); |
| 566 | update_topline(); |
| 567 | |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 568 | if (found != 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 569 | { |
| 570 | pos_T save_pos = curwin->w_cursor; |
| 571 | |
| 572 | is_state->match_start = curwin->w_cursor; |
| 573 | set_search_match(&curwin->w_cursor); |
| 574 | validate_cursor(); |
| 575 | end_pos = curwin->w_cursor; |
| 576 | is_state->match_end = end_pos; |
| 577 | curwin->w_cursor = save_pos; |
| 578 | } |
| 579 | else |
| 580 | end_pos = curwin->w_cursor; // shutup gcc 4 |
| 581 | |
Bram Moolenaar | 4edfe2d | 2018-08-23 20:55:45 +0200 | [diff] [blame] | 582 | // Disable 'hlsearch' highlighting if the pattern matches everything. |
| 583 | // Avoids a flash when typing "foo\|". |
| 584 | if (!use_last_pat) |
| 585 | { |
| 586 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 587 | ccline.cmdbuff[skiplen + patlen] = NUL; |
Bram Moolenaar | 65985ac | 2018-09-16 17:08:04 +0200 | [diff] [blame] | 588 | if (empty_pattern(ccline.cmdbuff) && !no_hlsearch) |
| 589 | { |
| 590 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 4edfe2d | 2018-08-23 20:55:45 +0200 | [diff] [blame] | 591 | set_no_hlsearch(TRUE); |
Bram Moolenaar | 65985ac | 2018-09-16 17:08:04 +0200 | [diff] [blame] | 592 | } |
Bram Moolenaar | 4edfe2d | 2018-08-23 20:55:45 +0200 | [diff] [blame] | 593 | ccline.cmdbuff[skiplen + patlen] = next_char; |
| 594 | } |
| 595 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 596 | validate_cursor(); |
| 597 | // May redraw the status line to show the cursor position. |
| 598 | if (p_ru && curwin->w_status_height > 0) |
| 599 | curwin->w_redr_status = TRUE; |
| 600 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 601 | update_screen(SOME_VALID); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 602 | restore_last_search_pattern(); |
| 603 | |
Bram Moolenaar | 99f043a | 2018-09-09 15:54:14 +0200 | [diff] [blame] | 604 | // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the |
| 605 | // end of the pattern, e.g. for ":s/pat/". |
| 606 | if (ccline.cmdbuff[skiplen + patlen] != NUL) |
| 607 | curwin->w_cursor = is_state->search_start; |
| 608 | else if (found != 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 609 | curwin->w_cursor = end_pos; |
| 610 | |
| 611 | msg_starthere(); |
| 612 | redrawcmdline(); |
| 613 | is_state->did_incsearch = TRUE; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next |
| 618 | * or previous match. |
| 619 | * Returns FAIL when jumping to cmdline_not_changed; |
| 620 | */ |
| 621 | static int |
| 622 | may_adjust_incsearch_highlighting( |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 623 | int firstc, |
| 624 | long count, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 625 | incsearch_state_T *is_state, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 626 | int c) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 627 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 628 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 629 | pos_T t; |
| 630 | char_u *pat; |
| 631 | int search_flags = SEARCH_NOOF; |
| 632 | int i; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 633 | int save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 634 | |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 635 | // Parsing range may already set the last search pattern. |
| 636 | save_last_search_pattern(); |
| 637 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 638 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 639 | { |
| 640 | restore_last_search_pattern(); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 641 | return OK; |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 642 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 643 | if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 644 | { |
| 645 | restore_last_search_pattern(); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 646 | return FAIL; |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 647 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 648 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 649 | if (firstc == ccline.cmdbuff[skiplen]) |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 650 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 651 | pat = last_search_pattern(); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 652 | skiplen = 0; |
Bram Moolenaar | d7cc163 | 2018-08-14 20:18:26 +0200 | [diff] [blame] | 653 | patlen = (int)STRLEN(pat); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 654 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 655 | else |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 656 | pat = ccline.cmdbuff + skiplen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 657 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 658 | cursor_off(); |
| 659 | out_flush(); |
| 660 | if (c == Ctrl_G) |
| 661 | { |
| 662 | t = is_state->match_end; |
| 663 | if (LT_POS(is_state->match_start, is_state->match_end)) |
| 664 | // Start searching at the end of the match not at the beginning of |
| 665 | // the next column. |
| 666 | (void)decl(&t); |
| 667 | search_flags += SEARCH_COL; |
| 668 | } |
| 669 | else |
| 670 | t = is_state->match_start; |
| 671 | if (!p_hls) |
| 672 | search_flags += SEARCH_KEEP; |
| 673 | ++emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 674 | save = pat[patlen]; |
| 675 | pat[patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 676 | i = searchit(curwin, curbuf, &t, |
| 677 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 678 | pat, count, search_flags, |
| 679 | RE_SEARCH, 0, NULL, NULL); |
| 680 | --emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 681 | pat[patlen] = save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 682 | if (i) |
| 683 | { |
| 684 | is_state->search_start = is_state->match_start; |
| 685 | is_state->match_end = t; |
| 686 | is_state->match_start = t; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 687 | if (c == Ctrl_T && firstc != '?') |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 688 | { |
| 689 | // Move just before the current match, so that when nv_search |
| 690 | // finishes the cursor will be put back on the match. |
| 691 | is_state->search_start = t; |
| 692 | (void)decl(&is_state->search_start); |
| 693 | } |
| 694 | else if (c == Ctrl_G && firstc == '?') |
| 695 | { |
| 696 | // Move just after the current match, so that when nv_search |
| 697 | // finishes the cursor will be put back on the match. |
| 698 | is_state->search_start = t; |
| 699 | (void)incl(&is_state->search_start); |
| 700 | } |
| 701 | if (LT_POS(t, is_state->search_start) && c == Ctrl_G) |
| 702 | { |
| 703 | // wrap around |
| 704 | is_state->search_start = t; |
| 705 | if (firstc == '?') |
| 706 | (void)incl(&is_state->search_start); |
| 707 | else |
| 708 | (void)decl(&is_state->search_start); |
| 709 | } |
| 710 | |
| 711 | set_search_match(&is_state->match_end); |
| 712 | curwin->w_cursor = is_state->match_start; |
| 713 | changed_cline_bef_curs(); |
| 714 | update_topline(); |
| 715 | validate_cursor(); |
| 716 | highlight_match = TRUE; |
| 717 | save_viewstate(&is_state->old_viewstate); |
| 718 | update_screen(NOT_VALID); |
| 719 | redrawcmdline(); |
| 720 | } |
| 721 | else |
| 722 | vim_beep(BO_ERROR); |
| 723 | restore_last_search_pattern(); |
| 724 | return FAIL; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * When CTRL-L typed: add character from the match to the pattern. |
| 729 | * May set "*c" to the added character. |
| 730 | * Return OK when jumping to cmdline_not_changed. |
| 731 | */ |
| 732 | static int |
| 733 | may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state) |
| 734 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 735 | int skiplen, patlen; |
| 736 | |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 737 | // Parsing range may already set the last search pattern. |
| 738 | save_last_search_pattern(); |
| 739 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 740 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 741 | { |
| 742 | restore_last_search_pattern(); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 743 | return FAIL; |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 744 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 745 | |
| 746 | // Add a character from under the cursor for 'incsearch'. |
| 747 | if (is_state->did_incsearch) |
| 748 | { |
| 749 | curwin->w_cursor = is_state->match_end; |
| 750 | if (!EQUAL_POS(curwin->w_cursor, is_state->search_start)) |
| 751 | { |
| 752 | *c = gchar_cursor(); |
| 753 | |
| 754 | // If 'ignorecase' and 'smartcase' are set and the |
| 755 | // command line has no uppercase characters, convert |
| 756 | // the character to lowercase. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 757 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 758 | *c = MB_TOLOWER(*c); |
| 759 | if (*c != NUL) |
| 760 | { |
| 761 | if (*c == firstc || vim_strchr((char_u *)( |
| 762 | p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) |
| 763 | { |
| 764 | // put a backslash before special characters |
| 765 | stuffcharReadbuff(*c); |
| 766 | *c = '\\'; |
| 767 | } |
| 768 | return FAIL; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | return OK; |
| 773 | } |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 774 | #endif |
| 775 | |
Bram Moolenaar | d3dc062 | 2018-09-30 17:45:30 +0200 | [diff] [blame] | 776 | void |
| 777 | cmdline_init(void) |
| 778 | { |
| 779 | vim_memset(&ccline, 0, sizeof(struct cmdline_info)); |
| 780 | } |
| 781 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 782 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 783 | * getcmdline() - accept a command line starting with firstc. |
| 784 | * |
| 785 | * firstc == ':' get ":" command line. |
| 786 | * firstc == '/' or '?' get search pattern |
| 787 | * firstc == '=' get expression |
| 788 | * firstc == '@' get text for input() function |
| 789 | * firstc == '>' get text for debug mode |
| 790 | * firstc == NUL get text for :insert command |
| 791 | * firstc == -1 like NUL, and break on CTRL-C |
| 792 | * |
| 793 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 794 | * command line. |
| 795 | * |
| 796 | * Careful: getcmdline() can be called recursively! |
| 797 | * |
| 798 | * Return pointer to allocated string if there is a commandline, NULL |
| 799 | * otherwise. |
| 800 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 801 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 802 | getcmdline( |
| 803 | int firstc, |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 804 | long count, // only used for incremental search |
| 805 | int indent) // indent for inside conditionals |
| 806 | { |
| 807 | return getcmdline_int(firstc, count, indent, TRUE); |
| 808 | } |
| 809 | |
| 810 | static char_u * |
| 811 | getcmdline_int( |
| 812 | int firstc, |
| 813 | long count UNUSED, // only used for incremental search |
| 814 | int indent, // indent for inside conditionals |
| 815 | int init_ccline) // clear ccline first |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | { |
| 817 | int c; |
| 818 | int i; |
| 819 | int j; |
| 820 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 821 | int do_abbr; /* when TRUE check for abbr. */ |
| 822 | #ifdef FEAT_CMDHIST |
| 823 | char_u *lookfor = NULL; /* string to match */ |
| 824 | int hiscnt; /* current history line in use */ |
| 825 | int histype; /* history type to be used */ |
| 826 | #endif |
| 827 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 828 | incsearch_state_T is_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | #endif |
| 830 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 831 | int wim_index = 0; /* index in wim_flags[] */ |
| 832 | int res; |
| 833 | int save_msg_scroll = msg_scroll; |
| 834 | int save_State = State; /* remember State when called */ |
| 835 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 836 | #ifdef FEAT_MOUSE |
| 837 | /* mouse drag and release events are ignored, unless they are |
| 838 | * preceded with a mouse down event */ |
| 839 | int ignore_drag_release = TRUE; |
| 840 | #endif |
| 841 | #ifdef FEAT_EVAL |
| 842 | int break_ctrl_c = FALSE; |
| 843 | #endif |
| 844 | expand_T xpc; |
| 845 | long *b_im_ptr = NULL; |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 846 | struct cmdline_info save_ccline; |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 847 | int did_save_ccline = FALSE; |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 848 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 849 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 850 | if (ccline.cmdbuff != NULL) |
| 851 | { |
| 852 | // Being called recursively. Since ccline is global, we need to save |
| 853 | // the current buffer and restore it when returning. |
| 854 | save_cmdline(&save_ccline); |
| 855 | did_save_ccline = TRUE; |
| 856 | } |
| 857 | if (init_ccline) |
| 858 | vim_memset(&ccline, 0, sizeof(struct cmdline_info)); |
| 859 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 860 | #ifdef FEAT_EVAL |
| 861 | if (firstc == -1) |
| 862 | { |
| 863 | firstc = NUL; |
| 864 | break_ctrl_c = TRUE; |
| 865 | } |
| 866 | #endif |
| 867 | #ifdef FEAT_RIGHTLEFT |
| 868 | /* start without Hebrew mapping for a command line */ |
| 869 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 870 | cmd_hkmap = 0; |
| 871 | #endif |
| 872 | |
| 873 | ccline.overstrike = FALSE; /* always start in insert mode */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 874 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 876 | init_incsearch_state(&is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | #endif |
| 878 | |
| 879 | /* |
| 880 | * set some variables for redrawcmd() |
| 881 | */ |
| 882 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 883 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 884 | |
| 885 | /* alloc initial ccline.cmdbuff */ |
| 886 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 887 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 888 | goto theend; // out of memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 889 | ccline.cmdlen = ccline.cmdpos = 0; |
| 890 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 891 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 893 | /* autoindent for :insert and :append */ |
| 894 | if (firstc <= 0) |
| 895 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 896 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 897 | ccline.cmdbuff[indent] = NUL; |
| 898 | ccline.cmdpos = indent; |
| 899 | ccline.cmdspos = indent; |
| 900 | ccline.cmdlen = indent; |
| 901 | } |
| 902 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 903 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 904 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 905 | |
| 906 | #ifdef FEAT_RIGHTLEFT |
| 907 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 908 | && (firstc == '/' || firstc == '?')) |
| 909 | cmdmsg_rl = TRUE; |
| 910 | else |
| 911 | cmdmsg_rl = FALSE; |
| 912 | #endif |
| 913 | |
| 914 | redir_off = TRUE; /* don't redirect the typed command */ |
| 915 | if (!cmd_silent) |
| 916 | { |
| 917 | i = msg_scrolled; |
| 918 | msg_scrolled = 0; /* avoid wait_return message */ |
| 919 | gotocmdline(TRUE); |
| 920 | msg_scrolled += i; |
| 921 | redrawcmdprompt(); /* draw prompt or indent */ |
| 922 | set_cmdspos(); |
| 923 | } |
| 924 | xpc.xp_context = EXPAND_NOTHING; |
| 925 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 926 | #ifndef BACKSLASH_IN_FILENAME |
| 927 | xpc.xp_shell = FALSE; |
| 928 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 930 | #if defined(FEAT_EVAL) |
| 931 | if (ccline.input_fn) |
| 932 | { |
| 933 | xpc.xp_context = ccline.xp_context; |
| 934 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 935 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 936 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 937 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 938 | } |
| 939 | #endif |
| 940 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | /* |
| 942 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 943 | * doing ":@0" when register 0 doesn't contain a CR. |
| 944 | */ |
| 945 | msg_scroll = FALSE; |
| 946 | |
| 947 | State = CMDLINE; |
| 948 | |
| 949 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 950 | { |
| 951 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 952 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 953 | b_im_ptr = &curbuf->b_p_iminsert; |
| 954 | else |
| 955 | b_im_ptr = &curbuf->b_p_imsearch; |
| 956 | if (*b_im_ptr == B_IMODE_LMAP) |
| 957 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 958 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 960 | #endif |
| 961 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 962 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 963 | else if (p_imcmdline) |
| 964 | im_set_active(TRUE); |
| 965 | #endif |
| 966 | |
| 967 | #ifdef FEAT_MOUSE |
| 968 | setmouse(); |
| 969 | #endif |
| 970 | #ifdef CURSOR_SHAPE |
| 971 | ui_cursor_shape(); /* may show different cursor shape */ |
| 972 | #endif |
| 973 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 974 | /* When inside an autocommand for writing "exiting" may be set and |
| 975 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 976 | settmode(TMODE_RAW); |
| 977 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 978 | /* Trigger CmdlineEnter autocommands. */ |
| 979 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 980 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 981 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 982 | #ifdef FEAT_CMDHIST |
| 983 | init_history(); |
| 984 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 985 | histype = hist_char2type(firstc); |
| 986 | #endif |
| 987 | |
| 988 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 989 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 990 | #endif |
| 991 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 992 | /* If something above caused an error, reset the flags, we do want to type |
| 993 | * and execute commands. Display may be messed up a bit. */ |
| 994 | if (did_emsg) |
| 995 | redrawcmd(); |
| 996 | did_emsg = FALSE; |
| 997 | got_int = FALSE; |
| 998 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | /* |
| 1000 | * Collect the command string, handling editing keys. |
| 1001 | */ |
| 1002 | for (;;) |
| 1003 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 1004 | redir_off = TRUE; /* Don't redirect the typed command. |
| 1005 | Repeated, because a ":redir" inside |
| 1006 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1007 | #ifdef USE_ON_FLY_SCROLL |
| 1008 | dont_scroll = FALSE; /* allow scrolling here */ |
| 1009 | #endif |
| 1010 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 1011 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 1012 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 1013 | that occurs while typing a command should |
| 1014 | cause the command not to be executed. */ |
| 1015 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1017 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 1018 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 1019 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1020 | do |
| 1021 | { |
| 1022 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 1023 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1024 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1025 | if (KeyTyped) |
| 1026 | { |
| 1027 | some_key_typed = TRUE; |
| 1028 | #ifdef FEAT_RIGHTLEFT |
| 1029 | if (cmd_hkmap) |
| 1030 | c = hkmap(c); |
| 1031 | # ifdef FEAT_FKMAP |
| 1032 | if (cmd_fkmap) |
| 1033 | c = cmdl_fkmap(c); |
| 1034 | # endif |
| 1035 | if (cmdmsg_rl && !KeyStuffed) |
| 1036 | { |
| 1037 | /* Invert horizontal movements and operations. Only when |
| 1038 | * typed by the user directly, not when the result of a |
| 1039 | * mapping. */ |
| 1040 | switch (c) |
| 1041 | { |
| 1042 | case K_RIGHT: c = K_LEFT; break; |
| 1043 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 1044 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 1045 | case K_LEFT: c = K_RIGHT; break; |
| 1046 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 1047 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 1048 | } |
| 1049 | } |
| 1050 | #endif |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Ignore got_int when CTRL-C was typed here. |
| 1055 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 1056 | * ":g/pat/normal /pat" (without the <CR>). |
| 1057 | * Don't ignore it for the input() function. |
| 1058 | */ |
| 1059 | if ((c == Ctrl_C |
| 1060 | #ifdef UNIX |
| 1061 | || c == intr_char |
| 1062 | #endif |
| 1063 | ) |
| 1064 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 1065 | && firstc != '@' |
| 1066 | #endif |
| 1067 | #ifdef FEAT_EVAL |
| 1068 | && !break_ctrl_c |
| 1069 | #endif |
| 1070 | && !global_busy) |
| 1071 | got_int = FALSE; |
| 1072 | |
| 1073 | #ifdef FEAT_CMDHIST |
| 1074 | /* free old command line when finished moving around in the history |
| 1075 | * list */ |
| 1076 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1077 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1078 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 1080 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1081 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1082 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1083 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | #endif |
| 1085 | |
| 1086 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1087 | * When there are matching completions to select <S-Tab> works like |
| 1088 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1089 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1090 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1091 | c = Ctrl_P; |
| 1092 | |
| 1093 | #ifdef FEAT_WILDMENU |
| 1094 | /* Special translations for 'wildmenu' */ |
| 1095 | if (did_wild_list && p_wmnu) |
| 1096 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1097 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1099 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | c = Ctrl_N; |
| 1101 | } |
| 1102 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 1103 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 1104 | && ccline.cmdpos > 1 |
| 1105 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 1106 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 1107 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 1108 | c = K_DOWN; |
| 1109 | #endif |
| 1110 | |
| 1111 | /* free expanded names when finished walking through matches */ |
| 1112 | if (xpc.xp_numfiles != -1 |
| 1113 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 1114 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 1115 | && c != Ctrl_L) |
| 1116 | { |
| 1117 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1118 | did_wild_list = FALSE; |
| 1119 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1120 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1121 | #endif |
| 1122 | xpc.xp_context = EXPAND_NOTHING; |
| 1123 | wim_index = 0; |
| 1124 | #ifdef FEAT_WILDMENU |
| 1125 | if (p_wmnu && wild_menu_showing != 0) |
| 1126 | { |
| 1127 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1128 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1131 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | |
| 1133 | if (wild_menu_showing == WM_SCROLLED) |
| 1134 | { |
| 1135 | /* Entered command line, move it up */ |
| 1136 | cmdline_row--; |
| 1137 | redrawcmd(); |
| 1138 | } |
| 1139 | else if (save_p_ls != -1) |
| 1140 | { |
| 1141 | /* restore 'laststatus' and 'winminheight' */ |
| 1142 | p_ls = save_p_ls; |
| 1143 | p_wmh = save_p_wmh; |
| 1144 | last_status(FALSE); |
| 1145 | update_screen(VALID); /* redraw the screen NOW */ |
| 1146 | redrawcmd(); |
| 1147 | save_p_ls = -1; |
| 1148 | } |
| 1149 | else |
| 1150 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | redraw_statuslines(); |
| 1153 | } |
| 1154 | KeyTyped = skt; |
| 1155 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1156 | if (ccline.input_fn) |
| 1157 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | } |
| 1159 | #endif |
| 1160 | } |
| 1161 | |
| 1162 | #ifdef FEAT_WILDMENU |
| 1163 | /* Special translations for 'wildmenu' */ |
| 1164 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 1165 | { |
| 1166 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1167 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 1168 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1169 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1170 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1171 | { |
| 1172 | /* Hitting <Up>: Remove one submenu name in front of the |
| 1173 | * cursor */ |
| 1174 | int found = FALSE; |
| 1175 | |
| 1176 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1177 | i = 0; |
| 1178 | while (--j > 0) |
| 1179 | { |
| 1180 | /* check for start of menu name */ |
| 1181 | if (ccline.cmdbuff[j] == ' ' |
| 1182 | && ccline.cmdbuff[j - 1] != '\\') |
| 1183 | { |
| 1184 | i = j + 1; |
| 1185 | break; |
| 1186 | } |
| 1187 | /* check for start of submenu name */ |
| 1188 | if (ccline.cmdbuff[j] == '.' |
| 1189 | && ccline.cmdbuff[j - 1] != '\\') |
| 1190 | { |
| 1191 | if (found) |
| 1192 | { |
| 1193 | i = j + 1; |
| 1194 | break; |
| 1195 | } |
| 1196 | else |
| 1197 | found = TRUE; |
| 1198 | } |
| 1199 | } |
| 1200 | if (i > 0) |
| 1201 | cmdline_del(i); |
| 1202 | c = p_wc; |
| 1203 | xpc.xp_context = EXPAND_NOTHING; |
| 1204 | } |
| 1205 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1206 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1207 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1208 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | { |
| 1210 | char_u upseg[5]; |
| 1211 | |
| 1212 | upseg[0] = PATHSEP; |
| 1213 | upseg[1] = '.'; |
| 1214 | upseg[2] = '.'; |
| 1215 | upseg[3] = PATHSEP; |
| 1216 | upseg[4] = NUL; |
| 1217 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1218 | if (c == K_DOWN |
| 1219 | && ccline.cmdpos > 0 |
| 1220 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 1221 | && (ccline.cmdpos < 3 |
| 1222 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 1224 | { |
| 1225 | /* go down a directory */ |
| 1226 | c = p_wc; |
| 1227 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1228 | 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] | 1229 | { |
| 1230 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 1231 | int found = FALSE; |
| 1232 | |
| 1233 | j = ccline.cmdpos; |
| 1234 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1235 | while (--j > i) |
| 1236 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1237 | #ifdef FEAT_MBYTE |
| 1238 | if (has_mbyte) |
| 1239 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1240 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1241 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 1242 | { |
| 1243 | found = TRUE; |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | if (found |
| 1248 | && ccline.cmdbuff[j - 1] == '.' |
| 1249 | && ccline.cmdbuff[j - 2] == '.' |
| 1250 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 1251 | { |
| 1252 | cmdline_del(j - 2); |
| 1253 | c = p_wc; |
| 1254 | } |
| 1255 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1256 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1257 | { |
| 1258 | /* go up a directory */ |
| 1259 | int found = FALSE; |
| 1260 | |
| 1261 | j = ccline.cmdpos - 1; |
| 1262 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1263 | while (--j > i) |
| 1264 | { |
| 1265 | #ifdef FEAT_MBYTE |
| 1266 | if (has_mbyte) |
| 1267 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1268 | #endif |
| 1269 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 1270 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1271 | && vim_strchr((char_u *)" *?[{`$%#", |
| 1272 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | #endif |
| 1274 | ) |
| 1275 | { |
| 1276 | if (found) |
| 1277 | { |
| 1278 | i = j + 1; |
| 1279 | break; |
| 1280 | } |
| 1281 | else |
| 1282 | found = TRUE; |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | if (!found) |
| 1287 | j = i; |
| 1288 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 1289 | j += 4; |
| 1290 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 1291 | && j == i) |
| 1292 | j += 3; |
| 1293 | else |
| 1294 | j = 0; |
| 1295 | if (j > 0) |
| 1296 | { |
| 1297 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 1298 | * machine-specific stuff here and in upseg init */ |
| 1299 | cmdline_del(j); |
| 1300 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 1301 | } |
| 1302 | else if (ccline.cmdpos > i) |
| 1303 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1304 | |
| 1305 | /* Now complete in the new directory. Set KeyTyped in case the |
| 1306 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1307 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1308 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1311 | |
| 1312 | #endif /* FEAT_WILDMENU */ |
| 1313 | |
| 1314 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 1315 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 1316 | if (c == Ctrl_BSL) |
| 1317 | { |
| 1318 | ++no_mapping; |
| 1319 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1320 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | --no_mapping; |
| 1322 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 1323 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 1324 | * is in a mapping. */ |
| 1325 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
Bram Moolenaar | 31cbadf | 2018-09-25 20:48:57 +0200 | [diff] [blame] | 1326 | || (ccline.cmdfirstc == '=' && KeyTyped) |
| 1327 | #ifdef FEAT_EVAL |
Bram Moolenaar | ee91c33 | 2018-09-25 22:27:35 +0200 | [diff] [blame] | 1328 | || cmdline_star > 0 |
Bram Moolenaar | 31cbadf | 2018-09-25 20:48:57 +0200 | [diff] [blame] | 1329 | #endif |
| 1330 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | { |
| 1332 | vungetc(c); |
| 1333 | c = Ctrl_BSL; |
| 1334 | } |
| 1335 | #ifdef FEAT_EVAL |
| 1336 | else if (c == 'e') |
| 1337 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 1338 | char_u *p = NULL; |
| 1339 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1340 | |
| 1341 | /* |
| 1342 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1343 | * Need to save and restore the current command line, to be |
| 1344 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | */ |
| 1346 | if (ccline.cmdpos == ccline.cmdlen) |
| 1347 | new_cmdpos = 99999; /* keep it at the end */ |
| 1348 | else |
| 1349 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1350 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1351 | c = get_expr_register(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | if (c == '=') |
| 1353 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1354 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1355 | * to avoid nasty things like going to another buffer when |
| 1356 | * evaluating an expression. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1357 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1358 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1359 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1360 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1361 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1362 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1363 | len = (int)STRLEN(p); |
| 1364 | if (realloc_cmdbuff(len + 1) == OK) |
| 1365 | { |
| 1366 | ccline.cmdlen = len; |
| 1367 | STRCPY(ccline.cmdbuff, p); |
| 1368 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1369 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1370 | /* Restore the cursor or use the position set with |
| 1371 | * set_cmdline_pos(). */ |
| 1372 | if (new_cmdpos > ccline.cmdlen) |
| 1373 | ccline.cmdpos = ccline.cmdlen; |
| 1374 | else |
| 1375 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1376 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1377 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1378 | redrawcmd(); |
| 1379 | goto cmdline_changed; |
| 1380 | } |
Bram Moolenaar | 4e303c8 | 2018-11-24 14:27:44 +0100 | [diff] [blame] | 1381 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 1385 | got_int = FALSE; /* don't abandon the command line */ |
| 1386 | did_emsg = FALSE; |
| 1387 | emsg_on_display = FALSE; |
| 1388 | redrawcmd(); |
| 1389 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | } |
| 1391 | #endif |
| 1392 | else |
| 1393 | { |
| 1394 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 1395 | restart_edit = 'a'; |
| 1396 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 1397 | in history */ |
| 1398 | goto returncmd; /* back to Normal mode */ |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | #ifdef FEAT_CMDWIN |
| 1403 | if (c == cedit_key || c == K_CMDWIN) |
| 1404 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1405 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 1406 | { |
| 1407 | /* |
| 1408 | * Open a window to edit the command line (and history). |
| 1409 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1410 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1411 | some_key_typed = TRUE; |
| 1412 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | } |
| 1414 | # ifdef FEAT_DIGRAPHS |
| 1415 | else |
| 1416 | # endif |
| 1417 | #endif |
| 1418 | #ifdef FEAT_DIGRAPHS |
| 1419 | c = do_digraph(c); |
| 1420 | #endif |
| 1421 | |
| 1422 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 1423 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 1424 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1425 | /* In Ex mode a backslash escapes a newline. */ |
| 1426 | if (exmode_active |
| 1427 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1428 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1429 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1430 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1431 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1432 | if (c == K_KENTER) |
| 1433 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1435 | else |
| 1436 | { |
| 1437 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 1438 | truncate the cmdline now. */ |
| 1439 | if (ccheck_abbr(c + ABBR_OFF)) |
| 1440 | goto cmdline_changed; |
| 1441 | if (!cmd_silent) |
| 1442 | { |
| 1443 | windgoto(msg_row, 0); |
| 1444 | out_flush(); |
| 1445 | } |
| 1446 | break; |
| 1447 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Completion for 'wildchar' or 'wildcharm' key. |
| 1452 | * - hitting <ESC> twice means: abandon command line. |
| 1453 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 1454 | * typed, not when it comes from a macro |
| 1455 | */ |
| 1456 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 1457 | { |
| 1458 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 1459 | { |
| 1460 | /* if 'wildmode' contains "list" may still need to list */ |
| 1461 | if (xpc.xp_numfiles > 1 |
| 1462 | && !did_wild_list |
| 1463 | && (wim_flags[wim_index] & WIM_LIST)) |
| 1464 | { |
| 1465 | (void)showmatches(&xpc, FALSE); |
| 1466 | redrawcmd(); |
| 1467 | did_wild_list = TRUE; |
| 1468 | } |
| 1469 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1470 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1471 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1472 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1473 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1474 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1475 | else |
| 1476 | res = OK; /* don't insert 'wildchar' now */ |
| 1477 | } |
| 1478 | else /* typed p_wc first time */ |
| 1479 | { |
| 1480 | wim_index = 0; |
| 1481 | j = ccline.cmdpos; |
| 1482 | /* if 'wildmode' first contains "longest", get longest |
| 1483 | * common part */ |
| 1484 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1485 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1486 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1487 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1488 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 1489 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* if interrupted while completing, behave like it failed */ |
| 1492 | if (got_int) |
| 1493 | { |
| 1494 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1495 | got_int = FALSE; /* don't abandon the command line */ |
| 1496 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1497 | #ifdef FEAT_WILDMENU |
| 1498 | xpc.xp_context = EXPAND_NOTHING; |
| 1499 | #endif |
| 1500 | goto cmdline_changed; |
| 1501 | } |
| 1502 | |
| 1503 | /* when more than one match, and 'wildmode' first contains |
| 1504 | * "list", or no change and 'wildmode' contains "longest,list", |
| 1505 | * list all matches */ |
| 1506 | if (res == OK && xpc.xp_numfiles > 1) |
| 1507 | { |
| 1508 | /* a "longest" that didn't do anything is skipped (but not |
| 1509 | * "list:longest") */ |
| 1510 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 1511 | wim_index = 1; |
| 1512 | if ((wim_flags[wim_index] & WIM_LIST) |
| 1513 | #ifdef FEAT_WILDMENU |
| 1514 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 1515 | #endif |
| 1516 | ) |
| 1517 | { |
| 1518 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 1519 | { |
| 1520 | #ifdef FEAT_WILDMENU |
| 1521 | int p_wmnu_save = p_wmnu; |
| 1522 | p_wmnu = 0; |
| 1523 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1524 | /* remove match */ |
| 1525 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1526 | #ifdef FEAT_WILDMENU |
| 1527 | p_wmnu = p_wmnu_save; |
| 1528 | #endif |
| 1529 | } |
| 1530 | #ifdef FEAT_WILDMENU |
| 1531 | (void)showmatches(&xpc, p_wmnu |
| 1532 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 1533 | #else |
| 1534 | (void)showmatches(&xpc, FALSE); |
| 1535 | #endif |
| 1536 | redrawcmd(); |
| 1537 | did_wild_list = TRUE; |
| 1538 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1539 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1540 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1541 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1542 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1543 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1544 | } |
| 1545 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1546 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1547 | } |
| 1548 | #ifdef FEAT_WILDMENU |
| 1549 | else if (xpc.xp_numfiles == -1) |
| 1550 | xpc.xp_context = EXPAND_NOTHING; |
| 1551 | #endif |
| 1552 | } |
| 1553 | if (wim_index < 3) |
| 1554 | ++wim_index; |
| 1555 | if (c == ESC) |
| 1556 | gotesc = TRUE; |
| 1557 | if (res == OK) |
| 1558 | goto cmdline_changed; |
| 1559 | } |
| 1560 | |
| 1561 | gotesc = FALSE; |
| 1562 | |
| 1563 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 1564 | if (c == K_S_TAB && KeyTyped) |
| 1565 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1566 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 1567 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 1568 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1569 | goto cmdline_changed; |
| 1570 | } |
| 1571 | |
| 1572 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 1573 | c = NL; |
| 1574 | |
| 1575 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1576 | |
| 1577 | /* |
| 1578 | * Big switch for a typed command line character. |
| 1579 | */ |
| 1580 | switch (c) |
| 1581 | { |
| 1582 | case K_BS: |
| 1583 | case Ctrl_H: |
| 1584 | case K_DEL: |
| 1585 | case K_KDEL: |
| 1586 | case Ctrl_W: |
| 1587 | #ifdef FEAT_FKMAP |
| 1588 | if (cmd_fkmap && c == K_BS) |
| 1589 | c = K_DEL; |
| 1590 | #endif |
| 1591 | if (c == K_KDEL) |
| 1592 | c = K_DEL; |
| 1593 | |
| 1594 | /* |
| 1595 | * delete current character is the same as backspace on next |
| 1596 | * character, except at end of line |
| 1597 | */ |
| 1598 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1599 | ++ccline.cmdpos; |
| 1600 | #ifdef FEAT_MBYTE |
| 1601 | if (has_mbyte && c == K_DEL) |
| 1602 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1603 | ccline.cmdbuff + ccline.cmdpos); |
| 1604 | #endif |
| 1605 | if (ccline.cmdpos > 0) |
| 1606 | { |
| 1607 | char_u *p; |
| 1608 | |
| 1609 | j = ccline.cmdpos; |
| 1610 | p = ccline.cmdbuff + j; |
| 1611 | #ifdef FEAT_MBYTE |
| 1612 | if (has_mbyte) |
| 1613 | { |
| 1614 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1615 | if (c == Ctrl_W) |
| 1616 | { |
| 1617 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1618 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1619 | i = mb_get_class(p); |
| 1620 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1621 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1622 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1623 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | else |
| 1627 | #endif |
| 1628 | if (c == Ctrl_W) |
| 1629 | { |
| 1630 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1631 | --p; |
| 1632 | i = vim_iswordc(p[-1]); |
| 1633 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1634 | && vim_iswordc(p[-1]) == i) |
| 1635 | --p; |
| 1636 | } |
| 1637 | else |
| 1638 | --p; |
| 1639 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1640 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1641 | i = ccline.cmdpos; |
| 1642 | while (i < ccline.cmdlen) |
| 1643 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1644 | |
| 1645 | /* Truncate at the end, required for multi-byte chars. */ |
| 1646 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1647 | #ifdef FEAT_SEARCH_EXTRA |
| 1648 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1649 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1650 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1651 | /* save view settings, so that the screen |
| 1652 | * won't be restored at the wrong position */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1653 | is_state.old_viewstate = is_state.init_viewstate; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1654 | } |
| 1655 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1656 | redrawcmd(); |
| 1657 | } |
| 1658 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1659 | && ccline.cmdprompt == NULL && indent == 0) |
| 1660 | { |
| 1661 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1662 | if (exmode_active |
| 1663 | #ifdef FEAT_EVAL |
| 1664 | || ccline.cmdfirstc == '>' |
| 1665 | #endif |
| 1666 | ) |
| 1667 | goto cmdline_not_changed; |
| 1668 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1669 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | if (!cmd_silent) |
| 1671 | { |
| 1672 | #ifdef FEAT_RIGHTLEFT |
| 1673 | if (cmdmsg_rl) |
| 1674 | msg_col = Columns; |
| 1675 | else |
| 1676 | #endif |
| 1677 | msg_col = 0; |
| 1678 | msg_putchar(' '); /* delete ':' */ |
| 1679 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1680 | #ifdef FEAT_SEARCH_EXTRA |
| 1681 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1682 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1683 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1684 | redraw_cmdline = TRUE; |
| 1685 | goto returncmd; /* back to cmd mode */ |
| 1686 | } |
| 1687 | goto cmdline_changed; |
| 1688 | |
| 1689 | case K_INS: |
| 1690 | case K_KINS: |
| 1691 | #ifdef FEAT_FKMAP |
| 1692 | /* if Farsi mode set, we are in reverse insert mode - |
| 1693 | Do not change the mode */ |
| 1694 | if (cmd_fkmap) |
| 1695 | beep_flush(); |
| 1696 | else |
| 1697 | #endif |
| 1698 | ccline.overstrike = !ccline.overstrike; |
| 1699 | #ifdef CURSOR_SHAPE |
| 1700 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1701 | #endif |
| 1702 | goto cmdline_not_changed; |
| 1703 | |
| 1704 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1705 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1706 | { |
| 1707 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1708 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1709 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1710 | im_set_active(FALSE); /* Disable input method */ |
| 1711 | #endif |
| 1712 | if (b_im_ptr != NULL) |
| 1713 | { |
| 1714 | if (State & LANGMAP) |
| 1715 | *b_im_ptr = B_IMODE_LMAP; |
| 1716 | else |
| 1717 | *b_im_ptr = B_IMODE_NONE; |
| 1718 | } |
| 1719 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1720 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1721 | else |
| 1722 | { |
| 1723 | /* There are no ":lmap" mappings, toggle IM. When |
| 1724 | * 'imdisable' is set don't try getting the status, it's |
| 1725 | * always off. */ |
| 1726 | if ((p_imdisable && b_im_ptr != NULL) |
| 1727 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1728 | { |
| 1729 | im_set_active(FALSE); /* Disable input method */ |
| 1730 | if (b_im_ptr != NULL) |
| 1731 | *b_im_ptr = B_IMODE_NONE; |
| 1732 | } |
| 1733 | else |
| 1734 | { |
| 1735 | im_set_active(TRUE); /* Enable input method */ |
| 1736 | if (b_im_ptr != NULL) |
| 1737 | *b_im_ptr = B_IMODE_IM; |
| 1738 | } |
| 1739 | } |
| 1740 | #endif |
| 1741 | if (b_im_ptr != NULL) |
| 1742 | { |
| 1743 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1744 | set_iminsert_global(); |
| 1745 | else |
| 1746 | set_imsearch_global(); |
| 1747 | } |
| 1748 | #ifdef CURSOR_SHAPE |
| 1749 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1750 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1751 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1752 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1753 | status_redraw_curbuf(); |
| 1754 | #endif |
| 1755 | goto cmdline_not_changed; |
| 1756 | |
| 1757 | /* case '@': only in very old vi */ |
| 1758 | case Ctrl_U: |
| 1759 | /* delete all characters left of the cursor */ |
| 1760 | j = ccline.cmdpos; |
| 1761 | ccline.cmdlen -= j; |
| 1762 | i = ccline.cmdpos = 0; |
| 1763 | while (i < ccline.cmdlen) |
| 1764 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1765 | /* Truncate at the end, required for multi-byte chars. */ |
| 1766 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1767 | #ifdef FEAT_SEARCH_EXTRA |
| 1768 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1769 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1770 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1771 | redrawcmd(); |
| 1772 | goto cmdline_changed; |
| 1773 | |
| 1774 | #ifdef FEAT_CLIPBOARD |
| 1775 | case Ctrl_Y: |
| 1776 | /* Copy the modeless selection, if there is one. */ |
| 1777 | if (clip_star.state != SELECT_CLEARED) |
| 1778 | { |
| 1779 | if (clip_star.state == SELECT_DONE) |
| 1780 | clip_copy_modeless_selection(TRUE); |
| 1781 | goto cmdline_not_changed; |
| 1782 | } |
| 1783 | break; |
| 1784 | #endif |
| 1785 | |
| 1786 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1787 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1788 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1789 | * ":normal" runs out of characters. */ |
| 1790 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1791 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | goto cmdline_not_changed; |
| 1793 | |
| 1794 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1795 | putting it in history */ |
| 1796 | goto returncmd; /* back to cmd mode */ |
| 1797 | |
| 1798 | case Ctrl_R: /* insert register */ |
| 1799 | #ifdef USE_ON_FLY_SCROLL |
| 1800 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1801 | #endif |
| 1802 | putcmdline('"', TRUE); |
| 1803 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1804 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | if (i == Ctrl_O) |
| 1806 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1807 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1808 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1809 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | --no_mapping; |
| 1811 | #ifdef FEAT_EVAL |
| 1812 | /* |
| 1813 | * Insert the result of an expression. |
| 1814 | * Need to save the current command line, to be able to enter |
| 1815 | * a new one... |
| 1816 | */ |
| 1817 | new_cmdpos = -1; |
| 1818 | if (c == '=') |
| 1819 | { |
Bram Moolenaar | ee91c33 | 2018-09-25 22:27:35 +0200 | [diff] [blame] | 1820 | if (ccline.cmdfirstc == '=' // can't do this recursively |
| 1821 | || cmdline_star > 0) // or when typing a password |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | { |
| 1823 | beep_flush(); |
| 1824 | c = ESC; |
| 1825 | } |
| 1826 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1827 | c = get_expr_register(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1828 | } |
| 1829 | #endif |
| 1830 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1831 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1832 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1833 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1834 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1835 | /* When there was a serious error abort getting the |
| 1836 | * command line. */ |
| 1837 | if (aborting()) |
| 1838 | { |
| 1839 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1840 | putting it in history */ |
| 1841 | goto returncmd; /* back to cmd mode */ |
| 1842 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1843 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1844 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1845 | #ifdef FEAT_EVAL |
| 1846 | if (new_cmdpos >= 0) |
| 1847 | { |
| 1848 | /* set_cmdline_pos() was used */ |
| 1849 | if (new_cmdpos > ccline.cmdlen) |
| 1850 | ccline.cmdpos = ccline.cmdlen; |
| 1851 | else |
| 1852 | ccline.cmdpos = new_cmdpos; |
| 1853 | } |
| 1854 | #endif |
| 1855 | } |
| 1856 | redrawcmd(); |
| 1857 | goto cmdline_changed; |
| 1858 | |
| 1859 | case Ctrl_D: |
| 1860 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1861 | break; /* Use ^D as normal char instead */ |
| 1862 | |
| 1863 | redrawcmd(); |
| 1864 | continue; /* don't do incremental search now */ |
| 1865 | |
| 1866 | case K_RIGHT: |
| 1867 | case K_S_RIGHT: |
| 1868 | case K_C_RIGHT: |
| 1869 | do |
| 1870 | { |
| 1871 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1872 | break; |
| 1873 | i = cmdline_charsize(ccline.cmdpos); |
| 1874 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1875 | break; |
| 1876 | ccline.cmdspos += i; |
| 1877 | #ifdef FEAT_MBYTE |
| 1878 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1879 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1880 | + ccline.cmdpos); |
| 1881 | else |
| 1882 | #endif |
| 1883 | ++ccline.cmdpos; |
| 1884 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1885 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1886 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1888 | #ifdef FEAT_MBYTE |
| 1889 | if (has_mbyte) |
| 1890 | set_cmdspos_cursor(); |
| 1891 | #endif |
| 1892 | goto cmdline_not_changed; |
| 1893 | |
| 1894 | case K_LEFT: |
| 1895 | case K_S_LEFT: |
| 1896 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1897 | if (ccline.cmdpos == 0) |
| 1898 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | do |
| 1900 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1901 | --ccline.cmdpos; |
| 1902 | #ifdef FEAT_MBYTE |
| 1903 | if (has_mbyte) /* move to first byte of char */ |
| 1904 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1905 | ccline.cmdbuff + ccline.cmdpos); |
| 1906 | #endif |
| 1907 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1908 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1909 | while (ccline.cmdpos > 0 |
| 1910 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1911 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1913 | #ifdef FEAT_MBYTE |
| 1914 | if (has_mbyte) |
| 1915 | set_cmdspos_cursor(); |
| 1916 | #endif |
| 1917 | goto cmdline_not_changed; |
| 1918 | |
| 1919 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1920 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1921 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1923 | #ifdef FEAT_GUI_W32 |
| 1924 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1925 | * cancelled. */ |
| 1926 | case K_F4: |
| 1927 | if (mod_mask == MOD_MASK_ALT) |
| 1928 | { |
| 1929 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1930 | goto cmdline_not_changed; |
| 1931 | } |
| 1932 | break; |
| 1933 | #endif |
| 1934 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1935 | #ifdef FEAT_MOUSE |
| 1936 | case K_MIDDLEDRAG: |
| 1937 | case K_MIDDLERELEASE: |
| 1938 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1939 | |
| 1940 | case K_MIDDLEMOUSE: |
| 1941 | # ifdef FEAT_GUI |
| 1942 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1943 | if (!gui.in_use) |
| 1944 | # endif |
| 1945 | if (!mouse_has(MOUSE_COMMAND)) |
| 1946 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1947 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1949 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1950 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1951 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1952 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | redrawcmd(); |
| 1954 | goto cmdline_changed; |
| 1955 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1956 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1957 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1958 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | redrawcmd(); |
| 1960 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1961 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1962 | |
| 1963 | case K_LEFTDRAG: |
| 1964 | case K_LEFTRELEASE: |
| 1965 | case K_RIGHTDRAG: |
| 1966 | case K_RIGHTRELEASE: |
| 1967 | /* Ignore drag and release events when the button-down wasn't |
| 1968 | * seen before. */ |
| 1969 | if (ignore_drag_release) |
| 1970 | goto cmdline_not_changed; |
| 1971 | /* FALLTHROUGH */ |
| 1972 | case K_LEFTMOUSE: |
| 1973 | case K_RIGHTMOUSE: |
| 1974 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1975 | ignore_drag_release = TRUE; |
| 1976 | else |
| 1977 | ignore_drag_release = FALSE; |
| 1978 | # ifdef FEAT_GUI |
| 1979 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1980 | if (!gui.in_use) |
| 1981 | # endif |
| 1982 | if (!mouse_has(MOUSE_COMMAND)) |
| 1983 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1984 | # ifdef FEAT_CLIPBOARD |
| 1985 | if (mouse_row < cmdline_row && clip_star.available) |
| 1986 | { |
| 1987 | int button, is_click, is_drag; |
| 1988 | |
| 1989 | /* |
| 1990 | * Handle modeless selection. |
| 1991 | */ |
| 1992 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1993 | &is_click, &is_drag); |
| 1994 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1995 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1996 | { |
| 1997 | /* Translate shift-left to right button. */ |
| 1998 | button = MOUSE_RIGHT; |
| 1999 | mod_mask &= ~MOD_MASK_SHIFT; |
| 2000 | } |
| 2001 | clip_modeless(button, is_click, is_drag); |
| 2002 | goto cmdline_not_changed; |
| 2003 | } |
| 2004 | # endif |
| 2005 | |
| 2006 | set_cmdspos(); |
| 2007 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 2008 | ++ccline.cmdpos) |
| 2009 | { |
| 2010 | i = cmdline_charsize(ccline.cmdpos); |
| 2011 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 2012 | && mouse_col < ccline.cmdspos % Columns + i) |
| 2013 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2014 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2015 | if (has_mbyte) |
| 2016 | { |
| 2017 | /* Count ">" for double-wide char that doesn't fit. */ |
| 2018 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2019 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2020 | + ccline.cmdpos) - 1; |
| 2021 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2022 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2023 | ccline.cmdspos += i; |
| 2024 | } |
| 2025 | goto cmdline_not_changed; |
| 2026 | |
| 2027 | /* Mouse scroll wheel: ignored here */ |
| 2028 | case K_MOUSEDOWN: |
| 2029 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2030 | case K_MOUSELEFT: |
| 2031 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2032 | /* Alternate buttons ignored here */ |
| 2033 | case K_X1MOUSE: |
| 2034 | case K_X1DRAG: |
| 2035 | case K_X1RELEASE: |
| 2036 | case K_X2MOUSE: |
| 2037 | case K_X2DRAG: |
| 2038 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 2039 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | goto cmdline_not_changed; |
| 2041 | |
| 2042 | #endif /* FEAT_MOUSE */ |
| 2043 | |
| 2044 | #ifdef FEAT_GUI |
| 2045 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 2046 | case K_LEFTRELEASE_NM: |
| 2047 | goto cmdline_not_changed; |
| 2048 | |
| 2049 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2050 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2051 | { |
| 2052 | gui_do_scroll(); |
| 2053 | redrawcmd(); |
| 2054 | } |
| 2055 | goto cmdline_not_changed; |
| 2056 | |
| 2057 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2058 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2059 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2060 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | redrawcmd(); |
| 2062 | } |
| 2063 | goto cmdline_not_changed; |
| 2064 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2065 | #ifdef FEAT_GUI_TABLINE |
| 2066 | case K_TABLINE: |
| 2067 | case K_TABMENU: |
| 2068 | /* Don't want to change any tabs here. Make sure the same tab |
| 2069 | * is still selected. */ |
| 2070 | if (gui_use_tabline()) |
| 2071 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 2072 | goto cmdline_not_changed; |
| 2073 | #endif |
| 2074 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2075 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 2076 | goto cmdline_not_changed; |
| 2077 | |
| 2078 | case Ctrl_B: /* begin of command line */ |
| 2079 | case K_HOME: |
| 2080 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | case K_S_HOME: |
| 2082 | case K_C_HOME: |
| 2083 | ccline.cmdpos = 0; |
| 2084 | set_cmdspos(); |
| 2085 | goto cmdline_not_changed; |
| 2086 | |
| 2087 | case Ctrl_E: /* end of command line */ |
| 2088 | case K_END: |
| 2089 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2090 | case K_S_END: |
| 2091 | case K_C_END: |
| 2092 | ccline.cmdpos = ccline.cmdlen; |
| 2093 | set_cmdspos_cursor(); |
| 2094 | goto cmdline_not_changed; |
| 2095 | |
| 2096 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2097 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | goto cmdline_changed; |
| 2100 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2101 | case Ctrl_L: |
| 2102 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2103 | if (may_add_char_to_search(firstc, &c, &is_state) == OK) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2104 | goto cmdline_not_changed; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2105 | #endif |
| 2106 | |
| 2107 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2108 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | goto cmdline_changed; |
| 2111 | |
| 2112 | case Ctrl_N: /* next match */ |
| 2113 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 2114 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2116 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 2117 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2118 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2119 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2120 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2121 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 2122 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2123 | case K_UP: |
| 2124 | case K_DOWN: |
| 2125 | case K_S_UP: |
| 2126 | case K_S_DOWN: |
| 2127 | case K_PAGEUP: |
| 2128 | case K_KPAGEUP: |
| 2129 | case K_PAGEDOWN: |
| 2130 | case K_KPAGEDOWN: |
| 2131 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 2132 | goto cmdline_not_changed; |
| 2133 | |
| 2134 | i = hiscnt; |
| 2135 | |
| 2136 | /* save current command string so it can be restored later */ |
| 2137 | if (lookfor == NULL) |
| 2138 | { |
| 2139 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 2140 | goto cmdline_not_changed; |
| 2141 | lookfor[ccline.cmdpos] = NUL; |
| 2142 | } |
| 2143 | |
| 2144 | j = (int)STRLEN(lookfor); |
| 2145 | for (;;) |
| 2146 | { |
| 2147 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2148 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2149 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2150 | { |
| 2151 | if (hiscnt == hislen) /* first time */ |
| 2152 | hiscnt = hisidx[histype]; |
| 2153 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 2154 | hiscnt = hislen - 1; |
| 2155 | else if (hiscnt != hisidx[histype] + 1) |
| 2156 | --hiscnt; |
| 2157 | else /* at top of list */ |
| 2158 | { |
| 2159 | hiscnt = i; |
| 2160 | break; |
| 2161 | } |
| 2162 | } |
| 2163 | else /* one step forwards */ |
| 2164 | { |
| 2165 | /* on last entry, clear the line */ |
| 2166 | if (hiscnt == hisidx[histype]) |
| 2167 | { |
| 2168 | hiscnt = hislen; |
| 2169 | break; |
| 2170 | } |
| 2171 | |
| 2172 | /* not on a history line, nothing to do */ |
| 2173 | if (hiscnt == hislen) |
| 2174 | break; |
| 2175 | if (hiscnt == hislen - 1) /* wrap around */ |
| 2176 | hiscnt = 0; |
| 2177 | else |
| 2178 | ++hiscnt; |
| 2179 | } |
| 2180 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 2181 | { |
| 2182 | hiscnt = i; |
| 2183 | break; |
| 2184 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2185 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2186 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 2188 | lookfor, (size_t)j) == 0) |
| 2189 | break; |
| 2190 | } |
| 2191 | |
| 2192 | if (hiscnt != i) /* jumped to other entry */ |
| 2193 | { |
| 2194 | char_u *p; |
| 2195 | int len; |
| 2196 | int old_firstc; |
| 2197 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2198 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2199 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2200 | if (hiscnt == hislen) |
| 2201 | p = lookfor; /* back to the old one */ |
| 2202 | else |
| 2203 | p = history[histype][hiscnt].hisstr; |
| 2204 | |
| 2205 | if (histype == HIST_SEARCH |
| 2206 | && p != lookfor |
| 2207 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 2208 | { |
| 2209 | /* Correct for the separator character used when |
| 2210 | * adding the history entry vs the one used now. |
| 2211 | * First loop: count length. |
| 2212 | * Second loop: copy the characters. */ |
| 2213 | for (i = 0; i <= 1; ++i) |
| 2214 | { |
| 2215 | len = 0; |
| 2216 | for (j = 0; p[j] != NUL; ++j) |
| 2217 | { |
| 2218 | /* Replace old sep with new sep, unless it is |
| 2219 | * escaped. */ |
| 2220 | if (p[j] == old_firstc |
| 2221 | && (j == 0 || p[j - 1] != '\\')) |
| 2222 | { |
| 2223 | if (i > 0) |
| 2224 | ccline.cmdbuff[len] = firstc; |
| 2225 | } |
| 2226 | else |
| 2227 | { |
| 2228 | /* Escape new sep, unless it is already |
| 2229 | * escaped. */ |
| 2230 | if (p[j] == firstc |
| 2231 | && (j == 0 || p[j - 1] != '\\')) |
| 2232 | { |
| 2233 | if (i > 0) |
| 2234 | ccline.cmdbuff[len] = '\\'; |
| 2235 | ++len; |
| 2236 | } |
| 2237 | if (i > 0) |
| 2238 | ccline.cmdbuff[len] = p[j]; |
| 2239 | } |
| 2240 | ++len; |
| 2241 | } |
| 2242 | if (i == 0) |
| 2243 | { |
| 2244 | alloc_cmdbuff(len); |
| 2245 | if (ccline.cmdbuff == NULL) |
| 2246 | goto returncmd; |
| 2247 | } |
| 2248 | } |
| 2249 | ccline.cmdbuff[len] = NUL; |
| 2250 | } |
| 2251 | else |
| 2252 | { |
| 2253 | alloc_cmdbuff((int)STRLEN(p)); |
| 2254 | if (ccline.cmdbuff == NULL) |
| 2255 | goto returncmd; |
| 2256 | STRCPY(ccline.cmdbuff, p); |
| 2257 | } |
| 2258 | |
| 2259 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 2260 | redrawcmd(); |
| 2261 | goto cmdline_changed; |
| 2262 | } |
| 2263 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2264 | #endif |
| 2265 | goto cmdline_not_changed; |
| 2266 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2267 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2268 | case Ctrl_G: /* next match */ |
| 2269 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2270 | if (may_adjust_incsearch_highlighting( |
| 2271 | firstc, count, &is_state, c) == FAIL) |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2272 | goto cmdline_not_changed; |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2273 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2274 | #endif |
| 2275 | |
| 2276 | case Ctrl_V: |
| 2277 | case Ctrl_Q: |
| 2278 | #ifdef FEAT_MOUSE |
| 2279 | ignore_drag_release = TRUE; |
| 2280 | #endif |
| 2281 | putcmdline('^', TRUE); |
| 2282 | c = get_literal(); /* get next (two) character(s) */ |
| 2283 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2284 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | #ifdef FEAT_MBYTE |
| 2286 | /* may need to remove ^ when composing char was typed */ |
| 2287 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 2288 | { |
| 2289 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2290 | msg_putchar(' '); |
| 2291 | cursorcmd(); |
| 2292 | } |
| 2293 | #endif |
| 2294 | break; |
| 2295 | |
| 2296 | #ifdef FEAT_DIGRAPHS |
| 2297 | case Ctrl_K: |
| 2298 | #ifdef FEAT_MOUSE |
| 2299 | ignore_drag_release = TRUE; |
| 2300 | #endif |
| 2301 | putcmdline('?', TRUE); |
| 2302 | #ifdef USE_ON_FLY_SCROLL |
| 2303 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 2304 | #endif |
| 2305 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2306 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | if (c != NUL) |
| 2308 | break; |
| 2309 | |
| 2310 | redrawcmd(); |
| 2311 | goto cmdline_not_changed; |
| 2312 | #endif /* FEAT_DIGRAPHS */ |
| 2313 | |
| 2314 | #ifdef FEAT_RIGHTLEFT |
| 2315 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 2316 | if (!p_ari) |
| 2317 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2318 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | if (p_altkeymap) |
| 2320 | { |
| 2321 | cmd_fkmap = !cmd_fkmap; |
| 2322 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 2323 | ccline.overstrike = FALSE; |
| 2324 | } |
| 2325 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2326 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2327 | cmd_hkmap = !cmd_hkmap; |
| 2328 | goto cmdline_not_changed; |
| 2329 | #endif |
| 2330 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2331 | case K_PS: |
| 2332 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 2333 | goto cmdline_changed; |
| 2334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | default: |
| 2336 | #ifdef UNIX |
| 2337 | if (c == intr_char) |
| 2338 | { |
| 2339 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 2340 | putting it in history */ |
| 2341 | goto returncmd; /* back to Normal mode */ |
| 2342 | } |
| 2343 | #endif |
| 2344 | /* |
| 2345 | * Normal character with no special meaning. Just set mod_mask |
| 2346 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 2347 | * the string <S-Space>. This should only happen after ^V. |
| 2348 | */ |
| 2349 | if (!IS_SPECIAL(c)) |
| 2350 | mod_mask = 0x0; |
| 2351 | break; |
| 2352 | } |
| 2353 | /* |
| 2354 | * End of switch on command line character. |
| 2355 | * We come here if we have a normal character. |
| 2356 | */ |
| 2357 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2358 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | #ifdef FEAT_MBYTE |
| 2360 | /* Add ABBR_OFF for characters above 0x100, this is |
| 2361 | * what check_abbr() expects. */ |
| 2362 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 2363 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2364 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | goto cmdline_changed; |
| 2366 | |
| 2367 | /* |
| 2368 | * put the character in the command line |
| 2369 | */ |
| 2370 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 2371 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 2372 | else |
| 2373 | { |
| 2374 | #ifdef FEAT_MBYTE |
| 2375 | if (has_mbyte) |
| 2376 | { |
| 2377 | j = (*mb_char2bytes)(c, IObuff); |
| 2378 | IObuff[j] = NUL; /* exclude composing chars */ |
| 2379 | put_on_cmdline(IObuff, j, TRUE); |
| 2380 | } |
| 2381 | else |
| 2382 | #endif |
| 2383 | { |
| 2384 | IObuff[0] = c; |
| 2385 | put_on_cmdline(IObuff, 1, TRUE); |
| 2386 | } |
| 2387 | } |
| 2388 | goto cmdline_changed; |
| 2389 | |
| 2390 | /* |
| 2391 | * This part implements incremental searches for "/" and "?" |
| 2392 | * Jump to cmdline_not_changed when a character has been read but the command |
| 2393 | * line did not change. Then we only search and redraw if something changed in |
| 2394 | * the past. |
| 2395 | * Jump to cmdline_changed when the command line did change. |
| 2396 | * (Sorry for the goto's, I know it is ugly). |
| 2397 | */ |
| 2398 | cmdline_not_changed: |
| 2399 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2400 | if (!is_state.incsearch_postponed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | continue; |
| 2402 | #endif |
| 2403 | |
| 2404 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2405 | /* Trigger CmdlineChanged autocommands. */ |
| 2406 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2407 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2409 | may_do_incsearch_highlighting(firstc, count, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2410 | #endif |
| 2411 | |
| 2412 | #ifdef FEAT_RIGHTLEFT |
| 2413 | if (cmdmsg_rl |
| 2414 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2415 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2416 | # endif |
| 2417 | ) |
| 2418 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2419 | * right-left typing. Not efficient, but it works. |
| 2420 | * Do it only when there are no characters left to read |
| 2421 | * to avoid useless intermediate redraws. */ |
| 2422 | if (vpeekc() == NUL) |
| 2423 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2424 | #endif |
| 2425 | } |
| 2426 | |
| 2427 | returncmd: |
| 2428 | |
| 2429 | #ifdef FEAT_RIGHTLEFT |
| 2430 | cmdmsg_rl = FALSE; |
| 2431 | #endif |
| 2432 | |
| 2433 | #ifdef FEAT_FKMAP |
| 2434 | cmd_fkmap = 0; |
| 2435 | #endif |
| 2436 | |
| 2437 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2438 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2439 | |
| 2440 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 2441 | finish_incsearch_highlighting(gotesc, &is_state, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | #endif |
| 2443 | |
| 2444 | if (ccline.cmdbuff != NULL) |
| 2445 | { |
| 2446 | /* |
| 2447 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2448 | */ |
| 2449 | #ifdef FEAT_CMDHIST |
| 2450 | if (ccline.cmdlen && firstc != NUL |
| 2451 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2452 | { |
| 2453 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2454 | histype == HIST_SEARCH ? firstc : NUL); |
| 2455 | if (firstc == ':') |
| 2456 | { |
| 2457 | vim_free(new_last_cmdline); |
| 2458 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2459 | } |
| 2460 | } |
| 2461 | #endif |
| 2462 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2463 | if (gotesc) |
| 2464 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | /* |
| 2468 | * If the screen was shifted up, redraw the whole screen (later). |
| 2469 | * If the line is too long, clear it, so ruler and shown command do |
| 2470 | * not get printed in the middle of it. |
| 2471 | */ |
| 2472 | msg_check(); |
| 2473 | msg_scroll = save_msg_scroll; |
| 2474 | redir_off = FALSE; |
| 2475 | |
| 2476 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2477 | if (some_key_typed) |
| 2478 | need_wait_return = FALSE; |
| 2479 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2480 | /* Trigger CmdlineLeave autocommands. */ |
| 2481 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2482 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2483 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2484 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2486 | im_save_status(b_im_ptr); |
| 2487 | im_set_active(FALSE); |
| 2488 | #endif |
| 2489 | #ifdef FEAT_MOUSE |
| 2490 | setmouse(); |
| 2491 | #endif |
| 2492 | #ifdef CURSOR_SHAPE |
| 2493 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2494 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2495 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2497 | theend: |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2498 | { |
| 2499 | char_u *p = ccline.cmdbuff; |
| 2500 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2501 | if (did_save_ccline) |
| 2502 | restore_cmdline(&save_ccline); |
| 2503 | else |
| 2504 | ccline.cmdbuff = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2505 | return p; |
| 2506 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2510 | /* |
| 2511 | * Get a command line with a prompt. |
| 2512 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2513 | * f_input() when evaluating an expression from CTRL-R =). |
| 2514 | * Returns the command line in allocated memory, or NULL. |
| 2515 | */ |
| 2516 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2517 | getcmdline_prompt( |
| 2518 | int firstc, |
| 2519 | char_u *prompt, /* command line prompt */ |
| 2520 | int attr, /* attributes for prompt */ |
| 2521 | int xp_context, /* type of expansion */ |
| 2522 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | { |
| 2524 | char_u *s; |
| 2525 | struct cmdline_info save_ccline; |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2526 | int did_save_ccline = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2527 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2528 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2529 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2530 | if (ccline.cmdbuff != NULL) |
| 2531 | { |
| 2532 | // Save the values of the current cmdline and restore them below. |
| 2533 | save_cmdline(&save_ccline); |
| 2534 | did_save_ccline = TRUE; |
| 2535 | } |
| 2536 | |
| 2537 | vim_memset(&ccline, 0, sizeof(struct cmdline_info)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2538 | ccline.cmdprompt = prompt; |
| 2539 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2540 | # ifdef FEAT_EVAL |
| 2541 | ccline.xp_context = xp_context; |
| 2542 | ccline.xp_arg = xp_arg; |
| 2543 | ccline.input_fn = (firstc == '@'); |
| 2544 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2545 | msg_silent = 0; |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 2546 | s = getcmdline_int(firstc, 1L, 0, FALSE); |
| 2547 | |
| 2548 | if (did_save_ccline) |
| 2549 | restore_cmdline(&save_ccline); |
| 2550 | |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2551 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2552 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2553 | * But only if called recursively and the commandline is therefore being |
| 2554 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2555 | * so we need its modified msg_col left intact. */ |
| 2556 | if (ccline.cmdbuff != NULL) |
| 2557 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2558 | |
| 2559 | return s; |
| 2560 | } |
| 2561 | #endif |
| 2562 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2563 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2564 | * Return TRUE when the text must not be changed and we can't switch to |
| 2565 | * another window or buffer. Used when editing the command line, evaluating |
| 2566 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2567 | */ |
| 2568 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2569 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2570 | { |
| 2571 | #ifdef FEAT_CMDWIN |
| 2572 | if (cmdwin_type != 0) |
| 2573 | return TRUE; |
| 2574 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2575 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Give an error message for a command that isn't allowed while the cmdline |
| 2580 | * window is open or editing the cmdline in another way. |
| 2581 | */ |
| 2582 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2583 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2584 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2585 | EMSG(_(get_text_locked_msg())); |
| 2586 | } |
| 2587 | |
| 2588 | char_u * |
| 2589 | get_text_locked_msg(void) |
| 2590 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2591 | #ifdef FEAT_CMDWIN |
| 2592 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2593 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2594 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2595 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2598 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2599 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2600 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2601 | */ |
| 2602 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2603 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2604 | { |
| 2605 | if (curbuf_lock > 0) |
| 2606 | { |
| 2607 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2608 | return TRUE; |
| 2609 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2610 | return allbuf_locked(); |
| 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2615 | * message. |
| 2616 | */ |
| 2617 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2618 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2619 | { |
| 2620 | if (allbuf_lock > 0) |
| 2621 | { |
| 2622 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2623 | return TRUE; |
| 2624 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2625 | return FALSE; |
| 2626 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2627 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2629 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | { |
| 2631 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2632 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2633 | return 1; |
| 2634 | #endif |
| 2635 | return ptr2cells(ccline.cmdbuff + idx); |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * Compute the offset of the cursor on the command line for the prompt and |
| 2640 | * indent. |
| 2641 | */ |
| 2642 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2643 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2645 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2646 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2647 | else |
| 2648 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * Compute the screen position for the cursor on the command line. |
| 2653 | */ |
| 2654 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2655 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | { |
| 2657 | int i, m, c; |
| 2658 | |
| 2659 | set_cmdspos(); |
| 2660 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2661 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2663 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2664 | m = MAXCOL; |
| 2665 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | else |
| 2667 | m = MAXCOL; |
| 2668 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2669 | { |
| 2670 | c = cmdline_charsize(i); |
| 2671 | #ifdef FEAT_MBYTE |
| 2672 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2673 | if (has_mbyte) |
| 2674 | correct_cmdspos(i, c); |
| 2675 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2676 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2677 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2678 | if ((ccline.cmdspos += c) >= m) |
| 2679 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | ccline.cmdspos -= c; |
| 2681 | break; |
| 2682 | } |
| 2683 | #ifdef FEAT_MBYTE |
| 2684 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2685 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | #endif |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | #ifdef FEAT_MBYTE |
| 2691 | /* |
| 2692 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2693 | * character that doesn't fit, so that a ">" must be displayed. |
| 2694 | */ |
| 2695 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2696 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2698 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2699 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2700 | && ccline.cmdspos % Columns + cells > Columns) |
| 2701 | ccline.cmdspos++; |
| 2702 | } |
| 2703 | #endif |
| 2704 | |
| 2705 | /* |
| 2706 | * Get an Ex command line for the ":" command. |
| 2707 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2708 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2709 | getexline( |
| 2710 | int c, /* normally ':', NUL for ":append" */ |
| 2711 | void *cookie UNUSED, |
| 2712 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2713 | { |
| 2714 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2715 | if (exec_from_reg && vpeekc() == ':') |
| 2716 | (void)vgetc(); |
| 2717 | return getcmdline(c, 1L, indent); |
| 2718 | } |
| 2719 | |
| 2720 | /* |
| 2721 | * Get an Ex command line for Ex mode. |
| 2722 | * In Ex mode we only use the OS supplied line editing features and no |
| 2723 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2724 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2726 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2727 | getexmodeline( |
| 2728 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2729 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2730 | void *cookie UNUSED, |
| 2731 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2732 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2733 | garray_T line_ga; |
| 2734 | char_u *pend; |
| 2735 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2736 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2737 | int escaped = FALSE; /* CTRL-V typed */ |
| 2738 | int vcol = 0; |
| 2739 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2740 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2741 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | |
| 2743 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2744 | * confuses the system function that computes tabstops. */ |
| 2745 | cursor_on(); |
| 2746 | |
| 2747 | /* always start in column 0; write a newline if necessary */ |
| 2748 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2749 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2750 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2751 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2753 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2754 | if (p_prompt) |
| 2755 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | while (indent-- > 0) |
| 2757 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2758 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | ga_init2(&line_ga, 1, 30); |
| 2762 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2763 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2764 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2765 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2766 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2767 | while (indent >= 8) |
| 2768 | { |
| 2769 | ga_append(&line_ga, TAB); |
| 2770 | msg_puts((char_u *)" "); |
| 2771 | indent -= 8; |
| 2772 | } |
| 2773 | while (indent-- > 0) |
| 2774 | { |
| 2775 | ga_append(&line_ga, ' '); |
| 2776 | msg_putchar(' '); |
| 2777 | } |
| 2778 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2779 | ++no_mapping; |
| 2780 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2781 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | /* |
| 2783 | * Get the line, one character at a time. |
| 2784 | */ |
| 2785 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2786 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2787 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2788 | long sw; |
| 2789 | char_u *s; |
| 2790 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2791 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2792 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2793 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2794 | /* |
| 2795 | * Get one character at a time. |
| 2796 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2797 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* Check for a ":normal" command and no more characters left. */ |
| 2800 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2801 | c1 = '\n'; |
| 2802 | else |
| 2803 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2804 | |
| 2805 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2806 | * Handle line editing. |
| 2807 | * Previously this was left to the system, putting the terminal in |
| 2808 | * 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] | 2809 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2810 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2812 | msg_putchar('\n'); |
| 2813 | break; |
| 2814 | } |
| 2815 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2816 | if (c1 == K_PS) |
| 2817 | { |
| 2818 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2819 | goto redraw; |
| 2820 | } |
| 2821 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2822 | if (!escaped) |
| 2823 | { |
| 2824 | /* CR typed means "enter", which is NL */ |
| 2825 | if (c1 == '\r') |
| 2826 | c1 = '\n'; |
| 2827 | |
| 2828 | if (c1 == BS || c1 == K_BS |
| 2829 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2830 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2831 | if (line_ga.ga_len > 0) |
| 2832 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2833 | #ifdef FEAT_MBYTE |
| 2834 | if (has_mbyte) |
| 2835 | { |
| 2836 | p = (char_u *)line_ga.ga_data; |
| 2837 | p[line_ga.ga_len] = NUL; |
| 2838 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2839 | line_ga.ga_len -= len; |
| 2840 | } |
| 2841 | else |
| 2842 | #endif |
| 2843 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2844 | goto redraw; |
| 2845 | } |
| 2846 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2849 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2850 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2851 | msg_col = startcol; |
| 2852 | msg_clr_eos(); |
| 2853 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2854 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | if (c1 == Ctrl_T) |
| 2858 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2859 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2860 | p = (char_u *)line_ga.ga_data; |
| 2861 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2862 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2863 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2864 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2865 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2866 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2867 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2868 | p = (char_u *)line_ga.ga_data; |
| 2869 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2870 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2871 | *s = ' '; |
| 2872 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2873 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2874 | redraw: |
| 2875 | /* redraw the line */ |
| 2876 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2877 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2878 | p = (char_u *)line_ga.ga_data; |
| 2879 | p[line_ga.ga_len] = NUL; |
| 2880 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2881 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2882 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2883 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2884 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2885 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2886 | msg_putchar(' '); |
| 2887 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2888 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2890 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2892 | len = MB_PTR2LEN(p); |
| 2893 | msg_outtrans_len(p, len); |
| 2894 | vcol += ptr2cells(p); |
| 2895 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2896 | } |
| 2897 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2898 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2899 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2900 | continue; |
| 2901 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2903 | if (c1 == Ctrl_D) |
| 2904 | { |
| 2905 | /* Delete one shiftwidth. */ |
| 2906 | p = (char_u *)line_ga.ga_data; |
| 2907 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2909 | if (prev_char == '^') |
| 2910 | ex_keep_indent = TRUE; |
| 2911 | indent = 0; |
| 2912 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2913 | } |
| 2914 | else |
| 2915 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2916 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2917 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2918 | if (indent > 0) |
| 2919 | { |
| 2920 | --indent; |
| 2921 | indent -= indent % get_sw_value(curbuf); |
| 2922 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2923 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2924 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2925 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2926 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2927 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2928 | --line_ga.ga_len; |
| 2929 | } |
| 2930 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2932 | |
| 2933 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2934 | { |
| 2935 | escaped = TRUE; |
| 2936 | continue; |
| 2937 | } |
| 2938 | |
| 2939 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2940 | if (IS_SPECIAL(c1)) |
| 2941 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2942 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2943 | |
| 2944 | if (IS_SPECIAL(c1)) |
| 2945 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2946 | #ifdef FEAT_MBYTE |
| 2947 | if (has_mbyte) |
| 2948 | len = (*mb_char2bytes)(c1, |
| 2949 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2950 | else |
| 2951 | #endif |
| 2952 | { |
| 2953 | len = 1; |
| 2954 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2955 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2956 | if (c1 == '\n') |
| 2957 | msg_putchar('\n'); |
| 2958 | else if (c1 == TAB) |
| 2959 | { |
| 2960 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2961 | do |
| 2962 | { |
| 2963 | msg_putchar(' '); |
| 2964 | } while (++vcol % 8); |
| 2965 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2967 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2968 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2969 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2970 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2972 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2973 | escaped = FALSE; |
| 2974 | |
| 2975 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2976 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2977 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2978 | /* We are done when a NL is entered, but not when it comes after an |
| 2979 | * odd number of backslashes, that results in a NUL. */ |
| 2980 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2982 | int bcount = 0; |
| 2983 | |
| 2984 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2985 | ++bcount; |
| 2986 | |
| 2987 | if (bcount > 0) |
| 2988 | { |
| 2989 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2990 | * "\NL", etc. */ |
| 2991 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2992 | pend -= (bcount + 1) / 2; |
| 2993 | pend[-1] = '\n'; |
| 2994 | } |
| 2995 | |
| 2996 | if ((bcount & 1) == 0) |
| 2997 | { |
| 2998 | --line_ga.ga_len; |
| 2999 | --pend; |
| 3000 | *pend = NUL; |
| 3001 | break; |
| 3002 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 3006 | --no_mapping; |
| 3007 | --allow_keys; |
| 3008 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | /* make following messages go to the next line */ |
| 3010 | msg_didout = FALSE; |
| 3011 | msg_col = 0; |
| 3012 | if (msg_row < Rows - 1) |
| 3013 | ++msg_row; |
| 3014 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 3015 | |
| 3016 | if (got_int) |
| 3017 | ga_clear(&line_ga); |
| 3018 | |
| 3019 | return (char_u *)line_ga.ga_data; |
| 3020 | } |
| 3021 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3022 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 3023 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | /* |
| 3025 | * Return TRUE if ccline.overstrike is on. |
| 3026 | */ |
| 3027 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3028 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3029 | { |
| 3030 | return ccline.overstrike; |
| 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Return TRUE if the cursor is at the end of the cmdline. |
| 3035 | */ |
| 3036 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3037 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | { |
| 3039 | return (ccline.cmdpos >= ccline.cmdlen); |
| 3040 | } |
| 3041 | #endif |
| 3042 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 3043 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | /* |
| 3045 | * Return the virtual column number at the current cursor position. |
| 3046 | * This is used by the IM code to obtain the start of the preedit string. |
| 3047 | */ |
| 3048 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3049 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | { |
| 3051 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 3052 | return MAXCOL; |
| 3053 | |
| 3054 | # ifdef FEAT_MBYTE |
| 3055 | if (has_mbyte) |
| 3056 | { |
| 3057 | colnr_T col; |
| 3058 | int i = 0; |
| 3059 | |
| 3060 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3061 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | |
| 3063 | return col; |
| 3064 | } |
| 3065 | else |
| 3066 | # endif |
| 3067 | return ccline.cmdpos; |
| 3068 | } |
| 3069 | #endif |
| 3070 | |
| 3071 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3072 | /* |
| 3073 | * If part of the command line is an IM preedit string, redraw it with |
| 3074 | * IM feedback attributes. The cursor position is restored after drawing. |
| 3075 | */ |
| 3076 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3077 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3078 | { |
| 3079 | if ((State & CMDLINE) |
| 3080 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 3081 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | && !p_imdisable |
| 3083 | && im_is_preediting()) |
| 3084 | { |
| 3085 | int cmdpos = 0; |
| 3086 | int cmdspos; |
| 3087 | int old_row; |
| 3088 | int old_col; |
| 3089 | colnr_T col; |
| 3090 | |
| 3091 | old_row = msg_row; |
| 3092 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3093 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3094 | |
| 3095 | # ifdef FEAT_MBYTE |
| 3096 | if (has_mbyte) |
| 3097 | { |
| 3098 | for (col = 0; col < preedit_start_col |
| 3099 | && cmdpos < ccline.cmdlen; ++col) |
| 3100 | { |
| 3101 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3102 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3103 | } |
| 3104 | } |
| 3105 | else |
| 3106 | # endif |
| 3107 | { |
| 3108 | cmdspos += preedit_start_col; |
| 3109 | cmdpos += preedit_start_col; |
| 3110 | } |
| 3111 | |
| 3112 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 3113 | msg_col = cmdspos % (int)Columns; |
| 3114 | if (msg_row >= Rows) |
| 3115 | msg_row = Rows - 1; |
| 3116 | |
| 3117 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 3118 | { |
| 3119 | int char_len; |
| 3120 | int char_attr; |
| 3121 | |
| 3122 | char_attr = im_get_feedback_attr(col); |
| 3123 | if (char_attr < 0) |
| 3124 | break; /* end of preedit string */ |
| 3125 | |
| 3126 | # ifdef FEAT_MBYTE |
| 3127 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3128 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3129 | else |
| 3130 | # endif |
| 3131 | char_len = 1; |
| 3132 | |
| 3133 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 3134 | cmdpos += char_len; |
| 3135 | } |
| 3136 | |
| 3137 | msg_row = old_row; |
| 3138 | msg_col = old_col; |
| 3139 | } |
| 3140 | } |
| 3141 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 3142 | |
| 3143 | /* |
| 3144 | * Allocate a new command line buffer. |
| 3145 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3146 | */ |
| 3147 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3148 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3149 | { |
| 3150 | /* |
| 3151 | * give some extra space to avoid having to allocate all the time |
| 3152 | */ |
| 3153 | if (len < 80) |
| 3154 | len = 100; |
| 3155 | else |
| 3156 | len += 20; |
| 3157 | |
| 3158 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 3159 | ccline.cmdbufflen = len; |
| 3160 | } |
| 3161 | |
| 3162 | /* |
| 3163 | * Re-allocate the command line to length len + something extra. |
| 3164 | * return FAIL for failure, OK otherwise |
| 3165 | */ |
| 3166 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3167 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3168 | { |
| 3169 | char_u *p; |
| 3170 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3171 | if (len < ccline.cmdbufflen) |
| 3172 | return OK; /* no need to resize */ |
| 3173 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3174 | p = ccline.cmdbuff; |
| 3175 | alloc_cmdbuff(len); /* will get some more */ |
| 3176 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 3177 | { |
| 3178 | ccline.cmdbuff = p; /* keep the old one */ |
| 3179 | return FAIL; |
| 3180 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 3181 | /* There isn't always a NUL after the command, but it may need to be |
| 3182 | * there, thus copy up to the NUL and add a NUL. */ |
| 3183 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 3184 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3185 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3186 | |
| 3187 | if (ccline.xpc != NULL |
| 3188 | && ccline.xpc->xp_pattern != NULL |
| 3189 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 3190 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 3191 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 3192 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3193 | |
| 3194 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 3195 | * to point into the newly allocated memory. */ |
| 3196 | if (i >= 0 && i <= ccline.cmdlen) |
| 3197 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 3198 | } |
| 3199 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3200 | return OK; |
| 3201 | } |
| 3202 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3203 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 3204 | static char_u *arshape_buf = NULL; |
| 3205 | |
| 3206 | # if defined(EXITFREE) || defined(PROTO) |
| 3207 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3208 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3209 | { |
| 3210 | vim_free(arshape_buf); |
| 3211 | } |
| 3212 | # endif |
| 3213 | #endif |
| 3214 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3215 | /* |
| 3216 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 3217 | * when cmdline_star is TRUE. |
| 3218 | */ |
| 3219 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3220 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3221 | { |
| 3222 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3223 | int i; |
| 3224 | |
| 3225 | if (cmdline_star > 0) |
| 3226 | for (i = 0; i < len; ++i) |
| 3227 | { |
| 3228 | msg_putchar('*'); |
| 3229 | # ifdef FEAT_MBYTE |
| 3230 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3231 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3232 | # endif |
| 3233 | } |
| 3234 | else |
| 3235 | #endif |
| 3236 | #ifdef FEAT_ARABIC |
| 3237 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 3238 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3239 | static int buflen = 0; |
| 3240 | char_u *p; |
| 3241 | int j; |
| 3242 | int newlen = 0; |
| 3243 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3244 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3245 | int prev_c = 0; |
| 3246 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3247 | int u8c; |
| 3248 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3249 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | |
| 3251 | /* |
| 3252 | * Do arabic shaping into a temporary buffer. This is very |
| 3253 | * inefficient! |
| 3254 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3255 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3256 | { |
| 3257 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 3258 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3259 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3260 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3261 | arshape_buf = alloc(buflen); |
| 3262 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | return; /* out of memory */ |
| 3264 | } |
| 3265 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3266 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 3267 | { |
| 3268 | /* Prepend a space to draw the leading composing char on. */ |
| 3269 | arshape_buf[0] = ' '; |
| 3270 | newlen = 1; |
| 3271 | } |
| 3272 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | for (j = start; j < start + len; j += mb_l) |
| 3274 | { |
| 3275 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3276 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3277 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3278 | if (ARABIC_CHAR(u8c)) |
| 3279 | { |
| 3280 | /* Do Arabic shaping. */ |
| 3281 | if (cmdmsg_rl) |
| 3282 | { |
| 3283 | /* displaying from right to left */ |
| 3284 | pc = prev_c; |
| 3285 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3286 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | if (j + mb_l >= start + len) |
| 3288 | nc = NUL; |
| 3289 | else |
| 3290 | nc = utf_ptr2char(p + mb_l); |
| 3291 | } |
| 3292 | else |
| 3293 | { |
| 3294 | /* displaying from left to right */ |
| 3295 | if (j + mb_l >= start + len) |
| 3296 | pc = NUL; |
| 3297 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3298 | { |
| 3299 | int pcc[MAX_MCO]; |
| 3300 | |
| 3301 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3302 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3303 | pc1 = pcc[0]; |
| 3304 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3305 | nc = prev_c; |
| 3306 | } |
| 3307 | prev_c = u8c; |
| 3308 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3309 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3310 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3311 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3312 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3313 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3314 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 3315 | if (u8cc[1] != 0) |
| 3316 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3317 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | else |
| 3321 | { |
| 3322 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3323 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3324 | newlen += mb_l; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3328 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3329 | } |
| 3330 | else |
| 3331 | #endif |
| 3332 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3333 | } |
| 3334 | |
| 3335 | /* |
| 3336 | * Put a character on the command line. Shifts the following text to the |
| 3337 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3338 | * "c" must be printable (fit in one display cell)! |
| 3339 | */ |
| 3340 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3341 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | { |
| 3343 | if (cmd_silent) |
| 3344 | return; |
| 3345 | msg_no_more = TRUE; |
| 3346 | msg_putchar(c); |
| 3347 | if (shift) |
| 3348 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3349 | msg_no_more = FALSE; |
| 3350 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3351 | extra_char = c; |
| 3352 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * Undo a putcmdline(c, FALSE). |
| 3357 | */ |
| 3358 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3359 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | { |
| 3361 | if (cmd_silent) |
| 3362 | return; |
| 3363 | msg_no_more = TRUE; |
| 3364 | if (ccline.cmdlen == ccline.cmdpos) |
| 3365 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3366 | #ifdef FEAT_MBYTE |
| 3367 | else if (has_mbyte) |
| 3368 | draw_cmdline(ccline.cmdpos, |
| 3369 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3370 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3371 | else |
| 3372 | draw_cmdline(ccline.cmdpos, 1); |
| 3373 | msg_no_more = FALSE; |
| 3374 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3375 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | /* |
| 3379 | * Put the given string, of the given length, onto the command line. |
| 3380 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3381 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3382 | * part will be redrawn, otherwise it will not. If this function is called |
| 3383 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3384 | * called afterwards. |
| 3385 | */ |
| 3386 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3387 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | { |
| 3389 | int retval; |
| 3390 | int i; |
| 3391 | int m; |
| 3392 | int c; |
| 3393 | |
| 3394 | if (len < 0) |
| 3395 | len = (int)STRLEN(str); |
| 3396 | |
| 3397 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3398 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3399 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | else |
| 3401 | retval = OK; |
| 3402 | if (retval == OK) |
| 3403 | { |
| 3404 | if (!ccline.overstrike) |
| 3405 | { |
| 3406 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3407 | ccline.cmdbuff + ccline.cmdpos, |
| 3408 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3409 | ccline.cmdlen += len; |
| 3410 | } |
| 3411 | else |
| 3412 | { |
| 3413 | #ifdef FEAT_MBYTE |
| 3414 | if (has_mbyte) |
| 3415 | { |
| 3416 | /* Count nr of characters in the new string. */ |
| 3417 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3418 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | ++m; |
| 3420 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3421 | * characters. */ |
| 3422 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3423 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3424 | --m; |
| 3425 | if (i < ccline.cmdlen) |
| 3426 | { |
| 3427 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3428 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3429 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3430 | } |
| 3431 | else |
| 3432 | ccline.cmdlen = ccline.cmdpos + len; |
| 3433 | } |
| 3434 | else |
| 3435 | #endif |
| 3436 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3437 | ccline.cmdlen = ccline.cmdpos + len; |
| 3438 | } |
| 3439 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3440 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3441 | |
| 3442 | #ifdef FEAT_MBYTE |
| 3443 | if (enc_utf8) |
| 3444 | { |
| 3445 | /* When the inserted text starts with a composing character, |
| 3446 | * backup to the character before it. There could be two of them. |
| 3447 | */ |
| 3448 | i = 0; |
| 3449 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3450 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3451 | { |
| 3452 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3453 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3454 | ccline.cmdpos -= i; |
| 3455 | len += i; |
| 3456 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3457 | } |
| 3458 | # ifdef FEAT_ARABIC |
| 3459 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3460 | { |
| 3461 | /* Check the previous character for Arabic combining pair. */ |
| 3462 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3463 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3464 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3465 | + ccline.cmdpos - i), c)) |
| 3466 | { |
| 3467 | ccline.cmdpos -= i; |
| 3468 | len += i; |
| 3469 | } |
| 3470 | else |
| 3471 | i = 0; |
| 3472 | } |
| 3473 | # endif |
| 3474 | if (i != 0) |
| 3475 | { |
| 3476 | /* Also backup the cursor position. */ |
| 3477 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3478 | ccline.cmdspos -= i; |
| 3479 | msg_col -= i; |
| 3480 | if (msg_col < 0) |
| 3481 | { |
| 3482 | msg_col += Columns; |
| 3483 | --msg_row; |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | #endif |
| 3488 | |
| 3489 | if (redraw && !cmd_silent) |
| 3490 | { |
| 3491 | msg_no_more = TRUE; |
| 3492 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3493 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3494 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3495 | /* Avoid clearing the rest of the line too often. */ |
| 3496 | if (cmdline_row != i || ccline.overstrike) |
| 3497 | msg_clr_eos(); |
| 3498 | msg_no_more = FALSE; |
| 3499 | } |
| 3500 | #ifdef FEAT_FKMAP |
| 3501 | /* |
| 3502 | * If we are in Farsi command mode, the character input must be in |
| 3503 | * Insert mode. So do not advance the cmdpos. |
| 3504 | */ |
| 3505 | if (!cmd_fkmap) |
| 3506 | #endif |
| 3507 | { |
| 3508 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3509 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3511 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3512 | m = MAXCOL; |
| 3513 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | else |
| 3515 | m = MAXCOL; |
| 3516 | for (i = 0; i < len; ++i) |
| 3517 | { |
| 3518 | c = cmdline_charsize(ccline.cmdpos); |
| 3519 | #ifdef FEAT_MBYTE |
| 3520 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3521 | if (has_mbyte) |
| 3522 | correct_cmdspos(ccline.cmdpos, c); |
| 3523 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3524 | /* Stop cursor at the end of the screen, but do increment the |
| 3525 | * insert position, so that entering a very long command |
| 3526 | * works, even though you can't see it. */ |
| 3527 | if (ccline.cmdspos + c < m) |
| 3528 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3529 | #ifdef FEAT_MBYTE |
| 3530 | if (has_mbyte) |
| 3531 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3532 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3533 | if (c > len - i - 1) |
| 3534 | c = len - i - 1; |
| 3535 | ccline.cmdpos += c; |
| 3536 | i += c; |
| 3537 | } |
| 3538 | #endif |
| 3539 | ++ccline.cmdpos; |
| 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | if (redraw) |
| 3544 | msg_check(); |
| 3545 | return retval; |
| 3546 | } |
| 3547 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3548 | static struct cmdline_info prev_ccline; |
| 3549 | static int prev_ccline_used = FALSE; |
| 3550 | |
| 3551 | /* |
| 3552 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3553 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3554 | * available globally in prev_ccline. |
| 3555 | */ |
| 3556 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3557 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3558 | { |
| 3559 | if (!prev_ccline_used) |
| 3560 | { |
| 3561 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3562 | prev_ccline_used = TRUE; |
| 3563 | } |
| 3564 | *ccp = prev_ccline; |
| 3565 | prev_ccline = ccline; |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 3566 | ccline.cmdbuff = NULL; // signal that ccline is not in use |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3570 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3571 | */ |
| 3572 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3573 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3574 | { |
| 3575 | ccline = prev_ccline; |
| 3576 | prev_ccline = *ccp; |
| 3577 | } |
| 3578 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3579 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3580 | * Paste a yank register into the command line. |
| 3581 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3582 | * insert_reg() can't be used here, because special characters from the |
| 3583 | * register contents will be interpreted as commands. |
| 3584 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3585 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3586 | */ |
| 3587 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3588 | cmdline_paste( |
| 3589 | int regname, |
| 3590 | int literally, /* Insert text literally instead of "as typed" */ |
| 3591 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3592 | { |
| 3593 | long i; |
| 3594 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3595 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3596 | int allocated; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3597 | |
| 3598 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3599 | * the command line */ |
| 3600 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 3601 | && regname != Ctrl_A && regname != Ctrl_L |
| 3602 | && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3603 | return FAIL; |
| 3604 | |
| 3605 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3606 | * CTRL-C to break the loop. */ |
| 3607 | line_breakcheck(); |
| 3608 | if (got_int) |
| 3609 | return FAIL; |
| 3610 | |
| 3611 | #ifdef FEAT_CLIPBOARD |
| 3612 | regname = may_get_selection(regname); |
| 3613 | #endif |
| 3614 | |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 3615 | // Need to set "textlock" to avoid nasty things like going to another |
| 3616 | // buffer when evaluating an expression. |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3617 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3618 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3619 | --textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3620 | |
| 3621 | if (i) |
| 3622 | { |
| 3623 | /* Got the value of a special register in "arg". */ |
| 3624 | if (arg == NULL) |
| 3625 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3626 | |
| 3627 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3628 | * part of the word. */ |
| 3629 | p = arg; |
| 3630 | if (p_is && regname == Ctrl_W) |
| 3631 | { |
| 3632 | char_u *w; |
| 3633 | int len; |
| 3634 | |
| 3635 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3636 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3637 | { |
| 3638 | #ifdef FEAT_MBYTE |
| 3639 | if (has_mbyte) |
| 3640 | { |
| 3641 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3642 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3643 | break; |
| 3644 | w -= len; |
| 3645 | } |
| 3646 | else |
| 3647 | #endif |
| 3648 | { |
| 3649 | if (!vim_iswordc(w[-1])) |
| 3650 | break; |
| 3651 | --w; |
| 3652 | } |
| 3653 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3654 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3655 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3656 | p += len; |
| 3657 | } |
| 3658 | |
| 3659 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3660 | if (allocated) |
| 3661 | vim_free(arg); |
| 3662 | return OK; |
| 3663 | } |
| 3664 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3665 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | /* |
| 3669 | * Put a string on the command line. |
| 3670 | * When "literally" is TRUE, insert literally. |
| 3671 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3672 | * line. |
| 3673 | */ |
| 3674 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3675 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3676 | { |
| 3677 | int c, cv; |
| 3678 | |
| 3679 | if (literally) |
| 3680 | put_on_cmdline(s, -1, TRUE); |
| 3681 | else |
| 3682 | while (*s != NUL) |
| 3683 | { |
| 3684 | cv = *s; |
| 3685 | if (cv == Ctrl_V && s[1]) |
| 3686 | ++s; |
| 3687 | #ifdef FEAT_MBYTE |
| 3688 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3689 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3690 | else |
| 3691 | #endif |
| 3692 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3693 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3694 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3695 | #ifdef UNIX |
| 3696 | || c == intr_char |
| 3697 | #endif |
| 3698 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3699 | stuffcharReadbuff(Ctrl_V); |
| 3700 | stuffcharReadbuff(c); |
| 3701 | } |
| 3702 | } |
| 3703 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3704 | #ifdef FEAT_WILDMENU |
| 3705 | /* |
| 3706 | * Delete characters on the command line, from "from" to the current |
| 3707 | * position. |
| 3708 | */ |
| 3709 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3710 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3711 | { |
| 3712 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3713 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3714 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3715 | ccline.cmdpos = from; |
| 3716 | } |
| 3717 | #endif |
| 3718 | |
| 3719 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3720 | * This function is called when the screen size changes and with incremental |
| 3721 | * search and in other situations where the command line may have been |
| 3722 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3723 | */ |
| 3724 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3725 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3726 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3727 | redrawcmdline_ex(TRUE); |
| 3728 | } |
| 3729 | |
| 3730 | void |
| 3731 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3732 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3733 | if (cmd_silent) |
| 3734 | return; |
| 3735 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3736 | if (do_compute_cmdrow) |
| 3737 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3738 | redrawcmd(); |
| 3739 | cursorcmd(); |
| 3740 | } |
| 3741 | |
| 3742 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3743 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | { |
| 3745 | int i; |
| 3746 | |
| 3747 | if (cmd_silent) |
| 3748 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3749 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | msg_putchar(ccline.cmdfirstc); |
| 3751 | if (ccline.cmdprompt != NULL) |
| 3752 | { |
| 3753 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3754 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3755 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3756 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3757 | --ccline.cmdindent; |
| 3758 | } |
| 3759 | else |
| 3760 | for (i = ccline.cmdindent; i > 0; --i) |
| 3761 | msg_putchar(' '); |
| 3762 | } |
| 3763 | |
| 3764 | /* |
| 3765 | * Redraw what is currently on the command line. |
| 3766 | */ |
| 3767 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3768 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | { |
| 3770 | if (cmd_silent) |
| 3771 | return; |
| 3772 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3773 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3774 | if (ccline.cmdbuff == NULL) |
| 3775 | { |
| 3776 | windgoto(cmdline_row, 0); |
| 3777 | msg_clr_eos(); |
| 3778 | return; |
| 3779 | } |
| 3780 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3781 | msg_start(); |
| 3782 | redrawcmdprompt(); |
| 3783 | |
| 3784 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3785 | msg_no_more = TRUE; |
| 3786 | draw_cmdline(0, ccline.cmdlen); |
| 3787 | msg_clr_eos(); |
| 3788 | msg_no_more = FALSE; |
| 3789 | |
| 3790 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3791 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3792 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3793 | |
| 3794 | /* |
| 3795 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3796 | * in cmdline mode we can reset them now. |
| 3797 | */ |
| 3798 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3799 | |
| 3800 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3801 | * in cmdline mode */ |
| 3802 | skip_redraw = FALSE; |
| 3803 | } |
| 3804 | |
| 3805 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3806 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3808 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3809 | cmdline_row = Rows - 1; |
| 3810 | else |
| 3811 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3812 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3816 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3817 | { |
| 3818 | if (cmd_silent) |
| 3819 | return; |
| 3820 | |
| 3821 | #ifdef FEAT_RIGHTLEFT |
| 3822 | if (cmdmsg_rl) |
| 3823 | { |
| 3824 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3825 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3826 | if (msg_row <= 0) |
| 3827 | msg_row = Rows - 1; |
| 3828 | } |
| 3829 | else |
| 3830 | #endif |
| 3831 | { |
| 3832 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3833 | msg_col = ccline.cmdspos % (int)Columns; |
| 3834 | if (msg_row >= Rows) |
| 3835 | msg_row = Rows - 1; |
| 3836 | } |
| 3837 | |
| 3838 | windgoto(msg_row, msg_col); |
| 3839 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3840 | if (p_imst == IM_ON_THE_SPOT) |
| 3841 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3842 | #endif |
| 3843 | #ifdef MCH_CURSOR_SHAPE |
| 3844 | mch_update_cursor(); |
| 3845 | #endif |
| 3846 | } |
| 3847 | |
| 3848 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3849 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3850 | { |
| 3851 | msg_start(); |
| 3852 | #ifdef FEAT_RIGHTLEFT |
| 3853 | if (cmdmsg_rl) |
| 3854 | msg_col = Columns - 1; |
| 3855 | else |
| 3856 | #endif |
| 3857 | msg_col = 0; /* always start in column 0 */ |
| 3858 | if (clr) /* clear the bottom line(s) */ |
| 3859 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3860 | windgoto(cmdline_row, 0); |
| 3861 | } |
| 3862 | |
| 3863 | /* |
| 3864 | * Check the word in front of the cursor for an abbreviation. |
| 3865 | * Called when the non-id character "c" has been entered. |
| 3866 | * When an abbreviation is recognized it is removed from the text with |
| 3867 | * backspaces and the replacement string is inserted, followed by "c". |
| 3868 | */ |
| 3869 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3870 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | { |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3872 | int spos = 0; |
| 3873 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3874 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3875 | return FALSE; |
| 3876 | |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3877 | /* Do not consider '<,'> be part of the mapping, skip leading whitespace. |
| 3878 | * Actually accepts any mark. */ |
| 3879 | while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen) |
| 3880 | spos++; |
| 3881 | if (ccline.cmdlen - spos > 5 |
| 3882 | && ccline.cmdbuff[spos] == '\'' |
| 3883 | && ccline.cmdbuff[spos + 2] == ',' |
| 3884 | && ccline.cmdbuff[spos + 3] == '\'') |
| 3885 | spos += 5; |
| 3886 | else |
| 3887 | /* check abbreviation from the beginning of the commandline */ |
| 3888 | spos = 0; |
| 3889 | |
| 3890 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3893 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3894 | static int |
| 3895 | #ifdef __BORLANDC__ |
| 3896 | _RTLENTRYF |
| 3897 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3898 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3899 | { |
| 3900 | char_u *p1 = *(char_u **)s1; |
| 3901 | char_u *p2 = *(char_u **)s2; |
| 3902 | |
| 3903 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3904 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3905 | return STRCMP(p1, p2); |
| 3906 | } |
| 3907 | #endif |
| 3908 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | /* |
| 3910 | * Return FAIL if this is not an appropriate context in which to do |
| 3911 | * completion of anything, return OK if it is (even if there are no matches). |
| 3912 | * For the caller, this means that the character is just passed through like a |
| 3913 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3914 | */ |
| 3915 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3916 | nextwild( |
| 3917 | expand_T *xp, |
| 3918 | int type, |
| 3919 | int options, /* extra options for ExpandOne() */ |
| 3920 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3921 | { |
| 3922 | int i, j; |
| 3923 | char_u *p1; |
| 3924 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3925 | int difflen; |
| 3926 | int v; |
| 3927 | |
| 3928 | if (xp->xp_numfiles == -1) |
| 3929 | { |
| 3930 | set_expand_context(xp); |
| 3931 | cmd_showtail = expand_showtail(xp); |
| 3932 | } |
| 3933 | |
| 3934 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3935 | { |
| 3936 | beep_flush(); |
| 3937 | return OK; /* Something illegal on command line */ |
| 3938 | } |
| 3939 | if (xp->xp_context == EXPAND_NOTHING) |
| 3940 | { |
| 3941 | /* Caller can use the character as a normal char instead */ |
| 3942 | return FAIL; |
| 3943 | } |
| 3944 | |
| 3945 | MSG_PUTS("..."); /* show that we are busy */ |
| 3946 | out_flush(); |
| 3947 | |
| 3948 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3949 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3950 | |
| 3951 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3952 | { |
| 3953 | /* |
| 3954 | * Get next/previous match for a previous expanded pattern. |
| 3955 | */ |
| 3956 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3957 | } |
| 3958 | else |
| 3959 | { |
| 3960 | /* |
| 3961 | * Translate string into pattern and expand it. |
| 3962 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3963 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3964 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3965 | p2 = NULL; |
| 3966 | else |
| 3967 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3968 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3969 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3970 | if (escape) |
| 3971 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3972 | |
| 3973 | if (p_wic) |
| 3974 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3975 | p2 = ExpandOne(xp, p1, |
| 3976 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3977 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3978 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3979 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3980 | if (p2 != NULL && type == WILD_LONGEST) |
| 3981 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3982 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | if (ccline.cmdbuff[i + j] == '*' |
| 3984 | || ccline.cmdbuff[i + j] == '?') |
| 3985 | break; |
| 3986 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3987 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3988 | } |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | if (p2 != NULL && !got_int) |
| 3993 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3994 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3995 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3997 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3998 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3999 | } |
| 4000 | else |
| 4001 | v = OK; |
| 4002 | if (v == OK) |
| 4003 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4004 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 4005 | &ccline.cmdbuff[ccline.cmdpos], |
| 4006 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 4007 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | ccline.cmdlen += difflen; |
| 4009 | ccline.cmdpos += difflen; |
| 4010 | } |
| 4011 | } |
| 4012 | vim_free(p2); |
| 4013 | |
| 4014 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 4015 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | |
| 4017 | /* When expanding a ":map" command and no matches are found, assume that |
| 4018 | * the key is supposed to be inserted literally */ |
| 4019 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 4020 | return FAIL; |
| 4021 | |
| 4022 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 4023 | beep_flush(); |
| 4024 | else if (xp->xp_numfiles == 1) |
| 4025 | /* free expanded pattern */ |
| 4026 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 4027 | |
| 4028 | return OK; |
| 4029 | } |
| 4030 | |
| 4031 | /* |
| 4032 | * Do wildcard expansion on the string 'str'. |
| 4033 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 4034 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4035 | * Return NULL for failure. |
| 4036 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4037 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 4038 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 4039 | * WILD_PREV "orig" should be NULL. |
| 4040 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 4041 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 4042 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4043 | * |
| 4044 | * mode = WILD_FREE: just free previously expanded matches |
| 4045 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 4046 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 4047 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 4048 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 4049 | * mode = WILD_ALL: return all matches concatenated |
| 4050 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4051 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4052 | * |
| 4053 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 4054 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 4055 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 4056 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 4057 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 4058 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 4059 | * options = WILD_SILENT: don't print warning messages |
| 4060 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4061 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4062 | * |
| 4063 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 4064 | */ |
| 4065 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4066 | ExpandOne( |
| 4067 | expand_T *xp, |
| 4068 | char_u *str, |
| 4069 | char_u *orig, /* allocated copy of original of expanded string */ |
| 4070 | int options, |
| 4071 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4072 | { |
| 4073 | char_u *ss = NULL; |
| 4074 | static int findex; |
| 4075 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4076 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4077 | int i; |
| 4078 | long_u len; |
| 4079 | int non_suf_match; /* number without matching suffix */ |
| 4080 | |
| 4081 | /* |
| 4082 | * first handle the case of using an old match |
| 4083 | */ |
| 4084 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 4085 | { |
| 4086 | if (xp->xp_numfiles > 0) |
| 4087 | { |
| 4088 | if (mode == WILD_PREV) |
| 4089 | { |
| 4090 | if (findex == -1) |
| 4091 | findex = xp->xp_numfiles; |
| 4092 | --findex; |
| 4093 | } |
| 4094 | else /* mode == WILD_NEXT */ |
| 4095 | ++findex; |
| 4096 | |
| 4097 | /* |
| 4098 | * When wrapping around, return the original string, set findex to |
| 4099 | * -1. |
| 4100 | */ |
| 4101 | if (findex < 0) |
| 4102 | { |
| 4103 | if (orig_save == NULL) |
| 4104 | findex = xp->xp_numfiles - 1; |
| 4105 | else |
| 4106 | findex = -1; |
| 4107 | } |
| 4108 | if (findex >= xp->xp_numfiles) |
| 4109 | { |
| 4110 | if (orig_save == NULL) |
| 4111 | findex = 0; |
| 4112 | else |
| 4113 | findex = -1; |
| 4114 | } |
| 4115 | #ifdef FEAT_WILDMENU |
| 4116 | if (p_wmnu) |
| 4117 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 4118 | findex, cmd_showtail); |
| 4119 | #endif |
| 4120 | if (findex == -1) |
| 4121 | return vim_strsave(orig_save); |
| 4122 | return vim_strsave(xp->xp_files[findex]); |
| 4123 | } |
| 4124 | else |
| 4125 | return NULL; |
| 4126 | } |
| 4127 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4128 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 4130 | { |
| 4131 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4132 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4133 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4134 | } |
| 4135 | findex = 0; |
| 4136 | |
| 4137 | if (mode == WILD_FREE) /* only release file name */ |
| 4138 | return NULL; |
| 4139 | |
| 4140 | if (xp->xp_numfiles == -1) |
| 4141 | { |
| 4142 | vim_free(orig_save); |
| 4143 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4144 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4145 | |
| 4146 | /* |
| 4147 | * Do the expansion. |
| 4148 | */ |
| 4149 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 4150 | options) == FAIL) |
| 4151 | { |
| 4152 | #ifdef FNAME_ILLEGAL |
| 4153 | /* Illegal file name has been silently skipped. But when there |
| 4154 | * are wildcards, the real problem is that there was no match, |
| 4155 | * causing the pattern to be added, which has illegal characters. |
| 4156 | */ |
| 4157 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 4158 | EMSG2(_(e_nomatch2), str); |
| 4159 | #endif |
| 4160 | } |
| 4161 | else if (xp->xp_numfiles == 0) |
| 4162 | { |
| 4163 | if (!(options & WILD_SILENT)) |
| 4164 | EMSG2(_(e_nomatch2), str); |
| 4165 | } |
| 4166 | else |
| 4167 | { |
| 4168 | /* Escape the matches for use on the command line. */ |
| 4169 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 4170 | |
| 4171 | /* |
| 4172 | * Check for matching suffixes in file names. |
| 4173 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4174 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 4175 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4176 | { |
| 4177 | if (xp->xp_numfiles) |
| 4178 | non_suf_match = xp->xp_numfiles; |
| 4179 | else |
| 4180 | non_suf_match = 1; |
| 4181 | if ((xp->xp_context == EXPAND_FILES |
| 4182 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4183 | && xp->xp_numfiles > 1) |
| 4184 | { |
| 4185 | /* |
| 4186 | * More than one match; check suffix. |
| 4187 | * The files will have been sorted on matching suffix in |
| 4188 | * expand_wildcards, only need to check the first two. |
| 4189 | */ |
| 4190 | non_suf_match = 0; |
| 4191 | for (i = 0; i < 2; ++i) |
| 4192 | if (match_suffix(xp->xp_files[i])) |
| 4193 | ++non_suf_match; |
| 4194 | } |
| 4195 | if (non_suf_match != 1) |
| 4196 | { |
| 4197 | /* Can we ever get here unless it's while expanding |
| 4198 | * interactively? If not, we can get rid of this all |
| 4199 | * together. Don't really want to wait for this message |
| 4200 | * (and possibly have to hit return to continue!). |
| 4201 | */ |
| 4202 | if (!(options & WILD_SILENT)) |
| 4203 | EMSG(_(e_toomany)); |
| 4204 | else if (!(options & WILD_NO_BEEP)) |
| 4205 | beep_flush(); |
| 4206 | } |
| 4207 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 4208 | ss = vim_strsave(xp->xp_files[0]); |
| 4209 | } |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | /* Find longest common part */ |
| 4214 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 4215 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4216 | int mb_len = 1; |
| 4217 | int c0, ci; |
| 4218 | |
| 4219 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4220 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4221 | #ifdef FEAT_MBYTE |
| 4222 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4224 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 4225 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 4226 | } |
| 4227 | else |
| 4228 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 4229 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4230 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 4231 | { |
| 4232 | #ifdef FEAT_MBYTE |
| 4233 | if (has_mbyte) |
| 4234 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 4235 | else |
| 4236 | #endif |
| 4237 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4238 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4239 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4240 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4241 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4242 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4243 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4244 | break; |
| 4245 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4246 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4247 | break; |
| 4248 | } |
| 4249 | if (i < xp->xp_numfiles) |
| 4250 | { |
| 4251 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 4252 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4253 | break; |
| 4254 | } |
| 4255 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | ss = alloc((unsigned)len + 1); |
| 4258 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4259 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4260 | findex = -1; /* next p_wc gets first one */ |
| 4261 | } |
| 4262 | |
| 4263 | /* Concatenate all matching names */ |
| 4264 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 4265 | { |
| 4266 | len = 0; |
| 4267 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4268 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 4269 | ss = lalloc(len, TRUE); |
| 4270 | if (ss != NULL) |
| 4271 | { |
| 4272 | *ss = NUL; |
| 4273 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4274 | { |
| 4275 | STRCAT(ss, xp->xp_files[i]); |
| 4276 | if (i != xp->xp_numfiles - 1) |
| 4277 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 4278 | } |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 4283 | ExpandCleanup(xp); |
| 4284 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4285 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4286 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4287 | vim_free(orig); |
| 4288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4289 | return ss; |
| 4290 | } |
| 4291 | |
| 4292 | /* |
| 4293 | * Prepare an expand structure for use. |
| 4294 | */ |
| 4295 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4296 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4297 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 4298 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4299 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4301 | #ifndef BACKSLASH_IN_FILENAME |
| 4302 | xp->xp_shell = FALSE; |
| 4303 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4304 | xp->xp_numfiles = -1; |
| 4305 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4306 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4307 | xp->xp_arg = NULL; |
| 4308 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4309 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
| 4312 | /* |
| 4313 | * Cleanup an expand structure after use. |
| 4314 | */ |
| 4315 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4316 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4317 | { |
| 4318 | if (xp->xp_numfiles >= 0) |
| 4319 | { |
| 4320 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4321 | xp->xp_numfiles = -1; |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4326 | ExpandEscape( |
| 4327 | expand_T *xp, |
| 4328 | char_u *str, |
| 4329 | int numfiles, |
| 4330 | char_u **files, |
| 4331 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4332 | { |
| 4333 | int i; |
| 4334 | char_u *p; |
| 4335 | |
| 4336 | /* |
| 4337 | * May change home directory back to "~" |
| 4338 | */ |
| 4339 | if (options & WILD_HOME_REPLACE) |
| 4340 | tilde_replace(str, numfiles, files); |
| 4341 | |
| 4342 | if (options & WILD_ESCAPE) |
| 4343 | { |
| 4344 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4345 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4346 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4347 | || xp->xp_context == EXPAND_BUFFERS |
| 4348 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4349 | { |
| 4350 | /* |
| 4351 | * Insert a backslash into a file name before a space, \, %, # |
| 4352 | * and wildmatch characters, except '~'. |
| 4353 | */ |
| 4354 | for (i = 0; i < numfiles; ++i) |
| 4355 | { |
| 4356 | /* for ":set path=" we need to escape spaces twice */ |
| 4357 | if (xp->xp_backslash == XP_BS_THREE) |
| 4358 | { |
| 4359 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4360 | if (p != NULL) |
| 4361 | { |
| 4362 | vim_free(files[i]); |
| 4363 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4364 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4365 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4366 | if (p != NULL) |
| 4367 | { |
| 4368 | vim_free(files[i]); |
| 4369 | files[i] = p; |
| 4370 | } |
| 4371 | #endif |
| 4372 | } |
| 4373 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4374 | #ifdef BACKSLASH_IN_FILENAME |
| 4375 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4376 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4377 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4378 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4379 | if (p != NULL) |
| 4380 | { |
| 4381 | vim_free(files[i]); |
| 4382 | files[i] = p; |
| 4383 | } |
| 4384 | |
| 4385 | /* If 'str' starts with "\~", replace "~" at start of |
| 4386 | * files[i] with "\~". */ |
| 4387 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4388 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | } |
| 4390 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4391 | |
| 4392 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4393 | * could be seen as "+cmd". */ |
| 4394 | if (*files[0] == '+') |
| 4395 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | } |
| 4397 | else if (xp->xp_context == EXPAND_TAGS) |
| 4398 | { |
| 4399 | /* |
| 4400 | * Insert a backslash before characters in a tag name that |
| 4401 | * would terminate the ":tag" command. |
| 4402 | */ |
| 4403 | for (i = 0; i < numfiles; ++i) |
| 4404 | { |
| 4405 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4406 | if (p != NULL) |
| 4407 | { |
| 4408 | vim_free(files[i]); |
| 4409 | files[i] = p; |
| 4410 | } |
| 4411 | } |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4417 | * Escape special characters in "fname" for when used as a file name argument |
| 4418 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4419 | * Returns the result in allocated memory. |
| 4420 | */ |
| 4421 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4422 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4423 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4424 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4425 | #ifdef BACKSLASH_IN_FILENAME |
| 4426 | char_u buf[20]; |
| 4427 | int j = 0; |
| 4428 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4429 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4430 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4431 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4432 | buf[j++] = *p; |
| 4433 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4434 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4435 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4436 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4437 | if (shell && csh_like_shell() && p != NULL) |
| 4438 | { |
| 4439 | char_u *s; |
| 4440 | |
| 4441 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4442 | * One is taken by Vim, one by the shell. */ |
| 4443 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4444 | vim_free(p); |
| 4445 | p = s; |
| 4446 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4447 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4448 | |
| 4449 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4450 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4451 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4452 | escape_fname(&p); |
| 4453 | |
| 4454 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4458 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4459 | */ |
| 4460 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4461 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4462 | { |
| 4463 | char_u *p; |
| 4464 | |
| 4465 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4466 | if (p != NULL) |
| 4467 | { |
| 4468 | p[0] = '\\'; |
| 4469 | STRCPY(p + 1, *pp); |
| 4470 | vim_free(*pp); |
| 4471 | *pp = p; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | * For each file name in files[num_files]: |
| 4477 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4478 | */ |
| 4479 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4480 | tilde_replace( |
| 4481 | char_u *orig_pat, |
| 4482 | int num_files, |
| 4483 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4484 | { |
| 4485 | int i; |
| 4486 | char_u *p; |
| 4487 | |
| 4488 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4489 | { |
| 4490 | for (i = 0; i < num_files; ++i) |
| 4491 | { |
| 4492 | p = home_replace_save(NULL, files[i]); |
| 4493 | if (p != NULL) |
| 4494 | { |
| 4495 | vim_free(files[i]); |
| 4496 | files[i] = p; |
| 4497 | } |
| 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | |
| 4502 | /* |
| 4503 | * Show all matches for completion on the command line. |
| 4504 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4505 | * be inserted like a normal character. |
| 4506 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4507 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4508 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4509 | { |
| 4510 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4511 | int num_files; |
| 4512 | char_u **files_found; |
| 4513 | int i, j, k; |
| 4514 | int maxlen; |
| 4515 | int lines; |
| 4516 | int columns; |
| 4517 | char_u *p; |
| 4518 | int lastlen; |
| 4519 | int attr; |
| 4520 | int showtail; |
| 4521 | |
| 4522 | if (xp->xp_numfiles == -1) |
| 4523 | { |
| 4524 | set_expand_context(xp); |
| 4525 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4526 | &num_files, &files_found); |
| 4527 | showtail = expand_showtail(xp); |
| 4528 | if (i != EXPAND_OK) |
| 4529 | return i; |
| 4530 | |
| 4531 | } |
| 4532 | else |
| 4533 | { |
| 4534 | num_files = xp->xp_numfiles; |
| 4535 | files_found = xp->xp_files; |
| 4536 | showtail = cmd_showtail; |
| 4537 | } |
| 4538 | |
| 4539 | #ifdef FEAT_WILDMENU |
| 4540 | if (!wildmenu) |
| 4541 | { |
| 4542 | #endif |
| 4543 | msg_didany = FALSE; /* lines_left will be set */ |
| 4544 | msg_start(); /* prepare for paging */ |
| 4545 | msg_putchar('\n'); |
| 4546 | out_flush(); |
| 4547 | cmdline_row = msg_row; |
| 4548 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4549 | msg_start(); /* prepare for paging */ |
| 4550 | #ifdef FEAT_WILDMENU |
| 4551 | } |
| 4552 | #endif |
| 4553 | |
| 4554 | if (got_int) |
| 4555 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4556 | #ifdef FEAT_WILDMENU |
| 4557 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4558 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | #endif |
| 4560 | else |
| 4561 | { |
| 4562 | /* find the length of the longest file name */ |
| 4563 | maxlen = 0; |
| 4564 | for (i = 0; i < num_files; ++i) |
| 4565 | { |
| 4566 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4567 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4569 | { |
| 4570 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4571 | j = vim_strsize(NameBuff); |
| 4572 | } |
| 4573 | else |
| 4574 | j = vim_strsize(L_SHOWFILE(i)); |
| 4575 | if (j > maxlen) |
| 4576 | maxlen = j; |
| 4577 | } |
| 4578 | |
| 4579 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4580 | lines = num_files; |
| 4581 | else |
| 4582 | { |
| 4583 | /* compute the number of columns and lines for the listing */ |
| 4584 | maxlen += 2; /* two spaces between file names */ |
| 4585 | columns = ((int)Columns + 2) / maxlen; |
| 4586 | if (columns < 1) |
| 4587 | columns = 1; |
| 4588 | lines = (num_files + columns - 1) / columns; |
| 4589 | } |
| 4590 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4591 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4592 | |
| 4593 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4594 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4595 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4596 | msg_clr_eos(); |
| 4597 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4598 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | /* list the files line by line */ |
| 4602 | for (i = 0; i < lines; ++i) |
| 4603 | { |
| 4604 | lastlen = 999; |
| 4605 | for (k = i; k < num_files; k += lines) |
| 4606 | { |
| 4607 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4608 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4609 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4610 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4611 | msg_advance(maxlen + 1); |
| 4612 | msg_puts(p); |
| 4613 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4614 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4615 | break; |
| 4616 | } |
| 4617 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4618 | msg_putchar(' '); |
| 4619 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4620 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4621 | || xp->xp_context == EXPAND_BUFFERS) |
| 4622 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4623 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4624 | if (xp->xp_numfiles != -1) |
| 4625 | { |
| 4626 | char_u *halved_slash; |
| 4627 | char_u *exp_path; |
| 4628 | |
| 4629 | /* Expansion was done before and special characters |
| 4630 | * were escaped, need to halve backslashes. Also |
| 4631 | * $HOME has been replaced with ~/. */ |
| 4632 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4633 | halved_slash = backslash_halve_save( |
| 4634 | exp_path != NULL ? exp_path : files_found[k]); |
| 4635 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4636 | : files_found[k]); |
| 4637 | vim_free(exp_path); |
| 4638 | vim_free(halved_slash); |
| 4639 | } |
| 4640 | else |
| 4641 | /* Expansion was done here, file names are literal. */ |
| 4642 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4643 | if (showtail) |
| 4644 | p = L_SHOWFILE(k); |
| 4645 | else |
| 4646 | { |
| 4647 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4648 | TRUE); |
| 4649 | p = NameBuff; |
| 4650 | } |
| 4651 | } |
| 4652 | else |
| 4653 | { |
| 4654 | j = FALSE; |
| 4655 | p = L_SHOWFILE(k); |
| 4656 | } |
| 4657 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4658 | } |
| 4659 | if (msg_col > 0) /* when not wrapped around */ |
| 4660 | { |
| 4661 | msg_clr_eos(); |
| 4662 | msg_putchar('\n'); |
| 4663 | } |
| 4664 | out_flush(); /* show one line at a time */ |
| 4665 | if (got_int) |
| 4666 | { |
| 4667 | got_int = FALSE; |
| 4668 | break; |
| 4669 | } |
| 4670 | } |
| 4671 | |
| 4672 | /* |
| 4673 | * we redraw the command below the lines that we have just listed |
| 4674 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4675 | */ |
| 4676 | cmdline_row = msg_row; /* will put it back later */ |
| 4677 | } |
| 4678 | |
| 4679 | if (xp->xp_numfiles == -1) |
| 4680 | FreeWild(num_files, files_found); |
| 4681 | |
| 4682 | return EXPAND_OK; |
| 4683 | } |
| 4684 | |
| 4685 | /* |
| 4686 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4687 | * Find tail of file name path, but ignore trailing "/". |
| 4688 | */ |
| 4689 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4690 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4691 | { |
| 4692 | char_u *p; |
| 4693 | char_u *t = s; |
| 4694 | int had_sep = FALSE; |
| 4695 | |
| 4696 | for (p = s; *p != NUL; ) |
| 4697 | { |
| 4698 | if (vim_ispathsep(*p) |
| 4699 | #ifdef BACKSLASH_IN_FILENAME |
| 4700 | && !rem_backslash(p) |
| 4701 | #endif |
| 4702 | ) |
| 4703 | had_sep = TRUE; |
| 4704 | else if (had_sep) |
| 4705 | { |
| 4706 | t = p; |
| 4707 | had_sep = FALSE; |
| 4708 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4709 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4710 | } |
| 4711 | return t; |
| 4712 | } |
| 4713 | |
| 4714 | /* |
| 4715 | * Return TRUE if we only need to show the tail of completion matches. |
| 4716 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4717 | * returned. |
| 4718 | */ |
| 4719 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4720 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | { |
| 4722 | char_u *s; |
| 4723 | char_u *end; |
| 4724 | |
| 4725 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4726 | if (xp->xp_context != EXPAND_FILES |
| 4727 | && xp->xp_context != EXPAND_SHELLCMD |
| 4728 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | return FALSE; |
| 4730 | |
| 4731 | end = gettail(xp->xp_pattern); |
| 4732 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4733 | return FALSE; |
| 4734 | |
| 4735 | for (s = xp->xp_pattern; s < end; s++) |
| 4736 | { |
| 4737 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4738 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4739 | if (rem_backslash(s)) |
| 4740 | ++s; |
| 4741 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4742 | return FALSE; |
| 4743 | } |
| 4744 | return TRUE; |
| 4745 | } |
| 4746 | |
| 4747 | /* |
| 4748 | * Prepare a string for expansion. |
| 4749 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4750 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4751 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4752 | * the name into allocated memory and prepend "^". |
| 4753 | */ |
| 4754 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4755 | addstar( |
| 4756 | char_u *fname, |
| 4757 | int len, |
| 4758 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | { |
| 4760 | char_u *retval; |
| 4761 | int i, j; |
| 4762 | int new_len; |
| 4763 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4764 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4765 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4766 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4767 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4768 | && context != EXPAND_SHELLCMD |
| 4769 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4770 | { |
| 4771 | /* |
| 4772 | * Matching will be done internally (on something other than files). |
| 4773 | * So we convert the file-matching-type wildcards into our kind for |
| 4774 | * use with vim_regcomp(). First work out how long it will be: |
| 4775 | */ |
| 4776 | |
| 4777 | /* For help tags the translation is done in find_help_tags(). |
| 4778 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4779 | if (context == EXPAND_HELP |
| 4780 | || context == EXPAND_COLORS |
| 4781 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4782 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4783 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4784 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4785 | || ((context == EXPAND_TAGS_LISTFILES |
| 4786 | || context == EXPAND_TAGS) |
| 4787 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4788 | retval = vim_strnsave(fname, len); |
| 4789 | else |
| 4790 | { |
| 4791 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4792 | for (i = 0; i < len; i++) |
| 4793 | { |
| 4794 | if (fname[i] == '*' || fname[i] == '~') |
| 4795 | new_len++; /* '*' needs to be replaced by ".*" |
| 4796 | '~' needs to be replaced by "\~" */ |
| 4797 | |
| 4798 | /* Buffer names are like file names. "." should be literal */ |
| 4799 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4800 | new_len++; /* "." becomes "\." */ |
| 4801 | |
| 4802 | /* Custom expansion takes care of special things, match |
| 4803 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4804 | if ((context == EXPAND_USER_DEFINED |
| 4805 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4806 | new_len++; /* '\' becomes "\\" */ |
| 4807 | } |
| 4808 | retval = alloc(new_len); |
| 4809 | if (retval != NULL) |
| 4810 | { |
| 4811 | retval[0] = '^'; |
| 4812 | j = 1; |
| 4813 | for (i = 0; i < len; i++, j++) |
| 4814 | { |
| 4815 | /* Skip backslash. But why? At least keep it for custom |
| 4816 | * expansion. */ |
| 4817 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4818 | && context != EXPAND_USER_LIST |
| 4819 | && fname[i] == '\\' |
| 4820 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | break; |
| 4822 | |
| 4823 | switch (fname[i]) |
| 4824 | { |
| 4825 | case '*': retval[j++] = '.'; |
| 4826 | break; |
| 4827 | case '~': retval[j++] = '\\'; |
| 4828 | break; |
| 4829 | case '?': retval[j] = '.'; |
| 4830 | continue; |
| 4831 | case '.': if (context == EXPAND_BUFFERS) |
| 4832 | retval[j++] = '\\'; |
| 4833 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4834 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4835 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4836 | retval[j++] = '\\'; |
| 4837 | break; |
| 4838 | } |
| 4839 | retval[j] = fname[i]; |
| 4840 | } |
| 4841 | retval[j] = NUL; |
| 4842 | } |
| 4843 | } |
| 4844 | } |
| 4845 | else |
| 4846 | { |
| 4847 | retval = alloc(len + 4); |
| 4848 | if (retval != NULL) |
| 4849 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4850 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4851 | |
| 4852 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4853 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4854 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4855 | * ~ would be at the start of the file name, but not the tail. |
| 4856 | * $ could be anywhere in the tail. |
| 4857 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4858 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4859 | */ |
| 4860 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4861 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4862 | #ifndef BACKSLASH_IN_FILENAME |
| 4863 | for (i = len - 2; i >= 0; --i) |
| 4864 | { |
| 4865 | if (retval[i] != '\\') |
| 4866 | break; |
| 4867 | ends_in_star = !ends_in_star; |
| 4868 | } |
| 4869 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4870 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4871 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4872 | && vim_strchr(tail, '$') == NULL |
| 4873 | && vim_strchr(retval, '`') == NULL) |
| 4874 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4875 | else if (len > 0 && retval[len - 1] == '$') |
| 4876 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4877 | retval[len] = NUL; |
| 4878 | } |
| 4879 | } |
| 4880 | return retval; |
| 4881 | } |
| 4882 | |
| 4883 | /* |
| 4884 | * Must parse the command line so far to work out what context we are in. |
| 4885 | * Completion can then be done based on that context. |
| 4886 | * This routine sets the variables: |
| 4887 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4888 | * the command line (ends at the cursor). |
| 4889 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4890 | * |
| 4891 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4892 | * the command line, like an unknown command. Caller |
| 4893 | * should beep. |
| 4894 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4895 | * a normal char, rather than for completion. eg |
| 4896 | * :s/^I/ |
| 4897 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4898 | * it. |
| 4899 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4900 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4901 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4902 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4903 | * when we know only directories are of interest. eg |
| 4904 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4905 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4906 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4907 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4908 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4909 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4910 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4911 | * EXPAND_EVENTS Complete event names |
| 4912 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4913 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4914 | * EXPAND_AUGROUP Complete autocommand group names |
| 4915 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4916 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4917 | * eg :unmap a^I , :cunab x^I |
| 4918 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4919 | * eg :call sub^I |
| 4920 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4921 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4922 | * names in expressions, eg :while s^I |
| 4923 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4924 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4925 | */ |
| 4926 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4927 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4928 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4929 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4930 | if (ccline.cmdfirstc != ':' |
| 4931 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4932 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4933 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4934 | #endif |
| 4935 | ) |
| 4936 | { |
| 4937 | xp->xp_context = EXPAND_NOTHING; |
| 4938 | return; |
| 4939 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4940 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4944 | set_cmd_context( |
| 4945 | expand_T *xp, |
| 4946 | char_u *str, /* start of command line */ |
| 4947 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4948 | int col, /* position of cursor */ |
| 4949 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | { |
| 4951 | int old_char = NUL; |
| 4952 | char_u *nextcomm; |
| 4953 | |
| 4954 | /* |
| 4955 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4956 | * written before. |
| 4957 | */ |
| 4958 | if (col < len) |
| 4959 | old_char = str[col]; |
| 4960 | str[col] = NUL; |
| 4961 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4962 | |
| 4963 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4964 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4965 | { |
| 4966 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4967 | /* pass CMD_SIZE because there is no real command */ |
| 4968 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4969 | # endif |
| 4970 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4971 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4972 | { |
| 4973 | xp->xp_context = ccline.xp_context; |
| 4974 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4975 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4976 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4977 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4978 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4979 | else |
| 4980 | #endif |
| 4981 | while (nextcomm != NULL) |
| 4982 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4983 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4984 | /* Store the string here so that call_user_expand_func() can get to them |
| 4985 | * easily. */ |
| 4986 | xp->xp_line = str; |
| 4987 | xp->xp_col = col; |
| 4988 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | str[col] = old_char; |
| 4990 | } |
| 4991 | |
| 4992 | /* |
| 4993 | * Expand the command line "str" from context "xp". |
| 4994 | * "xp" must have been set by set_cmd_context(). |
| 4995 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4996 | * starts. |
| 4997 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4998 | * cursor. |
| 4999 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 5000 | * key that triggered expansion literally. |
| 5001 | * Returns EXPAND_OK otherwise. |
| 5002 | */ |
| 5003 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5004 | expand_cmdline( |
| 5005 | expand_T *xp, |
| 5006 | char_u *str, /* start of command line */ |
| 5007 | int col, /* position of cursor */ |
| 5008 | int *matchcount, /* return: nr of matches */ |
| 5009 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | { |
| 5011 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5012 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5013 | |
| 5014 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 5015 | { |
| 5016 | beep_flush(); |
| 5017 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 5018 | } |
| 5019 | if (xp->xp_context == EXPAND_NOTHING) |
| 5020 | { |
| 5021 | /* Caller can use the character as a normal char instead */ |
| 5022 | return EXPAND_NOTHING; |
| 5023 | } |
| 5024 | |
| 5025 | /* 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] | 5026 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 5027 | 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] | 5028 | if (file_str == NULL) |
| 5029 | return EXPAND_UNSUCCESSFUL; |
| 5030 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5031 | if (p_wic) |
| 5032 | options += WILD_ICASE; |
| 5033 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5034 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5035 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | { |
| 5037 | *matchcount = 0; |
| 5038 | *matches = NULL; |
| 5039 | } |
| 5040 | vim_free(file_str); |
| 5041 | |
| 5042 | return EXPAND_OK; |
| 5043 | } |
| 5044 | |
| 5045 | #ifdef FEAT_MULTI_LANG |
| 5046 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5047 | * Cleanup matches for help tags: |
| 5048 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 5049 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5051 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5052 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5053 | { |
| 5054 | int i, j; |
| 5055 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5056 | char_u buf[4]; |
| 5057 | char_u *p = buf; |
| 5058 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5059 | 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] | 5060 | { |
| 5061 | *p++ = '@'; |
| 5062 | *p++ = p_hlg[0]; |
| 5063 | *p++ = p_hlg[1]; |
| 5064 | } |
| 5065 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5066 | |
| 5067 | for (i = 0; i < num_file; ++i) |
| 5068 | { |
| 5069 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5070 | if (len <= 0) |
| 5071 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5072 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5073 | { |
| 5074 | /* Sorting on priority means the same item in another language may |
| 5075 | * be anywhere. Search all items for a match up to the "@en". */ |
| 5076 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5077 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 5078 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5079 | break; |
| 5080 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5081 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5082 | file[i][len] = NUL; |
| 5083 | } |
| 5084 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5085 | |
| 5086 | if (*buf != NUL) |
| 5087 | for (i = 0; i < num_file; ++i) |
| 5088 | { |
| 5089 | len = (int)STRLEN(file[i]) - 3; |
| 5090 | if (len <= 0) |
| 5091 | continue; |
| 5092 | if (STRCMP(file[i] + len, buf) == 0) |
| 5093 | { |
| 5094 | /* remove the default language */ |
| 5095 | file[i][len] = NUL; |
| 5096 | } |
| 5097 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5098 | } |
| 5099 | #endif |
| 5100 | |
| 5101 | /* |
| 5102 | * Do the expansion based on xp->xp_context and "pat". |
| 5103 | */ |
| 5104 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5105 | ExpandFromContext( |
| 5106 | expand_T *xp, |
| 5107 | char_u *pat, |
| 5108 | int *num_file, |
| 5109 | char_u ***file, |
| 5110 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5111 | { |
| 5112 | #ifdef FEAT_CMDL_COMPL |
| 5113 | regmatch_T regmatch; |
| 5114 | #endif |
| 5115 | int ret; |
| 5116 | int flags; |
| 5117 | |
| 5118 | flags = EW_DIR; /* include directories */ |
| 5119 | if (options & WILD_LIST_NOTFOUND) |
| 5120 | flags |= EW_NOTFOUND; |
| 5121 | if (options & WILD_ADD_SLASH) |
| 5122 | flags |= EW_ADDSLASH; |
| 5123 | if (options & WILD_KEEP_ALL) |
| 5124 | flags |= EW_KEEPALL; |
| 5125 | if (options & WILD_SILENT) |
| 5126 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 5127 | if (options & WILD_ALLLINKS) |
| 5128 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5129 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5130 | if (xp->xp_context == EXPAND_FILES |
| 5131 | || xp->xp_context == EXPAND_DIRECTORIES |
| 5132 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5133 | { |
| 5134 | /* |
| 5135 | * Expand file or directory names. |
| 5136 | */ |
| 5137 | int free_pat = FALSE; |
| 5138 | int i; |
| 5139 | |
| 5140 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5141 | * space */ |
| 5142 | if (xp->xp_backslash != XP_BS_NONE) |
| 5143 | { |
| 5144 | free_pat = TRUE; |
| 5145 | pat = vim_strsave(pat); |
| 5146 | for (i = 0; pat[i]; ++i) |
| 5147 | if (pat[i] == '\\') |
| 5148 | { |
| 5149 | if (xp->xp_backslash == XP_BS_THREE |
| 5150 | && pat[i + 1] == '\\' |
| 5151 | && pat[i + 2] == '\\' |
| 5152 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5153 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5154 | if (xp->xp_backslash == XP_BS_ONE |
| 5155 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5156 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5157 | } |
| 5158 | } |
| 5159 | |
| 5160 | if (xp->xp_context == EXPAND_FILES) |
| 5161 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5162 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 5163 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5164 | else |
| 5165 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5166 | if (options & WILD_ICASE) |
| 5167 | flags |= EW_ICASE; |
| 5168 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5169 | /* Expand wildcards, supporting %:h and the like. */ |
| 5170 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5171 | if (free_pat) |
| 5172 | vim_free(pat); |
| 5173 | return ret; |
| 5174 | } |
| 5175 | |
| 5176 | *file = (char_u **)""; |
| 5177 | *num_file = 0; |
| 5178 | if (xp->xp_context == EXPAND_HELP) |
| 5179 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 5180 | /* With an empty argument we would get all the help tags, which is |
| 5181 | * very slow. Get matches for "help" instead. */ |
| 5182 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 5183 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5184 | { |
| 5185 | #ifdef FEAT_MULTI_LANG |
| 5186 | cleanup_help_tags(*num_file, *file); |
| 5187 | #endif |
| 5188 | return OK; |
| 5189 | } |
| 5190 | return FAIL; |
| 5191 | } |
| 5192 | |
| 5193 | #ifndef FEAT_CMDL_COMPL |
| 5194 | return FAIL; |
| 5195 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5196 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 5197 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5198 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 5199 | return ExpandOldSetting(num_file, file); |
| 5200 | if (xp->xp_context == EXPAND_BUFFERS) |
| 5201 | return ExpandBufnames(pat, num_file, file, options); |
| 5202 | if (xp->xp_context == EXPAND_TAGS |
| 5203 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 5204 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 5205 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5206 | { |
| 5207 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5208 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 5209 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5210 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5211 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5212 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 5213 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5214 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5215 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5216 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5217 | { |
| 5218 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5219 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5220 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5221 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5222 | { |
| 5223 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5224 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5225 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5226 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5227 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5228 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5229 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5230 | if (xp->xp_context == EXPAND_PACKADD) |
| 5231 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | |
| 5233 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5234 | if (regmatch.regprog == NULL) |
| 5235 | return FAIL; |
| 5236 | |
| 5237 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 5238 | regmatch.rm_ic = ignorecase(pat); |
| 5239 | |
| 5240 | if (xp->xp_context == EXPAND_SETTINGS |
| 5241 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 5242 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 5243 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 5244 | ret = ExpandMappings(®match, num_file, file); |
| 5245 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5246 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 5247 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 5248 | # endif |
| 5249 | else |
| 5250 | { |
| 5251 | static struct expgen |
| 5252 | { |
| 5253 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 5254 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5255 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5256 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5257 | } tab[] = |
| 5258 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5259 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 5260 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 5261 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 5262 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5263 | #ifdef FEAT_CMDHIST |
| 5264 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 5265 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5266 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5267 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 5268 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5269 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 5270 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 5271 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | #endif |
| 5273 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5274 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 5275 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 5276 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 5277 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | #endif |
| 5279 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5280 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 5281 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5282 | #endif |
| 5283 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5284 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5285 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 5286 | #ifdef FEAT_PROFILE |
| 5287 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 5288 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5289 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5290 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 5291 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5292 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5293 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5294 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5295 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5296 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5297 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5298 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5299 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5300 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5301 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5302 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5303 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5304 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5305 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5306 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5307 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5308 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5309 | }; |
| 5310 | int i; |
| 5311 | |
| 5312 | /* |
| 5313 | * Find a context in the table and call the ExpandGeneric() with the |
| 5314 | * right function to do the expansion. |
| 5315 | */ |
| 5316 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5317 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | if (xp->xp_context == tab[i].context) |
| 5319 | { |
| 5320 | if (tab[i].ic) |
| 5321 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5322 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5323 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5324 | break; |
| 5325 | } |
| 5326 | } |
| 5327 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5328 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | |
| 5330 | return ret; |
| 5331 | #endif /* FEAT_CMDL_COMPL */ |
| 5332 | } |
| 5333 | |
| 5334 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5335 | /* |
| 5336 | * Expand a list of names. |
| 5337 | * |
| 5338 | * Generic function for command line completion. It calls a function to |
| 5339 | * obtain strings, one by one. The strings are matched against a regexp |
| 5340 | * program. Matching strings are copied into an array, which is returned. |
| 5341 | * |
| 5342 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5343 | */ |
| 5344 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5345 | ExpandGeneric( |
| 5346 | expand_T *xp, |
| 5347 | regmatch_T *regmatch, |
| 5348 | int *num_file, |
| 5349 | char_u ***file, |
| 5350 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5352 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5353 | { |
| 5354 | int i; |
| 5355 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5356 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5357 | char_u *str; |
| 5358 | |
| 5359 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5360 | * round == 0: count the number of matching names |
| 5361 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5362 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5363 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5364 | { |
| 5365 | for (i = 0; ; ++i) |
| 5366 | { |
| 5367 | str = (*func)(xp, i); |
| 5368 | if (str == NULL) /* end of list */ |
| 5369 | break; |
| 5370 | if (*str == NUL) /* skip empty strings */ |
| 5371 | continue; |
| 5372 | |
| 5373 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5374 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5375 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5376 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5377 | if (escaped) |
| 5378 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5379 | else |
| 5380 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | (*file)[count] = str; |
| 5382 | #ifdef FEAT_MENU |
| 5383 | if (func == get_menu_names && str != NULL) |
| 5384 | { |
| 5385 | /* test for separator added by get_menu_names() */ |
| 5386 | str += STRLEN(str) - 1; |
| 5387 | if (*str == '\001') |
| 5388 | *str = '.'; |
| 5389 | } |
| 5390 | #endif |
| 5391 | } |
| 5392 | ++count; |
| 5393 | } |
| 5394 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5395 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5396 | { |
| 5397 | if (count == 0) |
| 5398 | return OK; |
| 5399 | *num_file = count; |
| 5400 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5401 | if (*file == NULL) |
| 5402 | { |
| 5403 | *file = (char_u **)""; |
| 5404 | return FAIL; |
| 5405 | } |
| 5406 | count = 0; |
| 5407 | } |
| 5408 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5409 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5410 | /* Sort the results. Keep menu's in the specified order. */ |
| 5411 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5412 | { |
| 5413 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5414 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5415 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5416 | /* <SNR> functions should be sorted to the end. */ |
| 5417 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5418 | sort_func_compare); |
| 5419 | else |
| 5420 | sort_strings(*file, *num_file); |
| 5421 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5422 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5423 | #ifdef FEAT_CMDL_COMPL |
| 5424 | /* Reset the variables used for special highlight names expansion, so that |
| 5425 | * they don't show up when getting normal highlight names by ID. */ |
| 5426 | reset_expand_highlight(); |
| 5427 | #endif |
| 5428 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5429 | return OK; |
| 5430 | } |
| 5431 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5432 | /* |
| 5433 | * Complete a shell command. |
| 5434 | * Returns FAIL or OK; |
| 5435 | */ |
| 5436 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5437 | expand_shellcmd( |
| 5438 | char_u *filepat, /* pattern to match with command names */ |
| 5439 | int *num_file, /* return: number of matches */ |
| 5440 | char_u ***file, /* return: array with matches */ |
| 5441 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5442 | { |
| 5443 | char_u *pat; |
| 5444 | int i; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5445 | char_u *path = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5446 | int mustfree = FALSE; |
| 5447 | garray_T ga; |
| 5448 | char_u *buf = alloc(MAXPATHL); |
| 5449 | size_t l; |
| 5450 | char_u *s, *e; |
| 5451 | int flags = flagsarg; |
| 5452 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5453 | int did_curdir = FALSE; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5454 | hashtab_T found_ht; |
| 5455 | hashitem_T *hi; |
| 5456 | hash_T hash; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5457 | |
| 5458 | if (buf == NULL) |
| 5459 | return FAIL; |
| 5460 | |
| 5461 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5462 | * space */ |
| 5463 | pat = vim_strsave(filepat); |
| 5464 | for (i = 0; pat[i]; ++i) |
| 5465 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5466 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5467 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5468 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5469 | |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5470 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 5471 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5472 | path = (char_u *)"."; |
| 5473 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5474 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5475 | /* For an absolute name we don't use $PATH. */ |
| 5476 | if (!mch_isFullName(pat)) |
| 5477 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5478 | if (path == NULL) |
| 5479 | path = (char_u *)""; |
| 5480 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5481 | |
| 5482 | /* |
| 5483 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5484 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5485 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5486 | */ |
| 5487 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5488 | hash_init(&found_ht); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5489 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5490 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5491 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5492 | e = vim_strchr(s, ';'); |
| 5493 | #else |
| 5494 | e = vim_strchr(s, ':'); |
| 5495 | #endif |
| 5496 | if (e == NULL) |
| 5497 | e = s + STRLEN(s); |
| 5498 | |
Bram Moolenaar | 6ab9e42 | 2018-07-28 19:20:13 +0200 | [diff] [blame] | 5499 | if (*s == NUL) |
| 5500 | { |
| 5501 | if (did_curdir) |
| 5502 | break; |
| 5503 | // Find directories in the current directory, path is empty. |
| 5504 | did_curdir = TRUE; |
| 5505 | flags |= EW_DIR; |
| 5506 | } |
| 5507 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 5508 | { |
| 5509 | did_curdir = TRUE; |
| 5510 | flags |= EW_DIR; |
| 5511 | } |
| 5512 | else |
| 5513 | // Do not match directories inside a $PATH item. |
| 5514 | flags &= ~EW_DIR; |
| 5515 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5516 | l = e - s; |
| 5517 | if (l > MAXPATHL - 5) |
| 5518 | break; |
| 5519 | vim_strncpy(buf, s, l); |
| 5520 | add_pathsep(buf); |
| 5521 | l = STRLEN(buf); |
| 5522 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5523 | |
| 5524 | /* Expand matches in one directory of $PATH. */ |
| 5525 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5526 | if (ret == OK) |
| 5527 | { |
| 5528 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5529 | FreeWild(*num_file, *file); |
| 5530 | else |
| 5531 | { |
| 5532 | for (i = 0; i < *num_file; ++i) |
| 5533 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5534 | char_u *name = (*file)[i]; |
| 5535 | |
| 5536 | if (STRLEN(name) > l) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5537 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5538 | // Check if this name was already found. |
| 5539 | hash = hash_hash(name + l); |
| 5540 | hi = hash_lookup(&found_ht, name + l, hash); |
| 5541 | if (HASHITEM_EMPTY(hi)) |
| 5542 | { |
| 5543 | // Remove the path that was prepended. |
| 5544 | STRMOVE(name, name + l); |
| 5545 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 5546 | hash_add_item(&found_ht, hi, name, hash); |
| 5547 | name = NULL; |
| 5548 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5549 | } |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5550 | vim_free(name); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5551 | } |
| 5552 | vim_free(*file); |
| 5553 | } |
| 5554 | } |
| 5555 | if (*e != NUL) |
| 5556 | ++e; |
| 5557 | } |
| 5558 | *file = ga.ga_data; |
| 5559 | *num_file = ga.ga_len; |
| 5560 | |
| 5561 | vim_free(buf); |
| 5562 | vim_free(pat); |
| 5563 | if (mustfree) |
| 5564 | vim_free(path); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5565 | hash_clear(&found_ht); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5566 | return OK; |
| 5567 | } |
| 5568 | |
| 5569 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5570 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5571 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5572 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5573 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5574 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5575 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5576 | call_user_expand_func( |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5577 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5578 | expand_T *xp, |
| 5579 | int *num_file, |
| 5580 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5581 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5582 | int keep = 0; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5583 | typval_T args[4]; |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 5584 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5585 | char_u *pat = NULL; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5586 | void *ret; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5587 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5588 | 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] | 5589 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5590 | *num_file = 0; |
| 5591 | *file = NULL; |
| 5592 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5593 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5594 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5595 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5596 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5597 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5598 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5599 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 5600 | |
| 5601 | args[0].v_type = VAR_STRING; |
| 5602 | args[0].vval.v_string = pat; |
| 5603 | args[1].v_type = VAR_STRING; |
| 5604 | args[1].vval.v_string = xp->xp_line; |
| 5605 | args[2].v_type = VAR_NUMBER; |
| 5606 | args[2].vval.v_number = xp->xp_col; |
| 5607 | args[3].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5608 | |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 5609 | current_sctx = xp->xp_script_ctx; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5610 | |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5611 | ret = user_expand_func(xp->xp_arg, 3, args); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5612 | |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 5613 | current_sctx = save_current_sctx; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5614 | if (ccline.cmdbuff != NULL) |
| 5615 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5616 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5617 | vim_free(pat); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5618 | return ret; |
| 5619 | } |
| 5620 | |
| 5621 | /* |
| 5622 | * Expand names with a function defined by the user. |
| 5623 | */ |
| 5624 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5625 | ExpandUserDefined( |
| 5626 | expand_T *xp, |
| 5627 | regmatch_T *regmatch, |
| 5628 | int *num_file, |
| 5629 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5630 | { |
| 5631 | char_u *retstr; |
| 5632 | char_u *s; |
| 5633 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5634 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5635 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5636 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5637 | |
| 5638 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5639 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5640 | return FAIL; |
| 5641 | |
| 5642 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5643 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | { |
| 5645 | e = vim_strchr(s, '\n'); |
| 5646 | if (e == NULL) |
| 5647 | e = s + STRLEN(s); |
| 5648 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5649 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5650 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5651 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5652 | *e = keep; |
| 5653 | |
| 5654 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5655 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5656 | if (ga_grow(&ga, 1) == FAIL) |
| 5657 | break; |
| 5658 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5659 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5662 | if (*e != NUL) |
| 5663 | ++e; |
| 5664 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5665 | vim_free(retstr); |
| 5666 | *file = ga.ga_data; |
| 5667 | *num_file = ga.ga_len; |
| 5668 | return OK; |
| 5669 | } |
| 5670 | |
| 5671 | /* |
| 5672 | * Expand names with a list returned by a function defined by the user. |
| 5673 | */ |
| 5674 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5675 | ExpandUserList( |
| 5676 | expand_T *xp, |
| 5677 | int *num_file, |
| 5678 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5679 | { |
| 5680 | list_T *retlist; |
| 5681 | listitem_T *li; |
| 5682 | garray_T ga; |
| 5683 | |
| 5684 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5685 | if (retlist == NULL) |
| 5686 | return FAIL; |
| 5687 | |
| 5688 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5689 | /* Loop over the items in the list. */ |
| 5690 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5691 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5692 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5693 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5694 | |
| 5695 | if (ga_grow(&ga, 1) == FAIL) |
| 5696 | break; |
| 5697 | |
| 5698 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5699 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5700 | ++ga.ga_len; |
| 5701 | } |
| 5702 | list_unref(retlist); |
| 5703 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5704 | *file = ga.ga_data; |
| 5705 | *num_file = ga.ga_len; |
| 5706 | return OK; |
| 5707 | } |
| 5708 | #endif |
| 5709 | |
| 5710 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5711 | * Expand color scheme, compiler or filetype names. |
| 5712 | * Search from 'runtimepath': |
| 5713 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5714 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5715 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5716 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5717 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5718 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5719 | */ |
| 5720 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5721 | ExpandRTDir( |
| 5722 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5723 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5724 | int *num_file, |
| 5725 | char_u ***file, |
| 5726 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5727 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5728 | char_u *s; |
| 5729 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5730 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5731 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5732 | int i; |
| 5733 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5734 | |
| 5735 | *num_file = 0; |
| 5736 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5737 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5738 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5739 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5740 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5742 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5743 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5744 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5745 | ga_clear_strings(&ga); |
| 5746 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5747 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5748 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5749 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5750 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5751 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5752 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5753 | if (flags & DIP_START) { |
| 5754 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5755 | { |
| 5756 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5757 | if (s == NULL) |
| 5758 | { |
| 5759 | ga_clear_strings(&ga); |
| 5760 | return FAIL; |
| 5761 | } |
| 5762 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5763 | globpath(p_pp, s, &ga, 0); |
| 5764 | vim_free(s); |
| 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | if (flags & DIP_OPT) { |
| 5769 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5770 | { |
| 5771 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5772 | if (s == NULL) |
| 5773 | { |
| 5774 | ga_clear_strings(&ga); |
| 5775 | return FAIL; |
| 5776 | } |
| 5777 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5778 | globpath(p_pp, s, &ga, 0); |
| 5779 | vim_free(s); |
| 5780 | } |
| 5781 | } |
| 5782 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5783 | for (i = 0; i < ga.ga_len; ++i) |
| 5784 | { |
| 5785 | match = ((char_u **)ga.ga_data)[i]; |
| 5786 | s = match; |
| 5787 | e = s + STRLEN(s); |
| 5788 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5789 | { |
| 5790 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5791 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5792 | if (s < match || vim_ispathsep(*s)) |
| 5793 | break; |
| 5794 | ++s; |
| 5795 | *e = NUL; |
| 5796 | mch_memmove(match, s, e - s + 1); |
| 5797 | } |
| 5798 | } |
| 5799 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5800 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5801 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5802 | |
| 5803 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5804 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5805 | remove_duplicates(&ga); |
| 5806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5807 | *file = ga.ga_data; |
| 5808 | *num_file = ga.ga_len; |
| 5809 | return OK; |
| 5810 | } |
| 5811 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5812 | /* |
| 5813 | * Expand loadplugin names: |
| 5814 | * 'packpath'/pack/ * /opt/{pat} |
| 5815 | */ |
| 5816 | static int |
| 5817 | ExpandPackAddDir( |
| 5818 | char_u *pat, |
| 5819 | int *num_file, |
| 5820 | char_u ***file) |
| 5821 | { |
| 5822 | char_u *s; |
| 5823 | char_u *e; |
| 5824 | char_u *match; |
| 5825 | garray_T ga; |
| 5826 | int i; |
| 5827 | int pat_len; |
| 5828 | |
| 5829 | *num_file = 0; |
| 5830 | *file = NULL; |
| 5831 | pat_len = (int)STRLEN(pat); |
| 5832 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5833 | |
| 5834 | s = alloc((unsigned)(pat_len + 26)); |
| 5835 | if (s == NULL) |
| 5836 | { |
| 5837 | ga_clear_strings(&ga); |
| 5838 | return FAIL; |
| 5839 | } |
| 5840 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5841 | globpath(p_pp, s, &ga, 0); |
| 5842 | vim_free(s); |
| 5843 | |
| 5844 | for (i = 0; i < ga.ga_len; ++i) |
| 5845 | { |
| 5846 | match = ((char_u **)ga.ga_data)[i]; |
| 5847 | s = gettail(match); |
| 5848 | e = s + STRLEN(s); |
| 5849 | mch_memmove(match, s, e - s + 1); |
| 5850 | } |
| 5851 | |
| 5852 | if (ga.ga_len == 0) |
| 5853 | return FAIL; |
| 5854 | |
| 5855 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5856 | * directories in dirnames. */ |
| 5857 | remove_duplicates(&ga); |
| 5858 | |
| 5859 | *file = ga.ga_data; |
| 5860 | *num_file = ga.ga_len; |
| 5861 | return OK; |
| 5862 | } |
| 5863 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5864 | #endif |
| 5865 | |
| 5866 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5867 | /* |
| 5868 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5869 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5870 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5871 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5872 | globpath( |
| 5873 | char_u *path, |
| 5874 | char_u *file, |
| 5875 | garray_T *ga, |
| 5876 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5877 | { |
| 5878 | expand_T xpc; |
| 5879 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | int num_p; |
| 5882 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5883 | |
| 5884 | buf = alloc(MAXPATHL); |
| 5885 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5886 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5887 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5888 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5889 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5890 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | /* Loop over all entries in {path}. */ |
| 5892 | while (*path != NUL) |
| 5893 | { |
| 5894 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5895 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5896 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5897 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5898 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5899 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5900 | * treat it as an escape character, use '/' instead. */ |
| 5901 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5902 | STRCAT(buf, "/"); |
| 5903 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5905 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5906 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5907 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5908 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5909 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5910 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5911 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5912 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5913 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5914 | for (i = 0; i < num_p; ++i) |
| 5915 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5916 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5917 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5918 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5919 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5921 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5922 | FreeWild(num_p, p); |
| 5923 | } |
| 5924 | } |
| 5925 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5926 | |
| 5927 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | #endif |
| 5931 | |
| 5932 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5933 | |
| 5934 | /********************************* |
| 5935 | * Command line history stuff * |
| 5936 | *********************************/ |
| 5937 | |
| 5938 | /* |
| 5939 | * Translate a history character to the associated type number. |
| 5940 | */ |
| 5941 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5942 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5943 | { |
| 5944 | if (c == ':') |
| 5945 | return HIST_CMD; |
| 5946 | if (c == '=') |
| 5947 | return HIST_EXPR; |
| 5948 | if (c == '@') |
| 5949 | return HIST_INPUT; |
| 5950 | if (c == '>') |
| 5951 | return HIST_DEBUG; |
| 5952 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5953 | } |
| 5954 | |
| 5955 | /* |
| 5956 | * Table of history names. |
| 5957 | * These names are used in :history and various hist...() functions. |
| 5958 | * It is sufficient to give the significant prefix of a history name. |
| 5959 | */ |
| 5960 | |
| 5961 | static char *(history_names[]) = |
| 5962 | { |
| 5963 | "cmd", |
| 5964 | "search", |
| 5965 | "expr", |
| 5966 | "input", |
| 5967 | "debug", |
| 5968 | NULL |
| 5969 | }; |
| 5970 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5971 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5972 | /* |
| 5973 | * Function given to ExpandGeneric() to obtain the possible first |
| 5974 | * arguments of the ":history command. |
| 5975 | */ |
| 5976 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5977 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5978 | { |
| 5979 | static char_u compl[2] = { NUL, NUL }; |
| 5980 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5981 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5982 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5983 | |
| 5984 | if (idx < short_names_count) |
| 5985 | { |
| 5986 | compl[0] = (char_u)short_names[idx]; |
| 5987 | return compl; |
| 5988 | } |
| 5989 | if (idx < short_names_count + history_name_count) |
| 5990 | return (char_u *)history_names[idx - short_names_count]; |
| 5991 | if (idx == short_names_count + history_name_count) |
| 5992 | return (char_u *)"all"; |
| 5993 | return NULL; |
| 5994 | } |
| 5995 | #endif |
| 5996 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5997 | /* |
| 5998 | * init_history() - Initialize the command line history. |
| 5999 | * Also used to re-allocate the history when the size changes. |
| 6000 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 6001 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6002 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6003 | { |
| 6004 | int newlen; /* new length of history table */ |
| 6005 | histentry_T *temp; |
| 6006 | int i; |
| 6007 | int j; |
| 6008 | int type; |
| 6009 | |
| 6010 | /* |
| 6011 | * If size of history table changed, reallocate it |
| 6012 | */ |
| 6013 | newlen = (int)p_hi; |
| 6014 | if (newlen != hislen) /* history length changed */ |
| 6015 | { |
| 6016 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 6017 | { |
| 6018 | if (newlen) |
| 6019 | { |
| 6020 | temp = (histentry_T *)lalloc( |
| 6021 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 6022 | if (temp == NULL) /* out of memory! */ |
| 6023 | { |
| 6024 | if (type == 0) /* first one: just keep the old length */ |
| 6025 | { |
| 6026 | newlen = hislen; |
| 6027 | break; |
| 6028 | } |
| 6029 | /* Already changed one table, now we can only have zero |
| 6030 | * length for all tables. */ |
| 6031 | newlen = 0; |
| 6032 | type = -1; |
| 6033 | continue; |
| 6034 | } |
| 6035 | } |
| 6036 | else |
| 6037 | temp = NULL; |
| 6038 | if (newlen == 0 || temp != NULL) |
| 6039 | { |
| 6040 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 6041 | { |
| 6042 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6043 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6044 | } |
| 6045 | else if (newlen > hislen) /* array becomes bigger */ |
| 6046 | { |
| 6047 | for (i = 0; i <= hisidx[type]; ++i) |
| 6048 | temp[i] = history[type][i]; |
| 6049 | j = i; |
| 6050 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6051 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6052 | for ( ; j < hislen; ++i, ++j) |
| 6053 | temp[i] = history[type][j]; |
| 6054 | } |
| 6055 | else /* array becomes smaller or 0 */ |
| 6056 | { |
| 6057 | j = hisidx[type]; |
| 6058 | for (i = newlen - 1; ; --i) |
| 6059 | { |
| 6060 | if (i >= 0) /* copy newest entries */ |
| 6061 | temp[i] = history[type][j]; |
| 6062 | else /* remove older entries */ |
| 6063 | vim_free(history[type][j].hisstr); |
| 6064 | if (--j < 0) |
| 6065 | j = hislen - 1; |
| 6066 | if (j == hisidx[type]) |
| 6067 | break; |
| 6068 | } |
| 6069 | hisidx[type] = newlen - 1; |
| 6070 | } |
| 6071 | vim_free(history[type]); |
| 6072 | history[type] = temp; |
| 6073 | } |
| 6074 | } |
| 6075 | hislen = newlen; |
| 6076 | } |
| 6077 | } |
| 6078 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6079 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6080 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6081 | { |
| 6082 | hisptr->hisnum = 0; |
| 6083 | hisptr->viminfo = FALSE; |
| 6084 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6085 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6086 | } |
| 6087 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6088 | /* |
| 6089 | * Check if command line 'str' is already in history. |
| 6090 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 6091 | */ |
| 6092 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6093 | in_history( |
| 6094 | int type, |
| 6095 | char_u *str, |
| 6096 | int move_to_front, /* Move the entry to the front if it exists */ |
| 6097 | int sep, |
| 6098 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6099 | { |
| 6100 | int i; |
| 6101 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6102 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6103 | |
| 6104 | if (hisidx[type] < 0) |
| 6105 | return FALSE; |
| 6106 | i = hisidx[type]; |
| 6107 | do |
| 6108 | { |
| 6109 | if (history[type][i].hisstr == NULL) |
| 6110 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6111 | |
| 6112 | /* For search history, check that the separator character matches as |
| 6113 | * well. */ |
| 6114 | p = history[type][i].hisstr; |
| 6115 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6116 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6117 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6118 | { |
| 6119 | if (!move_to_front) |
| 6120 | return TRUE; |
| 6121 | last_i = i; |
| 6122 | break; |
| 6123 | } |
| 6124 | if (--i < 0) |
| 6125 | i = hislen - 1; |
| 6126 | } while (i != hisidx[type]); |
| 6127 | |
| 6128 | if (last_i >= 0) |
| 6129 | { |
| 6130 | str = history[type][i].hisstr; |
| 6131 | while (i != hisidx[type]) |
| 6132 | { |
| 6133 | if (++i >= hislen) |
| 6134 | i = 0; |
| 6135 | history[type][last_i] = history[type][i]; |
| 6136 | last_i = i; |
| 6137 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6138 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6139 | history[type][i].viminfo = FALSE; |
| 6140 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6141 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6142 | return TRUE; |
| 6143 | } |
| 6144 | return FALSE; |
| 6145 | } |
| 6146 | |
| 6147 | /* |
| 6148 | * Convert history name (from table above) to its HIST_ equivalent. |
| 6149 | * When "name" is empty, return "cmd" history. |
| 6150 | * Returns -1 for unknown history name. |
| 6151 | */ |
| 6152 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6153 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6154 | { |
| 6155 | int i; |
| 6156 | int len = (int)STRLEN(name); |
| 6157 | |
| 6158 | /* No argument: use current history. */ |
| 6159 | if (len == 0) |
| 6160 | return hist_char2type(ccline.cmdfirstc); |
| 6161 | |
| 6162 | for (i = 0; history_names[i] != NULL; ++i) |
| 6163 | if (STRNICMP(name, history_names[i], len) == 0) |
| 6164 | return i; |
| 6165 | |
| 6166 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 6167 | return hist_char2type(name[0]); |
| 6168 | |
| 6169 | return -1; |
| 6170 | } |
| 6171 | |
| 6172 | static int last_maptick = -1; /* last seen maptick */ |
| 6173 | |
| 6174 | /* |
| 6175 | * Add the given string to the given history. If the string is already in the |
| 6176 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 6177 | * values. |
| 6178 | */ |
| 6179 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6180 | add_to_history( |
| 6181 | int histype, |
| 6182 | char_u *new_entry, |
| 6183 | int in_map, /* consider maptick when inside a mapping */ |
| 6184 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6185 | { |
| 6186 | histentry_T *hisptr; |
| 6187 | int len; |
| 6188 | |
| 6189 | if (hislen == 0) /* no history */ |
| 6190 | return; |
| 6191 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 6192 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 6193 | return; |
| 6194 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6195 | /* |
| 6196 | * Searches inside the same mapping overwrite each other, so that only |
| 6197 | * the last line is kept. Be careful not to remove a line that was moved |
| 6198 | * down, only lines that were added. |
| 6199 | */ |
| 6200 | if (histype == HIST_SEARCH && in_map) |
| 6201 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 6202 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6203 | { |
| 6204 | /* Current line is from the same mapping, remove it */ |
| 6205 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 6206 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6207 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6208 | --hisnum[histype]; |
| 6209 | if (--hisidx[HIST_SEARCH] < 0) |
| 6210 | hisidx[HIST_SEARCH] = hislen - 1; |
| 6211 | } |
| 6212 | last_maptick = -1; |
| 6213 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6214 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6215 | { |
| 6216 | if (++hisidx[histype] == hislen) |
| 6217 | hisidx[histype] = 0; |
| 6218 | hisptr = &history[histype][hisidx[histype]]; |
| 6219 | vim_free(hisptr->hisstr); |
| 6220 | |
| 6221 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6222 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6223 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 6224 | if (hisptr->hisstr != NULL) |
| 6225 | hisptr->hisstr[len + 1] = sep; |
| 6226 | |
| 6227 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6228 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6229 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | if (histype == HIST_SEARCH && in_map) |
| 6231 | last_maptick = maptick; |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6236 | |
| 6237 | /* |
| 6238 | * Get identifier of newest history entry. |
| 6239 | * "histype" may be one of the HIST_ values. |
| 6240 | */ |
| 6241 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6242 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6243 | { |
| 6244 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6245 | || hisidx[histype] < 0) |
| 6246 | return -1; |
| 6247 | |
| 6248 | return history[histype][hisidx[histype]].hisnum; |
| 6249 | } |
| 6250 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6251 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6252 | * Calculate history index from a number: |
| 6253 | * num > 0: seen as identifying number of a history entry |
| 6254 | * num < 0: relative position in history wrt newest entry |
| 6255 | * "histype" may be one of the HIST_ values. |
| 6256 | */ |
| 6257 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6258 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6259 | { |
| 6260 | int i; |
| 6261 | histentry_T *hist; |
| 6262 | int wrapped = FALSE; |
| 6263 | |
| 6264 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6265 | || (i = hisidx[histype]) < 0 || num == 0) |
| 6266 | return -1; |
| 6267 | |
| 6268 | hist = history[histype]; |
| 6269 | if (num > 0) |
| 6270 | { |
| 6271 | while (hist[i].hisnum > num) |
| 6272 | if (--i < 0) |
| 6273 | { |
| 6274 | if (wrapped) |
| 6275 | break; |
| 6276 | i += hislen; |
| 6277 | wrapped = TRUE; |
| 6278 | } |
| 6279 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 6280 | return i; |
| 6281 | } |
| 6282 | else if (-num <= hislen) |
| 6283 | { |
| 6284 | i += num + 1; |
| 6285 | if (i < 0) |
| 6286 | i += hislen; |
| 6287 | if (hist[i].hisstr != NULL) |
| 6288 | return i; |
| 6289 | } |
| 6290 | return -1; |
| 6291 | } |
| 6292 | |
| 6293 | /* |
| 6294 | * Get a history entry by its index. |
| 6295 | * "histype" may be one of the HIST_ values. |
| 6296 | */ |
| 6297 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6298 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6299 | { |
| 6300 | idx = calc_hist_idx(histype, idx); |
| 6301 | if (idx >= 0) |
| 6302 | return history[histype][idx].hisstr; |
| 6303 | else |
| 6304 | return (char_u *)""; |
| 6305 | } |
| 6306 | |
| 6307 | /* |
| 6308 | * Clear all entries of a history. |
| 6309 | * "histype" may be one of the HIST_ values. |
| 6310 | */ |
| 6311 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6312 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6313 | { |
| 6314 | int i; |
| 6315 | histentry_T *hisptr; |
| 6316 | |
| 6317 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6318 | { |
| 6319 | hisptr = history[histype]; |
| 6320 | for (i = hislen; i--;) |
| 6321 | { |
| 6322 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6323 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6324 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6325 | } |
| 6326 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6327 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6328 | return OK; |
| 6329 | } |
| 6330 | return FAIL; |
| 6331 | } |
| 6332 | |
| 6333 | /* |
| 6334 | * Remove all entries matching {str} from a history. |
| 6335 | * "histype" may be one of the HIST_ values. |
| 6336 | */ |
| 6337 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6338 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6339 | { |
| 6340 | regmatch_T regmatch; |
| 6341 | histentry_T *hisptr; |
| 6342 | int idx; |
| 6343 | int i; |
| 6344 | int last; |
| 6345 | int found = FALSE; |
| 6346 | |
| 6347 | regmatch.regprog = NULL; |
| 6348 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6349 | if (hislen != 0 |
| 6350 | && histype >= 0 |
| 6351 | && histype < HIST_COUNT |
| 6352 | && *str != NUL |
| 6353 | && (idx = hisidx[histype]) >= 0 |
| 6354 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6355 | != NULL) |
| 6356 | { |
| 6357 | i = last = idx; |
| 6358 | do |
| 6359 | { |
| 6360 | hisptr = &history[histype][i]; |
| 6361 | if (hisptr->hisstr == NULL) |
| 6362 | break; |
| 6363 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6364 | { |
| 6365 | found = TRUE; |
| 6366 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6367 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6368 | } |
| 6369 | else |
| 6370 | { |
| 6371 | if (i != last) |
| 6372 | { |
| 6373 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6374 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6375 | } |
| 6376 | if (--last < 0) |
| 6377 | last += hislen; |
| 6378 | } |
| 6379 | if (--i < 0) |
| 6380 | i += hislen; |
| 6381 | } while (i != idx); |
| 6382 | if (history[histype][idx].hisstr == NULL) |
| 6383 | hisidx[histype] = -1; |
| 6384 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6385 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6386 | return found; |
| 6387 | } |
| 6388 | |
| 6389 | /* |
| 6390 | * Remove an indexed entry from a history. |
| 6391 | * "histype" may be one of the HIST_ values. |
| 6392 | */ |
| 6393 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6394 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6395 | { |
| 6396 | int i, j; |
| 6397 | |
| 6398 | i = calc_hist_idx(histype, idx); |
| 6399 | if (i < 0) |
| 6400 | return FALSE; |
| 6401 | idx = hisidx[histype]; |
| 6402 | vim_free(history[histype][i].hisstr); |
| 6403 | |
| 6404 | /* When deleting the last added search string in a mapping, reset |
| 6405 | * last_maptick, so that the last added search string isn't deleted again. |
| 6406 | */ |
| 6407 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6408 | last_maptick = -1; |
| 6409 | |
| 6410 | while (i != idx) |
| 6411 | { |
| 6412 | j = (i + 1) % hislen; |
| 6413 | history[histype][i] = history[histype][j]; |
| 6414 | i = j; |
| 6415 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6416 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6417 | if (--i < 0) |
| 6418 | i += hislen; |
| 6419 | hisidx[histype] = i; |
| 6420 | return TRUE; |
| 6421 | } |
| 6422 | |
| 6423 | #endif /* FEAT_EVAL */ |
| 6424 | |
| 6425 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6426 | /* |
| 6427 | * Very specific function to remove the value in ":set key=val" from the |
| 6428 | * history. |
| 6429 | */ |
| 6430 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6431 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6432 | { |
| 6433 | char_u *p; |
| 6434 | int i; |
| 6435 | |
| 6436 | i = hisidx[HIST_CMD]; |
| 6437 | if (i < 0) |
| 6438 | return; |
| 6439 | p = history[HIST_CMD][i].hisstr; |
| 6440 | if (p != NULL) |
| 6441 | for ( ; *p; ++p) |
| 6442 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6443 | { |
| 6444 | p = vim_strchr(p + 3, '='); |
| 6445 | if (p == NULL) |
| 6446 | break; |
| 6447 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6448 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6449 | if (p[i] == '\\' && p[i + 1]) |
| 6450 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6451 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | --p; |
| 6453 | } |
| 6454 | } |
| 6455 | #endif |
| 6456 | |
| 6457 | #endif /* FEAT_CMDHIST */ |
| 6458 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6459 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6460 | /* |
Bram Moolenaar | 438d176 | 2018-09-30 17:11:48 +0200 | [diff] [blame] | 6461 | * Get pointer to the command line info to use. save_ccline() may clear |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6462 | * ccline and put the previous value in prev_ccline. |
| 6463 | */ |
| 6464 | static struct cmdline_info * |
| 6465 | get_ccline_ptr(void) |
| 6466 | { |
| 6467 | if ((State & CMDLINE) == 0) |
| 6468 | return NULL; |
| 6469 | if (ccline.cmdbuff != NULL) |
| 6470 | return &ccline; |
| 6471 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6472 | return &prev_ccline; |
| 6473 | return NULL; |
| 6474 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6475 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6476 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6477 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6478 | /* |
| 6479 | * Get the current command line in allocated memory. |
| 6480 | * Only works when the command line is being edited. |
| 6481 | * Returns NULL when something is wrong. |
| 6482 | */ |
| 6483 | char_u * |
| 6484 | get_cmdline_str(void) |
| 6485 | { |
Bram Moolenaar | ee91c33 | 2018-09-25 22:27:35 +0200 | [diff] [blame] | 6486 | struct cmdline_info *p; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6487 | |
Bram Moolenaar | ee91c33 | 2018-09-25 22:27:35 +0200 | [diff] [blame] | 6488 | if (cmdline_star > 0) |
| 6489 | return NULL; |
| 6490 | p = get_ccline_ptr(); |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6491 | if (p == NULL) |
| 6492 | return NULL; |
| 6493 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6494 | } |
| 6495 | |
| 6496 | /* |
| 6497 | * Get the current command line position, counted in bytes. |
| 6498 | * Zero is the first position. |
| 6499 | * Only works when the command line is being edited. |
| 6500 | * Returns -1 when something is wrong. |
| 6501 | */ |
| 6502 | int |
| 6503 | get_cmdline_pos(void) |
| 6504 | { |
| 6505 | struct cmdline_info *p = get_ccline_ptr(); |
| 6506 | |
| 6507 | if (p == NULL) |
| 6508 | return -1; |
| 6509 | return p->cmdpos; |
| 6510 | } |
| 6511 | |
| 6512 | /* |
| 6513 | * Set the command line byte position to "pos". Zero is the first position. |
| 6514 | * Only works when the command line is being edited. |
| 6515 | * Returns 1 when failed, 0 when OK. |
| 6516 | */ |
| 6517 | int |
| 6518 | set_cmdline_pos( |
| 6519 | int pos) |
| 6520 | { |
| 6521 | struct cmdline_info *p = get_ccline_ptr(); |
| 6522 | |
| 6523 | if (p == NULL) |
| 6524 | return 1; |
| 6525 | |
| 6526 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6527 | * changed the command line. */ |
| 6528 | if (pos < 0) |
| 6529 | new_cmdpos = 0; |
| 6530 | else |
| 6531 | new_cmdpos = pos; |
| 6532 | return 0; |
| 6533 | } |
| 6534 | #endif |
| 6535 | |
| 6536 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6537 | /* |
| 6538 | * Get the current command-line type. |
| 6539 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6540 | * Only works when the command line is being edited. |
| 6541 | * Returns NUL when something is wrong. |
| 6542 | */ |
| 6543 | int |
| 6544 | get_cmdline_type(void) |
| 6545 | { |
| 6546 | struct cmdline_info *p = get_ccline_ptr(); |
| 6547 | |
| 6548 | if (p == NULL) |
| 6549 | return NUL; |
| 6550 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6551 | return |
| 6552 | # ifdef FEAT_EVAL |
| 6553 | (p->input_fn) ? '@' : |
| 6554 | # endif |
| 6555 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6556 | return p->cmdfirstc; |
| 6557 | } |
| 6558 | #endif |
| 6559 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6560 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6561 | /* |
| 6562 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6563 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6564 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6565 | */ |
| 6566 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6567 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | { |
| 6569 | int len; |
| 6570 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6571 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6572 | |
| 6573 | *str = skipwhite(*str); |
| 6574 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6575 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6576 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6577 | *str += len; |
| 6578 | *num1 = (int)num; |
| 6579 | first = TRUE; |
| 6580 | } |
| 6581 | *str = skipwhite(*str); |
| 6582 | if (**str == ',') /* parse "to" part of range */ |
| 6583 | { |
| 6584 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6585 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6586 | if (len > 0) |
| 6587 | { |
| 6588 | *num2 = (int)num; |
| 6589 | *str = skipwhite(*str + len); |
| 6590 | } |
| 6591 | else if (!first) /* no number given at all */ |
| 6592 | return FAIL; |
| 6593 | } |
| 6594 | else if (first) /* only one number given */ |
| 6595 | *num2 = *num1; |
| 6596 | return OK; |
| 6597 | } |
| 6598 | #endif |
| 6599 | |
| 6600 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6601 | /* |
| 6602 | * :history command - print a history |
| 6603 | */ |
| 6604 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6605 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6606 | { |
| 6607 | histentry_T *hist; |
| 6608 | int histype1 = HIST_CMD; |
| 6609 | int histype2 = HIST_CMD; |
| 6610 | int hisidx1 = 1; |
| 6611 | int hisidx2 = -1; |
| 6612 | int idx; |
| 6613 | int i, j, k; |
| 6614 | char_u *end; |
| 6615 | char_u *arg = eap->arg; |
| 6616 | |
| 6617 | if (hislen == 0) |
| 6618 | { |
| 6619 | MSG(_("'history' option is zero")); |
| 6620 | return; |
| 6621 | } |
| 6622 | |
| 6623 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6624 | { |
| 6625 | end = arg; |
| 6626 | while (ASCII_ISALPHA(*end) |
| 6627 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6628 | end++; |
| 6629 | i = *end; |
| 6630 | *end = NUL; |
| 6631 | histype1 = get_histtype(arg); |
| 6632 | if (histype1 == -1) |
| 6633 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6634 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6635 | { |
| 6636 | histype1 = 0; |
| 6637 | histype2 = HIST_COUNT-1; |
| 6638 | } |
| 6639 | else |
| 6640 | { |
| 6641 | *end = i; |
| 6642 | EMSG(_(e_trailing)); |
| 6643 | return; |
| 6644 | } |
| 6645 | } |
| 6646 | else |
| 6647 | histype2 = histype1; |
| 6648 | *end = i; |
| 6649 | } |
| 6650 | else |
| 6651 | end = arg; |
| 6652 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6653 | { |
| 6654 | EMSG(_(e_trailing)); |
| 6655 | return; |
| 6656 | } |
| 6657 | |
| 6658 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6659 | { |
| 6660 | STRCPY(IObuff, "\n # "); |
| 6661 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6662 | MSG_PUTS_TITLE(IObuff); |
| 6663 | idx = hisidx[histype1]; |
| 6664 | hist = history[histype1]; |
| 6665 | j = hisidx1; |
| 6666 | k = hisidx2; |
| 6667 | if (j < 0) |
| 6668 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6669 | if (k < 0) |
| 6670 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6671 | if (idx >= 0 && j <= k) |
| 6672 | for (i = idx + 1; !got_int; ++i) |
| 6673 | { |
| 6674 | if (i == hislen) |
| 6675 | i = 0; |
| 6676 | if (hist[i].hisstr != NULL |
| 6677 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6678 | { |
| 6679 | msg_putchar('\n'); |
| 6680 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6681 | hist[i].hisnum); |
| 6682 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6683 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6684 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | else |
| 6686 | STRCAT(IObuff, hist[i].hisstr); |
| 6687 | msg_outtrans(IObuff); |
| 6688 | out_flush(); |
| 6689 | } |
| 6690 | if (i == idx) |
| 6691 | break; |
| 6692 | } |
| 6693 | } |
| 6694 | } |
| 6695 | #endif |
| 6696 | |
| 6697 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6698 | /* |
| 6699 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6700 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6701 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6702 | {NULL, NULL, NULL, NULL, NULL}; |
| 6703 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6704 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6705 | static int viminfo_add_at_front = FALSE; |
| 6706 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6707 | /* |
| 6708 | * Translate a history type number to the associated character. |
| 6709 | */ |
| 6710 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6711 | hist_type2char( |
| 6712 | int type, |
| 6713 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6714 | { |
| 6715 | if (type == HIST_CMD) |
| 6716 | return ':'; |
| 6717 | if (type == HIST_SEARCH) |
| 6718 | { |
| 6719 | if (use_question) |
| 6720 | return '?'; |
| 6721 | else |
| 6722 | return '/'; |
| 6723 | } |
| 6724 | if (type == HIST_EXPR) |
| 6725 | return '='; |
| 6726 | return '@'; |
| 6727 | } |
| 6728 | |
| 6729 | /* |
| 6730 | * Prepare for reading the history from the viminfo file. |
| 6731 | * This allocates history arrays to store the read history lines. |
| 6732 | */ |
| 6733 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6734 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6735 | { |
| 6736 | int i; |
| 6737 | int num; |
| 6738 | int type; |
| 6739 | int len; |
| 6740 | |
| 6741 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6742 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6743 | if (asklen > hislen) |
| 6744 | asklen = hislen; |
| 6745 | |
| 6746 | for (type = 0; type < HIST_COUNT; ++type) |
| 6747 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6748 | /* Count the number of empty spaces in the history list. Entries read |
| 6749 | * from viminfo previously are also considered empty. If there are |
| 6750 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6751 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6752 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6753 | num++; |
| 6754 | len = asklen; |
| 6755 | if (num > len) |
| 6756 | len = num; |
| 6757 | if (len <= 0) |
| 6758 | viminfo_history[type] = NULL; |
| 6759 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6760 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6761 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6762 | if (viminfo_history[type] == NULL) |
| 6763 | len = 0; |
| 6764 | viminfo_hislen[type] = len; |
| 6765 | viminfo_hisidx[type] = 0; |
| 6766 | } |
| 6767 | } |
| 6768 | |
| 6769 | /* |
| 6770 | * Accept a line from the viminfo, store it in the history array when it's |
| 6771 | * new. |
| 6772 | */ |
| 6773 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6774 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6775 | { |
| 6776 | int type; |
| 6777 | long_u len; |
| 6778 | char_u *val; |
| 6779 | char_u *p; |
| 6780 | |
| 6781 | type = hist_char2type(virp->vir_line[0]); |
| 6782 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6783 | { |
| 6784 | val = viminfo_readstring(virp, 1, TRUE); |
| 6785 | if (val != NULL && *val != NUL) |
| 6786 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6787 | int sep = (*val == ' ' ? NUL : *val); |
| 6788 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6789 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6790 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6791 | { |
| 6792 | /* Need to re-allocate to append the separator byte. */ |
| 6793 | len = STRLEN(val); |
| 6794 | p = lalloc(len + 2, TRUE); |
| 6795 | if (p != NULL) |
| 6796 | { |
| 6797 | if (type == HIST_SEARCH) |
| 6798 | { |
| 6799 | /* Search entry: Move the separator from the first |
| 6800 | * column to after the NUL. */ |
| 6801 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6802 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | } |
| 6804 | else |
| 6805 | { |
| 6806 | /* Not a search entry: No separator in the viminfo |
| 6807 | * file, add a NUL separator. */ |
| 6808 | mch_memmove(p, val, (size_t)len + 1); |
| 6809 | p[len + 1] = NUL; |
| 6810 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6811 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6812 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6813 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6814 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6815 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6816 | } |
| 6817 | } |
| 6818 | } |
| 6819 | vim_free(val); |
| 6820 | } |
| 6821 | return viminfo_readline(virp); |
| 6822 | } |
| 6823 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6824 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6825 | * Accept a new style history line from the viminfo, store it in the history |
| 6826 | * array when it's new. |
| 6827 | */ |
| 6828 | void |
| 6829 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6830 | garray_T *values, |
| 6831 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6832 | { |
| 6833 | int type; |
| 6834 | long_u len; |
| 6835 | char_u *val; |
| 6836 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6837 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6838 | |
| 6839 | /* Check the format: |
| 6840 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6841 | if (values->ga_len < 4 |
| 6842 | || vp[0].bv_type != BVAL_NR |
| 6843 | || vp[1].bv_type != BVAL_NR |
| 6844 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6845 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6846 | return; |
| 6847 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6848 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6849 | if (type >= HIST_COUNT) |
| 6850 | return; |
| 6851 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6852 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6853 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6854 | if (val != NULL && *val != NUL) |
| 6855 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6856 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6857 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6858 | int idx; |
| 6859 | int overwrite = FALSE; |
| 6860 | |
| 6861 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6862 | { |
| 6863 | /* If lines were written by an older Vim we need to avoid |
| 6864 | * getting duplicates. See if the entry already exists. */ |
| 6865 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6866 | { |
| 6867 | p = viminfo_history[type][idx].hisstr; |
| 6868 | if (STRCMP(val, p) == 0 |
| 6869 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6870 | { |
| 6871 | overwrite = TRUE; |
| 6872 | break; |
| 6873 | } |
| 6874 | } |
| 6875 | |
| 6876 | if (!overwrite) |
| 6877 | { |
| 6878 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6879 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6880 | p = lalloc(len + 2, TRUE); |
| 6881 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6882 | else |
| 6883 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6884 | if (p != NULL) |
| 6885 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6886 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6887 | if (!overwrite) |
| 6888 | { |
| 6889 | mch_memmove(p, val, (size_t)len + 1); |
| 6890 | /* Put the separator after the NUL. */ |
| 6891 | p[len + 1] = sep; |
| 6892 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6893 | viminfo_history[type][idx].hisnum = 0; |
| 6894 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6895 | viminfo_hisidx[type]++; |
| 6896 | } |
| 6897 | } |
| 6898 | } |
| 6899 | } |
| 6900 | } |
| 6901 | } |
| 6902 | |
| 6903 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6904 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6905 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6906 | static void |
| 6907 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6908 | { |
| 6909 | int idx; |
| 6910 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6911 | |
| 6912 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6913 | if (idx >= hislen) |
| 6914 | idx -= hislen; |
| 6915 | else if (idx < 0) |
| 6916 | idx = hislen - 1; |
| 6917 | if (viminfo_add_at_front) |
| 6918 | hisidx[type] = idx; |
| 6919 | else |
| 6920 | { |
| 6921 | if (hisidx[type] == -1) |
| 6922 | hisidx[type] = hislen - 1; |
| 6923 | do |
| 6924 | { |
| 6925 | if (history[type][idx].hisstr != NULL |
| 6926 | || history[type][idx].viminfo) |
| 6927 | break; |
| 6928 | if (++idx == hislen) |
| 6929 | idx = 0; |
| 6930 | } while (idx != hisidx[type]); |
| 6931 | if (idx != hisidx[type] && --idx < 0) |
| 6932 | idx = hislen - 1; |
| 6933 | } |
| 6934 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6935 | { |
| 6936 | vim_free(history[type][idx].hisstr); |
| 6937 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6938 | history[type][idx].viminfo = TRUE; |
| 6939 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6940 | if (--idx < 0) |
| 6941 | idx = hislen - 1; |
| 6942 | } |
| 6943 | idx += 1; |
| 6944 | idx %= hislen; |
| 6945 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6946 | { |
| 6947 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6948 | idx %= hislen; |
| 6949 | } |
| 6950 | } |
| 6951 | |
| 6952 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6953 | static int |
| 6954 | #ifdef __BORLANDC__ |
| 6955 | _RTLENTRYF |
| 6956 | #endif |
| 6957 | sort_hist(const void *s1, const void *s2) |
| 6958 | { |
| 6959 | histentry_T *p1 = *(histentry_T **)s1; |
| 6960 | histentry_T *p2 = *(histentry_T **)s2; |
| 6961 | |
| 6962 | if (p1->time_set < p2->time_set) return -1; |
| 6963 | if (p1->time_set > p2->time_set) return 1; |
| 6964 | return 0; |
| 6965 | } |
| 6966 | #endif |
| 6967 | |
| 6968 | /* |
| 6969 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6970 | * timestamp; |
| 6971 | */ |
| 6972 | static void |
| 6973 | merge_history(int type) |
| 6974 | { |
| 6975 | int max_len; |
| 6976 | histentry_T **tot_hist; |
| 6977 | histentry_T *new_hist; |
| 6978 | int i; |
| 6979 | int len; |
| 6980 | |
| 6981 | /* Make one long list with all entries. */ |
| 6982 | max_len = hislen + viminfo_hisidx[type]; |
| 6983 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6984 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6985 | if (tot_hist == NULL || new_hist == NULL) |
| 6986 | { |
| 6987 | vim_free(tot_hist); |
| 6988 | vim_free(new_hist); |
| 6989 | return; |
| 6990 | } |
| 6991 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6992 | tot_hist[i] = &viminfo_history[type][i]; |
| 6993 | len = i; |
| 6994 | for (i = 0; i < hislen; i++) |
| 6995 | if (history[type][i].hisstr != NULL) |
| 6996 | tot_hist[len++] = &history[type][i]; |
| 6997 | |
| 6998 | /* Sort the list on timestamp. */ |
| 6999 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 7000 | |
| 7001 | /* Keep the newest ones. */ |
| 7002 | for (i = 0; i < hislen; i++) |
| 7003 | { |
| 7004 | if (i < len) |
| 7005 | { |
| 7006 | new_hist[i] = *tot_hist[i]; |
| 7007 | tot_hist[i]->hisstr = NULL; |
| 7008 | if (new_hist[i].hisnum == 0) |
| 7009 | new_hist[i].hisnum = ++hisnum[type]; |
| 7010 | } |
| 7011 | else |
| 7012 | clear_hist_entry(&new_hist[i]); |
| 7013 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 7014 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7015 | |
| 7016 | /* Free what is not kept. */ |
| 7017 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 7018 | vim_free(viminfo_history[type][i].hisstr); |
| 7019 | for (i = 0; i < hislen; i++) |
| 7020 | vim_free(history[type][i].hisstr); |
| 7021 | vim_free(history[type]); |
| 7022 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 7023 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7024 | } |
| 7025 | |
| 7026 | /* |
| 7027 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 7028 | */ |
| 7029 | void |
| 7030 | finish_viminfo_history(vir_T *virp) |
| 7031 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7032 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7033 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | |
| 7035 | for (type = 0; type < HIST_COUNT; ++type) |
| 7036 | { |
| 7037 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7038 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7039 | |
| 7040 | if (merge) |
| 7041 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7042 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7043 | concat_history(type); |
| 7044 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7045 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7046 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7047 | } |
| 7048 | } |
| 7049 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7050 | /* |
| 7051 | * Write history to viminfo file in "fp". |
| 7052 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 7053 | * file, data is in viminfo_history[]. |
| 7054 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 7055 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7056 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7057 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7058 | { |
| 7059 | int i; |
| 7060 | int type; |
| 7061 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7062 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | |
| 7064 | init_history(); |
| 7065 | if (hislen == 0) |
| 7066 | return; |
| 7067 | for (type = 0; type < HIST_COUNT; ++type) |
| 7068 | { |
| 7069 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 7070 | if (num_saved == 0) |
| 7071 | continue; |
| 7072 | if (num_saved < 0) /* Use default */ |
| 7073 | num_saved = hislen; |
| 7074 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 7075 | type == HIST_CMD ? _("Command Line") : |
| 7076 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7077 | type == HIST_EXPR ? _("Expression") : |
| 7078 | type == HIST_INPUT ? _("Input Line") : |
| 7079 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7080 | if (num_saved > hislen) |
| 7081 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7082 | |
| 7083 | /* |
| 7084 | * Merge typed and viminfo history: |
| 7085 | * round 1: history of typed commands. |
| 7086 | * round 2: history from recently read viminfo. |
| 7087 | */ |
| 7088 | for (round = 1; round <= 2; ++round) |
| 7089 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 7090 | if (round == 1) |
| 7091 | /* start at newest entry, somewhere in the list */ |
| 7092 | i = hisidx[type]; |
| 7093 | else if (viminfo_hisidx[type] > 0) |
| 7094 | /* start at newest entry, first in the list */ |
| 7095 | i = 0; |
| 7096 | else |
| 7097 | /* empty list */ |
| 7098 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7099 | if (i >= 0) |
| 7100 | while (num_saved > 0 |
| 7101 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7102 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7103 | char_u *p; |
| 7104 | time_t timestamp; |
| 7105 | int c = NUL; |
| 7106 | |
| 7107 | if (round == 1) |
| 7108 | { |
| 7109 | p = history[type][i].hisstr; |
| 7110 | timestamp = history[type][i].time_set; |
| 7111 | } |
| 7112 | else |
| 7113 | { |
| 7114 | p = viminfo_history[type] == NULL ? NULL |
| 7115 | : viminfo_history[type][i].hisstr; |
| 7116 | timestamp = viminfo_history[type] == NULL ? 0 |
| 7117 | : viminfo_history[type][i].time_set; |
| 7118 | } |
| 7119 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7120 | if (p != NULL && (round == 2 |
| 7121 | || !merge |
| 7122 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7124 | --num_saved; |
| 7125 | fputc(hist_type2char(type, TRUE), fp); |
| 7126 | /* For the search history: put the separator in the |
| 7127 | * second column; use a space if there isn't one. */ |
| 7128 | if (type == HIST_SEARCH) |
| 7129 | { |
| 7130 | c = p[STRLEN(p) + 1]; |
| 7131 | putc(c == NUL ? ' ' : c, fp); |
| 7132 | } |
| 7133 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7134 | |
| 7135 | { |
| 7136 | char cbuf[NUMBUFLEN]; |
| 7137 | |
| 7138 | /* New style history with a bar line. Format: |
| 7139 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 7140 | if (c == NUL) |
| 7141 | cbuf[0] = NUL; |
| 7142 | else |
| 7143 | sprintf(cbuf, "%d", c); |
| 7144 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 7145 | type, (long)timestamp, cbuf); |
| 7146 | barline_writestring(fp, p, LSIZE - 20); |
| 7147 | putc('\n', fp); |
| 7148 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7149 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7150 | if (round == 1) |
| 7151 | { |
| 7152 | /* Decrement index, loop around and stop when back at |
| 7153 | * the start. */ |
| 7154 | if (--i < 0) |
| 7155 | i = hislen - 1; |
| 7156 | if (i == hisidx[type]) |
| 7157 | break; |
| 7158 | } |
| 7159 | else |
| 7160 | { |
| 7161 | /* Increment index. Stop at the end in the while. */ |
| 7162 | ++i; |
| 7163 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7164 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7165 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 7166 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7167 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7168 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7169 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 7170 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7171 | } |
| 7172 | } |
| 7173 | #endif /* FEAT_VIMINFO */ |
| 7174 | |
| 7175 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 7176 | /* |
| 7177 | * Write a character at the current cursor+offset position. |
| 7178 | * It is directly written into the command buffer block. |
| 7179 | */ |
| 7180 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7181 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7182 | { |
| 7183 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7184 | { |
| 7185 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 7186 | return; |
| 7187 | } |
| 7188 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 7189 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 7190 | } |
| 7191 | |
| 7192 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7193 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7194 | { |
| 7195 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7196 | { |
| 7197 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 7198 | return NUL; |
| 7199 | } |
| 7200 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 7201 | } |
| 7202 | #endif |
| 7203 | |
| 7204 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 7205 | /* |
| 7206 | * Open a window on the current command line and history. Allow editing in |
| 7207 | * the window. Returns when the window is closed. |
| 7208 | * Returns: |
| 7209 | * CR if the command is to be executed |
| 7210 | * Ctrl_C if it is to be abandoned |
| 7211 | * K_IGNORE if editing continues |
| 7212 | */ |
| 7213 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7214 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7215 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7216 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7217 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7218 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7219 | win_T *wp; |
| 7220 | int i; |
| 7221 | linenr_T lnum; |
| 7222 | int histtype; |
| 7223 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7224 | int save_restart_edit = restart_edit; |
| 7225 | int save_State = State; |
| 7226 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7227 | #ifdef FEAT_RIGHTLEFT |
| 7228 | int save_cmdmsg_rl = cmdmsg_rl; |
| 7229 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7230 | #ifdef FEAT_FOLDING |
| 7231 | int save_KeyTyped; |
| 7232 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7233 | |
| 7234 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 7235 | if (cmdwin_type != 0 |
| 7236 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 7237 | || cmdline_star > 0 |
| 7238 | # endif |
| 7239 | ) |
| 7240 | { |
| 7241 | beep_flush(); |
| 7242 | return K_IGNORE; |
| 7243 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7244 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7245 | |
| 7246 | /* Save current window sizes. */ |
| 7247 | win_size_save(&winsizes); |
| 7248 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7249 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7250 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7251 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7252 | /* don't use a new tab page */ |
| 7253 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7254 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7255 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7256 | /* Create a window for the command-line buffer. */ |
| 7257 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 7258 | { |
| 7259 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7260 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7261 | return K_IGNORE; |
| 7262 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7263 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7264 | |
| 7265 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 7266 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7267 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7268 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7269 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 7270 | #ifdef FEAT_FOLDING |
| 7271 | curwin->w_p_fen = FALSE; |
| 7272 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7273 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7274 | curwin->w_p_rl = cmdmsg_rl; |
| 7275 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7276 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 7277 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7278 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7279 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7280 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7281 | /* But don't allow switching to another buffer. */ |
| 7282 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7283 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7284 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 7285 | need_wait_return = FALSE; |
| 7286 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7287 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7288 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 7289 | { |
| 7290 | if (p_wc == TAB) |
| 7291 | { |
| 7292 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 7293 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 7294 | } |
| 7295 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 7296 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7297 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7298 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 7299 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7300 | * sets 'textwidth' to 78). */ |
| 7301 | curbuf->b_p_tw = 0; |
| 7302 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7303 | /* Fill the buffer with the history. */ |
| 7304 | init_history(); |
| 7305 | if (hislen > 0) |
| 7306 | { |
| 7307 | i = hisidx[histtype]; |
| 7308 | if (i >= 0) |
| 7309 | { |
| 7310 | lnum = 0; |
| 7311 | do |
| 7312 | { |
| 7313 | if (++i == hislen) |
| 7314 | i = 0; |
| 7315 | if (history[histtype][i].hisstr != NULL) |
| 7316 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7317 | (colnr_T)0, FALSE); |
| 7318 | } |
| 7319 | while (i != hisidx[histtype]); |
| 7320 | } |
| 7321 | } |
| 7322 | |
| 7323 | /* Replace the empty last line with the current command-line and put the |
| 7324 | * cursor there. */ |
| 7325 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7326 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7327 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7328 | changed_line_abv_curs(); |
| 7329 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7330 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7331 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7332 | /* No Ex mode here! */ |
| 7333 | exmode_active = 0; |
| 7334 | |
| 7335 | State = NORMAL; |
| 7336 | # ifdef FEAT_MOUSE |
| 7337 | setmouse(); |
| 7338 | # endif |
| 7339 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7340 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7341 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7342 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7343 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7344 | |
| 7345 | i = RedrawingDisabled; |
| 7346 | RedrawingDisabled = 0; |
| 7347 | |
| 7348 | /* |
| 7349 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7350 | */ |
| 7351 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7352 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7353 | |
| 7354 | RedrawingDisabled = i; |
| 7355 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7356 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7357 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7358 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7360 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7361 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7362 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7363 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7364 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7365 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7366 | # endif |
| 7367 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7368 | cmdwin_type = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7369 | exmode_active = save_exmode; |
| 7370 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7371 | /* 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] | 7372 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7373 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7374 | { |
| 7375 | cmdwin_result = Ctrl_C; |
| 7376 | EMSG(_("E199: Active window or buffer deleted")); |
| 7377 | } |
| 7378 | else |
| 7379 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7380 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7381 | /* autocmds may abort script processing */ |
| 7382 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7383 | cmdwin_result = Ctrl_C; |
| 7384 | # endif |
| 7385 | /* Set the new command line from the cmdline buffer. */ |
| 7386 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7387 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7388 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7389 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7390 | |
| 7391 | if (histtype == HIST_CMD) |
| 7392 | { |
| 7393 | /* Execute the command directly. */ |
| 7394 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7395 | cmdwin_result = CAR; |
| 7396 | } |
| 7397 | else |
| 7398 | { |
| 7399 | /* First need to cancel what we were doing. */ |
| 7400 | ccline.cmdbuff = NULL; |
| 7401 | stuffcharReadbuff(':'); |
| 7402 | stuffReadbuff((char_u *)p); |
| 7403 | stuffcharReadbuff(CAR); |
| 7404 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7405 | } |
| 7406 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7407 | { |
| 7408 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7409 | cmdwin_result = CAR; |
| 7410 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7411 | else if (cmdwin_result == Ctrl_C) |
| 7412 | { |
| 7413 | /* :q or :close, don't execute any command |
| 7414 | * and don't modify the cmd window. */ |
| 7415 | ccline.cmdbuff = NULL; |
| 7416 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7417 | else |
| 7418 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7419 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7420 | { |
| 7421 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7422 | ccline.cmdlen = 0; |
| 7423 | ccline.cmdbufflen = 1; |
| 7424 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7425 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7426 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7427 | else |
| 7428 | { |
| 7429 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7430 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7431 | ccline.cmdpos = curwin->w_cursor.col; |
| 7432 | if (ccline.cmdpos > ccline.cmdlen) |
| 7433 | ccline.cmdpos = ccline.cmdlen; |
| 7434 | if (cmdwin_result == K_IGNORE) |
| 7435 | { |
| 7436 | set_cmdspos_cursor(); |
| 7437 | redrawcmd(); |
| 7438 | } |
| 7439 | } |
| 7440 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7441 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7442 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7443 | # ifdef FEAT_CONCEAL |
| 7444 | /* Avoid command-line window first character being concealed. */ |
| 7445 | curwin->w_p_cole = 0; |
| 7446 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7447 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7448 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7449 | win_goto(old_curwin); |
| 7450 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7451 | |
| 7452 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7453 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7454 | if (bufref_valid(&bufref)) |
| 7455 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7456 | |
| 7457 | /* Restore window sizes. */ |
| 7458 | win_size_restore(&winsizes); |
| 7459 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7460 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
| 7463 | ga_clear(&winsizes); |
| 7464 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7465 | # ifdef FEAT_RIGHTLEFT |
| 7466 | cmdmsg_rl = save_cmdmsg_rl; |
| 7467 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7468 | |
| 7469 | State = save_State; |
| 7470 | # ifdef FEAT_MOUSE |
| 7471 | setmouse(); |
| 7472 | # endif |
| 7473 | |
| 7474 | return cmdwin_result; |
| 7475 | } |
| 7476 | #endif /* FEAT_CMDWIN */ |
| 7477 | |
| 7478 | /* |
| 7479 | * Used for commands that either take a simple command string argument, or: |
| 7480 | * cmd << endmarker |
| 7481 | * {script} |
| 7482 | * endmarker |
| 7483 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7484 | */ |
| 7485 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7486 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7487 | { |
| 7488 | char_u *theline; |
| 7489 | char *end_pattern = NULL; |
| 7490 | char dot[] = "."; |
| 7491 | garray_T ga; |
| 7492 | |
| 7493 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7494 | return NULL; |
| 7495 | |
| 7496 | ga_init2(&ga, 1, 0x400); |
| 7497 | |
| 7498 | if (cmd[2] != NUL) |
| 7499 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7500 | else |
| 7501 | end_pattern = dot; |
| 7502 | |
| 7503 | for (;;) |
| 7504 | { |
| 7505 | theline = eap->getline( |
| 7506 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7507 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7508 | #endif |
| 7509 | NUL, eap->cookie, 0); |
| 7510 | |
| 7511 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7512 | { |
| 7513 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7514 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7515 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | |
| 7517 | ga_concat(&ga, theline); |
| 7518 | ga_append(&ga, '\n'); |
| 7519 | vim_free(theline); |
| 7520 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7521 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7522 | |
| 7523 | return (char_u *)ga.ga_data; |
| 7524 | } |