Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_getln.c: Functions for entering and editing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 37b1556 | 2018-08-14 22:08:25 +0200 | [diff] [blame] | 16 | #ifndef MAX |
| 17 | # define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Variables shared between getcmdline(), redrawcmdline() and others. |
| 22 | * These need to be saved when using CTRL-R |, that's why they are in a |
| 23 | * structure. |
| 24 | */ |
| 25 | struct cmdline_info |
| 26 | { |
| 27 | char_u *cmdbuff; /* pointer to command line buffer */ |
| 28 | int cmdbufflen; /* length of cmdbuff */ |
| 29 | int cmdlen; /* number of chars in command line */ |
| 30 | int cmdpos; /* current cursor position */ |
| 31 | int cmdspos; /* cursor column on screen */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 32 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | int cmdindent; /* number of spaces before cmdline */ |
| 34 | char_u *cmdprompt; /* message in front of cmdline */ |
| 35 | int cmdattr; /* attributes for prompt */ |
| 36 | int overstrike; /* Typing mode on the command line. Shared by |
| 37 | getcmdline() and put_on_cmdline(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 38 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 39 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | int xp_context; /* type of expansion */ |
| 41 | # ifdef FEAT_EVAL |
| 42 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 43 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 44 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 47 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 48 | * used by other functions. When invoking getcmdline() recursively it needs |
| 49 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 50 | * TODO: make it local to getcmdline() and pass it around. */ |
| 51 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | |
| 53 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 54 | |
| 55 | #ifdef FEAT_EVAL |
| 56 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 57 | #endif |
| 58 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 59 | static int extra_char = NUL; /* extra character to display when redrawing |
| 60 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 61 | static int extra_char_shift; |
| 62 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | #ifdef FEAT_CMDHIST |
| 64 | typedef struct hist_entry |
| 65 | { |
| 66 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 67 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 69 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | } histentry_T; |
| 71 | |
| 72 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 73 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 74 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 75 | /* identifying (unique) number of newest history entry */ |
| 76 | static int hislen = 0; /* actual length of history tables */ |
| 77 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 78 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 80 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 82 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | # endif |
| 84 | #endif |
| 85 | |
| 86 | #ifdef FEAT_RIGHTLEFT |
| 87 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 88 | #endif |
| 89 | |
| 90 | #ifdef FEAT_FKMAP |
| 91 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 92 | #endif |
| 93 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 94 | static int cmdline_charsize(int idx); |
| 95 | static void set_cmdspos(void); |
| 96 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 98 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 100 | static void alloc_cmdbuff(int len); |
| 101 | static int realloc_cmdbuff(int len); |
| 102 | static void draw_cmdline(int start, int len); |
| 103 | static void save_cmdline(struct cmdline_info *ccp); |
| 104 | static void restore_cmdline(struct cmdline_info *ccp); |
| 105 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 107 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | #endif |
| 109 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 110 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 112 | static void redrawcmdprompt(void); |
| 113 | static void cursorcmd(void); |
| 114 | static int ccheck_abbr(int); |
| 115 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 116 | static void escape_fname(char_u **pp); |
| 117 | static int showmatches(expand_T *xp, int wildmenu); |
| 118 | static void set_expand_context(expand_T *xp); |
| 119 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 120 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 122 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 123 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 124 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 125 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 127 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 129 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 130 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | # endif |
| 132 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 133 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 134 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 135 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
| 137 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 138 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 141 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 142 | static int |
| 143 | #ifdef __BORLANDC__ |
| 144 | _RTLENTRYF |
| 145 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 146 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 150 | static void |
| 151 | trigger_cmd_autocmd(int typechar, int evt) |
| 152 | { |
| 153 | char_u typestr[2]; |
| 154 | |
| 155 | typestr[0] = typechar; |
| 156 | typestr[1] = NUL; |
| 157 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 158 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 161 | * Abandon the command line. |
| 162 | */ |
| 163 | static void |
| 164 | abandon_cmdline(void) |
| 165 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 166 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 167 | if (msg_scrolled == 0) |
| 168 | compute_cmdrow(); |
| 169 | MSG(""); |
| 170 | redraw_cmdline = TRUE; |
| 171 | } |
| 172 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 173 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 174 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 175 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 176 | * as a trailing \|, which can happen while typing a pattern. |
| 177 | */ |
| 178 | static int |
| 179 | empty_pattern(char_u *p) |
| 180 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 181 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 182 | |
| 183 | /* remove trailing \v and the like */ |
| 184 | while (n >= 2 && p[n - 2] == '\\' |
| 185 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 186 | n -= 2; |
| 187 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 188 | } |
| 189 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 190 | // Struct to store the viewstate during 'incsearch' highlighting. |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 191 | typedef struct { |
| 192 | colnr_T vs_curswant; |
| 193 | colnr_T vs_leftcol; |
| 194 | linenr_T vs_topline; |
| 195 | # ifdef FEAT_DIFF |
| 196 | int vs_topfill; |
| 197 | # endif |
| 198 | linenr_T vs_botline; |
| 199 | linenr_T vs_empty_rows; |
| 200 | } viewstate_T; |
| 201 | |
| 202 | static void |
| 203 | save_viewstate(viewstate_T *vs) |
| 204 | { |
| 205 | vs->vs_curswant = curwin->w_curswant; |
| 206 | vs->vs_leftcol = curwin->w_leftcol; |
| 207 | vs->vs_topline = curwin->w_topline; |
| 208 | # ifdef FEAT_DIFF |
| 209 | vs->vs_topfill = curwin->w_topfill; |
| 210 | # endif |
| 211 | vs->vs_botline = curwin->w_botline; |
| 212 | vs->vs_empty_rows = curwin->w_empty_rows; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | restore_viewstate(viewstate_T *vs) |
| 217 | { |
| 218 | curwin->w_curswant = vs->vs_curswant; |
| 219 | curwin->w_leftcol = vs->vs_leftcol; |
| 220 | curwin->w_topline = vs->vs_topline; |
| 221 | # ifdef FEAT_DIFF |
| 222 | curwin->w_topfill = vs->vs_topfill; |
| 223 | # endif |
| 224 | curwin->w_botline = vs->vs_botline; |
| 225 | curwin->w_empty_rows = vs->vs_empty_rows; |
| 226 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 227 | |
| 228 | // Struct to store the state of 'incsearch' highlighting. |
| 229 | typedef struct { |
| 230 | pos_T search_start; // where 'incsearch' starts searching |
| 231 | pos_T save_cursor; |
| 232 | viewstate_T init_viewstate; |
| 233 | viewstate_T old_viewstate; |
| 234 | pos_T match_start; |
| 235 | pos_T match_end; |
| 236 | int did_incsearch; |
| 237 | int incsearch_postponed; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 238 | int magic_save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 239 | } incsearch_state_T; |
| 240 | |
| 241 | static void |
| 242 | init_incsearch_state(incsearch_state_T *is_state) |
| 243 | { |
| 244 | is_state->match_start = curwin->w_cursor; |
| 245 | is_state->did_incsearch = FALSE; |
| 246 | is_state->incsearch_postponed = FALSE; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 247 | is_state->magic_save = p_magic; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 248 | CLEAR_POS(&is_state->match_end); |
| 249 | is_state->save_cursor = curwin->w_cursor; // may be restored later |
| 250 | is_state->search_start = curwin->w_cursor; |
| 251 | save_viewstate(&is_state->init_viewstate); |
| 252 | save_viewstate(&is_state->old_viewstate); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * First move cursor to end of match, then to the start. This |
| 257 | * moves the whole match onto the screen when 'nowrap' is set. |
| 258 | */ |
| 259 | static void |
| 260 | set_search_match(pos_T *t) |
| 261 | { |
| 262 | t->lnum += search_match_lines; |
| 263 | t->col = search_match_endcol; |
| 264 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 265 | { |
| 266 | t->lnum = curbuf->b_ml.ml_line_count; |
| 267 | coladvance((colnr_T)MAXCOL); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Return TRUE when 'incsearch' highlighting is to be done. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 273 | * Sets search_first_line and search_last_line to the address range. |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 274 | */ |
| 275 | static int |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 276 | do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, |
| 277 | int *skiplen, int *patlen) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 278 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 279 | *skiplen = 0; |
| 280 | *patlen = ccline.cmdlen; |
| 281 | |
| 282 | if (p_is && !cmd_silent) |
| 283 | { |
| 284 | // by default search all lines |
| 285 | search_first_line = 0; |
| 286 | search_last_line = MAXLNUM; |
| 287 | |
| 288 | if (firstc == '/' || firstc == '?') |
| 289 | return TRUE; |
| 290 | if (firstc == ':') |
| 291 | { |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 292 | char_u *cmd; |
| 293 | cmdmod_T save_cmdmod = cmdmod; |
| 294 | char_u *p; |
| 295 | int delim; |
| 296 | char_u *end; |
| 297 | char_u *dummy; |
| 298 | exarg_T ea; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 299 | |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 300 | vim_memset(&ea, 0, sizeof(ea)); |
| 301 | ea.line1 = 1; |
| 302 | ea.line2 = 1; |
| 303 | ea.cmd = ccline.cmdbuff; |
| 304 | ea.addr_type = ADDR_LINES; |
| 305 | |
| 306 | parse_command_modifiers(&ea, &dummy, TRUE); |
| 307 | cmdmod = save_cmdmod; |
| 308 | |
| 309 | cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 310 | if (*cmd == 's' || *cmd == 'g' || *cmd == 'v') |
| 311 | { |
| 312 | // Skip over "substitute" to find the pattern separator. |
| 313 | for (p = cmd; ASCII_ISALPHA(*p); ++p) |
| 314 | ; |
Bram Moolenaar | 2b926fc | 2018-08-13 11:07:57 +0200 | [diff] [blame] | 315 | if (*skipwhite(p) != NUL |
Bram Moolenaar | 21f990e | 2018-08-11 19:20:49 +0200 | [diff] [blame] | 316 | && (STRNCMP(cmd, "substitute", p - cmd) == 0 |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 317 | || STRNCMP(cmd, "smagic", p - cmd) == 0 |
| 318 | || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 |
Bram Moolenaar | 21f990e | 2018-08-11 19:20:49 +0200 | [diff] [blame] | 319 | || STRNCMP(cmd, "global", p - cmd) == 0 |
| 320 | || STRNCMP(cmd, "vglobal", p - cmd) == 0)) |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 321 | { |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +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 | |
Bram Moolenaar | def7b1d | 2018-08-13 22:54:35 +0200 | [diff] [blame] | 327 | // Check for "global!/". |
| 328 | if (*cmd == 'g' && *p == '!') |
| 329 | { |
| 330 | p++; |
| 331 | if (*skipwhite(p) == NUL) |
| 332 | return FALSE; |
| 333 | } |
Bram Moolenaar | 2b926fc | 2018-08-13 11:07:57 +0200 | [diff] [blame] | 334 | p = skipwhite(p); |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 335 | delim = *p++; |
| 336 | end = skip_regexp(p, delim, p_magic, NULL); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 337 | if (end > p || *end == delim) |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 338 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 339 | pos_T save_cursor = curwin->w_cursor; |
| 340 | |
| 341 | // found a non-empty pattern |
| 342 | *skiplen = (int)(p - ccline.cmdbuff); |
| 343 | *patlen = (int)(end - p); |
| 344 | |
| 345 | // parse the address range |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 346 | curwin->w_cursor = is_state->search_start; |
Bram Moolenaar | 976b847 | 2018-08-12 15:49:47 +0200 | [diff] [blame] | 347 | parse_cmd_address(&ea, &dummy); |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 348 | if (ea.addr_count > 0) |
| 349 | { |
Bram Moolenaar | 60d0871 | 2018-08-12 21:53:15 +0200 | [diff] [blame] | 350 | // Allow for reverse match. |
| 351 | if (ea.line2 < ea.line1) |
| 352 | { |
| 353 | search_first_line = ea.line2; |
| 354 | search_last_line = ea.line1; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | search_first_line = ea.line1; |
| 359 | search_last_line = ea.line2; |
| 360 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 361 | } |
| 362 | else if (*cmd == 's') |
| 363 | { |
| 364 | // :s defaults to the current line |
| 365 | search_first_line = curwin->w_cursor.lnum; |
| 366 | search_last_line = curwin->w_cursor.lnum; |
| 367 | } |
| 368 | |
| 369 | curwin->w_cursor = save_cursor; |
| 370 | return TRUE; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return FALSE; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 380 | static void |
| 381 | finish_incsearch_highlighting( |
| 382 | int gotesc, |
| 383 | incsearch_state_T *is_state, |
| 384 | int call_update_screen) |
| 385 | { |
| 386 | if (is_state->did_incsearch) |
| 387 | { |
| 388 | is_state->did_incsearch = FALSE; |
| 389 | if (gotesc) |
| 390 | curwin->w_cursor = is_state->save_cursor; |
| 391 | else |
| 392 | { |
| 393 | if (!EQUAL_POS(is_state->save_cursor, is_state->search_start)) |
| 394 | { |
| 395 | // put the '" mark at the original position |
| 396 | curwin->w_cursor = is_state->save_cursor; |
| 397 | setpcmark(); |
| 398 | } |
| 399 | curwin->w_cursor = is_state->search_start; |
| 400 | } |
| 401 | restore_viewstate(&is_state->old_viewstate); |
| 402 | highlight_match = FALSE; |
| 403 | validate_cursor(); /* needed for TAB */ |
| 404 | if (call_update_screen) |
| 405 | update_screen(SOME_VALID); |
| 406 | else |
| 407 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 408 | p_magic = is_state->magic_save; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 412 | /* |
| 413 | * Do 'incsearch' highlighting if desired. |
| 414 | */ |
| 415 | static void |
| 416 | may_do_incsearch_highlighting( |
| 417 | int firstc, |
| 418 | long count, |
| 419 | incsearch_state_T *is_state) |
| 420 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 421 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 422 | int i; |
| 423 | pos_T end_pos; |
| 424 | struct cmdline_info save_ccline; |
| 425 | #ifdef FEAT_RELTIME |
| 426 | proftime_T tm; |
| 427 | #endif |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 428 | int next_char; |
| 429 | int use_last_pat; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 430 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 431 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 432 | { |
| 433 | finish_incsearch_highlighting(FALSE, is_state, TRUE); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 434 | return; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 435 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 436 | |
| 437 | // If there is a character waiting, search and redraw later. |
| 438 | if (char_avail()) |
| 439 | { |
| 440 | is_state->incsearch_postponed = TRUE; |
| 441 | return; |
| 442 | } |
| 443 | is_state->incsearch_postponed = FALSE; |
| 444 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 445 | if (search_first_line == 0) |
| 446 | // start at the original cursor position |
| 447 | curwin->w_cursor = is_state->search_start; |
| 448 | else |
| 449 | { |
| 450 | // start at the first line in the range |
| 451 | curwin->w_cursor.lnum = search_first_line; |
| 452 | curwin->w_cursor.col = 0; |
| 453 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 454 | save_last_search_pattern(); |
| 455 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 456 | // Use the previous pattern for ":s//". |
| 457 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 458 | use_last_pat = patlen == 0 && skiplen > 0 |
| 459 | && ccline.cmdbuff[skiplen - 1] == next_char; |
| 460 | |
| 461 | // If there is no pattern, don't do anything. |
| 462 | if (patlen == 0 && !use_last_pat) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 463 | { |
| 464 | i = 0; |
| 465 | set_no_hlsearch(TRUE); // turn off previous highlight |
| 466 | redraw_all_later(SOME_VALID); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
| 471 | |
| 472 | cursor_off(); // so the user knows we're busy |
| 473 | out_flush(); |
| 474 | ++emsg_off; // so it doesn't beep if bad expr |
| 475 | #ifdef FEAT_RELTIME |
| 476 | // Set the time limit to half a second. |
| 477 | profile_setlimit(500L, &tm); |
| 478 | #endif |
| 479 | if (!p_hls) |
| 480 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 976b847 | 2018-08-12 15:49:47 +0200 | [diff] [blame] | 481 | if (search_first_line != 0) |
| 482 | search_flags += SEARCH_START; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 483 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 484 | i = do_search(NULL, firstc == ':' ? '/' : firstc, |
| 485 | ccline.cmdbuff + skiplen, count, search_flags, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 486 | #ifdef FEAT_RELTIME |
| 487 | &tm, NULL |
| 488 | #else |
| 489 | NULL, NULL |
| 490 | #endif |
| 491 | ); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 492 | ccline.cmdbuff[skiplen + patlen] = next_char; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 493 | --emsg_off; |
| 494 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 495 | if (curwin->w_cursor.lnum < search_first_line |
| 496 | || curwin->w_cursor.lnum > search_last_line) |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 497 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 498 | // match outside of address range |
| 499 | i = 0; |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 500 | curwin->w_cursor = is_state->search_start; |
| 501 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 502 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 503 | // if interrupted while searching, behave like it failed |
| 504 | if (got_int) |
| 505 | { |
| 506 | (void)vpeekc(); // remove <C-C> from input stream |
| 507 | got_int = FALSE; // don't abandon the command line |
| 508 | i = 0; |
| 509 | } |
| 510 | else if (char_avail()) |
| 511 | // cancelled searching because a char was typed |
| 512 | is_state->incsearch_postponed = TRUE; |
| 513 | } |
| 514 | if (i != 0) |
| 515 | highlight_match = TRUE; // highlight position |
| 516 | else |
| 517 | highlight_match = FALSE; // remove highlight |
| 518 | |
| 519 | // First restore the old curwin values, so the screen is positioned in the |
| 520 | // same way as the actual search command. |
| 521 | restore_viewstate(&is_state->old_viewstate); |
| 522 | changed_cline_bef_curs(); |
| 523 | update_topline(); |
| 524 | |
| 525 | if (i != 0) |
| 526 | { |
| 527 | pos_T save_pos = curwin->w_cursor; |
| 528 | |
| 529 | is_state->match_start = curwin->w_cursor; |
| 530 | set_search_match(&curwin->w_cursor); |
| 531 | validate_cursor(); |
| 532 | end_pos = curwin->w_cursor; |
| 533 | is_state->match_end = end_pos; |
| 534 | curwin->w_cursor = save_pos; |
| 535 | } |
| 536 | else |
| 537 | end_pos = curwin->w_cursor; // shutup gcc 4 |
| 538 | |
| 539 | // Disable 'hlsearch' highlighting if the pattern matches everything. |
| 540 | // Avoids a flash when typing "foo\|". |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 541 | if (!use_last_pat) |
| 542 | { |
| 543 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 544 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 545 | if (empty_pattern(ccline.cmdbuff)) |
| 546 | set_no_hlsearch(TRUE); |
| 547 | ccline.cmdbuff[skiplen + patlen] = next_char; |
| 548 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 549 | |
| 550 | validate_cursor(); |
| 551 | // May redraw the status line to show the cursor position. |
| 552 | if (p_ru && curwin->w_status_height > 0) |
| 553 | curwin->w_redr_status = TRUE; |
| 554 | |
| 555 | save_cmdline(&save_ccline); |
| 556 | update_screen(SOME_VALID); |
| 557 | restore_cmdline(&save_ccline); |
| 558 | restore_last_search_pattern(); |
| 559 | |
| 560 | // Leave it at the end to make CTRL-R CTRL-W work. |
| 561 | if (i != 0) |
| 562 | curwin->w_cursor = end_pos; |
| 563 | |
| 564 | msg_starthere(); |
| 565 | redrawcmdline(); |
| 566 | is_state->did_incsearch = TRUE; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next |
| 571 | * or previous match. |
| 572 | * Returns FAIL when jumping to cmdline_not_changed; |
| 573 | */ |
| 574 | static int |
| 575 | may_adjust_incsearch_highlighting( |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 576 | int firstc, |
| 577 | long count, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 578 | incsearch_state_T *is_state, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 579 | int c) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 580 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 581 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 582 | pos_T t; |
| 583 | char_u *pat; |
| 584 | int search_flags = SEARCH_NOOF; |
| 585 | int i; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 586 | int save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 587 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 588 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 589 | return OK; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 590 | if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 591 | return FAIL; |
| 592 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 593 | if (firstc == ccline.cmdbuff[skiplen]) |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 594 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 595 | pat = last_search_pattern(); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 596 | skiplen = 0; |
Bram Moolenaar | d7cc163 | 2018-08-14 20:18:26 +0200 | [diff] [blame] | 597 | patlen = (int)STRLEN(pat); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 598 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 599 | else |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 600 | pat = ccline.cmdbuff + skiplen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 601 | |
| 602 | save_last_search_pattern(); |
| 603 | cursor_off(); |
| 604 | out_flush(); |
| 605 | if (c == Ctrl_G) |
| 606 | { |
| 607 | t = is_state->match_end; |
| 608 | if (LT_POS(is_state->match_start, is_state->match_end)) |
| 609 | // Start searching at the end of the match not at the beginning of |
| 610 | // the next column. |
| 611 | (void)decl(&t); |
| 612 | search_flags += SEARCH_COL; |
| 613 | } |
| 614 | else |
| 615 | t = is_state->match_start; |
| 616 | if (!p_hls) |
| 617 | search_flags += SEARCH_KEEP; |
| 618 | ++emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 619 | save = pat[patlen]; |
| 620 | pat[patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 621 | i = searchit(curwin, curbuf, &t, |
| 622 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 623 | pat, count, search_flags, |
| 624 | RE_SEARCH, 0, NULL, NULL); |
| 625 | --emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 626 | pat[patlen] = save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 627 | if (i) |
| 628 | { |
| 629 | is_state->search_start = is_state->match_start; |
| 630 | is_state->match_end = t; |
| 631 | is_state->match_start = t; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 632 | if (c == Ctrl_T && firstc != '?') |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 633 | { |
| 634 | // Move just before the current match, so that when nv_search |
| 635 | // finishes the cursor will be put back on the match. |
| 636 | is_state->search_start = t; |
| 637 | (void)decl(&is_state->search_start); |
| 638 | } |
| 639 | else if (c == Ctrl_G && firstc == '?') |
| 640 | { |
| 641 | // Move just after the current match, so that when nv_search |
| 642 | // finishes the cursor will be put back on the match. |
| 643 | is_state->search_start = t; |
| 644 | (void)incl(&is_state->search_start); |
| 645 | } |
| 646 | if (LT_POS(t, is_state->search_start) && c == Ctrl_G) |
| 647 | { |
| 648 | // wrap around |
| 649 | is_state->search_start = t; |
| 650 | if (firstc == '?') |
| 651 | (void)incl(&is_state->search_start); |
| 652 | else |
| 653 | (void)decl(&is_state->search_start); |
| 654 | } |
| 655 | |
| 656 | set_search_match(&is_state->match_end); |
| 657 | curwin->w_cursor = is_state->match_start; |
| 658 | changed_cline_bef_curs(); |
| 659 | update_topline(); |
| 660 | validate_cursor(); |
| 661 | highlight_match = TRUE; |
| 662 | save_viewstate(&is_state->old_viewstate); |
| 663 | update_screen(NOT_VALID); |
| 664 | redrawcmdline(); |
| 665 | } |
| 666 | else |
| 667 | vim_beep(BO_ERROR); |
| 668 | restore_last_search_pattern(); |
| 669 | return FAIL; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * When CTRL-L typed: add character from the match to the pattern. |
| 674 | * May set "*c" to the added character. |
| 675 | * Return OK when jumping to cmdline_not_changed. |
| 676 | */ |
| 677 | static int |
| 678 | may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state) |
| 679 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 680 | int skiplen, patlen; |
| 681 | |
| 682 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 683 | return FAIL; |
| 684 | |
| 685 | // Add a character from under the cursor for 'incsearch'. |
| 686 | if (is_state->did_incsearch) |
| 687 | { |
| 688 | curwin->w_cursor = is_state->match_end; |
| 689 | if (!EQUAL_POS(curwin->w_cursor, is_state->search_start)) |
| 690 | { |
| 691 | *c = gchar_cursor(); |
| 692 | |
| 693 | // If 'ignorecase' and 'smartcase' are set and the |
| 694 | // command line has no uppercase characters, convert |
| 695 | // the character to lowercase. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 696 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 697 | *c = MB_TOLOWER(*c); |
| 698 | if (*c != NUL) |
| 699 | { |
| 700 | if (*c == firstc || vim_strchr((char_u *)( |
| 701 | p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) |
| 702 | { |
| 703 | // put a backslash before special characters |
| 704 | stuffcharReadbuff(*c); |
| 705 | *c = '\\'; |
| 706 | } |
| 707 | return FAIL; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | return OK; |
| 712 | } |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 713 | #endif |
| 714 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 715 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 716 | * getcmdline() - accept a command line starting with firstc. |
| 717 | * |
| 718 | * firstc == ':' get ":" command line. |
| 719 | * firstc == '/' or '?' get search pattern |
| 720 | * firstc == '=' get expression |
| 721 | * firstc == '@' get text for input() function |
| 722 | * firstc == '>' get text for debug mode |
| 723 | * firstc == NUL get text for :insert command |
| 724 | * firstc == -1 like NUL, and break on CTRL-C |
| 725 | * |
| 726 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 727 | * command line. |
| 728 | * |
| 729 | * Careful: getcmdline() can be called recursively! |
| 730 | * |
| 731 | * Return pointer to allocated string if there is a commandline, NULL |
| 732 | * otherwise. |
| 733 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 735 | getcmdline( |
| 736 | int firstc, |
| 737 | long count UNUSED, /* only used for incremental search */ |
| 738 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 739 | { |
| 740 | int c; |
| 741 | int i; |
| 742 | int j; |
| 743 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 744 | int do_abbr; /* when TRUE check for abbr. */ |
| 745 | #ifdef FEAT_CMDHIST |
| 746 | char_u *lookfor = NULL; /* string to match */ |
| 747 | int hiscnt; /* current history line in use */ |
| 748 | int histype; /* history type to be used */ |
| 749 | #endif |
| 750 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 751 | incsearch_state_T is_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | #endif |
| 753 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 754 | int wim_index = 0; /* index in wim_flags[] */ |
| 755 | int res; |
| 756 | int save_msg_scroll = msg_scroll; |
| 757 | int save_State = State; /* remember State when called */ |
| 758 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 759 | #ifdef FEAT_MOUSE |
| 760 | /* mouse drag and release events are ignored, unless they are |
| 761 | * preceded with a mouse down event */ |
| 762 | int ignore_drag_release = TRUE; |
| 763 | #endif |
| 764 | #ifdef FEAT_EVAL |
| 765 | int break_ctrl_c = FALSE; |
| 766 | #endif |
| 767 | expand_T xpc; |
| 768 | long *b_im_ptr = NULL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 769 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 770 | /* Everything that may work recursively should save and restore the |
| 771 | * current command line in save_ccline. That includes update_screen(), a |
| 772 | * custom status line may invoke ":normal". */ |
| 773 | struct cmdline_info save_ccline; |
| 774 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 775 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 776 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 777 | #ifdef FEAT_EVAL |
| 778 | if (firstc == -1) |
| 779 | { |
| 780 | firstc = NUL; |
| 781 | break_ctrl_c = TRUE; |
| 782 | } |
| 783 | #endif |
| 784 | #ifdef FEAT_RIGHTLEFT |
| 785 | /* start without Hebrew mapping for a command line */ |
| 786 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 787 | cmd_hkmap = 0; |
| 788 | #endif |
| 789 | |
| 790 | ccline.overstrike = FALSE; /* always start in insert mode */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 791 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 793 | init_incsearch_state(&is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | #endif |
| 795 | |
| 796 | /* |
| 797 | * set some variables for redrawcmd() |
| 798 | */ |
| 799 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 800 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 801 | |
| 802 | /* alloc initial ccline.cmdbuff */ |
| 803 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | if (ccline.cmdbuff == NULL) |
| 805 | return NULL; /* out of memory */ |
| 806 | ccline.cmdlen = ccline.cmdpos = 0; |
| 807 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 808 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 809 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 810 | /* autoindent for :insert and :append */ |
| 811 | if (firstc <= 0) |
| 812 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 813 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 814 | ccline.cmdbuff[indent] = NUL; |
| 815 | ccline.cmdpos = indent; |
| 816 | ccline.cmdspos = indent; |
| 817 | ccline.cmdlen = indent; |
| 818 | } |
| 819 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 820 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 821 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 822 | |
| 823 | #ifdef FEAT_RIGHTLEFT |
| 824 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 825 | && (firstc == '/' || firstc == '?')) |
| 826 | cmdmsg_rl = TRUE; |
| 827 | else |
| 828 | cmdmsg_rl = FALSE; |
| 829 | #endif |
| 830 | |
| 831 | redir_off = TRUE; /* don't redirect the typed command */ |
| 832 | if (!cmd_silent) |
| 833 | { |
| 834 | i = msg_scrolled; |
| 835 | msg_scrolled = 0; /* avoid wait_return message */ |
| 836 | gotocmdline(TRUE); |
| 837 | msg_scrolled += i; |
| 838 | redrawcmdprompt(); /* draw prompt or indent */ |
| 839 | set_cmdspos(); |
| 840 | } |
| 841 | xpc.xp_context = EXPAND_NOTHING; |
| 842 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 843 | #ifndef BACKSLASH_IN_FILENAME |
| 844 | xpc.xp_shell = FALSE; |
| 845 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 846 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 847 | #if defined(FEAT_EVAL) |
| 848 | if (ccline.input_fn) |
| 849 | { |
| 850 | xpc.xp_context = ccline.xp_context; |
| 851 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 852 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 853 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 854 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 855 | } |
| 856 | #endif |
| 857 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 858 | /* |
| 859 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 860 | * doing ":@0" when register 0 doesn't contain a CR. |
| 861 | */ |
| 862 | msg_scroll = FALSE; |
| 863 | |
| 864 | State = CMDLINE; |
| 865 | |
| 866 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 867 | { |
| 868 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 869 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 870 | b_im_ptr = &curbuf->b_p_iminsert; |
| 871 | else |
| 872 | b_im_ptr = &curbuf->b_p_imsearch; |
| 873 | if (*b_im_ptr == B_IMODE_LMAP) |
| 874 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 875 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 877 | #endif |
| 878 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 879 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 880 | else if (p_imcmdline) |
| 881 | im_set_active(TRUE); |
| 882 | #endif |
| 883 | |
| 884 | #ifdef FEAT_MOUSE |
| 885 | setmouse(); |
| 886 | #endif |
| 887 | #ifdef CURSOR_SHAPE |
| 888 | ui_cursor_shape(); /* may show different cursor shape */ |
| 889 | #endif |
| 890 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 891 | /* When inside an autocommand for writing "exiting" may be set and |
| 892 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 893 | settmode(TMODE_RAW); |
| 894 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 895 | /* Trigger CmdlineEnter autocommands. */ |
| 896 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 897 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 898 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 899 | #ifdef FEAT_CMDHIST |
| 900 | init_history(); |
| 901 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 902 | histype = hist_char2type(firstc); |
| 903 | #endif |
| 904 | |
| 905 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 906 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | #endif |
| 908 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 909 | /* If something above caused an error, reset the flags, we do want to type |
| 910 | * and execute commands. Display may be messed up a bit. */ |
| 911 | if (did_emsg) |
| 912 | redrawcmd(); |
| 913 | did_emsg = FALSE; |
| 914 | got_int = FALSE; |
| 915 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 916 | /* |
| 917 | * Collect the command string, handling editing keys. |
| 918 | */ |
| 919 | for (;;) |
| 920 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 921 | redir_off = TRUE; /* Don't redirect the typed command. |
| 922 | Repeated, because a ":redir" inside |
| 923 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | #ifdef USE_ON_FLY_SCROLL |
| 925 | dont_scroll = FALSE; /* allow scrolling here */ |
| 926 | #endif |
| 927 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 928 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 929 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 930 | that occurs while typing a command should |
| 931 | cause the command not to be executed. */ |
| 932 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 934 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 935 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 936 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 937 | do |
| 938 | { |
| 939 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 940 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 941 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | if (KeyTyped) |
| 943 | { |
| 944 | some_key_typed = TRUE; |
| 945 | #ifdef FEAT_RIGHTLEFT |
| 946 | if (cmd_hkmap) |
| 947 | c = hkmap(c); |
| 948 | # ifdef FEAT_FKMAP |
| 949 | if (cmd_fkmap) |
| 950 | c = cmdl_fkmap(c); |
| 951 | # endif |
| 952 | if (cmdmsg_rl && !KeyStuffed) |
| 953 | { |
| 954 | /* Invert horizontal movements and operations. Only when |
| 955 | * typed by the user directly, not when the result of a |
| 956 | * mapping. */ |
| 957 | switch (c) |
| 958 | { |
| 959 | case K_RIGHT: c = K_LEFT; break; |
| 960 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 961 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 962 | case K_LEFT: c = K_RIGHT; break; |
| 963 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 964 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 965 | } |
| 966 | } |
| 967 | #endif |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Ignore got_int when CTRL-C was typed here. |
| 972 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 973 | * ":g/pat/normal /pat" (without the <CR>). |
| 974 | * Don't ignore it for the input() function. |
| 975 | */ |
| 976 | if ((c == Ctrl_C |
| 977 | #ifdef UNIX |
| 978 | || c == intr_char |
| 979 | #endif |
| 980 | ) |
| 981 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 982 | && firstc != '@' |
| 983 | #endif |
| 984 | #ifdef FEAT_EVAL |
| 985 | && !break_ctrl_c |
| 986 | #endif |
| 987 | && !global_busy) |
| 988 | got_int = FALSE; |
| 989 | |
| 990 | #ifdef FEAT_CMDHIST |
| 991 | /* free old command line when finished moving around in the history |
| 992 | * list */ |
| 993 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 994 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 995 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 997 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 998 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1000 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | #endif |
| 1002 | |
| 1003 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1004 | * When there are matching completions to select <S-Tab> works like |
| 1005 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1006 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1007 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1008 | c = Ctrl_P; |
| 1009 | |
| 1010 | #ifdef FEAT_WILDMENU |
| 1011 | /* Special translations for 'wildmenu' */ |
| 1012 | if (did_wild_list && p_wmnu) |
| 1013 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1014 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1016 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | c = Ctrl_N; |
| 1018 | } |
| 1019 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 1020 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 1021 | && ccline.cmdpos > 1 |
| 1022 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 1023 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 1024 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 1025 | c = K_DOWN; |
| 1026 | #endif |
| 1027 | |
| 1028 | /* free expanded names when finished walking through matches */ |
| 1029 | if (xpc.xp_numfiles != -1 |
| 1030 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 1031 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 1032 | && c != Ctrl_L) |
| 1033 | { |
| 1034 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1035 | did_wild_list = FALSE; |
| 1036 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1037 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1038 | #endif |
| 1039 | xpc.xp_context = EXPAND_NOTHING; |
| 1040 | wim_index = 0; |
| 1041 | #ifdef FEAT_WILDMENU |
| 1042 | if (p_wmnu && wild_menu_showing != 0) |
| 1043 | { |
| 1044 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1045 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1046 | |
| 1047 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1048 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (wild_menu_showing == WM_SCROLLED) |
| 1051 | { |
| 1052 | /* Entered command line, move it up */ |
| 1053 | cmdline_row--; |
| 1054 | redrawcmd(); |
| 1055 | } |
| 1056 | else if (save_p_ls != -1) |
| 1057 | { |
| 1058 | /* restore 'laststatus' and 'winminheight' */ |
| 1059 | p_ls = save_p_ls; |
| 1060 | p_wmh = save_p_wmh; |
| 1061 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1062 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1063 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1064 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1065 | redrawcmd(); |
| 1066 | save_p_ls = -1; |
| 1067 | } |
| 1068 | else |
| 1069 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1070 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1071 | redraw_statuslines(); |
| 1072 | } |
| 1073 | KeyTyped = skt; |
| 1074 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1075 | if (ccline.input_fn) |
| 1076 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | } |
| 1078 | #endif |
| 1079 | } |
| 1080 | |
| 1081 | #ifdef FEAT_WILDMENU |
| 1082 | /* Special translations for 'wildmenu' */ |
| 1083 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 1084 | { |
| 1085 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1086 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 1087 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1089 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | { |
| 1091 | /* Hitting <Up>: Remove one submenu name in front of the |
| 1092 | * cursor */ |
| 1093 | int found = FALSE; |
| 1094 | |
| 1095 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1096 | i = 0; |
| 1097 | while (--j > 0) |
| 1098 | { |
| 1099 | /* check for start of menu name */ |
| 1100 | if (ccline.cmdbuff[j] == ' ' |
| 1101 | && ccline.cmdbuff[j - 1] != '\\') |
| 1102 | { |
| 1103 | i = j + 1; |
| 1104 | break; |
| 1105 | } |
| 1106 | /* check for start of submenu name */ |
| 1107 | if (ccline.cmdbuff[j] == '.' |
| 1108 | && ccline.cmdbuff[j - 1] != '\\') |
| 1109 | { |
| 1110 | if (found) |
| 1111 | { |
| 1112 | i = j + 1; |
| 1113 | break; |
| 1114 | } |
| 1115 | else |
| 1116 | found = TRUE; |
| 1117 | } |
| 1118 | } |
| 1119 | if (i > 0) |
| 1120 | cmdline_del(i); |
| 1121 | c = p_wc; |
| 1122 | xpc.xp_context = EXPAND_NOTHING; |
| 1123 | } |
| 1124 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1125 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1126 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1127 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1128 | { |
| 1129 | char_u upseg[5]; |
| 1130 | |
| 1131 | upseg[0] = PATHSEP; |
| 1132 | upseg[1] = '.'; |
| 1133 | upseg[2] = '.'; |
| 1134 | upseg[3] = PATHSEP; |
| 1135 | upseg[4] = NUL; |
| 1136 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1137 | if (c == K_DOWN |
| 1138 | && ccline.cmdpos > 0 |
| 1139 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 1140 | && (ccline.cmdpos < 3 |
| 1141 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1142 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 1143 | { |
| 1144 | /* go down a directory */ |
| 1145 | c = p_wc; |
| 1146 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1147 | 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] | 1148 | { |
| 1149 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 1150 | int found = FALSE; |
| 1151 | |
| 1152 | j = ccline.cmdpos; |
| 1153 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1154 | while (--j > i) |
| 1155 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1156 | #ifdef FEAT_MBYTE |
| 1157 | if (has_mbyte) |
| 1158 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1159 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1160 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 1161 | { |
| 1162 | found = TRUE; |
| 1163 | break; |
| 1164 | } |
| 1165 | } |
| 1166 | if (found |
| 1167 | && ccline.cmdbuff[j - 1] == '.' |
| 1168 | && ccline.cmdbuff[j - 2] == '.' |
| 1169 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 1170 | { |
| 1171 | cmdline_del(j - 2); |
| 1172 | c = p_wc; |
| 1173 | } |
| 1174 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1175 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1176 | { |
| 1177 | /* go up a directory */ |
| 1178 | int found = FALSE; |
| 1179 | |
| 1180 | j = ccline.cmdpos - 1; |
| 1181 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1182 | while (--j > i) |
| 1183 | { |
| 1184 | #ifdef FEAT_MBYTE |
| 1185 | if (has_mbyte) |
| 1186 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1187 | #endif |
| 1188 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 1189 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1190 | && vim_strchr((char_u *)" *?[{`$%#", |
| 1191 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1192 | #endif |
| 1193 | ) |
| 1194 | { |
| 1195 | if (found) |
| 1196 | { |
| 1197 | i = j + 1; |
| 1198 | break; |
| 1199 | } |
| 1200 | else |
| 1201 | found = TRUE; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | if (!found) |
| 1206 | j = i; |
| 1207 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 1208 | j += 4; |
| 1209 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 1210 | && j == i) |
| 1211 | j += 3; |
| 1212 | else |
| 1213 | j = 0; |
| 1214 | if (j > 0) |
| 1215 | { |
| 1216 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 1217 | * machine-specific stuff here and in upseg init */ |
| 1218 | cmdline_del(j); |
| 1219 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 1220 | } |
| 1221 | else if (ccline.cmdpos > i) |
| 1222 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1223 | |
| 1224 | /* Now complete in the new directory. Set KeyTyped in case the |
| 1225 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1226 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1227 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | |
| 1231 | #endif /* FEAT_WILDMENU */ |
| 1232 | |
| 1233 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 1234 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 1235 | if (c == Ctrl_BSL) |
| 1236 | { |
| 1237 | ++no_mapping; |
| 1238 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1239 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | --no_mapping; |
| 1241 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 1242 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 1243 | * is in a mapping. */ |
| 1244 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 1245 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | { |
| 1247 | vungetc(c); |
| 1248 | c = Ctrl_BSL; |
| 1249 | } |
| 1250 | #ifdef FEAT_EVAL |
| 1251 | else if (c == 'e') |
| 1252 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 1253 | char_u *p = NULL; |
| 1254 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | |
| 1256 | /* |
| 1257 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1258 | * Need to save and restore the current command line, to be |
| 1259 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1260 | */ |
| 1261 | if (ccline.cmdpos == ccline.cmdlen) |
| 1262 | new_cmdpos = 99999; /* keep it at the end */ |
| 1263 | else |
| 1264 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1265 | |
| 1266 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1268 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | if (c == '=') |
| 1270 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1271 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1272 | * to avoid nasty things like going to another buffer when |
| 1273 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1274 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1275 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1276 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1277 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1278 | restore_cmdline(&save_ccline); |
| 1279 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1280 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1282 | len = (int)STRLEN(p); |
| 1283 | if (realloc_cmdbuff(len + 1) == OK) |
| 1284 | { |
| 1285 | ccline.cmdlen = len; |
| 1286 | STRCPY(ccline.cmdbuff, p); |
| 1287 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1288 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1289 | /* Restore the cursor or use the position set with |
| 1290 | * set_cmdline_pos(). */ |
| 1291 | if (new_cmdpos > ccline.cmdlen) |
| 1292 | ccline.cmdpos = ccline.cmdlen; |
| 1293 | else |
| 1294 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1295 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1296 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1297 | redrawcmd(); |
| 1298 | goto cmdline_changed; |
| 1299 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 1303 | got_int = FALSE; /* don't abandon the command line */ |
| 1304 | did_emsg = FALSE; |
| 1305 | emsg_on_display = FALSE; |
| 1306 | redrawcmd(); |
| 1307 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | } |
| 1309 | #endif |
| 1310 | else |
| 1311 | { |
| 1312 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 1313 | restart_edit = 'a'; |
| 1314 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 1315 | in history */ |
| 1316 | goto returncmd; /* back to Normal mode */ |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | #ifdef FEAT_CMDWIN |
| 1321 | if (c == cedit_key || c == K_CMDWIN) |
| 1322 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1323 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 1324 | { |
| 1325 | /* |
| 1326 | * Open a window to edit the command line (and history). |
| 1327 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1328 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1329 | some_key_typed = TRUE; |
| 1330 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | } |
| 1332 | # ifdef FEAT_DIGRAPHS |
| 1333 | else |
| 1334 | # endif |
| 1335 | #endif |
| 1336 | #ifdef FEAT_DIGRAPHS |
| 1337 | c = do_digraph(c); |
| 1338 | #endif |
| 1339 | |
| 1340 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 1341 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 1342 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1343 | /* In Ex mode a backslash escapes a newline. */ |
| 1344 | if (exmode_active |
| 1345 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1346 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1347 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1348 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1349 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1350 | if (c == K_KENTER) |
| 1351 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1353 | else |
| 1354 | { |
| 1355 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 1356 | truncate the cmdline now. */ |
| 1357 | if (ccheck_abbr(c + ABBR_OFF)) |
| 1358 | goto cmdline_changed; |
| 1359 | if (!cmd_silent) |
| 1360 | { |
| 1361 | windgoto(msg_row, 0); |
| 1362 | out_flush(); |
| 1363 | } |
| 1364 | break; |
| 1365 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * Completion for 'wildchar' or 'wildcharm' key. |
| 1370 | * - hitting <ESC> twice means: abandon command line. |
| 1371 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 1372 | * typed, not when it comes from a macro |
| 1373 | */ |
| 1374 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 1375 | { |
| 1376 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 1377 | { |
| 1378 | /* if 'wildmode' contains "list" may still need to list */ |
| 1379 | if (xpc.xp_numfiles > 1 |
| 1380 | && !did_wild_list |
| 1381 | && (wim_flags[wim_index] & WIM_LIST)) |
| 1382 | { |
| 1383 | (void)showmatches(&xpc, FALSE); |
| 1384 | redrawcmd(); |
| 1385 | did_wild_list = TRUE; |
| 1386 | } |
| 1387 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1388 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1389 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1391 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1392 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1393 | else |
| 1394 | res = OK; /* don't insert 'wildchar' now */ |
| 1395 | } |
| 1396 | else /* typed p_wc first time */ |
| 1397 | { |
| 1398 | wim_index = 0; |
| 1399 | j = ccline.cmdpos; |
| 1400 | /* if 'wildmode' first contains "longest", get longest |
| 1401 | * common part */ |
| 1402 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1403 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1404 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1406 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 1407 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1408 | |
| 1409 | /* if interrupted while completing, behave like it failed */ |
| 1410 | if (got_int) |
| 1411 | { |
| 1412 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1413 | got_int = FALSE; /* don't abandon the command line */ |
| 1414 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1415 | #ifdef FEAT_WILDMENU |
| 1416 | xpc.xp_context = EXPAND_NOTHING; |
| 1417 | #endif |
| 1418 | goto cmdline_changed; |
| 1419 | } |
| 1420 | |
| 1421 | /* when more than one match, and 'wildmode' first contains |
| 1422 | * "list", or no change and 'wildmode' contains "longest,list", |
| 1423 | * list all matches */ |
| 1424 | if (res == OK && xpc.xp_numfiles > 1) |
| 1425 | { |
| 1426 | /* a "longest" that didn't do anything is skipped (but not |
| 1427 | * "list:longest") */ |
| 1428 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 1429 | wim_index = 1; |
| 1430 | if ((wim_flags[wim_index] & WIM_LIST) |
| 1431 | #ifdef FEAT_WILDMENU |
| 1432 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 1433 | #endif |
| 1434 | ) |
| 1435 | { |
| 1436 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 1437 | { |
| 1438 | #ifdef FEAT_WILDMENU |
| 1439 | int p_wmnu_save = p_wmnu; |
| 1440 | p_wmnu = 0; |
| 1441 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1442 | /* remove match */ |
| 1443 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1444 | #ifdef FEAT_WILDMENU |
| 1445 | p_wmnu = p_wmnu_save; |
| 1446 | #endif |
| 1447 | } |
| 1448 | #ifdef FEAT_WILDMENU |
| 1449 | (void)showmatches(&xpc, p_wmnu |
| 1450 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 1451 | #else |
| 1452 | (void)showmatches(&xpc, FALSE); |
| 1453 | #endif |
| 1454 | redrawcmd(); |
| 1455 | did_wild_list = TRUE; |
| 1456 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1457 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1458 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1459 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1460 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1461 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | } |
| 1463 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1464 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1465 | } |
| 1466 | #ifdef FEAT_WILDMENU |
| 1467 | else if (xpc.xp_numfiles == -1) |
| 1468 | xpc.xp_context = EXPAND_NOTHING; |
| 1469 | #endif |
| 1470 | } |
| 1471 | if (wim_index < 3) |
| 1472 | ++wim_index; |
| 1473 | if (c == ESC) |
| 1474 | gotesc = TRUE; |
| 1475 | if (res == OK) |
| 1476 | goto cmdline_changed; |
| 1477 | } |
| 1478 | |
| 1479 | gotesc = FALSE; |
| 1480 | |
| 1481 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 1482 | if (c == K_S_TAB && KeyTyped) |
| 1483 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1484 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 1485 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 1486 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1487 | goto cmdline_changed; |
| 1488 | } |
| 1489 | |
| 1490 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 1491 | c = NL; |
| 1492 | |
| 1493 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1494 | |
| 1495 | /* |
| 1496 | * Big switch for a typed command line character. |
| 1497 | */ |
| 1498 | switch (c) |
| 1499 | { |
| 1500 | case K_BS: |
| 1501 | case Ctrl_H: |
| 1502 | case K_DEL: |
| 1503 | case K_KDEL: |
| 1504 | case Ctrl_W: |
| 1505 | #ifdef FEAT_FKMAP |
| 1506 | if (cmd_fkmap && c == K_BS) |
| 1507 | c = K_DEL; |
| 1508 | #endif |
| 1509 | if (c == K_KDEL) |
| 1510 | c = K_DEL; |
| 1511 | |
| 1512 | /* |
| 1513 | * delete current character is the same as backspace on next |
| 1514 | * character, except at end of line |
| 1515 | */ |
| 1516 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1517 | ++ccline.cmdpos; |
| 1518 | #ifdef FEAT_MBYTE |
| 1519 | if (has_mbyte && c == K_DEL) |
| 1520 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1521 | ccline.cmdbuff + ccline.cmdpos); |
| 1522 | #endif |
| 1523 | if (ccline.cmdpos > 0) |
| 1524 | { |
| 1525 | char_u *p; |
| 1526 | |
| 1527 | j = ccline.cmdpos; |
| 1528 | p = ccline.cmdbuff + j; |
| 1529 | #ifdef FEAT_MBYTE |
| 1530 | if (has_mbyte) |
| 1531 | { |
| 1532 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1533 | if (c == Ctrl_W) |
| 1534 | { |
| 1535 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1536 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1537 | i = mb_get_class(p); |
| 1538 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1539 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1540 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1541 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | else |
| 1545 | #endif |
| 1546 | if (c == Ctrl_W) |
| 1547 | { |
| 1548 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1549 | --p; |
| 1550 | i = vim_iswordc(p[-1]); |
| 1551 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1552 | && vim_iswordc(p[-1]) == i) |
| 1553 | --p; |
| 1554 | } |
| 1555 | else |
| 1556 | --p; |
| 1557 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1558 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1559 | i = ccline.cmdpos; |
| 1560 | while (i < ccline.cmdlen) |
| 1561 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1562 | |
| 1563 | /* Truncate at the end, required for multi-byte chars. */ |
| 1564 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1565 | #ifdef FEAT_SEARCH_EXTRA |
| 1566 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1567 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1568 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1569 | /* save view settings, so that the screen |
| 1570 | * won't be restored at the wrong position */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1571 | is_state.old_viewstate = is_state.init_viewstate; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1572 | } |
| 1573 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1574 | redrawcmd(); |
| 1575 | } |
| 1576 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1577 | && ccline.cmdprompt == NULL && indent == 0) |
| 1578 | { |
| 1579 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1580 | if (exmode_active |
| 1581 | #ifdef FEAT_EVAL |
| 1582 | || ccline.cmdfirstc == '>' |
| 1583 | #endif |
| 1584 | ) |
| 1585 | goto cmdline_not_changed; |
| 1586 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1587 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1588 | if (!cmd_silent) |
| 1589 | { |
| 1590 | #ifdef FEAT_RIGHTLEFT |
| 1591 | if (cmdmsg_rl) |
| 1592 | msg_col = Columns; |
| 1593 | else |
| 1594 | #endif |
| 1595 | msg_col = 0; |
| 1596 | msg_putchar(' '); /* delete ':' */ |
| 1597 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1598 | #ifdef FEAT_SEARCH_EXTRA |
| 1599 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1600 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1601 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1602 | redraw_cmdline = TRUE; |
| 1603 | goto returncmd; /* back to cmd mode */ |
| 1604 | } |
| 1605 | goto cmdline_changed; |
| 1606 | |
| 1607 | case K_INS: |
| 1608 | case K_KINS: |
| 1609 | #ifdef FEAT_FKMAP |
| 1610 | /* if Farsi mode set, we are in reverse insert mode - |
| 1611 | Do not change the mode */ |
| 1612 | if (cmd_fkmap) |
| 1613 | beep_flush(); |
| 1614 | else |
| 1615 | #endif |
| 1616 | ccline.overstrike = !ccline.overstrike; |
| 1617 | #ifdef CURSOR_SHAPE |
| 1618 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1619 | #endif |
| 1620 | goto cmdline_not_changed; |
| 1621 | |
| 1622 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1623 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1624 | { |
| 1625 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1626 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1627 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | im_set_active(FALSE); /* Disable input method */ |
| 1629 | #endif |
| 1630 | if (b_im_ptr != NULL) |
| 1631 | { |
| 1632 | if (State & LANGMAP) |
| 1633 | *b_im_ptr = B_IMODE_LMAP; |
| 1634 | else |
| 1635 | *b_im_ptr = B_IMODE_NONE; |
| 1636 | } |
| 1637 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1638 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1639 | else |
| 1640 | { |
| 1641 | /* There are no ":lmap" mappings, toggle IM. When |
| 1642 | * 'imdisable' is set don't try getting the status, it's |
| 1643 | * always off. */ |
| 1644 | if ((p_imdisable && b_im_ptr != NULL) |
| 1645 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1646 | { |
| 1647 | im_set_active(FALSE); /* Disable input method */ |
| 1648 | if (b_im_ptr != NULL) |
| 1649 | *b_im_ptr = B_IMODE_NONE; |
| 1650 | } |
| 1651 | else |
| 1652 | { |
| 1653 | im_set_active(TRUE); /* Enable input method */ |
| 1654 | if (b_im_ptr != NULL) |
| 1655 | *b_im_ptr = B_IMODE_IM; |
| 1656 | } |
| 1657 | } |
| 1658 | #endif |
| 1659 | if (b_im_ptr != NULL) |
| 1660 | { |
| 1661 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1662 | set_iminsert_global(); |
| 1663 | else |
| 1664 | set_imsearch_global(); |
| 1665 | } |
| 1666 | #ifdef CURSOR_SHAPE |
| 1667 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1668 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1669 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1671 | status_redraw_curbuf(); |
| 1672 | #endif |
| 1673 | goto cmdline_not_changed; |
| 1674 | |
| 1675 | /* case '@': only in very old vi */ |
| 1676 | case Ctrl_U: |
| 1677 | /* delete all characters left of the cursor */ |
| 1678 | j = ccline.cmdpos; |
| 1679 | ccline.cmdlen -= j; |
| 1680 | i = ccline.cmdpos = 0; |
| 1681 | while (i < ccline.cmdlen) |
| 1682 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1683 | /* Truncate at the end, required for multi-byte chars. */ |
| 1684 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1685 | #ifdef FEAT_SEARCH_EXTRA |
| 1686 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1687 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1688 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | redrawcmd(); |
| 1690 | goto cmdline_changed; |
| 1691 | |
| 1692 | #ifdef FEAT_CLIPBOARD |
| 1693 | case Ctrl_Y: |
| 1694 | /* Copy the modeless selection, if there is one. */ |
| 1695 | if (clip_star.state != SELECT_CLEARED) |
| 1696 | { |
| 1697 | if (clip_star.state == SELECT_DONE) |
| 1698 | clip_copy_modeless_selection(TRUE); |
| 1699 | goto cmdline_not_changed; |
| 1700 | } |
| 1701 | break; |
| 1702 | #endif |
| 1703 | |
| 1704 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1705 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1706 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1707 | * ":normal" runs out of characters. */ |
| 1708 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1709 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1710 | goto cmdline_not_changed; |
| 1711 | |
| 1712 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1713 | putting it in history */ |
| 1714 | goto returncmd; /* back to cmd mode */ |
| 1715 | |
| 1716 | case Ctrl_R: /* insert register */ |
| 1717 | #ifdef USE_ON_FLY_SCROLL |
| 1718 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1719 | #endif |
| 1720 | putcmdline('"', TRUE); |
| 1721 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1722 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1723 | if (i == Ctrl_O) |
| 1724 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1725 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1726 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1727 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1728 | --no_mapping; |
| 1729 | #ifdef FEAT_EVAL |
| 1730 | /* |
| 1731 | * Insert the result of an expression. |
| 1732 | * Need to save the current command line, to be able to enter |
| 1733 | * a new one... |
| 1734 | */ |
| 1735 | new_cmdpos = -1; |
| 1736 | if (c == '=') |
| 1737 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1739 | { |
| 1740 | beep_flush(); |
| 1741 | c = ESC; |
| 1742 | } |
| 1743 | else |
| 1744 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1745 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1746 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1747 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | #endif |
| 1751 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1752 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1753 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1754 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1755 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1756 | /* When there was a serious error abort getting the |
| 1757 | * command line. */ |
| 1758 | if (aborting()) |
| 1759 | { |
| 1760 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1761 | putting it in history */ |
| 1762 | goto returncmd; /* back to cmd mode */ |
| 1763 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1764 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1765 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1766 | #ifdef FEAT_EVAL |
| 1767 | if (new_cmdpos >= 0) |
| 1768 | { |
| 1769 | /* set_cmdline_pos() was used */ |
| 1770 | if (new_cmdpos > ccline.cmdlen) |
| 1771 | ccline.cmdpos = ccline.cmdlen; |
| 1772 | else |
| 1773 | ccline.cmdpos = new_cmdpos; |
| 1774 | } |
| 1775 | #endif |
| 1776 | } |
| 1777 | redrawcmd(); |
| 1778 | goto cmdline_changed; |
| 1779 | |
| 1780 | case Ctrl_D: |
| 1781 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1782 | break; /* Use ^D as normal char instead */ |
| 1783 | |
| 1784 | redrawcmd(); |
| 1785 | continue; /* don't do incremental search now */ |
| 1786 | |
| 1787 | case K_RIGHT: |
| 1788 | case K_S_RIGHT: |
| 1789 | case K_C_RIGHT: |
| 1790 | do |
| 1791 | { |
| 1792 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1793 | break; |
| 1794 | i = cmdline_charsize(ccline.cmdpos); |
| 1795 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1796 | break; |
| 1797 | ccline.cmdspos += i; |
| 1798 | #ifdef FEAT_MBYTE |
| 1799 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1800 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | + ccline.cmdpos); |
| 1802 | else |
| 1803 | #endif |
| 1804 | ++ccline.cmdpos; |
| 1805 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1806 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1807 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1808 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1809 | #ifdef FEAT_MBYTE |
| 1810 | if (has_mbyte) |
| 1811 | set_cmdspos_cursor(); |
| 1812 | #endif |
| 1813 | goto cmdline_not_changed; |
| 1814 | |
| 1815 | case K_LEFT: |
| 1816 | case K_S_LEFT: |
| 1817 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1818 | if (ccline.cmdpos == 0) |
| 1819 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1820 | do |
| 1821 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | --ccline.cmdpos; |
| 1823 | #ifdef FEAT_MBYTE |
| 1824 | if (has_mbyte) /* move to first byte of char */ |
| 1825 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1826 | ccline.cmdbuff + ccline.cmdpos); |
| 1827 | #endif |
| 1828 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1829 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1830 | while (ccline.cmdpos > 0 |
| 1831 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1832 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1834 | #ifdef FEAT_MBYTE |
| 1835 | if (has_mbyte) |
| 1836 | set_cmdspos_cursor(); |
| 1837 | #endif |
| 1838 | goto cmdline_not_changed; |
| 1839 | |
| 1840 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1841 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1842 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1843 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1844 | #ifdef FEAT_GUI_W32 |
| 1845 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1846 | * cancelled. */ |
| 1847 | case K_F4: |
| 1848 | if (mod_mask == MOD_MASK_ALT) |
| 1849 | { |
| 1850 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1851 | goto cmdline_not_changed; |
| 1852 | } |
| 1853 | break; |
| 1854 | #endif |
| 1855 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | #ifdef FEAT_MOUSE |
| 1857 | case K_MIDDLEDRAG: |
| 1858 | case K_MIDDLERELEASE: |
| 1859 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1860 | |
| 1861 | case K_MIDDLEMOUSE: |
| 1862 | # ifdef FEAT_GUI |
| 1863 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1864 | if (!gui.in_use) |
| 1865 | # endif |
| 1866 | if (!mouse_has(MOUSE_COMMAND)) |
| 1867 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1868 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1869 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1870 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1871 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1872 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1873 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | redrawcmd(); |
| 1875 | goto cmdline_changed; |
| 1876 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1877 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1878 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1879 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1880 | redrawcmd(); |
| 1881 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1882 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1883 | |
| 1884 | case K_LEFTDRAG: |
| 1885 | case K_LEFTRELEASE: |
| 1886 | case K_RIGHTDRAG: |
| 1887 | case K_RIGHTRELEASE: |
| 1888 | /* Ignore drag and release events when the button-down wasn't |
| 1889 | * seen before. */ |
| 1890 | if (ignore_drag_release) |
| 1891 | goto cmdline_not_changed; |
| 1892 | /* FALLTHROUGH */ |
| 1893 | case K_LEFTMOUSE: |
| 1894 | case K_RIGHTMOUSE: |
| 1895 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1896 | ignore_drag_release = TRUE; |
| 1897 | else |
| 1898 | ignore_drag_release = FALSE; |
| 1899 | # ifdef FEAT_GUI |
| 1900 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1901 | if (!gui.in_use) |
| 1902 | # endif |
| 1903 | if (!mouse_has(MOUSE_COMMAND)) |
| 1904 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1905 | # ifdef FEAT_CLIPBOARD |
| 1906 | if (mouse_row < cmdline_row && clip_star.available) |
| 1907 | { |
| 1908 | int button, is_click, is_drag; |
| 1909 | |
| 1910 | /* |
| 1911 | * Handle modeless selection. |
| 1912 | */ |
| 1913 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1914 | &is_click, &is_drag); |
| 1915 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1916 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1917 | { |
| 1918 | /* Translate shift-left to right button. */ |
| 1919 | button = MOUSE_RIGHT; |
| 1920 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1921 | } |
| 1922 | clip_modeless(button, is_click, is_drag); |
| 1923 | goto cmdline_not_changed; |
| 1924 | } |
| 1925 | # endif |
| 1926 | |
| 1927 | set_cmdspos(); |
| 1928 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1929 | ++ccline.cmdpos) |
| 1930 | { |
| 1931 | i = cmdline_charsize(ccline.cmdpos); |
| 1932 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1933 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1934 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1935 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1936 | if (has_mbyte) |
| 1937 | { |
| 1938 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1939 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1940 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1941 | + ccline.cmdpos) - 1; |
| 1942 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1943 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1944 | ccline.cmdspos += i; |
| 1945 | } |
| 1946 | goto cmdline_not_changed; |
| 1947 | |
| 1948 | /* Mouse scroll wheel: ignored here */ |
| 1949 | case K_MOUSEDOWN: |
| 1950 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1951 | case K_MOUSELEFT: |
| 1952 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | /* Alternate buttons ignored here */ |
| 1954 | case K_X1MOUSE: |
| 1955 | case K_X1DRAG: |
| 1956 | case K_X1RELEASE: |
| 1957 | case K_X2MOUSE: |
| 1958 | case K_X2DRAG: |
| 1959 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1960 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | goto cmdline_not_changed; |
| 1962 | |
| 1963 | #endif /* FEAT_MOUSE */ |
| 1964 | |
| 1965 | #ifdef FEAT_GUI |
| 1966 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1967 | case K_LEFTRELEASE_NM: |
| 1968 | goto cmdline_not_changed; |
| 1969 | |
| 1970 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1971 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | { |
| 1973 | gui_do_scroll(); |
| 1974 | redrawcmd(); |
| 1975 | } |
| 1976 | goto cmdline_not_changed; |
| 1977 | |
| 1978 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1979 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1980 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1981 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | redrawcmd(); |
| 1983 | } |
| 1984 | goto cmdline_not_changed; |
| 1985 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1986 | #ifdef FEAT_GUI_TABLINE |
| 1987 | case K_TABLINE: |
| 1988 | case K_TABMENU: |
| 1989 | /* Don't want to change any tabs here. Make sure the same tab |
| 1990 | * is still selected. */ |
| 1991 | if (gui_use_tabline()) |
| 1992 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1993 | goto cmdline_not_changed; |
| 1994 | #endif |
| 1995 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1996 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1997 | goto cmdline_not_changed; |
| 1998 | |
| 1999 | case Ctrl_B: /* begin of command line */ |
| 2000 | case K_HOME: |
| 2001 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2002 | case K_S_HOME: |
| 2003 | case K_C_HOME: |
| 2004 | ccline.cmdpos = 0; |
| 2005 | set_cmdspos(); |
| 2006 | goto cmdline_not_changed; |
| 2007 | |
| 2008 | case Ctrl_E: /* end of command line */ |
| 2009 | case K_END: |
| 2010 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | case K_S_END: |
| 2012 | case K_C_END: |
| 2013 | ccline.cmdpos = ccline.cmdlen; |
| 2014 | set_cmdspos_cursor(); |
| 2015 | goto cmdline_not_changed; |
| 2016 | |
| 2017 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2018 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2019 | break; |
| 2020 | goto cmdline_changed; |
| 2021 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2022 | case Ctrl_L: |
| 2023 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2024 | if (may_add_char_to_search(firstc, &c, &is_state) == OK) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2025 | goto cmdline_not_changed; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2026 | #endif |
| 2027 | |
| 2028 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2029 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | goto cmdline_changed; |
| 2032 | |
| 2033 | case Ctrl_N: /* next match */ |
| 2034 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 2035 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2037 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 2038 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2040 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2041 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 2043 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | case K_UP: |
| 2045 | case K_DOWN: |
| 2046 | case K_S_UP: |
| 2047 | case K_S_DOWN: |
| 2048 | case K_PAGEUP: |
| 2049 | case K_KPAGEUP: |
| 2050 | case K_PAGEDOWN: |
| 2051 | case K_KPAGEDOWN: |
| 2052 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 2053 | goto cmdline_not_changed; |
| 2054 | |
| 2055 | i = hiscnt; |
| 2056 | |
| 2057 | /* save current command string so it can be restored later */ |
| 2058 | if (lookfor == NULL) |
| 2059 | { |
| 2060 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 2061 | goto cmdline_not_changed; |
| 2062 | lookfor[ccline.cmdpos] = NUL; |
| 2063 | } |
| 2064 | |
| 2065 | j = (int)STRLEN(lookfor); |
| 2066 | for (;;) |
| 2067 | { |
| 2068 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2069 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2070 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | { |
| 2072 | if (hiscnt == hislen) /* first time */ |
| 2073 | hiscnt = hisidx[histype]; |
| 2074 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 2075 | hiscnt = hislen - 1; |
| 2076 | else if (hiscnt != hisidx[histype] + 1) |
| 2077 | --hiscnt; |
| 2078 | else /* at top of list */ |
| 2079 | { |
| 2080 | hiscnt = i; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | else /* one step forwards */ |
| 2085 | { |
| 2086 | /* on last entry, clear the line */ |
| 2087 | if (hiscnt == hisidx[histype]) |
| 2088 | { |
| 2089 | hiscnt = hislen; |
| 2090 | break; |
| 2091 | } |
| 2092 | |
| 2093 | /* not on a history line, nothing to do */ |
| 2094 | if (hiscnt == hislen) |
| 2095 | break; |
| 2096 | if (hiscnt == hislen - 1) /* wrap around */ |
| 2097 | hiscnt = 0; |
| 2098 | else |
| 2099 | ++hiscnt; |
| 2100 | } |
| 2101 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 2102 | { |
| 2103 | hiscnt = i; |
| 2104 | break; |
| 2105 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2106 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2107 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2108 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 2109 | lookfor, (size_t)j) == 0) |
| 2110 | break; |
| 2111 | } |
| 2112 | |
| 2113 | if (hiscnt != i) /* jumped to other entry */ |
| 2114 | { |
| 2115 | char_u *p; |
| 2116 | int len; |
| 2117 | int old_firstc; |
| 2118 | |
| 2119 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2120 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2121 | if (hiscnt == hislen) |
| 2122 | p = lookfor; /* back to the old one */ |
| 2123 | else |
| 2124 | p = history[histype][hiscnt].hisstr; |
| 2125 | |
| 2126 | if (histype == HIST_SEARCH |
| 2127 | && p != lookfor |
| 2128 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 2129 | { |
| 2130 | /* Correct for the separator character used when |
| 2131 | * adding the history entry vs the one used now. |
| 2132 | * First loop: count length. |
| 2133 | * Second loop: copy the characters. */ |
| 2134 | for (i = 0; i <= 1; ++i) |
| 2135 | { |
| 2136 | len = 0; |
| 2137 | for (j = 0; p[j] != NUL; ++j) |
| 2138 | { |
| 2139 | /* Replace old sep with new sep, unless it is |
| 2140 | * escaped. */ |
| 2141 | if (p[j] == old_firstc |
| 2142 | && (j == 0 || p[j - 1] != '\\')) |
| 2143 | { |
| 2144 | if (i > 0) |
| 2145 | ccline.cmdbuff[len] = firstc; |
| 2146 | } |
| 2147 | else |
| 2148 | { |
| 2149 | /* Escape new sep, unless it is already |
| 2150 | * escaped. */ |
| 2151 | if (p[j] == firstc |
| 2152 | && (j == 0 || p[j - 1] != '\\')) |
| 2153 | { |
| 2154 | if (i > 0) |
| 2155 | ccline.cmdbuff[len] = '\\'; |
| 2156 | ++len; |
| 2157 | } |
| 2158 | if (i > 0) |
| 2159 | ccline.cmdbuff[len] = p[j]; |
| 2160 | } |
| 2161 | ++len; |
| 2162 | } |
| 2163 | if (i == 0) |
| 2164 | { |
| 2165 | alloc_cmdbuff(len); |
| 2166 | if (ccline.cmdbuff == NULL) |
| 2167 | goto returncmd; |
| 2168 | } |
| 2169 | } |
| 2170 | ccline.cmdbuff[len] = NUL; |
| 2171 | } |
| 2172 | else |
| 2173 | { |
| 2174 | alloc_cmdbuff((int)STRLEN(p)); |
| 2175 | if (ccline.cmdbuff == NULL) |
| 2176 | goto returncmd; |
| 2177 | STRCPY(ccline.cmdbuff, p); |
| 2178 | } |
| 2179 | |
| 2180 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 2181 | redrawcmd(); |
| 2182 | goto cmdline_changed; |
| 2183 | } |
| 2184 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2185 | #endif |
| 2186 | goto cmdline_not_changed; |
| 2187 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2188 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2189 | case Ctrl_G: /* next match */ |
| 2190 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2191 | if (may_adjust_incsearch_highlighting( |
| 2192 | firstc, count, &is_state, c) == FAIL) |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2193 | goto cmdline_not_changed; |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2194 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2195 | #endif |
| 2196 | |
| 2197 | case Ctrl_V: |
| 2198 | case Ctrl_Q: |
| 2199 | #ifdef FEAT_MOUSE |
| 2200 | ignore_drag_release = TRUE; |
| 2201 | #endif |
| 2202 | putcmdline('^', TRUE); |
| 2203 | c = get_literal(); /* get next (two) character(s) */ |
| 2204 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2205 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2206 | #ifdef FEAT_MBYTE |
| 2207 | /* may need to remove ^ when composing char was typed */ |
| 2208 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 2209 | { |
| 2210 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2211 | msg_putchar(' '); |
| 2212 | cursorcmd(); |
| 2213 | } |
| 2214 | #endif |
| 2215 | break; |
| 2216 | |
| 2217 | #ifdef FEAT_DIGRAPHS |
| 2218 | case Ctrl_K: |
| 2219 | #ifdef FEAT_MOUSE |
| 2220 | ignore_drag_release = TRUE; |
| 2221 | #endif |
| 2222 | putcmdline('?', TRUE); |
| 2223 | #ifdef USE_ON_FLY_SCROLL |
| 2224 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 2225 | #endif |
| 2226 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2227 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2228 | if (c != NUL) |
| 2229 | break; |
| 2230 | |
| 2231 | redrawcmd(); |
| 2232 | goto cmdline_not_changed; |
| 2233 | #endif /* FEAT_DIGRAPHS */ |
| 2234 | |
| 2235 | #ifdef FEAT_RIGHTLEFT |
| 2236 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 2237 | if (!p_ari) |
| 2238 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2239 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2240 | if (p_altkeymap) |
| 2241 | { |
| 2242 | cmd_fkmap = !cmd_fkmap; |
| 2243 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 2244 | ccline.overstrike = FALSE; |
| 2245 | } |
| 2246 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2247 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | cmd_hkmap = !cmd_hkmap; |
| 2249 | goto cmdline_not_changed; |
| 2250 | #endif |
| 2251 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2252 | case K_PS: |
| 2253 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 2254 | goto cmdline_changed; |
| 2255 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | default: |
| 2257 | #ifdef UNIX |
| 2258 | if (c == intr_char) |
| 2259 | { |
| 2260 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 2261 | putting it in history */ |
| 2262 | goto returncmd; /* back to Normal mode */ |
| 2263 | } |
| 2264 | #endif |
| 2265 | /* |
| 2266 | * Normal character with no special meaning. Just set mod_mask |
| 2267 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 2268 | * the string <S-Space>. This should only happen after ^V. |
| 2269 | */ |
| 2270 | if (!IS_SPECIAL(c)) |
| 2271 | mod_mask = 0x0; |
| 2272 | break; |
| 2273 | } |
| 2274 | /* |
| 2275 | * End of switch on command line character. |
| 2276 | * We come here if we have a normal character. |
| 2277 | */ |
| 2278 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2279 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2280 | #ifdef FEAT_MBYTE |
| 2281 | /* Add ABBR_OFF for characters above 0x100, this is |
| 2282 | * what check_abbr() expects. */ |
| 2283 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 2284 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2285 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | goto cmdline_changed; |
| 2287 | |
| 2288 | /* |
| 2289 | * put the character in the command line |
| 2290 | */ |
| 2291 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 2292 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 2293 | else |
| 2294 | { |
| 2295 | #ifdef FEAT_MBYTE |
| 2296 | if (has_mbyte) |
| 2297 | { |
| 2298 | j = (*mb_char2bytes)(c, IObuff); |
| 2299 | IObuff[j] = NUL; /* exclude composing chars */ |
| 2300 | put_on_cmdline(IObuff, j, TRUE); |
| 2301 | } |
| 2302 | else |
| 2303 | #endif |
| 2304 | { |
| 2305 | IObuff[0] = c; |
| 2306 | put_on_cmdline(IObuff, 1, TRUE); |
| 2307 | } |
| 2308 | } |
| 2309 | goto cmdline_changed; |
| 2310 | |
| 2311 | /* |
| 2312 | * This part implements incremental searches for "/" and "?" |
| 2313 | * Jump to cmdline_not_changed when a character has been read but the command |
| 2314 | * line did not change. Then we only search and redraw if something changed in |
| 2315 | * the past. |
| 2316 | * Jump to cmdline_changed when the command line did change. |
| 2317 | * (Sorry for the goto's, I know it is ugly). |
| 2318 | */ |
| 2319 | cmdline_not_changed: |
| 2320 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2321 | if (!is_state.incsearch_postponed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2322 | continue; |
| 2323 | #endif |
| 2324 | |
| 2325 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2326 | /* Trigger CmdlineChanged autocommands. */ |
| 2327 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2328 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2330 | may_do_incsearch_highlighting(firstc, count, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | #endif |
| 2332 | |
| 2333 | #ifdef FEAT_RIGHTLEFT |
| 2334 | if (cmdmsg_rl |
| 2335 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2336 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | # endif |
| 2338 | ) |
| 2339 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2340 | * right-left typing. Not efficient, but it works. |
| 2341 | * Do it only when there are no characters left to read |
| 2342 | * to avoid useless intermediate redraws. */ |
| 2343 | if (vpeekc() == NUL) |
| 2344 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2345 | #endif |
| 2346 | } |
| 2347 | |
| 2348 | returncmd: |
| 2349 | |
| 2350 | #ifdef FEAT_RIGHTLEFT |
| 2351 | cmdmsg_rl = FALSE; |
| 2352 | #endif |
| 2353 | |
| 2354 | #ifdef FEAT_FKMAP |
| 2355 | cmd_fkmap = 0; |
| 2356 | #endif |
| 2357 | |
| 2358 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2359 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2360 | |
| 2361 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 2362 | finish_incsearch_highlighting(gotesc, &is_state, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | #endif |
| 2364 | |
| 2365 | if (ccline.cmdbuff != NULL) |
| 2366 | { |
| 2367 | /* |
| 2368 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2369 | */ |
| 2370 | #ifdef FEAT_CMDHIST |
| 2371 | if (ccline.cmdlen && firstc != NUL |
| 2372 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2373 | { |
| 2374 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2375 | histype == HIST_SEARCH ? firstc : NUL); |
| 2376 | if (firstc == ':') |
| 2377 | { |
| 2378 | vim_free(new_last_cmdline); |
| 2379 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2380 | } |
| 2381 | } |
| 2382 | #endif |
| 2383 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2384 | if (gotesc) |
| 2385 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | * If the screen was shifted up, redraw the whole screen (later). |
| 2390 | * If the line is too long, clear it, so ruler and shown command do |
| 2391 | * not get printed in the middle of it. |
| 2392 | */ |
| 2393 | msg_check(); |
| 2394 | msg_scroll = save_msg_scroll; |
| 2395 | redir_off = FALSE; |
| 2396 | |
| 2397 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2398 | if (some_key_typed) |
| 2399 | need_wait_return = FALSE; |
| 2400 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2401 | /* Trigger CmdlineLeave autocommands. */ |
| 2402 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2403 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2405 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2407 | im_save_status(b_im_ptr); |
| 2408 | im_set_active(FALSE); |
| 2409 | #endif |
| 2410 | #ifdef FEAT_MOUSE |
| 2411 | setmouse(); |
| 2412 | #endif |
| 2413 | #ifdef CURSOR_SHAPE |
| 2414 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2415 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2416 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2417 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2418 | { |
| 2419 | char_u *p = ccline.cmdbuff; |
| 2420 | |
| 2421 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2422 | ccline.cmdbuff = NULL; |
| 2423 | return p; |
| 2424 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2428 | /* |
| 2429 | * Get a command line with a prompt. |
| 2430 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2431 | * f_input() when evaluating an expression from CTRL-R =). |
| 2432 | * Returns the command line in allocated memory, or NULL. |
| 2433 | */ |
| 2434 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2435 | getcmdline_prompt( |
| 2436 | int firstc, |
| 2437 | char_u *prompt, /* command line prompt */ |
| 2438 | int attr, /* attributes for prompt */ |
| 2439 | int xp_context, /* type of expansion */ |
| 2440 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2441 | { |
| 2442 | char_u *s; |
| 2443 | struct cmdline_info save_ccline; |
| 2444 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2445 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2447 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2448 | ccline.cmdprompt = prompt; |
| 2449 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2450 | # ifdef FEAT_EVAL |
| 2451 | ccline.xp_context = xp_context; |
| 2452 | ccline.xp_arg = xp_arg; |
| 2453 | ccline.input_fn = (firstc == '@'); |
| 2454 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2455 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2456 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2457 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2458 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2459 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2460 | * But only if called recursively and the commandline is therefore being |
| 2461 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2462 | * so we need its modified msg_col left intact. */ |
| 2463 | if (ccline.cmdbuff != NULL) |
| 2464 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2465 | |
| 2466 | return s; |
| 2467 | } |
| 2468 | #endif |
| 2469 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2470 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2471 | * Return TRUE when the text must not be changed and we can't switch to |
| 2472 | * another window or buffer. Used when editing the command line, evaluating |
| 2473 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2474 | */ |
| 2475 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2476 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2477 | { |
| 2478 | #ifdef FEAT_CMDWIN |
| 2479 | if (cmdwin_type != 0) |
| 2480 | return TRUE; |
| 2481 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2482 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * Give an error message for a command that isn't allowed while the cmdline |
| 2487 | * window is open or editing the cmdline in another way. |
| 2488 | */ |
| 2489 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2490 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2491 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2492 | EMSG(_(get_text_locked_msg())); |
| 2493 | } |
| 2494 | |
| 2495 | char_u * |
| 2496 | get_text_locked_msg(void) |
| 2497 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2498 | #ifdef FEAT_CMDWIN |
| 2499 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2500 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2501 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2502 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2505 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2506 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2507 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2508 | */ |
| 2509 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2510 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2511 | { |
| 2512 | if (curbuf_lock > 0) |
| 2513 | { |
| 2514 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2515 | return TRUE; |
| 2516 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2517 | return allbuf_locked(); |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2522 | * message. |
| 2523 | */ |
| 2524 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2525 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2526 | { |
| 2527 | if (allbuf_lock > 0) |
| 2528 | { |
| 2529 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2530 | return TRUE; |
| 2531 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2532 | return FALSE; |
| 2533 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2534 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2536 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2537 | { |
| 2538 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2539 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2540 | return 1; |
| 2541 | #endif |
| 2542 | return ptr2cells(ccline.cmdbuff + idx); |
| 2543 | } |
| 2544 | |
| 2545 | /* |
| 2546 | * Compute the offset of the cursor on the command line for the prompt and |
| 2547 | * indent. |
| 2548 | */ |
| 2549 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2550 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2552 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2553 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2554 | else |
| 2555 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2556 | } |
| 2557 | |
| 2558 | /* |
| 2559 | * Compute the screen position for the cursor on the command line. |
| 2560 | */ |
| 2561 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2562 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2563 | { |
| 2564 | int i, m, c; |
| 2565 | |
| 2566 | set_cmdspos(); |
| 2567 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2568 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2570 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2571 | m = MAXCOL; |
| 2572 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | else |
| 2574 | m = MAXCOL; |
| 2575 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2576 | { |
| 2577 | c = cmdline_charsize(i); |
| 2578 | #ifdef FEAT_MBYTE |
| 2579 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2580 | if (has_mbyte) |
| 2581 | correct_cmdspos(i, c); |
| 2582 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2583 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2584 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2585 | if ((ccline.cmdspos += c) >= m) |
| 2586 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2587 | ccline.cmdspos -= c; |
| 2588 | break; |
| 2589 | } |
| 2590 | #ifdef FEAT_MBYTE |
| 2591 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2592 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2593 | #endif |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | #ifdef FEAT_MBYTE |
| 2598 | /* |
| 2599 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2600 | * character that doesn't fit, so that a ">" must be displayed. |
| 2601 | */ |
| 2602 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2603 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2605 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2606 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2607 | && ccline.cmdspos % Columns + cells > Columns) |
| 2608 | ccline.cmdspos++; |
| 2609 | } |
| 2610 | #endif |
| 2611 | |
| 2612 | /* |
| 2613 | * Get an Ex command line for the ":" command. |
| 2614 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2616 | getexline( |
| 2617 | int c, /* normally ':', NUL for ":append" */ |
| 2618 | void *cookie UNUSED, |
| 2619 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2620 | { |
| 2621 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2622 | if (exec_from_reg && vpeekc() == ':') |
| 2623 | (void)vgetc(); |
| 2624 | return getcmdline(c, 1L, indent); |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Get an Ex command line for Ex mode. |
| 2629 | * In Ex mode we only use the OS supplied line editing features and no |
| 2630 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2631 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2632 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2633 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2634 | getexmodeline( |
| 2635 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2636 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2637 | void *cookie UNUSED, |
| 2638 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2639 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2640 | garray_T line_ga; |
| 2641 | char_u *pend; |
| 2642 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2643 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2644 | int escaped = FALSE; /* CTRL-V typed */ |
| 2645 | int vcol = 0; |
| 2646 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2647 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2648 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | |
| 2650 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2651 | * confuses the system function that computes tabstops. */ |
| 2652 | cursor_on(); |
| 2653 | |
| 2654 | /* always start in column 0; write a newline if necessary */ |
| 2655 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2656 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2658 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2659 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2660 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2661 | if (p_prompt) |
| 2662 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | while (indent-- > 0) |
| 2664 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | ga_init2(&line_ga, 1, 30); |
| 2669 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2670 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2671 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2672 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2673 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2674 | while (indent >= 8) |
| 2675 | { |
| 2676 | ga_append(&line_ga, TAB); |
| 2677 | msg_puts((char_u *)" "); |
| 2678 | indent -= 8; |
| 2679 | } |
| 2680 | while (indent-- > 0) |
| 2681 | { |
| 2682 | ga_append(&line_ga, ' '); |
| 2683 | msg_putchar(' '); |
| 2684 | } |
| 2685 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2686 | ++no_mapping; |
| 2687 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2688 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | /* |
| 2690 | * Get the line, one character at a time. |
| 2691 | */ |
| 2692 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2693 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2694 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2695 | long sw; |
| 2696 | char_u *s; |
| 2697 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2699 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2701 | /* |
| 2702 | * Get one character at a time. |
| 2703 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2704 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* Check for a ":normal" command and no more characters left. */ |
| 2707 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2708 | c1 = '\n'; |
| 2709 | else |
| 2710 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | |
| 2712 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2713 | * Handle line editing. |
| 2714 | * Previously this was left to the system, putting the terminal in |
| 2715 | * 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] | 2716 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2717 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2718 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2719 | msg_putchar('\n'); |
| 2720 | break; |
| 2721 | } |
| 2722 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2723 | if (c1 == K_PS) |
| 2724 | { |
| 2725 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2726 | goto redraw; |
| 2727 | } |
| 2728 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2729 | if (!escaped) |
| 2730 | { |
| 2731 | /* CR typed means "enter", which is NL */ |
| 2732 | if (c1 == '\r') |
| 2733 | c1 = '\n'; |
| 2734 | |
| 2735 | if (c1 == BS || c1 == K_BS |
| 2736 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2737 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2738 | if (line_ga.ga_len > 0) |
| 2739 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2740 | #ifdef FEAT_MBYTE |
| 2741 | if (has_mbyte) |
| 2742 | { |
| 2743 | p = (char_u *)line_ga.ga_data; |
| 2744 | p[line_ga.ga_len] = NUL; |
| 2745 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2746 | line_ga.ga_len -= len; |
| 2747 | } |
| 2748 | else |
| 2749 | #endif |
| 2750 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2751 | goto redraw; |
| 2752 | } |
| 2753 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2756 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2757 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2758 | msg_col = startcol; |
| 2759 | msg_clr_eos(); |
| 2760 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2761 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | if (c1 == Ctrl_T) |
| 2765 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2766 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2767 | p = (char_u *)line_ga.ga_data; |
| 2768 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2769 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2770 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2771 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2772 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2773 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2774 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2775 | p = (char_u *)line_ga.ga_data; |
| 2776 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2777 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2778 | *s = ' '; |
| 2779 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2780 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2781 | redraw: |
| 2782 | /* redraw the line */ |
| 2783 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2784 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2785 | p = (char_u *)line_ga.ga_data; |
| 2786 | p[line_ga.ga_len] = NUL; |
| 2787 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2789 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2790 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2791 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2792 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2793 | msg_putchar(' '); |
| 2794 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2795 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2796 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2797 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2798 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2799 | len = MB_PTR2LEN(p); |
| 2800 | msg_outtrans_len(p, len); |
| 2801 | vcol += ptr2cells(p); |
| 2802 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2803 | } |
| 2804 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2805 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2806 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2807 | continue; |
| 2808 | } |
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 (c1 == Ctrl_D) |
| 2811 | { |
| 2812 | /* Delete one shiftwidth. */ |
| 2813 | p = (char_u *)line_ga.ga_data; |
| 2814 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2815 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2816 | if (prev_char == '^') |
| 2817 | ex_keep_indent = TRUE; |
| 2818 | indent = 0; |
| 2819 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2820 | } |
| 2821 | else |
| 2822 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2823 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2824 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2825 | if (indent > 0) |
| 2826 | { |
| 2827 | --indent; |
| 2828 | indent -= indent % get_sw_value(curbuf); |
| 2829 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2830 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2831 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2832 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2833 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2834 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2835 | --line_ga.ga_len; |
| 2836 | } |
| 2837 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2839 | |
| 2840 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2841 | { |
| 2842 | escaped = TRUE; |
| 2843 | continue; |
| 2844 | } |
| 2845 | |
| 2846 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2847 | if (IS_SPECIAL(c1)) |
| 2848 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2849 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2850 | |
| 2851 | if (IS_SPECIAL(c1)) |
| 2852 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2853 | #ifdef FEAT_MBYTE |
| 2854 | if (has_mbyte) |
| 2855 | len = (*mb_char2bytes)(c1, |
| 2856 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2857 | else |
| 2858 | #endif |
| 2859 | { |
| 2860 | len = 1; |
| 2861 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2862 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2863 | if (c1 == '\n') |
| 2864 | msg_putchar('\n'); |
| 2865 | else if (c1 == TAB) |
| 2866 | { |
| 2867 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2868 | do |
| 2869 | { |
| 2870 | msg_putchar(' '); |
| 2871 | } while (++vcol % 8); |
| 2872 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2873 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2874 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2875 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2876 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2877 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2878 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2879 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2880 | escaped = FALSE; |
| 2881 | |
| 2882 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2883 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2884 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2885 | /* We are done when a NL is entered, but not when it comes after an |
| 2886 | * odd number of backslashes, that results in a NUL. */ |
| 2887 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2888 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2889 | int bcount = 0; |
| 2890 | |
| 2891 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2892 | ++bcount; |
| 2893 | |
| 2894 | if (bcount > 0) |
| 2895 | { |
| 2896 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2897 | * "\NL", etc. */ |
| 2898 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2899 | pend -= (bcount + 1) / 2; |
| 2900 | pend[-1] = '\n'; |
| 2901 | } |
| 2902 | |
| 2903 | if ((bcount & 1) == 0) |
| 2904 | { |
| 2905 | --line_ga.ga_len; |
| 2906 | --pend; |
| 2907 | *pend = NUL; |
| 2908 | break; |
| 2909 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | } |
| 2911 | } |
| 2912 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2913 | --no_mapping; |
| 2914 | --allow_keys; |
| 2915 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2916 | /* make following messages go to the next line */ |
| 2917 | msg_didout = FALSE; |
| 2918 | msg_col = 0; |
| 2919 | if (msg_row < Rows - 1) |
| 2920 | ++msg_row; |
| 2921 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2922 | |
| 2923 | if (got_int) |
| 2924 | ga_clear(&line_ga); |
| 2925 | |
| 2926 | return (char_u *)line_ga.ga_data; |
| 2927 | } |
| 2928 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2929 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2930 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | /* |
| 2932 | * Return TRUE if ccline.overstrike is on. |
| 2933 | */ |
| 2934 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2935 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2936 | { |
| 2937 | return ccline.overstrike; |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2942 | */ |
| 2943 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2944 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | { |
| 2946 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2947 | } |
| 2948 | #endif |
| 2949 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2950 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2951 | /* |
| 2952 | * Return the virtual column number at the current cursor position. |
| 2953 | * This is used by the IM code to obtain the start of the preedit string. |
| 2954 | */ |
| 2955 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2956 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2957 | { |
| 2958 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2959 | return MAXCOL; |
| 2960 | |
| 2961 | # ifdef FEAT_MBYTE |
| 2962 | if (has_mbyte) |
| 2963 | { |
| 2964 | colnr_T col; |
| 2965 | int i = 0; |
| 2966 | |
| 2967 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2968 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2969 | |
| 2970 | return col; |
| 2971 | } |
| 2972 | else |
| 2973 | # endif |
| 2974 | return ccline.cmdpos; |
| 2975 | } |
| 2976 | #endif |
| 2977 | |
| 2978 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2979 | /* |
| 2980 | * If part of the command line is an IM preedit string, redraw it with |
| 2981 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2982 | */ |
| 2983 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2984 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2985 | { |
| 2986 | if ((State & CMDLINE) |
| 2987 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2988 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2989 | && !p_imdisable |
| 2990 | && im_is_preediting()) |
| 2991 | { |
| 2992 | int cmdpos = 0; |
| 2993 | int cmdspos; |
| 2994 | int old_row; |
| 2995 | int old_col; |
| 2996 | colnr_T col; |
| 2997 | |
| 2998 | old_row = msg_row; |
| 2999 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3000 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3001 | |
| 3002 | # ifdef FEAT_MBYTE |
| 3003 | if (has_mbyte) |
| 3004 | { |
| 3005 | for (col = 0; col < preedit_start_col |
| 3006 | && cmdpos < ccline.cmdlen; ++col) |
| 3007 | { |
| 3008 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3009 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3010 | } |
| 3011 | } |
| 3012 | else |
| 3013 | # endif |
| 3014 | { |
| 3015 | cmdspos += preedit_start_col; |
| 3016 | cmdpos += preedit_start_col; |
| 3017 | } |
| 3018 | |
| 3019 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 3020 | msg_col = cmdspos % (int)Columns; |
| 3021 | if (msg_row >= Rows) |
| 3022 | msg_row = Rows - 1; |
| 3023 | |
| 3024 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 3025 | { |
| 3026 | int char_len; |
| 3027 | int char_attr; |
| 3028 | |
| 3029 | char_attr = im_get_feedback_attr(col); |
| 3030 | if (char_attr < 0) |
| 3031 | break; /* end of preedit string */ |
| 3032 | |
| 3033 | # ifdef FEAT_MBYTE |
| 3034 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3035 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3036 | else |
| 3037 | # endif |
| 3038 | char_len = 1; |
| 3039 | |
| 3040 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 3041 | cmdpos += char_len; |
| 3042 | } |
| 3043 | |
| 3044 | msg_row = old_row; |
| 3045 | msg_col = old_col; |
| 3046 | } |
| 3047 | } |
| 3048 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 3049 | |
| 3050 | /* |
| 3051 | * Allocate a new command line buffer. |
| 3052 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 3053 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 3054 | */ |
| 3055 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3056 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3057 | { |
| 3058 | /* |
| 3059 | * give some extra space to avoid having to allocate all the time |
| 3060 | */ |
| 3061 | if (len < 80) |
| 3062 | len = 100; |
| 3063 | else |
| 3064 | len += 20; |
| 3065 | |
| 3066 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 3067 | ccline.cmdbufflen = len; |
| 3068 | } |
| 3069 | |
| 3070 | /* |
| 3071 | * Re-allocate the command line to length len + something extra. |
| 3072 | * return FAIL for failure, OK otherwise |
| 3073 | */ |
| 3074 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3075 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | { |
| 3077 | char_u *p; |
| 3078 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3079 | if (len < ccline.cmdbufflen) |
| 3080 | return OK; /* no need to resize */ |
| 3081 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | p = ccline.cmdbuff; |
| 3083 | alloc_cmdbuff(len); /* will get some more */ |
| 3084 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 3085 | { |
| 3086 | ccline.cmdbuff = p; /* keep the old one */ |
| 3087 | return FAIL; |
| 3088 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 3089 | /* There isn't always a NUL after the command, but it may need to be |
| 3090 | * there, thus copy up to the NUL and add a NUL. */ |
| 3091 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 3092 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3093 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3094 | |
| 3095 | if (ccline.xpc != NULL |
| 3096 | && ccline.xpc->xp_pattern != NULL |
| 3097 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 3098 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 3099 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 3100 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3101 | |
| 3102 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 3103 | * to point into the newly allocated memory. */ |
| 3104 | if (i >= 0 && i <= ccline.cmdlen) |
| 3105 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 3106 | } |
| 3107 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3108 | return OK; |
| 3109 | } |
| 3110 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3111 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 3112 | static char_u *arshape_buf = NULL; |
| 3113 | |
| 3114 | # if defined(EXITFREE) || defined(PROTO) |
| 3115 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3116 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3117 | { |
| 3118 | vim_free(arshape_buf); |
| 3119 | } |
| 3120 | # endif |
| 3121 | #endif |
| 3122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3123 | /* |
| 3124 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 3125 | * when cmdline_star is TRUE. |
| 3126 | */ |
| 3127 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3128 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3129 | { |
| 3130 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3131 | int i; |
| 3132 | |
| 3133 | if (cmdline_star > 0) |
| 3134 | for (i = 0; i < len; ++i) |
| 3135 | { |
| 3136 | msg_putchar('*'); |
| 3137 | # ifdef FEAT_MBYTE |
| 3138 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3139 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3140 | # endif |
| 3141 | } |
| 3142 | else |
| 3143 | #endif |
| 3144 | #ifdef FEAT_ARABIC |
| 3145 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 3146 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3147 | static int buflen = 0; |
| 3148 | char_u *p; |
| 3149 | int j; |
| 3150 | int newlen = 0; |
| 3151 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3152 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3153 | int prev_c = 0; |
| 3154 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3155 | int u8c; |
| 3156 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3157 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | |
| 3159 | /* |
| 3160 | * Do arabic shaping into a temporary buffer. This is very |
| 3161 | * inefficient! |
| 3162 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3163 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3164 | { |
| 3165 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 3166 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3167 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3168 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3169 | arshape_buf = alloc(buflen); |
| 3170 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3171 | return; /* out of memory */ |
| 3172 | } |
| 3173 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3174 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 3175 | { |
| 3176 | /* Prepend a space to draw the leading composing char on. */ |
| 3177 | arshape_buf[0] = ' '; |
| 3178 | newlen = 1; |
| 3179 | } |
| 3180 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3181 | for (j = start; j < start + len; j += mb_l) |
| 3182 | { |
| 3183 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3184 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3185 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3186 | if (ARABIC_CHAR(u8c)) |
| 3187 | { |
| 3188 | /* Do Arabic shaping. */ |
| 3189 | if (cmdmsg_rl) |
| 3190 | { |
| 3191 | /* displaying from right to left */ |
| 3192 | pc = prev_c; |
| 3193 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3194 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3195 | if (j + mb_l >= start + len) |
| 3196 | nc = NUL; |
| 3197 | else |
| 3198 | nc = utf_ptr2char(p + mb_l); |
| 3199 | } |
| 3200 | else |
| 3201 | { |
| 3202 | /* displaying from left to right */ |
| 3203 | if (j + mb_l >= start + len) |
| 3204 | pc = NUL; |
| 3205 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3206 | { |
| 3207 | int pcc[MAX_MCO]; |
| 3208 | |
| 3209 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3210 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3211 | pc1 = pcc[0]; |
| 3212 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3213 | nc = prev_c; |
| 3214 | } |
| 3215 | prev_c = u8c; |
| 3216 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3217 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3218 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3219 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3220 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3221 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3222 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 3223 | if (u8cc[1] != 0) |
| 3224 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3225 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3226 | } |
| 3227 | } |
| 3228 | else |
| 3229 | { |
| 3230 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3231 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3232 | newlen += mb_l; |
| 3233 | } |
| 3234 | } |
| 3235 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3236 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3237 | } |
| 3238 | else |
| 3239 | #endif |
| 3240 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * Put a character on the command line. Shifts the following text to the |
| 3245 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3246 | * "c" must be printable (fit in one display cell)! |
| 3247 | */ |
| 3248 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3249 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | { |
| 3251 | if (cmd_silent) |
| 3252 | return; |
| 3253 | msg_no_more = TRUE; |
| 3254 | msg_putchar(c); |
| 3255 | if (shift) |
| 3256 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3257 | msg_no_more = FALSE; |
| 3258 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3259 | extra_char = c; |
| 3260 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | /* |
| 3264 | * Undo a putcmdline(c, FALSE). |
| 3265 | */ |
| 3266 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3267 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3268 | { |
| 3269 | if (cmd_silent) |
| 3270 | return; |
| 3271 | msg_no_more = TRUE; |
| 3272 | if (ccline.cmdlen == ccline.cmdpos) |
| 3273 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3274 | #ifdef FEAT_MBYTE |
| 3275 | else if (has_mbyte) |
| 3276 | draw_cmdline(ccline.cmdpos, |
| 3277 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3278 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3279 | else |
| 3280 | draw_cmdline(ccline.cmdpos, 1); |
| 3281 | msg_no_more = FALSE; |
| 3282 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3283 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * Put the given string, of the given length, onto the command line. |
| 3288 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3289 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3290 | * part will be redrawn, otherwise it will not. If this function is called |
| 3291 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3292 | * called afterwards. |
| 3293 | */ |
| 3294 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3295 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3296 | { |
| 3297 | int retval; |
| 3298 | int i; |
| 3299 | int m; |
| 3300 | int c; |
| 3301 | |
| 3302 | if (len < 0) |
| 3303 | len = (int)STRLEN(str); |
| 3304 | |
| 3305 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3306 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3307 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3308 | else |
| 3309 | retval = OK; |
| 3310 | if (retval == OK) |
| 3311 | { |
| 3312 | if (!ccline.overstrike) |
| 3313 | { |
| 3314 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3315 | ccline.cmdbuff + ccline.cmdpos, |
| 3316 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3317 | ccline.cmdlen += len; |
| 3318 | } |
| 3319 | else |
| 3320 | { |
| 3321 | #ifdef FEAT_MBYTE |
| 3322 | if (has_mbyte) |
| 3323 | { |
| 3324 | /* Count nr of characters in the new string. */ |
| 3325 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3326 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | ++m; |
| 3328 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3329 | * characters. */ |
| 3330 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3331 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3332 | --m; |
| 3333 | if (i < ccline.cmdlen) |
| 3334 | { |
| 3335 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3336 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3337 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3338 | } |
| 3339 | else |
| 3340 | ccline.cmdlen = ccline.cmdpos + len; |
| 3341 | } |
| 3342 | else |
| 3343 | #endif |
| 3344 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3345 | ccline.cmdlen = ccline.cmdpos + len; |
| 3346 | } |
| 3347 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3348 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3349 | |
| 3350 | #ifdef FEAT_MBYTE |
| 3351 | if (enc_utf8) |
| 3352 | { |
| 3353 | /* When the inserted text starts with a composing character, |
| 3354 | * backup to the character before it. There could be two of them. |
| 3355 | */ |
| 3356 | i = 0; |
| 3357 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3358 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3359 | { |
| 3360 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3361 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3362 | ccline.cmdpos -= i; |
| 3363 | len += i; |
| 3364 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3365 | } |
| 3366 | # ifdef FEAT_ARABIC |
| 3367 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3368 | { |
| 3369 | /* Check the previous character for Arabic combining pair. */ |
| 3370 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3371 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3372 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3373 | + ccline.cmdpos - i), c)) |
| 3374 | { |
| 3375 | ccline.cmdpos -= i; |
| 3376 | len += i; |
| 3377 | } |
| 3378 | else |
| 3379 | i = 0; |
| 3380 | } |
| 3381 | # endif |
| 3382 | if (i != 0) |
| 3383 | { |
| 3384 | /* Also backup the cursor position. */ |
| 3385 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3386 | ccline.cmdspos -= i; |
| 3387 | msg_col -= i; |
| 3388 | if (msg_col < 0) |
| 3389 | { |
| 3390 | msg_col += Columns; |
| 3391 | --msg_row; |
| 3392 | } |
| 3393 | } |
| 3394 | } |
| 3395 | #endif |
| 3396 | |
| 3397 | if (redraw && !cmd_silent) |
| 3398 | { |
| 3399 | msg_no_more = TRUE; |
| 3400 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3401 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3402 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3403 | /* Avoid clearing the rest of the line too often. */ |
| 3404 | if (cmdline_row != i || ccline.overstrike) |
| 3405 | msg_clr_eos(); |
| 3406 | msg_no_more = FALSE; |
| 3407 | } |
| 3408 | #ifdef FEAT_FKMAP |
| 3409 | /* |
| 3410 | * If we are in Farsi command mode, the character input must be in |
| 3411 | * Insert mode. So do not advance the cmdpos. |
| 3412 | */ |
| 3413 | if (!cmd_fkmap) |
| 3414 | #endif |
| 3415 | { |
| 3416 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3417 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3419 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3420 | m = MAXCOL; |
| 3421 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | else |
| 3423 | m = MAXCOL; |
| 3424 | for (i = 0; i < len; ++i) |
| 3425 | { |
| 3426 | c = cmdline_charsize(ccline.cmdpos); |
| 3427 | #ifdef FEAT_MBYTE |
| 3428 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3429 | if (has_mbyte) |
| 3430 | correct_cmdspos(ccline.cmdpos, c); |
| 3431 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3432 | /* Stop cursor at the end of the screen, but do increment the |
| 3433 | * insert position, so that entering a very long command |
| 3434 | * works, even though you can't see it. */ |
| 3435 | if (ccline.cmdspos + c < m) |
| 3436 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3437 | #ifdef FEAT_MBYTE |
| 3438 | if (has_mbyte) |
| 3439 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3440 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3441 | if (c > len - i - 1) |
| 3442 | c = len - i - 1; |
| 3443 | ccline.cmdpos += c; |
| 3444 | i += c; |
| 3445 | } |
| 3446 | #endif |
| 3447 | ++ccline.cmdpos; |
| 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | if (redraw) |
| 3452 | msg_check(); |
| 3453 | return retval; |
| 3454 | } |
| 3455 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3456 | static struct cmdline_info prev_ccline; |
| 3457 | static int prev_ccline_used = FALSE; |
| 3458 | |
| 3459 | /* |
| 3460 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3461 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3462 | * available globally in prev_ccline. |
| 3463 | */ |
| 3464 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3465 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3466 | { |
| 3467 | if (!prev_ccline_used) |
| 3468 | { |
| 3469 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3470 | prev_ccline_used = TRUE; |
| 3471 | } |
| 3472 | *ccp = prev_ccline; |
| 3473 | prev_ccline = ccline; |
| 3474 | ccline.cmdbuff = NULL; |
| 3475 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3476 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3480 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3481 | */ |
| 3482 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3483 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3484 | { |
| 3485 | ccline = prev_ccline; |
| 3486 | prev_ccline = *ccp; |
| 3487 | } |
| 3488 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3489 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3490 | /* |
| 3491 | * Save the command line into allocated memory. Returns a pointer to be |
| 3492 | * passed to restore_cmdline_alloc() later. |
| 3493 | * Returns NULL when failed. |
| 3494 | */ |
| 3495 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3496 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3497 | { |
| 3498 | struct cmdline_info *p; |
| 3499 | |
| 3500 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3501 | if (p != NULL) |
| 3502 | save_cmdline(p); |
| 3503 | return (char_u *)p; |
| 3504 | } |
| 3505 | |
| 3506 | /* |
| 3507 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3508 | */ |
| 3509 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3510 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3511 | { |
| 3512 | if (p != NULL) |
| 3513 | { |
| 3514 | restore_cmdline((struct cmdline_info *)p); |
| 3515 | vim_free(p); |
| 3516 | } |
| 3517 | } |
| 3518 | #endif |
| 3519 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3520 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3521 | * Paste a yank register into the command line. |
| 3522 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3523 | * insert_reg() can't be used here, because special characters from the |
| 3524 | * register contents will be interpreted as commands. |
| 3525 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3526 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3527 | */ |
| 3528 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3529 | cmdline_paste( |
| 3530 | int regname, |
| 3531 | int literally, /* Insert text literally instead of "as typed" */ |
| 3532 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3533 | { |
| 3534 | long i; |
| 3535 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3536 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3537 | int allocated; |
| 3538 | struct cmdline_info save_ccline; |
| 3539 | |
| 3540 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3541 | * the command line */ |
| 3542 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 3543 | && regname != Ctrl_A && regname != Ctrl_L |
| 3544 | && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3545 | return FAIL; |
| 3546 | |
| 3547 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3548 | * CTRL-C to break the loop. */ |
| 3549 | line_breakcheck(); |
| 3550 | if (got_int) |
| 3551 | return FAIL; |
| 3552 | |
| 3553 | #ifdef FEAT_CLIPBOARD |
| 3554 | regname = may_get_selection(regname); |
| 3555 | #endif |
| 3556 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3557 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3558 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3559 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3560 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3561 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3562 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3563 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3564 | |
| 3565 | if (i) |
| 3566 | { |
| 3567 | /* Got the value of a special register in "arg". */ |
| 3568 | if (arg == NULL) |
| 3569 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3570 | |
| 3571 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3572 | * part of the word. */ |
| 3573 | p = arg; |
| 3574 | if (p_is && regname == Ctrl_W) |
| 3575 | { |
| 3576 | char_u *w; |
| 3577 | int len; |
| 3578 | |
| 3579 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3580 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3581 | { |
| 3582 | #ifdef FEAT_MBYTE |
| 3583 | if (has_mbyte) |
| 3584 | { |
| 3585 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3586 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3587 | break; |
| 3588 | w -= len; |
| 3589 | } |
| 3590 | else |
| 3591 | #endif |
| 3592 | { |
| 3593 | if (!vim_iswordc(w[-1])) |
| 3594 | break; |
| 3595 | --w; |
| 3596 | } |
| 3597 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3598 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3599 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3600 | p += len; |
| 3601 | } |
| 3602 | |
| 3603 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3604 | if (allocated) |
| 3605 | vim_free(arg); |
| 3606 | return OK; |
| 3607 | } |
| 3608 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3609 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | /* |
| 3613 | * Put a string on the command line. |
| 3614 | * When "literally" is TRUE, insert literally. |
| 3615 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3616 | * line. |
| 3617 | */ |
| 3618 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3619 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3620 | { |
| 3621 | int c, cv; |
| 3622 | |
| 3623 | if (literally) |
| 3624 | put_on_cmdline(s, -1, TRUE); |
| 3625 | else |
| 3626 | while (*s != NUL) |
| 3627 | { |
| 3628 | cv = *s; |
| 3629 | if (cv == Ctrl_V && s[1]) |
| 3630 | ++s; |
| 3631 | #ifdef FEAT_MBYTE |
| 3632 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3633 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3634 | else |
| 3635 | #endif |
| 3636 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3637 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3638 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3639 | #ifdef UNIX |
| 3640 | || c == intr_char |
| 3641 | #endif |
| 3642 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3643 | stuffcharReadbuff(Ctrl_V); |
| 3644 | stuffcharReadbuff(c); |
| 3645 | } |
| 3646 | } |
| 3647 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3648 | #ifdef FEAT_WILDMENU |
| 3649 | /* |
| 3650 | * Delete characters on the command line, from "from" to the current |
| 3651 | * position. |
| 3652 | */ |
| 3653 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3654 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | { |
| 3656 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3657 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3658 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3659 | ccline.cmdpos = from; |
| 3660 | } |
| 3661 | #endif |
| 3662 | |
| 3663 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3664 | * This function is called when the screen size changes and with incremental |
| 3665 | * search and in other situations where the command line may have been |
| 3666 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3667 | */ |
| 3668 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3669 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3671 | redrawcmdline_ex(TRUE); |
| 3672 | } |
| 3673 | |
| 3674 | void |
| 3675 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3676 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3677 | if (cmd_silent) |
| 3678 | return; |
| 3679 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3680 | if (do_compute_cmdrow) |
| 3681 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3682 | redrawcmd(); |
| 3683 | cursorcmd(); |
| 3684 | } |
| 3685 | |
| 3686 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3687 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3688 | { |
| 3689 | int i; |
| 3690 | |
| 3691 | if (cmd_silent) |
| 3692 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3693 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3694 | msg_putchar(ccline.cmdfirstc); |
| 3695 | if (ccline.cmdprompt != NULL) |
| 3696 | { |
| 3697 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3698 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3699 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3700 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3701 | --ccline.cmdindent; |
| 3702 | } |
| 3703 | else |
| 3704 | for (i = ccline.cmdindent; i > 0; --i) |
| 3705 | msg_putchar(' '); |
| 3706 | } |
| 3707 | |
| 3708 | /* |
| 3709 | * Redraw what is currently on the command line. |
| 3710 | */ |
| 3711 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3712 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | { |
| 3714 | if (cmd_silent) |
| 3715 | return; |
| 3716 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3717 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3718 | if (ccline.cmdbuff == NULL) |
| 3719 | { |
| 3720 | windgoto(cmdline_row, 0); |
| 3721 | msg_clr_eos(); |
| 3722 | return; |
| 3723 | } |
| 3724 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | msg_start(); |
| 3726 | redrawcmdprompt(); |
| 3727 | |
| 3728 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3729 | msg_no_more = TRUE; |
| 3730 | draw_cmdline(0, ccline.cmdlen); |
| 3731 | msg_clr_eos(); |
| 3732 | msg_no_more = FALSE; |
| 3733 | |
| 3734 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3735 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3736 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3737 | |
| 3738 | /* |
| 3739 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3740 | * in cmdline mode we can reset them now. |
| 3741 | */ |
| 3742 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3743 | |
| 3744 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3745 | * in cmdline mode */ |
| 3746 | skip_redraw = FALSE; |
| 3747 | } |
| 3748 | |
| 3749 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3750 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3752 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | cmdline_row = Rows - 1; |
| 3754 | else |
| 3755 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3756 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3760 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3761 | { |
| 3762 | if (cmd_silent) |
| 3763 | return; |
| 3764 | |
| 3765 | #ifdef FEAT_RIGHTLEFT |
| 3766 | if (cmdmsg_rl) |
| 3767 | { |
| 3768 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3769 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3770 | if (msg_row <= 0) |
| 3771 | msg_row = Rows - 1; |
| 3772 | } |
| 3773 | else |
| 3774 | #endif |
| 3775 | { |
| 3776 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3777 | msg_col = ccline.cmdspos % (int)Columns; |
| 3778 | if (msg_row >= Rows) |
| 3779 | msg_row = Rows - 1; |
| 3780 | } |
| 3781 | |
| 3782 | windgoto(msg_row, msg_col); |
| 3783 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3784 | if (p_imst == IM_ON_THE_SPOT) |
| 3785 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | #endif |
| 3787 | #ifdef MCH_CURSOR_SHAPE |
| 3788 | mch_update_cursor(); |
| 3789 | #endif |
| 3790 | } |
| 3791 | |
| 3792 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3793 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3794 | { |
| 3795 | msg_start(); |
| 3796 | #ifdef FEAT_RIGHTLEFT |
| 3797 | if (cmdmsg_rl) |
| 3798 | msg_col = Columns - 1; |
| 3799 | else |
| 3800 | #endif |
| 3801 | msg_col = 0; /* always start in column 0 */ |
| 3802 | if (clr) /* clear the bottom line(s) */ |
| 3803 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3804 | windgoto(cmdline_row, 0); |
| 3805 | } |
| 3806 | |
| 3807 | /* |
| 3808 | * Check the word in front of the cursor for an abbreviation. |
| 3809 | * Called when the non-id character "c" has been entered. |
| 3810 | * When an abbreviation is recognized it is removed from the text with |
| 3811 | * backspaces and the replacement string is inserted, followed by "c". |
| 3812 | */ |
| 3813 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3814 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3815 | { |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3816 | int spos = 0; |
| 3817 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3818 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3819 | return FALSE; |
| 3820 | |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3821 | /* Do not consider '<,'> be part of the mapping, skip leading whitespace. |
| 3822 | * Actually accepts any mark. */ |
| 3823 | while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen) |
| 3824 | spos++; |
| 3825 | if (ccline.cmdlen - spos > 5 |
| 3826 | && ccline.cmdbuff[spos] == '\'' |
| 3827 | && ccline.cmdbuff[spos + 2] == ',' |
| 3828 | && ccline.cmdbuff[spos + 3] == '\'') |
| 3829 | spos += 5; |
| 3830 | else |
| 3831 | /* check abbreviation from the beginning of the commandline */ |
| 3832 | spos = 0; |
| 3833 | |
| 3834 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3837 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3838 | static int |
| 3839 | #ifdef __BORLANDC__ |
| 3840 | _RTLENTRYF |
| 3841 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3842 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3843 | { |
| 3844 | char_u *p1 = *(char_u **)s1; |
| 3845 | char_u *p2 = *(char_u **)s2; |
| 3846 | |
| 3847 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3848 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3849 | return STRCMP(p1, p2); |
| 3850 | } |
| 3851 | #endif |
| 3852 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3853 | /* |
| 3854 | * Return FAIL if this is not an appropriate context in which to do |
| 3855 | * completion of anything, return OK if it is (even if there are no matches). |
| 3856 | * For the caller, this means that the character is just passed through like a |
| 3857 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3858 | */ |
| 3859 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3860 | nextwild( |
| 3861 | expand_T *xp, |
| 3862 | int type, |
| 3863 | int options, /* extra options for ExpandOne() */ |
| 3864 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3865 | { |
| 3866 | int i, j; |
| 3867 | char_u *p1; |
| 3868 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | int difflen; |
| 3870 | int v; |
| 3871 | |
| 3872 | if (xp->xp_numfiles == -1) |
| 3873 | { |
| 3874 | set_expand_context(xp); |
| 3875 | cmd_showtail = expand_showtail(xp); |
| 3876 | } |
| 3877 | |
| 3878 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3879 | { |
| 3880 | beep_flush(); |
| 3881 | return OK; /* Something illegal on command line */ |
| 3882 | } |
| 3883 | if (xp->xp_context == EXPAND_NOTHING) |
| 3884 | { |
| 3885 | /* Caller can use the character as a normal char instead */ |
| 3886 | return FAIL; |
| 3887 | } |
| 3888 | |
| 3889 | MSG_PUTS("..."); /* show that we are busy */ |
| 3890 | out_flush(); |
| 3891 | |
| 3892 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3893 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | |
| 3895 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3896 | { |
| 3897 | /* |
| 3898 | * Get next/previous match for a previous expanded pattern. |
| 3899 | */ |
| 3900 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3901 | } |
| 3902 | else |
| 3903 | { |
| 3904 | /* |
| 3905 | * Translate string into pattern and expand it. |
| 3906 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3907 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3908 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | p2 = NULL; |
| 3910 | else |
| 3911 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3912 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3913 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3914 | if (escape) |
| 3915 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3916 | |
| 3917 | if (p_wic) |
| 3918 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3919 | p2 = ExpandOne(xp, p1, |
| 3920 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3921 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3922 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3923 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3924 | if (p2 != NULL && type == WILD_LONGEST) |
| 3925 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3926 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3927 | if (ccline.cmdbuff[i + j] == '*' |
| 3928 | || ccline.cmdbuff[i + j] == '?') |
| 3929 | break; |
| 3930 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3931 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | if (p2 != NULL && !got_int) |
| 3937 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3938 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3939 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3940 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3941 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3942 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3943 | } |
| 3944 | else |
| 3945 | v = OK; |
| 3946 | if (v == OK) |
| 3947 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3948 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3949 | &ccline.cmdbuff[ccline.cmdpos], |
| 3950 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3951 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | ccline.cmdlen += difflen; |
| 3953 | ccline.cmdpos += difflen; |
| 3954 | } |
| 3955 | } |
| 3956 | vim_free(p2); |
| 3957 | |
| 3958 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3959 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3960 | |
| 3961 | /* When expanding a ":map" command and no matches are found, assume that |
| 3962 | * the key is supposed to be inserted literally */ |
| 3963 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3964 | return FAIL; |
| 3965 | |
| 3966 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3967 | beep_flush(); |
| 3968 | else if (xp->xp_numfiles == 1) |
| 3969 | /* free expanded pattern */ |
| 3970 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3971 | |
| 3972 | return OK; |
| 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | * Do wildcard expansion on the string 'str'. |
| 3977 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3978 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3979 | * Return NULL for failure. |
| 3980 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3981 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3982 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3983 | * WILD_PREV "orig" should be NULL. |
| 3984 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3985 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3986 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3987 | * |
| 3988 | * mode = WILD_FREE: just free previously expanded matches |
| 3989 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3990 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3991 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3992 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3993 | * mode = WILD_ALL: return all matches concatenated |
| 3994 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3995 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | * |
| 3997 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3998 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3999 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 4000 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 4001 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 4002 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 4003 | * options = WILD_SILENT: don't print warning messages |
| 4004 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4005 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4006 | * |
| 4007 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 4008 | */ |
| 4009 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4010 | ExpandOne( |
| 4011 | expand_T *xp, |
| 4012 | char_u *str, |
| 4013 | char_u *orig, /* allocated copy of original of expanded string */ |
| 4014 | int options, |
| 4015 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | { |
| 4017 | char_u *ss = NULL; |
| 4018 | static int findex; |
| 4019 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4020 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | int i; |
| 4022 | long_u len; |
| 4023 | int non_suf_match; /* number without matching suffix */ |
| 4024 | |
| 4025 | /* |
| 4026 | * first handle the case of using an old match |
| 4027 | */ |
| 4028 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 4029 | { |
| 4030 | if (xp->xp_numfiles > 0) |
| 4031 | { |
| 4032 | if (mode == WILD_PREV) |
| 4033 | { |
| 4034 | if (findex == -1) |
| 4035 | findex = xp->xp_numfiles; |
| 4036 | --findex; |
| 4037 | } |
| 4038 | else /* mode == WILD_NEXT */ |
| 4039 | ++findex; |
| 4040 | |
| 4041 | /* |
| 4042 | * When wrapping around, return the original string, set findex to |
| 4043 | * -1. |
| 4044 | */ |
| 4045 | if (findex < 0) |
| 4046 | { |
| 4047 | if (orig_save == NULL) |
| 4048 | findex = xp->xp_numfiles - 1; |
| 4049 | else |
| 4050 | findex = -1; |
| 4051 | } |
| 4052 | if (findex >= xp->xp_numfiles) |
| 4053 | { |
| 4054 | if (orig_save == NULL) |
| 4055 | findex = 0; |
| 4056 | else |
| 4057 | findex = -1; |
| 4058 | } |
| 4059 | #ifdef FEAT_WILDMENU |
| 4060 | if (p_wmnu) |
| 4061 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 4062 | findex, cmd_showtail); |
| 4063 | #endif |
| 4064 | if (findex == -1) |
| 4065 | return vim_strsave(orig_save); |
| 4066 | return vim_strsave(xp->xp_files[findex]); |
| 4067 | } |
| 4068 | else |
| 4069 | return NULL; |
| 4070 | } |
| 4071 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4072 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4073 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 4074 | { |
| 4075 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4076 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4077 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4078 | } |
| 4079 | findex = 0; |
| 4080 | |
| 4081 | if (mode == WILD_FREE) /* only release file name */ |
| 4082 | return NULL; |
| 4083 | |
| 4084 | if (xp->xp_numfiles == -1) |
| 4085 | { |
| 4086 | vim_free(orig_save); |
| 4087 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4088 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4089 | |
| 4090 | /* |
| 4091 | * Do the expansion. |
| 4092 | */ |
| 4093 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 4094 | options) == FAIL) |
| 4095 | { |
| 4096 | #ifdef FNAME_ILLEGAL |
| 4097 | /* Illegal file name has been silently skipped. But when there |
| 4098 | * are wildcards, the real problem is that there was no match, |
| 4099 | * causing the pattern to be added, which has illegal characters. |
| 4100 | */ |
| 4101 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 4102 | EMSG2(_(e_nomatch2), str); |
| 4103 | #endif |
| 4104 | } |
| 4105 | else if (xp->xp_numfiles == 0) |
| 4106 | { |
| 4107 | if (!(options & WILD_SILENT)) |
| 4108 | EMSG2(_(e_nomatch2), str); |
| 4109 | } |
| 4110 | else |
| 4111 | { |
| 4112 | /* Escape the matches for use on the command line. */ |
| 4113 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 4114 | |
| 4115 | /* |
| 4116 | * Check for matching suffixes in file names. |
| 4117 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4118 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 4119 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4120 | { |
| 4121 | if (xp->xp_numfiles) |
| 4122 | non_suf_match = xp->xp_numfiles; |
| 4123 | else |
| 4124 | non_suf_match = 1; |
| 4125 | if ((xp->xp_context == EXPAND_FILES |
| 4126 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4127 | && xp->xp_numfiles > 1) |
| 4128 | { |
| 4129 | /* |
| 4130 | * More than one match; check suffix. |
| 4131 | * The files will have been sorted on matching suffix in |
| 4132 | * expand_wildcards, only need to check the first two. |
| 4133 | */ |
| 4134 | non_suf_match = 0; |
| 4135 | for (i = 0; i < 2; ++i) |
| 4136 | if (match_suffix(xp->xp_files[i])) |
| 4137 | ++non_suf_match; |
| 4138 | } |
| 4139 | if (non_suf_match != 1) |
| 4140 | { |
| 4141 | /* Can we ever get here unless it's while expanding |
| 4142 | * interactively? If not, we can get rid of this all |
| 4143 | * together. Don't really want to wait for this message |
| 4144 | * (and possibly have to hit return to continue!). |
| 4145 | */ |
| 4146 | if (!(options & WILD_SILENT)) |
| 4147 | EMSG(_(e_toomany)); |
| 4148 | else if (!(options & WILD_NO_BEEP)) |
| 4149 | beep_flush(); |
| 4150 | } |
| 4151 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 4152 | ss = vim_strsave(xp->xp_files[0]); |
| 4153 | } |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | /* Find longest common part */ |
| 4158 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 4159 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4160 | int mb_len = 1; |
| 4161 | int c0, ci; |
| 4162 | |
| 4163 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4164 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4165 | #ifdef FEAT_MBYTE |
| 4166 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4167 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4168 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 4169 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 4170 | } |
| 4171 | else |
| 4172 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 4173 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4174 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 4175 | { |
| 4176 | #ifdef FEAT_MBYTE |
| 4177 | if (has_mbyte) |
| 4178 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 4179 | else |
| 4180 | #endif |
| 4181 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4182 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4184 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4185 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4186 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4187 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4188 | break; |
| 4189 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4190 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4191 | break; |
| 4192 | } |
| 4193 | if (i < xp->xp_numfiles) |
| 4194 | { |
| 4195 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 4196 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4197 | break; |
| 4198 | } |
| 4199 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | ss = alloc((unsigned)len + 1); |
| 4202 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4203 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4204 | findex = -1; /* next p_wc gets first one */ |
| 4205 | } |
| 4206 | |
| 4207 | /* Concatenate all matching names */ |
| 4208 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 4209 | { |
| 4210 | len = 0; |
| 4211 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4212 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 4213 | ss = lalloc(len, TRUE); |
| 4214 | if (ss != NULL) |
| 4215 | { |
| 4216 | *ss = NUL; |
| 4217 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4218 | { |
| 4219 | STRCAT(ss, xp->xp_files[i]); |
| 4220 | if (i != xp->xp_numfiles - 1) |
| 4221 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 4222 | } |
| 4223 | } |
| 4224 | } |
| 4225 | |
| 4226 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 4227 | ExpandCleanup(xp); |
| 4228 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4229 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4230 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4231 | vim_free(orig); |
| 4232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4233 | return ss; |
| 4234 | } |
| 4235 | |
| 4236 | /* |
| 4237 | * Prepare an expand structure for use. |
| 4238 | */ |
| 4239 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4240 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4241 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 4242 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4243 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4244 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4245 | #ifndef BACKSLASH_IN_FILENAME |
| 4246 | xp->xp_shell = FALSE; |
| 4247 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4248 | xp->xp_numfiles = -1; |
| 4249 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4250 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4251 | xp->xp_arg = NULL; |
| 4252 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4253 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | /* |
| 4257 | * Cleanup an expand structure after use. |
| 4258 | */ |
| 4259 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4260 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4261 | { |
| 4262 | if (xp->xp_numfiles >= 0) |
| 4263 | { |
| 4264 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4265 | xp->xp_numfiles = -1; |
| 4266 | } |
| 4267 | } |
| 4268 | |
| 4269 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4270 | ExpandEscape( |
| 4271 | expand_T *xp, |
| 4272 | char_u *str, |
| 4273 | int numfiles, |
| 4274 | char_u **files, |
| 4275 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4276 | { |
| 4277 | int i; |
| 4278 | char_u *p; |
| 4279 | |
| 4280 | /* |
| 4281 | * May change home directory back to "~" |
| 4282 | */ |
| 4283 | if (options & WILD_HOME_REPLACE) |
| 4284 | tilde_replace(str, numfiles, files); |
| 4285 | |
| 4286 | if (options & WILD_ESCAPE) |
| 4287 | { |
| 4288 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4289 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4290 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4291 | || xp->xp_context == EXPAND_BUFFERS |
| 4292 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4293 | { |
| 4294 | /* |
| 4295 | * Insert a backslash into a file name before a space, \, %, # |
| 4296 | * and wildmatch characters, except '~'. |
| 4297 | */ |
| 4298 | for (i = 0; i < numfiles; ++i) |
| 4299 | { |
| 4300 | /* for ":set path=" we need to escape spaces twice */ |
| 4301 | if (xp->xp_backslash == XP_BS_THREE) |
| 4302 | { |
| 4303 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4304 | if (p != NULL) |
| 4305 | { |
| 4306 | vim_free(files[i]); |
| 4307 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4308 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4310 | if (p != NULL) |
| 4311 | { |
| 4312 | vim_free(files[i]); |
| 4313 | files[i] = p; |
| 4314 | } |
| 4315 | #endif |
| 4316 | } |
| 4317 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4318 | #ifdef BACKSLASH_IN_FILENAME |
| 4319 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4320 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4321 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4322 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4323 | if (p != NULL) |
| 4324 | { |
| 4325 | vim_free(files[i]); |
| 4326 | files[i] = p; |
| 4327 | } |
| 4328 | |
| 4329 | /* If 'str' starts with "\~", replace "~" at start of |
| 4330 | * files[i] with "\~". */ |
| 4331 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4332 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4333 | } |
| 4334 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4335 | |
| 4336 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4337 | * could be seen as "+cmd". */ |
| 4338 | if (*files[0] == '+') |
| 4339 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4340 | } |
| 4341 | else if (xp->xp_context == EXPAND_TAGS) |
| 4342 | { |
| 4343 | /* |
| 4344 | * Insert a backslash before characters in a tag name that |
| 4345 | * would terminate the ":tag" command. |
| 4346 | */ |
| 4347 | for (i = 0; i < numfiles; ++i) |
| 4348 | { |
| 4349 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4350 | if (p != NULL) |
| 4351 | { |
| 4352 | vim_free(files[i]); |
| 4353 | files[i] = p; |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4361 | * Escape special characters in "fname" for when used as a file name argument |
| 4362 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4363 | * Returns the result in allocated memory. |
| 4364 | */ |
| 4365 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4366 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4367 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4368 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4369 | #ifdef BACKSLASH_IN_FILENAME |
| 4370 | char_u buf[20]; |
| 4371 | int j = 0; |
| 4372 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4373 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4374 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4375 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4376 | buf[j++] = *p; |
| 4377 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4378 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4379 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4380 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4381 | if (shell && csh_like_shell() && p != NULL) |
| 4382 | { |
| 4383 | char_u *s; |
| 4384 | |
| 4385 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4386 | * One is taken by Vim, one by the shell. */ |
| 4387 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4388 | vim_free(p); |
| 4389 | p = s; |
| 4390 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4391 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4392 | |
| 4393 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4394 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4395 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4396 | escape_fname(&p); |
| 4397 | |
| 4398 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4402 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4403 | */ |
| 4404 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4405 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4406 | { |
| 4407 | char_u *p; |
| 4408 | |
| 4409 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4410 | if (p != NULL) |
| 4411 | { |
| 4412 | p[0] = '\\'; |
| 4413 | STRCPY(p + 1, *pp); |
| 4414 | vim_free(*pp); |
| 4415 | *pp = p; |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4420 | * For each file name in files[num_files]: |
| 4421 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4422 | */ |
| 4423 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4424 | tilde_replace( |
| 4425 | char_u *orig_pat, |
| 4426 | int num_files, |
| 4427 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | { |
| 4429 | int i; |
| 4430 | char_u *p; |
| 4431 | |
| 4432 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4433 | { |
| 4434 | for (i = 0; i < num_files; ++i) |
| 4435 | { |
| 4436 | p = home_replace_save(NULL, files[i]); |
| 4437 | if (p != NULL) |
| 4438 | { |
| 4439 | vim_free(files[i]); |
| 4440 | files[i] = p; |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | /* |
| 4447 | * Show all matches for completion on the command line. |
| 4448 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4449 | * be inserted like a normal character. |
| 4450 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4452 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | { |
| 4454 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4455 | int num_files; |
| 4456 | char_u **files_found; |
| 4457 | int i, j, k; |
| 4458 | int maxlen; |
| 4459 | int lines; |
| 4460 | int columns; |
| 4461 | char_u *p; |
| 4462 | int lastlen; |
| 4463 | int attr; |
| 4464 | int showtail; |
| 4465 | |
| 4466 | if (xp->xp_numfiles == -1) |
| 4467 | { |
| 4468 | set_expand_context(xp); |
| 4469 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4470 | &num_files, &files_found); |
| 4471 | showtail = expand_showtail(xp); |
| 4472 | if (i != EXPAND_OK) |
| 4473 | return i; |
| 4474 | |
| 4475 | } |
| 4476 | else |
| 4477 | { |
| 4478 | num_files = xp->xp_numfiles; |
| 4479 | files_found = xp->xp_files; |
| 4480 | showtail = cmd_showtail; |
| 4481 | } |
| 4482 | |
| 4483 | #ifdef FEAT_WILDMENU |
| 4484 | if (!wildmenu) |
| 4485 | { |
| 4486 | #endif |
| 4487 | msg_didany = FALSE; /* lines_left will be set */ |
| 4488 | msg_start(); /* prepare for paging */ |
| 4489 | msg_putchar('\n'); |
| 4490 | out_flush(); |
| 4491 | cmdline_row = msg_row; |
| 4492 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4493 | msg_start(); /* prepare for paging */ |
| 4494 | #ifdef FEAT_WILDMENU |
| 4495 | } |
| 4496 | #endif |
| 4497 | |
| 4498 | if (got_int) |
| 4499 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4500 | #ifdef FEAT_WILDMENU |
| 4501 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4502 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4503 | #endif |
| 4504 | else |
| 4505 | { |
| 4506 | /* find the length of the longest file name */ |
| 4507 | maxlen = 0; |
| 4508 | for (i = 0; i < num_files; ++i) |
| 4509 | { |
| 4510 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4511 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4512 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4513 | { |
| 4514 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4515 | j = vim_strsize(NameBuff); |
| 4516 | } |
| 4517 | else |
| 4518 | j = vim_strsize(L_SHOWFILE(i)); |
| 4519 | if (j > maxlen) |
| 4520 | maxlen = j; |
| 4521 | } |
| 4522 | |
| 4523 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4524 | lines = num_files; |
| 4525 | else |
| 4526 | { |
| 4527 | /* compute the number of columns and lines for the listing */ |
| 4528 | maxlen += 2; /* two spaces between file names */ |
| 4529 | columns = ((int)Columns + 2) / maxlen; |
| 4530 | if (columns < 1) |
| 4531 | columns = 1; |
| 4532 | lines = (num_files + columns - 1) / columns; |
| 4533 | } |
| 4534 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4535 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4536 | |
| 4537 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4538 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4539 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | msg_clr_eos(); |
| 4541 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4542 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | /* list the files line by line */ |
| 4546 | for (i = 0; i < lines; ++i) |
| 4547 | { |
| 4548 | lastlen = 999; |
| 4549 | for (k = i; k < num_files; k += lines) |
| 4550 | { |
| 4551 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4552 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4553 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4554 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4555 | msg_advance(maxlen + 1); |
| 4556 | msg_puts(p); |
| 4557 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4558 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | break; |
| 4560 | } |
| 4561 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4562 | msg_putchar(' '); |
| 4563 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4564 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4565 | || xp->xp_context == EXPAND_BUFFERS) |
| 4566 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4567 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4568 | if (xp->xp_numfiles != -1) |
| 4569 | { |
| 4570 | char_u *halved_slash; |
| 4571 | char_u *exp_path; |
| 4572 | |
| 4573 | /* Expansion was done before and special characters |
| 4574 | * were escaped, need to halve backslashes. Also |
| 4575 | * $HOME has been replaced with ~/. */ |
| 4576 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4577 | halved_slash = backslash_halve_save( |
| 4578 | exp_path != NULL ? exp_path : files_found[k]); |
| 4579 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4580 | : files_found[k]); |
| 4581 | vim_free(exp_path); |
| 4582 | vim_free(halved_slash); |
| 4583 | } |
| 4584 | else |
| 4585 | /* Expansion was done here, file names are literal. */ |
| 4586 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4587 | if (showtail) |
| 4588 | p = L_SHOWFILE(k); |
| 4589 | else |
| 4590 | { |
| 4591 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4592 | TRUE); |
| 4593 | p = NameBuff; |
| 4594 | } |
| 4595 | } |
| 4596 | else |
| 4597 | { |
| 4598 | j = FALSE; |
| 4599 | p = L_SHOWFILE(k); |
| 4600 | } |
| 4601 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4602 | } |
| 4603 | if (msg_col > 0) /* when not wrapped around */ |
| 4604 | { |
| 4605 | msg_clr_eos(); |
| 4606 | msg_putchar('\n'); |
| 4607 | } |
| 4608 | out_flush(); /* show one line at a time */ |
| 4609 | if (got_int) |
| 4610 | { |
| 4611 | got_int = FALSE; |
| 4612 | break; |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | /* |
| 4617 | * we redraw the command below the lines that we have just listed |
| 4618 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4619 | */ |
| 4620 | cmdline_row = msg_row; /* will put it back later */ |
| 4621 | } |
| 4622 | |
| 4623 | if (xp->xp_numfiles == -1) |
| 4624 | FreeWild(num_files, files_found); |
| 4625 | |
| 4626 | return EXPAND_OK; |
| 4627 | } |
| 4628 | |
| 4629 | /* |
| 4630 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4631 | * Find tail of file name path, but ignore trailing "/". |
| 4632 | */ |
| 4633 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4634 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4635 | { |
| 4636 | char_u *p; |
| 4637 | char_u *t = s; |
| 4638 | int had_sep = FALSE; |
| 4639 | |
| 4640 | for (p = s; *p != NUL; ) |
| 4641 | { |
| 4642 | if (vim_ispathsep(*p) |
| 4643 | #ifdef BACKSLASH_IN_FILENAME |
| 4644 | && !rem_backslash(p) |
| 4645 | #endif |
| 4646 | ) |
| 4647 | had_sep = TRUE; |
| 4648 | else if (had_sep) |
| 4649 | { |
| 4650 | t = p; |
| 4651 | had_sep = FALSE; |
| 4652 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4653 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4654 | } |
| 4655 | return t; |
| 4656 | } |
| 4657 | |
| 4658 | /* |
| 4659 | * Return TRUE if we only need to show the tail of completion matches. |
| 4660 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4661 | * returned. |
| 4662 | */ |
| 4663 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4664 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4665 | { |
| 4666 | char_u *s; |
| 4667 | char_u *end; |
| 4668 | |
| 4669 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4670 | if (xp->xp_context != EXPAND_FILES |
| 4671 | && xp->xp_context != EXPAND_SHELLCMD |
| 4672 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4673 | return FALSE; |
| 4674 | |
| 4675 | end = gettail(xp->xp_pattern); |
| 4676 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4677 | return FALSE; |
| 4678 | |
| 4679 | for (s = xp->xp_pattern; s < end; s++) |
| 4680 | { |
| 4681 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4682 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4683 | if (rem_backslash(s)) |
| 4684 | ++s; |
| 4685 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4686 | return FALSE; |
| 4687 | } |
| 4688 | return TRUE; |
| 4689 | } |
| 4690 | |
| 4691 | /* |
| 4692 | * Prepare a string for expansion. |
| 4693 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4694 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4695 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4696 | * the name into allocated memory and prepend "^". |
| 4697 | */ |
| 4698 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4699 | addstar( |
| 4700 | char_u *fname, |
| 4701 | int len, |
| 4702 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4703 | { |
| 4704 | char_u *retval; |
| 4705 | int i, j; |
| 4706 | int new_len; |
| 4707 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4708 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4710 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4711 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4712 | && context != EXPAND_SHELLCMD |
| 4713 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | { |
| 4715 | /* |
| 4716 | * Matching will be done internally (on something other than files). |
| 4717 | * So we convert the file-matching-type wildcards into our kind for |
| 4718 | * use with vim_regcomp(). First work out how long it will be: |
| 4719 | */ |
| 4720 | |
| 4721 | /* For help tags the translation is done in find_help_tags(). |
| 4722 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4723 | if (context == EXPAND_HELP |
| 4724 | || context == EXPAND_COLORS |
| 4725 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4726 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4727 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4728 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4729 | || ((context == EXPAND_TAGS_LISTFILES |
| 4730 | || context == EXPAND_TAGS) |
| 4731 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4732 | retval = vim_strnsave(fname, len); |
| 4733 | else |
| 4734 | { |
| 4735 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4736 | for (i = 0; i < len; i++) |
| 4737 | { |
| 4738 | if (fname[i] == '*' || fname[i] == '~') |
| 4739 | new_len++; /* '*' needs to be replaced by ".*" |
| 4740 | '~' needs to be replaced by "\~" */ |
| 4741 | |
| 4742 | /* Buffer names are like file names. "." should be literal */ |
| 4743 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4744 | new_len++; /* "." becomes "\." */ |
| 4745 | |
| 4746 | /* Custom expansion takes care of special things, match |
| 4747 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4748 | if ((context == EXPAND_USER_DEFINED |
| 4749 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | new_len++; /* '\' becomes "\\" */ |
| 4751 | } |
| 4752 | retval = alloc(new_len); |
| 4753 | if (retval != NULL) |
| 4754 | { |
| 4755 | retval[0] = '^'; |
| 4756 | j = 1; |
| 4757 | for (i = 0; i < len; i++, j++) |
| 4758 | { |
| 4759 | /* Skip backslash. But why? At least keep it for custom |
| 4760 | * expansion. */ |
| 4761 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4762 | && context != EXPAND_USER_LIST |
| 4763 | && fname[i] == '\\' |
| 4764 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4765 | break; |
| 4766 | |
| 4767 | switch (fname[i]) |
| 4768 | { |
| 4769 | case '*': retval[j++] = '.'; |
| 4770 | break; |
| 4771 | case '~': retval[j++] = '\\'; |
| 4772 | break; |
| 4773 | case '?': retval[j] = '.'; |
| 4774 | continue; |
| 4775 | case '.': if (context == EXPAND_BUFFERS) |
| 4776 | retval[j++] = '\\'; |
| 4777 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4778 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4779 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | retval[j++] = '\\'; |
| 4781 | break; |
| 4782 | } |
| 4783 | retval[j] = fname[i]; |
| 4784 | } |
| 4785 | retval[j] = NUL; |
| 4786 | } |
| 4787 | } |
| 4788 | } |
| 4789 | else |
| 4790 | { |
| 4791 | retval = alloc(len + 4); |
| 4792 | if (retval != NULL) |
| 4793 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4794 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4795 | |
| 4796 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4797 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4798 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | * ~ would be at the start of the file name, but not the tail. |
| 4800 | * $ could be anywhere in the tail. |
| 4801 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4802 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4803 | */ |
| 4804 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4805 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4806 | #ifndef BACKSLASH_IN_FILENAME |
| 4807 | for (i = len - 2; i >= 0; --i) |
| 4808 | { |
| 4809 | if (retval[i] != '\\') |
| 4810 | break; |
| 4811 | ends_in_star = !ends_in_star; |
| 4812 | } |
| 4813 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4814 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4815 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4816 | && vim_strchr(tail, '$') == NULL |
| 4817 | && vim_strchr(retval, '`') == NULL) |
| 4818 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4819 | else if (len > 0 && retval[len - 1] == '$') |
| 4820 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | retval[len] = NUL; |
| 4822 | } |
| 4823 | } |
| 4824 | return retval; |
| 4825 | } |
| 4826 | |
| 4827 | /* |
| 4828 | * Must parse the command line so far to work out what context we are in. |
| 4829 | * Completion can then be done based on that context. |
| 4830 | * This routine sets the variables: |
| 4831 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4832 | * the command line (ends at the cursor). |
| 4833 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4834 | * |
| 4835 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4836 | * the command line, like an unknown command. Caller |
| 4837 | * should beep. |
| 4838 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4839 | * a normal char, rather than for completion. eg |
| 4840 | * :s/^I/ |
| 4841 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4842 | * it. |
| 4843 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4844 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4845 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4846 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4847 | * when we know only directories are of interest. eg |
| 4848 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4849 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4850 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4851 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4852 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4853 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4854 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4855 | * EXPAND_EVENTS Complete event names |
| 4856 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4857 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4858 | * EXPAND_AUGROUP Complete autocommand group names |
| 4859 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4860 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4861 | * eg :unmap a^I , :cunab x^I |
| 4862 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4863 | * eg :call sub^I |
| 4864 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4865 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4866 | * names in expressions, eg :while s^I |
| 4867 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4868 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4869 | */ |
| 4870 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4871 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4872 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4873 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4874 | if (ccline.cmdfirstc != ':' |
| 4875 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4876 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4877 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | #endif |
| 4879 | ) |
| 4880 | { |
| 4881 | xp->xp_context = EXPAND_NOTHING; |
| 4882 | return; |
| 4883 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4884 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4888 | set_cmd_context( |
| 4889 | expand_T *xp, |
| 4890 | char_u *str, /* start of command line */ |
| 4891 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4892 | int col, /* position of cursor */ |
| 4893 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4894 | { |
| 4895 | int old_char = NUL; |
| 4896 | char_u *nextcomm; |
| 4897 | |
| 4898 | /* |
| 4899 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4900 | * written before. |
| 4901 | */ |
| 4902 | if (col < len) |
| 4903 | old_char = str[col]; |
| 4904 | str[col] = NUL; |
| 4905 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4906 | |
| 4907 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4908 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4909 | { |
| 4910 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4911 | /* pass CMD_SIZE because there is no real command */ |
| 4912 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4913 | # endif |
| 4914 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4915 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4916 | { |
| 4917 | xp->xp_context = ccline.xp_context; |
| 4918 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4919 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4920 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4921 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4922 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4923 | else |
| 4924 | #endif |
| 4925 | while (nextcomm != NULL) |
| 4926 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4927 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4928 | /* Store the string here so that call_user_expand_func() can get to them |
| 4929 | * easily. */ |
| 4930 | xp->xp_line = str; |
| 4931 | xp->xp_col = col; |
| 4932 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4933 | str[col] = old_char; |
| 4934 | } |
| 4935 | |
| 4936 | /* |
| 4937 | * Expand the command line "str" from context "xp". |
| 4938 | * "xp" must have been set by set_cmd_context(). |
| 4939 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4940 | * starts. |
| 4941 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4942 | * cursor. |
| 4943 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4944 | * key that triggered expansion literally. |
| 4945 | * Returns EXPAND_OK otherwise. |
| 4946 | */ |
| 4947 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4948 | expand_cmdline( |
| 4949 | expand_T *xp, |
| 4950 | char_u *str, /* start of command line */ |
| 4951 | int col, /* position of cursor */ |
| 4952 | int *matchcount, /* return: nr of matches */ |
| 4953 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | { |
| 4955 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4956 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4957 | |
| 4958 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4959 | { |
| 4960 | beep_flush(); |
| 4961 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4962 | } |
| 4963 | if (xp->xp_context == EXPAND_NOTHING) |
| 4964 | { |
| 4965 | /* Caller can use the character as a normal char instead */ |
| 4966 | return EXPAND_NOTHING; |
| 4967 | } |
| 4968 | |
| 4969 | /* 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] | 4970 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4971 | 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] | 4972 | if (file_str == NULL) |
| 4973 | return EXPAND_UNSUCCESSFUL; |
| 4974 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4975 | if (p_wic) |
| 4976 | options += WILD_ICASE; |
| 4977 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4978 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4979 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4980 | { |
| 4981 | *matchcount = 0; |
| 4982 | *matches = NULL; |
| 4983 | } |
| 4984 | vim_free(file_str); |
| 4985 | |
| 4986 | return EXPAND_OK; |
| 4987 | } |
| 4988 | |
| 4989 | #ifdef FEAT_MULTI_LANG |
| 4990 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4991 | * Cleanup matches for help tags: |
| 4992 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4993 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4994 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4995 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4996 | |
| 4997 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4998 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | { |
| 5000 | int i, j; |
| 5001 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5002 | char_u buf[4]; |
| 5003 | char_u *p = buf; |
| 5004 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5005 | 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] | 5006 | { |
| 5007 | *p++ = '@'; |
| 5008 | *p++ = p_hlg[0]; |
| 5009 | *p++ = p_hlg[1]; |
| 5010 | } |
| 5011 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | |
| 5013 | for (i = 0; i < num_file; ++i) |
| 5014 | { |
| 5015 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5016 | if (len <= 0) |
| 5017 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5018 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | { |
| 5020 | /* Sorting on priority means the same item in another language may |
| 5021 | * be anywhere. Search all items for a match up to the "@en". */ |
| 5022 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5023 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 5024 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5025 | break; |
| 5026 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5027 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5028 | file[i][len] = NUL; |
| 5029 | } |
| 5030 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5031 | |
| 5032 | if (*buf != NUL) |
| 5033 | for (i = 0; i < num_file; ++i) |
| 5034 | { |
| 5035 | len = (int)STRLEN(file[i]) - 3; |
| 5036 | if (len <= 0) |
| 5037 | continue; |
| 5038 | if (STRCMP(file[i] + len, buf) == 0) |
| 5039 | { |
| 5040 | /* remove the default language */ |
| 5041 | file[i][len] = NUL; |
| 5042 | } |
| 5043 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | } |
| 5045 | #endif |
| 5046 | |
| 5047 | /* |
| 5048 | * Do the expansion based on xp->xp_context and "pat". |
| 5049 | */ |
| 5050 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5051 | ExpandFromContext( |
| 5052 | expand_T *xp, |
| 5053 | char_u *pat, |
| 5054 | int *num_file, |
| 5055 | char_u ***file, |
| 5056 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5057 | { |
| 5058 | #ifdef FEAT_CMDL_COMPL |
| 5059 | regmatch_T regmatch; |
| 5060 | #endif |
| 5061 | int ret; |
| 5062 | int flags; |
| 5063 | |
| 5064 | flags = EW_DIR; /* include directories */ |
| 5065 | if (options & WILD_LIST_NOTFOUND) |
| 5066 | flags |= EW_NOTFOUND; |
| 5067 | if (options & WILD_ADD_SLASH) |
| 5068 | flags |= EW_ADDSLASH; |
| 5069 | if (options & WILD_KEEP_ALL) |
| 5070 | flags |= EW_KEEPALL; |
| 5071 | if (options & WILD_SILENT) |
| 5072 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 5073 | if (options & WILD_ALLLINKS) |
| 5074 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5075 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5076 | if (xp->xp_context == EXPAND_FILES |
| 5077 | || xp->xp_context == EXPAND_DIRECTORIES |
| 5078 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5079 | { |
| 5080 | /* |
| 5081 | * Expand file or directory names. |
| 5082 | */ |
| 5083 | int free_pat = FALSE; |
| 5084 | int i; |
| 5085 | |
| 5086 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5087 | * space */ |
| 5088 | if (xp->xp_backslash != XP_BS_NONE) |
| 5089 | { |
| 5090 | free_pat = TRUE; |
| 5091 | pat = vim_strsave(pat); |
| 5092 | for (i = 0; pat[i]; ++i) |
| 5093 | if (pat[i] == '\\') |
| 5094 | { |
| 5095 | if (xp->xp_backslash == XP_BS_THREE |
| 5096 | && pat[i + 1] == '\\' |
| 5097 | && pat[i + 2] == '\\' |
| 5098 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5099 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5100 | if (xp->xp_backslash == XP_BS_ONE |
| 5101 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5102 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | if (xp->xp_context == EXPAND_FILES) |
| 5107 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5108 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 5109 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5110 | else |
| 5111 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5112 | if (options & WILD_ICASE) |
| 5113 | flags |= EW_ICASE; |
| 5114 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5115 | /* Expand wildcards, supporting %:h and the like. */ |
| 5116 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | if (free_pat) |
| 5118 | vim_free(pat); |
| 5119 | return ret; |
| 5120 | } |
| 5121 | |
| 5122 | *file = (char_u **)""; |
| 5123 | *num_file = 0; |
| 5124 | if (xp->xp_context == EXPAND_HELP) |
| 5125 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 5126 | /* With an empty argument we would get all the help tags, which is |
| 5127 | * very slow. Get matches for "help" instead. */ |
| 5128 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 5129 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5130 | { |
| 5131 | #ifdef FEAT_MULTI_LANG |
| 5132 | cleanup_help_tags(*num_file, *file); |
| 5133 | #endif |
| 5134 | return OK; |
| 5135 | } |
| 5136 | return FAIL; |
| 5137 | } |
| 5138 | |
| 5139 | #ifndef FEAT_CMDL_COMPL |
| 5140 | return FAIL; |
| 5141 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5142 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 5143 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5144 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 5145 | return ExpandOldSetting(num_file, file); |
| 5146 | if (xp->xp_context == EXPAND_BUFFERS) |
| 5147 | return ExpandBufnames(pat, num_file, file, options); |
| 5148 | if (xp->xp_context == EXPAND_TAGS |
| 5149 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 5150 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 5151 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5152 | { |
| 5153 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5154 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 5155 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5156 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5157 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5158 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 5159 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5160 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5161 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5162 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5163 | { |
| 5164 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5165 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5166 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5167 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5168 | { |
| 5169 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5170 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5171 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5172 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5173 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5174 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5175 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5176 | if (xp->xp_context == EXPAND_PACKADD) |
| 5177 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5178 | |
| 5179 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5180 | if (regmatch.regprog == NULL) |
| 5181 | return FAIL; |
| 5182 | |
| 5183 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 5184 | regmatch.rm_ic = ignorecase(pat); |
| 5185 | |
| 5186 | if (xp->xp_context == EXPAND_SETTINGS |
| 5187 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 5188 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 5189 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 5190 | ret = ExpandMappings(®match, num_file, file); |
| 5191 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5192 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 5193 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 5194 | # endif |
| 5195 | else |
| 5196 | { |
| 5197 | static struct expgen |
| 5198 | { |
| 5199 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 5200 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5201 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5202 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | } tab[] = |
| 5204 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5205 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 5206 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 5207 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 5208 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5209 | #ifdef FEAT_CMDHIST |
| 5210 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 5211 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5212 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5213 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 5214 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5215 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 5216 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 5217 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5218 | #endif |
| 5219 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5220 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 5221 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 5222 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 5223 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5224 | #endif |
| 5225 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5226 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 5227 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5228 | #endif |
| 5229 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5230 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5231 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 5232 | #ifdef FEAT_PROFILE |
| 5233 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 5234 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5235 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5236 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 5237 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5238 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5239 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5240 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5241 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5242 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5243 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5244 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5245 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5246 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5247 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5248 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5249 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5250 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5252 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5253 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5254 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5255 | }; |
| 5256 | int i; |
| 5257 | |
| 5258 | /* |
| 5259 | * Find a context in the table and call the ExpandGeneric() with the |
| 5260 | * right function to do the expansion. |
| 5261 | */ |
| 5262 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5263 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5264 | if (xp->xp_context == tab[i].context) |
| 5265 | { |
| 5266 | if (tab[i].ic) |
| 5267 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5268 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5269 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5270 | break; |
| 5271 | } |
| 5272 | } |
| 5273 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5274 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5275 | |
| 5276 | return ret; |
| 5277 | #endif /* FEAT_CMDL_COMPL */ |
| 5278 | } |
| 5279 | |
| 5280 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5281 | /* |
| 5282 | * Expand a list of names. |
| 5283 | * |
| 5284 | * Generic function for command line completion. It calls a function to |
| 5285 | * obtain strings, one by one. The strings are matched against a regexp |
| 5286 | * program. Matching strings are copied into an array, which is returned. |
| 5287 | * |
| 5288 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5289 | */ |
| 5290 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5291 | ExpandGeneric( |
| 5292 | expand_T *xp, |
| 5293 | regmatch_T *regmatch, |
| 5294 | int *num_file, |
| 5295 | char_u ***file, |
| 5296 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5297 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5298 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5299 | { |
| 5300 | int i; |
| 5301 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5302 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5303 | char_u *str; |
| 5304 | |
| 5305 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5306 | * round == 0: count the number of matching names |
| 5307 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5308 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5309 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5310 | { |
| 5311 | for (i = 0; ; ++i) |
| 5312 | { |
| 5313 | str = (*func)(xp, i); |
| 5314 | if (str == NULL) /* end of list */ |
| 5315 | break; |
| 5316 | if (*str == NUL) /* skip empty strings */ |
| 5317 | continue; |
| 5318 | |
| 5319 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5320 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5321 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5322 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5323 | if (escaped) |
| 5324 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5325 | else |
| 5326 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5327 | (*file)[count] = str; |
| 5328 | #ifdef FEAT_MENU |
| 5329 | if (func == get_menu_names && str != NULL) |
| 5330 | { |
| 5331 | /* test for separator added by get_menu_names() */ |
| 5332 | str += STRLEN(str) - 1; |
| 5333 | if (*str == '\001') |
| 5334 | *str = '.'; |
| 5335 | } |
| 5336 | #endif |
| 5337 | } |
| 5338 | ++count; |
| 5339 | } |
| 5340 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5341 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5342 | { |
| 5343 | if (count == 0) |
| 5344 | return OK; |
| 5345 | *num_file = count; |
| 5346 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5347 | if (*file == NULL) |
| 5348 | { |
| 5349 | *file = (char_u **)""; |
| 5350 | return FAIL; |
| 5351 | } |
| 5352 | count = 0; |
| 5353 | } |
| 5354 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5355 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5356 | /* Sort the results. Keep menu's in the specified order. */ |
| 5357 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5358 | { |
| 5359 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5360 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5361 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5362 | /* <SNR> functions should be sorted to the end. */ |
| 5363 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5364 | sort_func_compare); |
| 5365 | else |
| 5366 | sort_strings(*file, *num_file); |
| 5367 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5368 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5369 | #ifdef FEAT_CMDL_COMPL |
| 5370 | /* Reset the variables used for special highlight names expansion, so that |
| 5371 | * they don't show up when getting normal highlight names by ID. */ |
| 5372 | reset_expand_highlight(); |
| 5373 | #endif |
| 5374 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5375 | return OK; |
| 5376 | } |
| 5377 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5378 | /* |
| 5379 | * Complete a shell command. |
| 5380 | * Returns FAIL or OK; |
| 5381 | */ |
| 5382 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5383 | expand_shellcmd( |
| 5384 | char_u *filepat, /* pattern to match with command names */ |
| 5385 | int *num_file, /* return: number of matches */ |
| 5386 | char_u ***file, /* return: array with matches */ |
| 5387 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5388 | { |
| 5389 | char_u *pat; |
| 5390 | int i; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5391 | char_u *path = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5392 | int mustfree = FALSE; |
| 5393 | garray_T ga; |
| 5394 | char_u *buf = alloc(MAXPATHL); |
| 5395 | size_t l; |
| 5396 | char_u *s, *e; |
| 5397 | int flags = flagsarg; |
| 5398 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5399 | int did_curdir = FALSE; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5400 | hashtab_T found_ht; |
| 5401 | hashitem_T *hi; |
| 5402 | hash_T hash; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5403 | |
| 5404 | if (buf == NULL) |
| 5405 | return FAIL; |
| 5406 | |
| 5407 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5408 | * space */ |
| 5409 | pat = vim_strsave(filepat); |
| 5410 | for (i = 0; pat[i]; ++i) |
| 5411 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5412 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5413 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5414 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5415 | |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5416 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 5417 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5418 | path = (char_u *)"."; |
| 5419 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5420 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5421 | /* For an absolute name we don't use $PATH. */ |
| 5422 | if (!mch_isFullName(pat)) |
| 5423 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5424 | if (path == NULL) |
| 5425 | path = (char_u *)""; |
| 5426 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5427 | |
| 5428 | /* |
| 5429 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5430 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5431 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5432 | */ |
| 5433 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5434 | hash_init(&found_ht); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5435 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5436 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5437 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5438 | e = vim_strchr(s, ';'); |
| 5439 | #else |
| 5440 | e = vim_strchr(s, ':'); |
| 5441 | #endif |
| 5442 | if (e == NULL) |
| 5443 | e = s + STRLEN(s); |
| 5444 | |
Bram Moolenaar | 6ab9e42 | 2018-07-28 19:20:13 +0200 | [diff] [blame] | 5445 | if (*s == NUL) |
| 5446 | { |
| 5447 | if (did_curdir) |
| 5448 | break; |
| 5449 | // Find directories in the current directory, path is empty. |
| 5450 | did_curdir = TRUE; |
| 5451 | flags |= EW_DIR; |
| 5452 | } |
| 5453 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 5454 | { |
| 5455 | did_curdir = TRUE; |
| 5456 | flags |= EW_DIR; |
| 5457 | } |
| 5458 | else |
| 5459 | // Do not match directories inside a $PATH item. |
| 5460 | flags &= ~EW_DIR; |
| 5461 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5462 | l = e - s; |
| 5463 | if (l > MAXPATHL - 5) |
| 5464 | break; |
| 5465 | vim_strncpy(buf, s, l); |
| 5466 | add_pathsep(buf); |
| 5467 | l = STRLEN(buf); |
| 5468 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5469 | |
| 5470 | /* Expand matches in one directory of $PATH. */ |
| 5471 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5472 | if (ret == OK) |
| 5473 | { |
| 5474 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5475 | FreeWild(*num_file, *file); |
| 5476 | else |
| 5477 | { |
| 5478 | for (i = 0; i < *num_file; ++i) |
| 5479 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5480 | char_u *name = (*file)[i]; |
| 5481 | |
| 5482 | if (STRLEN(name) > l) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5483 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5484 | // Check if this name was already found. |
| 5485 | hash = hash_hash(name + l); |
| 5486 | hi = hash_lookup(&found_ht, name + l, hash); |
| 5487 | if (HASHITEM_EMPTY(hi)) |
| 5488 | { |
| 5489 | // Remove the path that was prepended. |
| 5490 | STRMOVE(name, name + l); |
| 5491 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 5492 | hash_add_item(&found_ht, hi, name, hash); |
| 5493 | name = NULL; |
| 5494 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5495 | } |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5496 | vim_free(name); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5497 | } |
| 5498 | vim_free(*file); |
| 5499 | } |
| 5500 | } |
| 5501 | if (*e != NUL) |
| 5502 | ++e; |
| 5503 | } |
| 5504 | *file = ga.ga_data; |
| 5505 | *num_file = ga.ga_len; |
| 5506 | |
| 5507 | vim_free(buf); |
| 5508 | vim_free(pat); |
| 5509 | if (mustfree) |
| 5510 | vim_free(path); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5511 | hash_clear(&found_ht); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5512 | return OK; |
| 5513 | } |
| 5514 | |
| 5515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5516 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5517 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5518 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5519 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5520 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5521 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5522 | call_user_expand_func( |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5523 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5524 | expand_T *xp, |
| 5525 | int *num_file, |
| 5526 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5527 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5528 | int keep = 0; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5529 | typval_T args[4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5530 | int save_current_SID = current_SID; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5531 | char_u *pat = NULL; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5532 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5533 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5534 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5535 | 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] | 5536 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5537 | *num_file = 0; |
| 5538 | *file = NULL; |
| 5539 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5540 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5541 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5542 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5543 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5544 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5545 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5546 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 5547 | |
| 5548 | args[0].v_type = VAR_STRING; |
| 5549 | args[0].vval.v_string = pat; |
| 5550 | args[1].v_type = VAR_STRING; |
| 5551 | args[1].vval.v_string = xp->xp_line; |
| 5552 | args[2].v_type = VAR_NUMBER; |
| 5553 | args[2].vval.v_number = xp->xp_col; |
| 5554 | args[3].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5555 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5556 | /* Save the cmdline, we don't know what the function may do. */ |
| 5557 | save_ccline = ccline; |
| 5558 | ccline.cmdbuff = NULL; |
| 5559 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5560 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5561 | |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5562 | ret = user_expand_func(xp->xp_arg, 3, args); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5563 | |
| 5564 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5565 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5566 | if (ccline.cmdbuff != NULL) |
| 5567 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5568 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5569 | vim_free(pat); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5570 | return ret; |
| 5571 | } |
| 5572 | |
| 5573 | /* |
| 5574 | * Expand names with a function defined by the user. |
| 5575 | */ |
| 5576 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5577 | ExpandUserDefined( |
| 5578 | expand_T *xp, |
| 5579 | regmatch_T *regmatch, |
| 5580 | int *num_file, |
| 5581 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5582 | { |
| 5583 | char_u *retstr; |
| 5584 | char_u *s; |
| 5585 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5586 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5587 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5588 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5589 | |
| 5590 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5591 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5592 | return FAIL; |
| 5593 | |
| 5594 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5595 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5596 | { |
| 5597 | e = vim_strchr(s, '\n'); |
| 5598 | if (e == NULL) |
| 5599 | e = s + STRLEN(s); |
| 5600 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5601 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5602 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5603 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5604 | *e = keep; |
| 5605 | |
| 5606 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5608 | if (ga_grow(&ga, 1) == FAIL) |
| 5609 | break; |
| 5610 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5611 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5614 | if (*e != NUL) |
| 5615 | ++e; |
| 5616 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5617 | vim_free(retstr); |
| 5618 | *file = ga.ga_data; |
| 5619 | *num_file = ga.ga_len; |
| 5620 | return OK; |
| 5621 | } |
| 5622 | |
| 5623 | /* |
| 5624 | * Expand names with a list returned by a function defined by the user. |
| 5625 | */ |
| 5626 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5627 | ExpandUserList( |
| 5628 | expand_T *xp, |
| 5629 | int *num_file, |
| 5630 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5631 | { |
| 5632 | list_T *retlist; |
| 5633 | listitem_T *li; |
| 5634 | garray_T ga; |
| 5635 | |
| 5636 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5637 | if (retlist == NULL) |
| 5638 | return FAIL; |
| 5639 | |
| 5640 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5641 | /* Loop over the items in the list. */ |
| 5642 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5643 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5644 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5645 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5646 | |
| 5647 | if (ga_grow(&ga, 1) == FAIL) |
| 5648 | break; |
| 5649 | |
| 5650 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5651 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5652 | ++ga.ga_len; |
| 5653 | } |
| 5654 | list_unref(retlist); |
| 5655 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | *file = ga.ga_data; |
| 5657 | *num_file = ga.ga_len; |
| 5658 | return OK; |
| 5659 | } |
| 5660 | #endif |
| 5661 | |
| 5662 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5663 | * Expand color scheme, compiler or filetype names. |
| 5664 | * Search from 'runtimepath': |
| 5665 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5666 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5667 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5668 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5669 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5670 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5671 | */ |
| 5672 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5673 | ExpandRTDir( |
| 5674 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5675 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5676 | int *num_file, |
| 5677 | char_u ***file, |
| 5678 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5679 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5680 | char_u *s; |
| 5681 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5682 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5683 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5684 | int i; |
| 5685 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5686 | |
| 5687 | *num_file = 0; |
| 5688 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5689 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5690 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5692 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5693 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5694 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5695 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5697 | ga_clear_strings(&ga); |
| 5698 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5699 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5700 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5701 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5702 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5703 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5704 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5705 | if (flags & DIP_START) { |
| 5706 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5707 | { |
| 5708 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5709 | if (s == NULL) |
| 5710 | { |
| 5711 | ga_clear_strings(&ga); |
| 5712 | return FAIL; |
| 5713 | } |
| 5714 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5715 | globpath(p_pp, s, &ga, 0); |
| 5716 | vim_free(s); |
| 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | if (flags & DIP_OPT) { |
| 5721 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5722 | { |
| 5723 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5724 | if (s == NULL) |
| 5725 | { |
| 5726 | ga_clear_strings(&ga); |
| 5727 | return FAIL; |
| 5728 | } |
| 5729 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5730 | globpath(p_pp, s, &ga, 0); |
| 5731 | vim_free(s); |
| 5732 | } |
| 5733 | } |
| 5734 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5735 | for (i = 0; i < ga.ga_len; ++i) |
| 5736 | { |
| 5737 | match = ((char_u **)ga.ga_data)[i]; |
| 5738 | s = match; |
| 5739 | e = s + STRLEN(s); |
| 5740 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5741 | { |
| 5742 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5743 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5744 | if (s < match || vim_ispathsep(*s)) |
| 5745 | break; |
| 5746 | ++s; |
| 5747 | *e = NUL; |
| 5748 | mch_memmove(match, s, e - s + 1); |
| 5749 | } |
| 5750 | } |
| 5751 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5752 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5753 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5754 | |
| 5755 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5756 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5757 | remove_duplicates(&ga); |
| 5758 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5759 | *file = ga.ga_data; |
| 5760 | *num_file = ga.ga_len; |
| 5761 | return OK; |
| 5762 | } |
| 5763 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5764 | /* |
| 5765 | * Expand loadplugin names: |
| 5766 | * 'packpath'/pack/ * /opt/{pat} |
| 5767 | */ |
| 5768 | static int |
| 5769 | ExpandPackAddDir( |
| 5770 | char_u *pat, |
| 5771 | int *num_file, |
| 5772 | char_u ***file) |
| 5773 | { |
| 5774 | char_u *s; |
| 5775 | char_u *e; |
| 5776 | char_u *match; |
| 5777 | garray_T ga; |
| 5778 | int i; |
| 5779 | int pat_len; |
| 5780 | |
| 5781 | *num_file = 0; |
| 5782 | *file = NULL; |
| 5783 | pat_len = (int)STRLEN(pat); |
| 5784 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5785 | |
| 5786 | s = alloc((unsigned)(pat_len + 26)); |
| 5787 | if (s == NULL) |
| 5788 | { |
| 5789 | ga_clear_strings(&ga); |
| 5790 | return FAIL; |
| 5791 | } |
| 5792 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5793 | globpath(p_pp, s, &ga, 0); |
| 5794 | vim_free(s); |
| 5795 | |
| 5796 | for (i = 0; i < ga.ga_len; ++i) |
| 5797 | { |
| 5798 | match = ((char_u **)ga.ga_data)[i]; |
| 5799 | s = gettail(match); |
| 5800 | e = s + STRLEN(s); |
| 5801 | mch_memmove(match, s, e - s + 1); |
| 5802 | } |
| 5803 | |
| 5804 | if (ga.ga_len == 0) |
| 5805 | return FAIL; |
| 5806 | |
| 5807 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5808 | * directories in dirnames. */ |
| 5809 | remove_duplicates(&ga); |
| 5810 | |
| 5811 | *file = ga.ga_data; |
| 5812 | *num_file = ga.ga_len; |
| 5813 | return OK; |
| 5814 | } |
| 5815 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5816 | #endif |
| 5817 | |
| 5818 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5819 | /* |
| 5820 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5821 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5822 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5823 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5824 | globpath( |
| 5825 | char_u *path, |
| 5826 | char_u *file, |
| 5827 | garray_T *ga, |
| 5828 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | { |
| 5830 | expand_T xpc; |
| 5831 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5832 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5833 | int num_p; |
| 5834 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5835 | |
| 5836 | buf = alloc(MAXPATHL); |
| 5837 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5838 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5839 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5840 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5842 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5843 | /* Loop over all entries in {path}. */ |
| 5844 | while (*path != NUL) |
| 5845 | { |
| 5846 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5847 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5848 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5849 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5850 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5851 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5852 | * treat it as an escape character, use '/' instead. */ |
| 5853 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5854 | STRCAT(buf, "/"); |
| 5855 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5856 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5857 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5858 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5859 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5860 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5861 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5862 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5863 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5864 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5865 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5866 | for (i = 0; i < num_p; ++i) |
| 5867 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5868 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5869 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5870 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5872 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5873 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5874 | FreeWild(num_p, p); |
| 5875 | } |
| 5876 | } |
| 5877 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5878 | |
| 5879 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | #endif |
| 5883 | |
| 5884 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5885 | |
| 5886 | /********************************* |
| 5887 | * Command line history stuff * |
| 5888 | *********************************/ |
| 5889 | |
| 5890 | /* |
| 5891 | * Translate a history character to the associated type number. |
| 5892 | */ |
| 5893 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5894 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5895 | { |
| 5896 | if (c == ':') |
| 5897 | return HIST_CMD; |
| 5898 | if (c == '=') |
| 5899 | return HIST_EXPR; |
| 5900 | if (c == '@') |
| 5901 | return HIST_INPUT; |
| 5902 | if (c == '>') |
| 5903 | return HIST_DEBUG; |
| 5904 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5905 | } |
| 5906 | |
| 5907 | /* |
| 5908 | * Table of history names. |
| 5909 | * These names are used in :history and various hist...() functions. |
| 5910 | * It is sufficient to give the significant prefix of a history name. |
| 5911 | */ |
| 5912 | |
| 5913 | static char *(history_names[]) = |
| 5914 | { |
| 5915 | "cmd", |
| 5916 | "search", |
| 5917 | "expr", |
| 5918 | "input", |
| 5919 | "debug", |
| 5920 | NULL |
| 5921 | }; |
| 5922 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5923 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5924 | /* |
| 5925 | * Function given to ExpandGeneric() to obtain the possible first |
| 5926 | * arguments of the ":history command. |
| 5927 | */ |
| 5928 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5929 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5930 | { |
| 5931 | static char_u compl[2] = { NUL, NUL }; |
| 5932 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5933 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5934 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5935 | |
| 5936 | if (idx < short_names_count) |
| 5937 | { |
| 5938 | compl[0] = (char_u)short_names[idx]; |
| 5939 | return compl; |
| 5940 | } |
| 5941 | if (idx < short_names_count + history_name_count) |
| 5942 | return (char_u *)history_names[idx - short_names_count]; |
| 5943 | if (idx == short_names_count + history_name_count) |
| 5944 | return (char_u *)"all"; |
| 5945 | return NULL; |
| 5946 | } |
| 5947 | #endif |
| 5948 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5949 | /* |
| 5950 | * init_history() - Initialize the command line history. |
| 5951 | * Also used to re-allocate the history when the size changes. |
| 5952 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5953 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5954 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5955 | { |
| 5956 | int newlen; /* new length of history table */ |
| 5957 | histentry_T *temp; |
| 5958 | int i; |
| 5959 | int j; |
| 5960 | int type; |
| 5961 | |
| 5962 | /* |
| 5963 | * If size of history table changed, reallocate it |
| 5964 | */ |
| 5965 | newlen = (int)p_hi; |
| 5966 | if (newlen != hislen) /* history length changed */ |
| 5967 | { |
| 5968 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5969 | { |
| 5970 | if (newlen) |
| 5971 | { |
| 5972 | temp = (histentry_T *)lalloc( |
| 5973 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5974 | if (temp == NULL) /* out of memory! */ |
| 5975 | { |
| 5976 | if (type == 0) /* first one: just keep the old length */ |
| 5977 | { |
| 5978 | newlen = hislen; |
| 5979 | break; |
| 5980 | } |
| 5981 | /* Already changed one table, now we can only have zero |
| 5982 | * length for all tables. */ |
| 5983 | newlen = 0; |
| 5984 | type = -1; |
| 5985 | continue; |
| 5986 | } |
| 5987 | } |
| 5988 | else |
| 5989 | temp = NULL; |
| 5990 | if (newlen == 0 || temp != NULL) |
| 5991 | { |
| 5992 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5993 | { |
| 5994 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5995 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5996 | } |
| 5997 | else if (newlen > hislen) /* array becomes bigger */ |
| 5998 | { |
| 5999 | for (i = 0; i <= hisidx[type]; ++i) |
| 6000 | temp[i] = history[type][i]; |
| 6001 | j = i; |
| 6002 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6003 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6004 | for ( ; j < hislen; ++i, ++j) |
| 6005 | temp[i] = history[type][j]; |
| 6006 | } |
| 6007 | else /* array becomes smaller or 0 */ |
| 6008 | { |
| 6009 | j = hisidx[type]; |
| 6010 | for (i = newlen - 1; ; --i) |
| 6011 | { |
| 6012 | if (i >= 0) /* copy newest entries */ |
| 6013 | temp[i] = history[type][j]; |
| 6014 | else /* remove older entries */ |
| 6015 | vim_free(history[type][j].hisstr); |
| 6016 | if (--j < 0) |
| 6017 | j = hislen - 1; |
| 6018 | if (j == hisidx[type]) |
| 6019 | break; |
| 6020 | } |
| 6021 | hisidx[type] = newlen - 1; |
| 6022 | } |
| 6023 | vim_free(history[type]); |
| 6024 | history[type] = temp; |
| 6025 | } |
| 6026 | } |
| 6027 | hislen = newlen; |
| 6028 | } |
| 6029 | } |
| 6030 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6031 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6032 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6033 | { |
| 6034 | hisptr->hisnum = 0; |
| 6035 | hisptr->viminfo = FALSE; |
| 6036 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6037 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6038 | } |
| 6039 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6040 | /* |
| 6041 | * Check if command line 'str' is already in history. |
| 6042 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 6043 | */ |
| 6044 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6045 | in_history( |
| 6046 | int type, |
| 6047 | char_u *str, |
| 6048 | int move_to_front, /* Move the entry to the front if it exists */ |
| 6049 | int sep, |
| 6050 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6051 | { |
| 6052 | int i; |
| 6053 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6054 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6055 | |
| 6056 | if (hisidx[type] < 0) |
| 6057 | return FALSE; |
| 6058 | i = hisidx[type]; |
| 6059 | do |
| 6060 | { |
| 6061 | if (history[type][i].hisstr == NULL) |
| 6062 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6063 | |
| 6064 | /* For search history, check that the separator character matches as |
| 6065 | * well. */ |
| 6066 | p = history[type][i].hisstr; |
| 6067 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6068 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6069 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | { |
| 6071 | if (!move_to_front) |
| 6072 | return TRUE; |
| 6073 | last_i = i; |
| 6074 | break; |
| 6075 | } |
| 6076 | if (--i < 0) |
| 6077 | i = hislen - 1; |
| 6078 | } while (i != hisidx[type]); |
| 6079 | |
| 6080 | if (last_i >= 0) |
| 6081 | { |
| 6082 | str = history[type][i].hisstr; |
| 6083 | while (i != hisidx[type]) |
| 6084 | { |
| 6085 | if (++i >= hislen) |
| 6086 | i = 0; |
| 6087 | history[type][last_i] = history[type][i]; |
| 6088 | last_i = i; |
| 6089 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6091 | history[type][i].viminfo = FALSE; |
| 6092 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6093 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6094 | return TRUE; |
| 6095 | } |
| 6096 | return FALSE; |
| 6097 | } |
| 6098 | |
| 6099 | /* |
| 6100 | * Convert history name (from table above) to its HIST_ equivalent. |
| 6101 | * When "name" is empty, return "cmd" history. |
| 6102 | * Returns -1 for unknown history name. |
| 6103 | */ |
| 6104 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6105 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6106 | { |
| 6107 | int i; |
| 6108 | int len = (int)STRLEN(name); |
| 6109 | |
| 6110 | /* No argument: use current history. */ |
| 6111 | if (len == 0) |
| 6112 | return hist_char2type(ccline.cmdfirstc); |
| 6113 | |
| 6114 | for (i = 0; history_names[i] != NULL; ++i) |
| 6115 | if (STRNICMP(name, history_names[i], len) == 0) |
| 6116 | return i; |
| 6117 | |
| 6118 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 6119 | return hist_char2type(name[0]); |
| 6120 | |
| 6121 | return -1; |
| 6122 | } |
| 6123 | |
| 6124 | static int last_maptick = -1; /* last seen maptick */ |
| 6125 | |
| 6126 | /* |
| 6127 | * Add the given string to the given history. If the string is already in the |
| 6128 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 6129 | * values. |
| 6130 | */ |
| 6131 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6132 | add_to_history( |
| 6133 | int histype, |
| 6134 | char_u *new_entry, |
| 6135 | int in_map, /* consider maptick when inside a mapping */ |
| 6136 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6137 | { |
| 6138 | histentry_T *hisptr; |
| 6139 | int len; |
| 6140 | |
| 6141 | if (hislen == 0) /* no history */ |
| 6142 | return; |
| 6143 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 6144 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 6145 | return; |
| 6146 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6147 | /* |
| 6148 | * Searches inside the same mapping overwrite each other, so that only |
| 6149 | * the last line is kept. Be careful not to remove a line that was moved |
| 6150 | * down, only lines that were added. |
| 6151 | */ |
| 6152 | if (histype == HIST_SEARCH && in_map) |
| 6153 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 6154 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6155 | { |
| 6156 | /* Current line is from the same mapping, remove it */ |
| 6157 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 6158 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6159 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6160 | --hisnum[histype]; |
| 6161 | if (--hisidx[HIST_SEARCH] < 0) |
| 6162 | hisidx[HIST_SEARCH] = hislen - 1; |
| 6163 | } |
| 6164 | last_maptick = -1; |
| 6165 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6166 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6167 | { |
| 6168 | if (++hisidx[histype] == hislen) |
| 6169 | hisidx[histype] = 0; |
| 6170 | hisptr = &history[histype][hisidx[histype]]; |
| 6171 | vim_free(hisptr->hisstr); |
| 6172 | |
| 6173 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6174 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6175 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 6176 | if (hisptr->hisstr != NULL) |
| 6177 | hisptr->hisstr[len + 1] = sep; |
| 6178 | |
| 6179 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6180 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6181 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6182 | if (histype == HIST_SEARCH && in_map) |
| 6183 | last_maptick = maptick; |
| 6184 | } |
| 6185 | } |
| 6186 | |
| 6187 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6188 | |
| 6189 | /* |
| 6190 | * Get identifier of newest history entry. |
| 6191 | * "histype" may be one of the HIST_ values. |
| 6192 | */ |
| 6193 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6194 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6195 | { |
| 6196 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6197 | || hisidx[histype] < 0) |
| 6198 | return -1; |
| 6199 | |
| 6200 | return history[histype][hisidx[histype]].hisnum; |
| 6201 | } |
| 6202 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6203 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6204 | * Calculate history index from a number: |
| 6205 | * num > 0: seen as identifying number of a history entry |
| 6206 | * num < 0: relative position in history wrt newest entry |
| 6207 | * "histype" may be one of the HIST_ values. |
| 6208 | */ |
| 6209 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6210 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6211 | { |
| 6212 | int i; |
| 6213 | histentry_T *hist; |
| 6214 | int wrapped = FALSE; |
| 6215 | |
| 6216 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6217 | || (i = hisidx[histype]) < 0 || num == 0) |
| 6218 | return -1; |
| 6219 | |
| 6220 | hist = history[histype]; |
| 6221 | if (num > 0) |
| 6222 | { |
| 6223 | while (hist[i].hisnum > num) |
| 6224 | if (--i < 0) |
| 6225 | { |
| 6226 | if (wrapped) |
| 6227 | break; |
| 6228 | i += hislen; |
| 6229 | wrapped = TRUE; |
| 6230 | } |
| 6231 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 6232 | return i; |
| 6233 | } |
| 6234 | else if (-num <= hislen) |
| 6235 | { |
| 6236 | i += num + 1; |
| 6237 | if (i < 0) |
| 6238 | i += hislen; |
| 6239 | if (hist[i].hisstr != NULL) |
| 6240 | return i; |
| 6241 | } |
| 6242 | return -1; |
| 6243 | } |
| 6244 | |
| 6245 | /* |
| 6246 | * Get a history entry by its index. |
| 6247 | * "histype" may be one of the HIST_ values. |
| 6248 | */ |
| 6249 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6250 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6251 | { |
| 6252 | idx = calc_hist_idx(histype, idx); |
| 6253 | if (idx >= 0) |
| 6254 | return history[histype][idx].hisstr; |
| 6255 | else |
| 6256 | return (char_u *)""; |
| 6257 | } |
| 6258 | |
| 6259 | /* |
| 6260 | * Clear all entries of a history. |
| 6261 | * "histype" may be one of the HIST_ values. |
| 6262 | */ |
| 6263 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6264 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6265 | { |
| 6266 | int i; |
| 6267 | histentry_T *hisptr; |
| 6268 | |
| 6269 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6270 | { |
| 6271 | hisptr = history[histype]; |
| 6272 | for (i = hislen; i--;) |
| 6273 | { |
| 6274 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6275 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6276 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6277 | } |
| 6278 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6279 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6280 | return OK; |
| 6281 | } |
| 6282 | return FAIL; |
| 6283 | } |
| 6284 | |
| 6285 | /* |
| 6286 | * Remove all entries matching {str} from a history. |
| 6287 | * "histype" may be one of the HIST_ values. |
| 6288 | */ |
| 6289 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6290 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6291 | { |
| 6292 | regmatch_T regmatch; |
| 6293 | histentry_T *hisptr; |
| 6294 | int idx; |
| 6295 | int i; |
| 6296 | int last; |
| 6297 | int found = FALSE; |
| 6298 | |
| 6299 | regmatch.regprog = NULL; |
| 6300 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6301 | if (hislen != 0 |
| 6302 | && histype >= 0 |
| 6303 | && histype < HIST_COUNT |
| 6304 | && *str != NUL |
| 6305 | && (idx = hisidx[histype]) >= 0 |
| 6306 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6307 | != NULL) |
| 6308 | { |
| 6309 | i = last = idx; |
| 6310 | do |
| 6311 | { |
| 6312 | hisptr = &history[histype][i]; |
| 6313 | if (hisptr->hisstr == NULL) |
| 6314 | break; |
| 6315 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6316 | { |
| 6317 | found = TRUE; |
| 6318 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6319 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6320 | } |
| 6321 | else |
| 6322 | { |
| 6323 | if (i != last) |
| 6324 | { |
| 6325 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6326 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6327 | } |
| 6328 | if (--last < 0) |
| 6329 | last += hislen; |
| 6330 | } |
| 6331 | if (--i < 0) |
| 6332 | i += hislen; |
| 6333 | } while (i != idx); |
| 6334 | if (history[histype][idx].hisstr == NULL) |
| 6335 | hisidx[histype] = -1; |
| 6336 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6337 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6338 | return found; |
| 6339 | } |
| 6340 | |
| 6341 | /* |
| 6342 | * Remove an indexed entry from a history. |
| 6343 | * "histype" may be one of the HIST_ values. |
| 6344 | */ |
| 6345 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6346 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | { |
| 6348 | int i, j; |
| 6349 | |
| 6350 | i = calc_hist_idx(histype, idx); |
| 6351 | if (i < 0) |
| 6352 | return FALSE; |
| 6353 | idx = hisidx[histype]; |
| 6354 | vim_free(history[histype][i].hisstr); |
| 6355 | |
| 6356 | /* When deleting the last added search string in a mapping, reset |
| 6357 | * last_maptick, so that the last added search string isn't deleted again. |
| 6358 | */ |
| 6359 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6360 | last_maptick = -1; |
| 6361 | |
| 6362 | while (i != idx) |
| 6363 | { |
| 6364 | j = (i + 1) % hislen; |
| 6365 | history[histype][i] = history[histype][j]; |
| 6366 | i = j; |
| 6367 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6368 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6369 | if (--i < 0) |
| 6370 | i += hislen; |
| 6371 | hisidx[histype] = i; |
| 6372 | return TRUE; |
| 6373 | } |
| 6374 | |
| 6375 | #endif /* FEAT_EVAL */ |
| 6376 | |
| 6377 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6378 | /* |
| 6379 | * Very specific function to remove the value in ":set key=val" from the |
| 6380 | * history. |
| 6381 | */ |
| 6382 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6383 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6384 | { |
| 6385 | char_u *p; |
| 6386 | int i; |
| 6387 | |
| 6388 | i = hisidx[HIST_CMD]; |
| 6389 | if (i < 0) |
| 6390 | return; |
| 6391 | p = history[HIST_CMD][i].hisstr; |
| 6392 | if (p != NULL) |
| 6393 | for ( ; *p; ++p) |
| 6394 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6395 | { |
| 6396 | p = vim_strchr(p + 3, '='); |
| 6397 | if (p == NULL) |
| 6398 | break; |
| 6399 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6400 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6401 | if (p[i] == '\\' && p[i + 1]) |
| 6402 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6403 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | --p; |
| 6405 | } |
| 6406 | } |
| 6407 | #endif |
| 6408 | |
| 6409 | #endif /* FEAT_CMDHIST */ |
| 6410 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6411 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6412 | /* |
| 6413 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6414 | * ccline and put the previous value in prev_ccline. |
| 6415 | */ |
| 6416 | static struct cmdline_info * |
| 6417 | get_ccline_ptr(void) |
| 6418 | { |
| 6419 | if ((State & CMDLINE) == 0) |
| 6420 | return NULL; |
| 6421 | if (ccline.cmdbuff != NULL) |
| 6422 | return &ccline; |
| 6423 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6424 | return &prev_ccline; |
| 6425 | return NULL; |
| 6426 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6427 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6428 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6429 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6430 | /* |
| 6431 | * Get the current command line in allocated memory. |
| 6432 | * Only works when the command line is being edited. |
| 6433 | * Returns NULL when something is wrong. |
| 6434 | */ |
| 6435 | char_u * |
| 6436 | get_cmdline_str(void) |
| 6437 | { |
| 6438 | struct cmdline_info *p = get_ccline_ptr(); |
| 6439 | |
| 6440 | if (p == NULL) |
| 6441 | return NULL; |
| 6442 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6443 | } |
| 6444 | |
| 6445 | /* |
| 6446 | * Get the current command line position, counted in bytes. |
| 6447 | * Zero is the first position. |
| 6448 | * Only works when the command line is being edited. |
| 6449 | * Returns -1 when something is wrong. |
| 6450 | */ |
| 6451 | int |
| 6452 | get_cmdline_pos(void) |
| 6453 | { |
| 6454 | struct cmdline_info *p = get_ccline_ptr(); |
| 6455 | |
| 6456 | if (p == NULL) |
| 6457 | return -1; |
| 6458 | return p->cmdpos; |
| 6459 | } |
| 6460 | |
| 6461 | /* |
| 6462 | * Set the command line byte position to "pos". Zero is the first position. |
| 6463 | * Only works when the command line is being edited. |
| 6464 | * Returns 1 when failed, 0 when OK. |
| 6465 | */ |
| 6466 | int |
| 6467 | set_cmdline_pos( |
| 6468 | int pos) |
| 6469 | { |
| 6470 | struct cmdline_info *p = get_ccline_ptr(); |
| 6471 | |
| 6472 | if (p == NULL) |
| 6473 | return 1; |
| 6474 | |
| 6475 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6476 | * changed the command line. */ |
| 6477 | if (pos < 0) |
| 6478 | new_cmdpos = 0; |
| 6479 | else |
| 6480 | new_cmdpos = pos; |
| 6481 | return 0; |
| 6482 | } |
| 6483 | #endif |
| 6484 | |
| 6485 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6486 | /* |
| 6487 | * Get the current command-line type. |
| 6488 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6489 | * Only works when the command line is being edited. |
| 6490 | * Returns NUL when something is wrong. |
| 6491 | */ |
| 6492 | int |
| 6493 | get_cmdline_type(void) |
| 6494 | { |
| 6495 | struct cmdline_info *p = get_ccline_ptr(); |
| 6496 | |
| 6497 | if (p == NULL) |
| 6498 | return NUL; |
| 6499 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6500 | return |
| 6501 | # ifdef FEAT_EVAL |
| 6502 | (p->input_fn) ? '@' : |
| 6503 | # endif |
| 6504 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6505 | return p->cmdfirstc; |
| 6506 | } |
| 6507 | #endif |
| 6508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6509 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6510 | /* |
| 6511 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6512 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6513 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6514 | */ |
| 6515 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6516 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6517 | { |
| 6518 | int len; |
| 6519 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6520 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6521 | |
| 6522 | *str = skipwhite(*str); |
| 6523 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6524 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6525 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6526 | *str += len; |
| 6527 | *num1 = (int)num; |
| 6528 | first = TRUE; |
| 6529 | } |
| 6530 | *str = skipwhite(*str); |
| 6531 | if (**str == ',') /* parse "to" part of range */ |
| 6532 | { |
| 6533 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6534 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6535 | if (len > 0) |
| 6536 | { |
| 6537 | *num2 = (int)num; |
| 6538 | *str = skipwhite(*str + len); |
| 6539 | } |
| 6540 | else if (!first) /* no number given at all */ |
| 6541 | return FAIL; |
| 6542 | } |
| 6543 | else if (first) /* only one number given */ |
| 6544 | *num2 = *num1; |
| 6545 | return OK; |
| 6546 | } |
| 6547 | #endif |
| 6548 | |
| 6549 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6550 | /* |
| 6551 | * :history command - print a history |
| 6552 | */ |
| 6553 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6554 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6555 | { |
| 6556 | histentry_T *hist; |
| 6557 | int histype1 = HIST_CMD; |
| 6558 | int histype2 = HIST_CMD; |
| 6559 | int hisidx1 = 1; |
| 6560 | int hisidx2 = -1; |
| 6561 | int idx; |
| 6562 | int i, j, k; |
| 6563 | char_u *end; |
| 6564 | char_u *arg = eap->arg; |
| 6565 | |
| 6566 | if (hislen == 0) |
| 6567 | { |
| 6568 | MSG(_("'history' option is zero")); |
| 6569 | return; |
| 6570 | } |
| 6571 | |
| 6572 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6573 | { |
| 6574 | end = arg; |
| 6575 | while (ASCII_ISALPHA(*end) |
| 6576 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6577 | end++; |
| 6578 | i = *end; |
| 6579 | *end = NUL; |
| 6580 | histype1 = get_histtype(arg); |
| 6581 | if (histype1 == -1) |
| 6582 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6583 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6584 | { |
| 6585 | histype1 = 0; |
| 6586 | histype2 = HIST_COUNT-1; |
| 6587 | } |
| 6588 | else |
| 6589 | { |
| 6590 | *end = i; |
| 6591 | EMSG(_(e_trailing)); |
| 6592 | return; |
| 6593 | } |
| 6594 | } |
| 6595 | else |
| 6596 | histype2 = histype1; |
| 6597 | *end = i; |
| 6598 | } |
| 6599 | else |
| 6600 | end = arg; |
| 6601 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6602 | { |
| 6603 | EMSG(_(e_trailing)); |
| 6604 | return; |
| 6605 | } |
| 6606 | |
| 6607 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6608 | { |
| 6609 | STRCPY(IObuff, "\n # "); |
| 6610 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6611 | MSG_PUTS_TITLE(IObuff); |
| 6612 | idx = hisidx[histype1]; |
| 6613 | hist = history[histype1]; |
| 6614 | j = hisidx1; |
| 6615 | k = hisidx2; |
| 6616 | if (j < 0) |
| 6617 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6618 | if (k < 0) |
| 6619 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6620 | if (idx >= 0 && j <= k) |
| 6621 | for (i = idx + 1; !got_int; ++i) |
| 6622 | { |
| 6623 | if (i == hislen) |
| 6624 | i = 0; |
| 6625 | if (hist[i].hisstr != NULL |
| 6626 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6627 | { |
| 6628 | msg_putchar('\n'); |
| 6629 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6630 | hist[i].hisnum); |
| 6631 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6632 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6633 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6634 | else |
| 6635 | STRCAT(IObuff, hist[i].hisstr); |
| 6636 | msg_outtrans(IObuff); |
| 6637 | out_flush(); |
| 6638 | } |
| 6639 | if (i == idx) |
| 6640 | break; |
| 6641 | } |
| 6642 | } |
| 6643 | } |
| 6644 | #endif |
| 6645 | |
| 6646 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6647 | /* |
| 6648 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6649 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6650 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6651 | {NULL, NULL, NULL, NULL, NULL}; |
| 6652 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6653 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6654 | static int viminfo_add_at_front = FALSE; |
| 6655 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6656 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6657 | |
| 6658 | /* |
| 6659 | * Translate a history type number to the associated character. |
| 6660 | */ |
| 6661 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6662 | hist_type2char( |
| 6663 | int type, |
| 6664 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6665 | { |
| 6666 | if (type == HIST_CMD) |
| 6667 | return ':'; |
| 6668 | if (type == HIST_SEARCH) |
| 6669 | { |
| 6670 | if (use_question) |
| 6671 | return '?'; |
| 6672 | else |
| 6673 | return '/'; |
| 6674 | } |
| 6675 | if (type == HIST_EXPR) |
| 6676 | return '='; |
| 6677 | return '@'; |
| 6678 | } |
| 6679 | |
| 6680 | /* |
| 6681 | * Prepare for reading the history from the viminfo file. |
| 6682 | * This allocates history arrays to store the read history lines. |
| 6683 | */ |
| 6684 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6685 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6686 | { |
| 6687 | int i; |
| 6688 | int num; |
| 6689 | int type; |
| 6690 | int len; |
| 6691 | |
| 6692 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6693 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6694 | if (asklen > hislen) |
| 6695 | asklen = hislen; |
| 6696 | |
| 6697 | for (type = 0; type < HIST_COUNT; ++type) |
| 6698 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6699 | /* Count the number of empty spaces in the history list. Entries read |
| 6700 | * from viminfo previously are also considered empty. If there are |
| 6701 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6702 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6703 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6704 | num++; |
| 6705 | len = asklen; |
| 6706 | if (num > len) |
| 6707 | len = num; |
| 6708 | if (len <= 0) |
| 6709 | viminfo_history[type] = NULL; |
| 6710 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6711 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6712 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6713 | if (viminfo_history[type] == NULL) |
| 6714 | len = 0; |
| 6715 | viminfo_hislen[type] = len; |
| 6716 | viminfo_hisidx[type] = 0; |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | /* |
| 6721 | * Accept a line from the viminfo, store it in the history array when it's |
| 6722 | * new. |
| 6723 | */ |
| 6724 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6725 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6726 | { |
| 6727 | int type; |
| 6728 | long_u len; |
| 6729 | char_u *val; |
| 6730 | char_u *p; |
| 6731 | |
| 6732 | type = hist_char2type(virp->vir_line[0]); |
| 6733 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6734 | { |
| 6735 | val = viminfo_readstring(virp, 1, TRUE); |
| 6736 | if (val != NULL && *val != NUL) |
| 6737 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6738 | int sep = (*val == ' ' ? NUL : *val); |
| 6739 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6741 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6742 | { |
| 6743 | /* Need to re-allocate to append the separator byte. */ |
| 6744 | len = STRLEN(val); |
| 6745 | p = lalloc(len + 2, TRUE); |
| 6746 | if (p != NULL) |
| 6747 | { |
| 6748 | if (type == HIST_SEARCH) |
| 6749 | { |
| 6750 | /* Search entry: Move the separator from the first |
| 6751 | * column to after the NUL. */ |
| 6752 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6753 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6754 | } |
| 6755 | else |
| 6756 | { |
| 6757 | /* Not a search entry: No separator in the viminfo |
| 6758 | * file, add a NUL separator. */ |
| 6759 | mch_memmove(p, val, (size_t)len + 1); |
| 6760 | p[len + 1] = NUL; |
| 6761 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6762 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6763 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6764 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6765 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6766 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | } |
| 6770 | vim_free(val); |
| 6771 | } |
| 6772 | return viminfo_readline(virp); |
| 6773 | } |
| 6774 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6775 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6776 | * Accept a new style history line from the viminfo, store it in the history |
| 6777 | * array when it's new. |
| 6778 | */ |
| 6779 | void |
| 6780 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6781 | garray_T *values, |
| 6782 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6783 | { |
| 6784 | int type; |
| 6785 | long_u len; |
| 6786 | char_u *val; |
| 6787 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6788 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6789 | |
| 6790 | /* Check the format: |
| 6791 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6792 | if (values->ga_len < 4 |
| 6793 | || vp[0].bv_type != BVAL_NR |
| 6794 | || vp[1].bv_type != BVAL_NR |
| 6795 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6796 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6797 | return; |
| 6798 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6799 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6800 | if (type >= HIST_COUNT) |
| 6801 | return; |
| 6802 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6803 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6804 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6805 | if (val != NULL && *val != NUL) |
| 6806 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6807 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6808 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6809 | int idx; |
| 6810 | int overwrite = FALSE; |
| 6811 | |
| 6812 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6813 | { |
| 6814 | /* If lines were written by an older Vim we need to avoid |
| 6815 | * getting duplicates. See if the entry already exists. */ |
| 6816 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6817 | { |
| 6818 | p = viminfo_history[type][idx].hisstr; |
| 6819 | if (STRCMP(val, p) == 0 |
| 6820 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6821 | { |
| 6822 | overwrite = TRUE; |
| 6823 | break; |
| 6824 | } |
| 6825 | } |
| 6826 | |
| 6827 | if (!overwrite) |
| 6828 | { |
| 6829 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6830 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6831 | p = lalloc(len + 2, TRUE); |
| 6832 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6833 | else |
| 6834 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6835 | if (p != NULL) |
| 6836 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6837 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6838 | if (!overwrite) |
| 6839 | { |
| 6840 | mch_memmove(p, val, (size_t)len + 1); |
| 6841 | /* Put the separator after the NUL. */ |
| 6842 | p[len + 1] = sep; |
| 6843 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6844 | viminfo_history[type][idx].hisnum = 0; |
| 6845 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6846 | viminfo_hisidx[type]++; |
| 6847 | } |
| 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | } |
| 6852 | } |
| 6853 | |
| 6854 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6855 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6856 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6857 | static void |
| 6858 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6859 | { |
| 6860 | int idx; |
| 6861 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6862 | |
| 6863 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6864 | if (idx >= hislen) |
| 6865 | idx -= hislen; |
| 6866 | else if (idx < 0) |
| 6867 | idx = hislen - 1; |
| 6868 | if (viminfo_add_at_front) |
| 6869 | hisidx[type] = idx; |
| 6870 | else |
| 6871 | { |
| 6872 | if (hisidx[type] == -1) |
| 6873 | hisidx[type] = hislen - 1; |
| 6874 | do |
| 6875 | { |
| 6876 | if (history[type][idx].hisstr != NULL |
| 6877 | || history[type][idx].viminfo) |
| 6878 | break; |
| 6879 | if (++idx == hislen) |
| 6880 | idx = 0; |
| 6881 | } while (idx != hisidx[type]); |
| 6882 | if (idx != hisidx[type] && --idx < 0) |
| 6883 | idx = hislen - 1; |
| 6884 | } |
| 6885 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6886 | { |
| 6887 | vim_free(history[type][idx].hisstr); |
| 6888 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6889 | history[type][idx].viminfo = TRUE; |
| 6890 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6891 | if (--idx < 0) |
| 6892 | idx = hislen - 1; |
| 6893 | } |
| 6894 | idx += 1; |
| 6895 | idx %= hislen; |
| 6896 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6897 | { |
| 6898 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6899 | idx %= hislen; |
| 6900 | } |
| 6901 | } |
| 6902 | |
| 6903 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6904 | static int |
| 6905 | #ifdef __BORLANDC__ |
| 6906 | _RTLENTRYF |
| 6907 | #endif |
| 6908 | sort_hist(const void *s1, const void *s2) |
| 6909 | { |
| 6910 | histentry_T *p1 = *(histentry_T **)s1; |
| 6911 | histentry_T *p2 = *(histentry_T **)s2; |
| 6912 | |
| 6913 | if (p1->time_set < p2->time_set) return -1; |
| 6914 | if (p1->time_set > p2->time_set) return 1; |
| 6915 | return 0; |
| 6916 | } |
| 6917 | #endif |
| 6918 | |
| 6919 | /* |
| 6920 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6921 | * timestamp; |
| 6922 | */ |
| 6923 | static void |
| 6924 | merge_history(int type) |
| 6925 | { |
| 6926 | int max_len; |
| 6927 | histentry_T **tot_hist; |
| 6928 | histentry_T *new_hist; |
| 6929 | int i; |
| 6930 | int len; |
| 6931 | |
| 6932 | /* Make one long list with all entries. */ |
| 6933 | max_len = hislen + viminfo_hisidx[type]; |
| 6934 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6935 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6936 | if (tot_hist == NULL || new_hist == NULL) |
| 6937 | { |
| 6938 | vim_free(tot_hist); |
| 6939 | vim_free(new_hist); |
| 6940 | return; |
| 6941 | } |
| 6942 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6943 | tot_hist[i] = &viminfo_history[type][i]; |
| 6944 | len = i; |
| 6945 | for (i = 0; i < hislen; i++) |
| 6946 | if (history[type][i].hisstr != NULL) |
| 6947 | tot_hist[len++] = &history[type][i]; |
| 6948 | |
| 6949 | /* Sort the list on timestamp. */ |
| 6950 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6951 | |
| 6952 | /* Keep the newest ones. */ |
| 6953 | for (i = 0; i < hislen; i++) |
| 6954 | { |
| 6955 | if (i < len) |
| 6956 | { |
| 6957 | new_hist[i] = *tot_hist[i]; |
| 6958 | tot_hist[i]->hisstr = NULL; |
| 6959 | if (new_hist[i].hisnum == 0) |
| 6960 | new_hist[i].hisnum = ++hisnum[type]; |
| 6961 | } |
| 6962 | else |
| 6963 | clear_hist_entry(&new_hist[i]); |
| 6964 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6965 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6966 | |
| 6967 | /* Free what is not kept. */ |
| 6968 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6969 | vim_free(viminfo_history[type][i].hisstr); |
| 6970 | for (i = 0; i < hislen; i++) |
| 6971 | vim_free(history[type][i].hisstr); |
| 6972 | vim_free(history[type]); |
| 6973 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6974 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | /* |
| 6978 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6979 | */ |
| 6980 | void |
| 6981 | finish_viminfo_history(vir_T *virp) |
| 6982 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6983 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6984 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6985 | |
| 6986 | for (type = 0; type < HIST_COUNT; ++type) |
| 6987 | { |
| 6988 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6989 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6990 | |
| 6991 | if (merge) |
| 6992 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6993 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6994 | concat_history(type); |
| 6995 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6996 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6997 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6998 | } |
| 6999 | } |
| 7000 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7001 | /* |
| 7002 | * Write history to viminfo file in "fp". |
| 7003 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 7004 | * file, data is in viminfo_history[]. |
| 7005 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 7006 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7007 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7008 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7009 | { |
| 7010 | int i; |
| 7011 | int type; |
| 7012 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7013 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7014 | |
| 7015 | init_history(); |
| 7016 | if (hislen == 0) |
| 7017 | return; |
| 7018 | for (type = 0; type < HIST_COUNT; ++type) |
| 7019 | { |
| 7020 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 7021 | if (num_saved == 0) |
| 7022 | continue; |
| 7023 | if (num_saved < 0) /* Use default */ |
| 7024 | num_saved = hislen; |
| 7025 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 7026 | type == HIST_CMD ? _("Command Line") : |
| 7027 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7028 | type == HIST_EXPR ? _("Expression") : |
| 7029 | type == HIST_INPUT ? _("Input Line") : |
| 7030 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7031 | if (num_saved > hislen) |
| 7032 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7033 | |
| 7034 | /* |
| 7035 | * Merge typed and viminfo history: |
| 7036 | * round 1: history of typed commands. |
| 7037 | * round 2: history from recently read viminfo. |
| 7038 | */ |
| 7039 | for (round = 1; round <= 2; ++round) |
| 7040 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 7041 | if (round == 1) |
| 7042 | /* start at newest entry, somewhere in the list */ |
| 7043 | i = hisidx[type]; |
| 7044 | else if (viminfo_hisidx[type] > 0) |
| 7045 | /* start at newest entry, first in the list */ |
| 7046 | i = 0; |
| 7047 | else |
| 7048 | /* empty list */ |
| 7049 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7050 | if (i >= 0) |
| 7051 | while (num_saved > 0 |
| 7052 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7054 | char_u *p; |
| 7055 | time_t timestamp; |
| 7056 | int c = NUL; |
| 7057 | |
| 7058 | if (round == 1) |
| 7059 | { |
| 7060 | p = history[type][i].hisstr; |
| 7061 | timestamp = history[type][i].time_set; |
| 7062 | } |
| 7063 | else |
| 7064 | { |
| 7065 | p = viminfo_history[type] == NULL ? NULL |
| 7066 | : viminfo_history[type][i].hisstr; |
| 7067 | timestamp = viminfo_history[type] == NULL ? 0 |
| 7068 | : viminfo_history[type][i].time_set; |
| 7069 | } |
| 7070 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7071 | if (p != NULL && (round == 2 |
| 7072 | || !merge |
| 7073 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7075 | --num_saved; |
| 7076 | fputc(hist_type2char(type, TRUE), fp); |
| 7077 | /* For the search history: put the separator in the |
| 7078 | * second column; use a space if there isn't one. */ |
| 7079 | if (type == HIST_SEARCH) |
| 7080 | { |
| 7081 | c = p[STRLEN(p) + 1]; |
| 7082 | putc(c == NUL ? ' ' : c, fp); |
| 7083 | } |
| 7084 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7085 | |
| 7086 | { |
| 7087 | char cbuf[NUMBUFLEN]; |
| 7088 | |
| 7089 | /* New style history with a bar line. Format: |
| 7090 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 7091 | if (c == NUL) |
| 7092 | cbuf[0] = NUL; |
| 7093 | else |
| 7094 | sprintf(cbuf, "%d", c); |
| 7095 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 7096 | type, (long)timestamp, cbuf); |
| 7097 | barline_writestring(fp, p, LSIZE - 20); |
| 7098 | putc('\n', fp); |
| 7099 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7100 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7101 | if (round == 1) |
| 7102 | { |
| 7103 | /* Decrement index, loop around and stop when back at |
| 7104 | * the start. */ |
| 7105 | if (--i < 0) |
| 7106 | i = hislen - 1; |
| 7107 | if (i == hisidx[type]) |
| 7108 | break; |
| 7109 | } |
| 7110 | else |
| 7111 | { |
| 7112 | /* Increment index. Stop at the end in the while. */ |
| 7113 | ++i; |
| 7114 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7116 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 7117 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7118 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7119 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7120 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 7121 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7122 | } |
| 7123 | } |
| 7124 | #endif /* FEAT_VIMINFO */ |
| 7125 | |
| 7126 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 7127 | /* |
| 7128 | * Write a character at the current cursor+offset position. |
| 7129 | * It is directly written into the command buffer block. |
| 7130 | */ |
| 7131 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7132 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7133 | { |
| 7134 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7135 | { |
| 7136 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 7137 | return; |
| 7138 | } |
| 7139 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 7140 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 7141 | } |
| 7142 | |
| 7143 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7144 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7145 | { |
| 7146 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7147 | { |
| 7148 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 7149 | return NUL; |
| 7150 | } |
| 7151 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 7152 | } |
| 7153 | #endif |
| 7154 | |
| 7155 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 7156 | /* |
| 7157 | * Open a window on the current command line and history. Allow editing in |
| 7158 | * the window. Returns when the window is closed. |
| 7159 | * Returns: |
| 7160 | * CR if the command is to be executed |
| 7161 | * Ctrl_C if it is to be abandoned |
| 7162 | * K_IGNORE if editing continues |
| 7163 | */ |
| 7164 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7165 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7166 | { |
| 7167 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7168 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7169 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7170 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7171 | win_T *wp; |
| 7172 | int i; |
| 7173 | linenr_T lnum; |
| 7174 | int histtype; |
| 7175 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7176 | int save_restart_edit = restart_edit; |
| 7177 | int save_State = State; |
| 7178 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7179 | #ifdef FEAT_RIGHTLEFT |
| 7180 | int save_cmdmsg_rl = cmdmsg_rl; |
| 7181 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7182 | #ifdef FEAT_FOLDING |
| 7183 | int save_KeyTyped; |
| 7184 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7185 | |
| 7186 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 7187 | if (cmdwin_type != 0 |
| 7188 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 7189 | || cmdline_star > 0 |
| 7190 | # endif |
| 7191 | ) |
| 7192 | { |
| 7193 | beep_flush(); |
| 7194 | return K_IGNORE; |
| 7195 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7196 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7197 | |
| 7198 | /* Save current window sizes. */ |
| 7199 | win_size_save(&winsizes); |
| 7200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7201 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7202 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7203 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7204 | /* don't use a new tab page */ |
| 7205 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7206 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7207 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7208 | /* Create a window for the command-line buffer. */ |
| 7209 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 7210 | { |
| 7211 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7212 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7213 | return K_IGNORE; |
| 7214 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7215 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7216 | |
| 7217 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 7218 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7219 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7220 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 7222 | #ifdef FEAT_FOLDING |
| 7223 | curwin->w_p_fen = FALSE; |
| 7224 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7225 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7226 | curwin->w_p_rl = cmdmsg_rl; |
| 7227 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7228 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 7229 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7230 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7231 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7232 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7233 | /* But don't allow switching to another buffer. */ |
| 7234 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7235 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7236 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 7237 | need_wait_return = FALSE; |
| 7238 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7239 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7240 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 7241 | { |
| 7242 | if (p_wc == TAB) |
| 7243 | { |
| 7244 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 7245 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 7246 | } |
| 7247 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 7248 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7249 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7250 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 7251 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7252 | * sets 'textwidth' to 78). */ |
| 7253 | curbuf->b_p_tw = 0; |
| 7254 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7255 | /* Fill the buffer with the history. */ |
| 7256 | init_history(); |
| 7257 | if (hislen > 0) |
| 7258 | { |
| 7259 | i = hisidx[histtype]; |
| 7260 | if (i >= 0) |
| 7261 | { |
| 7262 | lnum = 0; |
| 7263 | do |
| 7264 | { |
| 7265 | if (++i == hislen) |
| 7266 | i = 0; |
| 7267 | if (history[histtype][i].hisstr != NULL) |
| 7268 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7269 | (colnr_T)0, FALSE); |
| 7270 | } |
| 7271 | while (i != hisidx[histtype]); |
| 7272 | } |
| 7273 | } |
| 7274 | |
| 7275 | /* Replace the empty last line with the current command-line and put the |
| 7276 | * cursor there. */ |
| 7277 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7278 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7279 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7280 | changed_line_abv_curs(); |
| 7281 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7282 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7283 | |
| 7284 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7285 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7286 | |
| 7287 | /* No Ex mode here! */ |
| 7288 | exmode_active = 0; |
| 7289 | |
| 7290 | State = NORMAL; |
| 7291 | # ifdef FEAT_MOUSE |
| 7292 | setmouse(); |
| 7293 | # endif |
| 7294 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7295 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7296 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7297 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7298 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7299 | |
| 7300 | i = RedrawingDisabled; |
| 7301 | RedrawingDisabled = 0; |
| 7302 | |
| 7303 | /* |
| 7304 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7305 | */ |
| 7306 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7307 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7308 | |
| 7309 | RedrawingDisabled = i; |
| 7310 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7311 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7312 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7313 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7315 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7316 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7317 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7318 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7319 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7320 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7321 | # endif |
| 7322 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7323 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7324 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7325 | cmdwin_type = 0; |
| 7326 | |
| 7327 | exmode_active = save_exmode; |
| 7328 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7329 | /* 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] | 7330 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7331 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7332 | { |
| 7333 | cmdwin_result = Ctrl_C; |
| 7334 | EMSG(_("E199: Active window or buffer deleted")); |
| 7335 | } |
| 7336 | else |
| 7337 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7338 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7339 | /* autocmds may abort script processing */ |
| 7340 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7341 | cmdwin_result = Ctrl_C; |
| 7342 | # endif |
| 7343 | /* Set the new command line from the cmdline buffer. */ |
| 7344 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7345 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7347 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7348 | |
| 7349 | if (histtype == HIST_CMD) |
| 7350 | { |
| 7351 | /* Execute the command directly. */ |
| 7352 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7353 | cmdwin_result = CAR; |
| 7354 | } |
| 7355 | else |
| 7356 | { |
| 7357 | /* First need to cancel what we were doing. */ |
| 7358 | ccline.cmdbuff = NULL; |
| 7359 | stuffcharReadbuff(':'); |
| 7360 | stuffReadbuff((char_u *)p); |
| 7361 | stuffcharReadbuff(CAR); |
| 7362 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7363 | } |
| 7364 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7365 | { |
| 7366 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7367 | cmdwin_result = CAR; |
| 7368 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7369 | else if (cmdwin_result == Ctrl_C) |
| 7370 | { |
| 7371 | /* :q or :close, don't execute any command |
| 7372 | * and don't modify the cmd window. */ |
| 7373 | ccline.cmdbuff = NULL; |
| 7374 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7375 | else |
| 7376 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7377 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7378 | { |
| 7379 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7380 | ccline.cmdlen = 0; |
| 7381 | ccline.cmdbufflen = 1; |
| 7382 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7383 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7384 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7385 | else |
| 7386 | { |
| 7387 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7388 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7389 | ccline.cmdpos = curwin->w_cursor.col; |
| 7390 | if (ccline.cmdpos > ccline.cmdlen) |
| 7391 | ccline.cmdpos = ccline.cmdlen; |
| 7392 | if (cmdwin_result == K_IGNORE) |
| 7393 | { |
| 7394 | set_cmdspos_cursor(); |
| 7395 | redrawcmd(); |
| 7396 | } |
| 7397 | } |
| 7398 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7399 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7400 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7401 | # ifdef FEAT_CONCEAL |
| 7402 | /* Avoid command-line window first character being concealed. */ |
| 7403 | curwin->w_p_cole = 0; |
| 7404 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7405 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7406 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7407 | win_goto(old_curwin); |
| 7408 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7409 | |
| 7410 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7411 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7412 | if (bufref_valid(&bufref)) |
| 7413 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7414 | |
| 7415 | /* Restore window sizes. */ |
| 7416 | win_size_restore(&winsizes); |
| 7417 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7418 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
| 7421 | ga_clear(&winsizes); |
| 7422 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7423 | # ifdef FEAT_RIGHTLEFT |
| 7424 | cmdmsg_rl = save_cmdmsg_rl; |
| 7425 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7426 | |
| 7427 | State = save_State; |
| 7428 | # ifdef FEAT_MOUSE |
| 7429 | setmouse(); |
| 7430 | # endif |
| 7431 | |
| 7432 | return cmdwin_result; |
| 7433 | } |
| 7434 | #endif /* FEAT_CMDWIN */ |
| 7435 | |
| 7436 | /* |
| 7437 | * Used for commands that either take a simple command string argument, or: |
| 7438 | * cmd << endmarker |
| 7439 | * {script} |
| 7440 | * endmarker |
| 7441 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7442 | */ |
| 7443 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7444 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7445 | { |
| 7446 | char_u *theline; |
| 7447 | char *end_pattern = NULL; |
| 7448 | char dot[] = "."; |
| 7449 | garray_T ga; |
| 7450 | |
| 7451 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7452 | return NULL; |
| 7453 | |
| 7454 | ga_init2(&ga, 1, 0x400); |
| 7455 | |
| 7456 | if (cmd[2] != NUL) |
| 7457 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7458 | else |
| 7459 | end_pattern = dot; |
| 7460 | |
| 7461 | for (;;) |
| 7462 | { |
| 7463 | theline = eap->getline( |
| 7464 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7465 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7466 | #endif |
| 7467 | NUL, eap->cookie, 0); |
| 7468 | |
| 7469 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7470 | { |
| 7471 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7472 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7473 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7474 | |
| 7475 | ga_concat(&ga, theline); |
| 7476 | ga_append(&ga, '\n'); |
| 7477 | vim_free(theline); |
| 7478 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7479 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7480 | |
| 7481 | return (char_u *)ga.ga_data; |
| 7482 | } |