Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_getln.c: Functions for entering and editing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 37b1556 | 2018-08-14 22:08:25 +0200 | [diff] [blame] | 16 | #ifndef MAX |
| 17 | # define MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 18 | #endif |
| 19 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Variables shared between getcmdline(), redrawcmdline() and others. |
| 22 | * These need to be saved when using CTRL-R |, that's why they are in a |
| 23 | * structure. |
| 24 | */ |
| 25 | struct cmdline_info |
| 26 | { |
| 27 | char_u *cmdbuff; /* pointer to command line buffer */ |
| 28 | int cmdbufflen; /* length of cmdbuff */ |
| 29 | int cmdlen; /* number of chars in command line */ |
| 30 | int cmdpos; /* current cursor position */ |
| 31 | int cmdspos; /* cursor column on screen */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 32 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 33 | int cmdindent; /* number of spaces before cmdline */ |
| 34 | char_u *cmdprompt; /* message in front of cmdline */ |
| 35 | int cmdattr; /* attributes for prompt */ |
| 36 | int overstrike; /* Typing mode on the command line. Shared by |
| 37 | getcmdline() and put_on_cmdline(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 38 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 39 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | int xp_context; /* type of expansion */ |
| 41 | # ifdef FEAT_EVAL |
| 42 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 43 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 44 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 47 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 48 | * used by other functions. When invoking getcmdline() recursively it needs |
| 49 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 50 | * TODO: make it local to getcmdline() and pass it around. */ |
| 51 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | |
| 53 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 54 | |
| 55 | #ifdef FEAT_EVAL |
| 56 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 57 | #endif |
| 58 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 59 | static int extra_char = NUL; /* extra character to display when redrawing |
| 60 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 61 | static int extra_char_shift; |
| 62 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | #ifdef FEAT_CMDHIST |
| 64 | typedef struct hist_entry |
| 65 | { |
| 66 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 67 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 69 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | } histentry_T; |
| 71 | |
| 72 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 73 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 74 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 75 | /* identifying (unique) number of newest history entry */ |
| 76 | static int hislen = 0; /* actual length of history tables */ |
| 77 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 78 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 80 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 82 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 83 | # endif |
| 84 | #endif |
| 85 | |
| 86 | #ifdef FEAT_RIGHTLEFT |
| 87 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 88 | #endif |
| 89 | |
| 90 | #ifdef FEAT_FKMAP |
| 91 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 92 | #endif |
| 93 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 94 | static int cmdline_charsize(int idx); |
| 95 | static void set_cmdspos(void); |
| 96 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 98 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 100 | static void alloc_cmdbuff(int len); |
| 101 | static int realloc_cmdbuff(int len); |
| 102 | static void draw_cmdline(int start, int len); |
| 103 | static void save_cmdline(struct cmdline_info *ccp); |
| 104 | static void restore_cmdline(struct cmdline_info *ccp); |
| 105 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 107 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | #endif |
| 109 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 110 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 112 | static void redrawcmdprompt(void); |
| 113 | static void cursorcmd(void); |
| 114 | static int ccheck_abbr(int); |
| 115 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 116 | static void escape_fname(char_u **pp); |
| 117 | static int showmatches(expand_T *xp, int wildmenu); |
| 118 | static void set_expand_context(expand_T *xp); |
| 119 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 120 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 122 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 123 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 124 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 125 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 127 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 129 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 130 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | # endif |
| 132 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 133 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 134 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 135 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
| 137 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 138 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 141 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 142 | static int |
| 143 | #ifdef __BORLANDC__ |
| 144 | _RTLENTRYF |
| 145 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 146 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 150 | static void |
| 151 | trigger_cmd_autocmd(int typechar, int evt) |
| 152 | { |
| 153 | char_u typestr[2]; |
| 154 | |
| 155 | typestr[0] = typechar; |
| 156 | typestr[1] = NUL; |
| 157 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 158 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 161 | * Abandon the command line. |
| 162 | */ |
| 163 | static void |
| 164 | abandon_cmdline(void) |
| 165 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 166 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 167 | if (msg_scrolled == 0) |
| 168 | compute_cmdrow(); |
| 169 | MSG(""); |
| 170 | redraw_cmdline = TRUE; |
| 171 | } |
| 172 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 173 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 174 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 175 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 176 | * as a trailing \|, which can happen while typing a pattern. |
| 177 | */ |
| 178 | static int |
| 179 | empty_pattern(char_u *p) |
| 180 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 181 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 182 | |
| 183 | /* remove trailing \v and the like */ |
| 184 | while (n >= 2 && p[n - 2] == '\\' |
| 185 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 186 | n -= 2; |
| 187 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 188 | } |
| 189 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 190 | // Struct to store the viewstate during 'incsearch' highlighting. |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 191 | typedef struct { |
| 192 | colnr_T vs_curswant; |
| 193 | colnr_T vs_leftcol; |
| 194 | linenr_T vs_topline; |
| 195 | # ifdef FEAT_DIFF |
| 196 | int vs_topfill; |
| 197 | # endif |
| 198 | linenr_T vs_botline; |
| 199 | linenr_T vs_empty_rows; |
| 200 | } viewstate_T; |
| 201 | |
| 202 | static void |
| 203 | save_viewstate(viewstate_T *vs) |
| 204 | { |
| 205 | vs->vs_curswant = curwin->w_curswant; |
| 206 | vs->vs_leftcol = curwin->w_leftcol; |
| 207 | vs->vs_topline = curwin->w_topline; |
| 208 | # ifdef FEAT_DIFF |
| 209 | vs->vs_topfill = curwin->w_topfill; |
| 210 | # endif |
| 211 | vs->vs_botline = curwin->w_botline; |
| 212 | vs->vs_empty_rows = curwin->w_empty_rows; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | restore_viewstate(viewstate_T *vs) |
| 217 | { |
| 218 | curwin->w_curswant = vs->vs_curswant; |
| 219 | curwin->w_leftcol = vs->vs_leftcol; |
| 220 | curwin->w_topline = vs->vs_topline; |
| 221 | # ifdef FEAT_DIFF |
| 222 | curwin->w_topfill = vs->vs_topfill; |
| 223 | # endif |
| 224 | curwin->w_botline = vs->vs_botline; |
| 225 | curwin->w_empty_rows = vs->vs_empty_rows; |
| 226 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 227 | |
| 228 | // Struct to store the state of 'incsearch' highlighting. |
| 229 | typedef struct { |
| 230 | pos_T search_start; // where 'incsearch' starts searching |
| 231 | pos_T save_cursor; |
| 232 | viewstate_T init_viewstate; |
| 233 | viewstate_T old_viewstate; |
| 234 | pos_T match_start; |
| 235 | pos_T match_end; |
| 236 | int did_incsearch; |
| 237 | int incsearch_postponed; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 238 | int magic_save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 239 | } incsearch_state_T; |
| 240 | |
| 241 | static void |
| 242 | init_incsearch_state(incsearch_state_T *is_state) |
| 243 | { |
| 244 | is_state->match_start = curwin->w_cursor; |
| 245 | is_state->did_incsearch = FALSE; |
| 246 | is_state->incsearch_postponed = FALSE; |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 247 | is_state->magic_save = p_magic; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 248 | CLEAR_POS(&is_state->match_end); |
| 249 | is_state->save_cursor = curwin->w_cursor; // may be restored later |
| 250 | is_state->search_start = curwin->w_cursor; |
| 251 | save_viewstate(&is_state->init_viewstate); |
| 252 | save_viewstate(&is_state->old_viewstate); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * First move cursor to end of match, then to the start. This |
| 257 | * moves the whole match onto the screen when 'nowrap' is set. |
| 258 | */ |
| 259 | static void |
| 260 | set_search_match(pos_T *t) |
| 261 | { |
| 262 | t->lnum += search_match_lines; |
| 263 | t->col = search_match_endcol; |
| 264 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 265 | { |
| 266 | t->lnum = curbuf->b_ml.ml_line_count; |
| 267 | coladvance((colnr_T)MAXCOL); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Return TRUE when 'incsearch' highlighting is to be done. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 273 | * Sets search_first_line and search_last_line to the address range. |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 274 | */ |
| 275 | static int |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 276 | do_incsearch_highlighting(int firstc, incsearch_state_T *is_state, |
| 277 | int *skiplen, int *patlen) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 278 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 279 | char_u *cmd; |
| 280 | cmdmod_T save_cmdmod = cmdmod; |
| 281 | char_u *p; |
| 282 | int delim_optional = FALSE; |
| 283 | int delim; |
| 284 | char_u *end; |
| 285 | char_u *dummy; |
| 286 | exarg_T ea; |
| 287 | pos_T save_cursor; |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame^] | 288 | int use_last_pat; |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 289 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 290 | *skiplen = 0; |
| 291 | *patlen = ccline.cmdlen; |
| 292 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 293 | if (!p_is || cmd_silent) |
| 294 | return FALSE; |
| 295 | |
| 296 | // by default search all lines |
| 297 | search_first_line = 0; |
| 298 | search_last_line = MAXLNUM; |
| 299 | |
| 300 | if (firstc == '/' || firstc == '?') |
| 301 | return TRUE; |
| 302 | if (firstc != ':') |
| 303 | return FALSE; |
| 304 | |
| 305 | vim_memset(&ea, 0, sizeof(ea)); |
| 306 | ea.line1 = 1; |
| 307 | ea.line2 = 1; |
| 308 | ea.cmd = ccline.cmdbuff; |
| 309 | ea.addr_type = ADDR_LINES; |
| 310 | |
| 311 | parse_command_modifiers(&ea, &dummy, TRUE); |
| 312 | cmdmod = save_cmdmod; |
| 313 | |
| 314 | cmd = skip_range(ea.cmd, NULL); |
| 315 | if (vim_strchr((char_u *)"sgvl", *cmd) == NULL) |
| 316 | return FALSE; |
| 317 | |
| 318 | // Skip over "substitute" to find the pattern separator. |
| 319 | for (p = cmd; ASCII_ISALPHA(*p); ++p) |
| 320 | ; |
| 321 | if (*skipwhite(p) == NUL) |
| 322 | return FALSE; |
| 323 | |
| 324 | if (STRNCMP(cmd, "substitute", p - cmd) == 0 |
| 325 | || STRNCMP(cmd, "smagic", p - cmd) == 0 |
| 326 | || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0 |
| 327 | || STRNCMP(cmd, "vglobal", p - cmd) == 0) |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 328 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 329 | if (*cmd == 's' && cmd[1] == 'm') |
| 330 | p_magic = TRUE; |
| 331 | else if (*cmd == 's' && cmd[1] == 'n') |
| 332 | p_magic = FALSE; |
| 333 | } |
| 334 | else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0) |
| 335 | { |
| 336 | // skip over flags |
| 337 | while (ASCII_ISALPHA(*(p = skipwhite(p)))) |
| 338 | ++p; |
| 339 | if (*p == NUL) |
| 340 | return FALSE; |
| 341 | } |
| 342 | else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0 |
| 343 | || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0 |
| 344 | || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0 |
| 345 | || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0 |
| 346 | || STRNCMP(cmd, "global", p - cmd) == 0) |
| 347 | { |
| 348 | // skip over "!" |
| 349 | if (*p == '!') |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 350 | { |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 351 | p++; |
| 352 | if (*skipwhite(p) == NUL) |
| 353 | return FALSE; |
| 354 | } |
| 355 | if (*cmd != 'g') |
| 356 | delim_optional = TRUE; |
| 357 | } |
| 358 | else |
| 359 | return FALSE; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 360 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 361 | p = skipwhite(p); |
| 362 | delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++; |
| 363 | end = skip_regexp(p, delim, p_magic, NULL); |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 364 | |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame^] | 365 | use_last_pat = end == p && *end == delim; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 366 | |
Bram Moolenaar | 8b0d5ce | 2018-08-22 23:05:44 +0200 | [diff] [blame^] | 367 | if (end == p && !use_last_pat) |
| 368 | return FALSE; |
| 369 | |
| 370 | // Don't do 'hlsearch' highlighting if the pattern matches everything. |
| 371 | if (!use_last_pat) |
| 372 | { |
| 373 | char c = *end; |
| 374 | int empty; |
| 375 | |
| 376 | *end = NUL; |
| 377 | empty = empty_pattern(p); |
| 378 | *end = c; |
| 379 | if (empty) |
| 380 | return FALSE; |
| 381 | } |
| 382 | |
| 383 | // found a non-empty pattern or // |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 384 | *skiplen = (int)(p - ccline.cmdbuff); |
| 385 | *patlen = (int)(end - p); |
Bram Moolenaar | 264cf5c | 2018-08-18 21:05:31 +0200 | [diff] [blame] | 386 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 387 | // parse the address range |
| 388 | save_cursor = curwin->w_cursor; |
| 389 | curwin->w_cursor = is_state->search_start; |
| 390 | parse_cmd_address(&ea, &dummy); |
| 391 | if (ea.addr_count > 0) |
| 392 | { |
| 393 | // Allow for reverse match. |
| 394 | if (ea.line2 < ea.line1) |
| 395 | { |
| 396 | search_first_line = ea.line2; |
| 397 | search_last_line = ea.line1; |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | search_first_line = ea.line1; |
| 402 | search_last_line = ea.line2; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 403 | } |
| 404 | } |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 405 | else if (cmd[0] == 's' && cmd[1] != 'o') |
| 406 | { |
| 407 | // :s defaults to the current line |
| 408 | search_first_line = curwin->w_cursor.lnum; |
| 409 | search_last_line = curwin->w_cursor.lnum; |
| 410 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 411 | |
Bram Moolenaar | 111bbd6 | 2018-08-18 21:23:05 +0200 | [diff] [blame] | 412 | curwin->w_cursor = save_cursor; |
| 413 | return TRUE; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 416 | static void |
| 417 | finish_incsearch_highlighting( |
| 418 | int gotesc, |
| 419 | incsearch_state_T *is_state, |
| 420 | int call_update_screen) |
| 421 | { |
| 422 | if (is_state->did_incsearch) |
| 423 | { |
| 424 | is_state->did_incsearch = FALSE; |
| 425 | if (gotesc) |
| 426 | curwin->w_cursor = is_state->save_cursor; |
| 427 | else |
| 428 | { |
| 429 | if (!EQUAL_POS(is_state->save_cursor, is_state->search_start)) |
| 430 | { |
| 431 | // put the '" mark at the original position |
| 432 | curwin->w_cursor = is_state->save_cursor; |
| 433 | setpcmark(); |
| 434 | } |
| 435 | curwin->w_cursor = is_state->search_start; |
| 436 | } |
| 437 | restore_viewstate(&is_state->old_viewstate); |
| 438 | highlight_match = FALSE; |
| 439 | validate_cursor(); /* needed for TAB */ |
| 440 | if (call_update_screen) |
| 441 | update_screen(SOME_VALID); |
| 442 | else |
| 443 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 167ae42 | 2018-08-14 21:32:21 +0200 | [diff] [blame] | 444 | p_magic = is_state->magic_save; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 448 | /* |
| 449 | * Do 'incsearch' highlighting if desired. |
| 450 | */ |
| 451 | static void |
| 452 | may_do_incsearch_highlighting( |
| 453 | int firstc, |
| 454 | long count, |
| 455 | incsearch_state_T *is_state) |
| 456 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 457 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 458 | int i; |
| 459 | pos_T end_pos; |
| 460 | struct cmdline_info save_ccline; |
| 461 | #ifdef FEAT_RELTIME |
| 462 | proftime_T tm; |
| 463 | #endif |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 464 | int next_char; |
| 465 | int use_last_pat; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 466 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 467 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 468 | { |
| 469 | finish_incsearch_highlighting(FALSE, is_state, TRUE); |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 470 | return; |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 471 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 472 | |
| 473 | // If there is a character waiting, search and redraw later. |
| 474 | if (char_avail()) |
| 475 | { |
| 476 | is_state->incsearch_postponed = TRUE; |
| 477 | return; |
| 478 | } |
| 479 | is_state->incsearch_postponed = FALSE; |
| 480 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 481 | if (search_first_line == 0) |
| 482 | // start at the original cursor position |
| 483 | curwin->w_cursor = is_state->search_start; |
| 484 | else |
| 485 | { |
| 486 | // start at the first line in the range |
| 487 | curwin->w_cursor.lnum = search_first_line; |
| 488 | curwin->w_cursor.col = 0; |
| 489 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 490 | save_last_search_pattern(); |
| 491 | |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 492 | // Use the previous pattern for ":s//". |
| 493 | next_char = ccline.cmdbuff[skiplen + patlen]; |
| 494 | use_last_pat = patlen == 0 && skiplen > 0 |
| 495 | && ccline.cmdbuff[skiplen - 1] == next_char; |
| 496 | |
| 497 | // If there is no pattern, don't do anything. |
| 498 | if (patlen == 0 && !use_last_pat) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 499 | { |
| 500 | i = 0; |
| 501 | set_no_hlsearch(TRUE); // turn off previous highlight |
| 502 | redraw_all_later(SOME_VALID); |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
| 507 | |
| 508 | cursor_off(); // so the user knows we're busy |
| 509 | out_flush(); |
| 510 | ++emsg_off; // so it doesn't beep if bad expr |
| 511 | #ifdef FEAT_RELTIME |
| 512 | // Set the time limit to half a second. |
| 513 | profile_setlimit(500L, &tm); |
| 514 | #endif |
| 515 | if (!p_hls) |
| 516 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 976b847 | 2018-08-12 15:49:47 +0200 | [diff] [blame] | 517 | if (search_first_line != 0) |
| 518 | search_flags += SEARCH_START; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 519 | ccline.cmdbuff[skiplen + patlen] = NUL; |
| 520 | i = do_search(NULL, firstc == ':' ? '/' : firstc, |
| 521 | ccline.cmdbuff + skiplen, count, search_flags, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 522 | #ifdef FEAT_RELTIME |
| 523 | &tm, NULL |
| 524 | #else |
| 525 | NULL, NULL |
| 526 | #endif |
| 527 | ); |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 528 | ccline.cmdbuff[skiplen + patlen] = next_char; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 529 | --emsg_off; |
| 530 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 531 | if (curwin->w_cursor.lnum < search_first_line |
| 532 | || curwin->w_cursor.lnum > search_last_line) |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 533 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 534 | // match outside of address range |
| 535 | i = 0; |
Bram Moolenaar | 2f6a346 | 2018-08-14 18:16:52 +0200 | [diff] [blame] | 536 | curwin->w_cursor = is_state->search_start; |
| 537 | } |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 538 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 539 | // if interrupted while searching, behave like it failed |
| 540 | if (got_int) |
| 541 | { |
| 542 | (void)vpeekc(); // remove <C-C> from input stream |
| 543 | got_int = FALSE; // don't abandon the command line |
| 544 | i = 0; |
| 545 | } |
| 546 | else if (char_avail()) |
| 547 | // cancelled searching because a char was typed |
| 548 | is_state->incsearch_postponed = TRUE; |
| 549 | } |
| 550 | if (i != 0) |
| 551 | highlight_match = TRUE; // highlight position |
| 552 | else |
| 553 | highlight_match = FALSE; // remove highlight |
| 554 | |
| 555 | // First restore the old curwin values, so the screen is positioned in the |
| 556 | // same way as the actual search command. |
| 557 | restore_viewstate(&is_state->old_viewstate); |
| 558 | changed_cline_bef_curs(); |
| 559 | update_topline(); |
| 560 | |
| 561 | if (i != 0) |
| 562 | { |
| 563 | pos_T save_pos = curwin->w_cursor; |
| 564 | |
| 565 | is_state->match_start = curwin->w_cursor; |
| 566 | set_search_match(&curwin->w_cursor); |
| 567 | validate_cursor(); |
| 568 | end_pos = curwin->w_cursor; |
| 569 | is_state->match_end = end_pos; |
| 570 | curwin->w_cursor = save_pos; |
| 571 | } |
| 572 | else |
| 573 | end_pos = curwin->w_cursor; // shutup gcc 4 |
| 574 | |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 575 | validate_cursor(); |
| 576 | // May redraw the status line to show the cursor position. |
| 577 | if (p_ru && curwin->w_status_height > 0) |
| 578 | curwin->w_redr_status = TRUE; |
| 579 | |
| 580 | save_cmdline(&save_ccline); |
| 581 | update_screen(SOME_VALID); |
| 582 | restore_cmdline(&save_ccline); |
| 583 | restore_last_search_pattern(); |
| 584 | |
| 585 | // Leave it at the end to make CTRL-R CTRL-W work. |
| 586 | if (i != 0) |
| 587 | curwin->w_cursor = end_pos; |
| 588 | |
| 589 | msg_starthere(); |
| 590 | redrawcmdline(); |
| 591 | is_state->did_incsearch = TRUE; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next |
| 596 | * or previous match. |
| 597 | * Returns FAIL when jumping to cmdline_not_changed; |
| 598 | */ |
| 599 | static int |
| 600 | may_adjust_incsearch_highlighting( |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 601 | int firstc, |
| 602 | long count, |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 603 | incsearch_state_T *is_state, |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 604 | int c) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 605 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 606 | int skiplen, patlen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 607 | pos_T t; |
| 608 | char_u *pat; |
| 609 | int search_flags = SEARCH_NOOF; |
| 610 | int i; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 611 | int save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 612 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 613 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 614 | return OK; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 615 | if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 616 | return FAIL; |
| 617 | |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 618 | if (firstc == ccline.cmdbuff[skiplen]) |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 619 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 620 | pat = last_search_pattern(); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 621 | skiplen = 0; |
Bram Moolenaar | d7cc163 | 2018-08-14 20:18:26 +0200 | [diff] [blame] | 622 | patlen = (int)STRLEN(pat); |
Bram Moolenaar | ef73a28 | 2018-08-11 19:02:22 +0200 | [diff] [blame] | 623 | } |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 624 | else |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 625 | pat = ccline.cmdbuff + skiplen; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 626 | |
| 627 | save_last_search_pattern(); |
| 628 | cursor_off(); |
| 629 | out_flush(); |
| 630 | if (c == Ctrl_G) |
| 631 | { |
| 632 | t = is_state->match_end; |
| 633 | if (LT_POS(is_state->match_start, is_state->match_end)) |
| 634 | // Start searching at the end of the match not at the beginning of |
| 635 | // the next column. |
| 636 | (void)decl(&t); |
| 637 | search_flags += SEARCH_COL; |
| 638 | } |
| 639 | else |
| 640 | t = is_state->match_start; |
| 641 | if (!p_hls) |
| 642 | search_flags += SEARCH_KEEP; |
| 643 | ++emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 644 | save = pat[patlen]; |
| 645 | pat[patlen] = NUL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 646 | i = searchit(curwin, curbuf, &t, |
| 647 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 648 | pat, count, search_flags, |
| 649 | RE_SEARCH, 0, NULL, NULL); |
| 650 | --emsg_off; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 651 | pat[patlen] = save; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 652 | if (i) |
| 653 | { |
| 654 | is_state->search_start = is_state->match_start; |
| 655 | is_state->match_end = t; |
| 656 | is_state->match_start = t; |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 657 | if (c == Ctrl_T && firstc != '?') |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 658 | { |
| 659 | // Move just before the current match, so that when nv_search |
| 660 | // finishes the cursor will be put back on the match. |
| 661 | is_state->search_start = t; |
| 662 | (void)decl(&is_state->search_start); |
| 663 | } |
| 664 | else if (c == Ctrl_G && firstc == '?') |
| 665 | { |
| 666 | // Move just after the current match, so that when nv_search |
| 667 | // finishes the cursor will be put back on the match. |
| 668 | is_state->search_start = t; |
| 669 | (void)incl(&is_state->search_start); |
| 670 | } |
| 671 | if (LT_POS(t, is_state->search_start) && c == Ctrl_G) |
| 672 | { |
| 673 | // wrap around |
| 674 | is_state->search_start = t; |
| 675 | if (firstc == '?') |
| 676 | (void)incl(&is_state->search_start); |
| 677 | else |
| 678 | (void)decl(&is_state->search_start); |
| 679 | } |
| 680 | |
| 681 | set_search_match(&is_state->match_end); |
| 682 | curwin->w_cursor = is_state->match_start; |
| 683 | changed_cline_bef_curs(); |
| 684 | update_topline(); |
| 685 | validate_cursor(); |
| 686 | highlight_match = TRUE; |
| 687 | save_viewstate(&is_state->old_viewstate); |
| 688 | update_screen(NOT_VALID); |
| 689 | redrawcmdline(); |
| 690 | } |
| 691 | else |
| 692 | vim_beep(BO_ERROR); |
| 693 | restore_last_search_pattern(); |
| 694 | return FAIL; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * When CTRL-L typed: add character from the match to the pattern. |
| 699 | * May set "*c" to the added character. |
| 700 | * Return OK when jumping to cmdline_not_changed. |
| 701 | */ |
| 702 | static int |
| 703 | may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state) |
| 704 | { |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 705 | int skiplen, patlen; |
| 706 | |
| 707 | if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 708 | return FAIL; |
| 709 | |
| 710 | // Add a character from under the cursor for 'incsearch'. |
| 711 | if (is_state->did_incsearch) |
| 712 | { |
| 713 | curwin->w_cursor = is_state->match_end; |
| 714 | if (!EQUAL_POS(curwin->w_cursor, is_state->search_start)) |
| 715 | { |
| 716 | *c = gchar_cursor(); |
| 717 | |
| 718 | // If 'ignorecase' and 'smartcase' are set and the |
| 719 | // command line has no uppercase characters, convert |
| 720 | // the character to lowercase. |
Bram Moolenaar | b0acacd | 2018-08-11 16:40:43 +0200 | [diff] [blame] | 721 | if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen)) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 722 | *c = MB_TOLOWER(*c); |
| 723 | if (*c != NUL) |
| 724 | { |
| 725 | if (*c == firstc || vim_strchr((char_u *)( |
| 726 | p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL) |
| 727 | { |
| 728 | // put a backslash before special characters |
| 729 | stuffcharReadbuff(*c); |
| 730 | *c = '\\'; |
| 731 | } |
| 732 | return FAIL; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | return OK; |
| 737 | } |
Bram Moolenaar | 9b25af3 | 2018-04-28 13:56:09 +0200 | [diff] [blame] | 738 | #endif |
| 739 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 740 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | * getcmdline() - accept a command line starting with firstc. |
| 742 | * |
| 743 | * firstc == ':' get ":" command line. |
| 744 | * firstc == '/' or '?' get search pattern |
| 745 | * firstc == '=' get expression |
| 746 | * firstc == '@' get text for input() function |
| 747 | * firstc == '>' get text for debug mode |
| 748 | * firstc == NUL get text for :insert command |
| 749 | * firstc == -1 like NUL, and break on CTRL-C |
| 750 | * |
| 751 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 752 | * command line. |
| 753 | * |
| 754 | * Careful: getcmdline() can be called recursively! |
| 755 | * |
| 756 | * Return pointer to allocated string if there is a commandline, NULL |
| 757 | * otherwise. |
| 758 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 760 | getcmdline( |
| 761 | int firstc, |
| 762 | long count UNUSED, /* only used for incremental search */ |
| 763 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 764 | { |
| 765 | int c; |
| 766 | int i; |
| 767 | int j; |
| 768 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 769 | int do_abbr; /* when TRUE check for abbr. */ |
| 770 | #ifdef FEAT_CMDHIST |
| 771 | char_u *lookfor = NULL; /* string to match */ |
| 772 | int hiscnt; /* current history line in use */ |
| 773 | int histype; /* history type to be used */ |
| 774 | #endif |
| 775 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 776 | incsearch_state_T is_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 777 | #endif |
| 778 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 779 | int wim_index = 0; /* index in wim_flags[] */ |
| 780 | int res; |
| 781 | int save_msg_scroll = msg_scroll; |
| 782 | int save_State = State; /* remember State when called */ |
| 783 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 784 | #ifdef FEAT_MOUSE |
| 785 | /* mouse drag and release events are ignored, unless they are |
| 786 | * preceded with a mouse down event */ |
| 787 | int ignore_drag_release = TRUE; |
| 788 | #endif |
| 789 | #ifdef FEAT_EVAL |
| 790 | int break_ctrl_c = FALSE; |
| 791 | #endif |
| 792 | expand_T xpc; |
| 793 | long *b_im_ptr = NULL; |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 794 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 795 | /* Everything that may work recursively should save and restore the |
| 796 | * current command line in save_ccline. That includes update_screen(), a |
| 797 | * custom status line may invoke ":normal". */ |
| 798 | struct cmdline_info save_ccline; |
| 799 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 800 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 801 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 802 | #ifdef FEAT_EVAL |
| 803 | if (firstc == -1) |
| 804 | { |
| 805 | firstc = NUL; |
| 806 | break_ctrl_c = TRUE; |
| 807 | } |
| 808 | #endif |
| 809 | #ifdef FEAT_RIGHTLEFT |
| 810 | /* start without Hebrew mapping for a command line */ |
| 811 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 812 | cmd_hkmap = 0; |
| 813 | #endif |
| 814 | |
| 815 | ccline.overstrike = FALSE; /* always start in insert mode */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 818 | init_incsearch_state(&is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | #endif |
| 820 | |
| 821 | /* |
| 822 | * set some variables for redrawcmd() |
| 823 | */ |
| 824 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 825 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 826 | |
| 827 | /* alloc initial ccline.cmdbuff */ |
| 828 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | if (ccline.cmdbuff == NULL) |
| 830 | return NULL; /* out of memory */ |
| 831 | ccline.cmdlen = ccline.cmdpos = 0; |
| 832 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 833 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 834 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 835 | /* autoindent for :insert and :append */ |
| 836 | if (firstc <= 0) |
| 837 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 838 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 839 | ccline.cmdbuff[indent] = NUL; |
| 840 | ccline.cmdpos = indent; |
| 841 | ccline.cmdspos = indent; |
| 842 | ccline.cmdlen = indent; |
| 843 | } |
| 844 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 845 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 846 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | |
| 848 | #ifdef FEAT_RIGHTLEFT |
| 849 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 850 | && (firstc == '/' || firstc == '?')) |
| 851 | cmdmsg_rl = TRUE; |
| 852 | else |
| 853 | cmdmsg_rl = FALSE; |
| 854 | #endif |
| 855 | |
| 856 | redir_off = TRUE; /* don't redirect the typed command */ |
| 857 | if (!cmd_silent) |
| 858 | { |
| 859 | i = msg_scrolled; |
| 860 | msg_scrolled = 0; /* avoid wait_return message */ |
| 861 | gotocmdline(TRUE); |
| 862 | msg_scrolled += i; |
| 863 | redrawcmdprompt(); /* draw prompt or indent */ |
| 864 | set_cmdspos(); |
| 865 | } |
| 866 | xpc.xp_context = EXPAND_NOTHING; |
| 867 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 868 | #ifndef BACKSLASH_IN_FILENAME |
| 869 | xpc.xp_shell = FALSE; |
| 870 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 872 | #if defined(FEAT_EVAL) |
| 873 | if (ccline.input_fn) |
| 874 | { |
| 875 | xpc.xp_context = ccline.xp_context; |
| 876 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 877 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 878 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 879 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 880 | } |
| 881 | #endif |
| 882 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | /* |
| 884 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 885 | * doing ":@0" when register 0 doesn't contain a CR. |
| 886 | */ |
| 887 | msg_scroll = FALSE; |
| 888 | |
| 889 | State = CMDLINE; |
| 890 | |
| 891 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 892 | { |
| 893 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 894 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 895 | b_im_ptr = &curbuf->b_p_iminsert; |
| 896 | else |
| 897 | b_im_ptr = &curbuf->b_p_imsearch; |
| 898 | if (*b_im_ptr == B_IMODE_LMAP) |
| 899 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 900 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 902 | #endif |
| 903 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 904 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 905 | else if (p_imcmdline) |
| 906 | im_set_active(TRUE); |
| 907 | #endif |
| 908 | |
| 909 | #ifdef FEAT_MOUSE |
| 910 | setmouse(); |
| 911 | #endif |
| 912 | #ifdef CURSOR_SHAPE |
| 913 | ui_cursor_shape(); /* may show different cursor shape */ |
| 914 | #endif |
| 915 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 916 | /* When inside an autocommand for writing "exiting" may be set and |
| 917 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 918 | settmode(TMODE_RAW); |
| 919 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 920 | /* Trigger CmdlineEnter autocommands. */ |
| 921 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 922 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 923 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | #ifdef FEAT_CMDHIST |
| 925 | init_history(); |
| 926 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 927 | histype = hist_char2type(firstc); |
| 928 | #endif |
| 929 | |
| 930 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 931 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | #endif |
| 933 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 934 | /* If something above caused an error, reset the flags, we do want to type |
| 935 | * and execute commands. Display may be messed up a bit. */ |
| 936 | if (did_emsg) |
| 937 | redrawcmd(); |
| 938 | did_emsg = FALSE; |
| 939 | got_int = FALSE; |
| 940 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | /* |
| 942 | * Collect the command string, handling editing keys. |
| 943 | */ |
| 944 | for (;;) |
| 945 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 946 | redir_off = TRUE; /* Don't redirect the typed command. |
| 947 | Repeated, because a ":redir" inside |
| 948 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 949 | #ifdef USE_ON_FLY_SCROLL |
| 950 | dont_scroll = FALSE; /* allow scrolling here */ |
| 951 | #endif |
| 952 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 953 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 954 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 955 | that occurs while typing a command should |
| 956 | cause the command not to be executed. */ |
| 957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 959 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 960 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 961 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 962 | do |
| 963 | { |
| 964 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 965 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 966 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | if (KeyTyped) |
| 968 | { |
| 969 | some_key_typed = TRUE; |
| 970 | #ifdef FEAT_RIGHTLEFT |
| 971 | if (cmd_hkmap) |
| 972 | c = hkmap(c); |
| 973 | # ifdef FEAT_FKMAP |
| 974 | if (cmd_fkmap) |
| 975 | c = cmdl_fkmap(c); |
| 976 | # endif |
| 977 | if (cmdmsg_rl && !KeyStuffed) |
| 978 | { |
| 979 | /* Invert horizontal movements and operations. Only when |
| 980 | * typed by the user directly, not when the result of a |
| 981 | * mapping. */ |
| 982 | switch (c) |
| 983 | { |
| 984 | case K_RIGHT: c = K_LEFT; break; |
| 985 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 986 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 987 | case K_LEFT: c = K_RIGHT; break; |
| 988 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 989 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 990 | } |
| 991 | } |
| 992 | #endif |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Ignore got_int when CTRL-C was typed here. |
| 997 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 998 | * ":g/pat/normal /pat" (without the <CR>). |
| 999 | * Don't ignore it for the input() function. |
| 1000 | */ |
| 1001 | if ((c == Ctrl_C |
| 1002 | #ifdef UNIX |
| 1003 | || c == intr_char |
| 1004 | #endif |
| 1005 | ) |
| 1006 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 1007 | && firstc != '@' |
| 1008 | #endif |
| 1009 | #ifdef FEAT_EVAL |
| 1010 | && !break_ctrl_c |
| 1011 | #endif |
| 1012 | && !global_busy) |
| 1013 | got_int = FALSE; |
| 1014 | |
| 1015 | #ifdef FEAT_CMDHIST |
| 1016 | /* free old command line when finished moving around in the history |
| 1017 | * list */ |
| 1018 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1019 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1020 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 1022 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1023 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1025 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1026 | #endif |
| 1027 | |
| 1028 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1029 | * When there are matching completions to select <S-Tab> works like |
| 1030 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1032 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | c = Ctrl_P; |
| 1034 | |
| 1035 | #ifdef FEAT_WILDMENU |
| 1036 | /* Special translations for 'wildmenu' */ |
| 1037 | if (did_wild_list && p_wmnu) |
| 1038 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1039 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1041 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1042 | c = Ctrl_N; |
| 1043 | } |
| 1044 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 1045 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 1046 | && ccline.cmdpos > 1 |
| 1047 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 1048 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 1049 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 1050 | c = K_DOWN; |
| 1051 | #endif |
| 1052 | |
| 1053 | /* free expanded names when finished walking through matches */ |
| 1054 | if (xpc.xp_numfiles != -1 |
| 1055 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 1056 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 1057 | && c != Ctrl_L) |
| 1058 | { |
| 1059 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1060 | did_wild_list = FALSE; |
| 1061 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1062 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1063 | #endif |
| 1064 | xpc.xp_context = EXPAND_NOTHING; |
| 1065 | wim_index = 0; |
| 1066 | #ifdef FEAT_WILDMENU |
| 1067 | if (p_wmnu && wild_menu_showing != 0) |
| 1068 | { |
| 1069 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1070 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 1073 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (wild_menu_showing == WM_SCROLLED) |
| 1076 | { |
| 1077 | /* Entered command line, move it up */ |
| 1078 | cmdline_row--; |
| 1079 | redrawcmd(); |
| 1080 | } |
| 1081 | else if (save_p_ls != -1) |
| 1082 | { |
| 1083 | /* restore 'laststatus' and 'winminheight' */ |
| 1084 | p_ls = save_p_ls; |
| 1085 | p_wmh = save_p_wmh; |
| 1086 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1087 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1089 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | redrawcmd(); |
| 1091 | save_p_ls = -1; |
| 1092 | } |
| 1093 | else |
| 1094 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1095 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | redraw_statuslines(); |
| 1097 | } |
| 1098 | KeyTyped = skt; |
| 1099 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 1100 | if (ccline.input_fn) |
| 1101 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | } |
| 1103 | #endif |
| 1104 | } |
| 1105 | |
| 1106 | #ifdef FEAT_WILDMENU |
| 1107 | /* Special translations for 'wildmenu' */ |
| 1108 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 1109 | { |
| 1110 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1111 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 1112 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1113 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1114 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1115 | { |
| 1116 | /* Hitting <Up>: Remove one submenu name in front of the |
| 1117 | * cursor */ |
| 1118 | int found = FALSE; |
| 1119 | |
| 1120 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1121 | i = 0; |
| 1122 | while (--j > 0) |
| 1123 | { |
| 1124 | /* check for start of menu name */ |
| 1125 | if (ccline.cmdbuff[j] == ' ' |
| 1126 | && ccline.cmdbuff[j - 1] != '\\') |
| 1127 | { |
| 1128 | i = j + 1; |
| 1129 | break; |
| 1130 | } |
| 1131 | /* check for start of submenu name */ |
| 1132 | if (ccline.cmdbuff[j] == '.' |
| 1133 | && ccline.cmdbuff[j - 1] != '\\') |
| 1134 | { |
| 1135 | if (found) |
| 1136 | { |
| 1137 | i = j + 1; |
| 1138 | break; |
| 1139 | } |
| 1140 | else |
| 1141 | found = TRUE; |
| 1142 | } |
| 1143 | } |
| 1144 | if (i > 0) |
| 1145 | cmdline_del(i); |
| 1146 | c = p_wc; |
| 1147 | xpc.xp_context = EXPAND_NOTHING; |
| 1148 | } |
| 1149 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1150 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1151 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 1152 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | { |
| 1154 | char_u upseg[5]; |
| 1155 | |
| 1156 | upseg[0] = PATHSEP; |
| 1157 | upseg[1] = '.'; |
| 1158 | upseg[2] = '.'; |
| 1159 | upseg[3] = PATHSEP; |
| 1160 | upseg[4] = NUL; |
| 1161 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1162 | if (c == K_DOWN |
| 1163 | && ccline.cmdpos > 0 |
| 1164 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 1165 | && (ccline.cmdpos < 3 |
| 1166 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1167 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 1168 | { |
| 1169 | /* go down a directory */ |
| 1170 | c = p_wc; |
| 1171 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1172 | 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] | 1173 | { |
| 1174 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 1175 | int found = FALSE; |
| 1176 | |
| 1177 | j = ccline.cmdpos; |
| 1178 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1179 | while (--j > i) |
| 1180 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1181 | #ifdef FEAT_MBYTE |
| 1182 | if (has_mbyte) |
| 1183 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1184 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 1186 | { |
| 1187 | found = TRUE; |
| 1188 | break; |
| 1189 | } |
| 1190 | } |
| 1191 | if (found |
| 1192 | && ccline.cmdbuff[j - 1] == '.' |
| 1193 | && ccline.cmdbuff[j - 2] == '.' |
| 1194 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 1195 | { |
| 1196 | cmdline_del(j - 2); |
| 1197 | c = p_wc; |
| 1198 | } |
| 1199 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1200 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1201 | { |
| 1202 | /* go up a directory */ |
| 1203 | int found = FALSE; |
| 1204 | |
| 1205 | j = ccline.cmdpos - 1; |
| 1206 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 1207 | while (--j > i) |
| 1208 | { |
| 1209 | #ifdef FEAT_MBYTE |
| 1210 | if (has_mbyte) |
| 1211 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 1212 | #endif |
| 1213 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 1214 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 1215 | && vim_strchr((char_u *)" *?[{`$%#", |
| 1216 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | #endif |
| 1218 | ) |
| 1219 | { |
| 1220 | if (found) |
| 1221 | { |
| 1222 | i = j + 1; |
| 1223 | break; |
| 1224 | } |
| 1225 | else |
| 1226 | found = TRUE; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | if (!found) |
| 1231 | j = i; |
| 1232 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 1233 | j += 4; |
| 1234 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 1235 | && j == i) |
| 1236 | j += 3; |
| 1237 | else |
| 1238 | j = 0; |
| 1239 | if (j > 0) |
| 1240 | { |
| 1241 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 1242 | * machine-specific stuff here and in upseg init */ |
| 1243 | cmdline_del(j); |
| 1244 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 1245 | } |
| 1246 | else if (ccline.cmdpos > i) |
| 1247 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* Now complete in the new directory. Set KeyTyped in case the |
| 1250 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1251 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 1252 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | |
| 1256 | #endif /* FEAT_WILDMENU */ |
| 1257 | |
| 1258 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 1259 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 1260 | if (c == Ctrl_BSL) |
| 1261 | { |
| 1262 | ++no_mapping; |
| 1263 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1264 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | --no_mapping; |
| 1266 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 1267 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 1268 | * is in a mapping. */ |
| 1269 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 1270 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | { |
| 1272 | vungetc(c); |
| 1273 | c = Ctrl_BSL; |
| 1274 | } |
| 1275 | #ifdef FEAT_EVAL |
| 1276 | else if (c == 'e') |
| 1277 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 1278 | char_u *p = NULL; |
| 1279 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1280 | |
| 1281 | /* |
| 1282 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1283 | * Need to save and restore the current command line, to be |
| 1284 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | */ |
| 1286 | if (ccline.cmdpos == ccline.cmdlen) |
| 1287 | new_cmdpos = 99999; /* keep it at the end */ |
| 1288 | else |
| 1289 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1290 | |
| 1291 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1293 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1294 | if (c == '=') |
| 1295 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1296 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 1297 | * to avoid nasty things like going to another buffer when |
| 1298 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1299 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1300 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 1302 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1303 | restore_cmdline(&save_ccline); |
| 1304 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1305 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1306 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1307 | len = (int)STRLEN(p); |
| 1308 | if (realloc_cmdbuff(len + 1) == OK) |
| 1309 | { |
| 1310 | ccline.cmdlen = len; |
| 1311 | STRCPY(ccline.cmdbuff, p); |
| 1312 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1314 | /* Restore the cursor or use the position set with |
| 1315 | * set_cmdline_pos(). */ |
| 1316 | if (new_cmdpos > ccline.cmdlen) |
| 1317 | ccline.cmdpos = ccline.cmdlen; |
| 1318 | else |
| 1319 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1320 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 1321 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1322 | redrawcmd(); |
| 1323 | goto cmdline_changed; |
| 1324 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 1328 | got_int = FALSE; /* don't abandon the command line */ |
| 1329 | did_emsg = FALSE; |
| 1330 | emsg_on_display = FALSE; |
| 1331 | redrawcmd(); |
| 1332 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | } |
| 1334 | #endif |
| 1335 | else |
| 1336 | { |
| 1337 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 1338 | restart_edit = 'a'; |
| 1339 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 1340 | in history */ |
| 1341 | goto returncmd; /* back to Normal mode */ |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | #ifdef FEAT_CMDWIN |
| 1346 | if (c == cedit_key || c == K_CMDWIN) |
| 1347 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1348 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 1349 | { |
| 1350 | /* |
| 1351 | * Open a window to edit the command line (and history). |
| 1352 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1353 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 1354 | some_key_typed = TRUE; |
| 1355 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1356 | } |
| 1357 | # ifdef FEAT_DIGRAPHS |
| 1358 | else |
| 1359 | # endif |
| 1360 | #endif |
| 1361 | #ifdef FEAT_DIGRAPHS |
| 1362 | c = do_digraph(c); |
| 1363 | #endif |
| 1364 | |
| 1365 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 1366 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 1367 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1368 | /* In Ex mode a backslash escapes a newline. */ |
| 1369 | if (exmode_active |
| 1370 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1371 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 1372 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1373 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1374 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1375 | if (c == K_KENTER) |
| 1376 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 1378 | else |
| 1379 | { |
| 1380 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 1381 | truncate the cmdline now. */ |
| 1382 | if (ccheck_abbr(c + ABBR_OFF)) |
| 1383 | goto cmdline_changed; |
| 1384 | if (!cmd_silent) |
| 1385 | { |
| 1386 | windgoto(msg_row, 0); |
| 1387 | out_flush(); |
| 1388 | } |
| 1389 | break; |
| 1390 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * Completion for 'wildchar' or 'wildcharm' key. |
| 1395 | * - hitting <ESC> twice means: abandon command line. |
| 1396 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 1397 | * typed, not when it comes from a macro |
| 1398 | */ |
| 1399 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 1400 | { |
| 1401 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 1402 | { |
| 1403 | /* if 'wildmode' contains "list" may still need to list */ |
| 1404 | if (xpc.xp_numfiles > 1 |
| 1405 | && !did_wild_list |
| 1406 | && (wim_flags[wim_index] & WIM_LIST)) |
| 1407 | { |
| 1408 | (void)showmatches(&xpc, FALSE); |
| 1409 | redrawcmd(); |
| 1410 | did_wild_list = TRUE; |
| 1411 | } |
| 1412 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1413 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1414 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1416 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1417 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | else |
| 1419 | res = OK; /* don't insert 'wildchar' now */ |
| 1420 | } |
| 1421 | else /* typed p_wc first time */ |
| 1422 | { |
| 1423 | wim_index = 0; |
| 1424 | j = ccline.cmdpos; |
| 1425 | /* if 'wildmode' first contains "longest", get longest |
| 1426 | * common part */ |
| 1427 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1428 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1429 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1430 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1431 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 1432 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1433 | |
| 1434 | /* if interrupted while completing, behave like it failed */ |
| 1435 | if (got_int) |
| 1436 | { |
| 1437 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1438 | got_int = FALSE; /* don't abandon the command line */ |
| 1439 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 1440 | #ifdef FEAT_WILDMENU |
| 1441 | xpc.xp_context = EXPAND_NOTHING; |
| 1442 | #endif |
| 1443 | goto cmdline_changed; |
| 1444 | } |
| 1445 | |
| 1446 | /* when more than one match, and 'wildmode' first contains |
| 1447 | * "list", or no change and 'wildmode' contains "longest,list", |
| 1448 | * list all matches */ |
| 1449 | if (res == OK && xpc.xp_numfiles > 1) |
| 1450 | { |
| 1451 | /* a "longest" that didn't do anything is skipped (but not |
| 1452 | * "list:longest") */ |
| 1453 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 1454 | wim_index = 1; |
| 1455 | if ((wim_flags[wim_index] & WIM_LIST) |
| 1456 | #ifdef FEAT_WILDMENU |
| 1457 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 1458 | #endif |
| 1459 | ) |
| 1460 | { |
| 1461 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 1462 | { |
| 1463 | #ifdef FEAT_WILDMENU |
| 1464 | int p_wmnu_save = p_wmnu; |
| 1465 | p_wmnu = 0; |
| 1466 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1467 | /* remove match */ |
| 1468 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1469 | #ifdef FEAT_WILDMENU |
| 1470 | p_wmnu = p_wmnu_save; |
| 1471 | #endif |
| 1472 | } |
| 1473 | #ifdef FEAT_WILDMENU |
| 1474 | (void)showmatches(&xpc, p_wmnu |
| 1475 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 1476 | #else |
| 1477 | (void)showmatches(&xpc, FALSE); |
| 1478 | #endif |
| 1479 | redrawcmd(); |
| 1480 | did_wild_list = TRUE; |
| 1481 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1482 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 1483 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1484 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1485 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 1486 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1487 | } |
| 1488 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1489 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1490 | } |
| 1491 | #ifdef FEAT_WILDMENU |
| 1492 | else if (xpc.xp_numfiles == -1) |
| 1493 | xpc.xp_context = EXPAND_NOTHING; |
| 1494 | #endif |
| 1495 | } |
| 1496 | if (wim_index < 3) |
| 1497 | ++wim_index; |
| 1498 | if (c == ESC) |
| 1499 | gotesc = TRUE; |
| 1500 | if (res == OK) |
| 1501 | goto cmdline_changed; |
| 1502 | } |
| 1503 | |
| 1504 | gotesc = FALSE; |
| 1505 | |
| 1506 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 1507 | if (c == K_S_TAB && KeyTyped) |
| 1508 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1509 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 1510 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 1511 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | goto cmdline_changed; |
| 1513 | } |
| 1514 | |
| 1515 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 1516 | c = NL; |
| 1517 | |
| 1518 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1519 | |
| 1520 | /* |
| 1521 | * Big switch for a typed command line character. |
| 1522 | */ |
| 1523 | switch (c) |
| 1524 | { |
| 1525 | case K_BS: |
| 1526 | case Ctrl_H: |
| 1527 | case K_DEL: |
| 1528 | case K_KDEL: |
| 1529 | case Ctrl_W: |
| 1530 | #ifdef FEAT_FKMAP |
| 1531 | if (cmd_fkmap && c == K_BS) |
| 1532 | c = K_DEL; |
| 1533 | #endif |
| 1534 | if (c == K_KDEL) |
| 1535 | c = K_DEL; |
| 1536 | |
| 1537 | /* |
| 1538 | * delete current character is the same as backspace on next |
| 1539 | * character, except at end of line |
| 1540 | */ |
| 1541 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1542 | ++ccline.cmdpos; |
| 1543 | #ifdef FEAT_MBYTE |
| 1544 | if (has_mbyte && c == K_DEL) |
| 1545 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1546 | ccline.cmdbuff + ccline.cmdpos); |
| 1547 | #endif |
| 1548 | if (ccline.cmdpos > 0) |
| 1549 | { |
| 1550 | char_u *p; |
| 1551 | |
| 1552 | j = ccline.cmdpos; |
| 1553 | p = ccline.cmdbuff + j; |
| 1554 | #ifdef FEAT_MBYTE |
| 1555 | if (has_mbyte) |
| 1556 | { |
| 1557 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1558 | if (c == Ctrl_W) |
| 1559 | { |
| 1560 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1561 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1562 | i = mb_get_class(p); |
| 1563 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1564 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1565 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1566 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | else |
| 1570 | #endif |
| 1571 | if (c == Ctrl_W) |
| 1572 | { |
| 1573 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1574 | --p; |
| 1575 | i = vim_iswordc(p[-1]); |
| 1576 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1577 | && vim_iswordc(p[-1]) == i) |
| 1578 | --p; |
| 1579 | } |
| 1580 | else |
| 1581 | --p; |
| 1582 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1583 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1584 | i = ccline.cmdpos; |
| 1585 | while (i < ccline.cmdlen) |
| 1586 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1587 | |
| 1588 | /* Truncate at the end, required for multi-byte chars. */ |
| 1589 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1590 | #ifdef FEAT_SEARCH_EXTRA |
| 1591 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1592 | { |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1593 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1594 | /* save view settings, so that the screen |
| 1595 | * won't be restored at the wrong position */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1596 | is_state.old_viewstate = is_state.init_viewstate; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1597 | } |
| 1598 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1599 | redrawcmd(); |
| 1600 | } |
| 1601 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1602 | && ccline.cmdprompt == NULL && indent == 0) |
| 1603 | { |
| 1604 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1605 | if (exmode_active |
| 1606 | #ifdef FEAT_EVAL |
| 1607 | || ccline.cmdfirstc == '>' |
| 1608 | #endif |
| 1609 | ) |
| 1610 | goto cmdline_not_changed; |
| 1611 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1612 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | if (!cmd_silent) |
| 1614 | { |
| 1615 | #ifdef FEAT_RIGHTLEFT |
| 1616 | if (cmdmsg_rl) |
| 1617 | msg_col = Columns; |
| 1618 | else |
| 1619 | #endif |
| 1620 | msg_col = 0; |
| 1621 | msg_putchar(' '); /* delete ':' */ |
| 1622 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1623 | #ifdef FEAT_SEARCH_EXTRA |
| 1624 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1625 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1626 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1627 | redraw_cmdline = TRUE; |
| 1628 | goto returncmd; /* back to cmd mode */ |
| 1629 | } |
| 1630 | goto cmdline_changed; |
| 1631 | |
| 1632 | case K_INS: |
| 1633 | case K_KINS: |
| 1634 | #ifdef FEAT_FKMAP |
| 1635 | /* if Farsi mode set, we are in reverse insert mode - |
| 1636 | Do not change the mode */ |
| 1637 | if (cmd_fkmap) |
| 1638 | beep_flush(); |
| 1639 | else |
| 1640 | #endif |
| 1641 | ccline.overstrike = !ccline.overstrike; |
| 1642 | #ifdef CURSOR_SHAPE |
| 1643 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1644 | #endif |
| 1645 | goto cmdline_not_changed; |
| 1646 | |
| 1647 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1648 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1649 | { |
| 1650 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1651 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1652 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1653 | im_set_active(FALSE); /* Disable input method */ |
| 1654 | #endif |
| 1655 | if (b_im_ptr != NULL) |
| 1656 | { |
| 1657 | if (State & LANGMAP) |
| 1658 | *b_im_ptr = B_IMODE_LMAP; |
| 1659 | else |
| 1660 | *b_im_ptr = B_IMODE_NONE; |
| 1661 | } |
| 1662 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1663 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1664 | else |
| 1665 | { |
| 1666 | /* There are no ":lmap" mappings, toggle IM. When |
| 1667 | * 'imdisable' is set don't try getting the status, it's |
| 1668 | * always off. */ |
| 1669 | if ((p_imdisable && b_im_ptr != NULL) |
| 1670 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1671 | { |
| 1672 | im_set_active(FALSE); /* Disable input method */ |
| 1673 | if (b_im_ptr != NULL) |
| 1674 | *b_im_ptr = B_IMODE_NONE; |
| 1675 | } |
| 1676 | else |
| 1677 | { |
| 1678 | im_set_active(TRUE); /* Enable input method */ |
| 1679 | if (b_im_ptr != NULL) |
| 1680 | *b_im_ptr = B_IMODE_IM; |
| 1681 | } |
| 1682 | } |
| 1683 | #endif |
| 1684 | if (b_im_ptr != NULL) |
| 1685 | { |
| 1686 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1687 | set_iminsert_global(); |
| 1688 | else |
| 1689 | set_imsearch_global(); |
| 1690 | } |
| 1691 | #ifdef CURSOR_SHAPE |
| 1692 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1693 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1694 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1695 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1696 | status_redraw_curbuf(); |
| 1697 | #endif |
| 1698 | goto cmdline_not_changed; |
| 1699 | |
| 1700 | /* case '@': only in very old vi */ |
| 1701 | case Ctrl_U: |
| 1702 | /* delete all characters left of the cursor */ |
| 1703 | j = ccline.cmdpos; |
| 1704 | ccline.cmdlen -= j; |
| 1705 | i = ccline.cmdpos = 0; |
| 1706 | while (i < ccline.cmdlen) |
| 1707 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1708 | /* Truncate at the end, required for multi-byte chars. */ |
| 1709 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1710 | #ifdef FEAT_SEARCH_EXTRA |
| 1711 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 1712 | is_state.search_start = is_state.save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1713 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1714 | redrawcmd(); |
| 1715 | goto cmdline_changed; |
| 1716 | |
| 1717 | #ifdef FEAT_CLIPBOARD |
| 1718 | case Ctrl_Y: |
| 1719 | /* Copy the modeless selection, if there is one. */ |
| 1720 | if (clip_star.state != SELECT_CLEARED) |
| 1721 | { |
| 1722 | if (clip_star.state == SELECT_DONE) |
| 1723 | clip_copy_modeless_selection(TRUE); |
| 1724 | goto cmdline_not_changed; |
| 1725 | } |
| 1726 | break; |
| 1727 | #endif |
| 1728 | |
| 1729 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1730 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1731 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1732 | * ":normal" runs out of characters. */ |
| 1733 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1734 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1735 | goto cmdline_not_changed; |
| 1736 | |
| 1737 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1738 | putting it in history */ |
| 1739 | goto returncmd; /* back to cmd mode */ |
| 1740 | |
| 1741 | case Ctrl_R: /* insert register */ |
| 1742 | #ifdef USE_ON_FLY_SCROLL |
| 1743 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1744 | #endif |
| 1745 | putcmdline('"', TRUE); |
| 1746 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1747 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | if (i == Ctrl_O) |
| 1749 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1750 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1751 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1752 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | --no_mapping; |
| 1754 | #ifdef FEAT_EVAL |
| 1755 | /* |
| 1756 | * Insert the result of an expression. |
| 1757 | * Need to save the current command line, to be able to enter |
| 1758 | * a new one... |
| 1759 | */ |
| 1760 | new_cmdpos = -1; |
| 1761 | if (c == '=') |
| 1762 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1763 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1764 | { |
| 1765 | beep_flush(); |
| 1766 | c = ESC; |
| 1767 | } |
| 1768 | else |
| 1769 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1770 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1771 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1772 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | #endif |
| 1776 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1777 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1778 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1779 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1780 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1781 | /* When there was a serious error abort getting the |
| 1782 | * command line. */ |
| 1783 | if (aborting()) |
| 1784 | { |
| 1785 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1786 | putting it in history */ |
| 1787 | goto returncmd; /* back to cmd mode */ |
| 1788 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1789 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1791 | #ifdef FEAT_EVAL |
| 1792 | if (new_cmdpos >= 0) |
| 1793 | { |
| 1794 | /* set_cmdline_pos() was used */ |
| 1795 | if (new_cmdpos > ccline.cmdlen) |
| 1796 | ccline.cmdpos = ccline.cmdlen; |
| 1797 | else |
| 1798 | ccline.cmdpos = new_cmdpos; |
| 1799 | } |
| 1800 | #endif |
| 1801 | } |
| 1802 | redrawcmd(); |
| 1803 | goto cmdline_changed; |
| 1804 | |
| 1805 | case Ctrl_D: |
| 1806 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1807 | break; /* Use ^D as normal char instead */ |
| 1808 | |
| 1809 | redrawcmd(); |
| 1810 | continue; /* don't do incremental search now */ |
| 1811 | |
| 1812 | case K_RIGHT: |
| 1813 | case K_S_RIGHT: |
| 1814 | case K_C_RIGHT: |
| 1815 | do |
| 1816 | { |
| 1817 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1818 | break; |
| 1819 | i = cmdline_charsize(ccline.cmdpos); |
| 1820 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1821 | break; |
| 1822 | ccline.cmdspos += i; |
| 1823 | #ifdef FEAT_MBYTE |
| 1824 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1825 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1826 | + ccline.cmdpos); |
| 1827 | else |
| 1828 | #endif |
| 1829 | ++ccline.cmdpos; |
| 1830 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1831 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 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] != ' '); |
| 1834 | #ifdef FEAT_MBYTE |
| 1835 | if (has_mbyte) |
| 1836 | set_cmdspos_cursor(); |
| 1837 | #endif |
| 1838 | goto cmdline_not_changed; |
| 1839 | |
| 1840 | case K_LEFT: |
| 1841 | case K_S_LEFT: |
| 1842 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1843 | if (ccline.cmdpos == 0) |
| 1844 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1845 | do |
| 1846 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1847 | --ccline.cmdpos; |
| 1848 | #ifdef FEAT_MBYTE |
| 1849 | if (has_mbyte) /* move to first byte of char */ |
| 1850 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1851 | ccline.cmdbuff + ccline.cmdpos); |
| 1852 | #endif |
| 1853 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1854 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1855 | while (ccline.cmdpos > 0 |
| 1856 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1857 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1858 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1859 | #ifdef FEAT_MBYTE |
| 1860 | if (has_mbyte) |
| 1861 | set_cmdspos_cursor(); |
| 1862 | #endif |
| 1863 | goto cmdline_not_changed; |
| 1864 | |
| 1865 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1866 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1867 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1869 | #ifdef FEAT_GUI_W32 |
| 1870 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1871 | * cancelled. */ |
| 1872 | case K_F4: |
| 1873 | if (mod_mask == MOD_MASK_ALT) |
| 1874 | { |
| 1875 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1876 | goto cmdline_not_changed; |
| 1877 | } |
| 1878 | break; |
| 1879 | #endif |
| 1880 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1881 | #ifdef FEAT_MOUSE |
| 1882 | case K_MIDDLEDRAG: |
| 1883 | case K_MIDDLERELEASE: |
| 1884 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1885 | |
| 1886 | case K_MIDDLEMOUSE: |
| 1887 | # ifdef FEAT_GUI |
| 1888 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1889 | if (!gui.in_use) |
| 1890 | # endif |
| 1891 | if (!mouse_has(MOUSE_COMMAND)) |
| 1892 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1893 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1895 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1896 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1897 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1898 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | redrawcmd(); |
| 1900 | goto cmdline_changed; |
| 1901 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1902 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1903 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1904 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | redrawcmd(); |
| 1906 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1907 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1908 | |
| 1909 | case K_LEFTDRAG: |
| 1910 | case K_LEFTRELEASE: |
| 1911 | case K_RIGHTDRAG: |
| 1912 | case K_RIGHTRELEASE: |
| 1913 | /* Ignore drag and release events when the button-down wasn't |
| 1914 | * seen before. */ |
| 1915 | if (ignore_drag_release) |
| 1916 | goto cmdline_not_changed; |
| 1917 | /* FALLTHROUGH */ |
| 1918 | case K_LEFTMOUSE: |
| 1919 | case K_RIGHTMOUSE: |
| 1920 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1921 | ignore_drag_release = TRUE; |
| 1922 | else |
| 1923 | ignore_drag_release = FALSE; |
| 1924 | # ifdef FEAT_GUI |
| 1925 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1926 | if (!gui.in_use) |
| 1927 | # endif |
| 1928 | if (!mouse_has(MOUSE_COMMAND)) |
| 1929 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1930 | # ifdef FEAT_CLIPBOARD |
| 1931 | if (mouse_row < cmdline_row && clip_star.available) |
| 1932 | { |
| 1933 | int button, is_click, is_drag; |
| 1934 | |
| 1935 | /* |
| 1936 | * Handle modeless selection. |
| 1937 | */ |
| 1938 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1939 | &is_click, &is_drag); |
| 1940 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1941 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1942 | { |
| 1943 | /* Translate shift-left to right button. */ |
| 1944 | button = MOUSE_RIGHT; |
| 1945 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1946 | } |
| 1947 | clip_modeless(button, is_click, is_drag); |
| 1948 | goto cmdline_not_changed; |
| 1949 | } |
| 1950 | # endif |
| 1951 | |
| 1952 | set_cmdspos(); |
| 1953 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1954 | ++ccline.cmdpos) |
| 1955 | { |
| 1956 | i = cmdline_charsize(ccline.cmdpos); |
| 1957 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1958 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1959 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1960 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | if (has_mbyte) |
| 1962 | { |
| 1963 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1964 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1965 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1966 | + ccline.cmdpos) - 1; |
| 1967 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1968 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1969 | ccline.cmdspos += i; |
| 1970 | } |
| 1971 | goto cmdline_not_changed; |
| 1972 | |
| 1973 | /* Mouse scroll wheel: ignored here */ |
| 1974 | case K_MOUSEDOWN: |
| 1975 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1976 | case K_MOUSELEFT: |
| 1977 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1978 | /* Alternate buttons ignored here */ |
| 1979 | case K_X1MOUSE: |
| 1980 | case K_X1DRAG: |
| 1981 | case K_X1RELEASE: |
| 1982 | case K_X2MOUSE: |
| 1983 | case K_X2DRAG: |
| 1984 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1985 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1986 | goto cmdline_not_changed; |
| 1987 | |
| 1988 | #endif /* FEAT_MOUSE */ |
| 1989 | |
| 1990 | #ifdef FEAT_GUI |
| 1991 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1992 | case K_LEFTRELEASE_NM: |
| 1993 | goto cmdline_not_changed; |
| 1994 | |
| 1995 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1996 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1997 | { |
| 1998 | gui_do_scroll(); |
| 1999 | redrawcmd(); |
| 2000 | } |
| 2001 | goto cmdline_not_changed; |
| 2002 | |
| 2003 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2004 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2005 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 2006 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2007 | redrawcmd(); |
| 2008 | } |
| 2009 | goto cmdline_not_changed; |
| 2010 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 2011 | #ifdef FEAT_GUI_TABLINE |
| 2012 | case K_TABLINE: |
| 2013 | case K_TABMENU: |
| 2014 | /* Don't want to change any tabs here. Make sure the same tab |
| 2015 | * is still selected. */ |
| 2016 | if (gui_use_tabline()) |
| 2017 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 2018 | goto cmdline_not_changed; |
| 2019 | #endif |
| 2020 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2021 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 2022 | goto cmdline_not_changed; |
| 2023 | |
| 2024 | case Ctrl_B: /* begin of command line */ |
| 2025 | case K_HOME: |
| 2026 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2027 | case K_S_HOME: |
| 2028 | case K_C_HOME: |
| 2029 | ccline.cmdpos = 0; |
| 2030 | set_cmdspos(); |
| 2031 | goto cmdline_not_changed; |
| 2032 | |
| 2033 | case Ctrl_E: /* end of command line */ |
| 2034 | case K_END: |
| 2035 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | case K_S_END: |
| 2037 | case K_C_END: |
| 2038 | ccline.cmdpos = ccline.cmdlen; |
| 2039 | set_cmdspos_cursor(); |
| 2040 | goto cmdline_not_changed; |
| 2041 | |
| 2042 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2043 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | break; |
| 2045 | goto cmdline_changed; |
| 2046 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2047 | case Ctrl_L: |
| 2048 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2049 | if (may_add_char_to_search(firstc, &c, &is_state) == OK) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2050 | goto cmdline_not_changed; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2051 | #endif |
| 2052 | |
| 2053 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2054 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2055 | break; |
| 2056 | goto cmdline_changed; |
| 2057 | |
| 2058 | case Ctrl_N: /* next match */ |
| 2059 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 2060 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 2062 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 2063 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2065 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2066 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2067 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 2068 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | case K_UP: |
| 2070 | case K_DOWN: |
| 2071 | case K_S_UP: |
| 2072 | case K_S_DOWN: |
| 2073 | case K_PAGEUP: |
| 2074 | case K_KPAGEUP: |
| 2075 | case K_PAGEDOWN: |
| 2076 | case K_KPAGEDOWN: |
| 2077 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 2078 | goto cmdline_not_changed; |
| 2079 | |
| 2080 | i = hiscnt; |
| 2081 | |
| 2082 | /* save current command string so it can be restored later */ |
| 2083 | if (lookfor == NULL) |
| 2084 | { |
| 2085 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 2086 | goto cmdline_not_changed; |
| 2087 | lookfor[ccline.cmdpos] = NUL; |
| 2088 | } |
| 2089 | |
| 2090 | j = (int)STRLEN(lookfor); |
| 2091 | for (;;) |
| 2092 | { |
| 2093 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2094 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2095 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2096 | { |
| 2097 | if (hiscnt == hislen) /* first time */ |
| 2098 | hiscnt = hisidx[histype]; |
| 2099 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 2100 | hiscnt = hislen - 1; |
| 2101 | else if (hiscnt != hisidx[histype] + 1) |
| 2102 | --hiscnt; |
| 2103 | else /* at top of list */ |
| 2104 | { |
| 2105 | hiscnt = i; |
| 2106 | break; |
| 2107 | } |
| 2108 | } |
| 2109 | else /* one step forwards */ |
| 2110 | { |
| 2111 | /* on last entry, clear the line */ |
| 2112 | if (hiscnt == hisidx[histype]) |
| 2113 | { |
| 2114 | hiscnt = hislen; |
| 2115 | break; |
| 2116 | } |
| 2117 | |
| 2118 | /* not on a history line, nothing to do */ |
| 2119 | if (hiscnt == hislen) |
| 2120 | break; |
| 2121 | if (hiscnt == hislen - 1) /* wrap around */ |
| 2122 | hiscnt = 0; |
| 2123 | else |
| 2124 | ++hiscnt; |
| 2125 | } |
| 2126 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 2127 | { |
| 2128 | hiscnt = i; |
| 2129 | break; |
| 2130 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 2131 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 2132 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2133 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 2134 | lookfor, (size_t)j) == 0) |
| 2135 | break; |
| 2136 | } |
| 2137 | |
| 2138 | if (hiscnt != i) /* jumped to other entry */ |
| 2139 | { |
| 2140 | char_u *p; |
| 2141 | int len; |
| 2142 | int old_firstc; |
| 2143 | |
| 2144 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2145 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | if (hiscnt == hislen) |
| 2147 | p = lookfor; /* back to the old one */ |
| 2148 | else |
| 2149 | p = history[histype][hiscnt].hisstr; |
| 2150 | |
| 2151 | if (histype == HIST_SEARCH |
| 2152 | && p != lookfor |
| 2153 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 2154 | { |
| 2155 | /* Correct for the separator character used when |
| 2156 | * adding the history entry vs the one used now. |
| 2157 | * First loop: count length. |
| 2158 | * Second loop: copy the characters. */ |
| 2159 | for (i = 0; i <= 1; ++i) |
| 2160 | { |
| 2161 | len = 0; |
| 2162 | for (j = 0; p[j] != NUL; ++j) |
| 2163 | { |
| 2164 | /* Replace old sep with new sep, unless it is |
| 2165 | * escaped. */ |
| 2166 | if (p[j] == old_firstc |
| 2167 | && (j == 0 || p[j - 1] != '\\')) |
| 2168 | { |
| 2169 | if (i > 0) |
| 2170 | ccline.cmdbuff[len] = firstc; |
| 2171 | } |
| 2172 | else |
| 2173 | { |
| 2174 | /* Escape new sep, unless it is already |
| 2175 | * escaped. */ |
| 2176 | if (p[j] == firstc |
| 2177 | && (j == 0 || p[j - 1] != '\\')) |
| 2178 | { |
| 2179 | if (i > 0) |
| 2180 | ccline.cmdbuff[len] = '\\'; |
| 2181 | ++len; |
| 2182 | } |
| 2183 | if (i > 0) |
| 2184 | ccline.cmdbuff[len] = p[j]; |
| 2185 | } |
| 2186 | ++len; |
| 2187 | } |
| 2188 | if (i == 0) |
| 2189 | { |
| 2190 | alloc_cmdbuff(len); |
| 2191 | if (ccline.cmdbuff == NULL) |
| 2192 | goto returncmd; |
| 2193 | } |
| 2194 | } |
| 2195 | ccline.cmdbuff[len] = NUL; |
| 2196 | } |
| 2197 | else |
| 2198 | { |
| 2199 | alloc_cmdbuff((int)STRLEN(p)); |
| 2200 | if (ccline.cmdbuff == NULL) |
| 2201 | goto returncmd; |
| 2202 | STRCPY(ccline.cmdbuff, p); |
| 2203 | } |
| 2204 | |
| 2205 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 2206 | redrawcmd(); |
| 2207 | goto cmdline_changed; |
| 2208 | } |
| 2209 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2210 | #endif |
| 2211 | goto cmdline_not_changed; |
| 2212 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2213 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 2214 | case Ctrl_G: /* next match */ |
| 2215 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2216 | if (may_adjust_incsearch_highlighting( |
| 2217 | firstc, count, &is_state, c) == FAIL) |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2218 | goto cmdline_not_changed; |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 2219 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2220 | #endif |
| 2221 | |
| 2222 | case Ctrl_V: |
| 2223 | case Ctrl_Q: |
| 2224 | #ifdef FEAT_MOUSE |
| 2225 | ignore_drag_release = TRUE; |
| 2226 | #endif |
| 2227 | putcmdline('^', TRUE); |
| 2228 | c = get_literal(); /* get next (two) character(s) */ |
| 2229 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2230 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | #ifdef FEAT_MBYTE |
| 2232 | /* may need to remove ^ when composing char was typed */ |
| 2233 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 2234 | { |
| 2235 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2236 | msg_putchar(' '); |
| 2237 | cursorcmd(); |
| 2238 | } |
| 2239 | #endif |
| 2240 | break; |
| 2241 | |
| 2242 | #ifdef FEAT_DIGRAPHS |
| 2243 | case Ctrl_K: |
| 2244 | #ifdef FEAT_MOUSE |
| 2245 | ignore_drag_release = TRUE; |
| 2246 | #endif |
| 2247 | putcmdline('?', TRUE); |
| 2248 | #ifdef USE_ON_FLY_SCROLL |
| 2249 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 2250 | #endif |
| 2251 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 2252 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2253 | if (c != NUL) |
| 2254 | break; |
| 2255 | |
| 2256 | redrawcmd(); |
| 2257 | goto cmdline_not_changed; |
| 2258 | #endif /* FEAT_DIGRAPHS */ |
| 2259 | |
| 2260 | #ifdef FEAT_RIGHTLEFT |
| 2261 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 2262 | if (!p_ari) |
| 2263 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2264 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2265 | if (p_altkeymap) |
| 2266 | { |
| 2267 | cmd_fkmap = !cmd_fkmap; |
| 2268 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 2269 | ccline.overstrike = FALSE; |
| 2270 | } |
| 2271 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 2272 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2273 | cmd_hkmap = !cmd_hkmap; |
| 2274 | goto cmdline_not_changed; |
| 2275 | #endif |
| 2276 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2277 | case K_PS: |
| 2278 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 2279 | goto cmdline_changed; |
| 2280 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | default: |
| 2282 | #ifdef UNIX |
| 2283 | if (c == intr_char) |
| 2284 | { |
| 2285 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 2286 | putting it in history */ |
| 2287 | goto returncmd; /* back to Normal mode */ |
| 2288 | } |
| 2289 | #endif |
| 2290 | /* |
| 2291 | * Normal character with no special meaning. Just set mod_mask |
| 2292 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 2293 | * the string <S-Space>. This should only happen after ^V. |
| 2294 | */ |
| 2295 | if (!IS_SPECIAL(c)) |
| 2296 | mod_mask = 0x0; |
| 2297 | break; |
| 2298 | } |
| 2299 | /* |
| 2300 | * End of switch on command line character. |
| 2301 | * We come here if we have a normal character. |
| 2302 | */ |
| 2303 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2304 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2305 | #ifdef FEAT_MBYTE |
| 2306 | /* Add ABBR_OFF for characters above 0x100, this is |
| 2307 | * what check_abbr() expects. */ |
| 2308 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 2309 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 2310 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2311 | goto cmdline_changed; |
| 2312 | |
| 2313 | /* |
| 2314 | * put the character in the command line |
| 2315 | */ |
| 2316 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 2317 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 2318 | else |
| 2319 | { |
| 2320 | #ifdef FEAT_MBYTE |
| 2321 | if (has_mbyte) |
| 2322 | { |
| 2323 | j = (*mb_char2bytes)(c, IObuff); |
| 2324 | IObuff[j] = NUL; /* exclude composing chars */ |
| 2325 | put_on_cmdline(IObuff, j, TRUE); |
| 2326 | } |
| 2327 | else |
| 2328 | #endif |
| 2329 | { |
| 2330 | IObuff[0] = c; |
| 2331 | put_on_cmdline(IObuff, 1, TRUE); |
| 2332 | } |
| 2333 | } |
| 2334 | goto cmdline_changed; |
| 2335 | |
| 2336 | /* |
| 2337 | * This part implements incremental searches for "/" and "?" |
| 2338 | * Jump to cmdline_not_changed when a character has been read but the command |
| 2339 | * line did not change. Then we only search and redraw if something changed in |
| 2340 | * the past. |
| 2341 | * Jump to cmdline_changed when the command line did change. |
| 2342 | * (Sorry for the goto's, I know it is ugly). |
| 2343 | */ |
| 2344 | cmdline_not_changed: |
| 2345 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2346 | if (!is_state.incsearch_postponed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | continue; |
| 2348 | #endif |
| 2349 | |
| 2350 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2351 | /* Trigger CmdlineChanged autocommands. */ |
| 2352 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 2353 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2354 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 0ee81cb | 2018-08-10 22:07:32 +0200 | [diff] [blame] | 2355 | may_do_incsearch_highlighting(firstc, count, &is_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2356 | #endif |
| 2357 | |
| 2358 | #ifdef FEAT_RIGHTLEFT |
| 2359 | if (cmdmsg_rl |
| 2360 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2361 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2362 | # endif |
| 2363 | ) |
| 2364 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2365 | * right-left typing. Not efficient, but it works. |
| 2366 | * Do it only when there are no characters left to read |
| 2367 | * to avoid useless intermediate redraws. */ |
| 2368 | if (vpeekc() == NUL) |
| 2369 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2370 | #endif |
| 2371 | } |
| 2372 | |
| 2373 | returncmd: |
| 2374 | |
| 2375 | #ifdef FEAT_RIGHTLEFT |
| 2376 | cmdmsg_rl = FALSE; |
| 2377 | #endif |
| 2378 | |
| 2379 | #ifdef FEAT_FKMAP |
| 2380 | cmd_fkmap = 0; |
| 2381 | #endif |
| 2382 | |
| 2383 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2384 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2385 | |
| 2386 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | c7f08b7 | 2018-08-12 17:39:14 +0200 | [diff] [blame] | 2387 | finish_incsearch_highlighting(gotesc, &is_state, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2388 | #endif |
| 2389 | |
| 2390 | if (ccline.cmdbuff != NULL) |
| 2391 | { |
| 2392 | /* |
| 2393 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2394 | */ |
| 2395 | #ifdef FEAT_CMDHIST |
| 2396 | if (ccline.cmdlen && firstc != NUL |
| 2397 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2398 | { |
| 2399 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2400 | histype == HIST_SEARCH ? firstc : NUL); |
| 2401 | if (firstc == ':') |
| 2402 | { |
| 2403 | vim_free(new_last_cmdline); |
| 2404 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2405 | } |
| 2406 | } |
| 2407 | #endif |
| 2408 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2409 | if (gotesc) |
| 2410 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | /* |
| 2414 | * If the screen was shifted up, redraw the whole screen (later). |
| 2415 | * If the line is too long, clear it, so ruler and shown command do |
| 2416 | * not get printed in the middle of it. |
| 2417 | */ |
| 2418 | msg_check(); |
| 2419 | msg_scroll = save_msg_scroll; |
| 2420 | redir_off = FALSE; |
| 2421 | |
| 2422 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2423 | if (some_key_typed) |
| 2424 | need_wait_return = FALSE; |
| 2425 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2426 | /* Trigger CmdlineLeave autocommands. */ |
| 2427 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2428 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2430 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2432 | im_save_status(b_im_ptr); |
| 2433 | im_set_active(FALSE); |
| 2434 | #endif |
| 2435 | #ifdef FEAT_MOUSE |
| 2436 | setmouse(); |
| 2437 | #endif |
| 2438 | #ifdef CURSOR_SHAPE |
| 2439 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2440 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2441 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2443 | { |
| 2444 | char_u *p = ccline.cmdbuff; |
| 2445 | |
| 2446 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2447 | ccline.cmdbuff = NULL; |
| 2448 | return p; |
| 2449 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2453 | /* |
| 2454 | * Get a command line with a prompt. |
| 2455 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2456 | * f_input() when evaluating an expression from CTRL-R =). |
| 2457 | * Returns the command line in allocated memory, or NULL. |
| 2458 | */ |
| 2459 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2460 | getcmdline_prompt( |
| 2461 | int firstc, |
| 2462 | char_u *prompt, /* command line prompt */ |
| 2463 | int attr, /* attributes for prompt */ |
| 2464 | int xp_context, /* type of expansion */ |
| 2465 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2466 | { |
| 2467 | char_u *s; |
| 2468 | struct cmdline_info save_ccline; |
| 2469 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2470 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2472 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | ccline.cmdprompt = prompt; |
| 2474 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2475 | # ifdef FEAT_EVAL |
| 2476 | ccline.xp_context = xp_context; |
| 2477 | ccline.xp_arg = xp_arg; |
| 2478 | ccline.input_fn = (firstc == '@'); |
| 2479 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2480 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2481 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2482 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2483 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2484 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2485 | * But only if called recursively and the commandline is therefore being |
| 2486 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2487 | * so we need its modified msg_col left intact. */ |
| 2488 | if (ccline.cmdbuff != NULL) |
| 2489 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | |
| 2491 | return s; |
| 2492 | } |
| 2493 | #endif |
| 2494 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2495 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2496 | * Return TRUE when the text must not be changed and we can't switch to |
| 2497 | * another window or buffer. Used when editing the command line, evaluating |
| 2498 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2499 | */ |
| 2500 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2501 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2502 | { |
| 2503 | #ifdef FEAT_CMDWIN |
| 2504 | if (cmdwin_type != 0) |
| 2505 | return TRUE; |
| 2506 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2507 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * Give an error message for a command that isn't allowed while the cmdline |
| 2512 | * window is open or editing the cmdline in another way. |
| 2513 | */ |
| 2514 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2515 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2516 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2517 | EMSG(_(get_text_locked_msg())); |
| 2518 | } |
| 2519 | |
| 2520 | char_u * |
| 2521 | get_text_locked_msg(void) |
| 2522 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2523 | #ifdef FEAT_CMDWIN |
| 2524 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2525 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2526 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2527 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2530 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2531 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2532 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2533 | */ |
| 2534 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2535 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2536 | { |
| 2537 | if (curbuf_lock > 0) |
| 2538 | { |
| 2539 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2540 | return TRUE; |
| 2541 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2542 | return allbuf_locked(); |
| 2543 | } |
| 2544 | |
| 2545 | /* |
| 2546 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2547 | * message. |
| 2548 | */ |
| 2549 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2550 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2551 | { |
| 2552 | if (allbuf_lock > 0) |
| 2553 | { |
| 2554 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2555 | return TRUE; |
| 2556 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2557 | return FALSE; |
| 2558 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2559 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2560 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2561 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2562 | { |
| 2563 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2564 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2565 | return 1; |
| 2566 | #endif |
| 2567 | return ptr2cells(ccline.cmdbuff + idx); |
| 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | * Compute the offset of the cursor on the command line for the prompt and |
| 2572 | * indent. |
| 2573 | */ |
| 2574 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2575 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2577 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2578 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2579 | else |
| 2580 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2581 | } |
| 2582 | |
| 2583 | /* |
| 2584 | * Compute the screen position for the cursor on the command line. |
| 2585 | */ |
| 2586 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2587 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2588 | { |
| 2589 | int i, m, c; |
| 2590 | |
| 2591 | set_cmdspos(); |
| 2592 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2593 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2595 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2596 | m = MAXCOL; |
| 2597 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2598 | else |
| 2599 | m = MAXCOL; |
| 2600 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2601 | { |
| 2602 | c = cmdline_charsize(i); |
| 2603 | #ifdef FEAT_MBYTE |
| 2604 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2605 | if (has_mbyte) |
| 2606 | correct_cmdspos(i, c); |
| 2607 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2608 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2609 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2610 | if ((ccline.cmdspos += c) >= m) |
| 2611 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2612 | ccline.cmdspos -= c; |
| 2613 | break; |
| 2614 | } |
| 2615 | #ifdef FEAT_MBYTE |
| 2616 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2617 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | #endif |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | #ifdef FEAT_MBYTE |
| 2623 | /* |
| 2624 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2625 | * character that doesn't fit, so that a ">" must be displayed. |
| 2626 | */ |
| 2627 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2628 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2630 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2631 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2632 | && ccline.cmdspos % Columns + cells > Columns) |
| 2633 | ccline.cmdspos++; |
| 2634 | } |
| 2635 | #endif |
| 2636 | |
| 2637 | /* |
| 2638 | * Get an Ex command line for the ":" command. |
| 2639 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2641 | getexline( |
| 2642 | int c, /* normally ':', NUL for ":append" */ |
| 2643 | void *cookie UNUSED, |
| 2644 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2645 | { |
| 2646 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2647 | if (exec_from_reg && vpeekc() == ':') |
| 2648 | (void)vgetc(); |
| 2649 | return getcmdline(c, 1L, indent); |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * Get an Ex command line for Ex mode. |
| 2654 | * In Ex mode we only use the OS supplied line editing features and no |
| 2655 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2656 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2657 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2659 | getexmodeline( |
| 2660 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2661 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2662 | void *cookie UNUSED, |
| 2663 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2665 | garray_T line_ga; |
| 2666 | char_u *pend; |
| 2667 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2668 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2669 | int escaped = FALSE; /* CTRL-V typed */ |
| 2670 | int vcol = 0; |
| 2671 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2672 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2673 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2676 | * confuses the system function that computes tabstops. */ |
| 2677 | cursor_on(); |
| 2678 | |
| 2679 | /* always start in column 0; write a newline if necessary */ |
| 2680 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2681 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2683 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2684 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2685 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2686 | if (p_prompt) |
| 2687 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2688 | while (indent-- > 0) |
| 2689 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | ga_init2(&line_ga, 1, 30); |
| 2694 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2695 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2696 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2697 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2698 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2699 | while (indent >= 8) |
| 2700 | { |
| 2701 | ga_append(&line_ga, TAB); |
| 2702 | msg_puts((char_u *)" "); |
| 2703 | indent -= 8; |
| 2704 | } |
| 2705 | while (indent-- > 0) |
| 2706 | { |
| 2707 | ga_append(&line_ga, ' '); |
| 2708 | msg_putchar(' '); |
| 2709 | } |
| 2710 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2711 | ++no_mapping; |
| 2712 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2713 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | /* |
| 2715 | * Get the line, one character at a time. |
| 2716 | */ |
| 2717 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2718 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2720 | long sw; |
| 2721 | char_u *s; |
| 2722 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2724 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2726 | /* |
| 2727 | * Get one character at a time. |
| 2728 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2729 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2730 | |
| 2731 | /* Check for a ":normal" command and no more characters left. */ |
| 2732 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2733 | c1 = '\n'; |
| 2734 | else |
| 2735 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | |
| 2737 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2738 | * Handle line editing. |
| 2739 | * Previously this was left to the system, putting the terminal in |
| 2740 | * 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] | 2741 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2742 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2743 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2744 | msg_putchar('\n'); |
| 2745 | break; |
| 2746 | } |
| 2747 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2748 | if (c1 == K_PS) |
| 2749 | { |
| 2750 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2751 | goto redraw; |
| 2752 | } |
| 2753 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2754 | if (!escaped) |
| 2755 | { |
| 2756 | /* CR typed means "enter", which is NL */ |
| 2757 | if (c1 == '\r') |
| 2758 | c1 = '\n'; |
| 2759 | |
| 2760 | if (c1 == BS || c1 == K_BS |
| 2761 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2762 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2763 | if (line_ga.ga_len > 0) |
| 2764 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2765 | #ifdef FEAT_MBYTE |
| 2766 | if (has_mbyte) |
| 2767 | { |
| 2768 | p = (char_u *)line_ga.ga_data; |
| 2769 | p[line_ga.ga_len] = NUL; |
| 2770 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2771 | line_ga.ga_len -= len; |
| 2772 | } |
| 2773 | else |
| 2774 | #endif |
| 2775 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2776 | goto redraw; |
| 2777 | } |
| 2778 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2781 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2783 | msg_col = startcol; |
| 2784 | msg_clr_eos(); |
| 2785 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2786 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | if (c1 == Ctrl_T) |
| 2790 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2791 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2792 | p = (char_u *)line_ga.ga_data; |
| 2793 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2794 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2795 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2796 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2797 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2798 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2799 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2800 | p = (char_u *)line_ga.ga_data; |
| 2801 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2802 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2803 | *s = ' '; |
| 2804 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2805 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2806 | redraw: |
| 2807 | /* redraw the line */ |
| 2808 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2809 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2810 | p = (char_u *)line_ga.ga_data; |
| 2811 | p[line_ga.ga_len] = NUL; |
| 2812 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2813 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2814 | if (*p == TAB) |
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 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2817 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2818 | msg_putchar(' '); |
| 2819 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2820 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2822 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2824 | len = MB_PTR2LEN(p); |
| 2825 | msg_outtrans_len(p, len); |
| 2826 | vcol += ptr2cells(p); |
| 2827 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2830 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2831 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2832 | continue; |
| 2833 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2834 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2835 | if (c1 == Ctrl_D) |
| 2836 | { |
| 2837 | /* Delete one shiftwidth. */ |
| 2838 | p = (char_u *)line_ga.ga_data; |
| 2839 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2840 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2841 | if (prev_char == '^') |
| 2842 | ex_keep_indent = TRUE; |
| 2843 | indent = 0; |
| 2844 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | } |
| 2846 | else |
| 2847 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2848 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2849 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2850 | if (indent > 0) |
| 2851 | { |
| 2852 | --indent; |
| 2853 | indent -= indent % get_sw_value(curbuf); |
| 2854 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2856 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2857 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2858 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2859 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2860 | --line_ga.ga_len; |
| 2861 | } |
| 2862 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2863 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2864 | |
| 2865 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2866 | { |
| 2867 | escaped = TRUE; |
| 2868 | continue; |
| 2869 | } |
| 2870 | |
| 2871 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2872 | if (IS_SPECIAL(c1)) |
| 2873 | continue; |
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 | |
| 2876 | if (IS_SPECIAL(c1)) |
| 2877 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2878 | #ifdef FEAT_MBYTE |
| 2879 | if (has_mbyte) |
| 2880 | len = (*mb_char2bytes)(c1, |
| 2881 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2882 | else |
| 2883 | #endif |
| 2884 | { |
| 2885 | len = 1; |
| 2886 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2887 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2888 | if (c1 == '\n') |
| 2889 | msg_putchar('\n'); |
| 2890 | else if (c1 == TAB) |
| 2891 | { |
| 2892 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2893 | do |
| 2894 | { |
| 2895 | msg_putchar(' '); |
| 2896 | } while (++vcol % 8); |
| 2897 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2899 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2900 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2901 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2902 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2903 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2904 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2905 | escaped = FALSE; |
| 2906 | |
| 2907 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2908 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2909 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2910 | /* We are done when a NL is entered, but not when it comes after an |
| 2911 | * odd number of backslashes, that results in a NUL. */ |
| 2912 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2913 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2914 | int bcount = 0; |
| 2915 | |
| 2916 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2917 | ++bcount; |
| 2918 | |
| 2919 | if (bcount > 0) |
| 2920 | { |
| 2921 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2922 | * "\NL", etc. */ |
| 2923 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2924 | pend -= (bcount + 1) / 2; |
| 2925 | pend[-1] = '\n'; |
| 2926 | } |
| 2927 | |
| 2928 | if ((bcount & 1) == 0) |
| 2929 | { |
| 2930 | --line_ga.ga_len; |
| 2931 | --pend; |
| 2932 | *pend = NUL; |
| 2933 | break; |
| 2934 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2938 | --no_mapping; |
| 2939 | --allow_keys; |
| 2940 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2941 | /* make following messages go to the next line */ |
| 2942 | msg_didout = FALSE; |
| 2943 | msg_col = 0; |
| 2944 | if (msg_row < Rows - 1) |
| 2945 | ++msg_row; |
| 2946 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2947 | |
| 2948 | if (got_int) |
| 2949 | ga_clear(&line_ga); |
| 2950 | |
| 2951 | return (char_u *)line_ga.ga_data; |
| 2952 | } |
| 2953 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2954 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2955 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2956 | /* |
| 2957 | * Return TRUE if ccline.overstrike is on. |
| 2958 | */ |
| 2959 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2960 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2961 | { |
| 2962 | return ccline.overstrike; |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2967 | */ |
| 2968 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2969 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | { |
| 2971 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2972 | } |
| 2973 | #endif |
| 2974 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2975 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | /* |
| 2977 | * Return the virtual column number at the current cursor position. |
| 2978 | * This is used by the IM code to obtain the start of the preedit string. |
| 2979 | */ |
| 2980 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2981 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2982 | { |
| 2983 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2984 | return MAXCOL; |
| 2985 | |
| 2986 | # ifdef FEAT_MBYTE |
| 2987 | if (has_mbyte) |
| 2988 | { |
| 2989 | colnr_T col; |
| 2990 | int i = 0; |
| 2991 | |
| 2992 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2993 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | |
| 2995 | return col; |
| 2996 | } |
| 2997 | else |
| 2998 | # endif |
| 2999 | return ccline.cmdpos; |
| 3000 | } |
| 3001 | #endif |
| 3002 | |
| 3003 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3004 | /* |
| 3005 | * If part of the command line is an IM preedit string, redraw it with |
| 3006 | * IM feedback attributes. The cursor position is restored after drawing. |
| 3007 | */ |
| 3008 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3009 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3010 | { |
| 3011 | if ((State & CMDLINE) |
| 3012 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 3013 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3014 | && !p_imdisable |
| 3015 | && im_is_preediting()) |
| 3016 | { |
| 3017 | int cmdpos = 0; |
| 3018 | int cmdspos; |
| 3019 | int old_row; |
| 3020 | int old_col; |
| 3021 | colnr_T col; |
| 3022 | |
| 3023 | old_row = msg_row; |
| 3024 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3025 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | |
| 3027 | # ifdef FEAT_MBYTE |
| 3028 | if (has_mbyte) |
| 3029 | { |
| 3030 | for (col = 0; col < preedit_start_col |
| 3031 | && cmdpos < ccline.cmdlen; ++col) |
| 3032 | { |
| 3033 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3034 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | else |
| 3038 | # endif |
| 3039 | { |
| 3040 | cmdspos += preedit_start_col; |
| 3041 | cmdpos += preedit_start_col; |
| 3042 | } |
| 3043 | |
| 3044 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 3045 | msg_col = cmdspos % (int)Columns; |
| 3046 | if (msg_row >= Rows) |
| 3047 | msg_row = Rows - 1; |
| 3048 | |
| 3049 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 3050 | { |
| 3051 | int char_len; |
| 3052 | int char_attr; |
| 3053 | |
| 3054 | char_attr = im_get_feedback_attr(col); |
| 3055 | if (char_attr < 0) |
| 3056 | break; /* end of preedit string */ |
| 3057 | |
| 3058 | # ifdef FEAT_MBYTE |
| 3059 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3060 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3061 | else |
| 3062 | # endif |
| 3063 | char_len = 1; |
| 3064 | |
| 3065 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 3066 | cmdpos += char_len; |
| 3067 | } |
| 3068 | |
| 3069 | msg_row = old_row; |
| 3070 | msg_col = old_col; |
| 3071 | } |
| 3072 | } |
| 3073 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 3074 | |
| 3075 | /* |
| 3076 | * Allocate a new command line buffer. |
| 3077 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 3078 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 3079 | */ |
| 3080 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3081 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | { |
| 3083 | /* |
| 3084 | * give some extra space to avoid having to allocate all the time |
| 3085 | */ |
| 3086 | if (len < 80) |
| 3087 | len = 100; |
| 3088 | else |
| 3089 | len += 20; |
| 3090 | |
| 3091 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 3092 | ccline.cmdbufflen = len; |
| 3093 | } |
| 3094 | |
| 3095 | /* |
| 3096 | * Re-allocate the command line to length len + something extra. |
| 3097 | * return FAIL for failure, OK otherwise |
| 3098 | */ |
| 3099 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3100 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3101 | { |
| 3102 | char_u *p; |
| 3103 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3104 | if (len < ccline.cmdbufflen) |
| 3105 | return OK; /* no need to resize */ |
| 3106 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3107 | p = ccline.cmdbuff; |
| 3108 | alloc_cmdbuff(len); /* will get some more */ |
| 3109 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 3110 | { |
| 3111 | ccline.cmdbuff = p; /* keep the old one */ |
| 3112 | return FAIL; |
| 3113 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 3114 | /* There isn't always a NUL after the command, but it may need to be |
| 3115 | * there, thus copy up to the NUL and add a NUL. */ |
| 3116 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 3117 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3118 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3119 | |
| 3120 | if (ccline.xpc != NULL |
| 3121 | && ccline.xpc->xp_pattern != NULL |
| 3122 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 3123 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 3124 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 3125 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3126 | |
| 3127 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 3128 | * to point into the newly allocated memory. */ |
| 3129 | if (i >= 0 && i <= ccline.cmdlen) |
| 3130 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 3131 | } |
| 3132 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3133 | return OK; |
| 3134 | } |
| 3135 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3136 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 3137 | static char_u *arshape_buf = NULL; |
| 3138 | |
| 3139 | # if defined(EXITFREE) || defined(PROTO) |
| 3140 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3141 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3142 | { |
| 3143 | vim_free(arshape_buf); |
| 3144 | } |
| 3145 | # endif |
| 3146 | #endif |
| 3147 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3148 | /* |
| 3149 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 3150 | * when cmdline_star is TRUE. |
| 3151 | */ |
| 3152 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3153 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3154 | { |
| 3155 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3156 | int i; |
| 3157 | |
| 3158 | if (cmdline_star > 0) |
| 3159 | for (i = 0; i < len; ++i) |
| 3160 | { |
| 3161 | msg_putchar('*'); |
| 3162 | # ifdef FEAT_MBYTE |
| 3163 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3164 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3165 | # endif |
| 3166 | } |
| 3167 | else |
| 3168 | #endif |
| 3169 | #ifdef FEAT_ARABIC |
| 3170 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 3171 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3172 | static int buflen = 0; |
| 3173 | char_u *p; |
| 3174 | int j; |
| 3175 | int newlen = 0; |
| 3176 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3177 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3178 | int prev_c = 0; |
| 3179 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3180 | int u8c; |
| 3181 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3182 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3183 | |
| 3184 | /* |
| 3185 | * Do arabic shaping into a temporary buffer. This is very |
| 3186 | * inefficient! |
| 3187 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3188 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3189 | { |
| 3190 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 3191 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3192 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3193 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3194 | arshape_buf = alloc(buflen); |
| 3195 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3196 | return; /* out of memory */ |
| 3197 | } |
| 3198 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 3199 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 3200 | { |
| 3201 | /* Prepend a space to draw the leading composing char on. */ |
| 3202 | arshape_buf[0] = ' '; |
| 3203 | newlen = 1; |
| 3204 | } |
| 3205 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3206 | for (j = start; j < start + len; j += mb_l) |
| 3207 | { |
| 3208 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3209 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3210 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3211 | if (ARABIC_CHAR(u8c)) |
| 3212 | { |
| 3213 | /* Do Arabic shaping. */ |
| 3214 | if (cmdmsg_rl) |
| 3215 | { |
| 3216 | /* displaying from right to left */ |
| 3217 | pc = prev_c; |
| 3218 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3219 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3220 | if (j + mb_l >= start + len) |
| 3221 | nc = NUL; |
| 3222 | else |
| 3223 | nc = utf_ptr2char(p + mb_l); |
| 3224 | } |
| 3225 | else |
| 3226 | { |
| 3227 | /* displaying from left to right */ |
| 3228 | if (j + mb_l >= start + len) |
| 3229 | pc = NUL; |
| 3230 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3231 | { |
| 3232 | int pcc[MAX_MCO]; |
| 3233 | |
| 3234 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3235 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3236 | pc1 = pcc[0]; |
| 3237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3238 | nc = prev_c; |
| 3239 | } |
| 3240 | prev_c = u8c; |
| 3241 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3242 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3243 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3244 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3245 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3246 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3247 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 3248 | if (u8cc[1] != 0) |
| 3249 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3250 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | } |
| 3252 | } |
| 3253 | else |
| 3254 | { |
| 3255 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3256 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3257 | newlen += mb_l; |
| 3258 | } |
| 3259 | } |
| 3260 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3261 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3262 | } |
| 3263 | else |
| 3264 | #endif |
| 3265 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3266 | } |
| 3267 | |
| 3268 | /* |
| 3269 | * Put a character on the command line. Shifts the following text to the |
| 3270 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3271 | * "c" must be printable (fit in one display cell)! |
| 3272 | */ |
| 3273 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3274 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3275 | { |
| 3276 | if (cmd_silent) |
| 3277 | return; |
| 3278 | msg_no_more = TRUE; |
| 3279 | msg_putchar(c); |
| 3280 | if (shift) |
| 3281 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3282 | msg_no_more = FALSE; |
| 3283 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3284 | extra_char = c; |
| 3285 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | * Undo a putcmdline(c, FALSE). |
| 3290 | */ |
| 3291 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3292 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | { |
| 3294 | if (cmd_silent) |
| 3295 | return; |
| 3296 | msg_no_more = TRUE; |
| 3297 | if (ccline.cmdlen == ccline.cmdpos) |
| 3298 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3299 | #ifdef FEAT_MBYTE |
| 3300 | else if (has_mbyte) |
| 3301 | draw_cmdline(ccline.cmdpos, |
| 3302 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3303 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3304 | else |
| 3305 | draw_cmdline(ccline.cmdpos, 1); |
| 3306 | msg_no_more = FALSE; |
| 3307 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3308 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * Put the given string, of the given length, onto the command line. |
| 3313 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3314 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3315 | * part will be redrawn, otherwise it will not. If this function is called |
| 3316 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3317 | * called afterwards. |
| 3318 | */ |
| 3319 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3320 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3321 | { |
| 3322 | int retval; |
| 3323 | int i; |
| 3324 | int m; |
| 3325 | int c; |
| 3326 | |
| 3327 | if (len < 0) |
| 3328 | len = (int)STRLEN(str); |
| 3329 | |
| 3330 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3331 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3332 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3333 | else |
| 3334 | retval = OK; |
| 3335 | if (retval == OK) |
| 3336 | { |
| 3337 | if (!ccline.overstrike) |
| 3338 | { |
| 3339 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3340 | ccline.cmdbuff + ccline.cmdpos, |
| 3341 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3342 | ccline.cmdlen += len; |
| 3343 | } |
| 3344 | else |
| 3345 | { |
| 3346 | #ifdef FEAT_MBYTE |
| 3347 | if (has_mbyte) |
| 3348 | { |
| 3349 | /* Count nr of characters in the new string. */ |
| 3350 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3351 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | ++m; |
| 3353 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3354 | * characters. */ |
| 3355 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3356 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3357 | --m; |
| 3358 | if (i < ccline.cmdlen) |
| 3359 | { |
| 3360 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3361 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3362 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3363 | } |
| 3364 | else |
| 3365 | ccline.cmdlen = ccline.cmdpos + len; |
| 3366 | } |
| 3367 | else |
| 3368 | #endif |
| 3369 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3370 | ccline.cmdlen = ccline.cmdpos + len; |
| 3371 | } |
| 3372 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3373 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3374 | |
| 3375 | #ifdef FEAT_MBYTE |
| 3376 | if (enc_utf8) |
| 3377 | { |
| 3378 | /* When the inserted text starts with a composing character, |
| 3379 | * backup to the character before it. There could be two of them. |
| 3380 | */ |
| 3381 | i = 0; |
| 3382 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3383 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3384 | { |
| 3385 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3386 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3387 | ccline.cmdpos -= i; |
| 3388 | len += i; |
| 3389 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3390 | } |
| 3391 | # ifdef FEAT_ARABIC |
| 3392 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3393 | { |
| 3394 | /* Check the previous character for Arabic combining pair. */ |
| 3395 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3396 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3397 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3398 | + ccline.cmdpos - i), c)) |
| 3399 | { |
| 3400 | ccline.cmdpos -= i; |
| 3401 | len += i; |
| 3402 | } |
| 3403 | else |
| 3404 | i = 0; |
| 3405 | } |
| 3406 | # endif |
| 3407 | if (i != 0) |
| 3408 | { |
| 3409 | /* Also backup the cursor position. */ |
| 3410 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3411 | ccline.cmdspos -= i; |
| 3412 | msg_col -= i; |
| 3413 | if (msg_col < 0) |
| 3414 | { |
| 3415 | msg_col += Columns; |
| 3416 | --msg_row; |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | #endif |
| 3421 | |
| 3422 | if (redraw && !cmd_silent) |
| 3423 | { |
| 3424 | msg_no_more = TRUE; |
| 3425 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3426 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3427 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3428 | /* Avoid clearing the rest of the line too often. */ |
| 3429 | if (cmdline_row != i || ccline.overstrike) |
| 3430 | msg_clr_eos(); |
| 3431 | msg_no_more = FALSE; |
| 3432 | } |
| 3433 | #ifdef FEAT_FKMAP |
| 3434 | /* |
| 3435 | * If we are in Farsi command mode, the character input must be in |
| 3436 | * Insert mode. So do not advance the cmdpos. |
| 3437 | */ |
| 3438 | if (!cmd_fkmap) |
| 3439 | #endif |
| 3440 | { |
| 3441 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3442 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3444 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3445 | m = MAXCOL; |
| 3446 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3447 | else |
| 3448 | m = MAXCOL; |
| 3449 | for (i = 0; i < len; ++i) |
| 3450 | { |
| 3451 | c = cmdline_charsize(ccline.cmdpos); |
| 3452 | #ifdef FEAT_MBYTE |
| 3453 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3454 | if (has_mbyte) |
| 3455 | correct_cmdspos(ccline.cmdpos, c); |
| 3456 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3457 | /* Stop cursor at the end of the screen, but do increment the |
| 3458 | * insert position, so that entering a very long command |
| 3459 | * works, even though you can't see it. */ |
| 3460 | if (ccline.cmdspos + c < m) |
| 3461 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3462 | #ifdef FEAT_MBYTE |
| 3463 | if (has_mbyte) |
| 3464 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3465 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3466 | if (c > len - i - 1) |
| 3467 | c = len - i - 1; |
| 3468 | ccline.cmdpos += c; |
| 3469 | i += c; |
| 3470 | } |
| 3471 | #endif |
| 3472 | ++ccline.cmdpos; |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | if (redraw) |
| 3477 | msg_check(); |
| 3478 | return retval; |
| 3479 | } |
| 3480 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3481 | static struct cmdline_info prev_ccline; |
| 3482 | static int prev_ccline_used = FALSE; |
| 3483 | |
| 3484 | /* |
| 3485 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3486 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3487 | * available globally in prev_ccline. |
| 3488 | */ |
| 3489 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3490 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3491 | { |
| 3492 | if (!prev_ccline_used) |
| 3493 | { |
| 3494 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3495 | prev_ccline_used = TRUE; |
| 3496 | } |
| 3497 | *ccp = prev_ccline; |
| 3498 | prev_ccline = ccline; |
| 3499 | ccline.cmdbuff = NULL; |
| 3500 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3501 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3505 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3506 | */ |
| 3507 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3508 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3509 | { |
| 3510 | ccline = prev_ccline; |
| 3511 | prev_ccline = *ccp; |
| 3512 | } |
| 3513 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3514 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3515 | /* |
| 3516 | * Save the command line into allocated memory. Returns a pointer to be |
| 3517 | * passed to restore_cmdline_alloc() later. |
| 3518 | * Returns NULL when failed. |
| 3519 | */ |
| 3520 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3521 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3522 | { |
| 3523 | struct cmdline_info *p; |
| 3524 | |
| 3525 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3526 | if (p != NULL) |
| 3527 | save_cmdline(p); |
| 3528 | return (char_u *)p; |
| 3529 | } |
| 3530 | |
| 3531 | /* |
| 3532 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3533 | */ |
| 3534 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3535 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3536 | { |
| 3537 | if (p != NULL) |
| 3538 | { |
| 3539 | restore_cmdline((struct cmdline_info *)p); |
| 3540 | vim_free(p); |
| 3541 | } |
| 3542 | } |
| 3543 | #endif |
| 3544 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3545 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3546 | * Paste a yank register into the command line. |
| 3547 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3548 | * insert_reg() can't be used here, because special characters from the |
| 3549 | * register contents will be interpreted as commands. |
| 3550 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3551 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3552 | */ |
| 3553 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3554 | cmdline_paste( |
| 3555 | int regname, |
| 3556 | int literally, /* Insert text literally instead of "as typed" */ |
| 3557 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3558 | { |
| 3559 | long i; |
| 3560 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3561 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3562 | int allocated; |
| 3563 | struct cmdline_info save_ccline; |
| 3564 | |
| 3565 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3566 | * the command line */ |
| 3567 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
Bram Moolenaar | e2c8d83 | 2018-05-01 19:24:03 +0200 | [diff] [blame] | 3568 | && regname != Ctrl_A && regname != Ctrl_L |
| 3569 | && !valid_yank_reg(regname, FALSE)) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3570 | return FAIL; |
| 3571 | |
| 3572 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3573 | * CTRL-C to break the loop. */ |
| 3574 | line_breakcheck(); |
| 3575 | if (got_int) |
| 3576 | return FAIL; |
| 3577 | |
| 3578 | #ifdef FEAT_CLIPBOARD |
| 3579 | regname = may_get_selection(regname); |
| 3580 | #endif |
| 3581 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3582 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3583 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3584 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3585 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3586 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3587 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3588 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3589 | |
| 3590 | if (i) |
| 3591 | { |
| 3592 | /* Got the value of a special register in "arg". */ |
| 3593 | if (arg == NULL) |
| 3594 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3595 | |
| 3596 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3597 | * part of the word. */ |
| 3598 | p = arg; |
| 3599 | if (p_is && regname == Ctrl_W) |
| 3600 | { |
| 3601 | char_u *w; |
| 3602 | int len; |
| 3603 | |
| 3604 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3605 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3606 | { |
| 3607 | #ifdef FEAT_MBYTE |
| 3608 | if (has_mbyte) |
| 3609 | { |
| 3610 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3611 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3612 | break; |
| 3613 | w -= len; |
| 3614 | } |
| 3615 | else |
| 3616 | #endif |
| 3617 | { |
| 3618 | if (!vim_iswordc(w[-1])) |
| 3619 | break; |
| 3620 | --w; |
| 3621 | } |
| 3622 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3623 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3624 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3625 | p += len; |
| 3626 | } |
| 3627 | |
| 3628 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3629 | if (allocated) |
| 3630 | vim_free(arg); |
| 3631 | return OK; |
| 3632 | } |
| 3633 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3634 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /* |
| 3638 | * Put a string on the command line. |
| 3639 | * When "literally" is TRUE, insert literally. |
| 3640 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3641 | * line. |
| 3642 | */ |
| 3643 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3644 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3645 | { |
| 3646 | int c, cv; |
| 3647 | |
| 3648 | if (literally) |
| 3649 | put_on_cmdline(s, -1, TRUE); |
| 3650 | else |
| 3651 | while (*s != NUL) |
| 3652 | { |
| 3653 | cv = *s; |
| 3654 | if (cv == Ctrl_V && s[1]) |
| 3655 | ++s; |
| 3656 | #ifdef FEAT_MBYTE |
| 3657 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3658 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3659 | else |
| 3660 | #endif |
| 3661 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3662 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3663 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3664 | #ifdef UNIX |
| 3665 | || c == intr_char |
| 3666 | #endif |
| 3667 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3668 | stuffcharReadbuff(Ctrl_V); |
| 3669 | stuffcharReadbuff(c); |
| 3670 | } |
| 3671 | } |
| 3672 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3673 | #ifdef FEAT_WILDMENU |
| 3674 | /* |
| 3675 | * Delete characters on the command line, from "from" to the current |
| 3676 | * position. |
| 3677 | */ |
| 3678 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3679 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3680 | { |
| 3681 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3682 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3683 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3684 | ccline.cmdpos = from; |
| 3685 | } |
| 3686 | #endif |
| 3687 | |
| 3688 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3689 | * This function is called when the screen size changes and with incremental |
| 3690 | * search and in other situations where the command line may have been |
| 3691 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3692 | */ |
| 3693 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3694 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3696 | redrawcmdline_ex(TRUE); |
| 3697 | } |
| 3698 | |
| 3699 | void |
| 3700 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3701 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3702 | if (cmd_silent) |
| 3703 | return; |
| 3704 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3705 | if (do_compute_cmdrow) |
| 3706 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | redrawcmd(); |
| 3708 | cursorcmd(); |
| 3709 | } |
| 3710 | |
| 3711 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3712 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | { |
| 3714 | int i; |
| 3715 | |
| 3716 | if (cmd_silent) |
| 3717 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3718 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3719 | msg_putchar(ccline.cmdfirstc); |
| 3720 | if (ccline.cmdprompt != NULL) |
| 3721 | { |
| 3722 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3723 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3724 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3725 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3726 | --ccline.cmdindent; |
| 3727 | } |
| 3728 | else |
| 3729 | for (i = ccline.cmdindent; i > 0; --i) |
| 3730 | msg_putchar(' '); |
| 3731 | } |
| 3732 | |
| 3733 | /* |
| 3734 | * Redraw what is currently on the command line. |
| 3735 | */ |
| 3736 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3737 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3738 | { |
| 3739 | if (cmd_silent) |
| 3740 | return; |
| 3741 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3742 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3743 | if (ccline.cmdbuff == NULL) |
| 3744 | { |
| 3745 | windgoto(cmdline_row, 0); |
| 3746 | msg_clr_eos(); |
| 3747 | return; |
| 3748 | } |
| 3749 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | msg_start(); |
| 3751 | redrawcmdprompt(); |
| 3752 | |
| 3753 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3754 | msg_no_more = TRUE; |
| 3755 | draw_cmdline(0, ccline.cmdlen); |
| 3756 | msg_clr_eos(); |
| 3757 | msg_no_more = FALSE; |
| 3758 | |
| 3759 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3760 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3761 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3762 | |
| 3763 | /* |
| 3764 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3765 | * in cmdline mode we can reset them now. |
| 3766 | */ |
| 3767 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3768 | |
| 3769 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3770 | * in cmdline mode */ |
| 3771 | skip_redraw = FALSE; |
| 3772 | } |
| 3773 | |
| 3774 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3775 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3777 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3778 | cmdline_row = Rows - 1; |
| 3779 | else |
| 3780 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3781 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3785 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | { |
| 3787 | if (cmd_silent) |
| 3788 | return; |
| 3789 | |
| 3790 | #ifdef FEAT_RIGHTLEFT |
| 3791 | if (cmdmsg_rl) |
| 3792 | { |
| 3793 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3794 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3795 | if (msg_row <= 0) |
| 3796 | msg_row = Rows - 1; |
| 3797 | } |
| 3798 | else |
| 3799 | #endif |
| 3800 | { |
| 3801 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3802 | msg_col = ccline.cmdspos % (int)Columns; |
| 3803 | if (msg_row >= Rows) |
| 3804 | msg_row = Rows - 1; |
| 3805 | } |
| 3806 | |
| 3807 | windgoto(msg_row, msg_col); |
| 3808 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3809 | if (p_imst == IM_ON_THE_SPOT) |
| 3810 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3811 | #endif |
| 3812 | #ifdef MCH_CURSOR_SHAPE |
| 3813 | mch_update_cursor(); |
| 3814 | #endif |
| 3815 | } |
| 3816 | |
| 3817 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3818 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3819 | { |
| 3820 | msg_start(); |
| 3821 | #ifdef FEAT_RIGHTLEFT |
| 3822 | if (cmdmsg_rl) |
| 3823 | msg_col = Columns - 1; |
| 3824 | else |
| 3825 | #endif |
| 3826 | msg_col = 0; /* always start in column 0 */ |
| 3827 | if (clr) /* clear the bottom line(s) */ |
| 3828 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3829 | windgoto(cmdline_row, 0); |
| 3830 | } |
| 3831 | |
| 3832 | /* |
| 3833 | * Check the word in front of the cursor for an abbreviation. |
| 3834 | * Called when the non-id character "c" has been entered. |
| 3835 | * When an abbreviation is recognized it is removed from the text with |
| 3836 | * backspaces and the replacement string is inserted, followed by "c". |
| 3837 | */ |
| 3838 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3839 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | { |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3841 | int spos = 0; |
| 3842 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3844 | return FALSE; |
| 3845 | |
Bram Moolenaar | 5e3423d | 2018-05-13 18:36:27 +0200 | [diff] [blame] | 3846 | /* Do not consider '<,'> be part of the mapping, skip leading whitespace. |
| 3847 | * Actually accepts any mark. */ |
| 3848 | while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen) |
| 3849 | spos++; |
| 3850 | if (ccline.cmdlen - spos > 5 |
| 3851 | && ccline.cmdbuff[spos] == '\'' |
| 3852 | && ccline.cmdbuff[spos + 2] == ',' |
| 3853 | && ccline.cmdbuff[spos + 3] == '\'') |
| 3854 | spos += 5; |
| 3855 | else |
| 3856 | /* check abbreviation from the beginning of the commandline */ |
| 3857 | spos = 0; |
| 3858 | |
| 3859 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3862 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3863 | static int |
| 3864 | #ifdef __BORLANDC__ |
| 3865 | _RTLENTRYF |
| 3866 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3867 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3868 | { |
| 3869 | char_u *p1 = *(char_u **)s1; |
| 3870 | char_u *p2 = *(char_u **)s2; |
| 3871 | |
| 3872 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3873 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3874 | return STRCMP(p1, p2); |
| 3875 | } |
| 3876 | #endif |
| 3877 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3878 | /* |
| 3879 | * Return FAIL if this is not an appropriate context in which to do |
| 3880 | * completion of anything, return OK if it is (even if there are no matches). |
| 3881 | * For the caller, this means that the character is just passed through like a |
| 3882 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3883 | */ |
| 3884 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3885 | nextwild( |
| 3886 | expand_T *xp, |
| 3887 | int type, |
| 3888 | int options, /* extra options for ExpandOne() */ |
| 3889 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3890 | { |
| 3891 | int i, j; |
| 3892 | char_u *p1; |
| 3893 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | int difflen; |
| 3895 | int v; |
| 3896 | |
| 3897 | if (xp->xp_numfiles == -1) |
| 3898 | { |
| 3899 | set_expand_context(xp); |
| 3900 | cmd_showtail = expand_showtail(xp); |
| 3901 | } |
| 3902 | |
| 3903 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3904 | { |
| 3905 | beep_flush(); |
| 3906 | return OK; /* Something illegal on command line */ |
| 3907 | } |
| 3908 | if (xp->xp_context == EXPAND_NOTHING) |
| 3909 | { |
| 3910 | /* Caller can use the character as a normal char instead */ |
| 3911 | return FAIL; |
| 3912 | } |
| 3913 | |
| 3914 | MSG_PUTS("..."); /* show that we are busy */ |
| 3915 | out_flush(); |
| 3916 | |
| 3917 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3918 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3919 | |
| 3920 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3921 | { |
| 3922 | /* |
| 3923 | * Get next/previous match for a previous expanded pattern. |
| 3924 | */ |
| 3925 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3926 | } |
| 3927 | else |
| 3928 | { |
| 3929 | /* |
| 3930 | * Translate string into pattern and expand it. |
| 3931 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3932 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3933 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | p2 = NULL; |
| 3935 | else |
| 3936 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3937 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3938 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3939 | if (escape) |
| 3940 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3941 | |
| 3942 | if (p_wic) |
| 3943 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3944 | p2 = ExpandOne(xp, p1, |
| 3945 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3946 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3947 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3948 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | if (p2 != NULL && type == WILD_LONGEST) |
| 3950 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3951 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | if (ccline.cmdbuff[i + j] == '*' |
| 3953 | || ccline.cmdbuff[i + j] == '?') |
| 3954 | break; |
| 3955 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3956 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | if (p2 != NULL && !got_int) |
| 3962 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3963 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3964 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3965 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3966 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3967 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3968 | } |
| 3969 | else |
| 3970 | v = OK; |
| 3971 | if (v == OK) |
| 3972 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3973 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3974 | &ccline.cmdbuff[ccline.cmdpos], |
| 3975 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3976 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3977 | ccline.cmdlen += difflen; |
| 3978 | ccline.cmdpos += difflen; |
| 3979 | } |
| 3980 | } |
| 3981 | vim_free(p2); |
| 3982 | |
| 3983 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3984 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | |
| 3986 | /* When expanding a ":map" command and no matches are found, assume that |
| 3987 | * the key is supposed to be inserted literally */ |
| 3988 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3989 | return FAIL; |
| 3990 | |
| 3991 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3992 | beep_flush(); |
| 3993 | else if (xp->xp_numfiles == 1) |
| 3994 | /* free expanded pattern */ |
| 3995 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3996 | |
| 3997 | return OK; |
| 3998 | } |
| 3999 | |
| 4000 | /* |
| 4001 | * Do wildcard expansion on the string 'str'. |
| 4002 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 4003 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4004 | * Return NULL for failure. |
| 4005 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4006 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 4007 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 4008 | * WILD_PREV "orig" should be NULL. |
| 4009 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 4010 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 4011 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | * |
| 4013 | * mode = WILD_FREE: just free previously expanded matches |
| 4014 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 4015 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 4016 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 4017 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 4018 | * mode = WILD_ALL: return all matches concatenated |
| 4019 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4020 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | * |
| 4022 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 4023 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 4024 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 4025 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 4026 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 4027 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 4028 | * options = WILD_SILENT: don't print warning messages |
| 4029 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4030 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4031 | * |
| 4032 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 4033 | */ |
| 4034 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4035 | ExpandOne( |
| 4036 | expand_T *xp, |
| 4037 | char_u *str, |
| 4038 | char_u *orig, /* allocated copy of original of expanded string */ |
| 4039 | int options, |
| 4040 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | { |
| 4042 | char_u *ss = NULL; |
| 4043 | static int findex; |
| 4044 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4045 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4046 | int i; |
| 4047 | long_u len; |
| 4048 | int non_suf_match; /* number without matching suffix */ |
| 4049 | |
| 4050 | /* |
| 4051 | * first handle the case of using an old match |
| 4052 | */ |
| 4053 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 4054 | { |
| 4055 | if (xp->xp_numfiles > 0) |
| 4056 | { |
| 4057 | if (mode == WILD_PREV) |
| 4058 | { |
| 4059 | if (findex == -1) |
| 4060 | findex = xp->xp_numfiles; |
| 4061 | --findex; |
| 4062 | } |
| 4063 | else /* mode == WILD_NEXT */ |
| 4064 | ++findex; |
| 4065 | |
| 4066 | /* |
| 4067 | * When wrapping around, return the original string, set findex to |
| 4068 | * -1. |
| 4069 | */ |
| 4070 | if (findex < 0) |
| 4071 | { |
| 4072 | if (orig_save == NULL) |
| 4073 | findex = xp->xp_numfiles - 1; |
| 4074 | else |
| 4075 | findex = -1; |
| 4076 | } |
| 4077 | if (findex >= xp->xp_numfiles) |
| 4078 | { |
| 4079 | if (orig_save == NULL) |
| 4080 | findex = 0; |
| 4081 | else |
| 4082 | findex = -1; |
| 4083 | } |
| 4084 | #ifdef FEAT_WILDMENU |
| 4085 | if (p_wmnu) |
| 4086 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 4087 | findex, cmd_showtail); |
| 4088 | #endif |
| 4089 | if (findex == -1) |
| 4090 | return vim_strsave(orig_save); |
| 4091 | return vim_strsave(xp->xp_files[findex]); |
| 4092 | } |
| 4093 | else |
| 4094 | return NULL; |
| 4095 | } |
| 4096 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4097 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4098 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 4099 | { |
| 4100 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4101 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 4102 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4103 | } |
| 4104 | findex = 0; |
| 4105 | |
| 4106 | if (mode == WILD_FREE) /* only release file name */ |
| 4107 | return NULL; |
| 4108 | |
| 4109 | if (xp->xp_numfiles == -1) |
| 4110 | { |
| 4111 | vim_free(orig_save); |
| 4112 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4113 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4114 | |
| 4115 | /* |
| 4116 | * Do the expansion. |
| 4117 | */ |
| 4118 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 4119 | options) == FAIL) |
| 4120 | { |
| 4121 | #ifdef FNAME_ILLEGAL |
| 4122 | /* Illegal file name has been silently skipped. But when there |
| 4123 | * are wildcards, the real problem is that there was no match, |
| 4124 | * causing the pattern to be added, which has illegal characters. |
| 4125 | */ |
| 4126 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 4127 | EMSG2(_(e_nomatch2), str); |
| 4128 | #endif |
| 4129 | } |
| 4130 | else if (xp->xp_numfiles == 0) |
| 4131 | { |
| 4132 | if (!(options & WILD_SILENT)) |
| 4133 | EMSG2(_(e_nomatch2), str); |
| 4134 | } |
| 4135 | else |
| 4136 | { |
| 4137 | /* Escape the matches for use on the command line. */ |
| 4138 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 4139 | |
| 4140 | /* |
| 4141 | * Check for matching suffixes in file names. |
| 4142 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 4143 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 4144 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4145 | { |
| 4146 | if (xp->xp_numfiles) |
| 4147 | non_suf_match = xp->xp_numfiles; |
| 4148 | else |
| 4149 | non_suf_match = 1; |
| 4150 | if ((xp->xp_context == EXPAND_FILES |
| 4151 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4152 | && xp->xp_numfiles > 1) |
| 4153 | { |
| 4154 | /* |
| 4155 | * More than one match; check suffix. |
| 4156 | * The files will have been sorted on matching suffix in |
| 4157 | * expand_wildcards, only need to check the first two. |
| 4158 | */ |
| 4159 | non_suf_match = 0; |
| 4160 | for (i = 0; i < 2; ++i) |
| 4161 | if (match_suffix(xp->xp_files[i])) |
| 4162 | ++non_suf_match; |
| 4163 | } |
| 4164 | if (non_suf_match != 1) |
| 4165 | { |
| 4166 | /* Can we ever get here unless it's while expanding |
| 4167 | * interactively? If not, we can get rid of this all |
| 4168 | * together. Don't really want to wait for this message |
| 4169 | * (and possibly have to hit return to continue!). |
| 4170 | */ |
| 4171 | if (!(options & WILD_SILENT)) |
| 4172 | EMSG(_(e_toomany)); |
| 4173 | else if (!(options & WILD_NO_BEEP)) |
| 4174 | beep_flush(); |
| 4175 | } |
| 4176 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 4177 | ss = vim_strsave(xp->xp_files[0]); |
| 4178 | } |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | /* Find longest common part */ |
| 4183 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 4184 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4185 | int mb_len = 1; |
| 4186 | int c0, ci; |
| 4187 | |
| 4188 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4189 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4190 | #ifdef FEAT_MBYTE |
| 4191 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4192 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4193 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 4194 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 4195 | } |
| 4196 | else |
| 4197 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 4198 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4199 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 4200 | { |
| 4201 | #ifdef FEAT_MBYTE |
| 4202 | if (has_mbyte) |
| 4203 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 4204 | else |
| 4205 | #endif |
| 4206 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4207 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4208 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4209 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4210 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4212 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | break; |
| 4214 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4215 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4216 | break; |
| 4217 | } |
| 4218 | if (i < xp->xp_numfiles) |
| 4219 | { |
| 4220 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 4221 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4222 | break; |
| 4223 | } |
| 4224 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 4225 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4226 | ss = alloc((unsigned)len + 1); |
| 4227 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4228 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4229 | findex = -1; /* next p_wc gets first one */ |
| 4230 | } |
| 4231 | |
| 4232 | /* Concatenate all matching names */ |
| 4233 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 4234 | { |
| 4235 | len = 0; |
| 4236 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4237 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 4238 | ss = lalloc(len, TRUE); |
| 4239 | if (ss != NULL) |
| 4240 | { |
| 4241 | *ss = NUL; |
| 4242 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 4243 | { |
| 4244 | STRCAT(ss, xp->xp_files[i]); |
| 4245 | if (i != xp->xp_numfiles - 1) |
| 4246 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 4247 | } |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 4252 | ExpandCleanup(xp); |
| 4253 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4254 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 4255 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 4256 | vim_free(orig); |
| 4257 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4258 | return ss; |
| 4259 | } |
| 4260 | |
| 4261 | /* |
| 4262 | * Prepare an expand structure for use. |
| 4263 | */ |
| 4264 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4265 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4266 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 4267 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4268 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4269 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4270 | #ifndef BACKSLASH_IN_FILENAME |
| 4271 | xp->xp_shell = FALSE; |
| 4272 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4273 | xp->xp_numfiles = -1; |
| 4274 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4275 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4276 | xp->xp_arg = NULL; |
| 4277 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4278 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * Cleanup an expand structure after use. |
| 4283 | */ |
| 4284 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4285 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | { |
| 4287 | if (xp->xp_numfiles >= 0) |
| 4288 | { |
| 4289 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4290 | xp->xp_numfiles = -1; |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4295 | ExpandEscape( |
| 4296 | expand_T *xp, |
| 4297 | char_u *str, |
| 4298 | int numfiles, |
| 4299 | char_u **files, |
| 4300 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | { |
| 4302 | int i; |
| 4303 | char_u *p; |
| 4304 | |
| 4305 | /* |
| 4306 | * May change home directory back to "~" |
| 4307 | */ |
| 4308 | if (options & WILD_HOME_REPLACE) |
| 4309 | tilde_replace(str, numfiles, files); |
| 4310 | |
| 4311 | if (options & WILD_ESCAPE) |
| 4312 | { |
| 4313 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4314 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4315 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4316 | || xp->xp_context == EXPAND_BUFFERS |
| 4317 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4318 | { |
| 4319 | /* |
| 4320 | * Insert a backslash into a file name before a space, \, %, # |
| 4321 | * and wildmatch characters, except '~'. |
| 4322 | */ |
| 4323 | for (i = 0; i < numfiles; ++i) |
| 4324 | { |
| 4325 | /* for ":set path=" we need to escape spaces twice */ |
| 4326 | if (xp->xp_backslash == XP_BS_THREE) |
| 4327 | { |
| 4328 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4329 | if (p != NULL) |
| 4330 | { |
| 4331 | vim_free(files[i]); |
| 4332 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4333 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4334 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4335 | if (p != NULL) |
| 4336 | { |
| 4337 | vim_free(files[i]); |
| 4338 | files[i] = p; |
| 4339 | } |
| 4340 | #endif |
| 4341 | } |
| 4342 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4343 | #ifdef BACKSLASH_IN_FILENAME |
| 4344 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4345 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4346 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4347 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4348 | if (p != NULL) |
| 4349 | { |
| 4350 | vim_free(files[i]); |
| 4351 | files[i] = p; |
| 4352 | } |
| 4353 | |
| 4354 | /* If 'str' starts with "\~", replace "~" at start of |
| 4355 | * files[i] with "\~". */ |
| 4356 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4357 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4358 | } |
| 4359 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4360 | |
| 4361 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4362 | * could be seen as "+cmd". */ |
| 4363 | if (*files[0] == '+') |
| 4364 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4365 | } |
| 4366 | else if (xp->xp_context == EXPAND_TAGS) |
| 4367 | { |
| 4368 | /* |
| 4369 | * Insert a backslash before characters in a tag name that |
| 4370 | * would terminate the ":tag" command. |
| 4371 | */ |
| 4372 | for (i = 0; i < numfiles; ++i) |
| 4373 | { |
| 4374 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4375 | if (p != NULL) |
| 4376 | { |
| 4377 | vim_free(files[i]); |
| 4378 | files[i] = p; |
| 4379 | } |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | } |
| 4384 | |
| 4385 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4386 | * Escape special characters in "fname" for when used as a file name argument |
| 4387 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4388 | * Returns the result in allocated memory. |
| 4389 | */ |
| 4390 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4391 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4392 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4393 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4394 | #ifdef BACKSLASH_IN_FILENAME |
| 4395 | char_u buf[20]; |
| 4396 | int j = 0; |
| 4397 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4398 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4399 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4400 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4401 | buf[j++] = *p; |
| 4402 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4403 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4404 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4405 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4406 | if (shell && csh_like_shell() && p != NULL) |
| 4407 | { |
| 4408 | char_u *s; |
| 4409 | |
| 4410 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4411 | * One is taken by Vim, one by the shell. */ |
| 4412 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4413 | vim_free(p); |
| 4414 | p = s; |
| 4415 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4416 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4417 | |
| 4418 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4419 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4420 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4421 | escape_fname(&p); |
| 4422 | |
| 4423 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4427 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4428 | */ |
| 4429 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4430 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4431 | { |
| 4432 | char_u *p; |
| 4433 | |
| 4434 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4435 | if (p != NULL) |
| 4436 | { |
| 4437 | p[0] = '\\'; |
| 4438 | STRCPY(p + 1, *pp); |
| 4439 | vim_free(*pp); |
| 4440 | *pp = p; |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4445 | * For each file name in files[num_files]: |
| 4446 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4447 | */ |
| 4448 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4449 | tilde_replace( |
| 4450 | char_u *orig_pat, |
| 4451 | int num_files, |
| 4452 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | { |
| 4454 | int i; |
| 4455 | char_u *p; |
| 4456 | |
| 4457 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4458 | { |
| 4459 | for (i = 0; i < num_files; ++i) |
| 4460 | { |
| 4461 | p = home_replace_save(NULL, files[i]); |
| 4462 | if (p != NULL) |
| 4463 | { |
| 4464 | vim_free(files[i]); |
| 4465 | files[i] = p; |
| 4466 | } |
| 4467 | } |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | /* |
| 4472 | * Show all matches for completion on the command line. |
| 4473 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4474 | * be inserted like a normal character. |
| 4475 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4477 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4478 | { |
| 4479 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4480 | int num_files; |
| 4481 | char_u **files_found; |
| 4482 | int i, j, k; |
| 4483 | int maxlen; |
| 4484 | int lines; |
| 4485 | int columns; |
| 4486 | char_u *p; |
| 4487 | int lastlen; |
| 4488 | int attr; |
| 4489 | int showtail; |
| 4490 | |
| 4491 | if (xp->xp_numfiles == -1) |
| 4492 | { |
| 4493 | set_expand_context(xp); |
| 4494 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4495 | &num_files, &files_found); |
| 4496 | showtail = expand_showtail(xp); |
| 4497 | if (i != EXPAND_OK) |
| 4498 | return i; |
| 4499 | |
| 4500 | } |
| 4501 | else |
| 4502 | { |
| 4503 | num_files = xp->xp_numfiles; |
| 4504 | files_found = xp->xp_files; |
| 4505 | showtail = cmd_showtail; |
| 4506 | } |
| 4507 | |
| 4508 | #ifdef FEAT_WILDMENU |
| 4509 | if (!wildmenu) |
| 4510 | { |
| 4511 | #endif |
| 4512 | msg_didany = FALSE; /* lines_left will be set */ |
| 4513 | msg_start(); /* prepare for paging */ |
| 4514 | msg_putchar('\n'); |
| 4515 | out_flush(); |
| 4516 | cmdline_row = msg_row; |
| 4517 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4518 | msg_start(); /* prepare for paging */ |
| 4519 | #ifdef FEAT_WILDMENU |
| 4520 | } |
| 4521 | #endif |
| 4522 | |
| 4523 | if (got_int) |
| 4524 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4525 | #ifdef FEAT_WILDMENU |
| 4526 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4527 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | #endif |
| 4529 | else |
| 4530 | { |
| 4531 | /* find the length of the longest file name */ |
| 4532 | maxlen = 0; |
| 4533 | for (i = 0; i < num_files; ++i) |
| 4534 | { |
| 4535 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4536 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4537 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4538 | { |
| 4539 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4540 | j = vim_strsize(NameBuff); |
| 4541 | } |
| 4542 | else |
| 4543 | j = vim_strsize(L_SHOWFILE(i)); |
| 4544 | if (j > maxlen) |
| 4545 | maxlen = j; |
| 4546 | } |
| 4547 | |
| 4548 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4549 | lines = num_files; |
| 4550 | else |
| 4551 | { |
| 4552 | /* compute the number of columns and lines for the listing */ |
| 4553 | maxlen += 2; /* two spaces between file names */ |
| 4554 | columns = ((int)Columns + 2) / maxlen; |
| 4555 | if (columns < 1) |
| 4556 | columns = 1; |
| 4557 | lines = (num_files + columns - 1) / columns; |
| 4558 | } |
| 4559 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4560 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4561 | |
| 4562 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4563 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4564 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4565 | msg_clr_eos(); |
| 4566 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4567 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | /* list the files line by line */ |
| 4571 | for (i = 0; i < lines; ++i) |
| 4572 | { |
| 4573 | lastlen = 999; |
| 4574 | for (k = i; k < num_files; k += lines) |
| 4575 | { |
| 4576 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4577 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4578 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4580 | msg_advance(maxlen + 1); |
| 4581 | msg_puts(p); |
| 4582 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4583 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4584 | break; |
| 4585 | } |
| 4586 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4587 | msg_putchar(' '); |
| 4588 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4589 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4590 | || xp->xp_context == EXPAND_BUFFERS) |
| 4591 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4592 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4593 | if (xp->xp_numfiles != -1) |
| 4594 | { |
| 4595 | char_u *halved_slash; |
| 4596 | char_u *exp_path; |
| 4597 | |
| 4598 | /* Expansion was done before and special characters |
| 4599 | * were escaped, need to halve backslashes. Also |
| 4600 | * $HOME has been replaced with ~/. */ |
| 4601 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4602 | halved_slash = backslash_halve_save( |
| 4603 | exp_path != NULL ? exp_path : files_found[k]); |
| 4604 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4605 | : files_found[k]); |
| 4606 | vim_free(exp_path); |
| 4607 | vim_free(halved_slash); |
| 4608 | } |
| 4609 | else |
| 4610 | /* Expansion was done here, file names are literal. */ |
| 4611 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | if (showtail) |
| 4613 | p = L_SHOWFILE(k); |
| 4614 | else |
| 4615 | { |
| 4616 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4617 | TRUE); |
| 4618 | p = NameBuff; |
| 4619 | } |
| 4620 | } |
| 4621 | else |
| 4622 | { |
| 4623 | j = FALSE; |
| 4624 | p = L_SHOWFILE(k); |
| 4625 | } |
| 4626 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4627 | } |
| 4628 | if (msg_col > 0) /* when not wrapped around */ |
| 4629 | { |
| 4630 | msg_clr_eos(); |
| 4631 | msg_putchar('\n'); |
| 4632 | } |
| 4633 | out_flush(); /* show one line at a time */ |
| 4634 | if (got_int) |
| 4635 | { |
| 4636 | got_int = FALSE; |
| 4637 | break; |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | /* |
| 4642 | * we redraw the command below the lines that we have just listed |
| 4643 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4644 | */ |
| 4645 | cmdline_row = msg_row; /* will put it back later */ |
| 4646 | } |
| 4647 | |
| 4648 | if (xp->xp_numfiles == -1) |
| 4649 | FreeWild(num_files, files_found); |
| 4650 | |
| 4651 | return EXPAND_OK; |
| 4652 | } |
| 4653 | |
| 4654 | /* |
| 4655 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4656 | * Find tail of file name path, but ignore trailing "/". |
| 4657 | */ |
| 4658 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4659 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4660 | { |
| 4661 | char_u *p; |
| 4662 | char_u *t = s; |
| 4663 | int had_sep = FALSE; |
| 4664 | |
| 4665 | for (p = s; *p != NUL; ) |
| 4666 | { |
| 4667 | if (vim_ispathsep(*p) |
| 4668 | #ifdef BACKSLASH_IN_FILENAME |
| 4669 | && !rem_backslash(p) |
| 4670 | #endif |
| 4671 | ) |
| 4672 | had_sep = TRUE; |
| 4673 | else if (had_sep) |
| 4674 | { |
| 4675 | t = p; |
| 4676 | had_sep = FALSE; |
| 4677 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4678 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4679 | } |
| 4680 | return t; |
| 4681 | } |
| 4682 | |
| 4683 | /* |
| 4684 | * Return TRUE if we only need to show the tail of completion matches. |
| 4685 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4686 | * returned. |
| 4687 | */ |
| 4688 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4689 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4690 | { |
| 4691 | char_u *s; |
| 4692 | char_u *end; |
| 4693 | |
| 4694 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4695 | if (xp->xp_context != EXPAND_FILES |
| 4696 | && xp->xp_context != EXPAND_SHELLCMD |
| 4697 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4698 | return FALSE; |
| 4699 | |
| 4700 | end = gettail(xp->xp_pattern); |
| 4701 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4702 | return FALSE; |
| 4703 | |
| 4704 | for (s = xp->xp_pattern; s < end; s++) |
| 4705 | { |
| 4706 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4707 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4708 | if (rem_backslash(s)) |
| 4709 | ++s; |
| 4710 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4711 | return FALSE; |
| 4712 | } |
| 4713 | return TRUE; |
| 4714 | } |
| 4715 | |
| 4716 | /* |
| 4717 | * Prepare a string for expansion. |
| 4718 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4719 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4720 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4721 | * the name into allocated memory and prepend "^". |
| 4722 | */ |
| 4723 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4724 | addstar( |
| 4725 | char_u *fname, |
| 4726 | int len, |
| 4727 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4728 | { |
| 4729 | char_u *retval; |
| 4730 | int i, j; |
| 4731 | int new_len; |
| 4732 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4733 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4734 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4735 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4736 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4737 | && context != EXPAND_SHELLCMD |
| 4738 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4739 | { |
| 4740 | /* |
| 4741 | * Matching will be done internally (on something other than files). |
| 4742 | * So we convert the file-matching-type wildcards into our kind for |
| 4743 | * use with vim_regcomp(). First work out how long it will be: |
| 4744 | */ |
| 4745 | |
| 4746 | /* For help tags the translation is done in find_help_tags(). |
| 4747 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4748 | if (context == EXPAND_HELP |
| 4749 | || context == EXPAND_COLORS |
| 4750 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4751 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4752 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4753 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4754 | || ((context == EXPAND_TAGS_LISTFILES |
| 4755 | || context == EXPAND_TAGS) |
| 4756 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | retval = vim_strnsave(fname, len); |
| 4758 | else |
| 4759 | { |
| 4760 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4761 | for (i = 0; i < len; i++) |
| 4762 | { |
| 4763 | if (fname[i] == '*' || fname[i] == '~') |
| 4764 | new_len++; /* '*' needs to be replaced by ".*" |
| 4765 | '~' needs to be replaced by "\~" */ |
| 4766 | |
| 4767 | /* Buffer names are like file names. "." should be literal */ |
| 4768 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4769 | new_len++; /* "." becomes "\." */ |
| 4770 | |
| 4771 | /* Custom expansion takes care of special things, match |
| 4772 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4773 | if ((context == EXPAND_USER_DEFINED |
| 4774 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4775 | new_len++; /* '\' becomes "\\" */ |
| 4776 | } |
| 4777 | retval = alloc(new_len); |
| 4778 | if (retval != NULL) |
| 4779 | { |
| 4780 | retval[0] = '^'; |
| 4781 | j = 1; |
| 4782 | for (i = 0; i < len; i++, j++) |
| 4783 | { |
| 4784 | /* Skip backslash. But why? At least keep it for custom |
| 4785 | * expansion. */ |
| 4786 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4787 | && context != EXPAND_USER_LIST |
| 4788 | && fname[i] == '\\' |
| 4789 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4790 | break; |
| 4791 | |
| 4792 | switch (fname[i]) |
| 4793 | { |
| 4794 | case '*': retval[j++] = '.'; |
| 4795 | break; |
| 4796 | case '~': retval[j++] = '\\'; |
| 4797 | break; |
| 4798 | case '?': retval[j] = '.'; |
| 4799 | continue; |
| 4800 | case '.': if (context == EXPAND_BUFFERS) |
| 4801 | retval[j++] = '\\'; |
| 4802 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4803 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4804 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | retval[j++] = '\\'; |
| 4806 | break; |
| 4807 | } |
| 4808 | retval[j] = fname[i]; |
| 4809 | } |
| 4810 | retval[j] = NUL; |
| 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | else |
| 4815 | { |
| 4816 | retval = alloc(len + 4); |
| 4817 | if (retval != NULL) |
| 4818 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4819 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4820 | |
| 4821 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4822 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4823 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4824 | * ~ would be at the start of the file name, but not the tail. |
| 4825 | * $ could be anywhere in the tail. |
| 4826 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4827 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4828 | */ |
| 4829 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4830 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4831 | #ifndef BACKSLASH_IN_FILENAME |
| 4832 | for (i = len - 2; i >= 0; --i) |
| 4833 | { |
| 4834 | if (retval[i] != '\\') |
| 4835 | break; |
| 4836 | ends_in_star = !ends_in_star; |
| 4837 | } |
| 4838 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4839 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4840 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4841 | && vim_strchr(tail, '$') == NULL |
| 4842 | && vim_strchr(retval, '`') == NULL) |
| 4843 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4844 | else if (len > 0 && retval[len - 1] == '$') |
| 4845 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4846 | retval[len] = NUL; |
| 4847 | } |
| 4848 | } |
| 4849 | return retval; |
| 4850 | } |
| 4851 | |
| 4852 | /* |
| 4853 | * Must parse the command line so far to work out what context we are in. |
| 4854 | * Completion can then be done based on that context. |
| 4855 | * This routine sets the variables: |
| 4856 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4857 | * the command line (ends at the cursor). |
| 4858 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4859 | * |
| 4860 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4861 | * the command line, like an unknown command. Caller |
| 4862 | * should beep. |
| 4863 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4864 | * a normal char, rather than for completion. eg |
| 4865 | * :s/^I/ |
| 4866 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4867 | * it. |
| 4868 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4869 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4870 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4871 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4872 | * when we know only directories are of interest. eg |
| 4873 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4874 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4875 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4876 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4877 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4878 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4879 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4880 | * EXPAND_EVENTS Complete event names |
| 4881 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4882 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4883 | * EXPAND_AUGROUP Complete autocommand group names |
| 4884 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4885 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4886 | * eg :unmap a^I , :cunab x^I |
| 4887 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4888 | * eg :call sub^I |
| 4889 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4890 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4891 | * names in expressions, eg :while s^I |
| 4892 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4893 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4894 | */ |
| 4895 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4896 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4897 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4898 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4899 | if (ccline.cmdfirstc != ':' |
| 4900 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4901 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4902 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4903 | #endif |
| 4904 | ) |
| 4905 | { |
| 4906 | xp->xp_context = EXPAND_NOTHING; |
| 4907 | return; |
| 4908 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4909 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4913 | set_cmd_context( |
| 4914 | expand_T *xp, |
| 4915 | char_u *str, /* start of command line */ |
| 4916 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4917 | int col, /* position of cursor */ |
| 4918 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | { |
| 4920 | int old_char = NUL; |
| 4921 | char_u *nextcomm; |
| 4922 | |
| 4923 | /* |
| 4924 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4925 | * written before. |
| 4926 | */ |
| 4927 | if (col < len) |
| 4928 | old_char = str[col]; |
| 4929 | str[col] = NUL; |
| 4930 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4931 | |
| 4932 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4933 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4934 | { |
| 4935 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4936 | /* pass CMD_SIZE because there is no real command */ |
| 4937 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4938 | # endif |
| 4939 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4940 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4941 | { |
| 4942 | xp->xp_context = ccline.xp_context; |
| 4943 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4944 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4945 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4946 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4947 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4948 | else |
| 4949 | #endif |
| 4950 | while (nextcomm != NULL) |
| 4951 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4952 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4953 | /* Store the string here so that call_user_expand_func() can get to them |
| 4954 | * easily. */ |
| 4955 | xp->xp_line = str; |
| 4956 | xp->xp_col = col; |
| 4957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4958 | str[col] = old_char; |
| 4959 | } |
| 4960 | |
| 4961 | /* |
| 4962 | * Expand the command line "str" from context "xp". |
| 4963 | * "xp" must have been set by set_cmd_context(). |
| 4964 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4965 | * starts. |
| 4966 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4967 | * cursor. |
| 4968 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4969 | * key that triggered expansion literally. |
| 4970 | * Returns EXPAND_OK otherwise. |
| 4971 | */ |
| 4972 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4973 | expand_cmdline( |
| 4974 | expand_T *xp, |
| 4975 | char_u *str, /* start of command line */ |
| 4976 | int col, /* position of cursor */ |
| 4977 | int *matchcount, /* return: nr of matches */ |
| 4978 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | { |
| 4980 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4981 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | |
| 4983 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4984 | { |
| 4985 | beep_flush(); |
| 4986 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4987 | } |
| 4988 | if (xp->xp_context == EXPAND_NOTHING) |
| 4989 | { |
| 4990 | /* Caller can use the character as a normal char instead */ |
| 4991 | return EXPAND_NOTHING; |
| 4992 | } |
| 4993 | |
| 4994 | /* 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] | 4995 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4996 | 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] | 4997 | if (file_str == NULL) |
| 4998 | return EXPAND_UNSUCCESSFUL; |
| 4999 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5000 | if (p_wic) |
| 5001 | options += WILD_ICASE; |
| 5002 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5004 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5005 | { |
| 5006 | *matchcount = 0; |
| 5007 | *matches = NULL; |
| 5008 | } |
| 5009 | vim_free(file_str); |
| 5010 | |
| 5011 | return EXPAND_OK; |
| 5012 | } |
| 5013 | |
| 5014 | #ifdef FEAT_MULTI_LANG |
| 5015 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5016 | * Cleanup matches for help tags: |
| 5017 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 5018 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5020 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5021 | |
| 5022 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5023 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5024 | { |
| 5025 | int i, j; |
| 5026 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5027 | char_u buf[4]; |
| 5028 | char_u *p = buf; |
| 5029 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5030 | 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] | 5031 | { |
| 5032 | *p++ = '@'; |
| 5033 | *p++ = p_hlg[0]; |
| 5034 | *p++ = p_hlg[1]; |
| 5035 | } |
| 5036 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5037 | |
| 5038 | for (i = 0; i < num_file; ++i) |
| 5039 | { |
| 5040 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 5041 | if (len <= 0) |
| 5042 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5043 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | { |
| 5045 | /* Sorting on priority means the same item in another language may |
| 5046 | * be anywhere. Search all items for a match up to the "@en". */ |
| 5047 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5048 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 5049 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | break; |
| 5051 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 5052 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5053 | file[i][len] = NUL; |
| 5054 | } |
| 5055 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 5056 | |
| 5057 | if (*buf != NUL) |
| 5058 | for (i = 0; i < num_file; ++i) |
| 5059 | { |
| 5060 | len = (int)STRLEN(file[i]) - 3; |
| 5061 | if (len <= 0) |
| 5062 | continue; |
| 5063 | if (STRCMP(file[i] + len, buf) == 0) |
| 5064 | { |
| 5065 | /* remove the default language */ |
| 5066 | file[i][len] = NUL; |
| 5067 | } |
| 5068 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | } |
| 5070 | #endif |
| 5071 | |
| 5072 | /* |
| 5073 | * Do the expansion based on xp->xp_context and "pat". |
| 5074 | */ |
| 5075 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5076 | ExpandFromContext( |
| 5077 | expand_T *xp, |
| 5078 | char_u *pat, |
| 5079 | int *num_file, |
| 5080 | char_u ***file, |
| 5081 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5082 | { |
| 5083 | #ifdef FEAT_CMDL_COMPL |
| 5084 | regmatch_T regmatch; |
| 5085 | #endif |
| 5086 | int ret; |
| 5087 | int flags; |
| 5088 | |
| 5089 | flags = EW_DIR; /* include directories */ |
| 5090 | if (options & WILD_LIST_NOTFOUND) |
| 5091 | flags |= EW_NOTFOUND; |
| 5092 | if (options & WILD_ADD_SLASH) |
| 5093 | flags |= EW_ADDSLASH; |
| 5094 | if (options & WILD_KEEP_ALL) |
| 5095 | flags |= EW_KEEPALL; |
| 5096 | if (options & WILD_SILENT) |
| 5097 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 5098 | if (options & WILD_ALLLINKS) |
| 5099 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5100 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5101 | if (xp->xp_context == EXPAND_FILES |
| 5102 | || xp->xp_context == EXPAND_DIRECTORIES |
| 5103 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5104 | { |
| 5105 | /* |
| 5106 | * Expand file or directory names. |
| 5107 | */ |
| 5108 | int free_pat = FALSE; |
| 5109 | int i; |
| 5110 | |
| 5111 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5112 | * space */ |
| 5113 | if (xp->xp_backslash != XP_BS_NONE) |
| 5114 | { |
| 5115 | free_pat = TRUE; |
| 5116 | pat = vim_strsave(pat); |
| 5117 | for (i = 0; pat[i]; ++i) |
| 5118 | if (pat[i] == '\\') |
| 5119 | { |
| 5120 | if (xp->xp_backslash == XP_BS_THREE |
| 5121 | && pat[i + 1] == '\\' |
| 5122 | && pat[i + 2] == '\\' |
| 5123 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5124 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5125 | if (xp->xp_backslash == XP_BS_ONE |
| 5126 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5127 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | if (xp->xp_context == EXPAND_FILES) |
| 5132 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 5133 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 5134 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | else |
| 5136 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 5137 | if (options & WILD_ICASE) |
| 5138 | flags |= EW_ICASE; |
| 5139 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 5140 | /* Expand wildcards, supporting %:h and the like. */ |
| 5141 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5142 | if (free_pat) |
| 5143 | vim_free(pat); |
| 5144 | return ret; |
| 5145 | } |
| 5146 | |
| 5147 | *file = (char_u **)""; |
| 5148 | *num_file = 0; |
| 5149 | if (xp->xp_context == EXPAND_HELP) |
| 5150 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 5151 | /* With an empty argument we would get all the help tags, which is |
| 5152 | * very slow. Get matches for "help" instead. */ |
| 5153 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 5154 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5155 | { |
| 5156 | #ifdef FEAT_MULTI_LANG |
| 5157 | cleanup_help_tags(*num_file, *file); |
| 5158 | #endif |
| 5159 | return OK; |
| 5160 | } |
| 5161 | return FAIL; |
| 5162 | } |
| 5163 | |
| 5164 | #ifndef FEAT_CMDL_COMPL |
| 5165 | return FAIL; |
| 5166 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5167 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 5168 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5169 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 5170 | return ExpandOldSetting(num_file, file); |
| 5171 | if (xp->xp_context == EXPAND_BUFFERS) |
| 5172 | return ExpandBufnames(pat, num_file, file, options); |
| 5173 | if (xp->xp_context == EXPAND_TAGS |
| 5174 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 5175 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 5176 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5177 | { |
| 5178 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5179 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 5180 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5181 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5182 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5183 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 5184 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5185 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5186 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5187 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5188 | { |
| 5189 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5190 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5191 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5192 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5193 | { |
| 5194 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5195 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5196 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5197 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5198 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 5199 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5200 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5201 | if (xp->xp_context == EXPAND_PACKADD) |
| 5202 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | |
| 5204 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5205 | if (regmatch.regprog == NULL) |
| 5206 | return FAIL; |
| 5207 | |
| 5208 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 5209 | regmatch.rm_ic = ignorecase(pat); |
| 5210 | |
| 5211 | if (xp->xp_context == EXPAND_SETTINGS |
| 5212 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 5213 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 5214 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 5215 | ret = ExpandMappings(®match, num_file, file); |
| 5216 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5217 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 5218 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 5219 | # endif |
| 5220 | else |
| 5221 | { |
| 5222 | static struct expgen |
| 5223 | { |
| 5224 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 5225 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5227 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5228 | } tab[] = |
| 5229 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5230 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 5231 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 5232 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 5233 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5234 | #ifdef FEAT_CMDHIST |
| 5235 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 5236 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5237 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5238 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 5239 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5240 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 5241 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 5242 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | #endif |
| 5244 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5245 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 5246 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 5247 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 5248 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5249 | #endif |
| 5250 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5251 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 5252 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | #endif |
| 5254 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5255 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5256 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 5257 | #ifdef FEAT_PROFILE |
| 5258 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 5259 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5260 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5261 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 5262 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5263 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5264 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 5265 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5266 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5267 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 5268 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5269 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5270 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5271 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5273 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5274 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5275 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5276 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5277 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5278 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5279 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5280 | }; |
| 5281 | int i; |
| 5282 | |
| 5283 | /* |
| 5284 | * Find a context in the table and call the ExpandGeneric() with the |
| 5285 | * right function to do the expansion. |
| 5286 | */ |
| 5287 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5288 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | if (xp->xp_context == tab[i].context) |
| 5290 | { |
| 5291 | if (tab[i].ic) |
| 5292 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5293 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5294 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5295 | break; |
| 5296 | } |
| 5297 | } |
| 5298 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5299 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5300 | |
| 5301 | return ret; |
| 5302 | #endif /* FEAT_CMDL_COMPL */ |
| 5303 | } |
| 5304 | |
| 5305 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5306 | /* |
| 5307 | * Expand a list of names. |
| 5308 | * |
| 5309 | * Generic function for command line completion. It calls a function to |
| 5310 | * obtain strings, one by one. The strings are matched against a regexp |
| 5311 | * program. Matching strings are copied into an array, which is returned. |
| 5312 | * |
| 5313 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5314 | */ |
| 5315 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5316 | ExpandGeneric( |
| 5317 | expand_T *xp, |
| 5318 | regmatch_T *regmatch, |
| 5319 | int *num_file, |
| 5320 | char_u ***file, |
| 5321 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5322 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5323 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5324 | { |
| 5325 | int i; |
| 5326 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5327 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | char_u *str; |
| 5329 | |
| 5330 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5331 | * round == 0: count the number of matching names |
| 5332 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5334 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5335 | { |
| 5336 | for (i = 0; ; ++i) |
| 5337 | { |
| 5338 | str = (*func)(xp, i); |
| 5339 | if (str == NULL) /* end of list */ |
| 5340 | break; |
| 5341 | if (*str == NUL) /* skip empty strings */ |
| 5342 | continue; |
| 5343 | |
| 5344 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5345 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5346 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5348 | if (escaped) |
| 5349 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5350 | else |
| 5351 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5352 | (*file)[count] = str; |
| 5353 | #ifdef FEAT_MENU |
| 5354 | if (func == get_menu_names && str != NULL) |
| 5355 | { |
| 5356 | /* test for separator added by get_menu_names() */ |
| 5357 | str += STRLEN(str) - 1; |
| 5358 | if (*str == '\001') |
| 5359 | *str = '.'; |
| 5360 | } |
| 5361 | #endif |
| 5362 | } |
| 5363 | ++count; |
| 5364 | } |
| 5365 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5366 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5367 | { |
| 5368 | if (count == 0) |
| 5369 | return OK; |
| 5370 | *num_file = count; |
| 5371 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5372 | if (*file == NULL) |
| 5373 | { |
| 5374 | *file = (char_u **)""; |
| 5375 | return FAIL; |
| 5376 | } |
| 5377 | count = 0; |
| 5378 | } |
| 5379 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5380 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5381 | /* Sort the results. Keep menu's in the specified order. */ |
| 5382 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5383 | { |
| 5384 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5385 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5386 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5387 | /* <SNR> functions should be sorted to the end. */ |
| 5388 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5389 | sort_func_compare); |
| 5390 | else |
| 5391 | sort_strings(*file, *num_file); |
| 5392 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5393 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5394 | #ifdef FEAT_CMDL_COMPL |
| 5395 | /* Reset the variables used for special highlight names expansion, so that |
| 5396 | * they don't show up when getting normal highlight names by ID. */ |
| 5397 | reset_expand_highlight(); |
| 5398 | #endif |
| 5399 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5400 | return OK; |
| 5401 | } |
| 5402 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5403 | /* |
| 5404 | * Complete a shell command. |
| 5405 | * Returns FAIL or OK; |
| 5406 | */ |
| 5407 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5408 | expand_shellcmd( |
| 5409 | char_u *filepat, /* pattern to match with command names */ |
| 5410 | int *num_file, /* return: number of matches */ |
| 5411 | char_u ***file, /* return: array with matches */ |
| 5412 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5413 | { |
| 5414 | char_u *pat; |
| 5415 | int i; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5416 | char_u *path = NULL; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5417 | int mustfree = FALSE; |
| 5418 | garray_T ga; |
| 5419 | char_u *buf = alloc(MAXPATHL); |
| 5420 | size_t l; |
| 5421 | char_u *s, *e; |
| 5422 | int flags = flagsarg; |
| 5423 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5424 | int did_curdir = FALSE; |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5425 | hashtab_T found_ht; |
| 5426 | hashitem_T *hi; |
| 5427 | hash_T hash; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5428 | |
| 5429 | if (buf == NULL) |
| 5430 | return FAIL; |
| 5431 | |
| 5432 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5433 | * space */ |
| 5434 | pat = vim_strsave(filepat); |
| 5435 | for (i = 0; pat[i]; ++i) |
| 5436 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5437 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5438 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5439 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5440 | |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5441 | if (pat[0] == '.' && (vim_ispathsep(pat[1]) |
| 5442 | || (pat[1] == '.' && vim_ispathsep(pat[2])))) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5443 | path = (char_u *)"."; |
| 5444 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5445 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5446 | /* For an absolute name we don't use $PATH. */ |
| 5447 | if (!mch_isFullName(pat)) |
| 5448 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5449 | if (path == NULL) |
| 5450 | path = (char_u *)""; |
| 5451 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5452 | |
| 5453 | /* |
| 5454 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5455 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5456 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5457 | */ |
| 5458 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5459 | hash_init(&found_ht); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5460 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5461 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5462 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5463 | e = vim_strchr(s, ';'); |
| 5464 | #else |
| 5465 | e = vim_strchr(s, ':'); |
| 5466 | #endif |
| 5467 | if (e == NULL) |
| 5468 | e = s + STRLEN(s); |
| 5469 | |
Bram Moolenaar | 6ab9e42 | 2018-07-28 19:20:13 +0200 | [diff] [blame] | 5470 | if (*s == NUL) |
| 5471 | { |
| 5472 | if (did_curdir) |
| 5473 | break; |
| 5474 | // Find directories in the current directory, path is empty. |
| 5475 | did_curdir = TRUE; |
| 5476 | flags |= EW_DIR; |
| 5477 | } |
| 5478 | else if (STRNCMP(s, ".", (int)(e - s)) == 0) |
| 5479 | { |
| 5480 | did_curdir = TRUE; |
| 5481 | flags |= EW_DIR; |
| 5482 | } |
| 5483 | else |
| 5484 | // Do not match directories inside a $PATH item. |
| 5485 | flags &= ~EW_DIR; |
| 5486 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5487 | l = e - s; |
| 5488 | if (l > MAXPATHL - 5) |
| 5489 | break; |
| 5490 | vim_strncpy(buf, s, l); |
| 5491 | add_pathsep(buf); |
| 5492 | l = STRLEN(buf); |
| 5493 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5494 | |
| 5495 | /* Expand matches in one directory of $PATH. */ |
| 5496 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5497 | if (ret == OK) |
| 5498 | { |
| 5499 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5500 | FreeWild(*num_file, *file); |
| 5501 | else |
| 5502 | { |
| 5503 | for (i = 0; i < *num_file; ++i) |
| 5504 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5505 | char_u *name = (*file)[i]; |
| 5506 | |
| 5507 | if (STRLEN(name) > l) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5508 | { |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5509 | // Check if this name was already found. |
| 5510 | hash = hash_hash(name + l); |
| 5511 | hi = hash_lookup(&found_ht, name + l, hash); |
| 5512 | if (HASHITEM_EMPTY(hi)) |
| 5513 | { |
| 5514 | // Remove the path that was prepended. |
| 5515 | STRMOVE(name, name + l); |
| 5516 | ((char_u **)ga.ga_data)[ga.ga_len++] = name; |
| 5517 | hash_add_item(&found_ht, hi, name, hash); |
| 5518 | name = NULL; |
| 5519 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5520 | } |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5521 | vim_free(name); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5522 | } |
| 5523 | vim_free(*file); |
| 5524 | } |
| 5525 | } |
| 5526 | if (*e != NUL) |
| 5527 | ++e; |
| 5528 | } |
| 5529 | *file = ga.ga_data; |
| 5530 | *num_file = ga.ga_len; |
| 5531 | |
| 5532 | vim_free(buf); |
| 5533 | vim_free(pat); |
| 5534 | if (mustfree) |
| 5535 | vim_free(path); |
Bram Moolenaar | 62fe66f | 2018-05-22 16:58:47 +0200 | [diff] [blame] | 5536 | hash_clear(&found_ht); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5537 | return OK; |
| 5538 | } |
| 5539 | |
| 5540 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5541 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 5542 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5543 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5544 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5545 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5546 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5547 | call_user_expand_func( |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5548 | void *(*user_expand_func)(char_u *, int, typval_T *), |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5549 | expand_T *xp, |
| 5550 | int *num_file, |
| 5551 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5552 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5553 | int keep = 0; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5554 | typval_T args[4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5555 | int save_current_SID = current_SID; |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5556 | char_u *pat = NULL; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5557 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5558 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5559 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5560 | 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] | 5561 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | *num_file = 0; |
| 5563 | *file = NULL; |
| 5564 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5565 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5566 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5567 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5568 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5569 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5570 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5571 | pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
| 5572 | |
| 5573 | args[0].v_type = VAR_STRING; |
| 5574 | args[0].vval.v_string = pat; |
| 5575 | args[1].v_type = VAR_STRING; |
| 5576 | args[1].vval.v_string = xp->xp_line; |
| 5577 | args[2].v_type = VAR_NUMBER; |
| 5578 | args[2].vval.v_number = xp->xp_col; |
| 5579 | args[3].v_type = VAR_UNKNOWN; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5580 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5581 | /* Save the cmdline, we don't know what the function may do. */ |
| 5582 | save_ccline = ccline; |
| 5583 | ccline.cmdbuff = NULL; |
| 5584 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5585 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5586 | |
Bram Moolenaar | ded27a1 | 2018-08-01 19:06:03 +0200 | [diff] [blame] | 5587 | ret = user_expand_func(xp->xp_arg, 3, args); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5588 | |
| 5589 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5590 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5591 | if (ccline.cmdbuff != NULL) |
| 5592 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5593 | |
Bram Moolenaar | ffa9684 | 2018-06-12 22:05:14 +0200 | [diff] [blame] | 5594 | vim_free(pat); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5595 | return ret; |
| 5596 | } |
| 5597 | |
| 5598 | /* |
| 5599 | * Expand names with a function defined by the user. |
| 5600 | */ |
| 5601 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5602 | ExpandUserDefined( |
| 5603 | expand_T *xp, |
| 5604 | regmatch_T *regmatch, |
| 5605 | int *num_file, |
| 5606 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5607 | { |
| 5608 | char_u *retstr; |
| 5609 | char_u *s; |
| 5610 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5611 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5612 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5613 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5614 | |
| 5615 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5616 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5617 | return FAIL; |
| 5618 | |
| 5619 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5620 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5621 | { |
| 5622 | e = vim_strchr(s, '\n'); |
| 5623 | if (e == NULL) |
| 5624 | e = s + STRLEN(s); |
| 5625 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5626 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5627 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5628 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5629 | *e = keep; |
| 5630 | |
| 5631 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5632 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5633 | if (ga_grow(&ga, 1) == FAIL) |
| 5634 | break; |
| 5635 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5636 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5639 | if (*e != NUL) |
| 5640 | ++e; |
| 5641 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5642 | vim_free(retstr); |
| 5643 | *file = ga.ga_data; |
| 5644 | *num_file = ga.ga_len; |
| 5645 | return OK; |
| 5646 | } |
| 5647 | |
| 5648 | /* |
| 5649 | * Expand names with a list returned by a function defined by the user. |
| 5650 | */ |
| 5651 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5652 | ExpandUserList( |
| 5653 | expand_T *xp, |
| 5654 | int *num_file, |
| 5655 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5656 | { |
| 5657 | list_T *retlist; |
| 5658 | listitem_T *li; |
| 5659 | garray_T ga; |
| 5660 | |
| 5661 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5662 | if (retlist == NULL) |
| 5663 | return FAIL; |
| 5664 | |
| 5665 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5666 | /* Loop over the items in the list. */ |
| 5667 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5668 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5669 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5670 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5671 | |
| 5672 | if (ga_grow(&ga, 1) == FAIL) |
| 5673 | break; |
| 5674 | |
| 5675 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5676 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5677 | ++ga.ga_len; |
| 5678 | } |
| 5679 | list_unref(retlist); |
| 5680 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5681 | *file = ga.ga_data; |
| 5682 | *num_file = ga.ga_len; |
| 5683 | return OK; |
| 5684 | } |
| 5685 | #endif |
| 5686 | |
| 5687 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5688 | * Expand color scheme, compiler or filetype names. |
| 5689 | * Search from 'runtimepath': |
| 5690 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5691 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5692 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5693 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5694 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5695 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5696 | */ |
| 5697 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5698 | ExpandRTDir( |
| 5699 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5700 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5701 | int *num_file, |
| 5702 | char_u ***file, |
| 5703 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5704 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5705 | char_u *s; |
| 5706 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5707 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5708 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5709 | int i; |
| 5710 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5711 | |
| 5712 | *num_file = 0; |
| 5713 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5714 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5715 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5716 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5717 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5718 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5719 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5720 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5721 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5722 | ga_clear_strings(&ga); |
| 5723 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5725 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5726 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5727 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5728 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5729 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5730 | if (flags & DIP_START) { |
| 5731 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5732 | { |
| 5733 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5734 | if (s == NULL) |
| 5735 | { |
| 5736 | ga_clear_strings(&ga); |
| 5737 | return FAIL; |
| 5738 | } |
| 5739 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5740 | globpath(p_pp, s, &ga, 0); |
| 5741 | vim_free(s); |
| 5742 | } |
| 5743 | } |
| 5744 | |
| 5745 | if (flags & DIP_OPT) { |
| 5746 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5747 | { |
| 5748 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5749 | if (s == NULL) |
| 5750 | { |
| 5751 | ga_clear_strings(&ga); |
| 5752 | return FAIL; |
| 5753 | } |
| 5754 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5755 | globpath(p_pp, s, &ga, 0); |
| 5756 | vim_free(s); |
| 5757 | } |
| 5758 | } |
| 5759 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5760 | for (i = 0; i < ga.ga_len; ++i) |
| 5761 | { |
| 5762 | match = ((char_u **)ga.ga_data)[i]; |
| 5763 | s = match; |
| 5764 | e = s + STRLEN(s); |
| 5765 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5766 | { |
| 5767 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5768 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5769 | if (s < match || vim_ispathsep(*s)) |
| 5770 | break; |
| 5771 | ++s; |
| 5772 | *e = NUL; |
| 5773 | mch_memmove(match, s, e - s + 1); |
| 5774 | } |
| 5775 | } |
| 5776 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5777 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5778 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5781 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5782 | remove_duplicates(&ga); |
| 5783 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5784 | *file = ga.ga_data; |
| 5785 | *num_file = ga.ga_len; |
| 5786 | return OK; |
| 5787 | } |
| 5788 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5789 | /* |
| 5790 | * Expand loadplugin names: |
| 5791 | * 'packpath'/pack/ * /opt/{pat} |
| 5792 | */ |
| 5793 | static int |
| 5794 | ExpandPackAddDir( |
| 5795 | char_u *pat, |
| 5796 | int *num_file, |
| 5797 | char_u ***file) |
| 5798 | { |
| 5799 | char_u *s; |
| 5800 | char_u *e; |
| 5801 | char_u *match; |
| 5802 | garray_T ga; |
| 5803 | int i; |
| 5804 | int pat_len; |
| 5805 | |
| 5806 | *num_file = 0; |
| 5807 | *file = NULL; |
| 5808 | pat_len = (int)STRLEN(pat); |
| 5809 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5810 | |
| 5811 | s = alloc((unsigned)(pat_len + 26)); |
| 5812 | if (s == NULL) |
| 5813 | { |
| 5814 | ga_clear_strings(&ga); |
| 5815 | return FAIL; |
| 5816 | } |
| 5817 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5818 | globpath(p_pp, s, &ga, 0); |
| 5819 | vim_free(s); |
| 5820 | |
| 5821 | for (i = 0; i < ga.ga_len; ++i) |
| 5822 | { |
| 5823 | match = ((char_u **)ga.ga_data)[i]; |
| 5824 | s = gettail(match); |
| 5825 | e = s + STRLEN(s); |
| 5826 | mch_memmove(match, s, e - s + 1); |
| 5827 | } |
| 5828 | |
| 5829 | if (ga.ga_len == 0) |
| 5830 | return FAIL; |
| 5831 | |
| 5832 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5833 | * directories in dirnames. */ |
| 5834 | remove_duplicates(&ga); |
| 5835 | |
| 5836 | *file = ga.ga_data; |
| 5837 | *num_file = ga.ga_len; |
| 5838 | return OK; |
| 5839 | } |
| 5840 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | #endif |
| 5842 | |
| 5843 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5844 | /* |
| 5845 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5846 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5847 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5848 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5849 | globpath( |
| 5850 | char_u *path, |
| 5851 | char_u *file, |
| 5852 | garray_T *ga, |
| 5853 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5854 | { |
| 5855 | expand_T xpc; |
| 5856 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5857 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5858 | int num_p; |
| 5859 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5860 | |
| 5861 | buf = alloc(MAXPATHL); |
| 5862 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5863 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5864 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5865 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5866 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5867 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5868 | /* Loop over all entries in {path}. */ |
| 5869 | while (*path != NUL) |
| 5870 | { |
| 5871 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5872 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5873 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5874 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5875 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5876 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5877 | * treat it as an escape character, use '/' instead. */ |
| 5878 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5879 | STRCAT(buf, "/"); |
| 5880 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5882 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5883 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5884 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5885 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5887 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5888 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5889 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5890 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5891 | for (i = 0; i < num_p; ++i) |
| 5892 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5893 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5894 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5895 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5896 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5897 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5898 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5899 | FreeWild(num_p, p); |
| 5900 | } |
| 5901 | } |
| 5902 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5903 | |
| 5904 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | #endif |
| 5908 | |
| 5909 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5910 | |
| 5911 | /********************************* |
| 5912 | * Command line history stuff * |
| 5913 | *********************************/ |
| 5914 | |
| 5915 | /* |
| 5916 | * Translate a history character to the associated type number. |
| 5917 | */ |
| 5918 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5919 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | { |
| 5921 | if (c == ':') |
| 5922 | return HIST_CMD; |
| 5923 | if (c == '=') |
| 5924 | return HIST_EXPR; |
| 5925 | if (c == '@') |
| 5926 | return HIST_INPUT; |
| 5927 | if (c == '>') |
| 5928 | return HIST_DEBUG; |
| 5929 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5930 | } |
| 5931 | |
| 5932 | /* |
| 5933 | * Table of history names. |
| 5934 | * These names are used in :history and various hist...() functions. |
| 5935 | * It is sufficient to give the significant prefix of a history name. |
| 5936 | */ |
| 5937 | |
| 5938 | static char *(history_names[]) = |
| 5939 | { |
| 5940 | "cmd", |
| 5941 | "search", |
| 5942 | "expr", |
| 5943 | "input", |
| 5944 | "debug", |
| 5945 | NULL |
| 5946 | }; |
| 5947 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5948 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5949 | /* |
| 5950 | * Function given to ExpandGeneric() to obtain the possible first |
| 5951 | * arguments of the ":history command. |
| 5952 | */ |
| 5953 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5954 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5955 | { |
| 5956 | static char_u compl[2] = { NUL, NUL }; |
| 5957 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5958 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5959 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5960 | |
| 5961 | if (idx < short_names_count) |
| 5962 | { |
| 5963 | compl[0] = (char_u)short_names[idx]; |
| 5964 | return compl; |
| 5965 | } |
| 5966 | if (idx < short_names_count + history_name_count) |
| 5967 | return (char_u *)history_names[idx - short_names_count]; |
| 5968 | if (idx == short_names_count + history_name_count) |
| 5969 | return (char_u *)"all"; |
| 5970 | return NULL; |
| 5971 | } |
| 5972 | #endif |
| 5973 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5974 | /* |
| 5975 | * init_history() - Initialize the command line history. |
| 5976 | * Also used to re-allocate the history when the size changes. |
| 5977 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5978 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5979 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5980 | { |
| 5981 | int newlen; /* new length of history table */ |
| 5982 | histentry_T *temp; |
| 5983 | int i; |
| 5984 | int j; |
| 5985 | int type; |
| 5986 | |
| 5987 | /* |
| 5988 | * If size of history table changed, reallocate it |
| 5989 | */ |
| 5990 | newlen = (int)p_hi; |
| 5991 | if (newlen != hislen) /* history length changed */ |
| 5992 | { |
| 5993 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5994 | { |
| 5995 | if (newlen) |
| 5996 | { |
| 5997 | temp = (histentry_T *)lalloc( |
| 5998 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5999 | if (temp == NULL) /* out of memory! */ |
| 6000 | { |
| 6001 | if (type == 0) /* first one: just keep the old length */ |
| 6002 | { |
| 6003 | newlen = hislen; |
| 6004 | break; |
| 6005 | } |
| 6006 | /* Already changed one table, now we can only have zero |
| 6007 | * length for all tables. */ |
| 6008 | newlen = 0; |
| 6009 | type = -1; |
| 6010 | continue; |
| 6011 | } |
| 6012 | } |
| 6013 | else |
| 6014 | temp = NULL; |
| 6015 | if (newlen == 0 || temp != NULL) |
| 6016 | { |
| 6017 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 6018 | { |
| 6019 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6020 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6021 | } |
| 6022 | else if (newlen > hislen) /* array becomes bigger */ |
| 6023 | { |
| 6024 | for (i = 0; i <= hisidx[type]; ++i) |
| 6025 | temp[i] = history[type][i]; |
| 6026 | j = i; |
| 6027 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6028 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6029 | for ( ; j < hislen; ++i, ++j) |
| 6030 | temp[i] = history[type][j]; |
| 6031 | } |
| 6032 | else /* array becomes smaller or 0 */ |
| 6033 | { |
| 6034 | j = hisidx[type]; |
| 6035 | for (i = newlen - 1; ; --i) |
| 6036 | { |
| 6037 | if (i >= 0) /* copy newest entries */ |
| 6038 | temp[i] = history[type][j]; |
| 6039 | else /* remove older entries */ |
| 6040 | vim_free(history[type][j].hisstr); |
| 6041 | if (--j < 0) |
| 6042 | j = hislen - 1; |
| 6043 | if (j == hisidx[type]) |
| 6044 | break; |
| 6045 | } |
| 6046 | hisidx[type] = newlen - 1; |
| 6047 | } |
| 6048 | vim_free(history[type]); |
| 6049 | history[type] = temp; |
| 6050 | } |
| 6051 | } |
| 6052 | hislen = newlen; |
| 6053 | } |
| 6054 | } |
| 6055 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6056 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6057 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6058 | { |
| 6059 | hisptr->hisnum = 0; |
| 6060 | hisptr->viminfo = FALSE; |
| 6061 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6062 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6063 | } |
| 6064 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6065 | /* |
| 6066 | * Check if command line 'str' is already in history. |
| 6067 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 6068 | */ |
| 6069 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6070 | in_history( |
| 6071 | int type, |
| 6072 | char_u *str, |
| 6073 | int move_to_front, /* Move the entry to the front if it exists */ |
| 6074 | int sep, |
| 6075 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | { |
| 6077 | int i; |
| 6078 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6079 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6080 | |
| 6081 | if (hisidx[type] < 0) |
| 6082 | return FALSE; |
| 6083 | i = hisidx[type]; |
| 6084 | do |
| 6085 | { |
| 6086 | if (history[type][i].hisstr == NULL) |
| 6087 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6088 | |
| 6089 | /* For search history, check that the separator character matches as |
| 6090 | * well. */ |
| 6091 | p = history[type][i].hisstr; |
| 6092 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6093 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 6094 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6095 | { |
| 6096 | if (!move_to_front) |
| 6097 | return TRUE; |
| 6098 | last_i = i; |
| 6099 | break; |
| 6100 | } |
| 6101 | if (--i < 0) |
| 6102 | i = hislen - 1; |
| 6103 | } while (i != hisidx[type]); |
| 6104 | |
| 6105 | if (last_i >= 0) |
| 6106 | { |
| 6107 | str = history[type][i].hisstr; |
| 6108 | while (i != hisidx[type]) |
| 6109 | { |
| 6110 | if (++i >= hislen) |
| 6111 | i = 0; |
| 6112 | history[type][last_i] = history[type][i]; |
| 6113 | last_i = i; |
| 6114 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6116 | history[type][i].viminfo = FALSE; |
| 6117 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6118 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6119 | return TRUE; |
| 6120 | } |
| 6121 | return FALSE; |
| 6122 | } |
| 6123 | |
| 6124 | /* |
| 6125 | * Convert history name (from table above) to its HIST_ equivalent. |
| 6126 | * When "name" is empty, return "cmd" history. |
| 6127 | * Returns -1 for unknown history name. |
| 6128 | */ |
| 6129 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6130 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6131 | { |
| 6132 | int i; |
| 6133 | int len = (int)STRLEN(name); |
| 6134 | |
| 6135 | /* No argument: use current history. */ |
| 6136 | if (len == 0) |
| 6137 | return hist_char2type(ccline.cmdfirstc); |
| 6138 | |
| 6139 | for (i = 0; history_names[i] != NULL; ++i) |
| 6140 | if (STRNICMP(name, history_names[i], len) == 0) |
| 6141 | return i; |
| 6142 | |
| 6143 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 6144 | return hist_char2type(name[0]); |
| 6145 | |
| 6146 | return -1; |
| 6147 | } |
| 6148 | |
| 6149 | static int last_maptick = -1; /* last seen maptick */ |
| 6150 | |
| 6151 | /* |
| 6152 | * Add the given string to the given history. If the string is already in the |
| 6153 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 6154 | * values. |
| 6155 | */ |
| 6156 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6157 | add_to_history( |
| 6158 | int histype, |
| 6159 | char_u *new_entry, |
| 6160 | int in_map, /* consider maptick when inside a mapping */ |
| 6161 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6162 | { |
| 6163 | histentry_T *hisptr; |
| 6164 | int len; |
| 6165 | |
| 6166 | if (hislen == 0) /* no history */ |
| 6167 | return; |
| 6168 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 6169 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 6170 | return; |
| 6171 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6172 | /* |
| 6173 | * Searches inside the same mapping overwrite each other, so that only |
| 6174 | * the last line is kept. Be careful not to remove a line that was moved |
| 6175 | * down, only lines that were added. |
| 6176 | */ |
| 6177 | if (histype == HIST_SEARCH && in_map) |
| 6178 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 6179 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6180 | { |
| 6181 | /* Current line is from the same mapping, remove it */ |
| 6182 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 6183 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6184 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6185 | --hisnum[histype]; |
| 6186 | if (--hisidx[HIST_SEARCH] < 0) |
| 6187 | hisidx[HIST_SEARCH] = hislen - 1; |
| 6188 | } |
| 6189 | last_maptick = -1; |
| 6190 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6191 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6192 | { |
| 6193 | if (++hisidx[histype] == hislen) |
| 6194 | hisidx[histype] = 0; |
| 6195 | hisptr = &history[histype][hisidx[histype]]; |
| 6196 | vim_free(hisptr->hisstr); |
| 6197 | |
| 6198 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6199 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6200 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 6201 | if (hisptr->hisstr != NULL) |
| 6202 | hisptr->hisstr[len + 1] = sep; |
| 6203 | |
| 6204 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6205 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6206 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6207 | if (histype == HIST_SEARCH && in_map) |
| 6208 | last_maptick = maptick; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 6213 | |
| 6214 | /* |
| 6215 | * Get identifier of newest history entry. |
| 6216 | * "histype" may be one of the HIST_ values. |
| 6217 | */ |
| 6218 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6219 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6220 | { |
| 6221 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6222 | || hisidx[histype] < 0) |
| 6223 | return -1; |
| 6224 | |
| 6225 | return history[histype][hisidx[histype]].hisnum; |
| 6226 | } |
| 6227 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 6228 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6229 | * Calculate history index from a number: |
| 6230 | * num > 0: seen as identifying number of a history entry |
| 6231 | * num < 0: relative position in history wrt newest entry |
| 6232 | * "histype" may be one of the HIST_ values. |
| 6233 | */ |
| 6234 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6235 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6236 | { |
| 6237 | int i; |
| 6238 | histentry_T *hist; |
| 6239 | int wrapped = FALSE; |
| 6240 | |
| 6241 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 6242 | || (i = hisidx[histype]) < 0 || num == 0) |
| 6243 | return -1; |
| 6244 | |
| 6245 | hist = history[histype]; |
| 6246 | if (num > 0) |
| 6247 | { |
| 6248 | while (hist[i].hisnum > num) |
| 6249 | if (--i < 0) |
| 6250 | { |
| 6251 | if (wrapped) |
| 6252 | break; |
| 6253 | i += hislen; |
| 6254 | wrapped = TRUE; |
| 6255 | } |
| 6256 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 6257 | return i; |
| 6258 | } |
| 6259 | else if (-num <= hislen) |
| 6260 | { |
| 6261 | i += num + 1; |
| 6262 | if (i < 0) |
| 6263 | i += hislen; |
| 6264 | if (hist[i].hisstr != NULL) |
| 6265 | return i; |
| 6266 | } |
| 6267 | return -1; |
| 6268 | } |
| 6269 | |
| 6270 | /* |
| 6271 | * Get a history entry by its index. |
| 6272 | * "histype" may be one of the HIST_ values. |
| 6273 | */ |
| 6274 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6275 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6276 | { |
| 6277 | idx = calc_hist_idx(histype, idx); |
| 6278 | if (idx >= 0) |
| 6279 | return history[histype][idx].hisstr; |
| 6280 | else |
| 6281 | return (char_u *)""; |
| 6282 | } |
| 6283 | |
| 6284 | /* |
| 6285 | * Clear all entries of a history. |
| 6286 | * "histype" may be one of the HIST_ values. |
| 6287 | */ |
| 6288 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6289 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6290 | { |
| 6291 | int i; |
| 6292 | histentry_T *hisptr; |
| 6293 | |
| 6294 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6295 | { |
| 6296 | hisptr = history[histype]; |
| 6297 | for (i = hislen; i--;) |
| 6298 | { |
| 6299 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6300 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6301 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6302 | } |
| 6303 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6304 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6305 | return OK; |
| 6306 | } |
| 6307 | return FAIL; |
| 6308 | } |
| 6309 | |
| 6310 | /* |
| 6311 | * Remove all entries matching {str} from a history. |
| 6312 | * "histype" may be one of the HIST_ values. |
| 6313 | */ |
| 6314 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6315 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6316 | { |
| 6317 | regmatch_T regmatch; |
| 6318 | histentry_T *hisptr; |
| 6319 | int idx; |
| 6320 | int i; |
| 6321 | int last; |
| 6322 | int found = FALSE; |
| 6323 | |
| 6324 | regmatch.regprog = NULL; |
| 6325 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6326 | if (hislen != 0 |
| 6327 | && histype >= 0 |
| 6328 | && histype < HIST_COUNT |
| 6329 | && *str != NUL |
| 6330 | && (idx = hisidx[histype]) >= 0 |
| 6331 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6332 | != NULL) |
| 6333 | { |
| 6334 | i = last = idx; |
| 6335 | do |
| 6336 | { |
| 6337 | hisptr = &history[histype][i]; |
| 6338 | if (hisptr->hisstr == NULL) |
| 6339 | break; |
| 6340 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6341 | { |
| 6342 | found = TRUE; |
| 6343 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6344 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6345 | } |
| 6346 | else |
| 6347 | { |
| 6348 | if (i != last) |
| 6349 | { |
| 6350 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6351 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6352 | } |
| 6353 | if (--last < 0) |
| 6354 | last += hislen; |
| 6355 | } |
| 6356 | if (--i < 0) |
| 6357 | i += hislen; |
| 6358 | } while (i != idx); |
| 6359 | if (history[histype][idx].hisstr == NULL) |
| 6360 | hisidx[histype] = -1; |
| 6361 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6362 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6363 | return found; |
| 6364 | } |
| 6365 | |
| 6366 | /* |
| 6367 | * Remove an indexed entry from a history. |
| 6368 | * "histype" may be one of the HIST_ values. |
| 6369 | */ |
| 6370 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6371 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6372 | { |
| 6373 | int i, j; |
| 6374 | |
| 6375 | i = calc_hist_idx(histype, idx); |
| 6376 | if (i < 0) |
| 6377 | return FALSE; |
| 6378 | idx = hisidx[histype]; |
| 6379 | vim_free(history[histype][i].hisstr); |
| 6380 | |
| 6381 | /* When deleting the last added search string in a mapping, reset |
| 6382 | * last_maptick, so that the last added search string isn't deleted again. |
| 6383 | */ |
| 6384 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6385 | last_maptick = -1; |
| 6386 | |
| 6387 | while (i != idx) |
| 6388 | { |
| 6389 | j = (i + 1) % hislen; |
| 6390 | history[histype][i] = history[histype][j]; |
| 6391 | i = j; |
| 6392 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6393 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6394 | if (--i < 0) |
| 6395 | i += hislen; |
| 6396 | hisidx[histype] = i; |
| 6397 | return TRUE; |
| 6398 | } |
| 6399 | |
| 6400 | #endif /* FEAT_EVAL */ |
| 6401 | |
| 6402 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6403 | /* |
| 6404 | * Very specific function to remove the value in ":set key=val" from the |
| 6405 | * history. |
| 6406 | */ |
| 6407 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6408 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6409 | { |
| 6410 | char_u *p; |
| 6411 | int i; |
| 6412 | |
| 6413 | i = hisidx[HIST_CMD]; |
| 6414 | if (i < 0) |
| 6415 | return; |
| 6416 | p = history[HIST_CMD][i].hisstr; |
| 6417 | if (p != NULL) |
| 6418 | for ( ; *p; ++p) |
| 6419 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6420 | { |
| 6421 | p = vim_strchr(p + 3, '='); |
| 6422 | if (p == NULL) |
| 6423 | break; |
| 6424 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6425 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | if (p[i] == '\\' && p[i + 1]) |
| 6427 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6428 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6429 | --p; |
| 6430 | } |
| 6431 | } |
| 6432 | #endif |
| 6433 | |
| 6434 | #endif /* FEAT_CMDHIST */ |
| 6435 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6436 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6437 | /* |
| 6438 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6439 | * ccline and put the previous value in prev_ccline. |
| 6440 | */ |
| 6441 | static struct cmdline_info * |
| 6442 | get_ccline_ptr(void) |
| 6443 | { |
| 6444 | if ((State & CMDLINE) == 0) |
| 6445 | return NULL; |
| 6446 | if (ccline.cmdbuff != NULL) |
| 6447 | return &ccline; |
| 6448 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6449 | return &prev_ccline; |
| 6450 | return NULL; |
| 6451 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6452 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6453 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6454 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6455 | /* |
| 6456 | * Get the current command line in allocated memory. |
| 6457 | * Only works when the command line is being edited. |
| 6458 | * Returns NULL when something is wrong. |
| 6459 | */ |
| 6460 | char_u * |
| 6461 | get_cmdline_str(void) |
| 6462 | { |
| 6463 | struct cmdline_info *p = get_ccline_ptr(); |
| 6464 | |
| 6465 | if (p == NULL) |
| 6466 | return NULL; |
| 6467 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * Get the current command line position, counted in bytes. |
| 6472 | * Zero is the first position. |
| 6473 | * Only works when the command line is being edited. |
| 6474 | * Returns -1 when something is wrong. |
| 6475 | */ |
| 6476 | int |
| 6477 | get_cmdline_pos(void) |
| 6478 | { |
| 6479 | struct cmdline_info *p = get_ccline_ptr(); |
| 6480 | |
| 6481 | if (p == NULL) |
| 6482 | return -1; |
| 6483 | return p->cmdpos; |
| 6484 | } |
| 6485 | |
| 6486 | /* |
| 6487 | * Set the command line byte position to "pos". Zero is the first position. |
| 6488 | * Only works when the command line is being edited. |
| 6489 | * Returns 1 when failed, 0 when OK. |
| 6490 | */ |
| 6491 | int |
| 6492 | set_cmdline_pos( |
| 6493 | int pos) |
| 6494 | { |
| 6495 | struct cmdline_info *p = get_ccline_ptr(); |
| 6496 | |
| 6497 | if (p == NULL) |
| 6498 | return 1; |
| 6499 | |
| 6500 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6501 | * changed the command line. */ |
| 6502 | if (pos < 0) |
| 6503 | new_cmdpos = 0; |
| 6504 | else |
| 6505 | new_cmdpos = pos; |
| 6506 | return 0; |
| 6507 | } |
| 6508 | #endif |
| 6509 | |
| 6510 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6511 | /* |
| 6512 | * Get the current command-line type. |
| 6513 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6514 | * Only works when the command line is being edited. |
| 6515 | * Returns NUL when something is wrong. |
| 6516 | */ |
| 6517 | int |
| 6518 | get_cmdline_type(void) |
| 6519 | { |
| 6520 | struct cmdline_info *p = get_ccline_ptr(); |
| 6521 | |
| 6522 | if (p == NULL) |
| 6523 | return NUL; |
| 6524 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6525 | return |
| 6526 | # ifdef FEAT_EVAL |
| 6527 | (p->input_fn) ? '@' : |
| 6528 | # endif |
| 6529 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6530 | return p->cmdfirstc; |
| 6531 | } |
| 6532 | #endif |
| 6533 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6534 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6535 | /* |
| 6536 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6537 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6538 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6539 | */ |
| 6540 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6541 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6542 | { |
| 6543 | int len; |
| 6544 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6545 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6546 | |
| 6547 | *str = skipwhite(*str); |
| 6548 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6549 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6550 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6551 | *str += len; |
| 6552 | *num1 = (int)num; |
| 6553 | first = TRUE; |
| 6554 | } |
| 6555 | *str = skipwhite(*str); |
| 6556 | if (**str == ',') /* parse "to" part of range */ |
| 6557 | { |
| 6558 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6559 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6560 | if (len > 0) |
| 6561 | { |
| 6562 | *num2 = (int)num; |
| 6563 | *str = skipwhite(*str + len); |
| 6564 | } |
| 6565 | else if (!first) /* no number given at all */ |
| 6566 | return FAIL; |
| 6567 | } |
| 6568 | else if (first) /* only one number given */ |
| 6569 | *num2 = *num1; |
| 6570 | return OK; |
| 6571 | } |
| 6572 | #endif |
| 6573 | |
| 6574 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6575 | /* |
| 6576 | * :history command - print a history |
| 6577 | */ |
| 6578 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6579 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6580 | { |
| 6581 | histentry_T *hist; |
| 6582 | int histype1 = HIST_CMD; |
| 6583 | int histype2 = HIST_CMD; |
| 6584 | int hisidx1 = 1; |
| 6585 | int hisidx2 = -1; |
| 6586 | int idx; |
| 6587 | int i, j, k; |
| 6588 | char_u *end; |
| 6589 | char_u *arg = eap->arg; |
| 6590 | |
| 6591 | if (hislen == 0) |
| 6592 | { |
| 6593 | MSG(_("'history' option is zero")); |
| 6594 | return; |
| 6595 | } |
| 6596 | |
| 6597 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6598 | { |
| 6599 | end = arg; |
| 6600 | while (ASCII_ISALPHA(*end) |
| 6601 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6602 | end++; |
| 6603 | i = *end; |
| 6604 | *end = NUL; |
| 6605 | histype1 = get_histtype(arg); |
| 6606 | if (histype1 == -1) |
| 6607 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6608 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6609 | { |
| 6610 | histype1 = 0; |
| 6611 | histype2 = HIST_COUNT-1; |
| 6612 | } |
| 6613 | else |
| 6614 | { |
| 6615 | *end = i; |
| 6616 | EMSG(_(e_trailing)); |
| 6617 | return; |
| 6618 | } |
| 6619 | } |
| 6620 | else |
| 6621 | histype2 = histype1; |
| 6622 | *end = i; |
| 6623 | } |
| 6624 | else |
| 6625 | end = arg; |
| 6626 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6627 | { |
| 6628 | EMSG(_(e_trailing)); |
| 6629 | return; |
| 6630 | } |
| 6631 | |
| 6632 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6633 | { |
| 6634 | STRCPY(IObuff, "\n # "); |
| 6635 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6636 | MSG_PUTS_TITLE(IObuff); |
| 6637 | idx = hisidx[histype1]; |
| 6638 | hist = history[histype1]; |
| 6639 | j = hisidx1; |
| 6640 | k = hisidx2; |
| 6641 | if (j < 0) |
| 6642 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6643 | if (k < 0) |
| 6644 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6645 | if (idx >= 0 && j <= k) |
| 6646 | for (i = idx + 1; !got_int; ++i) |
| 6647 | { |
| 6648 | if (i == hislen) |
| 6649 | i = 0; |
| 6650 | if (hist[i].hisstr != NULL |
| 6651 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6652 | { |
| 6653 | msg_putchar('\n'); |
| 6654 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6655 | hist[i].hisnum); |
| 6656 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6657 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6658 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | else |
| 6660 | STRCAT(IObuff, hist[i].hisstr); |
| 6661 | msg_outtrans(IObuff); |
| 6662 | out_flush(); |
| 6663 | } |
| 6664 | if (i == idx) |
| 6665 | break; |
| 6666 | } |
| 6667 | } |
| 6668 | } |
| 6669 | #endif |
| 6670 | |
| 6671 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6672 | /* |
| 6673 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6674 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6675 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6676 | {NULL, NULL, NULL, NULL, NULL}; |
| 6677 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6678 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6679 | static int viminfo_add_at_front = FALSE; |
| 6680 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6681 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6682 | |
| 6683 | /* |
| 6684 | * Translate a history type number to the associated character. |
| 6685 | */ |
| 6686 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6687 | hist_type2char( |
| 6688 | int type, |
| 6689 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6690 | { |
| 6691 | if (type == HIST_CMD) |
| 6692 | return ':'; |
| 6693 | if (type == HIST_SEARCH) |
| 6694 | { |
| 6695 | if (use_question) |
| 6696 | return '?'; |
| 6697 | else |
| 6698 | return '/'; |
| 6699 | } |
| 6700 | if (type == HIST_EXPR) |
| 6701 | return '='; |
| 6702 | return '@'; |
| 6703 | } |
| 6704 | |
| 6705 | /* |
| 6706 | * Prepare for reading the history from the viminfo file. |
| 6707 | * This allocates history arrays to store the read history lines. |
| 6708 | */ |
| 6709 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6710 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6711 | { |
| 6712 | int i; |
| 6713 | int num; |
| 6714 | int type; |
| 6715 | int len; |
| 6716 | |
| 6717 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6718 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6719 | if (asklen > hislen) |
| 6720 | asklen = hislen; |
| 6721 | |
| 6722 | for (type = 0; type < HIST_COUNT; ++type) |
| 6723 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6724 | /* Count the number of empty spaces in the history list. Entries read |
| 6725 | * from viminfo previously are also considered empty. If there are |
| 6726 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6727 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6728 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6729 | num++; |
| 6730 | len = asklen; |
| 6731 | if (num > len) |
| 6732 | len = num; |
| 6733 | if (len <= 0) |
| 6734 | viminfo_history[type] = NULL; |
| 6735 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6736 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6737 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | if (viminfo_history[type] == NULL) |
| 6739 | len = 0; |
| 6740 | viminfo_hislen[type] = len; |
| 6741 | viminfo_hisidx[type] = 0; |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | /* |
| 6746 | * Accept a line from the viminfo, store it in the history array when it's |
| 6747 | * new. |
| 6748 | */ |
| 6749 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6750 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6751 | { |
| 6752 | int type; |
| 6753 | long_u len; |
| 6754 | char_u *val; |
| 6755 | char_u *p; |
| 6756 | |
| 6757 | type = hist_char2type(virp->vir_line[0]); |
| 6758 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6759 | { |
| 6760 | val = viminfo_readstring(virp, 1, TRUE); |
| 6761 | if (val != NULL && *val != NUL) |
| 6762 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6763 | int sep = (*val == ' ' ? NUL : *val); |
| 6764 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6765 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6766 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6767 | { |
| 6768 | /* Need to re-allocate to append the separator byte. */ |
| 6769 | len = STRLEN(val); |
| 6770 | p = lalloc(len + 2, TRUE); |
| 6771 | if (p != NULL) |
| 6772 | { |
| 6773 | if (type == HIST_SEARCH) |
| 6774 | { |
| 6775 | /* Search entry: Move the separator from the first |
| 6776 | * column to after the NUL. */ |
| 6777 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6778 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6779 | } |
| 6780 | else |
| 6781 | { |
| 6782 | /* Not a search entry: No separator in the viminfo |
| 6783 | * file, add a NUL separator. */ |
| 6784 | mch_memmove(p, val, (size_t)len + 1); |
| 6785 | p[len + 1] = NUL; |
| 6786 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6787 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6788 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6789 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6790 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6791 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6792 | } |
| 6793 | } |
| 6794 | } |
| 6795 | vim_free(val); |
| 6796 | } |
| 6797 | return viminfo_readline(virp); |
| 6798 | } |
| 6799 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6800 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6801 | * Accept a new style history line from the viminfo, store it in the history |
| 6802 | * array when it's new. |
| 6803 | */ |
| 6804 | void |
| 6805 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6806 | garray_T *values, |
| 6807 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6808 | { |
| 6809 | int type; |
| 6810 | long_u len; |
| 6811 | char_u *val; |
| 6812 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6813 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6814 | |
| 6815 | /* Check the format: |
| 6816 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6817 | if (values->ga_len < 4 |
| 6818 | || vp[0].bv_type != BVAL_NR |
| 6819 | || vp[1].bv_type != BVAL_NR |
| 6820 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6821 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6822 | return; |
| 6823 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6824 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6825 | if (type >= HIST_COUNT) |
| 6826 | return; |
| 6827 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6828 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6829 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6830 | if (val != NULL && *val != NUL) |
| 6831 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6832 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6833 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6834 | int idx; |
| 6835 | int overwrite = FALSE; |
| 6836 | |
| 6837 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6838 | { |
| 6839 | /* If lines were written by an older Vim we need to avoid |
| 6840 | * getting duplicates. See if the entry already exists. */ |
| 6841 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6842 | { |
| 6843 | p = viminfo_history[type][idx].hisstr; |
| 6844 | if (STRCMP(val, p) == 0 |
| 6845 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6846 | { |
| 6847 | overwrite = TRUE; |
| 6848 | break; |
| 6849 | } |
| 6850 | } |
| 6851 | |
| 6852 | if (!overwrite) |
| 6853 | { |
| 6854 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6855 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6856 | p = lalloc(len + 2, TRUE); |
| 6857 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6858 | else |
| 6859 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6860 | if (p != NULL) |
| 6861 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6862 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6863 | if (!overwrite) |
| 6864 | { |
| 6865 | mch_memmove(p, val, (size_t)len + 1); |
| 6866 | /* Put the separator after the NUL. */ |
| 6867 | p[len + 1] = sep; |
| 6868 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6869 | viminfo_history[type][idx].hisnum = 0; |
| 6870 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6871 | viminfo_hisidx[type]++; |
| 6872 | } |
| 6873 | } |
| 6874 | } |
| 6875 | } |
| 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6880 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6881 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6882 | static void |
| 6883 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6884 | { |
| 6885 | int idx; |
| 6886 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6887 | |
| 6888 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6889 | if (idx >= hislen) |
| 6890 | idx -= hislen; |
| 6891 | else if (idx < 0) |
| 6892 | idx = hislen - 1; |
| 6893 | if (viminfo_add_at_front) |
| 6894 | hisidx[type] = idx; |
| 6895 | else |
| 6896 | { |
| 6897 | if (hisidx[type] == -1) |
| 6898 | hisidx[type] = hislen - 1; |
| 6899 | do |
| 6900 | { |
| 6901 | if (history[type][idx].hisstr != NULL |
| 6902 | || history[type][idx].viminfo) |
| 6903 | break; |
| 6904 | if (++idx == hislen) |
| 6905 | idx = 0; |
| 6906 | } while (idx != hisidx[type]); |
| 6907 | if (idx != hisidx[type] && --idx < 0) |
| 6908 | idx = hislen - 1; |
| 6909 | } |
| 6910 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6911 | { |
| 6912 | vim_free(history[type][idx].hisstr); |
| 6913 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6914 | history[type][idx].viminfo = TRUE; |
| 6915 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6916 | if (--idx < 0) |
| 6917 | idx = hislen - 1; |
| 6918 | } |
| 6919 | idx += 1; |
| 6920 | idx %= hislen; |
| 6921 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6922 | { |
| 6923 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6924 | idx %= hislen; |
| 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6929 | static int |
| 6930 | #ifdef __BORLANDC__ |
| 6931 | _RTLENTRYF |
| 6932 | #endif |
| 6933 | sort_hist(const void *s1, const void *s2) |
| 6934 | { |
| 6935 | histentry_T *p1 = *(histentry_T **)s1; |
| 6936 | histentry_T *p2 = *(histentry_T **)s2; |
| 6937 | |
| 6938 | if (p1->time_set < p2->time_set) return -1; |
| 6939 | if (p1->time_set > p2->time_set) return 1; |
| 6940 | return 0; |
| 6941 | } |
| 6942 | #endif |
| 6943 | |
| 6944 | /* |
| 6945 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6946 | * timestamp; |
| 6947 | */ |
| 6948 | static void |
| 6949 | merge_history(int type) |
| 6950 | { |
| 6951 | int max_len; |
| 6952 | histentry_T **tot_hist; |
| 6953 | histentry_T *new_hist; |
| 6954 | int i; |
| 6955 | int len; |
| 6956 | |
| 6957 | /* Make one long list with all entries. */ |
| 6958 | max_len = hislen + viminfo_hisidx[type]; |
| 6959 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6960 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6961 | if (tot_hist == NULL || new_hist == NULL) |
| 6962 | { |
| 6963 | vim_free(tot_hist); |
| 6964 | vim_free(new_hist); |
| 6965 | return; |
| 6966 | } |
| 6967 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6968 | tot_hist[i] = &viminfo_history[type][i]; |
| 6969 | len = i; |
| 6970 | for (i = 0; i < hislen; i++) |
| 6971 | if (history[type][i].hisstr != NULL) |
| 6972 | tot_hist[len++] = &history[type][i]; |
| 6973 | |
| 6974 | /* Sort the list on timestamp. */ |
| 6975 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6976 | |
| 6977 | /* Keep the newest ones. */ |
| 6978 | for (i = 0; i < hislen; i++) |
| 6979 | { |
| 6980 | if (i < len) |
| 6981 | { |
| 6982 | new_hist[i] = *tot_hist[i]; |
| 6983 | tot_hist[i]->hisstr = NULL; |
| 6984 | if (new_hist[i].hisnum == 0) |
| 6985 | new_hist[i].hisnum = ++hisnum[type]; |
| 6986 | } |
| 6987 | else |
| 6988 | clear_hist_entry(&new_hist[i]); |
| 6989 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6990 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6991 | |
| 6992 | /* Free what is not kept. */ |
| 6993 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6994 | vim_free(viminfo_history[type][i].hisstr); |
| 6995 | for (i = 0; i < hislen; i++) |
| 6996 | vim_free(history[type][i].hisstr); |
| 6997 | vim_free(history[type]); |
| 6998 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6999 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | /* |
| 7003 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 7004 | */ |
| 7005 | void |
| 7006 | finish_viminfo_history(vir_T *virp) |
| 7007 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7009 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7010 | |
| 7011 | for (type = 0; type < HIST_COUNT; ++type) |
| 7012 | { |
| 7013 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7014 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7015 | |
| 7016 | if (merge) |
| 7017 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7018 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 7019 | concat_history(type); |
| 7020 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7021 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7022 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7023 | } |
| 7024 | } |
| 7025 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7026 | /* |
| 7027 | * Write history to viminfo file in "fp". |
| 7028 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 7029 | * file, data is in viminfo_history[]. |
| 7030 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 7031 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7032 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7033 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | { |
| 7035 | int i; |
| 7036 | int type; |
| 7037 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7038 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7039 | |
| 7040 | init_history(); |
| 7041 | if (hislen == 0) |
| 7042 | return; |
| 7043 | for (type = 0; type < HIST_COUNT; ++type) |
| 7044 | { |
| 7045 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 7046 | if (num_saved == 0) |
| 7047 | continue; |
| 7048 | if (num_saved < 0) /* Use default */ |
| 7049 | num_saved = hislen; |
| 7050 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 7051 | type == HIST_CMD ? _("Command Line") : |
| 7052 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7053 | type == HIST_EXPR ? _("Expression") : |
| 7054 | type == HIST_INPUT ? _("Input Line") : |
| 7055 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7056 | if (num_saved > hislen) |
| 7057 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7058 | |
| 7059 | /* |
| 7060 | * Merge typed and viminfo history: |
| 7061 | * round 1: history of typed commands. |
| 7062 | * round 2: history from recently read viminfo. |
| 7063 | */ |
| 7064 | for (round = 1; round <= 2; ++round) |
| 7065 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 7066 | if (round == 1) |
| 7067 | /* start at newest entry, somewhere in the list */ |
| 7068 | i = hisidx[type]; |
| 7069 | else if (viminfo_hisidx[type] > 0) |
| 7070 | /* start at newest entry, first in the list */ |
| 7071 | i = 0; |
| 7072 | else |
| 7073 | /* empty list */ |
| 7074 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7075 | if (i >= 0) |
| 7076 | while (num_saved > 0 |
| 7077 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7078 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7079 | char_u *p; |
| 7080 | time_t timestamp; |
| 7081 | int c = NUL; |
| 7082 | |
| 7083 | if (round == 1) |
| 7084 | { |
| 7085 | p = history[type][i].hisstr; |
| 7086 | timestamp = history[type][i].time_set; |
| 7087 | } |
| 7088 | else |
| 7089 | { |
| 7090 | p = viminfo_history[type] == NULL ? NULL |
| 7091 | : viminfo_history[type][i].hisstr; |
| 7092 | timestamp = viminfo_history[type] == NULL ? 0 |
| 7093 | : viminfo_history[type][i].time_set; |
| 7094 | } |
| 7095 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 7096 | if (p != NULL && (round == 2 |
| 7097 | || !merge |
| 7098 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7099 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7100 | --num_saved; |
| 7101 | fputc(hist_type2char(type, TRUE), fp); |
| 7102 | /* For the search history: put the separator in the |
| 7103 | * second column; use a space if there isn't one. */ |
| 7104 | if (type == HIST_SEARCH) |
| 7105 | { |
| 7106 | c = p[STRLEN(p) + 1]; |
| 7107 | putc(c == NUL ? ' ' : c, fp); |
| 7108 | } |
| 7109 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7110 | |
| 7111 | { |
| 7112 | char cbuf[NUMBUFLEN]; |
| 7113 | |
| 7114 | /* New style history with a bar line. Format: |
| 7115 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 7116 | if (c == NUL) |
| 7117 | cbuf[0] = NUL; |
| 7118 | else |
| 7119 | sprintf(cbuf, "%d", c); |
| 7120 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 7121 | type, (long)timestamp, cbuf); |
| 7122 | barline_writestring(fp, p, LSIZE - 20); |
| 7123 | putc('\n', fp); |
| 7124 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7125 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7126 | if (round == 1) |
| 7127 | { |
| 7128 | /* Decrement index, loop around and stop when back at |
| 7129 | * the start. */ |
| 7130 | if (--i < 0) |
| 7131 | i = hislen - 1; |
| 7132 | if (i == hisidx[type]) |
| 7133 | break; |
| 7134 | } |
| 7135 | else |
| 7136 | { |
| 7137 | /* Increment index. Stop at the end in the while. */ |
| 7138 | ++i; |
| 7139 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7140 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 7141 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 7142 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 7143 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 7144 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 7145 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 7146 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | #endif /* FEAT_VIMINFO */ |
| 7150 | |
| 7151 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 7152 | /* |
| 7153 | * Write a character at the current cursor+offset position. |
| 7154 | * It is directly written into the command buffer block. |
| 7155 | */ |
| 7156 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7157 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7158 | { |
| 7159 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7160 | { |
| 7161 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 7162 | return; |
| 7163 | } |
| 7164 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 7165 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 7166 | } |
| 7167 | |
| 7168 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7169 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | { |
| 7171 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 7172 | { |
| 7173 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 7174 | return NUL; |
| 7175 | } |
| 7176 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 7177 | } |
| 7178 | #endif |
| 7179 | |
| 7180 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 7181 | /* |
| 7182 | * Open a window on the current command line and history. Allow editing in |
| 7183 | * the window. Returns when the window is closed. |
| 7184 | * Returns: |
| 7185 | * CR if the command is to be executed |
| 7186 | * Ctrl_C if it is to be abandoned |
| 7187 | * K_IGNORE if editing continues |
| 7188 | */ |
| 7189 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7190 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7191 | { |
| 7192 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7193 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7194 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7195 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7196 | win_T *wp; |
| 7197 | int i; |
| 7198 | linenr_T lnum; |
| 7199 | int histtype; |
| 7200 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7201 | int save_restart_edit = restart_edit; |
| 7202 | int save_State = State; |
| 7203 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7204 | #ifdef FEAT_RIGHTLEFT |
| 7205 | int save_cmdmsg_rl = cmdmsg_rl; |
| 7206 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7207 | #ifdef FEAT_FOLDING |
| 7208 | int save_KeyTyped; |
| 7209 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7210 | |
| 7211 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 7212 | if (cmdwin_type != 0 |
| 7213 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 7214 | || cmdline_star > 0 |
| 7215 | # endif |
| 7216 | ) |
| 7217 | { |
| 7218 | beep_flush(); |
| 7219 | return K_IGNORE; |
| 7220 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7221 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7222 | |
| 7223 | /* Save current window sizes. */ |
| 7224 | win_size_save(&winsizes); |
| 7225 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7227 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7228 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7229 | /* don't use a new tab page */ |
| 7230 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 7231 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 7232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7233 | /* Create a window for the command-line buffer. */ |
| 7234 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 7235 | { |
| 7236 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7237 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7238 | return K_IGNORE; |
| 7239 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7240 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | |
| 7242 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 7243 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7244 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7245 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7246 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 7247 | #ifdef FEAT_FOLDING |
| 7248 | curwin->w_p_fen = FALSE; |
| 7249 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7250 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7251 | curwin->w_p_rl = cmdmsg_rl; |
| 7252 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7253 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 7254 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7255 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7256 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7257 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7258 | /* But don't allow switching to another buffer. */ |
| 7259 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7260 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7261 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 7262 | need_wait_return = FALSE; |
| 7263 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 7264 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7265 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 7266 | { |
| 7267 | if (p_wc == TAB) |
| 7268 | { |
| 7269 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 7270 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 7271 | } |
| 7272 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 7273 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 7274 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7275 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 7276 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7277 | * sets 'textwidth' to 78). */ |
| 7278 | curbuf->b_p_tw = 0; |
| 7279 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7280 | /* Fill the buffer with the history. */ |
| 7281 | init_history(); |
| 7282 | if (hislen > 0) |
| 7283 | { |
| 7284 | i = hisidx[histtype]; |
| 7285 | if (i >= 0) |
| 7286 | { |
| 7287 | lnum = 0; |
| 7288 | do |
| 7289 | { |
| 7290 | if (++i == hislen) |
| 7291 | i = 0; |
| 7292 | if (history[histtype][i].hisstr != NULL) |
| 7293 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7294 | (colnr_T)0, FALSE); |
| 7295 | } |
| 7296 | while (i != hisidx[histtype]); |
| 7297 | } |
| 7298 | } |
| 7299 | |
| 7300 | /* Replace the empty last line with the current command-line and put the |
| 7301 | * cursor there. */ |
| 7302 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7303 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7304 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7305 | changed_line_abv_curs(); |
| 7306 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7307 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7308 | |
| 7309 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7310 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7311 | |
| 7312 | /* No Ex mode here! */ |
| 7313 | exmode_active = 0; |
| 7314 | |
| 7315 | State = NORMAL; |
| 7316 | # ifdef FEAT_MOUSE |
| 7317 | setmouse(); |
| 7318 | # endif |
| 7319 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7320 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7321 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7322 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7323 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7324 | |
| 7325 | i = RedrawingDisabled; |
| 7326 | RedrawingDisabled = 0; |
| 7327 | |
| 7328 | /* |
| 7329 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7330 | */ |
| 7331 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7332 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7333 | |
| 7334 | RedrawingDisabled = i; |
| 7335 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7336 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7337 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7338 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7339 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7340 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7341 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7342 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7343 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7344 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7345 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | # endif |
| 7347 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7348 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7349 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | cmdwin_type = 0; |
| 7351 | |
| 7352 | exmode_active = save_exmode; |
| 7353 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7354 | /* 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] | 7355 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7356 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7357 | { |
| 7358 | cmdwin_result = Ctrl_C; |
| 7359 | EMSG(_("E199: Active window or buffer deleted")); |
| 7360 | } |
| 7361 | else |
| 7362 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7363 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7364 | /* autocmds may abort script processing */ |
| 7365 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7366 | cmdwin_result = Ctrl_C; |
| 7367 | # endif |
| 7368 | /* Set the new command line from the cmdline buffer. */ |
| 7369 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7370 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7371 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7372 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7373 | |
| 7374 | if (histtype == HIST_CMD) |
| 7375 | { |
| 7376 | /* Execute the command directly. */ |
| 7377 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7378 | cmdwin_result = CAR; |
| 7379 | } |
| 7380 | else |
| 7381 | { |
| 7382 | /* First need to cancel what we were doing. */ |
| 7383 | ccline.cmdbuff = NULL; |
| 7384 | stuffcharReadbuff(':'); |
| 7385 | stuffReadbuff((char_u *)p); |
| 7386 | stuffcharReadbuff(CAR); |
| 7387 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7388 | } |
| 7389 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7390 | { |
| 7391 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7392 | cmdwin_result = CAR; |
| 7393 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7394 | else if (cmdwin_result == Ctrl_C) |
| 7395 | { |
| 7396 | /* :q or :close, don't execute any command |
| 7397 | * and don't modify the cmd window. */ |
| 7398 | ccline.cmdbuff = NULL; |
| 7399 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7400 | else |
| 7401 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7402 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7403 | { |
| 7404 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7405 | ccline.cmdlen = 0; |
| 7406 | ccline.cmdbufflen = 1; |
| 7407 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7408 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7409 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7410 | else |
| 7411 | { |
| 7412 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7413 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7414 | ccline.cmdpos = curwin->w_cursor.col; |
| 7415 | if (ccline.cmdpos > ccline.cmdlen) |
| 7416 | ccline.cmdpos = ccline.cmdlen; |
| 7417 | if (cmdwin_result == K_IGNORE) |
| 7418 | { |
| 7419 | set_cmdspos_cursor(); |
| 7420 | redrawcmd(); |
| 7421 | } |
| 7422 | } |
| 7423 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7424 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7425 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7426 | # ifdef FEAT_CONCEAL |
| 7427 | /* Avoid command-line window first character being concealed. */ |
| 7428 | curwin->w_p_cole = 0; |
| 7429 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7430 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7431 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7432 | win_goto(old_curwin); |
| 7433 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7434 | |
| 7435 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7436 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7437 | if (bufref_valid(&bufref)) |
| 7438 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7439 | |
| 7440 | /* Restore window sizes. */ |
| 7441 | win_size_restore(&winsizes); |
| 7442 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7443 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
| 7446 | ga_clear(&winsizes); |
| 7447 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7448 | # ifdef FEAT_RIGHTLEFT |
| 7449 | cmdmsg_rl = save_cmdmsg_rl; |
| 7450 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7451 | |
| 7452 | State = save_State; |
| 7453 | # ifdef FEAT_MOUSE |
| 7454 | setmouse(); |
| 7455 | # endif |
| 7456 | |
| 7457 | return cmdwin_result; |
| 7458 | } |
| 7459 | #endif /* FEAT_CMDWIN */ |
| 7460 | |
| 7461 | /* |
| 7462 | * Used for commands that either take a simple command string argument, or: |
| 7463 | * cmd << endmarker |
| 7464 | * {script} |
| 7465 | * endmarker |
| 7466 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7467 | */ |
| 7468 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7469 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7470 | { |
| 7471 | char_u *theline; |
| 7472 | char *end_pattern = NULL; |
| 7473 | char dot[] = "."; |
| 7474 | garray_T ga; |
| 7475 | |
| 7476 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7477 | return NULL; |
| 7478 | |
| 7479 | ga_init2(&ga, 1, 0x400); |
| 7480 | |
| 7481 | if (cmd[2] != NUL) |
| 7482 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7483 | else |
| 7484 | end_pattern = dot; |
| 7485 | |
| 7486 | for (;;) |
| 7487 | { |
| 7488 | theline = eap->getline( |
| 7489 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7490 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7491 | #endif |
| 7492 | NUL, eap->cookie, 0); |
| 7493 | |
| 7494 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7495 | { |
| 7496 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7497 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7498 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7499 | |
| 7500 | ga_concat(&ga, theline); |
| 7501 | ga_append(&ga, '\n'); |
| 7502 | vim_free(theline); |
| 7503 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7504 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7505 | |
| 7506 | return (char_u *)ga.ga_data; |
| 7507 | } |