blob: 471479c2caa60986e659d7a9bed4bd7dcbdabd87 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
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 */
25struct 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 Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 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 Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaar438d1762018-09-30 17:11:48 +020047// The current cmdline_info. It is initialized in getcmdline() and after that
48// used by other functions. When invoking getcmdline() recursively it needs
49// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
64static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
65static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
66 /* identifying (unique) number of newest history entry */
67static int hislen = 0; /* actual length of history tables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#endif
69
70#ifdef FEAT_RIGHTLEFT
71static int cmd_hkmap = 0; /* Hebrew mapping during command line */
72#endif
73
Bram Moolenaar438d1762018-09-30 17:11:48 +020074static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010075static int cmdline_charsize(int idx);
76static void set_cmdspos(void);
77static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010079static void alloc_cmdbuff(int len);
80static int realloc_cmdbuff(int len);
81static void draw_cmdline(int start, int len);
82static void save_cmdline(struct cmdline_info *ccp);
83static void restore_cmdline(struct cmdline_info *ccp);
84static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010086static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static void redrawcmdprompt(void);
89static void cursorcmd(void);
90static int ccheck_abbr(int);
91static int nextwild(expand_T *xp, int type, int options, int escape);
92static void escape_fname(char_u **pp);
93static int showmatches(expand_T *xp, int wildmenu);
94static void set_expand_context(expand_T *xp);
95static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
96static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +010099static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100100static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200101# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200103# endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200104# if defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100105static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
106static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107# endif
108#endif
109
110#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200111static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#endif
113
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200114#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaareae1b912019-05-09 15:12:55 +0200115static int sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200116#endif
117
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200118
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200119 static void
120trigger_cmd_autocmd(int typechar, int evt)
121{
122 char_u typestr[2];
123
124 typestr[0] = typechar;
125 typestr[1] = NUL;
126 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
127}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200130 * Abandon the command line.
131 */
132 static void
133abandon_cmdline(void)
134{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100135 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200136 if (msg_scrolled == 0)
137 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200139 redraw_cmdline = TRUE;
140}
141
Bram Moolenaaree219b02017-12-17 14:55:01 +0100142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200143/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100144 * Guess that the pattern matches everything. Only finds specific cases, such
145 * as a trailing \|, which can happen while typing a pattern.
146 */
147 static int
148empty_pattern(char_u *p)
149{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100150 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100151
152 /* remove trailing \v and the like */
153 while (n >= 2 && p[n - 2] == '\\'
154 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
155 n -= 2;
156 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
157}
158
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200159// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200160typedef struct {
161 colnr_T vs_curswant;
162 colnr_T vs_leftcol;
163 linenr_T vs_topline;
164# ifdef FEAT_DIFF
165 int vs_topfill;
166# endif
167 linenr_T vs_botline;
168 linenr_T vs_empty_rows;
169} viewstate_T;
170
171 static void
172save_viewstate(viewstate_T *vs)
173{
174 vs->vs_curswant = curwin->w_curswant;
175 vs->vs_leftcol = curwin->w_leftcol;
176 vs->vs_topline = curwin->w_topline;
177# ifdef FEAT_DIFF
178 vs->vs_topfill = curwin->w_topfill;
179# endif
180 vs->vs_botline = curwin->w_botline;
181 vs->vs_empty_rows = curwin->w_empty_rows;
182}
183
184 static void
185restore_viewstate(viewstate_T *vs)
186{
187 curwin->w_curswant = vs->vs_curswant;
188 curwin->w_leftcol = vs->vs_leftcol;
189 curwin->w_topline = vs->vs_topline;
190# ifdef FEAT_DIFF
191 curwin->w_topfill = vs->vs_topfill;
192# endif
193 curwin->w_botline = vs->vs_botline;
194 curwin->w_empty_rows = vs->vs_empty_rows;
195}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200196
197// Struct to store the state of 'incsearch' highlighting.
198typedef struct {
199 pos_T search_start; // where 'incsearch' starts searching
200 pos_T save_cursor;
201 viewstate_T init_viewstate;
202 viewstate_T old_viewstate;
203 pos_T match_start;
204 pos_T match_end;
205 int did_incsearch;
206 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200207 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200208} incsearch_state_T;
209
210 static void
211init_incsearch_state(incsearch_state_T *is_state)
212{
213 is_state->match_start = curwin->w_cursor;
214 is_state->did_incsearch = FALSE;
215 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200216 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200217 CLEAR_POS(&is_state->match_end);
218 is_state->save_cursor = curwin->w_cursor; // may be restored later
219 is_state->search_start = curwin->w_cursor;
220 save_viewstate(&is_state->init_viewstate);
221 save_viewstate(&is_state->old_viewstate);
222}
223
224/*
225 * First move cursor to end of match, then to the start. This
226 * moves the whole match onto the screen when 'nowrap' is set.
227 */
228 static void
229set_search_match(pos_T *t)
230{
231 t->lnum += search_match_lines;
232 t->col = search_match_endcol;
233 if (t->lnum > curbuf->b_ml.ml_line_count)
234 {
235 t->lnum = curbuf->b_ml.ml_line_count;
236 coladvance((colnr_T)MAXCOL);
237 }
238}
239
240/*
241 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200242 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200243 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200244 */
245 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200246do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
247 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200249 char_u *cmd;
250 cmdmod_T save_cmdmod = cmdmod;
251 char_u *p;
252 int delim_optional = FALSE;
253 int delim;
254 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100255 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200256 exarg_T ea;
257 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200258 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200259
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200260 *skiplen = 0;
261 *patlen = ccline.cmdlen;
262
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200263 if (!p_is || cmd_silent)
264 return FALSE;
265
266 // by default search all lines
267 search_first_line = 0;
268 search_last_line = MAXLNUM;
269
270 if (firstc == '/' || firstc == '?')
271 return TRUE;
272 if (firstc != ':')
273 return FALSE;
274
275 vim_memset(&ea, 0, sizeof(ea));
276 ea.line1 = 1;
277 ea.line2 = 1;
278 ea.cmd = ccline.cmdbuff;
279 ea.addr_type = ADDR_LINES;
280
281 parse_command_modifiers(&ea, &dummy, TRUE);
282 cmdmod = save_cmdmod;
283
284 cmd = skip_range(ea.cmd, NULL);
285 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
286 return FALSE;
287
288 // Skip over "substitute" to find the pattern separator.
289 for (p = cmd; ASCII_ISALPHA(*p); ++p)
290 ;
291 if (*skipwhite(p) == NUL)
292 return FALSE;
293
294 if (STRNCMP(cmd, "substitute", p - cmd) == 0
295 || STRNCMP(cmd, "smagic", p - cmd) == 0
296 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
297 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200298 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200299 if (*cmd == 's' && cmd[1] == 'm')
300 p_magic = TRUE;
301 else if (*cmd == 's' && cmd[1] == 'n')
302 p_magic = FALSE;
303 }
304 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
305 {
306 // skip over flags
307 while (ASCII_ISALPHA(*(p = skipwhite(p))))
308 ++p;
309 if (*p == NUL)
310 return FALSE;
311 }
312 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
313 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
314 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
315 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
316 || STRNCMP(cmd, "global", p - cmd) == 0)
317 {
318 // skip over "!"
319 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200320 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200321 p++;
322 if (*skipwhite(p) == NUL)
323 return FALSE;
324 }
325 if (*cmd != 'g')
326 delim_optional = TRUE;
327 }
328 else
329 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200330
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200331 p = skipwhite(p);
332 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
333 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200334
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200335 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200336
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200337 if (end == p && !use_last_pat)
338 return FALSE;
339
340 // Don't do 'hlsearch' highlighting if the pattern matches everything.
341 if (!use_last_pat)
342 {
343 char c = *end;
344 int empty;
345
346 *end = NUL;
347 empty = empty_pattern(p);
348 *end = c;
349 if (empty)
350 return FALSE;
351 }
352
353 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200354 *skiplen = (int)(p - ccline.cmdbuff);
355 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200356
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200357 // parse the address range
358 save_cursor = curwin->w_cursor;
359 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200360 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200361 if (ea.addr_count > 0)
362 {
363 // Allow for reverse match.
364 if (ea.line2 < ea.line1)
365 {
366 search_first_line = ea.line2;
367 search_last_line = ea.line1;
368 }
369 else
370 {
371 search_first_line = ea.line1;
372 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200373 }
374 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200375 else if (cmd[0] == 's' && cmd[1] != 'o')
376 {
377 // :s defaults to the current line
378 search_first_line = curwin->w_cursor.lnum;
379 search_last_line = curwin->w_cursor.lnum;
380 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200381
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200382 curwin->w_cursor = save_cursor;
383 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384}
385
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200386 static void
387finish_incsearch_highlighting(
388 int gotesc,
389 incsearch_state_T *is_state,
390 int call_update_screen)
391{
392 if (is_state->did_incsearch)
393 {
394 is_state->did_incsearch = FALSE;
395 if (gotesc)
396 curwin->w_cursor = is_state->save_cursor;
397 else
398 {
399 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
400 {
401 // put the '" mark at the original position
402 curwin->w_cursor = is_state->save_cursor;
403 setpcmark();
404 }
405 curwin->w_cursor = is_state->search_start;
406 }
407 restore_viewstate(&is_state->old_viewstate);
408 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200409
410 // by default search all lines
411 search_first_line = 0;
412 search_last_line = MAXLNUM;
413
414 p_magic = is_state->magic_save;
415
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200417 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200418 if (call_update_screen)
419 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200420 }
421}
422
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200423/*
424 * Do 'incsearch' highlighting if desired.
425 */
426 static void
427may_do_incsearch_highlighting(
428 int firstc,
429 long count,
430 incsearch_state_T *is_state)
431{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200432 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200433 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200434 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200435#ifdef FEAT_RELTIME
436 proftime_T tm;
437#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200438 int next_char;
439 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200440 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200441
Bram Moolenaar198cb662018-09-06 21:44:17 +0200442 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100443 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200444 save_last_search_pattern();
445
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200446 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200447 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200448 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200449 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200450 if (did_do_incsearch && vpeekc() == NUL)
451 // may have skipped a redraw, do it now
452 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200453 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200454 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200455
456 // If there is a character waiting, search and redraw later.
457 if (char_avail())
458 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200459 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200460 is_state->incsearch_postponed = TRUE;
461 return;
462 }
463 is_state->incsearch_postponed = FALSE;
464
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200465 if (search_first_line == 0)
466 // start at the original cursor position
467 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100468 else if (search_first_line > curbuf->b_ml.ml_line_count)
469 {
470 // start after the last line
471 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
472 curwin->w_cursor.col = MAXCOL;
473 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200474 else
475 {
476 // start at the first line in the range
477 curwin->w_cursor.lnum = search_first_line;
478 curwin->w_cursor.col = 0;
479 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200481 // Use the previous pattern for ":s//".
482 next_char = ccline.cmdbuff[skiplen + patlen];
483 use_last_pat = patlen == 0 && skiplen > 0
484 && ccline.cmdbuff[skiplen - 1] == next_char;
485
486 // If there is no pattern, don't do anything.
487 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200488 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200489 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 set_no_hlsearch(TRUE); // turn off previous highlight
491 redraw_all_later(SOME_VALID);
492 }
493 else
494 {
495 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
496
497 cursor_off(); // so the user knows we're busy
498 out_flush();
499 ++emsg_off; // so it doesn't beep if bad expr
500#ifdef FEAT_RELTIME
501 // Set the time limit to half a second.
502 profile_setlimit(500L, &tm);
503#endif
504 if (!p_hls)
505 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200506 if (search_first_line != 0)
507 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200508 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200509 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200510 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200511#ifdef FEAT_RELTIME
512 &tm, NULL
513#else
514 NULL, NULL
515#endif
516 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200517 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200518 --emsg_off;
519
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200520 if (curwin->w_cursor.lnum < search_first_line
521 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200522 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200523 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200524 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200525 curwin->w_cursor = is_state->search_start;
526 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200527
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200528 // if interrupted while searching, behave like it failed
529 if (got_int)
530 {
531 (void)vpeekc(); // remove <C-C> from input stream
532 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200533 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 }
535 else if (char_avail())
536 // cancelled searching because a char was typed
537 is_state->incsearch_postponed = TRUE;
538 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200539 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 highlight_match = TRUE; // highlight position
541 else
542 highlight_match = FALSE; // remove highlight
543
544 // First restore the old curwin values, so the screen is positioned in the
545 // same way as the actual search command.
546 restore_viewstate(&is_state->old_viewstate);
547 changed_cline_bef_curs();
548 update_topline();
549
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200550 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 {
552 pos_T save_pos = curwin->w_cursor;
553
554 is_state->match_start = curwin->w_cursor;
555 set_search_match(&curwin->w_cursor);
556 validate_cursor();
557 end_pos = curwin->w_cursor;
558 is_state->match_end = end_pos;
559 curwin->w_cursor = save_pos;
560 }
561 else
562 end_pos = curwin->w_cursor; // shutup gcc 4
563
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200564 // Disable 'hlsearch' highlighting if the pattern matches everything.
565 // Avoids a flash when typing "foo\|".
566 if (!use_last_pat)
567 {
568 next_char = ccline.cmdbuff[skiplen + patlen];
569 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200570 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
571 {
572 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200573 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200574 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200575 ccline.cmdbuff[skiplen + patlen] = next_char;
576 }
577
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200578 validate_cursor();
579 // May redraw the status line to show the cursor position.
580 if (p_ru && curwin->w_status_height > 0)
581 curwin->w_redr_status = TRUE;
582
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200584 restore_last_search_pattern();
585
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200586 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
587 // end of the pattern, e.g. for ":s/pat/".
588 if (ccline.cmdbuff[skiplen + patlen] != NUL)
589 curwin->w_cursor = is_state->search_start;
590 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 curwin->w_cursor = end_pos;
592
593 msg_starthere();
594 redrawcmdline();
595 is_state->did_incsearch = TRUE;
596}
597
598/*
599 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
600 * or previous match.
601 * Returns FAIL when jumping to cmdline_not_changed;
602 */
603 static int
604may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200605 int firstc,
606 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200608 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200610 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200611 pos_T t;
612 char_u *pat;
613 int search_flags = SEARCH_NOOF;
614 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616
Bram Moolenaar198cb662018-09-06 21:44:17 +0200617 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100618 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200619 save_last_search_pattern();
620
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200622 {
623 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200625 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200627 {
628 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200630 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200631
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200632 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200633 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200635 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200636 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200637 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200640
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 cursor_off();
642 out_flush();
643 if (c == Ctrl_G)
644 {
645 t = is_state->match_end;
646 if (LT_POS(is_state->match_start, is_state->match_end))
647 // Start searching at the end of the match not at the beginning of
648 // the next column.
649 (void)decl(&t);
650 search_flags += SEARCH_COL;
651 }
652 else
653 t = is_state->match_start;
654 if (!p_hls)
655 search_flags += SEARCH_KEEP;
656 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200657 save = pat[patlen];
658 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100659 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 c == Ctrl_G ? FORWARD : BACKWARD,
661 pat, count, search_flags,
662 RE_SEARCH, 0, NULL, NULL);
663 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200664 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200665 if (i)
666 {
667 is_state->search_start = is_state->match_start;
668 is_state->match_end = t;
669 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200670 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200671 {
672 // Move just before the current match, so that when nv_search
673 // finishes the cursor will be put back on the match.
674 is_state->search_start = t;
675 (void)decl(&is_state->search_start);
676 }
677 else if (c == Ctrl_G && firstc == '?')
678 {
679 // Move just after the current match, so that when nv_search
680 // finishes the cursor will be put back on the match.
681 is_state->search_start = t;
682 (void)incl(&is_state->search_start);
683 }
684 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
685 {
686 // wrap around
687 is_state->search_start = t;
688 if (firstc == '?')
689 (void)incl(&is_state->search_start);
690 else
691 (void)decl(&is_state->search_start);
692 }
693
694 set_search_match(&is_state->match_end);
695 curwin->w_cursor = is_state->match_start;
696 changed_cline_bef_curs();
697 update_topline();
698 validate_cursor();
699 highlight_match = TRUE;
700 save_viewstate(&is_state->old_viewstate);
701 update_screen(NOT_VALID);
702 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200703 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200704 }
705 else
706 vim_beep(BO_ERROR);
707 restore_last_search_pattern();
708 return FAIL;
709}
710
711/*
712 * When CTRL-L typed: add character from the match to the pattern.
713 * May set "*c" to the added character.
714 * Return OK when jumping to cmdline_not_changed.
715 */
716 static int
717may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
718{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200719 int skiplen, patlen;
720
Bram Moolenaar198cb662018-09-06 21:44:17 +0200721 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100722 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200723 save_last_search_pattern();
724
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200725 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200726 {
727 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200728 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200729 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100730 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200731
732 // Add a character from under the cursor for 'incsearch'.
733 if (is_state->did_incsearch)
734 {
735 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200736 *c = gchar_cursor();
737 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200738 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739 // If 'ignorecase' and 'smartcase' are set and the
740 // command line has no uppercase characters, convert
741 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200742 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200744 if (*c == firstc || vim_strchr((char_u *)(
745 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200746 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200747 // put a backslash before special characters
748 stuffcharReadbuff(*c);
749 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200750 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200751 // add any composing characters
752 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
753 {
754 int save_c = *c;
755
756 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
757 {
758 curwin->w_cursor.col += mb_char2len(*c);
759 *c = gchar_cursor();
760 stuffcharReadbuff(*c);
761 }
762 *c = save_c;
763 }
764 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200765 }
766 }
767 return OK;
768}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200769#endif
770
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200771#ifdef FEAT_ARABIC
772/*
773 * Return TRUE if the command line has an Arabic character at or after "start"
774 * for "len" bytes.
775 */
776 static int
777cmdline_has_arabic(int start, int len)
778{
779 int j;
780 int mb_l;
781 int u8c;
782 char_u *p;
783 int u8cc[MAX_MCO];
784
785 if (!enc_utf8)
786 return FALSE;
787
788 for (j = start; j < start + len; j += mb_l)
789 {
790 p = ccline.cmdbuff + j;
791 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
792 mb_l = utfc_ptr2len_len(p, start + len - j);
793 if (ARABIC_CHAR(u8c))
794 return TRUE;
795 }
796 return FALSE;
797}
798#endif
799
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200800 void
801cmdline_init(void)
802{
803 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
804}
805
Bram Moolenaar66216052017-12-16 16:33:44 +0100806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * getcmdline() - accept a command line starting with firstc.
808 *
809 * firstc == ':' get ":" command line.
810 * firstc == '/' or '?' get search pattern
811 * firstc == '=' get expression
812 * firstc == '@' get text for input() function
813 * firstc == '>' get text for debug mode
814 * firstc == NUL get text for :insert command
815 * firstc == -1 like NUL, and break on CTRL-C
816 *
817 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
818 * command line.
819 *
820 * Careful: getcmdline() can be called recursively!
821 *
822 * Return pointer to allocated string if there is a commandline, NULL
823 * otherwise.
824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100826getcmdline(
827 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200828 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200829 int indent, // indent for inside conditionals
830 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200831{
832 return getcmdline_int(firstc, count, indent, TRUE);
833}
834
835 static char_u *
836getcmdline_int(
837 int firstc,
838 long count UNUSED, // only used for incremental search
839 int indent, // indent for inside conditionals
840 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842 int c;
843 int i;
844 int j;
845 int gotesc = FALSE; /* TRUE when <ESC> just typed */
846 int do_abbr; /* when TRUE check for abbr. */
847#ifdef FEAT_CMDHIST
848 char_u *lookfor = NULL; /* string to match */
849 int hiscnt; /* current history line in use */
850 int histype; /* history type to be used */
851#endif
852#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200853 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855 int did_wild_list = FALSE; /* did wild_list() recently */
856 int wim_index = 0; /* index in wim_flags[] */
857 int res;
858 int save_msg_scroll = msg_scroll;
859 int save_State = State; /* remember State when called */
860 int some_key_typed = FALSE; /* one of the keys was typed */
861#ifdef FEAT_MOUSE
862 /* mouse drag and release events are ignored, unless they are
863 * preceded with a mouse down event */
864 int ignore_drag_release = TRUE;
865#endif
866#ifdef FEAT_EVAL
867 int break_ctrl_c = FALSE;
868#endif
869 expand_T xpc;
870 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000871 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200872 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200873 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaar438d1762018-09-30 17:11:48 +0200875 if (ccline.cmdbuff != NULL)
876 {
877 // Being called recursively. Since ccline is global, we need to save
878 // the current buffer and restore it when returning.
879 save_cmdline(&save_ccline);
880 did_save_ccline = TRUE;
881 }
882 if (init_ccline)
883 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_EVAL
886 if (firstc == -1)
887 {
888 firstc = NUL;
889 break_ctrl_c = TRUE;
890 }
891#endif
892#ifdef FEAT_RIGHTLEFT
893 /* start without Hebrew mapping for a command line */
894 if (firstc == ':' || firstc == '=' || firstc == '>')
895 cmd_hkmap = 0;
896#endif
897
898 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200901 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
903
904 /*
905 * set some variables for redrawcmd()
906 */
907 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000908 ccline.cmdindent = (firstc > 0 ? indent : 0);
909
910 /* alloc initial ccline.cmdbuff */
911 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200913 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 ccline.cmdlen = ccline.cmdpos = 0;
915 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100916 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000918 /* autoindent for :insert and :append */
919 if (firstc <= 0)
920 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200921 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000922 ccline.cmdbuff[indent] = NUL;
923 ccline.cmdpos = indent;
924 ccline.cmdspos = indent;
925 ccline.cmdlen = indent;
926 }
927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000929 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931#ifdef FEAT_RIGHTLEFT
932 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
933 && (firstc == '/' || firstc == '?'))
934 cmdmsg_rl = TRUE;
935 else
936 cmdmsg_rl = FALSE;
937#endif
938
939 redir_off = TRUE; /* don't redirect the typed command */
940 if (!cmd_silent)
941 {
942 i = msg_scrolled;
943 msg_scrolled = 0; /* avoid wait_return message */
944 gotocmdline(TRUE);
945 msg_scrolled += i;
946 redrawcmdprompt(); /* draw prompt or indent */
947 set_cmdspos();
948 }
949 xpc.xp_context = EXPAND_NOTHING;
950 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000951#ifndef BACKSLASH_IN_FILENAME
952 xpc.xp_shell = FALSE;
953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000955#if defined(FEAT_EVAL)
956 if (ccline.input_fn)
957 {
958 xpc.xp_context = ccline.xp_context;
959 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200960# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000961 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000962# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000963 }
964#endif
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /*
967 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
968 * doing ":@0" when register 0 doesn't contain a CR.
969 */
970 msg_scroll = FALSE;
971
972 State = CMDLINE;
973
974 if (firstc == '/' || firstc == '?' || firstc == '@')
975 {
976 /* Use ":lmap" mappings for search pattern and input(). */
977 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
978 b_im_ptr = &curbuf->b_p_iminsert;
979 else
980 b_im_ptr = &curbuf->b_p_imsearch;
981 if (*b_im_ptr == B_IMODE_LMAP)
982 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100983#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 im_set_active(*b_im_ptr == B_IMODE_IM);
985#endif
986 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100987#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 else if (p_imcmdline)
989 im_set_active(TRUE);
990#endif
991
992#ifdef FEAT_MOUSE
993 setmouse();
994#endif
995#ifdef CURSOR_SHAPE
996 ui_cursor_shape(); /* may show different cursor shape */
997#endif
998
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000999 /* When inside an autocommand for writing "exiting" may be set and
1000 * terminal mode set to cooked. Need to set raw mode here then. */
1001 settmode(TMODE_RAW);
1002
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001003 /* Trigger CmdlineEnter autocommands. */
1004 cmdline_type = firstc == NUL ? '-' : firstc;
1005 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#ifdef FEAT_CMDHIST
1008 init_history();
1009 hiscnt = hislen; /* set hiscnt to impossible history value */
1010 histype = hist_char2type(firstc);
1011#endif
1012
1013#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +00001014 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001017 /* If something above caused an error, reset the flags, we do want to type
1018 * and execute commands. Display may be messed up a bit. */
1019 if (did_emsg)
1020 redrawcmd();
1021 did_emsg = FALSE;
1022 got_int = FALSE;
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 /*
1025 * Collect the command string, handling editing keys.
1026 */
1027 for (;;)
1028 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001029 redir_off = TRUE; /* Don't redirect the typed command.
1030 Repeated, because a ":redir" inside
1031 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#ifdef USE_ON_FLY_SCROLL
1033 dont_scroll = FALSE; /* allow scrolling here */
1034#endif
1035 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1036
Bram Moolenaar72532d32018-04-07 19:09:09 +02001037 did_emsg = FALSE; /* There can't really be a reason why an error
1038 that occurs while typing a command should
1039 cause the command not to be executed. */
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001042
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001043 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1044 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001045 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001046 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001047 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (KeyTyped)
1050 {
1051 some_key_typed = TRUE;
1052#ifdef FEAT_RIGHTLEFT
1053 if (cmd_hkmap)
1054 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (cmdmsg_rl && !KeyStuffed)
1056 {
1057 /* Invert horizontal movements and operations. Only when
1058 * typed by the user directly, not when the result of a
1059 * mapping. */
1060 switch (c)
1061 {
1062 case K_RIGHT: c = K_LEFT; break;
1063 case K_S_RIGHT: c = K_S_LEFT; break;
1064 case K_C_RIGHT: c = K_C_LEFT; break;
1065 case K_LEFT: c = K_RIGHT; break;
1066 case K_S_LEFT: c = K_S_RIGHT; break;
1067 case K_C_LEFT: c = K_C_RIGHT; break;
1068 }
1069 }
1070#endif
1071 }
1072
1073 /*
1074 * Ignore got_int when CTRL-C was typed here.
1075 * Don't ignore it in :global, we really need to break then, e.g., for
1076 * ":g/pat/normal /pat" (without the <CR>).
1077 * Don't ignore it for the input() function.
1078 */
1079 if ((c == Ctrl_C
1080#ifdef UNIX
1081 || c == intr_char
1082#endif
1083 )
1084#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1085 && firstc != '@'
1086#endif
1087#ifdef FEAT_EVAL
1088 && !break_ctrl_c
1089#endif
1090 && !global_busy)
1091 got_int = FALSE;
1092
1093#ifdef FEAT_CMDHIST
1094 /* free old command line when finished moving around in the history
1095 * list */
1096 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001097 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001098 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 && c != K_PAGEDOWN && c != K_PAGEUP
1100 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001101 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001103 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105
1106 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001107 * When there are matching completions to select <S-Tab> works like
1108 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001110 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 c = Ctrl_P;
1112
1113#ifdef FEAT_WILDMENU
1114 /* Special translations for 'wildmenu' */
1115 if (did_wild_list && p_wmnu)
1116 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001117 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001119 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 c = Ctrl_N;
1121 }
1122 /* Hitting CR after "emenu Name.": complete submenu */
1123 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1124 && ccline.cmdpos > 1
1125 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1126 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1127 && (c == '\n' || c == '\r' || c == K_KENTER))
1128 c = K_DOWN;
1129#endif
1130
1131 /* free expanded names when finished walking through matches */
1132 if (xpc.xp_numfiles != -1
1133 && !(c == p_wc && KeyTyped) && c != p_wcm
1134 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1135 && c != Ctrl_L)
1136 {
1137 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1138 did_wild_list = FALSE;
1139#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001140 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142 xpc.xp_context = EXPAND_NOTHING;
1143 wim_index = 0;
1144#ifdef FEAT_WILDMENU
1145 if (p_wmnu && wild_menu_showing != 0)
1146 {
1147 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001148 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001149
1150 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001151 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
1153 if (wild_menu_showing == WM_SCROLLED)
1154 {
1155 /* Entered command line, move it up */
1156 cmdline_row--;
1157 redrawcmd();
1158 }
1159 else if (save_p_ls != -1)
1160 {
1161 /* restore 'laststatus' and 'winminheight' */
1162 p_ls = save_p_ls;
1163 p_wmh = save_p_wmh;
1164 last_status(FALSE);
1165 update_screen(VALID); /* redraw the screen NOW */
1166 redrawcmd();
1167 save_p_ls = -1;
1168 }
1169 else
1170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 redraw_statuslines();
1173 }
1174 KeyTyped = skt;
1175 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001176 if (ccline.input_fn)
1177 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179#endif
1180 }
1181
1182#ifdef FEAT_WILDMENU
1183 /* Special translations for 'wildmenu' */
1184 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1185 {
1186 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001187 if (c == K_DOWN && ccline.cmdpos > 0
1188 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001190 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
1192 /* Hitting <Up>: Remove one submenu name in front of the
1193 * cursor */
1194 int found = FALSE;
1195
1196 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1197 i = 0;
1198 while (--j > 0)
1199 {
1200 /* check for start of menu name */
1201 if (ccline.cmdbuff[j] == ' '
1202 && ccline.cmdbuff[j - 1] != '\\')
1203 {
1204 i = j + 1;
1205 break;
1206 }
1207 /* check for start of submenu name */
1208 if (ccline.cmdbuff[j] == '.'
1209 && ccline.cmdbuff[j - 1] != '\\')
1210 {
1211 if (found)
1212 {
1213 i = j + 1;
1214 break;
1215 }
1216 else
1217 found = TRUE;
1218 }
1219 }
1220 if (i > 0)
1221 cmdline_del(i);
1222 c = p_wc;
1223 xpc.xp_context = EXPAND_NOTHING;
1224 }
1225 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001226 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001227 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001228 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
1230 char_u upseg[5];
1231
1232 upseg[0] = PATHSEP;
1233 upseg[1] = '.';
1234 upseg[2] = '.';
1235 upseg[3] = PATHSEP;
1236 upseg[4] = NUL;
1237
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001238 if (c == K_DOWN
1239 && ccline.cmdpos > 0
1240 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1241 && (ccline.cmdpos < 3
1242 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1244 {
1245 /* go down a directory */
1246 c = p_wc;
1247 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001248 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 /* If in a direct ancestor, strip off one ../ to go down */
1251 int found = FALSE;
1252
1253 j = ccline.cmdpos;
1254 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1255 while (--j > i)
1256 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001257 if (has_mbyte)
1258 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (vim_ispathsep(ccline.cmdbuff[j]))
1260 {
1261 found = TRUE;
1262 break;
1263 }
1264 }
1265 if (found
1266 && ccline.cmdbuff[j - 1] == '.'
1267 && ccline.cmdbuff[j - 2] == '.'
1268 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1269 {
1270 cmdline_del(j - 2);
1271 c = p_wc;
1272 }
1273 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001274 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
1276 /* go up a directory */
1277 int found = FALSE;
1278
1279 j = ccline.cmdpos - 1;
1280 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1281 while (--j > i)
1282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (has_mbyte)
1284 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (vim_ispathsep(ccline.cmdbuff[j])
1286#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001287 && vim_strchr((char_u *)" *?[{`$%#",
1288 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289#endif
1290 )
1291 {
1292 if (found)
1293 {
1294 i = j + 1;
1295 break;
1296 }
1297 else
1298 found = TRUE;
1299 }
1300 }
1301
1302 if (!found)
1303 j = i;
1304 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1305 j += 4;
1306 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1307 && j == i)
1308 j += 3;
1309 else
1310 j = 0;
1311 if (j > 0)
1312 {
1313 /* TODO this is only for DOS/UNIX systems - need to put in
1314 * machine-specific stuff here and in upseg init */
1315 cmdline_del(j);
1316 put_on_cmdline(upseg + 1, 3, FALSE);
1317 }
1318 else if (ccline.cmdpos > i)
1319 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001320
1321 /* Now complete in the new directory. Set KeyTyped in case the
1322 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001324 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328#endif /* FEAT_WILDMENU */
1329
1330 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1331 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1332 if (c == Ctrl_BSL)
1333 {
1334 ++no_mapping;
1335 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001336 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 --no_mapping;
1338 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001339 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1340 * is in a mapping. */
1341 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001342 || (ccline.cmdfirstc == '=' && KeyTyped)
1343#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001344 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001345#endif
1346 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 vungetc(c);
1349 c = Ctrl_BSL;
1350 }
1351#ifdef FEAT_EVAL
1352 else if (c == 'e')
1353 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001354 char_u *p = NULL;
1355 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 /*
1358 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001359 * Need to save and restore the current command line, to be
1360 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
1362 if (ccline.cmdpos == ccline.cmdlen)
1363 new_cmdpos = 99999; /* keep it at the end */
1364 else
1365 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (c == '=')
1369 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001370 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001371 * to avoid nasty things like going to another buffer when
1372 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001375 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001376
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001377 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001379 len = (int)STRLEN(p);
1380 if (realloc_cmdbuff(len + 1) == OK)
1381 {
1382 ccline.cmdlen = len;
1383 STRCPY(ccline.cmdbuff, p);
1384 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001386 /* Restore the cursor or use the position set with
1387 * set_cmdline_pos(). */
1388 if (new_cmdpos > ccline.cmdlen)
1389 ccline.cmdpos = ccline.cmdlen;
1390 else
1391 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001393 KeyTyped = FALSE; /* Don't do p_wc completion. */
1394 redrawcmd();
1395 goto cmdline_changed;
1396 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001397 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399 }
1400 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001401 got_int = FALSE; /* don't abandon the command line */
1402 did_emsg = FALSE;
1403 emsg_on_display = FALSE;
1404 redrawcmd();
1405 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
1407#endif
1408 else
1409 {
1410 if (c == Ctrl_G && p_im && restart_edit == 0)
1411 restart_edit = 'a';
1412 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1413 in history */
1414 goto returncmd; /* back to Normal mode */
1415 }
1416 }
1417
1418#ifdef FEAT_CMDWIN
1419 if (c == cedit_key || c == K_CMDWIN)
1420 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001421 if (ex_normal_busy == 0 && got_int == FALSE)
1422 {
1423 /*
1424 * Open a window to edit the command line (and history).
1425 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001426 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001427 some_key_typed = TRUE;
1428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
1430# ifdef FEAT_DIGRAPHS
1431 else
1432# endif
1433#endif
1434#ifdef FEAT_DIGRAPHS
1435 c = do_digraph(c);
1436#endif
1437
1438 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1439 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1440 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001441 /* In Ex mode a backslash escapes a newline. */
1442 if (exmode_active
1443 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001444 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001445 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001446 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001448 if (c == K_KENTER)
1449 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001451 else
1452 {
1453 gotesc = FALSE; /* Might have typed ESC previously, don't
1454 truncate the cmdline now. */
1455 if (ccheck_abbr(c + ABBR_OFF))
1456 goto cmdline_changed;
1457 if (!cmd_silent)
1458 {
1459 windgoto(msg_row, 0);
1460 out_flush();
1461 }
1462 break;
1463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465
1466 /*
1467 * Completion for 'wildchar' or 'wildcharm' key.
1468 * - hitting <ESC> twice means: abandon command line.
1469 * - wildcard expansion is only done when the 'wildchar' key is really
1470 * typed, not when it comes from a macro
1471 */
1472 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1473 {
1474 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1475 {
1476 /* if 'wildmode' contains "list" may still need to list */
1477 if (xpc.xp_numfiles > 1
1478 && !did_wild_list
1479 && (wim_flags[wim_index] & WIM_LIST))
1480 {
1481 (void)showmatches(&xpc, FALSE);
1482 redrawcmd();
1483 did_wild_list = TRUE;
1484 }
1485 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1487 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001489 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1490 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 else
1492 res = OK; /* don't insert 'wildchar' now */
1493 }
1494 else /* typed p_wc first time */
1495 {
1496 wim_index = 0;
1497 j = ccline.cmdpos;
1498 /* if 'wildmode' first contains "longest", get longest
1499 * common part */
1500 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001501 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1502 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001504 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1505 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
1507 /* if interrupted while completing, behave like it failed */
1508 if (got_int)
1509 {
1510 (void)vpeekc(); /* remove <C-C> from input stream */
1511 got_int = FALSE; /* don't abandon the command line */
1512 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1513#ifdef FEAT_WILDMENU
1514 xpc.xp_context = EXPAND_NOTHING;
1515#endif
1516 goto cmdline_changed;
1517 }
1518
1519 /* when more than one match, and 'wildmode' first contains
1520 * "list", or no change and 'wildmode' contains "longest,list",
1521 * list all matches */
1522 if (res == OK && xpc.xp_numfiles > 1)
1523 {
1524 /* a "longest" that didn't do anything is skipped (but not
1525 * "list:longest") */
1526 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1527 wim_index = 1;
1528 if ((wim_flags[wim_index] & WIM_LIST)
1529#ifdef FEAT_WILDMENU
1530 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1531#endif
1532 )
1533 {
1534 if (!(wim_flags[0] & WIM_LONGEST))
1535 {
1536#ifdef FEAT_WILDMENU
1537 int p_wmnu_save = p_wmnu;
1538 p_wmnu = 0;
1539#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001540 /* remove match */
1541 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#ifdef FEAT_WILDMENU
1543 p_wmnu = p_wmnu_save;
1544#endif
1545 }
1546#ifdef FEAT_WILDMENU
1547 (void)showmatches(&xpc, p_wmnu
1548 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1549#else
1550 (void)showmatches(&xpc, FALSE);
1551#endif
1552 redrawcmd();
1553 did_wild_list = TRUE;
1554 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001555 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1556 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001558 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1559 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 }
1561 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001562 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564#ifdef FEAT_WILDMENU
1565 else if (xpc.xp_numfiles == -1)
1566 xpc.xp_context = EXPAND_NOTHING;
1567#endif
1568 }
1569 if (wim_index < 3)
1570 ++wim_index;
1571 if (c == ESC)
1572 gotesc = TRUE;
1573 if (res == OK)
1574 goto cmdline_changed;
1575 }
1576
1577 gotesc = FALSE;
1578
1579 /* <S-Tab> goes to last match, in a clumsy way */
1580 if (c == K_S_TAB && KeyTyped)
1581 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001582 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1583 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1584 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 goto cmdline_changed;
1586 }
1587
1588 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1589 c = NL;
1590
1591 do_abbr = TRUE; /* default: check for abbreviation */
1592
1593 /*
1594 * Big switch for a typed command line character.
1595 */
1596 switch (c)
1597 {
1598 case K_BS:
1599 case Ctrl_H:
1600 case K_DEL:
1601 case K_KDEL:
1602 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (c == K_KDEL)
1604 c = K_DEL;
1605
1606 /*
1607 * delete current character is the same as backspace on next
1608 * character, except at end of line
1609 */
1610 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1611 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (has_mbyte && c == K_DEL)
1613 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1614 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (ccline.cmdpos > 0)
1616 {
1617 char_u *p;
1618
1619 j = ccline.cmdpos;
1620 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (has_mbyte)
1622 {
1623 p = mb_prevptr(ccline.cmdbuff, p);
1624 if (c == Ctrl_W)
1625 {
1626 while (p > ccline.cmdbuff && vim_isspace(*p))
1627 p = mb_prevptr(ccline.cmdbuff, p);
1628 i = mb_get_class(p);
1629 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1630 p = mb_prevptr(ccline.cmdbuff, p);
1631 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001632 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001635 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
1637 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1638 --p;
1639 i = vim_iswordc(p[-1]);
1640 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1641 && vim_iswordc(p[-1]) == i)
1642 --p;
1643 }
1644 else
1645 --p;
1646 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1647 ccline.cmdlen -= j - ccline.cmdpos;
1648 i = ccline.cmdpos;
1649 while (i < ccline.cmdlen)
1650 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1651
1652 /* Truncate at the end, required for multi-byte chars. */
1653 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001654#ifdef FEAT_SEARCH_EXTRA
1655 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001656 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001657 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001658 /* save view settings, so that the screen
1659 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001660 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001661 }
1662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 redrawcmd();
1664 }
1665 else if (ccline.cmdlen == 0 && c != Ctrl_W
1666 && ccline.cmdprompt == NULL && indent == 0)
1667 {
1668 /* In ex and debug mode it doesn't make sense to return. */
1669 if (exmode_active
1670#ifdef FEAT_EVAL
1671 || ccline.cmdfirstc == '>'
1672#endif
1673 )
1674 goto cmdline_not_changed;
1675
Bram Moolenaard23a8232018-02-10 18:45:26 +01001676 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (!cmd_silent)
1678 {
1679#ifdef FEAT_RIGHTLEFT
1680 if (cmdmsg_rl)
1681 msg_col = Columns;
1682 else
1683#endif
1684 msg_col = 0;
1685 msg_putchar(' '); /* delete ':' */
1686 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001687#ifdef FEAT_SEARCH_EXTRA
1688 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001689 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 redraw_cmdline = TRUE;
1692 goto returncmd; /* back to cmd mode */
1693 }
1694 goto cmdline_changed;
1695
1696 case K_INS:
1697 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 ccline.overstrike = !ccline.overstrike;
1699#ifdef CURSOR_SHAPE
1700 ui_cursor_shape(); /* may show different cursor shape */
1701#endif
1702 goto cmdline_not_changed;
1703
1704 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001705 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 {
1707 /* ":lmap" mappings exists, toggle use of mappings. */
1708 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001709#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 im_set_active(FALSE); /* Disable input method */
1711#endif
1712 if (b_im_ptr != NULL)
1713 {
1714 if (State & LANGMAP)
1715 *b_im_ptr = B_IMODE_LMAP;
1716 else
1717 *b_im_ptr = B_IMODE_NONE;
1718 }
1719 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
1722 {
1723 /* There are no ":lmap" mappings, toggle IM. When
1724 * 'imdisable' is set don't try getting the status, it's
1725 * always off. */
1726 if ((p_imdisable && b_im_ptr != NULL)
1727 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1728 {
1729 im_set_active(FALSE); /* Disable input method */
1730 if (b_im_ptr != NULL)
1731 *b_im_ptr = B_IMODE_NONE;
1732 }
1733 else
1734 {
1735 im_set_active(TRUE); /* Enable input method */
1736 if (b_im_ptr != NULL)
1737 *b_im_ptr = B_IMODE_IM;
1738 }
1739 }
1740#endif
1741 if (b_im_ptr != NULL)
1742 {
1743 if (b_im_ptr == &curbuf->b_p_iminsert)
1744 set_iminsert_global();
1745 else
1746 set_imsearch_global();
1747 }
1748#ifdef CURSOR_SHAPE
1749 ui_cursor_shape(); /* may show different cursor shape */
1750#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001751#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /* Show/unshow value of 'keymap' in status lines later. */
1753 status_redraw_curbuf();
1754#endif
1755 goto cmdline_not_changed;
1756
1757/* case '@': only in very old vi */
1758 case Ctrl_U:
1759 /* delete all characters left of the cursor */
1760 j = ccline.cmdpos;
1761 ccline.cmdlen -= j;
1762 i = ccline.cmdpos = 0;
1763 while (i < ccline.cmdlen)
1764 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1765 /* Truncate at the end, required for multi-byte chars. */
1766 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001767#ifdef FEAT_SEARCH_EXTRA
1768 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001769 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 redrawcmd();
1772 goto cmdline_changed;
1773
1774#ifdef FEAT_CLIPBOARD
1775 case Ctrl_Y:
1776 /* Copy the modeless selection, if there is one. */
1777 if (clip_star.state != SELECT_CLEARED)
1778 {
1779 if (clip_star.state == SELECT_DONE)
1780 clip_copy_modeless_selection(TRUE);
1781 goto cmdline_not_changed;
1782 }
1783 break;
1784#endif
1785
1786 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1787 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001788 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001789 * ":normal" runs out of characters. */
1790 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001791 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 goto cmdline_not_changed;
1793
1794 gotesc = TRUE; /* will free ccline.cmdbuff after
1795 putting it in history */
1796 goto returncmd; /* back to cmd mode */
1797
1798 case Ctrl_R: /* insert register */
1799#ifdef USE_ON_FLY_SCROLL
1800 dont_scroll = TRUE; /* disallow scrolling here */
1801#endif
1802 putcmdline('"', TRUE);
1803 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001804 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if (i == Ctrl_O)
1806 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1807 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001808 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001809 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 --no_mapping;
1811#ifdef FEAT_EVAL
1812 /*
1813 * Insert the result of an expression.
1814 * Need to save the current command line, to be able to enter
1815 * a new one...
1816 */
1817 new_cmdpos = -1;
1818 if (c == '=')
1819 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001820 if (ccline.cmdfirstc == '=' // can't do this recursively
1821 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 beep_flush();
1824 c = ESC;
1825 }
1826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829#endif
1830 if (c != ESC) /* use ESC to cancel inserting register */
1831 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001832 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001833
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001834#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001835 /* When there was a serious error abort getting the
1836 * command line. */
1837 if (aborting())
1838 {
1839 gotesc = TRUE; /* will free ccline.cmdbuff after
1840 putting it in history */
1841 goto returncmd; /* back to cmd mode */
1842 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 KeyTyped = FALSE; /* Don't do p_wc completion. */
1845#ifdef FEAT_EVAL
1846 if (new_cmdpos >= 0)
1847 {
1848 /* set_cmdline_pos() was used */
1849 if (new_cmdpos > ccline.cmdlen)
1850 ccline.cmdpos = ccline.cmdlen;
1851 else
1852 ccline.cmdpos = new_cmdpos;
1853 }
1854#endif
1855 }
1856 redrawcmd();
1857 goto cmdline_changed;
1858
1859 case Ctrl_D:
1860 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1861 break; /* Use ^D as normal char instead */
1862
1863 redrawcmd();
1864 continue; /* don't do incremental search now */
1865
1866 case K_RIGHT:
1867 case K_S_RIGHT:
1868 case K_C_RIGHT:
1869 do
1870 {
1871 if (ccline.cmdpos >= ccline.cmdlen)
1872 break;
1873 i = cmdline_charsize(ccline.cmdpos);
1874 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1875 break;
1876 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001878 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 + ccline.cmdpos);
1880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 ++ccline.cmdpos;
1882 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001883 while ((c == K_S_RIGHT || c == K_C_RIGHT
1884 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (has_mbyte)
1887 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 goto cmdline_not_changed;
1889
1890 case K_LEFT:
1891 case K_S_LEFT:
1892 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001893 if (ccline.cmdpos == 0)
1894 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 do
1896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (has_mbyte) /* move to first byte of char */
1899 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1900 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1902 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001903 while (ccline.cmdpos > 0
1904 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001905 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (has_mbyte)
1908 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 goto cmdline_not_changed;
1910
1911 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001912 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001913 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
Bram Moolenaar4f974752019-02-17 17:44:42 +01001915#ifdef FEAT_GUI_MSWIN
1916 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1917 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001918 case K_F4:
1919 if (mod_mask == MOD_MASK_ALT)
1920 {
1921 redrawcmd(); /* somehow the cmdline is cleared */
1922 goto cmdline_not_changed;
1923 }
1924 break;
1925#endif
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_MOUSE
1928 case K_MIDDLEDRAG:
1929 case K_MIDDLERELEASE:
1930 goto cmdline_not_changed; /* Ignore mouse */
1931
1932 case K_MIDDLEMOUSE:
1933# ifdef FEAT_GUI
1934 /* When GUI is active, also paste when 'mouse' is empty */
1935 if (!gui.in_use)
1936# endif
1937 if (!mouse_has(MOUSE_COMMAND))
1938 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001939# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001941 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001943# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001944 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 redrawcmd();
1946 goto cmdline_changed;
1947
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001948# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001950 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 redrawcmd();
1952 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 case K_LEFTDRAG:
1956 case K_LEFTRELEASE:
1957 case K_RIGHTDRAG:
1958 case K_RIGHTRELEASE:
1959 /* Ignore drag and release events when the button-down wasn't
1960 * seen before. */
1961 if (ignore_drag_release)
1962 goto cmdline_not_changed;
1963 /* FALLTHROUGH */
1964 case K_LEFTMOUSE:
1965 case K_RIGHTMOUSE:
1966 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1967 ignore_drag_release = TRUE;
1968 else
1969 ignore_drag_release = FALSE;
1970# ifdef FEAT_GUI
1971 /* When GUI is active, also move when 'mouse' is empty */
1972 if (!gui.in_use)
1973# endif
1974 if (!mouse_has(MOUSE_COMMAND))
1975 goto cmdline_not_changed; /* Ignore mouse */
1976# ifdef FEAT_CLIPBOARD
1977 if (mouse_row < cmdline_row && clip_star.available)
1978 {
1979 int button, is_click, is_drag;
1980
1981 /*
1982 * Handle modeless selection.
1983 */
1984 button = get_mouse_button(KEY2TERMCAP1(c),
1985 &is_click, &is_drag);
1986 if (mouse_model_popup() && button == MOUSE_LEFT
1987 && (mod_mask & MOD_MASK_SHIFT))
1988 {
1989 /* Translate shift-left to right button. */
1990 button = MOUSE_RIGHT;
1991 mod_mask &= ~MOD_MASK_SHIFT;
1992 }
1993 clip_modeless(button, is_click, is_drag);
1994 goto cmdline_not_changed;
1995 }
1996# endif
1997
1998 set_cmdspos();
1999 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2000 ++ccline.cmdpos)
2001 {
2002 i = cmdline_charsize(ccline.cmdpos);
2003 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2004 && mouse_col < ccline.cmdspos % Columns + i)
2005 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (has_mbyte)
2007 {
2008 /* Count ">" for double-wide char that doesn't fit. */
2009 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002010 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 + ccline.cmdpos) - 1;
2012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 ccline.cmdspos += i;
2014 }
2015 goto cmdline_not_changed;
2016
2017 /* Mouse scroll wheel: ignored here */
2018 case K_MOUSEDOWN:
2019 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002020 case K_MOUSELEFT:
2021 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /* Alternate buttons ignored here */
2023 case K_X1MOUSE:
2024 case K_X1DRAG:
2025 case K_X1RELEASE:
2026 case K_X2MOUSE:
2027 case K_X2DRAG:
2028 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002029 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 goto cmdline_not_changed;
2031
2032#endif /* FEAT_MOUSE */
2033
2034#ifdef FEAT_GUI
2035 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2036 case K_LEFTRELEASE_NM:
2037 goto cmdline_not_changed;
2038
2039 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002040 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
2042 gui_do_scroll();
2043 redrawcmd();
2044 }
2045 goto cmdline_not_changed;
2046
2047 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002048 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002050 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 redrawcmd();
2052 }
2053 goto cmdline_not_changed;
2054#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002055#ifdef FEAT_GUI_TABLINE
2056 case K_TABLINE:
2057 case K_TABMENU:
2058 /* Don't want to change any tabs here. Make sure the same tab
2059 * is still selected. */
2060 if (gui_use_tabline())
2061 gui_mch_set_curtab(tabpage_index(curtab));
2062 goto cmdline_not_changed;
2063#endif
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 case K_SELECT: /* end of Select mode mapping - ignore */
2066 goto cmdline_not_changed;
2067
2068 case Ctrl_B: /* begin of command line */
2069 case K_HOME:
2070 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 case K_S_HOME:
2072 case K_C_HOME:
2073 ccline.cmdpos = 0;
2074 set_cmdspos();
2075 goto cmdline_not_changed;
2076
2077 case Ctrl_E: /* end of command line */
2078 case K_END:
2079 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case K_S_END:
2081 case K_C_END:
2082 ccline.cmdpos = ccline.cmdlen;
2083 set_cmdspos_cursor();
2084 goto cmdline_not_changed;
2085
2086 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002087 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 break;
2089 goto cmdline_changed;
2090
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002091 case Ctrl_L:
2092#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002093 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002094 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002095#endif
2096
2097 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002098 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 break;
2100 goto cmdline_changed;
2101
2102 case Ctrl_N: /* next match */
2103 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002104 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002106 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2107 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002109 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002112 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 case K_UP:
2114 case K_DOWN:
2115 case K_S_UP:
2116 case K_S_DOWN:
2117 case K_PAGEUP:
2118 case K_KPAGEUP:
2119 case K_PAGEDOWN:
2120 case K_KPAGEDOWN:
2121 if (hislen == 0 || firstc == NUL) /* no history */
2122 goto cmdline_not_changed;
2123
2124 i = hiscnt;
2125
2126 /* save current command string so it can be restored later */
2127 if (lookfor == NULL)
2128 {
2129 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2130 goto cmdline_not_changed;
2131 lookfor[ccline.cmdpos] = NUL;
2132 }
2133
2134 j = (int)STRLEN(lookfor);
2135 for (;;)
2136 {
2137 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002138 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002139 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
2141 if (hiscnt == hislen) /* first time */
2142 hiscnt = hisidx[histype];
2143 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2144 hiscnt = hislen - 1;
2145 else if (hiscnt != hisidx[histype] + 1)
2146 --hiscnt;
2147 else /* at top of list */
2148 {
2149 hiscnt = i;
2150 break;
2151 }
2152 }
2153 else /* one step forwards */
2154 {
2155 /* on last entry, clear the line */
2156 if (hiscnt == hisidx[histype])
2157 {
2158 hiscnt = hislen;
2159 break;
2160 }
2161
2162 /* not on a history line, nothing to do */
2163 if (hiscnt == hislen)
2164 break;
2165 if (hiscnt == hislen - 1) /* wrap around */
2166 hiscnt = 0;
2167 else
2168 ++hiscnt;
2169 }
2170 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2171 {
2172 hiscnt = i;
2173 break;
2174 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002175 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002176 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 || STRNCMP(history[histype][hiscnt].hisstr,
2178 lookfor, (size_t)j) == 0)
2179 break;
2180 }
2181
2182 if (hiscnt != i) /* jumped to other entry */
2183 {
2184 char_u *p;
2185 int len;
2186 int old_firstc;
2187
Bram Moolenaar438d1762018-09-30 17:11:48 +02002188 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002189 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (hiscnt == hislen)
2191 p = lookfor; /* back to the old one */
2192 else
2193 p = history[histype][hiscnt].hisstr;
2194
2195 if (histype == HIST_SEARCH
2196 && p != lookfor
2197 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2198 {
2199 /* Correct for the separator character used when
2200 * adding the history entry vs the one used now.
2201 * First loop: count length.
2202 * Second loop: copy the characters. */
2203 for (i = 0; i <= 1; ++i)
2204 {
2205 len = 0;
2206 for (j = 0; p[j] != NUL; ++j)
2207 {
2208 /* Replace old sep with new sep, unless it is
2209 * escaped. */
2210 if (p[j] == old_firstc
2211 && (j == 0 || p[j - 1] != '\\'))
2212 {
2213 if (i > 0)
2214 ccline.cmdbuff[len] = firstc;
2215 }
2216 else
2217 {
2218 /* Escape new sep, unless it is already
2219 * escaped. */
2220 if (p[j] == firstc
2221 && (j == 0 || p[j - 1] != '\\'))
2222 {
2223 if (i > 0)
2224 ccline.cmdbuff[len] = '\\';
2225 ++len;
2226 }
2227 if (i > 0)
2228 ccline.cmdbuff[len] = p[j];
2229 }
2230 ++len;
2231 }
2232 if (i == 0)
2233 {
2234 alloc_cmdbuff(len);
2235 if (ccline.cmdbuff == NULL)
2236 goto returncmd;
2237 }
2238 }
2239 ccline.cmdbuff[len] = NUL;
2240 }
2241 else
2242 {
2243 alloc_cmdbuff((int)STRLEN(p));
2244 if (ccline.cmdbuff == NULL)
2245 goto returncmd;
2246 STRCPY(ccline.cmdbuff, p);
2247 }
2248
2249 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2250 redrawcmd();
2251 goto cmdline_changed;
2252 }
2253 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002254#endif
2255 goto cmdline_not_changed;
2256
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002257#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002258 case Ctrl_G: /* next match */
2259 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002260 if (may_adjust_incsearch_highlighting(
2261 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002262 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265
2266 case Ctrl_V:
2267 case Ctrl_Q:
2268#ifdef FEAT_MOUSE
2269 ignore_drag_release = TRUE;
2270#endif
2271 putcmdline('^', TRUE);
2272 c = get_literal(); /* get next (two) character(s) */
2273 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002274 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /* may need to remove ^ when composing char was typed */
2276 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2277 {
2278 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2279 msg_putchar(' ');
2280 cursorcmd();
2281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 break;
2283
2284#ifdef FEAT_DIGRAPHS
2285 case Ctrl_K:
2286#ifdef FEAT_MOUSE
2287 ignore_drag_release = TRUE;
2288#endif
2289 putcmdline('?', TRUE);
2290#ifdef USE_ON_FLY_SCROLL
2291 dont_scroll = TRUE; /* disallow scrolling here */
2292#endif
2293 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002294 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (c != NUL)
2296 break;
2297
2298 redrawcmd();
2299 goto cmdline_not_changed;
2300#endif /* FEAT_DIGRAPHS */
2301
2302#ifdef FEAT_RIGHTLEFT
2303 case Ctrl__: /* CTRL-_: switch language mode */
2304 if (!p_ari)
2305 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002306 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 goto cmdline_not_changed;
2308#endif
2309
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002310 case K_PS:
2311 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2312 goto cmdline_changed;
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 default:
2315#ifdef UNIX
2316 if (c == intr_char)
2317 {
2318 gotesc = TRUE; /* will free ccline.cmdbuff after
2319 putting it in history */
2320 goto returncmd; /* back to Normal mode */
2321 }
2322#endif
2323 /*
2324 * Normal character with no special meaning. Just set mod_mask
2325 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2326 * the string <S-Space>. This should only happen after ^V.
2327 */
2328 if (!IS_SPECIAL(c))
2329 mod_mask = 0x0;
2330 break;
2331 }
2332 /*
2333 * End of switch on command line character.
2334 * We come here if we have a normal character.
2335 */
2336
Bram Moolenaar13505972019-01-24 15:04:48 +01002337 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2338 && (ccheck_abbr(
2339 // Add ABBR_OFF for characters above 0x100, this is
2340 // what check_abbr() expects.
2341 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2342 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 goto cmdline_changed;
2344
2345 /*
2346 * put the character in the command line
2347 */
2348 if (IS_SPECIAL(c) || mod_mask != 0)
2349 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2350 else
2351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (has_mbyte)
2353 {
2354 j = (*mb_char2bytes)(c, IObuff);
2355 IObuff[j] = NUL; /* exclude composing chars */
2356 put_on_cmdline(IObuff, j, TRUE);
2357 }
2358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 IObuff[0] = c;
2361 put_on_cmdline(IObuff, 1, TRUE);
2362 }
2363 }
2364 goto cmdline_changed;
2365
2366/*
2367 * This part implements incremental searches for "/" and "?"
2368 * Jump to cmdline_not_changed when a character has been read but the command
2369 * line did not change. Then we only search and redraw if something changed in
2370 * the past.
2371 * Jump to cmdline_changed when the command line did change.
2372 * (Sorry for the goto's, I know it is ugly).
2373 */
2374cmdline_not_changed:
2375#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002376 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 continue;
2378#endif
2379
2380cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002381 /* Trigger CmdlineChanged autocommands. */
2382 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002385 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#endif
2387
2388#ifdef FEAT_RIGHTLEFT
2389 if (cmdmsg_rl
2390# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002391 || (p_arshape && !p_tbidi
2392 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393# endif
2394 )
2395 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002396 * right-left typing. Not efficient, but it works.
2397 * Do it only when there are no characters left to read
2398 * to avoid useless intermediate redraws. */
2399 if (vpeekc() == NUL)
2400 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401#endif
2402 }
2403
2404returncmd:
2405
2406#ifdef FEAT_RIGHTLEFT
2407 cmdmsg_rl = FALSE;
2408#endif
2409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002411 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002414 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#endif
2416
2417 if (ccline.cmdbuff != NULL)
2418 {
2419 /*
2420 * Put line in history buffer (":" and "=" only when it was typed).
2421 */
2422#ifdef FEAT_CMDHIST
2423 if (ccline.cmdlen && firstc != NUL
2424 && (some_key_typed || histype == HIST_SEARCH))
2425 {
2426 add_to_history(histype, ccline.cmdbuff, TRUE,
2427 histype == HIST_SEARCH ? firstc : NUL);
2428 if (firstc == ':')
2429 {
2430 vim_free(new_last_cmdline);
2431 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2432 }
2433 }
2434#endif
2435
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002436 if (gotesc)
2437 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439
2440 /*
2441 * If the screen was shifted up, redraw the whole screen (later).
2442 * If the line is too long, clear it, so ruler and shown command do
2443 * not get printed in the middle of it.
2444 */
2445 msg_check();
2446 msg_scroll = save_msg_scroll;
2447 redir_off = FALSE;
2448
2449 /* When the command line was typed, no need for a wait-return prompt. */
2450 if (some_key_typed)
2451 need_wait_return = FALSE;
2452
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002453 /* Trigger CmdlineLeave autocommands. */
2454 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002457#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2459 im_save_status(b_im_ptr);
2460 im_set_active(FALSE);
2461#endif
2462#ifdef FEAT_MOUSE
2463 setmouse();
2464#endif
2465#ifdef CURSOR_SHAPE
2466 ui_cursor_shape(); /* may show different cursor shape */
2467#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002468 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar438d1762018-09-30 17:11:48 +02002470theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002471 {
2472 char_u *p = ccline.cmdbuff;
2473
Bram Moolenaar438d1762018-09-30 17:11:48 +02002474 if (did_save_ccline)
2475 restore_cmdline(&save_ccline);
2476 else
2477 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002478 return p;
2479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480}
2481
2482#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2483/*
2484 * Get a command line with a prompt.
2485 * This is prepared to be called recursively from getcmdline() (e.g. by
2486 * f_input() when evaluating an expression from CTRL-R =).
2487 * Returns the command line in allocated memory, or NULL.
2488 */
2489 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002490getcmdline_prompt(
2491 int firstc,
2492 char_u *prompt, /* command line prompt */
2493 int attr, /* attributes for prompt */
2494 int xp_context, /* type of expansion */
2495 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 char_u *s;
2498 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002499 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002501 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar438d1762018-09-30 17:11:48 +02002503 if (ccline.cmdbuff != NULL)
2504 {
2505 // Save the values of the current cmdline and restore them below.
2506 save_cmdline(&save_ccline);
2507 did_save_ccline = TRUE;
2508 }
2509
2510 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 ccline.cmdprompt = prompt;
2512 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002513# ifdef FEAT_EVAL
2514 ccline.xp_context = xp_context;
2515 ccline.xp_arg = xp_arg;
2516 ccline.input_fn = (firstc == '@');
2517# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002518 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002519 s = getcmdline_int(firstc, 1L, 0, FALSE);
2520
2521 if (did_save_ccline)
2522 restore_cmdline(&save_ccline);
2523
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002524 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002525 /* Restore msg_col, the prompt from input() may have changed it.
2526 * But only if called recursively and the commandline is therefore being
2527 * restored to an old one; if not, the input() prompt stays on the screen,
2528 * so we need its modified msg_col left intact. */
2529 if (ccline.cmdbuff != NULL)
2530 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 return s;
2533}
2534#endif
2535
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002536/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002537 * Return TRUE when the text must not be changed and we can't switch to
2538 * another window or buffer. Used when editing the command line, evaluating
2539 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002540 */
2541 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002542text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002543{
2544#ifdef FEAT_CMDWIN
2545 if (cmdwin_type != 0)
2546 return TRUE;
2547#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002548 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549}
2550
2551/*
2552 * Give an error message for a command that isn't allowed while the cmdline
2553 * window is open or editing the cmdline in another way.
2554 */
2555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002556text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002557{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002558 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002559}
2560
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002562get_text_locked_msg(void)
2563{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002564#ifdef FEAT_CMDWIN
2565 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002566 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002568 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569}
2570
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002571/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002572 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2573 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002574 */
2575 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002577{
2578 if (curbuf_lock > 0)
2579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002581 return TRUE;
2582 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002583 return allbuf_locked();
2584}
2585
2586/*
2587 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2588 * message.
2589 */
2590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002591allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002592{
2593 if (allbuf_lock > 0)
2594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002595 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002596 return TRUE;
2597 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002598 return FALSE;
2599}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2605 if (cmdline_star > 0) /* showing '*', always 1 position */
2606 return 1;
2607#endif
2608 return ptr2cells(ccline.cmdbuff + idx);
2609}
2610
2611/*
2612 * Compute the offset of the cursor on the command line for the prompt and
2613 * indent.
2614 */
2615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002616set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002618 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ccline.cmdspos = 1 + ccline.cmdindent;
2620 else
2621 ccline.cmdspos = 0 + ccline.cmdindent;
2622}
2623
2624/*
2625 * Compute the screen position for the cursor on the command line.
2626 */
2627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002628set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 int i, m, c;
2631
2632 set_cmdspos();
2633 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002636 if (m < 0) /* overflow, Columns or Rows at weird value */
2637 m = MAXCOL;
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 else
2640 m = MAXCOL;
2641 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2642 {
2643 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2645 if (has_mbyte)
2646 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002647 /* If the cmdline doesn't fit, show cursor on last visible char.
2648 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if ((ccline.cmdspos += c) >= m)
2650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ccline.cmdspos -= c;
2652 break;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002655 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657}
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659/*
2660 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2661 * character that doesn't fit, so that a ">" must be displayed.
2662 */
2663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002664correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2668 && ccline.cmdspos % Columns + cells > Columns)
2669 ccline.cmdspos++;
2670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672/*
2673 * Get an Ex command line for the ":" command.
2674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002676getexline(
2677 int c, /* normally ':', NUL for ":append" */
2678 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002679 int indent, /* indent for inside conditionals */
2680 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 /* When executing a register, remove ':' that's in front of each line. */
2683 if (exec_from_reg && vpeekc() == ':')
2684 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002685 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686}
2687
2688/*
2689 * Get an Ex command line for Ex mode.
2690 * In Ex mode we only use the OS supplied line editing features and no
2691 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002692 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002695getexmodeline(
2696 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002698 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002699 int indent, /* indent for inside conditionals */
2700 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 garray_T line_ga;
2703 char_u *pend;
2704 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002705 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 int escaped = FALSE; /* CTRL-V typed */
2707 int vcol = 0;
2708 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002709 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002710 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 /* Switch cursor on now. This avoids that it happens after the "\n", which
2713 * confuses the system function that computes tabstops. */
2714 cursor_on();
2715
2716 /* always start in column 0; write a newline if necessary */
2717 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002722 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002723 if (p_prompt)
2724 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 while (indent-- > 0)
2726 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
2729
2730 ga_init2(&line_ga, 1, 30);
2731
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002732 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002734 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002735 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002736 while (indent >= 8)
2737 {
2738 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002739 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002740 indent -= 8;
2741 }
2742 while (indent-- > 0)
2743 {
2744 ga_append(&line_ga, ' ');
2745 msg_putchar(' ');
2746 }
2747 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748 ++no_mapping;
2749 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 /*
2752 * Get the line, one character at a time.
2753 */
2754 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002757 long sw;
2758 char_u *s;
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (ga_grow(&line_ga, 40) == FAIL)
2761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaarba748c82017-02-26 14:00:07 +01002763 /*
2764 * Get one character at a time.
2765 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002766 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002767
2768 /* Check for a ":normal" command and no more characters left. */
2769 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2770 c1 = '\n';
2771 else
2772 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 * Handle line editing.
2776 * Previously this was left to the system, putting the terminal in
2777 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 msg_putchar('\n');
2782 break;
2783 }
2784
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002785 if (c1 == K_PS)
2786 {
2787 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2788 goto redraw;
2789 }
2790
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 if (!escaped)
2792 {
2793 /* CR typed means "enter", which is NL */
2794 if (c1 == '\r')
2795 c1 = '\n';
2796
2797 if (c1 == BS || c1 == K_BS
2798 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 if (line_ga.ga_len > 0)
2801 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002802 if (has_mbyte)
2803 {
2804 p = (char_u *)line_ga.ga_data;
2805 p[line_ga.ga_len] = NUL;
2806 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2807 line_ga.ga_len -= len;
2808 }
2809 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002810 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 goto redraw;
2812 }
2813 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 msg_col = startcol;
2819 msg_clr_eos();
2820 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002821 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 }
2823
2824 if (c1 == Ctrl_T)
2825 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002826 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 p = (char_u *)line_ga.ga_data;
2828 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002829 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002830 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002832 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002834 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002835 p = (char_u *)line_ga.ga_data;
2836 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2838 *s = ' ';
2839 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841redraw:
2842 /* redraw the line */
2843 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002845 p = (char_u *)line_ga.ga_data;
2846 p[line_ga.ga_len] = NUL;
2847 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002853 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002854 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002858 len = MB_PTR2LEN(p);
2859 msg_outtrans_len(p, len);
2860 vcol += ptr2cells(p);
2861 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002865 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866 continue;
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 if (c1 == Ctrl_D)
2870 {
2871 /* Delete one shiftwidth. */
2872 p = (char_u *)line_ga.ga_data;
2873 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875 if (prev_char == '^')
2876 ex_keep_indent = TRUE;
2877 indent = 0;
2878 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880 else
2881 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002883 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002884 if (indent > 0)
2885 {
2886 --indent;
2887 indent -= indent % get_sw_value(curbuf);
2888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002890 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002892 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002893 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2894 --line_ga.ga_len;
2895 }
2896 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898
2899 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2900 {
2901 escaped = TRUE;
2902 continue;
2903 }
2904
2905 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2906 if (IS_SPECIAL(c1))
2907 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909
2910 if (IS_SPECIAL(c1))
2911 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002912 if (has_mbyte)
2913 len = (*mb_char2bytes)(c1,
2914 (char_u *)line_ga.ga_data + line_ga.ga_len);
2915 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002916 {
2917 len = 1;
2918 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2919 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 if (c1 == '\n')
2921 msg_putchar('\n');
2922 else if (c1 == TAB)
2923 {
2924 /* Don't use chartabsize(), 'ts' can be different */
2925 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002927 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002932 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002935 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936 escaped = FALSE;
2937
2938 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002939 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002940
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002941 /* We are done when a NL is entered, but not when it comes after an
2942 * odd number of backslashes, that results in a NUL. */
2943 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002945 int bcount = 0;
2946
2947 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2948 ++bcount;
2949
2950 if (bcount > 0)
2951 {
2952 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2953 * "\NL", etc. */
2954 line_ga.ga_len -= (bcount + 1) / 2;
2955 pend -= (bcount + 1) / 2;
2956 pend[-1] = '\n';
2957 }
2958
2959 if ((bcount & 1) == 0)
2960 {
2961 --line_ga.ga_len;
2962 --pend;
2963 *pend = NUL;
2964 break;
2965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
2967 }
2968
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002969 --no_mapping;
2970 --allow_keys;
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 /* make following messages go to the next line */
2973 msg_didout = FALSE;
2974 msg_col = 0;
2975 if (msg_row < Rows - 1)
2976 ++msg_row;
2977 emsg_on_display = FALSE; /* don't want ui_delay() */
2978
2979 if (got_int)
2980 ga_clear(&line_ga);
2981
2982 return (char_u *)line_ga.ga_data;
2983}
2984
Bram Moolenaare344bea2005-09-01 20:46:49 +00002985# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2986 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
2988 * Return TRUE if ccline.overstrike is on.
2989 */
2990 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002991cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 return ccline.overstrike;
2994}
2995
2996/*
2997 * Return TRUE if the cursor is at the end of the cmdline.
2998 */
2999 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003000cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 return (ccline.cmdpos >= ccline.cmdlen);
3003}
3004#endif
3005
Bram Moolenaar9372a112005-12-06 19:59:18 +00003006#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007/*
3008 * Return the virtual column number at the current cursor position.
3009 * This is used by the IM code to obtain the start of the preedit string.
3010 */
3011 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3015 return MAXCOL;
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (has_mbyte)
3018 {
3019 colnr_T col;
3020 int i = 0;
3021
3022 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003023 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 return col;
3026 }
3027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 return ccline.cmdpos;
3029}
3030#endif
3031
3032#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3033/*
3034 * If part of the command line is an IM preedit string, redraw it with
3035 * IM feedback attributes. The cursor position is restored after drawing.
3036 */
3037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003038redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
3040 if ((State & CMDLINE)
3041 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003042 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 && !p_imdisable
3044 && im_is_preediting())
3045 {
3046 int cmdpos = 0;
3047 int cmdspos;
3048 int old_row;
3049 int old_col;
3050 colnr_T col;
3051
3052 old_row = msg_row;
3053 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003054 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (has_mbyte)
3057 {
3058 for (col = 0; col < preedit_start_col
3059 && cmdpos < ccline.cmdlen; ++col)
3060 {
3061 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003062 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064 }
3065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
3067 cmdspos += preedit_start_col;
3068 cmdpos += preedit_start_col;
3069 }
3070
3071 msg_row = cmdline_row + (cmdspos / (int)Columns);
3072 msg_col = cmdspos % (int)Columns;
3073 if (msg_row >= Rows)
3074 msg_row = Rows - 1;
3075
3076 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3077 {
3078 int char_len;
3079 int char_attr;
3080
3081 char_attr = im_get_feedback_attr(col);
3082 if (char_attr < 0)
3083 break; /* end of preedit string */
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003086 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 char_len = 1;
3089
3090 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3091 cmdpos += char_len;
3092 }
3093
3094 msg_row = old_row;
3095 msg_col = old_col;
3096 }
3097}
3098#endif /* FEAT_XIM && FEAT_GUI_GTK */
3099
3100/*
3101 * Allocate a new command line buffer.
3102 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003105alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 /*
3108 * give some extra space to avoid having to allocate all the time
3109 */
3110 if (len < 80)
3111 len = 100;
3112 else
3113 len += 20;
3114
3115 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3116 ccline.cmdbufflen = len;
3117}
3118
3119/*
3120 * Re-allocate the command line to length len + something extra.
3121 * return FAIL for failure, OK otherwise
3122 */
3123 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003124realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126 char_u *p;
3127
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003128 if (len < ccline.cmdbufflen)
3129 return OK; /* no need to resize */
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 p = ccline.cmdbuff;
3132 alloc_cmdbuff(len); /* will get some more */
3133 if (ccline.cmdbuff == NULL) /* out of memory */
3134 {
3135 ccline.cmdbuff = p; /* keep the old one */
3136 return FAIL;
3137 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003138 /* There isn't always a NUL after the command, but it may need to be
3139 * there, thus copy up to the NUL and add a NUL. */
3140 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3141 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003143
3144 if (ccline.xpc != NULL
3145 && ccline.xpc->xp_pattern != NULL
3146 && ccline.xpc->xp_context != EXPAND_NOTHING
3147 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3148 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003149 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003150
3151 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3152 * to point into the newly allocated memory. */
3153 if (i >= 0 && i <= ccline.cmdlen)
3154 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3155 }
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return OK;
3158}
3159
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003160#if defined(FEAT_ARABIC) || defined(PROTO)
3161static char_u *arshape_buf = NULL;
3162
3163# if defined(EXITFREE) || defined(PROTO)
3164 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003165free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003166{
3167 vim_free(arshape_buf);
3168}
3169# endif
3170#endif
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172/*
3173 * Draw part of the cmdline at the current cursor position. But draw stars
3174 * when cmdline_star is TRUE.
3175 */
3176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003177draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3180 int i;
3181
3182 if (cmdline_star > 0)
3183 for (i = 0; i < len; ++i)
3184 {
3185 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003187 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189 else
3190#endif
3191#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003192 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 static int buflen = 0;
3195 char_u *p;
3196 int j;
3197 int newlen = 0;
3198 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003199 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 int prev_c = 0;
3201 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003202 int u8c;
3203 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 /*
3207 * Do arabic shaping into a temporary buffer. This is very
3208 * inefficient!
3209 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003210 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 /* Re-allocate the buffer. We keep it around to avoid a lot of
3213 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003215 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003216 arshape_buf = alloc(buflen);
3217 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 return; /* out of memory */
3219 }
3220
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003221 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3222 {
3223 /* Prepend a space to draw the leading composing char on. */
3224 arshape_buf[0] = ' ';
3225 newlen = 1;
3226 }
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 for (j = start; j < start + len; j += mb_l)
3229 {
3230 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003231 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003232 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (ARABIC_CHAR(u8c))
3234 {
3235 /* Do Arabic shaping. */
3236 if (cmdmsg_rl)
3237 {
3238 /* displaying from right to left */
3239 pc = prev_c;
3240 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003241 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 if (j + mb_l >= start + len)
3243 nc = NUL;
3244 else
3245 nc = utf_ptr2char(p + mb_l);
3246 }
3247 else
3248 {
3249 /* displaying from left to right */
3250 if (j + mb_l >= start + len)
3251 pc = NUL;
3252 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 {
3254 int pcc[MAX_MCO];
3255
3256 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 pc1 = pcc[0];
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 nc = prev_c;
3261 }
3262 prev_c = u8c;
3263
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003264 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003267 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003269 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3270 if (u8cc[1] != 0)
3271 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 }
3275 else
3276 {
3277 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 newlen += mb_l;
3280 }
3281 }
3282
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003283 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285 else
3286#endif
3287 msg_outtrans_len(ccline.cmdbuff + start, len);
3288}
3289
3290/*
3291 * Put a character on the command line. Shifts the following text to the
3292 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3293 * "c" must be printable (fit in one display cell)!
3294 */
3295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003296putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 if (cmd_silent)
3299 return;
3300 msg_no_more = TRUE;
3301 msg_putchar(c);
3302 if (shift)
3303 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3304 msg_no_more = FALSE;
3305 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003306 extra_char = c;
3307 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308}
3309
3310/*
3311 * Undo a putcmdline(c, FALSE).
3312 */
3313 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003314unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 if (cmd_silent)
3317 return;
3318 msg_no_more = TRUE;
3319 if (ccline.cmdlen == ccline.cmdpos)
3320 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003321 else if (has_mbyte)
3322 draw_cmdline(ccline.cmdpos,
3323 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 else
3325 draw_cmdline(ccline.cmdpos, 1);
3326 msg_no_more = FALSE;
3327 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003328 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329}
3330
3331/*
3332 * Put the given string, of the given length, onto the command line.
3333 * If len is -1, then STRLEN() is used to calculate the length.
3334 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3335 * part will be redrawn, otherwise it will not. If this function is called
3336 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3337 * called afterwards.
3338 */
3339 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003340put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int retval;
3343 int i;
3344 int m;
3345 int c;
3346
3347 if (len < 0)
3348 len = (int)STRLEN(str);
3349
3350 /* Check if ccline.cmdbuff needs to be longer */
3351 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003352 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 else
3354 retval = OK;
3355 if (retval == OK)
3356 {
3357 if (!ccline.overstrike)
3358 {
3359 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3360 ccline.cmdbuff + ccline.cmdpos,
3361 (size_t)(ccline.cmdlen - ccline.cmdpos));
3362 ccline.cmdlen += len;
3363 }
3364 else
3365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (has_mbyte)
3367 {
3368 /* Count nr of characters in the new string. */
3369 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003370 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 ++m;
3372 /* Count nr of bytes in cmdline that are overwritten by these
3373 * characters. */
3374 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003375 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 --m;
3377 if (i < ccline.cmdlen)
3378 {
3379 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3380 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3381 ccline.cmdlen += ccline.cmdpos + len - i;
3382 }
3383 else
3384 ccline.cmdlen = ccline.cmdpos + len;
3385 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003386 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 ccline.cmdlen = ccline.cmdpos + len;
3388 }
3389 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3390 ccline.cmdbuff[ccline.cmdlen] = NUL;
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (enc_utf8)
3393 {
3394 /* When the inserted text starts with a composing character,
3395 * backup to the character before it. There could be two of them.
3396 */
3397 i = 0;
3398 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3399 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3400 {
3401 i = (*mb_head_off)(ccline.cmdbuff,
3402 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3403 ccline.cmdpos -= i;
3404 len += i;
3405 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3406 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003407#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3409 {
3410 /* Check the previous character for Arabic combining pair. */
3411 i = (*mb_head_off)(ccline.cmdbuff,
3412 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3413 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3414 + ccline.cmdpos - i), c))
3415 {
3416 ccline.cmdpos -= i;
3417 len += i;
3418 }
3419 else
3420 i = 0;
3421 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (i != 0)
3424 {
3425 /* Also backup the cursor position. */
3426 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3427 ccline.cmdspos -= i;
3428 msg_col -= i;
3429 if (msg_col < 0)
3430 {
3431 msg_col += Columns;
3432 --msg_row;
3433 }
3434 }
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 if (redraw && !cmd_silent)
3438 {
3439 msg_no_more = TRUE;
3440 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003441 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3443 /* Avoid clearing the rest of the line too often. */
3444 if (cmdline_row != i || ccline.overstrike)
3445 msg_clr_eos();
3446 msg_no_more = FALSE;
3447 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003448 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003450 m = Columns * Rows;
3451 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003453 }
3454 else
3455 m = MAXCOL;
3456 for (i = 0; i < len; ++i)
3457 {
3458 c = cmdline_charsize(ccline.cmdpos);
3459 /* count ">" for a double-wide char that doesn't fit. */
3460 if (has_mbyte)
3461 correct_cmdspos(ccline.cmdpos, c);
3462 /* Stop cursor at the end of the screen, but do increment the
3463 * insert position, so that entering a very long command
3464 * works, even though you can't see it. */
3465 if (ccline.cmdspos + c < m)
3466 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003467
Bram Moolenaar14184a32019-02-16 15:10:30 +01003468 if (has_mbyte)
3469 {
3470 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3471 if (c > len - i - 1)
3472 c = len - i - 1;
3473 ccline.cmdpos += c;
3474 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003476 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
3478 }
3479 if (redraw)
3480 msg_check();
3481 return retval;
3482}
3483
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003484static struct cmdline_info prev_ccline;
3485static int prev_ccline_used = FALSE;
3486
3487/*
3488 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3489 * and overwrite it. But get_cmdline_str() may need it, thus make it
3490 * available globally in prev_ccline.
3491 */
3492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003493save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003494{
3495 if (!prev_ccline_used)
3496 {
3497 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3498 prev_ccline_used = TRUE;
3499 }
3500 *ccp = prev_ccline;
3501 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003502 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003503}
3504
3505/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003506 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003507 */
3508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003509restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003510{
3511 ccline = prev_ccline;
3512 prev_ccline = *ccp;
3513}
3514
Bram Moolenaar8299df92004-07-10 09:47:34 +00003515/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003516 * Paste a yank register into the command line.
3517 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003518 * insert_reg() can't be used here, because special characters from the
3519 * register contents will be interpreted as commands.
3520 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003521 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522 */
3523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003524cmdline_paste(
3525 int regname,
3526 int literally, /* Insert text literally instead of "as typed" */
3527 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003528{
3529 long i;
3530 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003531 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003532 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533
3534 /* check for valid regname; also accept special characters for CTRL-R in
3535 * the command line */
3536 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003537 && regname != Ctrl_A && regname != Ctrl_L
3538 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539 return FAIL;
3540
3541 /* A register containing CTRL-R can cause an endless loop. Allow using
3542 * CTRL-C to break the loop. */
3543 line_breakcheck();
3544 if (got_int)
3545 return FAIL;
3546
3547#ifdef FEAT_CLIPBOARD
3548 regname = may_get_selection(regname);
3549#endif
3550
Bram Moolenaar438d1762018-09-30 17:11:48 +02003551 // Need to set "textlock" to avoid nasty things like going to another
3552 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003553 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003554 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003555 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003556
3557 if (i)
3558 {
3559 /* Got the value of a special register in "arg". */
3560 if (arg == NULL)
3561 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003562
3563 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3564 * part of the word. */
3565 p = arg;
3566 if (p_is && regname == Ctrl_W)
3567 {
3568 char_u *w;
3569 int len;
3570
3571 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003572 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003573 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003574 if (has_mbyte)
3575 {
3576 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3577 if (!vim_iswordc(mb_ptr2char(w - len)))
3578 break;
3579 w -= len;
3580 }
3581 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003582 {
3583 if (!vim_iswordc(w[-1]))
3584 break;
3585 --w;
3586 }
3587 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003588 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003589 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3590 p += len;
3591 }
3592
3593 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003594 if (allocated)
3595 vim_free(arg);
3596 return OK;
3597 }
3598
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003599 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003600}
3601
3602/*
3603 * Put a string on the command line.
3604 * When "literally" is TRUE, insert literally.
3605 * When "literally" is FALSE, insert as typed, but don't leave the command
3606 * line.
3607 */
3608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003609cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610{
3611 int c, cv;
3612
3613 if (literally)
3614 put_on_cmdline(s, -1, TRUE);
3615 else
3616 while (*s != NUL)
3617 {
3618 cv = *s;
3619 if (cv == Ctrl_V && s[1])
3620 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003621 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003622 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003623 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003624 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003625 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3626 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003627#ifdef UNIX
3628 || c == intr_char
3629#endif
3630 || (c == Ctrl_BSL && *s == Ctrl_N))
3631 stuffcharReadbuff(Ctrl_V);
3632 stuffcharReadbuff(c);
3633 }
3634}
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#ifdef FEAT_WILDMENU
3637/*
3638 * Delete characters on the command line, from "from" to the current
3639 * position.
3640 */
3641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003642cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3645 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3646 ccline.cmdlen -= ccline.cmdpos - from;
3647 ccline.cmdpos = from;
3648}
3649#endif
3650
3651/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003652 * This function is called when the screen size changes and with incremental
3653 * search and in other situations where the command line may have been
3654 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
3656 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003657redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003659 redrawcmdline_ex(TRUE);
3660}
3661
3662 void
3663redrawcmdline_ex(int do_compute_cmdrow)
3664{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (cmd_silent)
3666 return;
3667 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003668 if (do_compute_cmdrow)
3669 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 redrawcmd();
3671 cursorcmd();
3672}
3673
3674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003675redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 int i;
3678
3679 if (cmd_silent)
3680 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003681 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 msg_putchar(ccline.cmdfirstc);
3683 if (ccline.cmdprompt != NULL)
3684 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003685 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3687 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003688 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 --ccline.cmdindent;
3690 }
3691 else
3692 for (i = ccline.cmdindent; i > 0; --i)
3693 msg_putchar(' ');
3694}
3695
3696/*
3697 * Redraw what is currently on the command line.
3698 */
3699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003700redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 if (cmd_silent)
3703 return;
3704
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003705 /* when 'incsearch' is set there may be no command line while redrawing */
3706 if (ccline.cmdbuff == NULL)
3707 {
3708 windgoto(cmdline_row, 0);
3709 msg_clr_eos();
3710 return;
3711 }
3712
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 msg_start();
3714 redrawcmdprompt();
3715
3716 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3717 msg_no_more = TRUE;
3718 draw_cmdline(0, ccline.cmdlen);
3719 msg_clr_eos();
3720 msg_no_more = FALSE;
3721
3722 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003723 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003724 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 /*
3727 * An emsg() before may have set msg_scroll. This is used in normal mode,
3728 * in cmdline mode we can reset them now.
3729 */
3730 msg_scroll = FALSE; /* next message overwrites cmdline */
3731
3732 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3733 * in cmdline mode */
3734 skip_redraw = FALSE;
3735}
3736
3737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003738compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003740 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 cmdline_row = Rows - 1;
3742 else
3743 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003744 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745}
3746
3747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
3750 if (cmd_silent)
3751 return;
3752
3753#ifdef FEAT_RIGHTLEFT
3754 if (cmdmsg_rl)
3755 {
3756 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3757 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3758 if (msg_row <= 0)
3759 msg_row = Rows - 1;
3760 }
3761 else
3762#endif
3763 {
3764 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3765 msg_col = ccline.cmdspos % (int)Columns;
3766 if (msg_row >= Rows)
3767 msg_row = Rows - 1;
3768 }
3769
3770 windgoto(msg_row, msg_col);
3771#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003772 if (p_imst == IM_ON_THE_SPOT)
3773 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#endif
3775#ifdef MCH_CURSOR_SHAPE
3776 mch_update_cursor();
3777#endif
3778}
3779
3780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003781gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 msg_start();
3784#ifdef FEAT_RIGHTLEFT
3785 if (cmdmsg_rl)
3786 msg_col = Columns - 1;
3787 else
3788#endif
3789 msg_col = 0; /* always start in column 0 */
3790 if (clr) /* clear the bottom line(s) */
3791 msg_clr_eos(); /* will reset clear_cmdline */
3792 windgoto(cmdline_row, 0);
3793}
3794
3795/*
3796 * Check the word in front of the cursor for an abbreviation.
3797 * Called when the non-id character "c" has been entered.
3798 * When an abbreviation is recognized it is removed from the text with
3799 * backspaces and the replacement string is inserted, followed by "c".
3800 */
3801 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003802ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003804 int spos = 0;
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3807 return FALSE;
3808
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003809 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3810 * Actually accepts any mark. */
3811 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3812 spos++;
3813 if (ccline.cmdlen - spos > 5
3814 && ccline.cmdbuff[spos] == '\''
3815 && ccline.cmdbuff[spos + 2] == ','
3816 && ccline.cmdbuff[spos + 3] == '\'')
3817 spos += 5;
3818 else
3819 /* check abbreviation from the beginning of the commandline */
3820 spos = 0;
3821
3822 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003825#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3826 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003827sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003828{
3829 char_u *p1 = *(char_u **)s1;
3830 char_u *p2 = *(char_u **)s2;
3831
3832 if (*p1 != '<' && *p2 == '<') return -1;
3833 if (*p1 == '<' && *p2 != '<') return 1;
3834 return STRCMP(p1, p2);
3835}
3836#endif
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838/*
3839 * Return FAIL if this is not an appropriate context in which to do
3840 * completion of anything, return OK if it is (even if there are no matches).
3841 * For the caller, this means that the character is just passed through like a
3842 * normal character (instead of being expanded). This allows :s/^I^D etc.
3843 */
3844 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003845nextwild(
3846 expand_T *xp,
3847 int type,
3848 int options, /* extra options for ExpandOne() */
3849 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 int i, j;
3852 char_u *p1;
3853 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 int difflen;
3855 int v;
3856
3857 if (xp->xp_numfiles == -1)
3858 {
3859 set_expand_context(xp);
3860 cmd_showtail = expand_showtail(xp);
3861 }
3862
3863 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3864 {
3865 beep_flush();
3866 return OK; /* Something illegal on command line */
3867 }
3868 if (xp->xp_context == EXPAND_NOTHING)
3869 {
3870 /* Caller can use the character as a normal char instead */
3871 return FAIL;
3872 }
3873
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 out_flush();
3876
3877 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003878 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 if (type == WILD_NEXT || type == WILD_PREV)
3881 {
3882 /*
3883 * Get next/previous match for a previous expanded pattern.
3884 */
3885 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3886 }
3887 else
3888 {
3889 /*
3890 * Translate string into pattern and expand it.
3891 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003892 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3893 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 p2 = NULL;
3895 else
3896 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003897 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003898 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3899 if (escape)
3900 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003901
3902 if (p_wic)
3903 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003904 p2 = ExpandOne(xp, p1,
3905 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003906 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003908 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (p2 != NULL && type == WILD_LONGEST)
3910 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003911 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (ccline.cmdbuff[i + j] == '*'
3913 || ccline.cmdbuff[i + j] == '?')
3914 break;
3915 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003916 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 }
3919 }
3920
3921 if (p2 != NULL && !got_int)
3922 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003923 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003924 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003926 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 xp->xp_pattern = ccline.cmdbuff + i;
3928 }
3929 else
3930 v = OK;
3931 if (v == OK)
3932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3934 &ccline.cmdbuff[ccline.cmdpos],
3935 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3936 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 ccline.cmdlen += difflen;
3938 ccline.cmdpos += difflen;
3939 }
3940 }
3941 vim_free(p2);
3942
3943 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003944 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 /* When expanding a ":map" command and no matches are found, assume that
3947 * the key is supposed to be inserted literally */
3948 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3949 return FAIL;
3950
3951 if (xp->xp_numfiles <= 0 && p2 == NULL)
3952 beep_flush();
3953 else if (xp->xp_numfiles == 1)
3954 /* free expanded pattern */
3955 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3956
3957 return OK;
3958}
3959
3960/*
3961 * Do wildcard expansion on the string 'str'.
3962 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003963 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 * Return NULL for failure.
3965 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003966 * "orig" is the originally expanded string, copied to allocated memory. It
3967 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3968 * WILD_PREV "orig" should be NULL.
3969 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003970 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3971 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 *
3973 * mode = WILD_FREE: just free previously expanded matches
3974 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3975 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3976 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3977 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3978 * mode = WILD_ALL: return all matches concatenated
3979 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003980 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 *
3982 * options = WILD_LIST_NOTFOUND: list entries without a match
3983 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3984 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3985 * options = WILD_NO_BEEP: Don't beep for multiple matches
3986 * options = WILD_ADD_SLASH: add a slash after directory names
3987 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3988 * options = WILD_SILENT: don't print warning messages
3989 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003990 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * The variables xp->xp_context and xp->xp_backslash must have been set!
3993 */
3994 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003995ExpandOne(
3996 expand_T *xp,
3997 char_u *str,
3998 char_u *orig, /* allocated copy of original of expanded string */
3999 int options,
4000 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
4002 char_u *ss = NULL;
4003 static int findex;
4004 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004005 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 int i;
4007 long_u len;
4008 int non_suf_match; /* number without matching suffix */
4009
4010 /*
4011 * first handle the case of using an old match
4012 */
4013 if (mode == WILD_NEXT || mode == WILD_PREV)
4014 {
4015 if (xp->xp_numfiles > 0)
4016 {
4017 if (mode == WILD_PREV)
4018 {
4019 if (findex == -1)
4020 findex = xp->xp_numfiles;
4021 --findex;
4022 }
4023 else /* mode == WILD_NEXT */
4024 ++findex;
4025
4026 /*
4027 * When wrapping around, return the original string, set findex to
4028 * -1.
4029 */
4030 if (findex < 0)
4031 {
4032 if (orig_save == NULL)
4033 findex = xp->xp_numfiles - 1;
4034 else
4035 findex = -1;
4036 }
4037 if (findex >= xp->xp_numfiles)
4038 {
4039 if (orig_save == NULL)
4040 findex = 0;
4041 else
4042 findex = -1;
4043 }
4044#ifdef FEAT_WILDMENU
4045 if (p_wmnu)
4046 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4047 findex, cmd_showtail);
4048#endif
4049 if (findex == -1)
4050 return vim_strsave(orig_save);
4051 return vim_strsave(xp->xp_files[findex]);
4052 }
4053 else
4054 return NULL;
4055 }
4056
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004057 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4059 {
4060 FreeWild(xp->xp_numfiles, xp->xp_files);
4061 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004062 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 findex = 0;
4065
4066 if (mode == WILD_FREE) /* only release file name */
4067 return NULL;
4068
4069 if (xp->xp_numfiles == -1)
4070 {
4071 vim_free(orig_save);
4072 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004073 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075 /*
4076 * Do the expansion.
4077 */
4078 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4079 options) == FAIL)
4080 {
4081#ifdef FNAME_ILLEGAL
4082 /* Illegal file name has been silently skipped. But when there
4083 * are wildcards, the real problem is that there was no match,
4084 * causing the pattern to be added, which has illegal characters.
4085 */
4086 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004087 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#endif
4089 }
4090 else if (xp->xp_numfiles == 0)
4091 {
4092 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004093 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 else
4096 {
4097 /* Escape the matches for use on the command line. */
4098 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4099
4100 /*
4101 * Check for matching suffixes in file names.
4102 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004103 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4104 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 if (xp->xp_numfiles)
4107 non_suf_match = xp->xp_numfiles;
4108 else
4109 non_suf_match = 1;
4110 if ((xp->xp_context == EXPAND_FILES
4111 || xp->xp_context == EXPAND_DIRECTORIES)
4112 && xp->xp_numfiles > 1)
4113 {
4114 /*
4115 * More than one match; check suffix.
4116 * The files will have been sorted on matching suffix in
4117 * expand_wildcards, only need to check the first two.
4118 */
4119 non_suf_match = 0;
4120 for (i = 0; i < 2; ++i)
4121 if (match_suffix(xp->xp_files[i]))
4122 ++non_suf_match;
4123 }
4124 if (non_suf_match != 1)
4125 {
4126 /* Can we ever get here unless it's while expanding
4127 * interactively? If not, we can get rid of this all
4128 * together. Don't really want to wait for this message
4129 * (and possibly have to hit return to continue!).
4130 */
4131 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 else if (!(options & WILD_NO_BEEP))
4134 beep_flush();
4135 }
4136 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4137 ss = vim_strsave(xp->xp_files[0]);
4138 }
4139 }
4140 }
4141
4142 /* Find longest common part */
4143 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4144 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004145 int mb_len = 1;
4146 int c0, ci;
4147
4148 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004150 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004152 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4153 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4154 }
4155 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004156 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004157 for (i = 1; i < xp->xp_numfiles; ++i)
4158 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004159 if (has_mbyte)
4160 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4161 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004162 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004163 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004165 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004166 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004168 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 break;
4170 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004171 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 break;
4173 }
4174 if (i < xp->xp_numfiles)
4175 {
4176 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004177 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 break;
4179 }
4180 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004181
Bram Moolenaar964b3742019-05-24 18:54:09 +02004182 ss = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004184 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 findex = -1; /* next p_wc gets first one */
4186 }
4187
4188 /* Concatenate all matching names */
4189 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4190 {
4191 len = 0;
4192 for (i = 0; i < xp->xp_numfiles; ++i)
4193 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004194 ss = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if (ss != NULL)
4196 {
4197 *ss = NUL;
4198 for (i = 0; i < xp->xp_numfiles; ++i)
4199 {
4200 STRCAT(ss, xp->xp_files[i]);
4201 if (i != xp->xp_numfiles - 1)
4202 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4203 }
4204 }
4205 }
4206
4207 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4208 ExpandCleanup(xp);
4209
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004210 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004211 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004212 vim_free(orig);
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return ss;
4215}
4216
4217/*
4218 * Prepare an expand structure for use.
4219 */
4220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004221ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004223 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004224 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004226#ifndef BACKSLASH_IN_FILENAME
4227 xp->xp_shell = FALSE;
4228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 xp->xp_numfiles = -1;
4230 xp->xp_files = NULL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004231#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004232 xp->xp_arg = NULL;
4233#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004234 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237/*
4238 * Cleanup an expand structure after use.
4239 */
4240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004241ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
4243 if (xp->xp_numfiles >= 0)
4244 {
4245 FreeWild(xp->xp_numfiles, xp->xp_files);
4246 xp->xp_numfiles = -1;
4247 }
4248}
4249
4250 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004251ExpandEscape(
4252 expand_T *xp,
4253 char_u *str,
4254 int numfiles,
4255 char_u **files,
4256 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
4258 int i;
4259 char_u *p;
4260
4261 /*
4262 * May change home directory back to "~"
4263 */
4264 if (options & WILD_HOME_REPLACE)
4265 tilde_replace(str, numfiles, files);
4266
4267 if (options & WILD_ESCAPE)
4268 {
4269 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004270 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 || xp->xp_context == EXPAND_BUFFERS
4273 || xp->xp_context == EXPAND_DIRECTORIES)
4274 {
4275 /*
4276 * Insert a backslash into a file name before a space, \, %, #
4277 * and wildmatch characters, except '~'.
4278 */
4279 for (i = 0; i < numfiles; ++i)
4280 {
4281 /* for ":set path=" we need to escape spaces twice */
4282 if (xp->xp_backslash == XP_BS_THREE)
4283 {
4284 p = vim_strsave_escaped(files[i], (char_u *)" ");
4285 if (p != NULL)
4286 {
4287 vim_free(files[i]);
4288 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004289#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 p = vim_strsave_escaped(files[i], (char_u *)" ");
4291 if (p != NULL)
4292 {
4293 vim_free(files[i]);
4294 files[i] = p;
4295 }
4296#endif
4297 }
4298 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004299#ifdef BACKSLASH_IN_FILENAME
4300 p = vim_strsave_fnameescape(files[i], FALSE);
4301#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004302 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (p != NULL)
4305 {
4306 vim_free(files[i]);
4307 files[i] = p;
4308 }
4309
4310 /* If 'str' starts with "\~", replace "~" at start of
4311 * files[i] with "\~". */
4312 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004313 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004316
4317 /* If the first file starts with a '+' escape it. Otherwise it
4318 * could be seen as "+cmd". */
4319 if (*files[0] == '+')
4320 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 else if (xp->xp_context == EXPAND_TAGS)
4323 {
4324 /*
4325 * Insert a backslash before characters in a tag name that
4326 * would terminate the ":tag" command.
4327 */
4328 for (i = 0; i < numfiles; ++i)
4329 {
4330 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4331 if (p != NULL)
4332 {
4333 vim_free(files[i]);
4334 files[i] = p;
4335 }
4336 }
4337 }
4338 }
4339}
4340
4341/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004342 * Escape special characters in "fname" for when used as a file name argument
4343 * after a Vim command, or, when "shell" is non-zero, a shell command.
4344 * Returns the result in allocated memory.
4345 */
4346 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004347vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004348{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004349 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004350#ifdef BACKSLASH_IN_FILENAME
4351 char_u buf[20];
4352 int j = 0;
4353
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004354 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004355 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004356 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004357 buf[j++] = *p;
4358 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004359 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004360#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004361 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4362 if (shell && csh_like_shell() && p != NULL)
4363 {
4364 char_u *s;
4365
4366 /* For csh and similar shells need to put two backslashes before '!'.
4367 * One is taken by Vim, one by the shell. */
4368 s = vim_strsave_escaped(p, (char_u *)"!");
4369 vim_free(p);
4370 p = s;
4371 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004372#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004373
4374 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4375 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004376 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004377 escape_fname(&p);
4378
4379 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004380}
4381
4382/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004383 * Put a backslash before the file name in "pp", which is in allocated memory.
4384 */
4385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004386escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004387{
4388 char_u *p;
4389
Bram Moolenaar964b3742019-05-24 18:54:09 +02004390 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00004391 if (p != NULL)
4392 {
4393 p[0] = '\\';
4394 STRCPY(p + 1, *pp);
4395 vim_free(*pp);
4396 *pp = p;
4397 }
4398}
4399
4400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 * For each file name in files[num_files]:
4402 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4403 */
4404 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004405tilde_replace(
4406 char_u *orig_pat,
4407 int num_files,
4408 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 int i;
4411 char_u *p;
4412
4413 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4414 {
4415 for (i = 0; i < num_files; ++i)
4416 {
4417 p = home_replace_save(NULL, files[i]);
4418 if (p != NULL)
4419 {
4420 vim_free(files[i]);
4421 files[i] = p;
4422 }
4423 }
4424 }
4425}
4426
4427/*
4428 * Show all matches for completion on the command line.
4429 * Returns EXPAND_NOTHING when the character that triggered expansion should
4430 * be inserted like a normal character.
4431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004433showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
4435#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4436 int num_files;
4437 char_u **files_found;
4438 int i, j, k;
4439 int maxlen;
4440 int lines;
4441 int columns;
4442 char_u *p;
4443 int lastlen;
4444 int attr;
4445 int showtail;
4446
4447 if (xp->xp_numfiles == -1)
4448 {
4449 set_expand_context(xp);
4450 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4451 &num_files, &files_found);
4452 showtail = expand_showtail(xp);
4453 if (i != EXPAND_OK)
4454 return i;
4455
4456 }
4457 else
4458 {
4459 num_files = xp->xp_numfiles;
4460 files_found = xp->xp_files;
4461 showtail = cmd_showtail;
4462 }
4463
4464#ifdef FEAT_WILDMENU
4465 if (!wildmenu)
4466 {
4467#endif
4468 msg_didany = FALSE; /* lines_left will be set */
4469 msg_start(); /* prepare for paging */
4470 msg_putchar('\n');
4471 out_flush();
4472 cmdline_row = msg_row;
4473 msg_didany = FALSE; /* lines_left will be set again */
4474 msg_start(); /* prepare for paging */
4475#ifdef FEAT_WILDMENU
4476 }
4477#endif
4478
4479 if (got_int)
4480 got_int = FALSE; /* only int. the completion, not the cmd line */
4481#ifdef FEAT_WILDMENU
4482 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004483 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#endif
4485 else
4486 {
4487 /* find the length of the longest file name */
4488 maxlen = 0;
4489 for (i = 0; i < num_files; ++i)
4490 {
4491 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004492 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 || xp->xp_context == EXPAND_BUFFERS))
4494 {
4495 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4496 j = vim_strsize(NameBuff);
4497 }
4498 else
4499 j = vim_strsize(L_SHOWFILE(i));
4500 if (j > maxlen)
4501 maxlen = j;
4502 }
4503
4504 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4505 lines = num_files;
4506 else
4507 {
4508 /* compute the number of columns and lines for the listing */
4509 maxlen += 2; /* two spaces between file names */
4510 columns = ((int)Columns + 2) / maxlen;
4511 if (columns < 1)
4512 columns = 1;
4513 lines = (num_files + columns - 1) / columns;
4514 }
4515
Bram Moolenaar8820b482017-03-16 17:23:31 +01004516 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4519 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004520 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 msg_clr_eos();
4522 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004523 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525
4526 /* list the files line by line */
4527 for (i = 0; i < lines; ++i)
4528 {
4529 lastlen = 999;
4530 for (k = i; k < num_files; k += lines)
4531 {
4532 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4533 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004534 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 p = files_found[k] + STRLEN(files_found[k]) + 1;
4536 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004537 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004539 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 break;
4541 }
4542 for (j = maxlen - lastlen; --j >= 0; )
4543 msg_putchar(' ');
4544 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004545 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 || xp->xp_context == EXPAND_BUFFERS)
4547 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004548 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004549 if (xp->xp_numfiles != -1)
4550 {
4551 char_u *halved_slash;
4552 char_u *exp_path;
4553
4554 /* Expansion was done before and special characters
4555 * were escaped, need to halve backslashes. Also
4556 * $HOME has been replaced with ~/. */
4557 exp_path = expand_env_save_opt(files_found[k], TRUE);
4558 halved_slash = backslash_halve_save(
4559 exp_path != NULL ? exp_path : files_found[k]);
4560 j = mch_isdir(halved_slash != NULL ? halved_slash
4561 : files_found[k]);
4562 vim_free(exp_path);
4563 vim_free(halved_slash);
4564 }
4565 else
4566 /* Expansion was done here, file names are literal. */
4567 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (showtail)
4569 p = L_SHOWFILE(k);
4570 else
4571 {
4572 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4573 TRUE);
4574 p = NameBuff;
4575 }
4576 }
4577 else
4578 {
4579 j = FALSE;
4580 p = L_SHOWFILE(k);
4581 }
4582 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4583 }
4584 if (msg_col > 0) /* when not wrapped around */
4585 {
4586 msg_clr_eos();
4587 msg_putchar('\n');
4588 }
4589 out_flush(); /* show one line at a time */
4590 if (got_int)
4591 {
4592 got_int = FALSE;
4593 break;
4594 }
4595 }
4596
4597 /*
4598 * we redraw the command below the lines that we have just listed
4599 * This is a bit tricky, but it saves a lot of screen updating.
4600 */
4601 cmdline_row = msg_row; /* will put it back later */
4602 }
4603
4604 if (xp->xp_numfiles == -1)
4605 FreeWild(num_files, files_found);
4606
4607 return EXPAND_OK;
4608}
4609
4610/*
4611 * Private gettail for showmatches() (and win_redr_status_matches()):
4612 * Find tail of file name path, but ignore trailing "/".
4613 */
4614 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004615sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
4617 char_u *p;
4618 char_u *t = s;
4619 int had_sep = FALSE;
4620
4621 for (p = s; *p != NUL; )
4622 {
4623 if (vim_ispathsep(*p)
4624#ifdef BACKSLASH_IN_FILENAME
4625 && !rem_backslash(p)
4626#endif
4627 )
4628 had_sep = TRUE;
4629 else if (had_sep)
4630 {
4631 t = p;
4632 had_sep = FALSE;
4633 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004634 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 return t;
4637}
4638
4639/*
4640 * Return TRUE if we only need to show the tail of completion matches.
4641 * When not completing file names or there is a wildcard in the path FALSE is
4642 * returned.
4643 */
4644 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004645expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 char_u *s;
4648 char_u *end;
4649
4650 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004651 if (xp->xp_context != EXPAND_FILES
4652 && xp->xp_context != EXPAND_SHELLCMD
4653 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return FALSE;
4655
4656 end = gettail(xp->xp_pattern);
4657 if (end == xp->xp_pattern) /* there is no path separator */
4658 return FALSE;
4659
4660 for (s = xp->xp_pattern; s < end; s++)
4661 {
4662 /* Skip escaped wildcards. Only when the backslash is not a path
4663 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4664 if (rem_backslash(s))
4665 ++s;
4666 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4667 return FALSE;
4668 }
4669 return TRUE;
4670}
4671
4672/*
4673 * Prepare a string for expansion.
4674 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004675 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 * When expanding other names: The string will be used with regcomp(). Copy
4677 * the name into allocated memory and prepend "^".
4678 */
4679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004680addstar(
4681 char_u *fname,
4682 int len,
4683 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 char_u *retval;
4686 int i, j;
4687 int new_len;
4688 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004689 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004691 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004692 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004693 && context != EXPAND_SHELLCMD
4694 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 /*
4697 * Matching will be done internally (on something other than files).
4698 * So we convert the file-matching-type wildcards into our kind for
4699 * use with vim_regcomp(). First work out how long it will be:
4700 */
4701
4702 /* For help tags the translation is done in find_help_tags().
4703 * For a tag pattern starting with "/" no translation is needed. */
4704 if (context == EXPAND_HELP
4705 || context == EXPAND_COLORS
4706 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004707 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004708 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004709 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004710 || ((context == EXPAND_TAGS_LISTFILES
4711 || context == EXPAND_TAGS)
4712 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 retval = vim_strnsave(fname, len);
4714 else
4715 {
4716 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4717 for (i = 0; i < len; i++)
4718 {
4719 if (fname[i] == '*' || fname[i] == '~')
4720 new_len++; /* '*' needs to be replaced by ".*"
4721 '~' needs to be replaced by "\~" */
4722
4723 /* Buffer names are like file names. "." should be literal */
4724 if (context == EXPAND_BUFFERS && fname[i] == '.')
4725 new_len++; /* "." becomes "\." */
4726
4727 /* Custom expansion takes care of special things, match
4728 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004729 if ((context == EXPAND_USER_DEFINED
4730 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 new_len++; /* '\' becomes "\\" */
4732 }
4733 retval = alloc(new_len);
4734 if (retval != NULL)
4735 {
4736 retval[0] = '^';
4737 j = 1;
4738 for (i = 0; i < len; i++, j++)
4739 {
4740 /* Skip backslash. But why? At least keep it for custom
4741 * expansion. */
4742 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004743 && context != EXPAND_USER_LIST
4744 && fname[i] == '\\'
4745 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 break;
4747
4748 switch (fname[i])
4749 {
4750 case '*': retval[j++] = '.';
4751 break;
4752 case '~': retval[j++] = '\\';
4753 break;
4754 case '?': retval[j] = '.';
4755 continue;
4756 case '.': if (context == EXPAND_BUFFERS)
4757 retval[j++] = '\\';
4758 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004759 case '\\': if (context == EXPAND_USER_DEFINED
4760 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 retval[j++] = '\\';
4762 break;
4763 }
4764 retval[j] = fname[i];
4765 }
4766 retval[j] = NUL;
4767 }
4768 }
4769 }
4770 else
4771 {
4772 retval = alloc(len + 4);
4773 if (retval != NULL)
4774 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004775 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004778 * Don't add a star to *, ~, ~user, $var or `cmd`.
4779 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 * ~ would be at the start of the file name, but not the tail.
4781 * $ could be anywhere in the tail.
4782 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004783 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
4785 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004786 ends_in_star = (len > 0 && retval[len - 1] == '*');
4787#ifndef BACKSLASH_IN_FILENAME
4788 for (i = len - 2; i >= 0; --i)
4789 {
4790 if (retval[i] != '\\')
4791 break;
4792 ends_in_star = !ends_in_star;
4793 }
4794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004796 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 && vim_strchr(tail, '$') == NULL
4798 && vim_strchr(retval, '`') == NULL)
4799 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004800 else if (len > 0 && retval[len - 1] == '$')
4801 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 retval[len] = NUL;
4803 }
4804 }
4805 return retval;
4806}
4807
4808/*
4809 * Must parse the command line so far to work out what context we are in.
4810 * Completion can then be done based on that context.
4811 * This routine sets the variables:
4812 * xp->xp_pattern The start of the pattern to be expanded within
4813 * the command line (ends at the cursor).
4814 * xp->xp_context The type of thing to expand. Will be one of:
4815 *
4816 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4817 * the command line, like an unknown command. Caller
4818 * should beep.
4819 * EXPAND_NOTHING Unrecognised context for completion, use char like
4820 * a normal char, rather than for completion. eg
4821 * :s/^I/
4822 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4823 * it.
4824 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004825 * EXPAND_FILES After command with EX_XFILE set, or after setting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 * with P_EXPAND set. eg :e ^I, :w>>^I
4827 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4828 * when we know only directories are of interest. eg
4829 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004830 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4832 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4833 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4834 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4835 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4836 * EXPAND_EVENTS Complete event names
4837 * EXPAND_SYNTAX Complete :syntax command arguments
4838 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4839 * EXPAND_AUGROUP Complete autocommand group names
4840 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4841 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4842 * eg :unmap a^I , :cunab x^I
4843 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4844 * eg :call sub^I
4845 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4846 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4847 * names in expressions, eg :while s^I
4848 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004849 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 */
4851 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004854 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (ccline.cmdfirstc != ':'
4856#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004857 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004858 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
4860 )
4861 {
4862 xp->xp_context = EXPAND_NOTHING;
4863 return;
4864 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004865 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866}
4867
4868 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004869set_cmd_context(
4870 expand_T *xp,
4871 char_u *str, /* start of command line */
4872 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004873 int col, /* position of cursor */
4874 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
4876 int old_char = NUL;
4877 char_u *nextcomm;
4878
4879 /*
4880 * Avoid a UMR warning from Purify, only save the character if it has been
4881 * written before.
4882 */
4883 if (col < len)
4884 old_char = str[col];
4885 str[col] = NUL;
4886 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004887
4888#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004889 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004890 {
4891# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004892 /* pass CMD_SIZE because there is no real command */
4893 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004894# endif
4895 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004896 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004897 {
4898 xp->xp_context = ccline.xp_context;
4899 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004900# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004901 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004902# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004903 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004904 else
4905#endif
4906 while (nextcomm != NULL)
4907 nextcomm = set_one_cmd_context(xp, nextcomm);
4908
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004909 /* Store the string here so that call_user_expand_func() can get to them
4910 * easily. */
4911 xp->xp_line = str;
4912 xp->xp_col = col;
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 str[col] = old_char;
4915}
4916
4917/*
4918 * Expand the command line "str" from context "xp".
4919 * "xp" must have been set by set_cmd_context().
4920 * xp->xp_pattern points into "str", to where the text that is to be expanded
4921 * starts.
4922 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4923 * cursor.
4924 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4925 * key that triggered expansion literally.
4926 * Returns EXPAND_OK otherwise.
4927 */
4928 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004929expand_cmdline(
4930 expand_T *xp,
4931 char_u *str, /* start of command line */
4932 int col, /* position of cursor */
4933 int *matchcount, /* return: nr of matches */
4934 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004937 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4940 {
4941 beep_flush();
4942 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4943 }
4944 if (xp->xp_context == EXPAND_NOTHING)
4945 {
4946 /* Caller can use the character as a normal char instead */
4947 return EXPAND_NOTHING;
4948 }
4949
4950 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004951 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4952 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (file_str == NULL)
4954 return EXPAND_UNSUCCESSFUL;
4955
Bram Moolenaar94950a92010-12-02 16:01:29 +01004956 if (p_wic)
4957 options += WILD_ICASE;
4958
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004960 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 *matchcount = 0;
4963 *matches = NULL;
4964 }
4965 vim_free(file_str);
4966
4967 return EXPAND_OK;
4968}
4969
4970#ifdef FEAT_MULTI_LANG
4971/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004972 * Cleanup matches for help tags:
4973 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4974 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004977cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
4979 int i, j;
4980 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004981 char_u buf[4];
4982 char_u *p = buf;
4983
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004984 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004985 {
4986 *p++ = '@';
4987 *p++ = p_hlg[0];
4988 *p++ = p_hlg[1];
4989 }
4990 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 for (i = 0; i < num_file; ++i)
4993 {
4994 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004995 if (len <= 0)
4996 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004997 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 /* Sorting on priority means the same item in another language may
5000 * be anywhere. Search all items for a match up to the "@en". */
5001 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005002 if (j != i && (int)STRLEN(file[j]) == len + 3
5003 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005006 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 file[i][len] = NUL;
5008 }
5009 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005010
5011 if (*buf != NUL)
5012 for (i = 0; i < num_file; ++i)
5013 {
5014 len = (int)STRLEN(file[i]) - 3;
5015 if (len <= 0)
5016 continue;
5017 if (STRCMP(file[i] + len, buf) == 0)
5018 {
5019 /* remove the default language */
5020 file[i][len] = NUL;
5021 }
5022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023}
5024#endif
5025
5026/*
5027 * Do the expansion based on xp->xp_context and "pat".
5028 */
5029 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005030ExpandFromContext(
5031 expand_T *xp,
5032 char_u *pat,
5033 int *num_file,
5034 char_u ***file,
5035 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037#ifdef FEAT_CMDL_COMPL
5038 regmatch_T regmatch;
5039#endif
5040 int ret;
5041 int flags;
5042
5043 flags = EW_DIR; /* include directories */
5044 if (options & WILD_LIST_NOTFOUND)
5045 flags |= EW_NOTFOUND;
5046 if (options & WILD_ADD_SLASH)
5047 flags |= EW_ADDSLASH;
5048 if (options & WILD_KEEP_ALL)
5049 flags |= EW_KEEPALL;
5050 if (options & WILD_SILENT)
5051 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005052 if (options & WILD_ALLLINKS)
5053 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005055 if (xp->xp_context == EXPAND_FILES
5056 || xp->xp_context == EXPAND_DIRECTORIES
5057 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 /*
5060 * Expand file or directory names.
5061 */
5062 int free_pat = FALSE;
5063 int i;
5064
5065 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5066 * space */
5067 if (xp->xp_backslash != XP_BS_NONE)
5068 {
5069 free_pat = TRUE;
5070 pat = vim_strsave(pat);
5071 for (i = 0; pat[i]; ++i)
5072 if (pat[i] == '\\')
5073 {
5074 if (xp->xp_backslash == XP_BS_THREE
5075 && pat[i + 1] == '\\'
5076 && pat[i + 2] == '\\'
5077 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005078 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (xp->xp_backslash == XP_BS_ONE
5080 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005081 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 }
5084
5085 if (xp->xp_context == EXPAND_FILES)
5086 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005087 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5088 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 else
5090 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005091 if (options & WILD_ICASE)
5092 flags |= EW_ICASE;
5093
Bram Moolenaard7834d32009-12-02 16:14:36 +00005094 /* Expand wildcards, supporting %:h and the like. */
5095 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (free_pat)
5097 vim_free(pat);
5098 return ret;
5099 }
5100
5101 *file = (char_u **)"";
5102 *num_file = 0;
5103 if (xp->xp_context == EXPAND_HELP)
5104 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005105 /* With an empty argument we would get all the help tags, which is
5106 * very slow. Get matches for "help" instead. */
5107 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5108 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110#ifdef FEAT_MULTI_LANG
5111 cleanup_help_tags(*num_file, *file);
5112#endif
5113 return OK;
5114 }
5115 return FAIL;
5116 }
5117
5118#ifndef FEAT_CMDL_COMPL
5119 return FAIL;
5120#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005121 if (xp->xp_context == EXPAND_SHELLCMD)
5122 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (xp->xp_context == EXPAND_OLD_SETTING)
5124 return ExpandOldSetting(num_file, file);
5125 if (xp->xp_context == EXPAND_BUFFERS)
5126 return ExpandBufnames(pat, num_file, file, options);
5127 if (xp->xp_context == EXPAND_TAGS
5128 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5129 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5130 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005131 {
5132 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005133 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5134 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005137 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005138 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005139 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005140 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005141 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005142 {
5143 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005144 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005145 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005146 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005147 {
5148 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005149 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005150 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005151# if defined(FEAT_EVAL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005152 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005153 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005154# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005155 if (xp->xp_context == EXPAND_PACKADD)
5156 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5159 if (regmatch.regprog == NULL)
5160 return FAIL;
5161
5162 /* set ignore-case according to p_ic, p_scs and pat */
5163 regmatch.rm_ic = ignorecase(pat);
5164
5165 if (xp->xp_context == EXPAND_SETTINGS
5166 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5167 ret = ExpandSettings(xp, &regmatch, num_file, file);
5168 else if (xp->xp_context == EXPAND_MAPPINGS)
5169 ret = ExpandMappings(&regmatch, num_file, file);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005170# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 else if (xp->xp_context == EXPAND_USER_DEFINED)
5172 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5173# endif
5174 else
5175 {
5176 static struct expgen
5177 {
5178 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005179 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005181 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 } tab[] =
5183 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005184 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5185 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005186 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005187 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005188#ifdef FEAT_CMDHIST
5189 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5190#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005191 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005192 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005193 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5194 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5195 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005197 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5198 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5199 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5200 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201#endif
5202#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005203 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5204 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
5206#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005207 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005209#ifdef FEAT_PROFILE
5210 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5211#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5214 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005215#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005217#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005218#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005220#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005221#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005223#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005224#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5226 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005228 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005229 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005230 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 };
5232 int i;
5233
5234 /*
5235 * Find a context in the table and call the ExpandGeneric() with the
5236 * right function to do the expansion.
5237 */
5238 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005239 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (xp->xp_context == tab[i].context)
5241 {
5242 if (tab[i].ic)
5243 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005244 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005245 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 break;
5247 }
5248 }
5249
Bram Moolenaar473de612013-06-08 18:19:48 +02005250 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
5252 return ret;
5253#endif /* FEAT_CMDL_COMPL */
5254}
5255
5256#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5257/*
5258 * Expand a list of names.
5259 *
5260 * Generic function for command line completion. It calls a function to
5261 * obtain strings, one by one. The strings are matched against a regexp
5262 * program. Matching strings are copied into an array, which is returned.
5263 *
5264 * Returns OK when no problems encountered, FAIL for error (out of memory).
5265 */
5266 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267ExpandGeneric(
5268 expand_T *xp,
5269 regmatch_T *regmatch,
5270 int *num_file,
5271 char_u ***file,
5272 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 int i;
5277 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005278 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 char_u *str;
5280
5281 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005282 * round == 0: count the number of matching names
5283 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005285 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 for (i = 0; ; ++i)
5288 {
5289 str = (*func)(xp, i);
5290 if (str == NULL) /* end of list */
5291 break;
5292 if (*str == NUL) /* skip empty strings */
5293 continue;
5294
5295 if (vim_regexec(regmatch, str, (colnr_T)0))
5296 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005297 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005299 if (escaped)
5300 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5301 else
5302 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 (*file)[count] = str;
5304#ifdef FEAT_MENU
5305 if (func == get_menu_names && str != NULL)
5306 {
5307 /* test for separator added by get_menu_names() */
5308 str += STRLEN(str) - 1;
5309 if (*str == '\001')
5310 *str = '.';
5311 }
5312#endif
5313 }
5314 ++count;
5315 }
5316 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005317 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 if (count == 0)
5320 return OK;
5321 *num_file = count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005322 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (*file == NULL)
5324 {
5325 *file = (char_u **)"";
5326 return FAIL;
5327 }
5328 count = 0;
5329 }
5330 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005331
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005332 /* Sort the results. Keep menu's in the specified order. */
5333 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005334 {
5335 if (xp->xp_context == EXPAND_EXPRESSION
5336 || xp->xp_context == EXPAND_FUNCTIONS
5337 || xp->xp_context == EXPAND_USER_FUNC)
5338 /* <SNR> functions should be sorted to the end. */
5339 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5340 sort_func_compare);
5341 else
5342 sort_strings(*file, *num_file);
5343 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005344
Bram Moolenaar4f688582007-07-24 12:34:30 +00005345#ifdef FEAT_CMDL_COMPL
5346 /* Reset the variables used for special highlight names expansion, so that
5347 * they don't show up when getting normal highlight names by ID. */
5348 reset_expand_highlight();
5349#endif
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 return OK;
5352}
5353
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005354/*
5355 * Complete a shell command.
5356 * Returns FAIL or OK;
5357 */
5358 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359expand_shellcmd(
5360 char_u *filepat, /* pattern to match with command names */
5361 int *num_file, /* return: number of matches */
5362 char_u ***file, /* return: array with matches */
5363 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005364{
5365 char_u *pat;
5366 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005367 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005368 int mustfree = FALSE;
5369 garray_T ga;
5370 char_u *buf = alloc(MAXPATHL);
5371 size_t l;
5372 char_u *s, *e;
5373 int flags = flagsarg;
5374 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005375 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005376 hashtab_T found_ht;
5377 hashitem_T *hi;
5378 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005379
5380 if (buf == NULL)
5381 return FAIL;
5382
5383 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5384 * space */
5385 pat = vim_strsave(filepat);
5386 for (i = 0; pat[i]; ++i)
5387 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005388 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005389
Bram Moolenaarb5971142015-03-21 17:32:19 +01005390 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005391
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005392 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5393 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005394 path = (char_u *)".";
5395 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005396 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005397 /* For an absolute name we don't use $PATH. */
5398 if (!mch_isFullName(pat))
5399 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005400 if (path == NULL)
5401 path = (char_u *)"";
5402 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403
5404 /*
5405 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005406 * collect them in "ga". When "." is not in $PATH also expand for the
5407 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005408 */
5409 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005410 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005411 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005412 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005413#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005414 e = vim_strchr(s, ';');
5415#else
5416 e = vim_strchr(s, ':');
5417#endif
5418 if (e == NULL)
5419 e = s + STRLEN(s);
5420
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005421 if (*s == NUL)
5422 {
5423 if (did_curdir)
5424 break;
5425 // Find directories in the current directory, path is empty.
5426 did_curdir = TRUE;
5427 flags |= EW_DIR;
5428 }
5429 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5430 {
5431 did_curdir = TRUE;
5432 flags |= EW_DIR;
5433 }
5434 else
5435 // Do not match directories inside a $PATH item.
5436 flags &= ~EW_DIR;
5437
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438 l = e - s;
5439 if (l > MAXPATHL - 5)
5440 break;
5441 vim_strncpy(buf, s, l);
5442 add_pathsep(buf);
5443 l = STRLEN(buf);
5444 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5445
5446 /* Expand matches in one directory of $PATH. */
5447 ret = expand_wildcards(1, &buf, num_file, file, flags);
5448 if (ret == OK)
5449 {
5450 if (ga_grow(&ga, *num_file) == FAIL)
5451 FreeWild(*num_file, *file);
5452 else
5453 {
5454 for (i = 0; i < *num_file; ++i)
5455 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005456 char_u *name = (*file)[i];
5457
5458 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005459 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005460 // Check if this name was already found.
5461 hash = hash_hash(name + l);
5462 hi = hash_lookup(&found_ht, name + l, hash);
5463 if (HASHITEM_EMPTY(hi))
5464 {
5465 // Remove the path that was prepended.
5466 STRMOVE(name, name + l);
5467 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5468 hash_add_item(&found_ht, hi, name, hash);
5469 name = NULL;
5470 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005471 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005472 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005473 }
5474 vim_free(*file);
5475 }
5476 }
5477 if (*e != NUL)
5478 ++e;
5479 }
5480 *file = ga.ga_data;
5481 *num_file = ga.ga_len;
5482
5483 vim_free(buf);
5484 vim_free(pat);
5485 if (mustfree)
5486 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005487 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005488 return OK;
5489}
5490
5491
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005492# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005494 * Call "user_expand_func()" to invoke a user defined Vim script function and
5495 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005497 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005499 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500 expand_T *xp,
5501 int *num_file,
5502 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005504 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005505 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005506 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005507 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005508 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
Bram Moolenaarb7515462013-06-29 12:58:33 +02005510 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005511 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 *num_file = 0;
5513 *file = NULL;
5514
Bram Moolenaarb7515462013-06-29 12:58:33 +02005515 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005516 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005517 keep = ccline.cmdbuff[ccline.cmdlen];
5518 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005519 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005520
Bram Moolenaarffa96842018-06-12 22:05:14 +02005521 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5522
5523 args[0].v_type = VAR_STRING;
5524 args[0].vval.v_string = pat;
5525 args[1].v_type = VAR_STRING;
5526 args[1].vval.v_string = xp->xp_line;
5527 args[2].v_type = VAR_NUMBER;
5528 args[2].vval.v_number = xp->xp_col;
5529 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005531 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005532
Bram Moolenaarded27a12018-08-01 19:06:03 +02005533 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005534
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005535 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005536 if (ccline.cmdbuff != NULL)
5537 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005538
Bram Moolenaarffa96842018-06-12 22:05:14 +02005539 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005540 return ret;
5541}
5542
5543/*
5544 * Expand names with a function defined by the user.
5545 */
5546 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ExpandUserDefined(
5548 expand_T *xp,
5549 regmatch_T *regmatch,
5550 int *num_file,
5551 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005552{
5553 char_u *retstr;
5554 char_u *s;
5555 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005556 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005557 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005558 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005559
5560 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5561 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return FAIL;
5563
5564 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005565 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
5567 e = vim_strchr(s, '\n');
5568 if (e == NULL)
5569 e = s + STRLEN(s);
5570 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005571 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005573 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5574 *e = keep;
5575
5576 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005578 if (ga_grow(&ga, 1) == FAIL)
5579 break;
5580 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5581 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (*e != NUL)
5585 ++e;
5586 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005587 vim_free(retstr);
5588 *file = ga.ga_data;
5589 *num_file = ga.ga_len;
5590 return OK;
5591}
5592
5593/*
5594 * Expand names with a list returned by a function defined by the user.
5595 */
5596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ExpandUserList(
5598 expand_T *xp,
5599 int *num_file,
5600 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005601{
5602 list_T *retlist;
5603 listitem_T *li;
5604 garray_T ga;
5605
5606 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5607 if (retlist == NULL)
5608 return FAIL;
5609
5610 ga_init2(&ga, (int)sizeof(char *), 3);
5611 /* Loop over the items in the list. */
5612 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5613 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005614 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5615 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005616
5617 if (ga_grow(&ga, 1) == FAIL)
5618 break;
5619
5620 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005621 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005622 ++ga.ga_len;
5623 }
5624 list_unref(retlist);
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 *file = ga.ga_data;
5627 *num_file = ga.ga_len;
5628 return OK;
5629}
5630#endif
5631
5632/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005633 * Expand color scheme, compiler or filetype names.
5634 * Search from 'runtimepath':
5635 * 'runtimepath'/{dirnames}/{pat}.vim
5636 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5637 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5638 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5639 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005640 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
5642 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ExpandRTDir(
5644 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005645 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646 int *num_file,
5647 char_u ***file,
5648 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 char_u *s;
5651 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005652 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005654 int i;
5655 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 *num_file = 0;
5658 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005659 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005660 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005662 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005664 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005665 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005667 ga_clear_strings(&ga);
5668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005670 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005671 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005672 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005674
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005675 if (flags & DIP_START) {
5676 for (i = 0; dirnames[i] != NULL; ++i)
5677 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005678 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005679 if (s == NULL)
5680 {
5681 ga_clear_strings(&ga);
5682 return FAIL;
5683 }
5684 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5685 globpath(p_pp, s, &ga, 0);
5686 vim_free(s);
5687 }
5688 }
5689
5690 if (flags & DIP_OPT) {
5691 for (i = 0; dirnames[i] != NULL; ++i)
5692 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005693 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005694 if (s == NULL)
5695 {
5696 ga_clear_strings(&ga);
5697 return FAIL;
5698 }
5699 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5700 globpath(p_pp, s, &ga, 0);
5701 vim_free(s);
5702 }
5703 }
5704
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005705 for (i = 0; i < ga.ga_len; ++i)
5706 {
5707 match = ((char_u **)ga.ga_data)[i];
5708 s = match;
5709 e = s + STRLEN(s);
5710 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5711 {
5712 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005713 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005714 if (s < match || vim_ispathsep(*s))
5715 break;
5716 ++s;
5717 *e = NUL;
5718 mch_memmove(match, s, e - s + 1);
5719 }
5720 }
5721
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005722 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005723 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005724
5725 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005726 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005727 remove_duplicates(&ga);
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 *file = ga.ga_data;
5730 *num_file = ga.ga_len;
5731 return OK;
5732}
5733
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005734/*
5735 * Expand loadplugin names:
5736 * 'packpath'/pack/ * /opt/{pat}
5737 */
5738 static int
5739ExpandPackAddDir(
5740 char_u *pat,
5741 int *num_file,
5742 char_u ***file)
5743{
5744 char_u *s;
5745 char_u *e;
5746 char_u *match;
5747 garray_T ga;
5748 int i;
5749 int pat_len;
5750
5751 *num_file = 0;
5752 *file = NULL;
5753 pat_len = (int)STRLEN(pat);
5754 ga_init2(&ga, (int)sizeof(char *), 10);
5755
Bram Moolenaar964b3742019-05-24 18:54:09 +02005756 s = alloc(pat_len + 26);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005757 if (s == NULL)
5758 {
5759 ga_clear_strings(&ga);
5760 return FAIL;
5761 }
5762 sprintf((char *)s, "pack/*/opt/%s*", pat);
5763 globpath(p_pp, s, &ga, 0);
5764 vim_free(s);
5765
5766 for (i = 0; i < ga.ga_len; ++i)
5767 {
5768 match = ((char_u **)ga.ga_data)[i];
5769 s = gettail(match);
5770 e = s + STRLEN(s);
5771 mch_memmove(match, s, e - s + 1);
5772 }
5773
5774 if (ga.ga_len == 0)
5775 return FAIL;
5776
5777 /* Sort and remove duplicates which can happen when specifying multiple
5778 * directories in dirnames. */
5779 remove_duplicates(&ga);
5780
5781 *file = ga.ga_data;
5782 *num_file = ga.ga_len;
5783 return OK;
5784}
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#endif
5787
5788#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5789/*
5790 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005791 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794globpath(
5795 char_u *path,
5796 char_u *file,
5797 garray_T *ga,
5798 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 expand_T xpc;
5801 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 int num_p;
5804 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 buf = alloc(MAXPATHL);
5807 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005808 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005810 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 /* Loop over all entries in {path}. */
5814 while (*path != NUL)
5815 {
5816 /* Copy one item of the path to buf[] and concatenate the file name. */
5817 copy_option_part(&path, buf, MAXPATHL, ",");
5818 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5819 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005820# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005821 /* Using the platform's path separator (\) makes vim incorrectly
5822 * treat it as an escape character, use '/' instead. */
5823 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5824 STRCAT(buf, "/");
5825# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005829 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5830 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005832 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005834 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 for (i = 0; i < num_p; ++i)
5837 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005838 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005839 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005840 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 FreeWild(num_p, p);
5845 }
5846 }
5847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850}
5851
5852#endif
5853
5854#if defined(FEAT_CMDHIST) || defined(PROTO)
5855
5856/*********************************
5857 * Command line history stuff *
5858 *********************************/
5859
5860/*
5861 * Translate a history character to the associated type number.
5862 */
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02005863 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005864hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 if (c == ':')
5867 return HIST_CMD;
5868 if (c == '=')
5869 return HIST_EXPR;
5870 if (c == '@')
5871 return HIST_INPUT;
5872 if (c == '>')
5873 return HIST_DEBUG;
5874 return HIST_SEARCH; /* must be '?' or '/' */
5875}
5876
5877/*
5878 * Table of history names.
5879 * These names are used in :history and various hist...() functions.
5880 * It is sufficient to give the significant prefix of a history name.
5881 */
5882
5883static char *(history_names[]) =
5884{
5885 "cmd",
5886 "search",
5887 "expr",
5888 "input",
5889 "debug",
5890 NULL
5891};
5892
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5894/*
5895 * Function given to ExpandGeneric() to obtain the possible first
5896 * arguments of the ":history command.
5897 */
5898 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005899get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005900{
5901 static char_u compl[2] = { NUL, NUL };
5902 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005903 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005904 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5905
5906 if (idx < short_names_count)
5907 {
5908 compl[0] = (char_u)short_names[idx];
5909 return compl;
5910 }
5911 if (idx < short_names_count + history_name_count)
5912 return (char_u *)history_names[idx - short_names_count];
5913 if (idx == short_names_count + history_name_count)
5914 return (char_u *)"all";
5915 return NULL;
5916}
5917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919/*
5920 * init_history() - Initialize the command line history.
5921 * Also used to re-allocate the history when the size changes.
5922 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005923 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005924init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
5926 int newlen; /* new length of history table */
5927 histentry_T *temp;
5928 int i;
5929 int j;
5930 int type;
5931
5932 /*
5933 * If size of history table changed, reallocate it
5934 */
5935 newlen = (int)p_hi;
5936 if (newlen != hislen) /* history length changed */
5937 {
5938 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5939 {
5940 if (newlen)
5941 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005942 temp = ALLOC_MULT(histentry_T, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 if (temp == NULL) /* out of memory! */
5944 {
5945 if (type == 0) /* first one: just keep the old length */
5946 {
5947 newlen = hislen;
5948 break;
5949 }
5950 /* Already changed one table, now we can only have zero
5951 * length for all tables. */
5952 newlen = 0;
5953 type = -1;
5954 continue;
5955 }
5956 }
5957 else
5958 temp = NULL;
5959 if (newlen == 0 || temp != NULL)
5960 {
5961 if (hisidx[type] < 0) /* there are no entries yet */
5962 {
5963 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005964 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 else if (newlen > hislen) /* array becomes bigger */
5967 {
5968 for (i = 0; i <= hisidx[type]; ++i)
5969 temp[i] = history[type][i];
5970 j = i;
5971 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005972 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 for ( ; j < hislen; ++i, ++j)
5974 temp[i] = history[type][j];
5975 }
5976 else /* array becomes smaller or 0 */
5977 {
5978 j = hisidx[type];
5979 for (i = newlen - 1; ; --i)
5980 {
5981 if (i >= 0) /* copy newest entries */
5982 temp[i] = history[type][j];
5983 else /* remove older entries */
5984 vim_free(history[type][j].hisstr);
5985 if (--j < 0)
5986 j = hislen - 1;
5987 if (j == hisidx[type])
5988 break;
5989 }
5990 hisidx[type] = newlen - 1;
5991 }
5992 vim_free(history[type]);
5993 history[type] = temp;
5994 }
5995 }
5996 hislen = newlen;
5997 }
5998}
5999
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006000 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006002{
6003 hisptr->hisnum = 0;
6004 hisptr->viminfo = FALSE;
6005 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006006 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006007}
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009/*
6010 * Check if command line 'str' is already in history.
6011 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6012 */
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006013 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014in_history(
6015 int type,
6016 char_u *str,
6017 int move_to_front, /* Move the entry to the front if it exists */
6018 int sep,
6019 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 int i;
6022 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006023 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 if (hisidx[type] < 0)
6026 return FALSE;
6027 i = hisidx[type];
6028 do
6029 {
6030 if (history[type][i].hisstr == NULL)
6031 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006032
6033 /* For search history, check that the separator character matches as
6034 * well. */
6035 p = history[type][i].hisstr;
6036 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006037 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006038 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
6040 if (!move_to_front)
6041 return TRUE;
6042 last_i = i;
6043 break;
6044 }
6045 if (--i < 0)
6046 i = hislen - 1;
6047 } while (i != hisidx[type]);
6048
6049 if (last_i >= 0)
6050 {
6051 str = history[type][i].hisstr;
6052 while (i != hisidx[type])
6053 {
6054 if (++i >= hislen)
6055 i = 0;
6056 history[type][last_i] = history[type][i];
6057 last_i = i;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006060 history[type][i].viminfo = FALSE;
6061 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006062 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 return TRUE;
6064 }
6065 return FALSE;
6066}
6067
6068/*
6069 * Convert history name (from table above) to its HIST_ equivalent.
6070 * When "name" is empty, return "cmd" history.
6071 * Returns -1 for unknown history name.
6072 */
6073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 int i;
6077 int len = (int)STRLEN(name);
6078
6079 /* No argument: use current history. */
6080 if (len == 0)
6081 return hist_char2type(ccline.cmdfirstc);
6082
6083 for (i = 0; history_names[i] != NULL; ++i)
6084 if (STRNICMP(name, history_names[i], len) == 0)
6085 return i;
6086
6087 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6088 return hist_char2type(name[0]);
6089
6090 return -1;
6091}
6092
6093static int last_maptick = -1; /* last seen maptick */
6094
6095/*
6096 * Add the given string to the given history. If the string is already in the
6097 * history then it is moved to the front. "histype" may be one of he HIST_
6098 * values.
6099 */
6100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101add_to_history(
6102 int histype,
6103 char_u *new_entry,
6104 int in_map, /* consider maptick when inside a mapping */
6105 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 histentry_T *hisptr;
6108 int len;
6109
6110 if (hislen == 0) /* no history */
6111 return;
6112
Bram Moolenaara939e432013-11-09 05:30:26 +01006113 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6114 return;
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 /*
6117 * Searches inside the same mapping overwrite each other, so that only
6118 * the last line is kept. Be careful not to remove a line that was moved
6119 * down, only lines that were added.
6120 */
6121 if (histype == HIST_SEARCH && in_map)
6122 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006123 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 /* Current line is from the same mapping, remove it */
6126 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6127 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006128 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 --hisnum[histype];
6130 if (--hisidx[HIST_SEARCH] < 0)
6131 hisidx[HIST_SEARCH] = hislen - 1;
6132 }
6133 last_maptick = -1;
6134 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006135 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 if (++hisidx[histype] == hislen)
6138 hisidx[histype] = 0;
6139 hisptr = &history[histype][hisidx[histype]];
6140 vim_free(hisptr->hisstr);
6141
6142 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006143 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6145 if (hisptr->hisstr != NULL)
6146 hisptr->hisstr[len + 1] = sep;
6147
6148 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006149 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006150 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (histype == HIST_SEARCH && in_map)
6152 last_maptick = maptick;
6153 }
6154}
6155
6156#if defined(FEAT_EVAL) || defined(PROTO)
6157
6158/*
6159 * Get identifier of newest history entry.
6160 * "histype" may be one of the HIST_ values.
6161 */
6162 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006163get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6166 || hisidx[histype] < 0)
6167 return -1;
6168
6169 return history[histype][hisidx[histype]].hisnum;
6170}
6171
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * Calculate history index from a number:
6174 * num > 0: seen as identifying number of a history entry
6175 * num < 0: relative position in history wrt newest entry
6176 * "histype" may be one of the HIST_ values.
6177 */
6178 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 int i;
6182 histentry_T *hist;
6183 int wrapped = FALSE;
6184
6185 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6186 || (i = hisidx[histype]) < 0 || num == 0)
6187 return -1;
6188
6189 hist = history[histype];
6190 if (num > 0)
6191 {
6192 while (hist[i].hisnum > num)
6193 if (--i < 0)
6194 {
6195 if (wrapped)
6196 break;
6197 i += hislen;
6198 wrapped = TRUE;
6199 }
6200 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6201 return i;
6202 }
6203 else if (-num <= hislen)
6204 {
6205 i += num + 1;
6206 if (i < 0)
6207 i += hislen;
6208 if (hist[i].hisstr != NULL)
6209 return i;
6210 }
6211 return -1;
6212}
6213
6214/*
6215 * Get a history entry by its index.
6216 * "histype" may be one of the HIST_ values.
6217 */
6218 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 idx = calc_hist_idx(histype, idx);
6222 if (idx >= 0)
6223 return history[histype][idx].hisstr;
6224 else
6225 return (char_u *)"";
6226}
6227
6228/*
6229 * Clear all entries of a history.
6230 * "histype" may be one of the HIST_ values.
6231 */
6232 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006233clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 int i;
6236 histentry_T *hisptr;
6237
6238 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6239 {
6240 hisptr = history[histype];
6241 for (i = hislen; i--;)
6242 {
6243 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006244 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006245 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247 hisidx[histype] = -1; /* mark history as cleared */
6248 hisnum[histype] = 0; /* reset identifier counter */
6249 return OK;
6250 }
6251 return FAIL;
6252}
6253
6254/*
6255 * Remove all entries matching {str} from a history.
6256 * "histype" may be one of the HIST_ values.
6257 */
6258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 regmatch_T regmatch;
6262 histentry_T *hisptr;
6263 int idx;
6264 int i;
6265 int last;
6266 int found = FALSE;
6267
6268 regmatch.regprog = NULL;
6269 regmatch.rm_ic = FALSE; /* always match case */
6270 if (hislen != 0
6271 && histype >= 0
6272 && histype < HIST_COUNT
6273 && *str != NUL
6274 && (idx = hisidx[histype]) >= 0
6275 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6276 != NULL)
6277 {
6278 i = last = idx;
6279 do
6280 {
6281 hisptr = &history[histype][i];
6282 if (hisptr->hisstr == NULL)
6283 break;
6284 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6285 {
6286 found = TRUE;
6287 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006288 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 else
6291 {
6292 if (i != last)
6293 {
6294 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006295 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297 if (--last < 0)
6298 last += hislen;
6299 }
6300 if (--i < 0)
6301 i += hislen;
6302 } while (i != idx);
6303 if (history[histype][idx].hisstr == NULL)
6304 hisidx[histype] = -1;
6305 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006306 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 return found;
6308}
6309
6310/*
6311 * Remove an indexed entry from a history.
6312 * "histype" may be one of the HIST_ values.
6313 */
6314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int i, j;
6318
6319 i = calc_hist_idx(histype, idx);
6320 if (i < 0)
6321 return FALSE;
6322 idx = hisidx[histype];
6323 vim_free(history[histype][i].hisstr);
6324
6325 /* When deleting the last added search string in a mapping, reset
6326 * last_maptick, so that the last added search string isn't deleted again.
6327 */
6328 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6329 last_maptick = -1;
6330
6331 while (i != idx)
6332 {
6333 j = (i + 1) % hislen;
6334 history[histype][i] = history[histype][j];
6335 i = j;
6336 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006337 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (--i < 0)
6339 i += hislen;
6340 hisidx[histype] = i;
6341 return TRUE;
6342}
6343
6344#endif /* FEAT_EVAL */
6345
6346#if defined(FEAT_CRYPT) || defined(PROTO)
6347/*
6348 * Very specific function to remove the value in ":set key=val" from the
6349 * history.
6350 */
6351 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006352remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 char_u *p;
6355 int i;
6356
6357 i = hisidx[HIST_CMD];
6358 if (i < 0)
6359 return;
6360 p = history[HIST_CMD][i].hisstr;
6361 if (p != NULL)
6362 for ( ; *p; ++p)
6363 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6364 {
6365 p = vim_strchr(p + 3, '=');
6366 if (p == NULL)
6367 break;
6368 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006369 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (p[i] == '\\' && p[i + 1])
6371 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006372 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 --p;
6374 }
6375}
6376#endif
6377
6378#endif /* FEAT_CMDHIST */
6379
Bram Moolenaar064154c2016-03-19 22:50:43 +01006380#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006381/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006382 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006383 * ccline and put the previous value in prev_ccline.
6384 */
6385 static struct cmdline_info *
6386get_ccline_ptr(void)
6387{
6388 if ((State & CMDLINE) == 0)
6389 return NULL;
6390 if (ccline.cmdbuff != NULL)
6391 return &ccline;
6392 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6393 return &prev_ccline;
6394 return NULL;
6395}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006396#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006397
Bram Moolenaar064154c2016-03-19 22:50:43 +01006398#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006399/*
6400 * Get the current command line in allocated memory.
6401 * Only works when the command line is being edited.
6402 * Returns NULL when something is wrong.
6403 */
6404 char_u *
6405get_cmdline_str(void)
6406{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006407 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006408
Bram Moolenaaree91c332018-09-25 22:27:35 +02006409 if (cmdline_star > 0)
6410 return NULL;
6411 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006412 if (p == NULL)
6413 return NULL;
6414 return vim_strnsave(p->cmdbuff, p->cmdlen);
6415}
6416
6417/*
6418 * Get the current command line position, counted in bytes.
6419 * Zero is the first position.
6420 * Only works when the command line is being edited.
6421 * Returns -1 when something is wrong.
6422 */
6423 int
6424get_cmdline_pos(void)
6425{
6426 struct cmdline_info *p = get_ccline_ptr();
6427
6428 if (p == NULL)
6429 return -1;
6430 return p->cmdpos;
6431}
6432
6433/*
6434 * Set the command line byte position to "pos". Zero is the first position.
6435 * Only works when the command line is being edited.
6436 * Returns 1 when failed, 0 when OK.
6437 */
6438 int
6439set_cmdline_pos(
6440 int pos)
6441{
6442 struct cmdline_info *p = get_ccline_ptr();
6443
6444 if (p == NULL)
6445 return 1;
6446
6447 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6448 * changed the command line. */
6449 if (pos < 0)
6450 new_cmdpos = 0;
6451 else
6452 new_cmdpos = pos;
6453 return 0;
6454}
6455#endif
6456
6457#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6458/*
6459 * Get the current command-line type.
6460 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6461 * Only works when the command line is being edited.
6462 * Returns NUL when something is wrong.
6463 */
6464 int
6465get_cmdline_type(void)
6466{
6467 struct cmdline_info *p = get_ccline_ptr();
6468
6469 if (p == NULL)
6470 return NUL;
6471 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006472 return
6473# ifdef FEAT_EVAL
6474 (p->input_fn) ? '@' :
6475# endif
6476 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006477 return p->cmdfirstc;
6478}
6479#endif
6480
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6482/*
6483 * Get indices "num1,num2" that specify a range within a list (not a range of
6484 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6485 * Returns OK if parsed successfully, otherwise FAIL.
6486 */
6487 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006488get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 int len;
6491 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006492 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 *str = skipwhite(*str);
6495 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6496 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006497 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 *str += len;
6499 *num1 = (int)num;
6500 first = TRUE;
6501 }
6502 *str = skipwhite(*str);
6503 if (**str == ',') /* parse "to" part of range */
6504 {
6505 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006506 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (len > 0)
6508 {
6509 *num2 = (int)num;
6510 *str = skipwhite(*str + len);
6511 }
6512 else if (!first) /* no number given at all */
6513 return FAIL;
6514 }
6515 else if (first) /* only one number given */
6516 *num2 = *num1;
6517 return OK;
6518}
6519#endif
6520
6521#if defined(FEAT_CMDHIST) || defined(PROTO)
6522/*
6523 * :history command - print a history
6524 */
6525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006526ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 histentry_T *hist;
6529 int histype1 = HIST_CMD;
6530 int histype2 = HIST_CMD;
6531 int hisidx1 = 1;
6532 int hisidx2 = -1;
6533 int idx;
6534 int i, j, k;
6535 char_u *end;
6536 char_u *arg = eap->arg;
6537
6538 if (hislen == 0)
6539 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006540 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 return;
6542 }
6543
6544 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6545 {
6546 end = arg;
6547 while (ASCII_ISALPHA(*end)
6548 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6549 end++;
6550 i = *end;
6551 *end = NUL;
6552 histype1 = get_histtype(arg);
6553 if (histype1 == -1)
6554 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006555 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
6557 histype1 = 0;
6558 histype2 = HIST_COUNT-1;
6559 }
6560 else
6561 {
6562 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006563 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 return;
6565 }
6566 }
6567 else
6568 histype2 = histype1;
6569 *end = i;
6570 }
6571 else
6572 end = arg;
6573 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006575 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 return;
6577 }
6578
6579 for (; !got_int && histype1 <= histype2; ++histype1)
6580 {
6581 STRCPY(IObuff, "\n # ");
6582 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006583 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 idx = hisidx[histype1];
6585 hist = history[histype1];
6586 j = hisidx1;
6587 k = hisidx2;
6588 if (j < 0)
6589 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6590 if (k < 0)
6591 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6592 if (idx >= 0 && j <= k)
6593 for (i = idx + 1; !got_int; ++i)
6594 {
6595 if (i == hislen)
6596 i = 0;
6597 if (hist[i].hisstr != NULL
6598 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6599 {
6600 msg_putchar('\n');
6601 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6602 hist[i].hisnum);
6603 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6604 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006605 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 else
6607 STRCAT(IObuff, hist[i].hisstr);
6608 msg_outtrans(IObuff);
6609 out_flush();
6610 }
6611 if (i == idx)
6612 break;
6613 }
6614 }
6615}
6616#endif
6617
6618#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 int
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006620get_hislen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006622 return hislen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006625 histentry_T *
6626get_histentry(int hist_type)
6627{
6628 return history[hist_type];
6629}
6630
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006631 void
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006632set_histentry(int hist_type, histentry_T *entry)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006633{
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006634 history[hist_type] = entry;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006635}
6636
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006637 int *
6638get_hisidx(int hist_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006640 return &hisidx[hist_type];
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006641}
6642
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006643 int *
6644get_hisnum(int hist_type)
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006645{
Bram Moolenaar5f32ece2019-07-21 21:51:59 +02006646 return &hisnum[hist_type];
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006647}
6648#endif
6649
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650#if defined(FEAT_CMDWIN) || defined(PROTO)
6651/*
6652 * Open a window on the current command line and history. Allow editing in
6653 * the window. Returns when the window is closed.
6654 * Returns:
6655 * CR if the command is to be executed
6656 * Ctrl_C if it is to be abandoned
6657 * K_IGNORE if editing continues
6658 */
6659 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006660open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006662 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006664 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 win_T *wp;
6666 int i;
6667 linenr_T lnum;
6668 int histtype;
6669 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 int save_restart_edit = restart_edit;
6671 int save_State = State;
6672 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006673#ifdef FEAT_RIGHTLEFT
6674 int save_cmdmsg_rl = cmdmsg_rl;
6675#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006676#ifdef FEAT_FOLDING
6677 int save_KeyTyped;
6678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 /* Can't do this recursively. Can't do it when typing a password. */
6681 if (cmdwin_type != 0
6682# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6683 || cmdline_star > 0
6684# endif
6685 )
6686 {
6687 beep_flush();
6688 return K_IGNORE;
6689 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006690 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 /* Save current window sizes. */
6693 win_size_save(&winsizes);
6694
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006696 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006697
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01006698#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01006699 // When using completion in Insert mode with <C-R>=<C-F> one can open the
6700 // command line window, but we don't want the popup menu then.
6701 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01006702#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01006703
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006704 /* don't use a new tab page */
6705 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006706 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 /* Create a window for the command-line buffer. */
6709 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6710 {
6711 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006712 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 return K_IGNORE;
6714 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006715 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006718 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006719 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006722#ifdef FEAT_FOLDING
6723 curwin->w_p_fen = FALSE;
6724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006726 curwin->w_p_rl = cmdmsg_rl;
6727 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006729 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006732 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006733 /* But don't allow switching to another buffer. */
6734 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
Bram Moolenaar46152342005-09-07 21:18:43 +00006736 /* Showing the prompt may have set need_wait_return, reset it. */
6737 need_wait_return = FALSE;
6738
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006739 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6741 {
6742 if (p_wc == TAB)
6743 {
6744 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6745 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6746 }
6747 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6748 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006749 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006751 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6752 * sets 'textwidth' to 78). */
6753 curbuf->b_p_tw = 0;
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 /* Fill the buffer with the history. */
6756 init_history();
6757 if (hislen > 0)
6758 {
6759 i = hisidx[histtype];
6760 if (i >= 0)
6761 {
6762 lnum = 0;
6763 do
6764 {
6765 if (++i == hislen)
6766 i = 0;
6767 if (history[histtype][i].hisstr != NULL)
6768 ml_append(lnum++, history[histtype][i].hisstr,
6769 (colnr_T)0, FALSE);
6770 }
6771 while (i != hisidx[histtype]);
6772 }
6773 }
6774
6775 /* Replace the empty last line with the current command-line and put the
6776 * cursor there. */
6777 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6778 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6779 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006780 changed_line_abv_curs();
6781 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006782 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 /* No Ex mode here! */
6785 exmode_active = 0;
6786
6787 State = NORMAL;
6788# ifdef FEAT_MOUSE
6789 setmouse();
6790# endif
6791
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02006793 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006794 if (restart_edit != 0) /* autocmd with ":startinsert" */
6795 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797 i = RedrawingDisabled;
6798 RedrawingDisabled = 0;
6799
6800 /*
6801 * Call the main loop until <CR> or CTRL-C is typed.
6802 */
6803 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006804 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 RedrawingDisabled = i;
6807
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006808# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006809 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006810# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02006813 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006814
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006815# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006816 /* Restore KeyTyped in case it is modified by autocommands */
6817 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818# endif
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 exmode_active = save_exmode;
6822
Bram Moolenaarf9821062008-06-20 16:31:07 +00006823 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006825 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
6827 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006828 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830 else
6831 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006832# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 /* autocmds may abort script processing */
6834 if (aborting() && cmdwin_result != K_IGNORE)
6835 cmdwin_result = Ctrl_C;
6836# endif
6837 /* Set the new command line from the cmdline buffer. */
6838 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006839 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006841 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6842
6843 if (histtype == HIST_CMD)
6844 {
6845 /* Execute the command directly. */
6846 ccline.cmdbuff = vim_strsave((char_u *)p);
6847 cmdwin_result = CAR;
6848 }
6849 else
6850 {
6851 /* First need to cancel what we were doing. */
6852 ccline.cmdbuff = NULL;
6853 stuffcharReadbuff(':');
6854 stuffReadbuff((char_u *)p);
6855 stuffcharReadbuff(CAR);
6856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
6858 else if (cmdwin_result == K_XF2) /* :qa typed */
6859 {
6860 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6861 cmdwin_result = CAR;
6862 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006863 else if (cmdwin_result == Ctrl_C)
6864 {
6865 /* :q or :close, don't execute any command
6866 * and don't modify the cmd window. */
6867 ccline.cmdbuff = NULL;
6868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 else
6870 ccline.cmdbuff = vim_strsave(ml_get_curline());
6871 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02006872 {
6873 ccline.cmdbuff = vim_strsave((char_u *)"");
6874 ccline.cmdlen = 0;
6875 ccline.cmdbufflen = 1;
6876 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02006878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 else
6880 {
6881 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6882 ccline.cmdbufflen = ccline.cmdlen + 1;
6883 ccline.cmdpos = curwin->w_cursor.col;
6884 if (ccline.cmdpos > ccline.cmdlen)
6885 ccline.cmdpos = ccline.cmdlen;
6886 if (cmdwin_result == K_IGNORE)
6887 {
6888 set_cmdspos_cursor();
6889 redrawcmd();
6890 }
6891 }
6892
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006894 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006895# ifdef FEAT_CONCEAL
6896 /* Avoid command-line window first character being concealed. */
6897 curwin->w_p_cole = 0;
6898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006900 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 win_goto(old_curwin);
6902 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006903
6904 /* win_close() may have already wiped the buffer when 'bh' is
6905 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006906 if (bufref_valid(&bufref))
6907 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 /* Restore window sizes. */
6910 win_size_restore(&winsizes);
6911
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006912 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 }
6914
6915 ga_clear(&winsizes);
6916 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006917# ifdef FEAT_RIGHTLEFT
6918 cmdmsg_rl = save_cmdmsg_rl;
6919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 State = save_State;
6922# ifdef FEAT_MOUSE
6923 setmouse();
6924# endif
6925
6926 return cmdwin_result;
6927}
6928#endif /* FEAT_CMDWIN */
6929
6930/*
6931 * Used for commands that either take a simple command string argument, or:
6932 * cmd << endmarker
6933 * {script}
6934 * endmarker
6935 * Returns a pointer to allocated memory with {script} or NULL.
6936 */
6937 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 char_u *theline;
6941 char *end_pattern = NULL;
6942 char dot[] = ".";
6943 garray_T ga;
6944
6945 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6946 return NULL;
6947
6948 ga_init2(&ga, 1, 0x400);
6949
6950 if (cmd[2] != NUL)
6951 end_pattern = (char *)skipwhite(cmd + 2);
6952 else
6953 end_pattern = dot;
6954
6955 for (;;)
6956 {
6957 theline = eap->getline(
6958#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006959 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02006961 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006964 {
6965 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
6969 ga_concat(&ga, theline);
6970 ga_append(&ga, '\n');
6971 vim_free(theline);
6972 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006973 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 return (char_u *)ga.ga_data;
6976}