blob: 0a3d9d4dafe4bb87c018bad32f83867f230cf1c0 [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 Moolenaar438d1762018-09-30 17:11:48 +020020// The current cmdline_info. It is initialized in getcmdline() and after that
21// used by other functions. When invoking getcmdline() recursively it needs
22// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaar66b51422019-08-18 21:44:12 +020023static cmdline_info_T ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#ifdef FEAT_EVAL
26static int new_cmdpos; /* position set by set_cmdline_pos() */
27#endif
28
Bram Moolenaara92522f2017-07-15 15:21:38 +020029static int extra_char = NUL; /* extra character to display when redrawing
30 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020031static int extra_char_shift;
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_RIGHTLEFT
34static int cmd_hkmap = 0; /* Hebrew mapping during command line */
35#endif
36
Bram Moolenaar438d1762018-09-30 17:11:48 +020037static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010038static int cmdline_charsize(int idx);
39static void set_cmdspos(void);
40static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010041static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010042static void alloc_cmdbuff(int len);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010043static void draw_cmdline(int start, int len);
Bram Moolenaar66b51422019-08-18 21:44:12 +020044static void save_cmdline(cmdline_info_T *ccp);
45static void restore_cmdline(cmdline_info_T *ccp);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010048static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010050static void redrawcmdprompt(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010051static int ccheck_abbr(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +020054static int open_cmdwin(void);
Bram Moolenaar7bae0b12019-11-21 22:14:18 +010055
56static int cedit_key INIT(= -1); // key value of 'cedit' option
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020059
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020060 static void
61trigger_cmd_autocmd(int typechar, int evt)
62{
63 char_u typestr[2];
64
65 typestr[0] = typechar;
66 typestr[1] = NUL;
67 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
68}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020071 * Abandon the command line.
72 */
73 static void
74abandon_cmdline(void)
75{
Bram Moolenaard23a8232018-02-10 18:45:26 +010076 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020077 if (msg_scrolled == 0)
78 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010079 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020080 redraw_cmdline = TRUE;
81}
82
Bram Moolenaaree219b02017-12-17 14:55:01 +010083#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020084/*
Bram Moolenaar66216052017-12-16 16:33:44 +010085 * Guess that the pattern matches everything. Only finds specific cases, such
86 * as a trailing \|, which can happen while typing a pattern.
87 */
88 static int
89empty_pattern(char_u *p)
90{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010091 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010092
93 /* remove trailing \v and the like */
94 while (n >= 2 && p[n - 2] == '\\'
95 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
96 n -= 2;
97 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
98}
99
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200100// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200101typedef struct {
102 colnr_T vs_curswant;
103 colnr_T vs_leftcol;
104 linenr_T vs_topline;
105# ifdef FEAT_DIFF
106 int vs_topfill;
107# endif
108 linenr_T vs_botline;
109 linenr_T vs_empty_rows;
110} viewstate_T;
111
112 static void
113save_viewstate(viewstate_T *vs)
114{
115 vs->vs_curswant = curwin->w_curswant;
116 vs->vs_leftcol = curwin->w_leftcol;
117 vs->vs_topline = curwin->w_topline;
118# ifdef FEAT_DIFF
119 vs->vs_topfill = curwin->w_topfill;
120# endif
121 vs->vs_botline = curwin->w_botline;
122 vs->vs_empty_rows = curwin->w_empty_rows;
123}
124
125 static void
126restore_viewstate(viewstate_T *vs)
127{
128 curwin->w_curswant = vs->vs_curswant;
129 curwin->w_leftcol = vs->vs_leftcol;
130 curwin->w_topline = vs->vs_topline;
131# ifdef FEAT_DIFF
132 curwin->w_topfill = vs->vs_topfill;
133# endif
134 curwin->w_botline = vs->vs_botline;
135 curwin->w_empty_rows = vs->vs_empty_rows;
136}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200137
138// Struct to store the state of 'incsearch' highlighting.
139typedef struct {
140 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200141 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200142 viewstate_T init_viewstate;
143 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200144 pos_T match_start;
145 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200146 int did_incsearch;
147 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200148 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200149} incsearch_state_T;
150
151 static void
152init_incsearch_state(incsearch_state_T *is_state)
153{
154 is_state->match_start = curwin->w_cursor;
155 is_state->did_incsearch = FALSE;
156 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200157 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200158 CLEAR_POS(&is_state->match_end);
159 is_state->save_cursor = curwin->w_cursor; // may be restored later
160 is_state->search_start = curwin->w_cursor;
161 save_viewstate(&is_state->init_viewstate);
162 save_viewstate(&is_state->old_viewstate);
163}
164
165/*
166 * First move cursor to end of match, then to the start. This
167 * moves the whole match onto the screen when 'nowrap' is set.
168 */
169 static void
170set_search_match(pos_T *t)
171{
172 t->lnum += search_match_lines;
173 t->col = search_match_endcol;
174 if (t->lnum > curbuf->b_ml.ml_line_count)
175 {
176 t->lnum = curbuf->b_ml.ml_line_count;
177 coladvance((colnr_T)MAXCOL);
178 }
179}
180
181/*
182 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200183 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200184 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200185 */
186 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200187do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
188 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200189{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200190 char_u *cmd;
191 cmdmod_T save_cmdmod = cmdmod;
192 char_u *p;
193 int delim_optional = FALSE;
194 int delim;
195 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100196 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200197 exarg_T ea;
198 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200199 int use_last_pat;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100200 int retval = FALSE;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200201
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200202 *skiplen = 0;
203 *patlen = ccline.cmdlen;
204
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200205 if (!p_is || cmd_silent)
206 return FALSE;
207
208 // by default search all lines
209 search_first_line = 0;
210 search_last_line = MAXLNUM;
211
212 if (firstc == '/' || firstc == '?')
213 return TRUE;
214 if (firstc != ':')
215 return FALSE;
216
Bram Moolenaarc6725252019-11-23 21:56:46 +0100217 ++emsg_off;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200218 vim_memset(&ea, 0, sizeof(ea));
219 ea.line1 = 1;
220 ea.line2 = 1;
221 ea.cmd = ccline.cmdbuff;
222 ea.addr_type = ADDR_LINES;
223
224 parse_command_modifiers(&ea, &dummy, TRUE);
225 cmdmod = save_cmdmod;
226
227 cmd = skip_range(ea.cmd, NULL);
228 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100229 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200230
231 // Skip over "substitute" to find the pattern separator.
232 for (p = cmd; ASCII_ISALPHA(*p); ++p)
233 ;
234 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100235 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200236
237 if (STRNCMP(cmd, "substitute", p - cmd) == 0
238 || STRNCMP(cmd, "smagic", p - cmd) == 0
239 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
240 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200241 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200242 if (*cmd == 's' && cmd[1] == 'm')
243 p_magic = TRUE;
244 else if (*cmd == 's' && cmd[1] == 'n')
245 p_magic = FALSE;
246 }
247 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
248 {
249 // skip over flags
250 while (ASCII_ISALPHA(*(p = skipwhite(p))))
251 ++p;
252 if (*p == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100253 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200254 }
255 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
256 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
257 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
258 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
259 || STRNCMP(cmd, "global", p - cmd) == 0)
260 {
261 // skip over "!"
262 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200263 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200264 p++;
265 if (*skipwhite(p) == NUL)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100266 goto theend;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200267 }
268 if (*cmd != 'g')
269 delim_optional = TRUE;
270 }
271 else
Bram Moolenaarc6725252019-11-23 21:56:46 +0100272 goto theend;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200274 p = skipwhite(p);
275 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
276 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200277
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200278 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200279
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200280 if (end == p && !use_last_pat)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100281 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200282
283 // Don't do 'hlsearch' highlighting if the pattern matches everything.
284 if (!use_last_pat)
285 {
286 char c = *end;
287 int empty;
288
289 *end = NUL;
290 empty = empty_pattern(p);
291 *end = c;
292 if (empty)
Bram Moolenaarc6725252019-11-23 21:56:46 +0100293 goto theend;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200294 }
295
296 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200297 *skiplen = (int)(p - ccline.cmdbuff);
298 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200299
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200300 // parse the address range
301 save_cursor = curwin->w_cursor;
302 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200303 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200304 if (ea.addr_count > 0)
305 {
306 // Allow for reverse match.
307 if (ea.line2 < ea.line1)
308 {
309 search_first_line = ea.line2;
310 search_last_line = ea.line1;
311 }
312 else
313 {
314 search_first_line = ea.line1;
315 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200316 }
317 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200318 else if (cmd[0] == 's' && cmd[1] != 'o')
319 {
320 // :s defaults to the current line
321 search_first_line = curwin->w_cursor.lnum;
322 search_last_line = curwin->w_cursor.lnum;
323 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200324
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200325 curwin->w_cursor = save_cursor;
Bram Moolenaarc6725252019-11-23 21:56:46 +0100326 retval = TRUE;
327theend:
328 --emsg_off;
329 return retval;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200330}
331
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200332 static void
333finish_incsearch_highlighting(
334 int gotesc,
335 incsearch_state_T *is_state,
336 int call_update_screen)
337{
338 if (is_state->did_incsearch)
339 {
340 is_state->did_incsearch = FALSE;
341 if (gotesc)
342 curwin->w_cursor = is_state->save_cursor;
343 else
344 {
345 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
346 {
347 // put the '" mark at the original position
348 curwin->w_cursor = is_state->save_cursor;
349 setpcmark();
350 }
351 curwin->w_cursor = is_state->search_start;
352 }
353 restore_viewstate(&is_state->old_viewstate);
354 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200355
356 // by default search all lines
357 search_first_line = 0;
358 search_last_line = MAXLNUM;
359
360 p_magic = is_state->magic_save;
361
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200362 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200363 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200364 if (call_update_screen)
365 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200366 }
367}
368
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200369/*
370 * Do 'incsearch' highlighting if desired.
371 */
372 static void
373may_do_incsearch_highlighting(
374 int firstc,
375 long count,
376 incsearch_state_T *is_state)
377{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200378 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200379 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200380 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200381#ifdef FEAT_RELTIME
382 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200383 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200385 int next_char;
386 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200387 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200388
Bram Moolenaar198cb662018-09-06 21:44:17 +0200389 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100390 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200391 save_last_search_pattern();
392
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200393 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200394 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200395 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200396 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200397 if (did_do_incsearch && vpeekc() == NUL)
398 // may have skipped a redraw, do it now
399 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200400 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200401 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
403 // If there is a character waiting, search and redraw later.
404 if (char_avail())
405 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200406 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407 is_state->incsearch_postponed = TRUE;
408 return;
409 }
410 is_state->incsearch_postponed = FALSE;
411
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412 if (search_first_line == 0)
413 // start at the original cursor position
414 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100415 else if (search_first_line > curbuf->b_ml.ml_line_count)
416 {
417 // start after the last line
418 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
419 curwin->w_cursor.col = MAXCOL;
420 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200421 else
422 {
423 // start at the first line in the range
424 curwin->w_cursor.lnum = search_first_line;
425 curwin->w_cursor.col = 0;
426 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200427
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 // Use the previous pattern for ":s//".
429 next_char = ccline.cmdbuff[skiplen + patlen];
430 use_last_pat = patlen == 0 && skiplen > 0
431 && ccline.cmdbuff[skiplen - 1] == next_char;
432
433 // If there is no pattern, don't do anything.
434 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200435 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200436 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200437 set_no_hlsearch(TRUE); // turn off previous highlight
438 redraw_all_later(SOME_VALID);
439 }
440 else
441 {
442 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
443
444 cursor_off(); // so the user knows we're busy
445 out_flush();
446 ++emsg_off; // so it doesn't beep if bad expr
447#ifdef FEAT_RELTIME
448 // Set the time limit to half a second.
449 profile_setlimit(500L, &tm);
450#endif
451 if (!p_hls)
452 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200453 if (search_first_line != 0)
454 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200456#ifdef FEAT_RELTIME
457 vim_memset(&sia, 0, sizeof(sia));
458 sia.sa_tm = &tm;
459#endif
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200460 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200463 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200465 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466#endif
467 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469 --emsg_off;
470
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200471 if (curwin->w_cursor.lnum < search_first_line
472 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200473 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200474 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200475 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200476 curwin->w_cursor = is_state->search_start;
477 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 // if interrupted while searching, behave like it failed
480 if (got_int)
481 {
482 (void)vpeekc(); // remove <C-C> from input stream
483 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200484 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200485 }
486 else if (char_avail())
487 // cancelled searching because a char was typed
488 is_state->incsearch_postponed = TRUE;
489 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200490 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200491 highlight_match = TRUE; // highlight position
492 else
493 highlight_match = FALSE; // remove highlight
494
495 // First restore the old curwin values, so the screen is positioned in the
496 // same way as the actual search command.
497 restore_viewstate(&is_state->old_viewstate);
498 changed_cline_bef_curs();
499 update_topline();
500
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200501 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 {
503 pos_T save_pos = curwin->w_cursor;
504
505 is_state->match_start = curwin->w_cursor;
506 set_search_match(&curwin->w_cursor);
507 validate_cursor();
508 end_pos = curwin->w_cursor;
509 is_state->match_end = end_pos;
510 curwin->w_cursor = save_pos;
511 }
512 else
513 end_pos = curwin->w_cursor; // shutup gcc 4
514
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200515 // Disable 'hlsearch' highlighting if the pattern matches everything.
516 // Avoids a flash when typing "foo\|".
517 if (!use_last_pat)
518 {
519 next_char = ccline.cmdbuff[skiplen + patlen];
520 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200521 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
522 {
523 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200524 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200525 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200526 ccline.cmdbuff[skiplen + patlen] = next_char;
527 }
528
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 validate_cursor();
530 // May redraw the status line to show the cursor position.
531 if (p_ru && curwin->w_status_height > 0)
532 curwin->w_redr_status = TRUE;
533
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200535 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 restore_last_search_pattern();
537
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200538 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
539 // end of the pattern, e.g. for ":s/pat/".
540 if (ccline.cmdbuff[skiplen + patlen] != NUL)
541 curwin->w_cursor = is_state->search_start;
542 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200543 curwin->w_cursor = end_pos;
544
545 msg_starthere();
546 redrawcmdline();
547 is_state->did_incsearch = TRUE;
548}
549
550/*
551 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
552 * or previous match.
553 * Returns FAIL when jumping to cmdline_not_changed;
554 */
555 static int
556may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557 int firstc,
558 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563 pos_T t;
564 char_u *pat;
565 int search_flags = SEARCH_NOOF;
566 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200567 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568
Bram Moolenaar198cb662018-09-06 21:44:17 +0200569 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100570 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200571 save_last_search_pattern();
572
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200574 {
575 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200577 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200578 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200579 {
580 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200582 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200585 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200586 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200587 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200588 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200589 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200590 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200591 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 cursor_off();
594 out_flush();
595 if (c == Ctrl_G)
596 {
597 t = is_state->match_end;
598 if (LT_POS(is_state->match_start, is_state->match_end))
599 // Start searching at the end of the match not at the beginning of
600 // the next column.
601 (void)decl(&t);
602 search_flags += SEARCH_COL;
603 }
604 else
605 t = is_state->match_start;
606 if (!p_hls)
607 search_flags += SEARCH_KEEP;
608 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 save = pat[patlen];
610 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100611 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200613 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 if (i)
617 {
618 is_state->search_start = is_state->match_start;
619 is_state->match_end = t;
620 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622 {
623 // Move just before the current match, so that when nv_search
624 // finishes the cursor will be put back on the match.
625 is_state->search_start = t;
626 (void)decl(&is_state->search_start);
627 }
628 else if (c == Ctrl_G && firstc == '?')
629 {
630 // Move just after the current match, so that when nv_search
631 // finishes the cursor will be put back on the match.
632 is_state->search_start = t;
633 (void)incl(&is_state->search_start);
634 }
635 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
636 {
637 // wrap around
638 is_state->search_start = t;
639 if (firstc == '?')
640 (void)incl(&is_state->search_start);
641 else
642 (void)decl(&is_state->search_start);
643 }
644
645 set_search_match(&is_state->match_end);
646 curwin->w_cursor = is_state->match_start;
647 changed_cline_bef_curs();
648 update_topline();
649 validate_cursor();
650 highlight_match = TRUE;
651 save_viewstate(&is_state->old_viewstate);
652 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200653 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200655 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 }
657 else
658 vim_beep(BO_ERROR);
659 restore_last_search_pattern();
660 return FAIL;
661}
662
663/*
664 * When CTRL-L typed: add character from the match to the pattern.
665 * May set "*c" to the added character.
666 * Return OK when jumping to cmdline_not_changed.
667 */
668 static int
669may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
670{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200671 int skiplen, patlen;
672
Bram Moolenaar198cb662018-09-06 21:44:17 +0200673 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100674 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200675 save_last_search_pattern();
676
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200678 {
679 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200680 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200681 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100682 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683
684 // Add a character from under the cursor for 'incsearch'.
685 if (is_state->did_incsearch)
686 {
687 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200688 *c = gchar_cursor();
689 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 // If 'ignorecase' and 'smartcase' are set and the
692 // command line has no uppercase characters, convert
693 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200694 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200695 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200696 if (*c == firstc || vim_strchr((char_u *)(
697 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200698 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200699 // put a backslash before special characters
700 stuffcharReadbuff(*c);
701 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200703 // add any composing characters
704 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
705 {
706 int save_c = *c;
707
708 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
709 {
710 curwin->w_cursor.col += mb_char2len(*c);
711 *c = gchar_cursor();
712 stuffcharReadbuff(*c);
713 }
714 *c = save_c;
715 }
716 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200717 }
718 }
719 return OK;
720}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200721#endif
722
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200723#ifdef FEAT_ARABIC
724/*
725 * Return TRUE if the command line has an Arabic character at or after "start"
726 * for "len" bytes.
727 */
728 static int
729cmdline_has_arabic(int start, int len)
730{
731 int j;
732 int mb_l;
733 int u8c;
734 char_u *p;
735 int u8cc[MAX_MCO];
736
737 if (!enc_utf8)
738 return FALSE;
739
740 for (j = start; j < start + len; j += mb_l)
741 {
742 p = ccline.cmdbuff + j;
743 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
744 mb_l = utfc_ptr2len_len(p, start + len - j);
745 if (ARABIC_CHAR(u8c))
746 return TRUE;
747 }
748 return FALSE;
749}
750#endif
751
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200752 void
753cmdline_init(void)
754{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200755 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200756}
757
Bram Moolenaar66216052017-12-16 16:33:44 +0100758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * getcmdline() - accept a command line starting with firstc.
760 *
761 * firstc == ':' get ":" command line.
762 * firstc == '/' or '?' get search pattern
763 * firstc == '=' get expression
764 * firstc == '@' get text for input() function
765 * firstc == '>' get text for debug mode
766 * firstc == NUL get text for :insert command
767 * firstc == -1 like NUL, and break on CTRL-C
768 *
769 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
770 * command line.
771 *
772 * Careful: getcmdline() can be called recursively!
773 *
774 * Return pointer to allocated string if there is a commandline, NULL
775 * otherwise.
776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100778getcmdline(
779 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200780 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200781 int indent, // indent for inside conditionals
782 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200783{
784 return getcmdline_int(firstc, count, indent, TRUE);
785}
786
787 static char_u *
788getcmdline_int(
789 int firstc,
790 long count UNUSED, // only used for incremental search
791 int indent, // indent for inside conditionals
792 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 int c;
795 int i;
796 int j;
797 int gotesc = FALSE; /* TRUE when <ESC> just typed */
798 int do_abbr; /* when TRUE check for abbr. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 char_u *lookfor = NULL; /* string to match */
800 int hiscnt; /* current history line in use */
801 int histype; /* history type to be used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200803 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#endif
805 int did_wild_list = FALSE; /* did wild_list() recently */
806 int wim_index = 0; /* index in wim_flags[] */
807 int res;
808 int save_msg_scroll = msg_scroll;
809 int save_State = State; /* remember State when called */
810 int some_key_typed = FALSE; /* one of the keys was typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 /* mouse drag and release events are ignored, unless they are
812 * preceded with a mouse down event */
813 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_EVAL
815 int break_ctrl_c = FALSE;
816#endif
817 expand_T xpc;
818 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200819 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200820 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200821 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822
Bram Moolenaar438d1762018-09-30 17:11:48 +0200823 if (ccline.cmdbuff != NULL)
824 {
825 // Being called recursively. Since ccline is global, we need to save
826 // the current buffer and restore it when returning.
827 save_cmdline(&save_ccline);
828 did_save_ccline = TRUE;
829 }
830 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200831 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#ifdef FEAT_EVAL
834 if (firstc == -1)
835 {
836 firstc = NUL;
837 break_ctrl_c = TRUE;
838 }
839#endif
840#ifdef FEAT_RIGHTLEFT
841 /* start without Hebrew mapping for a command line */
842 if (firstc == ':' || firstc == '=' || firstc == '>')
843 cmd_hkmap = 0;
844#endif
845
846 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200849 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
851
852 /*
853 * set some variables for redrawcmd()
854 */
855 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000856 ccline.cmdindent = (firstc > 0 ? indent : 0);
857
858 /* alloc initial ccline.cmdbuff */
859 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200861 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 ccline.cmdlen = ccline.cmdpos = 0;
863 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100864 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000866 /* autoindent for :insert and :append */
867 if (firstc <= 0)
868 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200869 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000870 ccline.cmdbuff[indent] = NUL;
871 ccline.cmdpos = indent;
872 ccline.cmdspos = indent;
873 ccline.cmdlen = indent;
874 }
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000877 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879#ifdef FEAT_RIGHTLEFT
880 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
881 && (firstc == '/' || firstc == '?'))
882 cmdmsg_rl = TRUE;
883 else
884 cmdmsg_rl = FALSE;
885#endif
886
887 redir_off = TRUE; /* don't redirect the typed command */
888 if (!cmd_silent)
889 {
890 i = msg_scrolled;
891 msg_scrolled = 0; /* avoid wait_return message */
892 gotocmdline(TRUE);
893 msg_scrolled += i;
894 redrawcmdprompt(); /* draw prompt or indent */
895 set_cmdspos();
896 }
897 xpc.xp_context = EXPAND_NOTHING;
898 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000899#ifndef BACKSLASH_IN_FILENAME
900 xpc.xp_shell = FALSE;
901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000903#if defined(FEAT_EVAL)
904 if (ccline.input_fn)
905 {
906 xpc.xp_context = ccline.xp_context;
907 xpc.xp_pattern = ccline.cmdbuff;
908 xpc.xp_arg = ccline.xp_arg;
909 }
910#endif
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 /*
913 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
914 * doing ":@0" when register 0 doesn't contain a CR.
915 */
916 msg_scroll = FALSE;
917
918 State = CMDLINE;
919
920 if (firstc == '/' || firstc == '?' || firstc == '@')
921 {
922 /* Use ":lmap" mappings for search pattern and input(). */
923 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
924 b_im_ptr = &curbuf->b_p_iminsert;
925 else
926 b_im_ptr = &curbuf->b_p_imsearch;
927 if (*b_im_ptr == B_IMODE_LMAP)
928 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 im_set_active(*b_im_ptr == B_IMODE_IM);
931#endif
932 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100933#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 else if (p_imcmdline)
935 im_set_active(TRUE);
936#endif
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef CURSOR_SHAPE
940 ui_cursor_shape(); /* may show different cursor shape */
941#endif
942
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000943 /* When inside an autocommand for writing "exiting" may be set and
944 * terminal mode set to cooked. Need to set raw mode here then. */
945 settmode(TMODE_RAW);
946
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200947 /* Trigger CmdlineEnter autocommands. */
948 cmdline_type = firstc == NUL ? '-' : firstc;
949 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200952 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000956 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
958
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200959 /* If something above caused an error, reset the flags, we do want to type
960 * and execute commands. Display may be messed up a bit. */
961 if (did_emsg)
962 redrawcmd();
963 did_emsg = FALSE;
964 got_int = FALSE;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /*
967 * Collect the command string, handling editing keys.
968 */
969 for (;;)
970 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000971 redir_off = TRUE; /* Don't redirect the typed command.
972 Repeated, because a ":redir" inside
973 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef USE_ON_FLY_SCROLL
975 dont_scroll = FALSE; /* allow scrolling here */
976#endif
977 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
978
Bram Moolenaar72532d32018-04-07 19:09:09 +0200979 did_emsg = FALSE; /* There can't really be a reason why an error
980 that occurs while typing a command should
981 cause the command not to be executed. */
982
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200983 // Trigger SafeState if nothing is pending.
984 may_trigger_safestate(xpc.xp_numfiles <= 0);
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000987
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100988 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
989 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000990 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000991 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100992 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (KeyTyped)
995 {
996 some_key_typed = TRUE;
997#ifdef FEAT_RIGHTLEFT
998 if (cmd_hkmap)
999 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (cmdmsg_rl && !KeyStuffed)
1001 {
1002 /* Invert horizontal movements and operations. Only when
1003 * typed by the user directly, not when the result of a
1004 * mapping. */
1005 switch (c)
1006 {
1007 case K_RIGHT: c = K_LEFT; break;
1008 case K_S_RIGHT: c = K_S_LEFT; break;
1009 case K_C_RIGHT: c = K_C_LEFT; break;
1010 case K_LEFT: c = K_RIGHT; break;
1011 case K_S_LEFT: c = K_S_RIGHT; break;
1012 case K_C_LEFT: c = K_C_RIGHT; break;
1013 }
1014 }
1015#endif
1016 }
1017
1018 /*
1019 * Ignore got_int when CTRL-C was typed here.
1020 * Don't ignore it in :global, we really need to break then, e.g., for
1021 * ":g/pat/normal /pat" (without the <CR>).
1022 * Don't ignore it for the input() function.
1023 */
1024 if ((c == Ctrl_C
1025#ifdef UNIX
1026 || c == intr_char
1027#endif
1028 )
1029#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1030 && firstc != '@'
1031#endif
1032#ifdef FEAT_EVAL
1033 && !break_ctrl_c
1034#endif
1035 && !global_busy)
1036 got_int = FALSE;
1037
Bram Moolenaard7663c22019-08-06 21:59:57 +02001038 // free old command line when finished moving around in the history
1039 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001041 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001042 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && c != K_PAGEDOWN && c != K_PAGEUP
1044 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001045 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001047 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001050 * When there are matching completions to select <S-Tab> works like
1051 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001053 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 c = Ctrl_P;
1055
1056#ifdef FEAT_WILDMENU
1057 /* Special translations for 'wildmenu' */
1058 if (did_wild_list && p_wmnu)
1059 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001060 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 c = Ctrl_N;
1064 }
1065 /* Hitting CR after "emenu Name.": complete submenu */
1066 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1067 && ccline.cmdpos > 1
1068 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1069 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1070 && (c == '\n' || c == '\r' || c == K_KENTER))
1071 c = K_DOWN;
1072#endif
1073
1074 /* free expanded names when finished walking through matches */
1075 if (xpc.xp_numfiles != -1
1076 && !(c == p_wc && KeyTyped) && c != p_wcm
1077 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1078 && c != Ctrl_L)
1079 {
1080 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1081 did_wild_list = FALSE;
1082#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001083 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085 xpc.xp_context = EXPAND_NOTHING;
1086 wim_index = 0;
1087#ifdef FEAT_WILDMENU
1088 if (p_wmnu && wild_menu_showing != 0)
1089 {
1090 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001091 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001092
1093 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001094 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 if (wild_menu_showing == WM_SCROLLED)
1097 {
1098 /* Entered command line, move it up */
1099 cmdline_row--;
1100 redrawcmd();
1101 }
1102 else if (save_p_ls != -1)
1103 {
1104 /* restore 'laststatus' and 'winminheight' */
1105 p_ls = save_p_ls;
1106 p_wmh = save_p_wmh;
1107 last_status(FALSE);
1108 update_screen(VALID); /* redraw the screen NOW */
1109 redrawcmd();
1110 save_p_ls = -1;
1111 }
1112 else
1113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 redraw_statuslines();
1116 }
1117 KeyTyped = skt;
1118 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001119 if (ccline.input_fn)
1120 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122#endif
1123 }
1124
1125#ifdef FEAT_WILDMENU
1126 /* Special translations for 'wildmenu' */
1127 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1128 {
1129 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001130 if (c == K_DOWN && ccline.cmdpos > 0
1131 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001133 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
1135 /* Hitting <Up>: Remove one submenu name in front of the
1136 * cursor */
1137 int found = FALSE;
1138
1139 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1140 i = 0;
1141 while (--j > 0)
1142 {
1143 /* check for start of menu name */
1144 if (ccline.cmdbuff[j] == ' '
1145 && ccline.cmdbuff[j - 1] != '\\')
1146 {
1147 i = j + 1;
1148 break;
1149 }
1150 /* check for start of submenu name */
1151 if (ccline.cmdbuff[j] == '.'
1152 && ccline.cmdbuff[j - 1] != '\\')
1153 {
1154 if (found)
1155 {
1156 i = j + 1;
1157 break;
1158 }
1159 else
1160 found = TRUE;
1161 }
1162 }
1163 if (i > 0)
1164 cmdline_del(i);
1165 c = p_wc;
1166 xpc.xp_context = EXPAND_NOTHING;
1167 }
1168 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001169 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001170 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001171 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 char_u upseg[5];
1174
1175 upseg[0] = PATHSEP;
1176 upseg[1] = '.';
1177 upseg[2] = '.';
1178 upseg[3] = PATHSEP;
1179 upseg[4] = NUL;
1180
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001181 if (c == K_DOWN
1182 && ccline.cmdpos > 0
1183 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1184 && (ccline.cmdpos < 3
1185 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1187 {
1188 /* go down a directory */
1189 c = p_wc;
1190 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001191 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
1193 /* If in a direct ancestor, strip off one ../ to go down */
1194 int found = FALSE;
1195
1196 j = ccline.cmdpos;
1197 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1198 while (--j > i)
1199 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001200 if (has_mbyte)
1201 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (vim_ispathsep(ccline.cmdbuff[j]))
1203 {
1204 found = TRUE;
1205 break;
1206 }
1207 }
1208 if (found
1209 && ccline.cmdbuff[j - 1] == '.'
1210 && ccline.cmdbuff[j - 2] == '.'
1211 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1212 {
1213 cmdline_del(j - 2);
1214 c = p_wc;
1215 }
1216 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001217 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 /* go up a directory */
1220 int found = FALSE;
1221
1222 j = ccline.cmdpos - 1;
1223 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1224 while (--j > i)
1225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (has_mbyte)
1227 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (vim_ispathsep(ccline.cmdbuff[j])
1229#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001230 && vim_strchr((char_u *)" *?[{`$%#",
1231 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233 )
1234 {
1235 if (found)
1236 {
1237 i = j + 1;
1238 break;
1239 }
1240 else
1241 found = TRUE;
1242 }
1243 }
1244
1245 if (!found)
1246 j = i;
1247 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1248 j += 4;
1249 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1250 && j == i)
1251 j += 3;
1252 else
1253 j = 0;
1254 if (j > 0)
1255 {
1256 /* TODO this is only for DOS/UNIX systems - need to put in
1257 * machine-specific stuff here and in upseg init */
1258 cmdline_del(j);
1259 put_on_cmdline(upseg + 1, 3, FALSE);
1260 }
1261 else if (ccline.cmdpos > i)
1262 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001263
1264 /* Now complete in the new directory. Set KeyTyped in case the
1265 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001267 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271#endif /* FEAT_WILDMENU */
1272
1273 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1274 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1275 if (c == Ctrl_BSL)
1276 {
1277 ++no_mapping;
1278 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001279 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 --no_mapping;
1281 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001282 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1283 * is in a mapping. */
1284 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001285 || (ccline.cmdfirstc == '=' && KeyTyped)
1286#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001287 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001288#endif
1289 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
1291 vungetc(c);
1292 c = Ctrl_BSL;
1293 }
1294#ifdef FEAT_EVAL
1295 else if (c == 'e')
1296 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001297 char_u *p = NULL;
1298 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 /*
1301 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001302 * Need to save and restore the current command line, to be
1303 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 */
1305 if (ccline.cmdpos == ccline.cmdlen)
1306 new_cmdpos = 99999; /* keep it at the end */
1307 else
1308 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (c == '=')
1312 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001313 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001314 * to avoid nasty things like going to another buffer when
1315 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001316 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001318 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001319
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001320 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001322 len = (int)STRLEN(p);
1323 if (realloc_cmdbuff(len + 1) == OK)
1324 {
1325 ccline.cmdlen = len;
1326 STRCPY(ccline.cmdbuff, p);
1327 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001329 /* Restore the cursor or use the position set with
1330 * set_cmdline_pos(). */
1331 if (new_cmdpos > ccline.cmdlen)
1332 ccline.cmdpos = ccline.cmdlen;
1333 else
1334 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001336 KeyTyped = FALSE; /* Don't do p_wc completion. */
1337 redrawcmd();
1338 goto cmdline_changed;
1339 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001340 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342 }
1343 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001344 got_int = FALSE; /* don't abandon the command line */
1345 did_emsg = FALSE;
1346 emsg_on_display = FALSE;
1347 redrawcmd();
1348 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#endif
1351 else
1352 {
1353 if (c == Ctrl_G && p_im && restart_edit == 0)
1354 restart_edit = 'a';
1355 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1356 in history */
1357 goto returncmd; /* back to Normal mode */
1358 }
1359 }
1360
1361#ifdef FEAT_CMDWIN
1362 if (c == cedit_key || c == K_CMDWIN)
1363 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001364 if (ex_normal_busy == 0 && got_int == FALSE)
1365 {
1366 /*
1367 * Open a window to edit the command line (and history).
1368 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001369 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001370 some_key_typed = TRUE;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373# ifdef FEAT_DIGRAPHS
1374 else
1375# endif
1376#endif
1377#ifdef FEAT_DIGRAPHS
1378 c = do_digraph(c);
1379#endif
1380
1381 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1382 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1383 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 /* In Ex mode a backslash escapes a newline. */
1385 if (exmode_active
1386 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001387 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001388 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001391 if (c == K_KENTER)
1392 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001394 else
1395 {
1396 gotesc = FALSE; /* Might have typed ESC previously, don't
1397 truncate the cmdline now. */
1398 if (ccheck_abbr(c + ABBR_OFF))
1399 goto cmdline_changed;
1400 if (!cmd_silent)
1401 {
1402 windgoto(msg_row, 0);
1403 out_flush();
1404 }
1405 break;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408
1409 /*
1410 * Completion for 'wildchar' or 'wildcharm' key.
1411 * - hitting <ESC> twice means: abandon command line.
1412 * - wildcard expansion is only done when the 'wildchar' key is really
1413 * typed, not when it comes from a macro
1414 */
1415 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1416 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001417 int options = WILD_NO_BEEP;
1418 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1419 options |= WILD_BUFLASTUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1421 {
1422 /* if 'wildmode' contains "list" may still need to list */
1423 if (xpc.xp_numfiles > 1
1424 && !did_wild_list
1425 && (wim_flags[wim_index] & WIM_LIST))
1426 {
1427 (void)showmatches(&xpc, FALSE);
1428 redrawcmd();
1429 did_wild_list = TRUE;
1430 }
1431 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001432 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001433 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001435 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001436 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
1438 res = OK; /* don't insert 'wildchar' now */
1439 }
1440 else /* typed p_wc first time */
1441 {
1442 wim_index = 0;
1443 j = ccline.cmdpos;
1444 /* if 'wildmode' first contains "longest", get longest
1445 * common part */
1446 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001447 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001448 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001450 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001451 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 /* if interrupted while completing, behave like it failed */
1454 if (got_int)
1455 {
1456 (void)vpeekc(); /* remove <C-C> from input stream */
1457 got_int = FALSE; /* don't abandon the command line */
1458 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1459#ifdef FEAT_WILDMENU
1460 xpc.xp_context = EXPAND_NOTHING;
1461#endif
1462 goto cmdline_changed;
1463 }
1464
1465 /* when more than one match, and 'wildmode' first contains
1466 * "list", or no change and 'wildmode' contains "longest,list",
1467 * list all matches */
1468 if (res == OK && xpc.xp_numfiles > 1)
1469 {
1470 /* a "longest" that didn't do anything is skipped (but not
1471 * "list:longest") */
1472 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1473 wim_index = 1;
1474 if ((wim_flags[wim_index] & WIM_LIST)
1475#ifdef FEAT_WILDMENU
1476 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1477#endif
1478 )
1479 {
1480 if (!(wim_flags[0] & WIM_LONGEST))
1481 {
1482#ifdef FEAT_WILDMENU
1483 int p_wmnu_save = p_wmnu;
1484 p_wmnu = 0;
1485#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 /* remove match */
1487 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#ifdef FEAT_WILDMENU
1489 p_wmnu = p_wmnu_save;
1490#endif
1491 }
1492#ifdef FEAT_WILDMENU
1493 (void)showmatches(&xpc, p_wmnu
1494 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1495#else
1496 (void)showmatches(&xpc, FALSE);
1497#endif
1498 redrawcmd();
1499 did_wild_list = TRUE;
1500 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001501 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001502 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001504 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001505 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001508 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 }
1510#ifdef FEAT_WILDMENU
1511 else if (xpc.xp_numfiles == -1)
1512 xpc.xp_context = EXPAND_NOTHING;
1513#endif
1514 }
1515 if (wim_index < 3)
1516 ++wim_index;
1517 if (c == ESC)
1518 gotesc = TRUE;
1519 if (res == OK)
1520 goto cmdline_changed;
1521 }
1522
1523 gotesc = FALSE;
1524
1525 /* <S-Tab> goes to last match, in a clumsy way */
1526 if (c == K_S_TAB && KeyTyped)
1527 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001528 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1529 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1530 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 goto cmdline_changed;
1532 }
1533
1534 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1535 c = NL;
1536
1537 do_abbr = TRUE; /* default: check for abbreviation */
1538
1539 /*
1540 * Big switch for a typed command line character.
1541 */
1542 switch (c)
1543 {
1544 case K_BS:
1545 case Ctrl_H:
1546 case K_DEL:
1547 case K_KDEL:
1548 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (c == K_KDEL)
1550 c = K_DEL;
1551
1552 /*
1553 * delete current character is the same as backspace on next
1554 * character, except at end of line
1555 */
1556 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1557 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (has_mbyte && c == K_DEL)
1559 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1560 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (ccline.cmdpos > 0)
1562 {
1563 char_u *p;
1564
1565 j = ccline.cmdpos;
1566 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (has_mbyte)
1568 {
1569 p = mb_prevptr(ccline.cmdbuff, p);
1570 if (c == Ctrl_W)
1571 {
1572 while (p > ccline.cmdbuff && vim_isspace(*p))
1573 p = mb_prevptr(ccline.cmdbuff, p);
1574 i = mb_get_class(p);
1575 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1576 p = mb_prevptr(ccline.cmdbuff, p);
1577 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001578 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001581 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1584 --p;
1585 i = vim_iswordc(p[-1]);
1586 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1587 && vim_iswordc(p[-1]) == i)
1588 --p;
1589 }
1590 else
1591 --p;
1592 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1593 ccline.cmdlen -= j - ccline.cmdpos;
1594 i = ccline.cmdpos;
1595 while (i < ccline.cmdlen)
1596 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1597
1598 /* Truncate at the end, required for multi-byte chars. */
1599 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001600#ifdef FEAT_SEARCH_EXTRA
1601 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001602 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001603 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001604 /* save view settings, so that the screen
1605 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001606 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001607 }
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 redrawcmd();
1610 }
1611 else if (ccline.cmdlen == 0 && c != Ctrl_W
1612 && ccline.cmdprompt == NULL && indent == 0)
1613 {
1614 /* In ex and debug mode it doesn't make sense to return. */
1615 if (exmode_active
1616#ifdef FEAT_EVAL
1617 || ccline.cmdfirstc == '>'
1618#endif
1619 )
1620 goto cmdline_not_changed;
1621
Bram Moolenaard23a8232018-02-10 18:45:26 +01001622 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (!cmd_silent)
1624 {
1625#ifdef FEAT_RIGHTLEFT
1626 if (cmdmsg_rl)
1627 msg_col = Columns;
1628 else
1629#endif
1630 msg_col = 0;
1631 msg_putchar(' '); /* delete ':' */
1632 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633#ifdef FEAT_SEARCH_EXTRA
1634 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 redraw_cmdline = TRUE;
1638 goto returncmd; /* back to cmd mode */
1639 }
1640 goto cmdline_changed;
1641
1642 case K_INS:
1643 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 ccline.overstrike = !ccline.overstrike;
1645#ifdef CURSOR_SHAPE
1646 ui_cursor_shape(); /* may show different cursor shape */
1647#endif
1648 goto cmdline_not_changed;
1649
1650 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001651 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 /* ":lmap" mappings exists, toggle use of mappings. */
1654 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 im_set_active(FALSE); /* Disable input method */
1657#endif
1658 if (b_im_ptr != NULL)
1659 {
1660 if (State & LANGMAP)
1661 *b_im_ptr = B_IMODE_LMAP;
1662 else
1663 *b_im_ptr = B_IMODE_NONE;
1664 }
1665 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 else
1668 {
1669 /* There are no ":lmap" mappings, toggle IM. When
1670 * 'imdisable' is set don't try getting the status, it's
1671 * always off. */
1672 if ((p_imdisable && b_im_ptr != NULL)
1673 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1674 {
1675 im_set_active(FALSE); /* Disable input method */
1676 if (b_im_ptr != NULL)
1677 *b_im_ptr = B_IMODE_NONE;
1678 }
1679 else
1680 {
1681 im_set_active(TRUE); /* Enable input method */
1682 if (b_im_ptr != NULL)
1683 *b_im_ptr = B_IMODE_IM;
1684 }
1685 }
1686#endif
1687 if (b_im_ptr != NULL)
1688 {
1689 if (b_im_ptr == &curbuf->b_p_iminsert)
1690 set_iminsert_global();
1691 else
1692 set_imsearch_global();
1693 }
1694#ifdef CURSOR_SHAPE
1695 ui_cursor_shape(); /* may show different cursor shape */
1696#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001697#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 /* Show/unshow value of 'keymap' in status lines later. */
1699 status_redraw_curbuf();
1700#endif
1701 goto cmdline_not_changed;
1702
1703/* case '@': only in very old vi */
1704 case Ctrl_U:
1705 /* delete all characters left of the cursor */
1706 j = ccline.cmdpos;
1707 ccline.cmdlen -= j;
1708 i = ccline.cmdpos = 0;
1709 while (i < ccline.cmdlen)
1710 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1711 /* Truncate at the end, required for multi-byte chars. */
1712 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001713#ifdef FEAT_SEARCH_EXTRA
1714 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001715 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 redrawcmd();
1718 goto cmdline_changed;
1719
1720#ifdef FEAT_CLIPBOARD
1721 case Ctrl_Y:
1722 /* Copy the modeless selection, if there is one. */
1723 if (clip_star.state != SELECT_CLEARED)
1724 {
1725 if (clip_star.state == SELECT_DONE)
1726 clip_copy_modeless_selection(TRUE);
1727 goto cmdline_not_changed;
1728 }
1729 break;
1730#endif
1731
1732 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1733 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001734 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001735 * ":normal" runs out of characters. */
1736 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001737 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 goto cmdline_not_changed;
1739
1740 gotesc = TRUE; /* will free ccline.cmdbuff after
1741 putting it in history */
1742 goto returncmd; /* back to cmd mode */
1743
1744 case Ctrl_R: /* insert register */
1745#ifdef USE_ON_FLY_SCROLL
1746 dont_scroll = TRUE; /* disallow scrolling here */
1747#endif
1748 putcmdline('"', TRUE);
1749 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001750 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (i == Ctrl_O)
1752 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1753 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001754 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001755 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 --no_mapping;
1757#ifdef FEAT_EVAL
1758 /*
1759 * Insert the result of an expression.
1760 * Need to save the current command line, to be able to enter
1761 * a new one...
1762 */
1763 new_cmdpos = -1;
1764 if (c == '=')
1765 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001766 if (ccline.cmdfirstc == '=' // can't do this recursively
1767 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
1769 beep_flush();
1770 c = ESC;
1771 }
1772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775#endif
1776 if (c != ESC) /* use ESC to cancel inserting register */
1777 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001778 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001779
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001780#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001781 /* When there was a serious error abort getting the
1782 * command line. */
1783 if (aborting())
1784 {
1785 gotesc = TRUE; /* will free ccline.cmdbuff after
1786 putting it in history */
1787 goto returncmd; /* back to cmd mode */
1788 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 KeyTyped = FALSE; /* Don't do p_wc completion. */
1791#ifdef FEAT_EVAL
1792 if (new_cmdpos >= 0)
1793 {
1794 /* set_cmdline_pos() was used */
1795 if (new_cmdpos > ccline.cmdlen)
1796 ccline.cmdpos = ccline.cmdlen;
1797 else
1798 ccline.cmdpos = new_cmdpos;
1799 }
1800#endif
1801 }
1802 redrawcmd();
1803 goto cmdline_changed;
1804
1805 case Ctrl_D:
1806 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1807 break; /* Use ^D as normal char instead */
1808
1809 redrawcmd();
1810 continue; /* don't do incremental search now */
1811
1812 case K_RIGHT:
1813 case K_S_RIGHT:
1814 case K_C_RIGHT:
1815 do
1816 {
1817 if (ccline.cmdpos >= ccline.cmdlen)
1818 break;
1819 i = cmdline_charsize(ccline.cmdpos);
1820 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1821 break;
1822 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001824 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 + ccline.cmdpos);
1826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 ++ccline.cmdpos;
1828 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001829 while ((c == K_S_RIGHT || c == K_C_RIGHT
1830 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (has_mbyte)
1833 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 goto cmdline_not_changed;
1835
1836 case K_LEFT:
1837 case K_S_LEFT:
1838 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001839 if (ccline.cmdpos == 0)
1840 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 do
1842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (has_mbyte) /* move to first byte of char */
1845 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1846 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1848 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001849 while (ccline.cmdpos > 0
1850 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001851 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (has_mbyte)
1854 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 goto cmdline_not_changed;
1856
1857 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001858 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001859 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
Bram Moolenaar4f974752019-02-17 17:44:42 +01001861#ifdef FEAT_GUI_MSWIN
1862 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1863 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001864 case K_F4:
1865 if (mod_mask == MOD_MASK_ALT)
1866 {
1867 redrawcmd(); /* somehow the cmdline is cleared */
1868 goto cmdline_not_changed;
1869 }
1870 break;
1871#endif
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 case K_MIDDLEDRAG:
1874 case K_MIDDLERELEASE:
1875 goto cmdline_not_changed; /* Ignore mouse */
1876
1877 case K_MIDDLEMOUSE:
1878# ifdef FEAT_GUI
1879 /* When GUI is active, also paste when 'mouse' is empty */
1880 if (!gui.in_use)
1881# endif
1882 if (!mouse_has(MOUSE_COMMAND))
1883 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001884# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001886 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001888# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001889 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 redrawcmd();
1891 goto cmdline_changed;
1892
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001893# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001895 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 redrawcmd();
1897 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 case K_LEFTDRAG:
1901 case K_LEFTRELEASE:
1902 case K_RIGHTDRAG:
1903 case K_RIGHTRELEASE:
1904 /* Ignore drag and release events when the button-down wasn't
1905 * seen before. */
1906 if (ignore_drag_release)
1907 goto cmdline_not_changed;
1908 /* FALLTHROUGH */
1909 case K_LEFTMOUSE:
1910 case K_RIGHTMOUSE:
1911 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1912 ignore_drag_release = TRUE;
1913 else
1914 ignore_drag_release = FALSE;
1915# ifdef FEAT_GUI
1916 /* When GUI is active, also move when 'mouse' is empty */
1917 if (!gui.in_use)
1918# endif
1919 if (!mouse_has(MOUSE_COMMAND))
1920 goto cmdline_not_changed; /* Ignore mouse */
1921# ifdef FEAT_CLIPBOARD
1922 if (mouse_row < cmdline_row && clip_star.available)
1923 {
1924 int button, is_click, is_drag;
1925
1926 /*
1927 * Handle modeless selection.
1928 */
1929 button = get_mouse_button(KEY2TERMCAP1(c),
1930 &is_click, &is_drag);
1931 if (mouse_model_popup() && button == MOUSE_LEFT
1932 && (mod_mask & MOD_MASK_SHIFT))
1933 {
1934 /* Translate shift-left to right button. */
1935 button = MOUSE_RIGHT;
1936 mod_mask &= ~MOD_MASK_SHIFT;
1937 }
1938 clip_modeless(button, is_click, is_drag);
1939 goto cmdline_not_changed;
1940 }
1941# endif
1942
1943 set_cmdspos();
1944 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1945 ++ccline.cmdpos)
1946 {
1947 i = cmdline_charsize(ccline.cmdpos);
1948 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1949 && mouse_col < ccline.cmdspos % Columns + i)
1950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (has_mbyte)
1952 {
1953 /* Count ">" for double-wide char that doesn't fit. */
1954 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001955 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 + ccline.cmdpos) - 1;
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 ccline.cmdspos += i;
1959 }
1960 goto cmdline_not_changed;
1961
1962 /* Mouse scroll wheel: ignored here */
1963 case K_MOUSEDOWN:
1964 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001965 case K_MOUSELEFT:
1966 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 /* Alternate buttons ignored here */
1968 case K_X1MOUSE:
1969 case K_X1DRAG:
1970 case K_X1RELEASE:
1971 case K_X2MOUSE:
1972 case K_X2DRAG:
1973 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001974 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto cmdline_not_changed;
1976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#ifdef FEAT_GUI
1978 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1979 case K_LEFTRELEASE_NM:
1980 goto cmdline_not_changed;
1981
1982 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001983 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 gui_do_scroll();
1986 redrawcmd();
1987 }
1988 goto cmdline_not_changed;
1989
1990 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001991 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001993 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 redrawcmd();
1995 }
1996 goto cmdline_not_changed;
1997#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001998#ifdef FEAT_GUI_TABLINE
1999 case K_TABLINE:
2000 case K_TABMENU:
2001 /* Don't want to change any tabs here. Make sure the same tab
2002 * is still selected. */
2003 if (gui_use_tabline())
2004 gui_mch_set_curtab(tabpage_index(curtab));
2005 goto cmdline_not_changed;
2006#endif
2007
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 case K_SELECT: /* end of Select mode mapping - ignore */
2009 goto cmdline_not_changed;
2010
2011 case Ctrl_B: /* begin of command line */
2012 case K_HOME:
2013 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 case K_S_HOME:
2015 case K_C_HOME:
2016 ccline.cmdpos = 0;
2017 set_cmdspos();
2018 goto cmdline_not_changed;
2019
2020 case Ctrl_E: /* end of command line */
2021 case K_END:
2022 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case K_S_END:
2024 case K_C_END:
2025 ccline.cmdpos = ccline.cmdlen;
2026 set_cmdspos_cursor();
2027 goto cmdline_not_changed;
2028
2029 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002030 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 break;
2032 goto cmdline_changed;
2033
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002034 case Ctrl_L:
2035#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002036 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002037 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002038#endif
2039
2040 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002041 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 break;
2043 goto cmdline_changed;
2044
2045 case Ctrl_N: /* next match */
2046 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002047 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002049 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2050 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002052 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002054 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 case K_UP:
2056 case K_DOWN:
2057 case K_S_UP:
2058 case K_S_DOWN:
2059 case K_PAGEUP:
2060 case K_KPAGEUP:
2061 case K_PAGEDOWN:
2062 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002063 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 goto cmdline_not_changed;
2065
2066 i = hiscnt;
2067
2068 /* save current command string so it can be restored later */
2069 if (lookfor == NULL)
2070 {
2071 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2072 goto cmdline_not_changed;
2073 lookfor[ccline.cmdpos] = NUL;
2074 }
2075
2076 j = (int)STRLEN(lookfor);
2077 for (;;)
2078 {
2079 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002080 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002081 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002083 if (hiscnt == get_hislen()) /* first time */
2084 hiscnt = *get_hisidx(histype);
2085 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2086 hiscnt = get_hislen() - 1;
2087 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 --hiscnt;
2089 else /* at top of list */
2090 {
2091 hiscnt = i;
2092 break;
2093 }
2094 }
2095 else /* one step forwards */
2096 {
2097 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002098 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002100 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 break;
2102 }
2103
2104 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002105 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002107 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 hiscnt = 0;
2109 else
2110 ++hiscnt;
2111 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002112 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
2114 hiscnt = i;
2115 break;
2116 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002117 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002118 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002119 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 lookfor, (size_t)j) == 0)
2121 break;
2122 }
2123
2124 if (hiscnt != i) /* jumped to other entry */
2125 {
2126 char_u *p;
2127 int len;
2128 int old_firstc;
2129
Bram Moolenaar438d1762018-09-30 17:11:48 +02002130 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002131 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002132 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 p = lookfor; /* back to the old one */
2134 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002135 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 if (histype == HIST_SEARCH
2138 && p != lookfor
2139 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2140 {
2141 /* Correct for the separator character used when
2142 * adding the history entry vs the one used now.
2143 * First loop: count length.
2144 * Second loop: copy the characters. */
2145 for (i = 0; i <= 1; ++i)
2146 {
2147 len = 0;
2148 for (j = 0; p[j] != NUL; ++j)
2149 {
2150 /* Replace old sep with new sep, unless it is
2151 * escaped. */
2152 if (p[j] == old_firstc
2153 && (j == 0 || p[j - 1] != '\\'))
2154 {
2155 if (i > 0)
2156 ccline.cmdbuff[len] = firstc;
2157 }
2158 else
2159 {
2160 /* Escape new sep, unless it is already
2161 * escaped. */
2162 if (p[j] == firstc
2163 && (j == 0 || p[j - 1] != '\\'))
2164 {
2165 if (i > 0)
2166 ccline.cmdbuff[len] = '\\';
2167 ++len;
2168 }
2169 if (i > 0)
2170 ccline.cmdbuff[len] = p[j];
2171 }
2172 ++len;
2173 }
2174 if (i == 0)
2175 {
2176 alloc_cmdbuff(len);
2177 if (ccline.cmdbuff == NULL)
2178 goto returncmd;
2179 }
2180 }
2181 ccline.cmdbuff[len] = NUL;
2182 }
2183 else
2184 {
2185 alloc_cmdbuff((int)STRLEN(p));
2186 if (ccline.cmdbuff == NULL)
2187 goto returncmd;
2188 STRCPY(ccline.cmdbuff, p);
2189 }
2190
2191 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2192 redrawcmd();
2193 goto cmdline_changed;
2194 }
2195 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002196 goto cmdline_not_changed;
2197
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002198#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002199 case Ctrl_G: /* next match */
2200 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002201 if (may_adjust_incsearch_highlighting(
2202 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002203 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002204 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205#endif
2206
2207 case Ctrl_V:
2208 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 putcmdline('^', TRUE);
2211 c = get_literal(); /* get next (two) character(s) */
2212 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002213 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 /* may need to remove ^ when composing char was typed */
2215 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2216 {
2217 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2218 msg_putchar(' ');
2219 cursorcmd();
2220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 break;
2222
2223#ifdef FEAT_DIGRAPHS
2224 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002227# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002231 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (c != NUL)
2233 break;
2234
2235 redrawcmd();
2236 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002237#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239#ifdef FEAT_RIGHTLEFT
2240 case Ctrl__: /* CTRL-_: switch language mode */
2241 if (!p_ari)
2242 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002243 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 goto cmdline_not_changed;
2245#endif
2246
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002247 case K_PS:
2248 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2249 goto cmdline_changed;
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 default:
2252#ifdef UNIX
2253 if (c == intr_char)
2254 {
2255 gotesc = TRUE; /* will free ccline.cmdbuff after
2256 putting it in history */
2257 goto returncmd; /* back to Normal mode */
2258 }
2259#endif
2260 /*
2261 * Normal character with no special meaning. Just set mod_mask
2262 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2263 * the string <S-Space>. This should only happen after ^V.
2264 */
2265 if (!IS_SPECIAL(c))
2266 mod_mask = 0x0;
2267 break;
2268 }
2269 /*
2270 * End of switch on command line character.
2271 * We come here if we have a normal character.
2272 */
2273
Bram Moolenaar13505972019-01-24 15:04:48 +01002274 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2275 && (ccheck_abbr(
2276 // Add ABBR_OFF for characters above 0x100, this is
2277 // what check_abbr() expects.
2278 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2279 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 goto cmdline_changed;
2281
2282 /*
2283 * put the character in the command line
2284 */
2285 if (IS_SPECIAL(c) || mod_mask != 0)
2286 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2287 else
2288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (has_mbyte)
2290 {
2291 j = (*mb_char2bytes)(c, IObuff);
2292 IObuff[j] = NUL; /* exclude composing chars */
2293 put_on_cmdline(IObuff, j, TRUE);
2294 }
2295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 IObuff[0] = c;
2298 put_on_cmdline(IObuff, 1, TRUE);
2299 }
2300 }
2301 goto cmdline_changed;
2302
2303/*
2304 * This part implements incremental searches for "/" and "?"
2305 * Jump to cmdline_not_changed when a character has been read but the command
2306 * line did not change. Then we only search and redraw if something changed in
2307 * the past.
2308 * Jump to cmdline_changed when the command line did change.
2309 * (Sorry for the goto's, I know it is ugly).
2310 */
2311cmdline_not_changed:
2312#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002313 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 continue;
2315#endif
2316
2317cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002318 /* Trigger CmdlineChanged autocommands. */
2319 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002322 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324
2325#ifdef FEAT_RIGHTLEFT
2326 if (cmdmsg_rl
2327# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002328 || (p_arshape && !p_tbidi
2329 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330# endif
2331 )
2332 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002333 * right-left typing. Not efficient, but it works.
2334 * Do it only when there are no characters left to read
2335 * to avoid useless intermediate redraws. */
2336 if (vpeekc() == NUL)
2337 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338#endif
2339 }
2340
2341returncmd:
2342
2343#ifdef FEAT_RIGHTLEFT
2344 cmdmsg_rl = FALSE;
2345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002348 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002351 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#endif
2353
2354 if (ccline.cmdbuff != NULL)
2355 {
2356 /*
2357 * Put line in history buffer (":" and "=" only when it was typed).
2358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (ccline.cmdlen && firstc != NUL
2360 && (some_key_typed || histype == HIST_SEARCH))
2361 {
2362 add_to_history(histype, ccline.cmdbuff, TRUE,
2363 histype == HIST_SEARCH ? firstc : NUL);
2364 if (firstc == ':')
2365 {
2366 vim_free(new_last_cmdline);
2367 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2368 }
2369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002371 if (gotesc)
2372 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374
2375 /*
2376 * If the screen was shifted up, redraw the whole screen (later).
2377 * If the line is too long, clear it, so ruler and shown command do
2378 * not get printed in the middle of it.
2379 */
2380 msg_check();
2381 msg_scroll = save_msg_scroll;
2382 redir_off = FALSE;
2383
2384 /* When the command line was typed, no need for a wait-return prompt. */
2385 if (some_key_typed)
2386 need_wait_return = FALSE;
2387
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002388 /* Trigger CmdlineLeave autocommands. */
2389 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002392#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2394 im_save_status(b_im_ptr);
2395 im_set_active(FALSE);
2396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifdef CURSOR_SHAPE
2399 ui_cursor_shape(); /* may show different cursor shape */
2400#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002401 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
Bram Moolenaar438d1762018-09-30 17:11:48 +02002403theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002404 {
2405 char_u *p = ccline.cmdbuff;
2406
Bram Moolenaar438d1762018-09-30 17:11:48 +02002407 if (did_save_ccline)
2408 restore_cmdline(&save_ccline);
2409 else
2410 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002411 return p;
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414
2415#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2416/*
2417 * Get a command line with a prompt.
2418 * This is prepared to be called recursively from getcmdline() (e.g. by
2419 * f_input() when evaluating an expression from CTRL-R =).
2420 * Returns the command line in allocated memory, or NULL.
2421 */
2422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002423getcmdline_prompt(
2424 int firstc,
2425 char_u *prompt, /* command line prompt */
2426 int attr, /* attributes for prompt */
2427 int xp_context, /* type of expansion */
2428 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002431 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002432 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002434 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
Bram Moolenaar438d1762018-09-30 17:11:48 +02002436 if (ccline.cmdbuff != NULL)
2437 {
2438 // Save the values of the current cmdline and restore them below.
2439 save_cmdline(&save_ccline);
2440 did_save_ccline = TRUE;
2441 }
2442
Bram Moolenaar66b51422019-08-18 21:44:12 +02002443 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 ccline.cmdprompt = prompt;
2445 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002446# ifdef FEAT_EVAL
2447 ccline.xp_context = xp_context;
2448 ccline.xp_arg = xp_arg;
2449 ccline.input_fn = (firstc == '@');
2450# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002451 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002452 s = getcmdline_int(firstc, 1L, 0, FALSE);
2453
2454 if (did_save_ccline)
2455 restore_cmdline(&save_ccline);
2456
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002457 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002458 /* Restore msg_col, the prompt from input() may have changed it.
2459 * But only if called recursively and the commandline is therefore being
2460 * restored to an old one; if not, the input() prompt stays on the screen,
2461 * so we need its modified msg_col left intact. */
2462 if (ccline.cmdbuff != NULL)
2463 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 return s;
2466}
2467#endif
2468
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002469/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01002470 * Read the 'wildmode' option, fill wim_flags[].
2471 */
2472 int
2473check_opt_wim(void)
2474{
2475 char_u new_wim_flags[4];
2476 char_u *p;
2477 int i;
2478 int idx = 0;
2479
2480 for (i = 0; i < 4; ++i)
2481 new_wim_flags[i] = 0;
2482
2483 for (p = p_wim; *p; ++p)
2484 {
2485 for (i = 0; ASCII_ISALPHA(p[i]); ++i)
2486 ;
2487 if (p[i] != NUL && p[i] != ',' && p[i] != ':')
2488 return FAIL;
2489 if (i == 7 && STRNCMP(p, "longest", 7) == 0)
2490 new_wim_flags[idx] |= WIM_LONGEST;
2491 else if (i == 4 && STRNCMP(p, "full", 4) == 0)
2492 new_wim_flags[idx] |= WIM_FULL;
2493 else if (i == 4 && STRNCMP(p, "list", 4) == 0)
2494 new_wim_flags[idx] |= WIM_LIST;
2495 else if (i == 8 && STRNCMP(p, "lastused", 8) == 0)
2496 new_wim_flags[idx] |= WIM_BUFLASTUSED;
2497 else
2498 return FAIL;
2499 p += i;
2500 if (*p == NUL)
2501 break;
2502 if (*p == ',')
2503 {
2504 if (idx == 3)
2505 return FAIL;
2506 ++idx;
2507 }
2508 }
2509
2510 /* fill remaining entries with last flag */
2511 while (idx < 3)
2512 {
2513 new_wim_flags[idx + 1] = new_wim_flags[idx];
2514 ++idx;
2515 }
2516
2517 /* only when there are no errors, wim_flags[] is changed */
2518 for (i = 0; i < 4; ++i)
2519 wim_flags[i] = new_wim_flags[i];
2520 return OK;
2521}
2522
2523/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002524 * Return TRUE when the text must not be changed and we can't switch to
2525 * another window or buffer. Used when editing the command line, evaluating
2526 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002527 */
2528 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002529text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002530{
2531#ifdef FEAT_CMDWIN
2532 if (cmdwin_type != 0)
2533 return TRUE;
2534#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002535 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002536}
2537
2538/*
2539 * Give an error message for a command that isn't allowed while the cmdline
2540 * window is open or editing the cmdline in another way.
2541 */
2542 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002543text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002544{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002545 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002546}
2547
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002548 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002549get_text_locked_msg(void)
2550{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002551#ifdef FEAT_CMDWIN
2552 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002553 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002555 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002556}
2557
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002558/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002559 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2560 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002561 */
2562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002563curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002564{
2565 if (curbuf_lock > 0)
2566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002567 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002568 return TRUE;
2569 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002570 return allbuf_locked();
2571}
2572
2573/*
2574 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2575 * message.
2576 */
2577 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002578allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002579{
2580 if (allbuf_lock > 0)
2581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002582 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002583 return TRUE;
2584 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585 return FALSE;
2586}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2592 if (cmdline_star > 0) /* showing '*', always 1 position */
2593 return 1;
2594#endif
2595 return ptr2cells(ccline.cmdbuff + idx);
2596}
2597
2598/*
2599 * Compute the offset of the cursor on the command line for the prompt and
2600 * indent.
2601 */
2602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002605 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 ccline.cmdspos = 1 + ccline.cmdindent;
2607 else
2608 ccline.cmdspos = 0 + ccline.cmdindent;
2609}
2610
2611/*
2612 * Compute the screen position for the cursor on the command line.
2613 */
2614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002615set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 int i, m, c;
2618
2619 set_cmdspos();
2620 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002623 if (m < 0) /* overflow, Columns or Rows at weird value */
2624 m = MAXCOL;
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else
2627 m = MAXCOL;
2628 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2629 {
2630 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2632 if (has_mbyte)
2633 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002634 /* If the cmdline doesn't fit, show cursor on last visible char.
2635 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if ((ccline.cmdspos += c) >= m)
2637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ccline.cmdspos -= c;
2639 break;
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002642 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644}
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646/*
2647 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2648 * character that doesn't fit, so that a ">" must be displayed.
2649 */
2650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002651correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002653 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2655 && ccline.cmdspos % Columns + cells > Columns)
2656 ccline.cmdspos++;
2657}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659/*
2660 * Get an Ex command line for the ":" command.
2661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002663getexline(
2664 int c, /* normally ':', NUL for ":append" */
2665 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002666 int indent, /* indent for inside conditionals */
2667 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 /* When executing a register, remove ':' that's in front of each line. */
2670 if (exec_from_reg && vpeekc() == ':')
2671 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002672 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673}
2674
2675/*
2676 * Get an Ex command line for Ex mode.
2677 * In Ex mode we only use the OS supplied line editing features and no
2678 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002682getexmodeline(
2683 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002684 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002685 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002686 int indent, /* indent for inside conditionals */
2687 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 garray_T line_ga;
2690 char_u *pend;
2691 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002692 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 int escaped = FALSE; /* CTRL-V typed */
2694 int vcol = 0;
2695 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002696 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002697 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /* Switch cursor on now. This avoids that it happens after the "\n", which
2700 * confuses the system function that computes tabstops. */
2701 cursor_on();
2702
2703 /* always start in column 0; write a newline if necessary */
2704 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002709 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 if (p_prompt)
2711 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 while (indent-- > 0)
2713 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716
2717 ga_init2(&line_ga, 1, 30);
2718
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002719 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002721 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002722 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002723 while (indent >= 8)
2724 {
2725 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002726 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002727 indent -= 8;
2728 }
2729 while (indent-- > 0)
2730 {
2731 ga_append(&line_ga, ' ');
2732 msg_putchar(' ');
2733 }
2734 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 ++no_mapping;
2736 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 /*
2739 * Get the line, one character at a time.
2740 */
2741 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002744 long sw;
2745 char_u *s;
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (ga_grow(&line_ga, 40) == FAIL)
2748 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaarba748c82017-02-26 14:00:07 +01002750 /*
2751 * Get one character at a time.
2752 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002753 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002754
2755 /* Check for a ":normal" command and no more characters left. */
2756 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2757 c1 = '\n';
2758 else
2759 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 * Handle line editing.
2763 * Previously this was left to the system, putting the terminal in
2764 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 msg_putchar('\n');
2769 break;
2770 }
2771
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002772 if (c1 == K_PS)
2773 {
2774 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2775 goto redraw;
2776 }
2777
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 if (!escaped)
2779 {
2780 /* CR typed means "enter", which is NL */
2781 if (c1 == '\r')
2782 c1 = '\n';
2783
2784 if (c1 == BS || c1 == K_BS
2785 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 if (line_ga.ga_len > 0)
2788 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002789 if (has_mbyte)
2790 {
2791 p = (char_u *)line_ga.ga_data;
2792 p[line_ga.ga_len] = NUL;
2793 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2794 line_ga.ga_len -= len;
2795 }
2796 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002797 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 goto redraw;
2799 }
2800 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 msg_col = startcol;
2806 msg_clr_eos();
2807 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002808 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 }
2810
2811 if (c1 == Ctrl_T)
2812 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002813 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002814 p = (char_u *)line_ga.ga_data;
2815 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002816 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002817 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002819 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002821 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002822 p = (char_u *)line_ga.ga_data;
2823 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2825 *s = ' ';
2826 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828redraw:
2829 /* redraw the line */
2830 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002832 p = (char_u *)line_ga.ga_data;
2833 p[line_ga.ga_len] = NUL;
2834 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002840 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002845 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002846 msg_outtrans_len(p, len);
2847 vcol += ptr2cells(p);
2848 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
2850 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002852 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 continue;
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002856 if (c1 == Ctrl_D)
2857 {
2858 /* Delete one shiftwidth. */
2859 p = (char_u *)line_ga.ga_data;
2860 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 if (prev_char == '^')
2863 ex_keep_indent = TRUE;
2864 indent = 0;
2865 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 else
2868 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002870 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002871 if (indent > 0)
2872 {
2873 --indent;
2874 indent -= indent % get_sw_value(curbuf);
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002877 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002879 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2881 --line_ga.ga_len;
2882 }
2883 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002885
2886 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2887 {
2888 escaped = TRUE;
2889 continue;
2890 }
2891
2892 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2893 if (IS_SPECIAL(c1))
2894 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896
2897 if (IS_SPECIAL(c1))
2898 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002899 if (has_mbyte)
2900 len = (*mb_char2bytes)(c1,
2901 (char_u *)line_ga.ga_data + line_ga.ga_len);
2902 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002903 {
2904 len = 1;
2905 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2906 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002907 if (c1 == '\n')
2908 msg_putchar('\n');
2909 else if (c1 == TAB)
2910 {
2911 /* Don't use chartabsize(), 'ts' can be different */
2912 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002914 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002918 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002919 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002922 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002923 escaped = FALSE;
2924
2925 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002926 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002927
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002928 /* We are done when a NL is entered, but not when it comes after an
2929 * odd number of backslashes, that results in a NUL. */
2930 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002932 int bcount = 0;
2933
2934 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2935 ++bcount;
2936
2937 if (bcount > 0)
2938 {
2939 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2940 * "\NL", etc. */
2941 line_ga.ga_len -= (bcount + 1) / 2;
2942 pend -= (bcount + 1) / 2;
2943 pend[-1] = '\n';
2944 }
2945
2946 if ((bcount & 1) == 0)
2947 {
2948 --line_ga.ga_len;
2949 --pend;
2950 *pend = NUL;
2951 break;
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 }
2955
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002956 --no_mapping;
2957 --allow_keys;
2958
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 /* make following messages go to the next line */
2960 msg_didout = FALSE;
2961 msg_col = 0;
2962 if (msg_row < Rows - 1)
2963 ++msg_row;
2964 emsg_on_display = FALSE; /* don't want ui_delay() */
2965
2966 if (got_int)
2967 ga_clear(&line_ga);
2968
2969 return (char_u *)line_ga.ga_data;
2970}
2971
Bram Moolenaare344bea2005-09-01 20:46:49 +00002972# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2973 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
2975 * Return TRUE if ccline.overstrike is on.
2976 */
2977 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002978cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 return ccline.overstrike;
2981}
2982
2983/*
2984 * Return TRUE if the cursor is at the end of the cmdline.
2985 */
2986 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002987cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 return (ccline.cmdpos >= ccline.cmdlen);
2990}
2991#endif
2992
Bram Moolenaar9372a112005-12-06 19:59:18 +00002993#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994/*
2995 * Return the virtual column number at the current cursor position.
2996 * This is used by the IM code to obtain the start of the preedit string.
2997 */
2998 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3002 return MAXCOL;
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (has_mbyte)
3005 {
3006 colnr_T col;
3007 int i = 0;
3008
3009 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003010 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 return col;
3013 }
3014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 return ccline.cmdpos;
3016}
3017#endif
3018
3019#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3020/*
3021 * If part of the command line is an IM preedit string, redraw it with
3022 * IM feedback attributes. The cursor position is restored after drawing.
3023 */
3024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if ((State & CMDLINE)
3028 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003029 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 && !p_imdisable
3031 && im_is_preediting())
3032 {
3033 int cmdpos = 0;
3034 int cmdspos;
3035 int old_row;
3036 int old_col;
3037 colnr_T col;
3038
3039 old_row = msg_row;
3040 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003041 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if (has_mbyte)
3044 {
3045 for (col = 0; col < preedit_start_col
3046 && cmdpos < ccline.cmdlen; ++col)
3047 {
3048 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003049 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 }
3052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
3054 cmdspos += preedit_start_col;
3055 cmdpos += preedit_start_col;
3056 }
3057
3058 msg_row = cmdline_row + (cmdspos / (int)Columns);
3059 msg_col = cmdspos % (int)Columns;
3060 if (msg_row >= Rows)
3061 msg_row = Rows - 1;
3062
3063 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3064 {
3065 int char_len;
3066 int char_attr;
3067
3068 char_attr = im_get_feedback_attr(col);
3069 if (char_attr < 0)
3070 break; /* end of preedit string */
3071
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003073 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 char_len = 1;
3076
3077 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3078 cmdpos += char_len;
3079 }
3080
3081 msg_row = old_row;
3082 msg_col = old_col;
3083 }
3084}
3085#endif /* FEAT_XIM && FEAT_GUI_GTK */
3086
3087/*
3088 * Allocate a new command line buffer.
3089 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 */
3091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 /*
3095 * give some extra space to avoid having to allocate all the time
3096 */
3097 if (len < 80)
3098 len = 100;
3099 else
3100 len += 20;
3101
3102 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3103 ccline.cmdbufflen = len;
3104}
3105
3106/*
3107 * Re-allocate the command line to length len + something extra.
3108 * return FAIL for failure, OK otherwise
3109 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003111realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 char_u *p;
3114
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003115 if (len < ccline.cmdbufflen)
3116 return OK; /* no need to resize */
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 p = ccline.cmdbuff;
3119 alloc_cmdbuff(len); /* will get some more */
3120 if (ccline.cmdbuff == NULL) /* out of memory */
3121 {
3122 ccline.cmdbuff = p; /* keep the old one */
3123 return FAIL;
3124 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003125 /* There isn't always a NUL after the command, but it may need to be
3126 * there, thus copy up to the NUL and add a NUL. */
3127 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3128 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003130
3131 if (ccline.xpc != NULL
3132 && ccline.xpc->xp_pattern != NULL
3133 && ccline.xpc->xp_context != EXPAND_NOTHING
3134 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3135 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003136 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003137
3138 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3139 * to point into the newly allocated memory. */
3140 if (i >= 0 && i <= ccline.cmdlen)
3141 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3142 }
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return OK;
3145}
3146
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147#if defined(FEAT_ARABIC) || defined(PROTO)
3148static char_u *arshape_buf = NULL;
3149
3150# if defined(EXITFREE) || defined(PROTO)
3151 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003152free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003153{
3154 vim_free(arshape_buf);
3155}
3156# endif
3157#endif
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159/*
3160 * Draw part of the cmdline at the current cursor position. But draw stars
3161 * when cmdline_star is TRUE.
3162 */
3163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003164draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3167 int i;
3168
3169 if (cmdline_star > 0)
3170 for (i = 0; i < len; ++i)
3171 {
3172 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003174 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176 else
3177#endif
3178#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003179 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 static int buflen = 0;
3182 char_u *p;
3183 int j;
3184 int newlen = 0;
3185 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003186 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 int prev_c = 0;
3188 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003189 int u8c;
3190 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 /*
3194 * Do arabic shaping into a temporary buffer. This is very
3195 * inefficient!
3196 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003197 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 /* Re-allocate the buffer. We keep it around to avoid a lot of
3200 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003202 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 arshape_buf = alloc(buflen);
3204 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 return; /* out of memory */
3206 }
3207
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003208 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3209 {
3210 /* Prepend a space to draw the leading composing char on. */
3211 arshape_buf[0] = ' ';
3212 newlen = 1;
3213 }
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 for (j = start; j < start + len; j += mb_l)
3216 {
3217 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003218 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003219 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (ARABIC_CHAR(u8c))
3221 {
3222 /* Do Arabic shaping. */
3223 if (cmdmsg_rl)
3224 {
3225 /* displaying from right to left */
3226 pc = prev_c;
3227 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003228 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (j + mb_l >= start + len)
3230 nc = NUL;
3231 else
3232 nc = utf_ptr2char(p + mb_l);
3233 }
3234 else
3235 {
3236 /* displaying from left to right */
3237 if (j + mb_l >= start + len)
3238 pc = NUL;
3239 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003240 {
3241 int pcc[MAX_MCO];
3242
3243 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003245 pc1 = pcc[0];
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 nc = prev_c;
3248 }
3249 prev_c = u8c;
3250
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003251 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003254 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003256 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3257 if (u8cc[1] != 0)
3258 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
3261 }
3262 else
3263 {
3264 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 newlen += mb_l;
3267 }
3268 }
3269
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 }
3272 else
3273#endif
3274 msg_outtrans_len(ccline.cmdbuff + start, len);
3275}
3276
3277/*
3278 * Put a character on the command line. Shifts the following text to the
3279 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3280 * "c" must be printable (fit in one display cell)!
3281 */
3282 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003283putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 if (cmd_silent)
3286 return;
3287 msg_no_more = TRUE;
3288 msg_putchar(c);
3289 if (shift)
3290 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3291 msg_no_more = FALSE;
3292 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003293 extra_char = c;
3294 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297/*
3298 * Undo a putcmdline(c, FALSE).
3299 */
3300 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003301unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 if (cmd_silent)
3304 return;
3305 msg_no_more = TRUE;
3306 if (ccline.cmdlen == ccline.cmdpos)
3307 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003308 else if (has_mbyte)
3309 draw_cmdline(ccline.cmdpos,
3310 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else
3312 draw_cmdline(ccline.cmdpos, 1);
3313 msg_no_more = FALSE;
3314 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003315 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316}
3317
3318/*
3319 * Put the given string, of the given length, onto the command line.
3320 * If len is -1, then STRLEN() is used to calculate the length.
3321 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3322 * part will be redrawn, otherwise it will not. If this function is called
3323 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3324 * called afterwards.
3325 */
3326 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003327put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int retval;
3330 int i;
3331 int m;
3332 int c;
3333
3334 if (len < 0)
3335 len = (int)STRLEN(str);
3336
3337 /* Check if ccline.cmdbuff needs to be longer */
3338 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003339 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
3341 retval = OK;
3342 if (retval == OK)
3343 {
3344 if (!ccline.overstrike)
3345 {
3346 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3347 ccline.cmdbuff + ccline.cmdpos,
3348 (size_t)(ccline.cmdlen - ccline.cmdpos));
3349 ccline.cmdlen += len;
3350 }
3351 else
3352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (has_mbyte)
3354 {
3355 /* Count nr of characters in the new string. */
3356 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003357 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 ++m;
3359 /* Count nr of bytes in cmdline that are overwritten by these
3360 * characters. */
3361 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003362 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 --m;
3364 if (i < ccline.cmdlen)
3365 {
3366 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3367 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3368 ccline.cmdlen += ccline.cmdpos + len - i;
3369 }
3370 else
3371 ccline.cmdlen = ccline.cmdpos + len;
3372 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003373 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 ccline.cmdlen = ccline.cmdpos + len;
3375 }
3376 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3377 ccline.cmdbuff[ccline.cmdlen] = NUL;
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (enc_utf8)
3380 {
3381 /* When the inserted text starts with a composing character,
3382 * backup to the character before it. There could be two of them.
3383 */
3384 i = 0;
3385 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3386 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3387 {
3388 i = (*mb_head_off)(ccline.cmdbuff,
3389 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3390 ccline.cmdpos -= i;
3391 len += i;
3392 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3393 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003394#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3396 {
3397 /* Check the previous character for Arabic combining pair. */
3398 i = (*mb_head_off)(ccline.cmdbuff,
3399 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3400 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3401 + ccline.cmdpos - i), c))
3402 {
3403 ccline.cmdpos -= i;
3404 len += i;
3405 }
3406 else
3407 i = 0;
3408 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (i != 0)
3411 {
3412 /* Also backup the cursor position. */
3413 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3414 ccline.cmdspos -= i;
3415 msg_col -= i;
3416 if (msg_col < 0)
3417 {
3418 msg_col += Columns;
3419 --msg_row;
3420 }
3421 }
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 if (redraw && !cmd_silent)
3425 {
3426 msg_no_more = TRUE;
3427 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003428 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3430 /* Avoid clearing the rest of the line too often. */
3431 if (cmdline_row != i || ccline.overstrike)
3432 msg_clr_eos();
3433 msg_no_more = FALSE;
3434 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003435 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003437 m = Columns * Rows;
3438 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003440 }
3441 else
3442 m = MAXCOL;
3443 for (i = 0; i < len; ++i)
3444 {
3445 c = cmdline_charsize(ccline.cmdpos);
3446 /* count ">" for a double-wide char that doesn't fit. */
3447 if (has_mbyte)
3448 correct_cmdspos(ccline.cmdpos, c);
3449 /* Stop cursor at the end of the screen, but do increment the
3450 * insert position, so that entering a very long command
3451 * works, even though you can't see it. */
3452 if (ccline.cmdspos + c < m)
3453 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003454
Bram Moolenaar14184a32019-02-16 15:10:30 +01003455 if (has_mbyte)
3456 {
3457 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3458 if (c > len - i - 1)
3459 c = len - i - 1;
3460 ccline.cmdpos += c;
3461 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003463 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 }
3466 if (redraw)
3467 msg_check();
3468 return retval;
3469}
3470
Bram Moolenaar66b51422019-08-18 21:44:12 +02003471static cmdline_info_T prev_ccline;
3472static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003473
3474/*
3475 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3476 * and overwrite it. But get_cmdline_str() may need it, thus make it
3477 * available globally in prev_ccline.
3478 */
3479 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003481{
3482 if (!prev_ccline_used)
3483 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003485 prev_ccline_used = TRUE;
3486 }
3487 *ccp = prev_ccline;
3488 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003489 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003490}
3491
3492/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003493 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003494 */
3495 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497{
3498 ccline = prev_ccline;
3499 prev_ccline = *ccp;
3500}
3501
Bram Moolenaar8299df92004-07-10 09:47:34 +00003502/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003503 * Paste a yank register into the command line.
3504 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003505 * insert_reg() can't be used here, because special characters from the
3506 * register contents will be interpreted as commands.
3507 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003508 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003509 */
3510 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003511cmdline_paste(
3512 int regname,
3513 int literally, /* Insert text literally instead of "as typed" */
3514 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003515{
3516 long i;
3517 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003518 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003519 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003520
3521 /* check for valid regname; also accept special characters for CTRL-R in
3522 * the command line */
3523 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003524 && regname != Ctrl_A && regname != Ctrl_L
3525 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003526 return FAIL;
3527
3528 /* A register containing CTRL-R can cause an endless loop. Allow using
3529 * CTRL-C to break the loop. */
3530 line_breakcheck();
3531 if (got_int)
3532 return FAIL;
3533
3534#ifdef FEAT_CLIPBOARD
3535 regname = may_get_selection(regname);
3536#endif
3537
Bram Moolenaar438d1762018-09-30 17:11:48 +02003538 // Need to set "textlock" to avoid nasty things like going to another
3539 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003540 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003542 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543
3544 if (i)
3545 {
3546 /* Got the value of a special register in "arg". */
3547 if (arg == NULL)
3548 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003549
3550 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3551 * part of the word. */
3552 p = arg;
3553 if (p_is && regname == Ctrl_W)
3554 {
3555 char_u *w;
3556 int len;
3557
3558 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003559 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003560 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003561 if (has_mbyte)
3562 {
3563 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3564 if (!vim_iswordc(mb_ptr2char(w - len)))
3565 break;
3566 w -= len;
3567 }
3568 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003569 {
3570 if (!vim_iswordc(w[-1]))
3571 break;
3572 --w;
3573 }
3574 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003575 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003576 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3577 p += len;
3578 }
3579
3580 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 if (allocated)
3582 vim_free(arg);
3583 return OK;
3584 }
3585
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003586 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587}
3588
3589/*
3590 * Put a string on the command line.
3591 * When "literally" is TRUE, insert literally.
3592 * When "literally" is FALSE, insert as typed, but don't leave the command
3593 * line.
3594 */
3595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003596cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597{
3598 int c, cv;
3599
3600 if (literally)
3601 put_on_cmdline(s, -1, TRUE);
3602 else
3603 while (*s != NUL)
3604 {
3605 cv = *s;
3606 if (cv == Ctrl_V && s[1])
3607 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003608 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003609 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003611 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003612 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3613 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003614#ifdef UNIX
3615 || c == intr_char
3616#endif
3617 || (c == Ctrl_BSL && *s == Ctrl_N))
3618 stuffcharReadbuff(Ctrl_V);
3619 stuffcharReadbuff(c);
3620 }
3621}
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623#ifdef FEAT_WILDMENU
3624/*
3625 * Delete characters on the command line, from "from" to the current
3626 * position.
3627 */
3628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003629cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3632 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3633 ccline.cmdlen -= ccline.cmdpos - from;
3634 ccline.cmdpos = from;
3635}
3636#endif
3637
3638/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003639 * This function is called when the screen size changes and with incremental
3640 * search and in other situations where the command line may have been
3641 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003644redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003646 redrawcmdline_ex(TRUE);
3647}
3648
3649 void
3650redrawcmdline_ex(int do_compute_cmdrow)
3651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (cmd_silent)
3653 return;
3654 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003655 if (do_compute_cmdrow)
3656 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 redrawcmd();
3658 cursorcmd();
3659}
3660
3661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003662redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 int i;
3665
3666 if (cmd_silent)
3667 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003668 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 msg_putchar(ccline.cmdfirstc);
3670 if (ccline.cmdprompt != NULL)
3671 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003672 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3674 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003675 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 --ccline.cmdindent;
3677 }
3678 else
3679 for (i = ccline.cmdindent; i > 0; --i)
3680 msg_putchar(' ');
3681}
3682
3683/*
3684 * Redraw what is currently on the command line.
3685 */
3686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003687redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 if (cmd_silent)
3690 return;
3691
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003692 /* when 'incsearch' is set there may be no command line while redrawing */
3693 if (ccline.cmdbuff == NULL)
3694 {
3695 windgoto(cmdline_row, 0);
3696 msg_clr_eos();
3697 return;
3698 }
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 msg_start();
3701 redrawcmdprompt();
3702
3703 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3704 msg_no_more = TRUE;
3705 draw_cmdline(0, ccline.cmdlen);
3706 msg_clr_eos();
3707 msg_no_more = FALSE;
3708
3709 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003710 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003711 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713 /*
3714 * An emsg() before may have set msg_scroll. This is used in normal mode,
3715 * in cmdline mode we can reset them now.
3716 */
3717 msg_scroll = FALSE; /* next message overwrites cmdline */
3718
3719 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3720 * in cmdline mode */
3721 skip_redraw = FALSE;
3722}
3723
3724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003725compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003727 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 cmdline_row = Rows - 1;
3729 else
3730 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003731 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732}
3733
Bram Moolenaar66b51422019-08-18 21:44:12 +02003734 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003735cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 if (cmd_silent)
3738 return;
3739
3740#ifdef FEAT_RIGHTLEFT
3741 if (cmdmsg_rl)
3742 {
3743 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3744 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3745 if (msg_row <= 0)
3746 msg_row = Rows - 1;
3747 }
3748 else
3749#endif
3750 {
3751 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3752 msg_col = ccline.cmdspos % (int)Columns;
3753 if (msg_row >= Rows)
3754 msg_row = Rows - 1;
3755 }
3756
3757 windgoto(msg_row, msg_col);
3758#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003759 if (p_imst == IM_ON_THE_SPOT)
3760 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#endif
3762#ifdef MCH_CURSOR_SHAPE
3763 mch_update_cursor();
3764#endif
3765}
3766
3767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003768gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 msg_start();
3771#ifdef FEAT_RIGHTLEFT
3772 if (cmdmsg_rl)
3773 msg_col = Columns - 1;
3774 else
3775#endif
3776 msg_col = 0; /* always start in column 0 */
3777 if (clr) /* clear the bottom line(s) */
3778 msg_clr_eos(); /* will reset clear_cmdline */
3779 windgoto(cmdline_row, 0);
3780}
3781
3782/*
3783 * Check the word in front of the cursor for an abbreviation.
3784 * Called when the non-id character "c" has been entered.
3785 * When an abbreviation is recognized it is removed from the text with
3786 * backspaces and the replacement string is inserted, followed by "c".
3787 */
3788 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003789ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003791 int spos = 0;
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3794 return FALSE;
3795
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003796 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3797 * Actually accepts any mark. */
3798 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3799 spos++;
3800 if (ccline.cmdlen - spos > 5
3801 && ccline.cmdbuff[spos] == '\''
3802 && ccline.cmdbuff[spos + 2] == ','
3803 && ccline.cmdbuff[spos + 3] == '\'')
3804 spos += 5;
3805 else
3806 /* check abbreviation from the beginning of the commandline */
3807 spos = 0;
3808
3809 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810}
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003813 * Escape special characters in "fname" for when used as a file name argument
3814 * after a Vim command, or, when "shell" is non-zero, a shell command.
3815 * Returns the result in allocated memory.
3816 */
3817 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003818vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003819{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003820 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003821#ifdef BACKSLASH_IN_FILENAME
3822 char_u buf[20];
3823 int j = 0;
3824
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003825 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003826 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003827 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828 buf[j++] = *p;
3829 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003830 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003831#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003832 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3833 if (shell && csh_like_shell() && p != NULL)
3834 {
3835 char_u *s;
3836
3837 /* For csh and similar shells need to put two backslashes before '!'.
3838 * One is taken by Vim, one by the shell. */
3839 s = vim_strsave_escaped(p, (char_u *)"!");
3840 vim_free(p);
3841 p = s;
3842 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003843#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003844
3845 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3846 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003847 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003848 escape_fname(&p);
3849
3850 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003851}
3852
3853/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003854 * Put a backslash before the file name in "pp", which is in allocated memory.
3855 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003857escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003858{
3859 char_u *p;
3860
Bram Moolenaar964b3742019-05-24 18:54:09 +02003861 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003862 if (p != NULL)
3863 {
3864 p[0] = '\\';
3865 STRCPY(p + 1, *pp);
3866 vim_free(*pp);
3867 *pp = p;
3868 }
3869}
3870
3871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 * For each file name in files[num_files]:
3873 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3874 */
3875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003876tilde_replace(
3877 char_u *orig_pat,
3878 int num_files,
3879 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
3881 int i;
3882 char_u *p;
3883
3884 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3885 {
3886 for (i = 0; i < num_files; ++i)
3887 {
3888 p = home_replace_save(NULL, files[i]);
3889 if (p != NULL)
3890 {
3891 vim_free(files[i]);
3892 files[i] = p;
3893 }
3894 }
3895 }
3896}
3897
3898/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003899 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003901 cmdline_info_T *
3902get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003904 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905}
3906
Bram Moolenaar064154c2016-03-19 22:50:43 +01003907#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003908/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003909 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003910 * ccline and put the previous value in prev_ccline.
3911 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003912 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003913get_ccline_ptr(void)
3914{
3915 if ((State & CMDLINE) == 0)
3916 return NULL;
3917 if (ccline.cmdbuff != NULL)
3918 return &ccline;
3919 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3920 return &prev_ccline;
3921 return NULL;
3922}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003923#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003924
Bram Moolenaar064154c2016-03-19 22:50:43 +01003925#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003926/*
3927 * Get the current command line in allocated memory.
3928 * Only works when the command line is being edited.
3929 * Returns NULL when something is wrong.
3930 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003931 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003932get_cmdline_str(void)
3933{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003934 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003935
Bram Moolenaaree91c332018-09-25 22:27:35 +02003936 if (cmdline_star > 0)
3937 return NULL;
3938 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003939 if (p == NULL)
3940 return NULL;
3941 return vim_strnsave(p->cmdbuff, p->cmdlen);
3942}
3943
3944/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003945 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003946 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003947 void
3948f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3949{
3950 rettv->v_type = VAR_STRING;
3951 rettv->vval.v_string = get_cmdline_str();
3952}
3953
3954/*
3955 * "getcmdpos()" function
3956 */
3957 void
3958f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003959{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003960 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003961
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003962 rettv->vval.v_number = 0;
3963 if (p != NULL)
3964 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003965}
3966
3967/*
3968 * Set the command line byte position to "pos". Zero is the first position.
3969 * Only works when the command line is being edited.
3970 * Returns 1 when failed, 0 when OK.
3971 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003972 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003973set_cmdline_pos(
3974 int pos)
3975{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003976 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003977
3978 if (p == NULL)
3979 return 1;
3980
3981 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3982 * changed the command line. */
3983 if (pos < 0)
3984 new_cmdpos = 0;
3985 else
3986 new_cmdpos = pos;
3987 return 0;
3988}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003989
3990/*
3991 * "setcmdpos()" function
3992 */
3993 void
3994f_setcmdpos(typval_T *argvars, typval_T *rettv)
3995{
3996 int pos = (int)tv_get_number(&argvars[0]) - 1;
3997
3998 if (pos >= 0)
3999 rettv->vval.v_number = set_cmdline_pos(pos);
4000}
4001
4002/*
4003 * "getcmdtype()" function
4004 */
4005 void
4006f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4007{
4008 rettv->v_type = VAR_STRING;
4009 rettv->vval.v_string = alloc(2);
4010 if (rettv->vval.v_string != NULL)
4011 {
4012 rettv->vval.v_string[0] = get_cmdline_type();
4013 rettv->vval.v_string[1] = NUL;
4014 }
4015}
4016
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004017#endif
4018
4019#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
4020/*
4021 * Get the current command-line type.
4022 * Returns ':' or '/' or '?' or '@' or '>' or '-'
4023 * Only works when the command line is being edited.
4024 * Returns NUL when something is wrong.
4025 */
4026 int
4027get_cmdline_type(void)
4028{
Bram Moolenaar66b51422019-08-18 21:44:12 +02004029 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004030
4031 if (p == NULL)
4032 return NUL;
4033 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01004034 return
4035# ifdef FEAT_EVAL
4036 (p->input_fn) ? '@' :
4037# endif
4038 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01004039 return p->cmdfirstc;
4040}
4041#endif
4042
Bram Moolenaard7663c22019-08-06 21:59:57 +02004043/*
4044 * Return the first character of the current command line.
4045 */
4046 int
4047get_cmdline_firstc(void)
4048{
4049 return ccline.cmdfirstc;
4050}
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052/*
4053 * Get indices "num1,num2" that specify a range within a list (not a range of
4054 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4055 * Returns OK if parsed successfully, otherwise FAIL.
4056 */
4057 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004058get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
4060 int len;
4061 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004062 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 *str = skipwhite(*str);
4065 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4066 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004067 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 *str += len;
4069 *num1 = (int)num;
4070 first = TRUE;
4071 }
4072 *str = skipwhite(*str);
4073 if (**str == ',') /* parse "to" part of range */
4074 {
4075 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004076 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (len > 0)
4078 {
4079 *num2 = (int)num;
4080 *str = skipwhite(*str + len);
4081 }
4082 else if (!first) /* no number given at all */
4083 return FAIL;
4084 }
4085 else if (first) /* only one number given */
4086 *num2 = *num1;
4087 return OK;
4088}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#if defined(FEAT_CMDWIN) || defined(PROTO)
4091/*
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004092 * Check value of 'cedit' and set cedit_key.
4093 * Returns NULL if value is OK, error message otherwise.
4094 */
4095 char *
4096check_cedit(void)
4097{
4098 int n;
4099
4100 if (*p_cedit == NUL)
4101 cedit_key = -1;
4102 else
4103 {
4104 n = string_to_key(p_cedit, FALSE);
4105 if (vim_isprintc(n))
4106 return e_invarg;
4107 cedit_key = n;
4108 }
4109 return NULL;
4110}
4111
4112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * Open a window on the current command line and history. Allow editing in
4114 * the window. Returns when the window is closed.
4115 * Returns:
4116 * CR if the command is to be executed
4117 * Ctrl_C if it is to be abandoned
4118 * K_IGNORE if editing continues
4119 */
4120 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004121open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004123 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004125 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 win_T *wp;
4127 int i;
4128 linenr_T lnum;
4129 int histtype;
4130 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 int save_restart_edit = restart_edit;
4132 int save_State = State;
4133 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004134#ifdef FEAT_RIGHTLEFT
4135 int save_cmdmsg_rl = cmdmsg_rl;
4136#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004137#ifdef FEAT_FOLDING
4138 int save_KeyTyped;
4139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 /* Can't do this recursively. Can't do it when typing a password. */
4142 if (cmdwin_type != 0
4143# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4144 || cmdline_star > 0
4145# endif
4146 )
4147 {
4148 beep_flush();
4149 return K_IGNORE;
4150 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004151 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004153 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 win_size_save(&winsizes);
4155
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004156 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4157 // command line window, but we don't want the popup menu then.
4158 pum_undisplay();
4159
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004160 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004161 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004162 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004163
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004164 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4166 {
4167 beep_flush();
4168 return K_IGNORE;
4169 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004170 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004172 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004173 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004174 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004177#ifdef FEAT_FOLDING
4178 curwin->w_p_fen = FALSE;
4179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004181 curwin->w_p_rl = cmdmsg_rl;
4182 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004184 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004186 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004187 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004189 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004190 need_wait_return = FALSE;
4191
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004192 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4194 {
4195 if (p_wc == TAB)
4196 {
4197 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4198 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4199 }
4200 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4201 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004202 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004204 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4205 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004206 curbuf->b_p_tw = 0;
4207
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004208 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004210 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004212 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (i >= 0)
4214 {
4215 lnum = 0;
4216 do
4217 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004218 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004220 if (get_histentry(histtype)[i].hisstr != NULL)
4221 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 (colnr_T)0, FALSE);
4223 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004224 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226 }
4227
4228 /* Replace the empty last line with the current command-line and put the
4229 * cursor there. */
4230 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4231 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4232 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004233 changed_line_abv_curs();
4234 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004235 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar23324a02019-10-01 17:39:04 +02004237 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 exmode_active = 0;
4239
4240 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar23324a02019-10-01 17:39:04 +02004243 // Reset here so it can be set by a CmdWinEnter autocommand.
4244 cmdwin_result = 0;
4245
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004246 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004247 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004248 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004249 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 i = RedrawingDisabled;
4252 RedrawingDisabled = 0;
4253
4254 /*
4255 * Call the main loop until <CR> or CTRL-C is typed.
4256 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004257 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 RedrawingDisabled = i;
4260
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004261# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004262 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004263# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004264
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004265 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004266 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004267
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004268# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004269 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004270 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271# endif
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 exmode_active = save_exmode;
4275
Bram Moolenaarf9821062008-06-20 16:31:07 +00004276 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004278 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004281 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 else
4284 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004285# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /* autocmds may abort script processing */
4287 if (aborting() && cmdwin_result != K_IGNORE)
4288 cmdwin_result = Ctrl_C;
4289# endif
4290 /* Set the new command line from the cmdline buffer. */
4291 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004292 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004294 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4295
4296 if (histtype == HIST_CMD)
4297 {
4298 /* Execute the command directly. */
4299 ccline.cmdbuff = vim_strsave((char_u *)p);
4300 cmdwin_result = CAR;
4301 }
4302 else
4303 {
4304 /* First need to cancel what we were doing. */
4305 ccline.cmdbuff = NULL;
4306 stuffcharReadbuff(':');
4307 stuffReadbuff((char_u *)p);
4308 stuffcharReadbuff(CAR);
4309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else if (cmdwin_result == K_XF2) /* :qa typed */
4312 {
4313 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4314 cmdwin_result = CAR;
4315 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004316 else if (cmdwin_result == Ctrl_C)
4317 {
4318 /* :q or :close, don't execute any command
4319 * and don't modify the cmd window. */
4320 ccline.cmdbuff = NULL;
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 else
4323 ccline.cmdbuff = vim_strsave(ml_get_curline());
4324 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004325 {
4326 ccline.cmdbuff = vim_strsave((char_u *)"");
4327 ccline.cmdlen = 0;
4328 ccline.cmdbufflen = 1;
4329 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 else
4333 {
4334 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4335 ccline.cmdbufflen = ccline.cmdlen + 1;
4336 ccline.cmdpos = curwin->w_cursor.col;
4337 if (ccline.cmdpos > ccline.cmdlen)
4338 ccline.cmdpos = ccline.cmdlen;
4339 if (cmdwin_result == K_IGNORE)
4340 {
4341 set_cmdspos_cursor();
4342 redrawcmd();
4343 }
4344 }
4345
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004346# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004347 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004348 curwin->w_p_cole = 0;
4349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004351 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 win_goto(old_curwin);
4353 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004354
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004355 // win_close() may have already wiped the buffer when 'bh' is
4356 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004357 if (bufref_valid(&bufref))
4358 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004360 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363
4364 ga_clear(&winsizes);
4365 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004366# ifdef FEAT_RIGHTLEFT
4367 cmdmsg_rl = save_cmdmsg_rl;
4368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 return cmdwin_result;
4374}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004375#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377/*
4378 * Used for commands that either take a simple command string argument, or:
4379 * cmd << endmarker
4380 * {script}
4381 * endmarker
4382 * Returns a pointer to allocated memory with {script} or NULL.
4383 */
4384 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004385script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
4387 char_u *theline;
4388 char *end_pattern = NULL;
4389 char dot[] = ".";
4390 garray_T ga;
4391
4392 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4393 return NULL;
4394
4395 ga_init2(&ga, 1, 0x400);
4396
4397 if (cmd[2] != NUL)
4398 end_pattern = (char *)skipwhite(cmd + 2);
4399 else
4400 end_pattern = dot;
4401
4402 for (;;)
4403 {
4404 theline = eap->getline(
4405#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004406 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004408 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004411 {
4412 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 ga_concat(&ga, theline);
4417 ga_append(&ga, '\n');
4418 vim_free(theline);
4419 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004420 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 return (char_u *)ga.ga_data;
4423}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004424
4425#if defined(FEAT_EVAL) || defined(PROTO)
4426/*
4427 * This function is used by f_input() and f_inputdialog() functions. The third
4428 * argument to f_input() specifies the type of completion to use at the
4429 * prompt. The third argument to f_inputdialog() specifies the value to return
4430 * when the user cancels the prompt.
4431 */
4432 void
4433get_user_input(
4434 typval_T *argvars,
4435 typval_T *rettv,
4436 int inputdialog,
4437 int secret)
4438{
4439 char_u *prompt = tv_get_string_chk(&argvars[0]);
4440 char_u *p = NULL;
4441 int c;
4442 char_u buf[NUMBUFLEN];
4443 int cmd_silent_save = cmd_silent;
4444 char_u *defstr = (char_u *)"";
4445 int xp_type = EXPAND_NOTHING;
4446 char_u *xp_arg = NULL;
4447
4448 rettv->v_type = VAR_STRING;
4449 rettv->vval.v_string = NULL;
4450
4451#ifdef NO_CONSOLE_INPUT
4452 // While starting up, there is no place to enter text. When running tests
4453 // with --not-a-term we assume feedkeys() will be used.
4454 if (no_console_input() && !is_not_a_term())
4455 return;
4456#endif
4457
4458 cmd_silent = FALSE; // Want to see the prompt.
4459 if (prompt != NULL)
4460 {
4461 // Only the part of the message after the last NL is considered as
4462 // prompt for the command line
4463 p = vim_strrchr(prompt, '\n');
4464 if (p == NULL)
4465 p = prompt;
4466 else
4467 {
4468 ++p;
4469 c = *p;
4470 *p = NUL;
4471 msg_start();
4472 msg_clr_eos();
4473 msg_puts_attr((char *)prompt, get_echo_attr());
4474 msg_didout = FALSE;
4475 msg_starthere();
4476 *p = c;
4477 }
4478 cmdline_row = msg_row;
4479
4480 if (argvars[1].v_type != VAR_UNKNOWN)
4481 {
4482 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4483 if (defstr != NULL)
4484 stuffReadbuffSpec(defstr);
4485
4486 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4487 {
4488 char_u *xp_name;
4489 int xp_namelen;
4490 long argt;
4491
4492 // input() with a third argument: completion
4493 rettv->vval.v_string = NULL;
4494
4495 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4496 if (xp_name == NULL)
4497 return;
4498
4499 xp_namelen = (int)STRLEN(xp_name);
4500
4501 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4502 &xp_arg) == FAIL)
4503 return;
4504 }
4505 }
4506
4507 if (defstr != NULL)
4508 {
4509 int save_ex_normal_busy = ex_normal_busy;
4510
4511 ex_normal_busy = 0;
4512 rettv->vval.v_string =
4513 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4514 xp_type, xp_arg);
4515 ex_normal_busy = save_ex_normal_busy;
4516 }
4517 if (inputdialog && rettv->vval.v_string == NULL
4518 && argvars[1].v_type != VAR_UNKNOWN
4519 && argvars[2].v_type != VAR_UNKNOWN)
4520 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4521 &argvars[2], buf));
4522
4523 vim_free(xp_arg);
4524
4525 // since the user typed this, no need to wait for return
4526 need_wait_return = FALSE;
4527 msg_didout = FALSE;
4528 }
4529 cmd_silent = cmd_silent_save;
4530}
4531#endif