blob: 5cc1c3489752e4bd0283ef51bf6e5d43f9346905 [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 Moolenaar071d4272004-06-13 20:20:40 +000055#endif
56
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020057
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020058 static void
59trigger_cmd_autocmd(int typechar, int evt)
60{
61 char_u typestr[2];
62
63 typestr[0] = typechar;
64 typestr[1] = NUL;
65 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
66}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +020067
Bram Moolenaar071d4272004-06-13 20:20:40 +000068/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020069 * Abandon the command line.
70 */
71 static void
72abandon_cmdline(void)
73{
Bram Moolenaard23a8232018-02-10 18:45:26 +010074 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020075 if (msg_scrolled == 0)
76 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +010077 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020078 redraw_cmdline = TRUE;
79}
80
Bram Moolenaaree219b02017-12-17 14:55:01 +010081#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +020082/*
Bram Moolenaar66216052017-12-16 16:33:44 +010083 * Guess that the pattern matches everything. Only finds specific cases, such
84 * as a trailing \|, which can happen while typing a pattern.
85 */
86 static int
87empty_pattern(char_u *p)
88{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +010089 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +010090
91 /* remove trailing \v and the like */
92 while (n >= 2 && p[n - 2] == '\\'
93 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
94 n -= 2;
95 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
96}
97
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +020098// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +020099typedef struct {
100 colnr_T vs_curswant;
101 colnr_T vs_leftcol;
102 linenr_T vs_topline;
103# ifdef FEAT_DIFF
104 int vs_topfill;
105# endif
106 linenr_T vs_botline;
107 linenr_T vs_empty_rows;
108} viewstate_T;
109
110 static void
111save_viewstate(viewstate_T *vs)
112{
113 vs->vs_curswant = curwin->w_curswant;
114 vs->vs_leftcol = curwin->w_leftcol;
115 vs->vs_topline = curwin->w_topline;
116# ifdef FEAT_DIFF
117 vs->vs_topfill = curwin->w_topfill;
118# endif
119 vs->vs_botline = curwin->w_botline;
120 vs->vs_empty_rows = curwin->w_empty_rows;
121}
122
123 static void
124restore_viewstate(viewstate_T *vs)
125{
126 curwin->w_curswant = vs->vs_curswant;
127 curwin->w_leftcol = vs->vs_leftcol;
128 curwin->w_topline = vs->vs_topline;
129# ifdef FEAT_DIFF
130 curwin->w_topfill = vs->vs_topfill;
131# endif
132 curwin->w_botline = vs->vs_botline;
133 curwin->w_empty_rows = vs->vs_empty_rows;
134}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200135
136// Struct to store the state of 'incsearch' highlighting.
137typedef struct {
138 pos_T search_start; // where 'incsearch' starts searching
Bram Moolenaar23324a02019-10-01 17:39:04 +0200139 pos_T save_cursor;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200140 viewstate_T init_viewstate;
141 viewstate_T old_viewstate;
Bram Moolenaar23324a02019-10-01 17:39:04 +0200142 pos_T match_start;
143 pos_T match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200144 int did_incsearch;
145 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200146 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200147} incsearch_state_T;
148
149 static void
150init_incsearch_state(incsearch_state_T *is_state)
151{
152 is_state->match_start = curwin->w_cursor;
153 is_state->did_incsearch = FALSE;
154 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200155 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200156 CLEAR_POS(&is_state->match_end);
157 is_state->save_cursor = curwin->w_cursor; // may be restored later
158 is_state->search_start = curwin->w_cursor;
159 save_viewstate(&is_state->init_viewstate);
160 save_viewstate(&is_state->old_viewstate);
161}
162
163/*
164 * First move cursor to end of match, then to the start. This
165 * moves the whole match onto the screen when 'nowrap' is set.
166 */
167 static void
168set_search_match(pos_T *t)
169{
170 t->lnum += search_match_lines;
171 t->col = search_match_endcol;
172 if (t->lnum > curbuf->b_ml.ml_line_count)
173 {
174 t->lnum = curbuf->b_ml.ml_line_count;
175 coladvance((colnr_T)MAXCOL);
176 }
177}
178
179/*
180 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200181 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200182 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200183 */
184 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200185do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
186 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200187{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200188 char_u *cmd;
189 cmdmod_T save_cmdmod = cmdmod;
190 char_u *p;
191 int delim_optional = FALSE;
192 int delim;
193 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100194 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200195 exarg_T ea;
196 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200197 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200198
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200199 *skiplen = 0;
200 *patlen = ccline.cmdlen;
201
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200202 if (!p_is || cmd_silent)
203 return FALSE;
204
205 // by default search all lines
206 search_first_line = 0;
207 search_last_line = MAXLNUM;
208
209 if (firstc == '/' || firstc == '?')
210 return TRUE;
211 if (firstc != ':')
212 return FALSE;
213
214 vim_memset(&ea, 0, sizeof(ea));
215 ea.line1 = 1;
216 ea.line2 = 1;
217 ea.cmd = ccline.cmdbuff;
218 ea.addr_type = ADDR_LINES;
219
220 parse_command_modifiers(&ea, &dummy, TRUE);
221 cmdmod = save_cmdmod;
222
223 cmd = skip_range(ea.cmd, NULL);
224 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
225 return FALSE;
226
227 // Skip over "substitute" to find the pattern separator.
228 for (p = cmd; ASCII_ISALPHA(*p); ++p)
229 ;
230 if (*skipwhite(p) == NUL)
231 return FALSE;
232
233 if (STRNCMP(cmd, "substitute", p - cmd) == 0
234 || STRNCMP(cmd, "smagic", p - cmd) == 0
235 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
236 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200237 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200238 if (*cmd == 's' && cmd[1] == 'm')
239 p_magic = TRUE;
240 else if (*cmd == 's' && cmd[1] == 'n')
241 p_magic = FALSE;
242 }
243 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
244 {
245 // skip over flags
246 while (ASCII_ISALPHA(*(p = skipwhite(p))))
247 ++p;
248 if (*p == NUL)
249 return FALSE;
250 }
251 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
252 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
253 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
254 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
255 || STRNCMP(cmd, "global", p - cmd) == 0)
256 {
257 // skip over "!"
258 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200259 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200260 p++;
261 if (*skipwhite(p) == NUL)
262 return FALSE;
263 }
264 if (*cmd != 'g')
265 delim_optional = TRUE;
266 }
267 else
268 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200269
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200270 p = skipwhite(p);
271 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
272 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200273
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200274 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200275
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200276 if (end == p && !use_last_pat)
277 return FALSE;
278
279 // Don't do 'hlsearch' highlighting if the pattern matches everything.
280 if (!use_last_pat)
281 {
282 char c = *end;
283 int empty;
284
285 *end = NUL;
286 empty = empty_pattern(p);
287 *end = c;
288 if (empty)
289 return FALSE;
290 }
291
292 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200293 *skiplen = (int)(p - ccline.cmdbuff);
294 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200295
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200296 // parse the address range
297 save_cursor = curwin->w_cursor;
298 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200299 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200300 if (ea.addr_count > 0)
301 {
302 // Allow for reverse match.
303 if (ea.line2 < ea.line1)
304 {
305 search_first_line = ea.line2;
306 search_last_line = ea.line1;
307 }
308 else
309 {
310 search_first_line = ea.line1;
311 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200312 }
313 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200314 else if (cmd[0] == 's' && cmd[1] != 'o')
315 {
316 // :s defaults to the current line
317 search_first_line = curwin->w_cursor.lnum;
318 search_last_line = curwin->w_cursor.lnum;
319 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200320
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200321 curwin->w_cursor = save_cursor;
322 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200323}
324
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200325 static void
326finish_incsearch_highlighting(
327 int gotesc,
328 incsearch_state_T *is_state,
329 int call_update_screen)
330{
331 if (is_state->did_incsearch)
332 {
333 is_state->did_incsearch = FALSE;
334 if (gotesc)
335 curwin->w_cursor = is_state->save_cursor;
336 else
337 {
338 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
339 {
340 // put the '" mark at the original position
341 curwin->w_cursor = is_state->save_cursor;
342 setpcmark();
343 }
344 curwin->w_cursor = is_state->search_start;
345 }
346 restore_viewstate(&is_state->old_viewstate);
347 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200348
349 // by default search all lines
350 search_first_line = 0;
351 search_last_line = MAXLNUM;
352
353 p_magic = is_state->magic_save;
354
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200355 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200356 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200357 if (call_update_screen)
358 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200359 }
360}
361
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200362/*
363 * Do 'incsearch' highlighting if desired.
364 */
365 static void
366may_do_incsearch_highlighting(
367 int firstc,
368 long count,
369 incsearch_state_T *is_state)
370{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200371 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200372 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200373 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200374#ifdef FEAT_RELTIME
375 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200376 searchit_arg_T sia;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200377#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200378 int next_char;
379 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200380 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200381
Bram Moolenaar198cb662018-09-06 21:44:17 +0200382 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100383 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200384 save_last_search_pattern();
385
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200386 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200387 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200388 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200389 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200390 if (did_do_incsearch && vpeekc() == NUL)
391 // may have skipped a redraw, do it now
392 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200394 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200395
396 // If there is a character waiting, search and redraw later.
397 if (char_avail())
398 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200399 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200400 is_state->incsearch_postponed = TRUE;
401 return;
402 }
403 is_state->incsearch_postponed = FALSE;
404
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200405 if (search_first_line == 0)
406 // start at the original cursor position
407 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100408 else if (search_first_line > curbuf->b_ml.ml_line_count)
409 {
410 // start after the last line
411 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
412 curwin->w_cursor.col = MAXCOL;
413 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200414 else
415 {
416 // start at the first line in the range
417 curwin->w_cursor.lnum = search_first_line;
418 curwin->w_cursor.col = 0;
419 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200420
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200421 // Use the previous pattern for ":s//".
422 next_char = ccline.cmdbuff[skiplen + patlen];
423 use_last_pat = patlen == 0 && skiplen > 0
424 && ccline.cmdbuff[skiplen - 1] == next_char;
425
426 // If there is no pattern, don't do anything.
427 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200428 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200429 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200430 set_no_hlsearch(TRUE); // turn off previous highlight
431 redraw_all_later(SOME_VALID);
432 }
433 else
434 {
435 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
436
437 cursor_off(); // so the user knows we're busy
438 out_flush();
439 ++emsg_off; // so it doesn't beep if bad expr
440#ifdef FEAT_RELTIME
441 // Set the time limit to half a second.
442 profile_setlimit(500L, &tm);
443#endif
444 if (!p_hls)
445 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200446 if (search_first_line != 0)
447 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200448 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200449#ifdef FEAT_RELTIME
450 vim_memset(&sia, 0, sizeof(sia));
451 sia.sa_tm = &tm;
452#endif
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200453 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200454 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200455#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200456 &sia
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457#else
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200458 NULL
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459#endif
460 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200461 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462 --emsg_off;
463
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200464 if (curwin->w_cursor.lnum < search_first_line
465 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200466 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200468 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200469 curwin->w_cursor = is_state->search_start;
470 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200471
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472 // if interrupted while searching, behave like it failed
473 if (got_int)
474 {
475 (void)vpeekc(); // remove <C-C> from input stream
476 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200477 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200478 }
479 else if (char_avail())
480 // cancelled searching because a char was typed
481 is_state->incsearch_postponed = TRUE;
482 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200483 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200484 highlight_match = TRUE; // highlight position
485 else
486 highlight_match = FALSE; // remove highlight
487
488 // First restore the old curwin values, so the screen is positioned in the
489 // same way as the actual search command.
490 restore_viewstate(&is_state->old_viewstate);
491 changed_cline_bef_curs();
492 update_topline();
493
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200494 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200495 {
496 pos_T save_pos = curwin->w_cursor;
497
498 is_state->match_start = curwin->w_cursor;
499 set_search_match(&curwin->w_cursor);
500 validate_cursor();
501 end_pos = curwin->w_cursor;
502 is_state->match_end = end_pos;
503 curwin->w_cursor = save_pos;
504 }
505 else
506 end_pos = curwin->w_cursor; // shutup gcc 4
507
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200508 // Disable 'hlsearch' highlighting if the pattern matches everything.
509 // Avoids a flash when typing "foo\|".
510 if (!use_last_pat)
511 {
512 next_char = ccline.cmdbuff[skiplen + patlen];
513 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200514 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
515 {
516 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200517 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200518 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200519 ccline.cmdbuff[skiplen + patlen] = next_char;
520 }
521
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200522 validate_cursor();
523 // May redraw the status line to show the cursor position.
524 if (p_ru && curwin->w_status_height > 0)
525 curwin->w_redr_status = TRUE;
526
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200527 update_screen(SOME_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200528 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 restore_last_search_pattern();
530
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200531 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
532 // end of the pattern, e.g. for ":s/pat/".
533 if (ccline.cmdbuff[skiplen + patlen] != NUL)
534 curwin->w_cursor = is_state->search_start;
535 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 curwin->w_cursor = end_pos;
537
538 msg_starthere();
539 redrawcmdline();
540 is_state->did_incsearch = TRUE;
541}
542
543/*
544 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
545 * or previous match.
546 * Returns FAIL when jumping to cmdline_not_changed;
547 */
548 static int
549may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200550 int firstc,
551 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200552 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200553 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200554{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200555 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200556 pos_T t;
557 char_u *pat;
558 int search_flags = SEARCH_NOOF;
559 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561
Bram Moolenaar198cb662018-09-06 21:44:17 +0200562 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100563 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200564 save_last_search_pattern();
565
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200566 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200567 {
568 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200569 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200570 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200571 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200572 {
573 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200574 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200575 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200576
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200578 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200579 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200580 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200581 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200582 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200583 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200584 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200585
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200586 cursor_off();
587 out_flush();
588 if (c == Ctrl_G)
589 {
590 t = is_state->match_end;
591 if (LT_POS(is_state->match_start, is_state->match_end))
592 // Start searching at the end of the match not at the beginning of
593 // the next column.
594 (void)decl(&t);
595 search_flags += SEARCH_COL;
596 }
597 else
598 t = is_state->match_start;
599 if (!p_hls)
600 search_flags += SEARCH_KEEP;
601 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200602 save = pat[patlen];
603 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100604 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200605 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200606 pat, count, search_flags, RE_SEARCH, NULL);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200608 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 if (i)
610 {
611 is_state->search_start = is_state->match_start;
612 is_state->match_end = t;
613 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200614 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200615 {
616 // Move just before the current match, so that when nv_search
617 // finishes the cursor will be put back on the match.
618 is_state->search_start = t;
619 (void)decl(&is_state->search_start);
620 }
621 else if (c == Ctrl_G && firstc == '?')
622 {
623 // Move just after 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)incl(&is_state->search_start);
627 }
628 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
629 {
630 // wrap around
631 is_state->search_start = t;
632 if (firstc == '?')
633 (void)incl(&is_state->search_start);
634 else
635 (void)decl(&is_state->search_start);
636 }
637
638 set_search_match(&is_state->match_end);
639 curwin->w_cursor = is_state->match_start;
640 changed_cline_bef_curs();
641 update_topline();
642 validate_cursor();
643 highlight_match = TRUE;
644 save_viewstate(&is_state->old_viewstate);
645 update_screen(NOT_VALID);
Bram Moolenaar7ab5d772019-10-26 20:45:24 +0200646 highlight_match = FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200648 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200649 }
650 else
651 vim_beep(BO_ERROR);
652 restore_last_search_pattern();
653 return FAIL;
654}
655
656/*
657 * When CTRL-L typed: add character from the match to the pattern.
658 * May set "*c" to the added character.
659 * Return OK when jumping to cmdline_not_changed.
660 */
661 static int
662may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
663{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200664 int skiplen, patlen;
665
Bram Moolenaar198cb662018-09-06 21:44:17 +0200666 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100667 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200668 save_last_search_pattern();
669
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200670 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200671 {
672 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200673 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200674 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100675 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200676
677 // Add a character from under the cursor for 'incsearch'.
678 if (is_state->did_incsearch)
679 {
680 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200681 *c = gchar_cursor();
682 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200683 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 // If 'ignorecase' and 'smartcase' are set and the
685 // command line has no uppercase characters, convert
686 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200687 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200688 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200689 if (*c == firstc || vim_strchr((char_u *)(
690 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200692 // put a backslash before special characters
693 stuffcharReadbuff(*c);
694 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200695 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200696 // add any composing characters
697 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
698 {
699 int save_c = *c;
700
701 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
702 {
703 curwin->w_cursor.col += mb_char2len(*c);
704 *c = gchar_cursor();
705 stuffcharReadbuff(*c);
706 }
707 *c = save_c;
708 }
709 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200710 }
711 }
712 return OK;
713}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200714#endif
715
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200716#ifdef FEAT_ARABIC
717/*
718 * Return TRUE if the command line has an Arabic character at or after "start"
719 * for "len" bytes.
720 */
721 static int
722cmdline_has_arabic(int start, int len)
723{
724 int j;
725 int mb_l;
726 int u8c;
727 char_u *p;
728 int u8cc[MAX_MCO];
729
730 if (!enc_utf8)
731 return FALSE;
732
733 for (j = start; j < start + len; j += mb_l)
734 {
735 p = ccline.cmdbuff + j;
736 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
737 mb_l = utfc_ptr2len_len(p, start + len - j);
738 if (ARABIC_CHAR(u8c))
739 return TRUE;
740 }
741 return FALSE;
742}
743#endif
744
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200745 void
746cmdline_init(void)
747{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200748 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200749}
750
Bram Moolenaar66216052017-12-16 16:33:44 +0100751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * getcmdline() - accept a command line starting with firstc.
753 *
754 * firstc == ':' get ":" command line.
755 * firstc == '/' or '?' get search pattern
756 * firstc == '=' get expression
757 * firstc == '@' get text for input() function
758 * firstc == '>' get text for debug mode
759 * firstc == NUL get text for :insert command
760 * firstc == -1 like NUL, and break on CTRL-C
761 *
762 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
763 * command line.
764 *
765 * Careful: getcmdline() can be called recursively!
766 *
767 * Return pointer to allocated string if there is a commandline, NULL
768 * otherwise.
769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100771getcmdline(
772 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200773 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200774 int indent, // indent for inside conditionals
775 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200776{
777 return getcmdline_int(firstc, count, indent, TRUE);
778}
779
780 static char_u *
781getcmdline_int(
782 int firstc,
783 long count UNUSED, // only used for incremental search
784 int indent, // indent for inside conditionals
785 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
787 int c;
788 int i;
789 int j;
790 int gotesc = FALSE; /* TRUE when <ESC> just typed */
791 int do_abbr; /* when TRUE check for abbr. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 char_u *lookfor = NULL; /* string to match */
793 int hiscnt; /* current history line in use */
794 int histype; /* history type to be used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200796 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797#endif
798 int did_wild_list = FALSE; /* did wild_list() recently */
799 int wim_index = 0; /* index in wim_flags[] */
800 int res;
801 int save_msg_scroll = msg_scroll;
802 int save_State = State; /* remember State when called */
803 int some_key_typed = FALSE; /* one of the keys was typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 /* mouse drag and release events are ignored, unless they are
805 * preceded with a mouse down event */
806 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#ifdef FEAT_EVAL
808 int break_ctrl_c = FALSE;
809#endif
810 expand_T xpc;
811 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200812 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200813 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200814 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar438d1762018-09-30 17:11:48 +0200816 if (ccline.cmdbuff != NULL)
817 {
818 // Being called recursively. Since ccline is global, we need to save
819 // the current buffer and restore it when returning.
820 save_cmdline(&save_ccline);
821 did_save_ccline = TRUE;
822 }
823 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200824 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#ifdef FEAT_EVAL
827 if (firstc == -1)
828 {
829 firstc = NUL;
830 break_ctrl_c = TRUE;
831 }
832#endif
833#ifdef FEAT_RIGHTLEFT
834 /* start without Hebrew mapping for a command line */
835 if (firstc == ':' || firstc == '=' || firstc == '>')
836 cmd_hkmap = 0;
837#endif
838
839 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200842 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#endif
844
845 /*
846 * set some variables for redrawcmd()
847 */
848 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000849 ccline.cmdindent = (firstc > 0 ? indent : 0);
850
851 /* alloc initial ccline.cmdbuff */
852 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200854 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 ccline.cmdlen = ccline.cmdpos = 0;
856 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100857 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000859 /* autoindent for :insert and :append */
860 if (firstc <= 0)
861 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200862 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000863 ccline.cmdbuff[indent] = NUL;
864 ccline.cmdpos = indent;
865 ccline.cmdspos = indent;
866 ccline.cmdlen = indent;
867 }
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000870 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872#ifdef FEAT_RIGHTLEFT
873 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
874 && (firstc == '/' || firstc == '?'))
875 cmdmsg_rl = TRUE;
876 else
877 cmdmsg_rl = FALSE;
878#endif
879
880 redir_off = TRUE; /* don't redirect the typed command */
881 if (!cmd_silent)
882 {
883 i = msg_scrolled;
884 msg_scrolled = 0; /* avoid wait_return message */
885 gotocmdline(TRUE);
886 msg_scrolled += i;
887 redrawcmdprompt(); /* draw prompt or indent */
888 set_cmdspos();
889 }
890 xpc.xp_context = EXPAND_NOTHING;
891 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000892#ifndef BACKSLASH_IN_FILENAME
893 xpc.xp_shell = FALSE;
894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000896#if defined(FEAT_EVAL)
897 if (ccline.input_fn)
898 {
899 xpc.xp_context = ccline.xp_context;
900 xpc.xp_pattern = ccline.cmdbuff;
901 xpc.xp_arg = ccline.xp_arg;
902 }
903#endif
904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 /*
906 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
907 * doing ":@0" when register 0 doesn't contain a CR.
908 */
909 msg_scroll = FALSE;
910
911 State = CMDLINE;
912
913 if (firstc == '/' || firstc == '?' || firstc == '@')
914 {
915 /* Use ":lmap" mappings for search pattern and input(). */
916 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
917 b_im_ptr = &curbuf->b_p_iminsert;
918 else
919 b_im_ptr = &curbuf->b_p_imsearch;
920 if (*b_im_ptr == B_IMODE_LMAP)
921 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100922#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 im_set_active(*b_im_ptr == B_IMODE_IM);
924#endif
925 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100926#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 else if (p_imcmdline)
928 im_set_active(TRUE);
929#endif
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef CURSOR_SHAPE
933 ui_cursor_shape(); /* may show different cursor shape */
934#endif
935
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000936 /* When inside an autocommand for writing "exiting" may be set and
937 * terminal mode set to cooked. Need to set raw mode here then. */
938 settmode(TMODE_RAW);
939
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200940 /* Trigger CmdlineEnter autocommands. */
941 cmdline_type = firstc == NUL ? '-' : firstc;
942 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200945 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
948#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000949 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200952 /* If something above caused an error, reset the flags, we do want to type
953 * and execute commands. Display may be messed up a bit. */
954 if (did_emsg)
955 redrawcmd();
956 did_emsg = FALSE;
957 got_int = FALSE;
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /*
960 * Collect the command string, handling editing keys.
961 */
962 for (;;)
963 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000964 redir_off = TRUE; /* Don't redirect the typed command.
965 Repeated, because a ":redir" inside
966 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef USE_ON_FLY_SCROLL
968 dont_scroll = FALSE; /* allow scrolling here */
969#endif
970 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
971
Bram Moolenaar72532d32018-04-07 19:09:09 +0200972 did_emsg = FALSE; /* There can't really be a reason why an error
973 that occurs while typing a command should
974 cause the command not to be executed. */
975
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200976 // Trigger SafeState if nothing is pending.
977 may_trigger_safestate(xpc.xp_numfiles <= 0);
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000980
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100981 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
982 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000983 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000984 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100985 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (KeyTyped)
988 {
989 some_key_typed = TRUE;
990#ifdef FEAT_RIGHTLEFT
991 if (cmd_hkmap)
992 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (cmdmsg_rl && !KeyStuffed)
994 {
995 /* Invert horizontal movements and operations. Only when
996 * typed by the user directly, not when the result of a
997 * mapping. */
998 switch (c)
999 {
1000 case K_RIGHT: c = K_LEFT; break;
1001 case K_S_RIGHT: c = K_S_LEFT; break;
1002 case K_C_RIGHT: c = K_C_LEFT; break;
1003 case K_LEFT: c = K_RIGHT; break;
1004 case K_S_LEFT: c = K_S_RIGHT; break;
1005 case K_C_LEFT: c = K_C_RIGHT; break;
1006 }
1007 }
1008#endif
1009 }
1010
1011 /*
1012 * Ignore got_int when CTRL-C was typed here.
1013 * Don't ignore it in :global, we really need to break then, e.g., for
1014 * ":g/pat/normal /pat" (without the <CR>).
1015 * Don't ignore it for the input() function.
1016 */
1017 if ((c == Ctrl_C
1018#ifdef UNIX
1019 || c == intr_char
1020#endif
1021 )
1022#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1023 && firstc != '@'
1024#endif
1025#ifdef FEAT_EVAL
1026 && !break_ctrl_c
1027#endif
1028 && !global_busy)
1029 got_int = FALSE;
1030
Bram Moolenaard7663c22019-08-06 21:59:57 +02001031 // free old command line when finished moving around in the history
1032 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001034 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001035 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && c != K_PAGEDOWN && c != K_PAGEUP
1037 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001038 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001040 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001043 * When there are matching completions to select <S-Tab> works like
1044 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001046 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 c = Ctrl_P;
1048
1049#ifdef FEAT_WILDMENU
1050 /* Special translations for 'wildmenu' */
1051 if (did_wild_list && p_wmnu)
1052 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001053 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001055 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 c = Ctrl_N;
1057 }
1058 /* Hitting CR after "emenu Name.": complete submenu */
1059 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1060 && ccline.cmdpos > 1
1061 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1062 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1063 && (c == '\n' || c == '\r' || c == K_KENTER))
1064 c = K_DOWN;
1065#endif
1066
1067 /* free expanded names when finished walking through matches */
1068 if (xpc.xp_numfiles != -1
1069 && !(c == p_wc && KeyTyped) && c != p_wcm
1070 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1071 && c != Ctrl_L)
1072 {
1073 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1074 did_wild_list = FALSE;
1075#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001076 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
1078 xpc.xp_context = EXPAND_NOTHING;
1079 wim_index = 0;
1080#ifdef FEAT_WILDMENU
1081 if (p_wmnu && wild_menu_showing != 0)
1082 {
1083 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001084 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001085
1086 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001087 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 if (wild_menu_showing == WM_SCROLLED)
1090 {
1091 /* Entered command line, move it up */
1092 cmdline_row--;
1093 redrawcmd();
1094 }
1095 else if (save_p_ls != -1)
1096 {
1097 /* restore 'laststatus' and 'winminheight' */
1098 p_ls = save_p_ls;
1099 p_wmh = save_p_wmh;
1100 last_status(FALSE);
1101 update_screen(VALID); /* redraw the screen NOW */
1102 redrawcmd();
1103 save_p_ls = -1;
1104 }
1105 else
1106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 redraw_statuslines();
1109 }
1110 KeyTyped = skt;
1111 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001112 if (ccline.input_fn)
1113 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115#endif
1116 }
1117
1118#ifdef FEAT_WILDMENU
1119 /* Special translations for 'wildmenu' */
1120 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1121 {
1122 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001123 if (c == K_DOWN && ccline.cmdpos > 0
1124 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001126 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
1128 /* Hitting <Up>: Remove one submenu name in front of the
1129 * cursor */
1130 int found = FALSE;
1131
1132 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1133 i = 0;
1134 while (--j > 0)
1135 {
1136 /* check for start of menu name */
1137 if (ccline.cmdbuff[j] == ' '
1138 && ccline.cmdbuff[j - 1] != '\\')
1139 {
1140 i = j + 1;
1141 break;
1142 }
1143 /* check for start of submenu name */
1144 if (ccline.cmdbuff[j] == '.'
1145 && ccline.cmdbuff[j - 1] != '\\')
1146 {
1147 if (found)
1148 {
1149 i = j + 1;
1150 break;
1151 }
1152 else
1153 found = TRUE;
1154 }
1155 }
1156 if (i > 0)
1157 cmdline_del(i);
1158 c = p_wc;
1159 xpc.xp_context = EXPAND_NOTHING;
1160 }
1161 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001162 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001163 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001164 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 char_u upseg[5];
1167
1168 upseg[0] = PATHSEP;
1169 upseg[1] = '.';
1170 upseg[2] = '.';
1171 upseg[3] = PATHSEP;
1172 upseg[4] = NUL;
1173
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001174 if (c == K_DOWN
1175 && ccline.cmdpos > 0
1176 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1177 && (ccline.cmdpos < 3
1178 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1180 {
1181 /* go down a directory */
1182 c = p_wc;
1183 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001184 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 {
1186 /* If in a direct ancestor, strip off one ../ to go down */
1187 int found = FALSE;
1188
1189 j = ccline.cmdpos;
1190 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1191 while (--j > i)
1192 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001193 if (has_mbyte)
1194 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (vim_ispathsep(ccline.cmdbuff[j]))
1196 {
1197 found = TRUE;
1198 break;
1199 }
1200 }
1201 if (found
1202 && ccline.cmdbuff[j - 1] == '.'
1203 && ccline.cmdbuff[j - 2] == '.'
1204 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1205 {
1206 cmdline_del(j - 2);
1207 c = p_wc;
1208 }
1209 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001210 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 /* go up a directory */
1213 int found = FALSE;
1214
1215 j = ccline.cmdpos - 1;
1216 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1217 while (--j > i)
1218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if (has_mbyte)
1220 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (vim_ispathsep(ccline.cmdbuff[j])
1222#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001223 && vim_strchr((char_u *)" *?[{`$%#",
1224 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#endif
1226 )
1227 {
1228 if (found)
1229 {
1230 i = j + 1;
1231 break;
1232 }
1233 else
1234 found = TRUE;
1235 }
1236 }
1237
1238 if (!found)
1239 j = i;
1240 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1241 j += 4;
1242 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1243 && j == i)
1244 j += 3;
1245 else
1246 j = 0;
1247 if (j > 0)
1248 {
1249 /* TODO this is only for DOS/UNIX systems - need to put in
1250 * machine-specific stuff here and in upseg init */
1251 cmdline_del(j);
1252 put_on_cmdline(upseg + 1, 3, FALSE);
1253 }
1254 else if (ccline.cmdpos > i)
1255 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001256
1257 /* Now complete in the new directory. Set KeyTyped in case the
1258 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001260 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264#endif /* FEAT_WILDMENU */
1265
1266 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1267 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1268 if (c == Ctrl_BSL)
1269 {
1270 ++no_mapping;
1271 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001272 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 --no_mapping;
1274 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001275 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1276 * is in a mapping. */
1277 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001278 || (ccline.cmdfirstc == '=' && KeyTyped)
1279#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001280 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001281#endif
1282 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 vungetc(c);
1285 c = Ctrl_BSL;
1286 }
1287#ifdef FEAT_EVAL
1288 else if (c == 'e')
1289 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001290 char_u *p = NULL;
1291 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293 /*
1294 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001295 * Need to save and restore the current command line, to be
1296 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 */
1298 if (ccline.cmdpos == ccline.cmdlen)
1299 new_cmdpos = 99999; /* keep it at the end */
1300 else
1301 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (c == '=')
1305 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001306 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001307 * to avoid nasty things like going to another buffer when
1308 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001309 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001311 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001312
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001313 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001315 len = (int)STRLEN(p);
1316 if (realloc_cmdbuff(len + 1) == OK)
1317 {
1318 ccline.cmdlen = len;
1319 STRCPY(ccline.cmdbuff, p);
1320 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001322 /* Restore the cursor or use the position set with
1323 * set_cmdline_pos(). */
1324 if (new_cmdpos > ccline.cmdlen)
1325 ccline.cmdpos = ccline.cmdlen;
1326 else
1327 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001329 KeyTyped = FALSE; /* Don't do p_wc completion. */
1330 redrawcmd();
1331 goto cmdline_changed;
1332 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001333 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335 }
1336 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001337 got_int = FALSE; /* don't abandon the command line */
1338 did_emsg = FALSE;
1339 emsg_on_display = FALSE;
1340 redrawcmd();
1341 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343#endif
1344 else
1345 {
1346 if (c == Ctrl_G && p_im && restart_edit == 0)
1347 restart_edit = 'a';
1348 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1349 in history */
1350 goto returncmd; /* back to Normal mode */
1351 }
1352 }
1353
1354#ifdef FEAT_CMDWIN
1355 if (c == cedit_key || c == K_CMDWIN)
1356 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001357 if (ex_normal_busy == 0 && got_int == FALSE)
1358 {
1359 /*
1360 * Open a window to edit the command line (and history).
1361 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001362 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001363 some_key_typed = TRUE;
1364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366# ifdef FEAT_DIGRAPHS
1367 else
1368# endif
1369#endif
1370#ifdef FEAT_DIGRAPHS
1371 c = do_digraph(c);
1372#endif
1373
1374 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1375 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1376 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001377 /* In Ex mode a backslash escapes a newline. */
1378 if (exmode_active
1379 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001380 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001381 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001382 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 if (c == K_KENTER)
1385 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001387 else
1388 {
1389 gotesc = FALSE; /* Might have typed ESC previously, don't
1390 truncate the cmdline now. */
1391 if (ccheck_abbr(c + ABBR_OFF))
1392 goto cmdline_changed;
1393 if (!cmd_silent)
1394 {
1395 windgoto(msg_row, 0);
1396 out_flush();
1397 }
1398 break;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401
1402 /*
1403 * Completion for 'wildchar' or 'wildcharm' key.
1404 * - hitting <ESC> twice means: abandon command line.
1405 * - wildcard expansion is only done when the 'wildchar' key is really
1406 * typed, not when it comes from a macro
1407 */
1408 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1409 {
Bram Moolenaar52410572019-10-27 05:12:45 +01001410 int options = WILD_NO_BEEP;
1411 if (wim_flags[wim_index] & WIM_BUFLASTUSED)
1412 options |= WILD_BUFLASTUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1414 {
1415 /* if 'wildmode' contains "list" may still need to list */
1416 if (xpc.xp_numfiles > 1
1417 && !did_wild_list
1418 && (wim_flags[wim_index] & WIM_LIST))
1419 {
1420 (void)showmatches(&xpc, FALSE);
1421 redrawcmd();
1422 did_wild_list = TRUE;
1423 }
1424 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001425 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001426 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001428 res = nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001429 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 else
1431 res = OK; /* don't insert 'wildchar' now */
1432 }
1433 else /* typed p_wc first time */
1434 {
1435 wim_index = 0;
1436 j = ccline.cmdpos;
1437 /* if 'wildmode' first contains "longest", get longest
1438 * common part */
1439 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001440 res = nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001441 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 else
Bram Moolenaar52410572019-10-27 05:12:45 +01001443 res = nextwild(&xpc, WILD_EXPAND_KEEP, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001444 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 /* if interrupted while completing, behave like it failed */
1447 if (got_int)
1448 {
1449 (void)vpeekc(); /* remove <C-C> from input stream */
1450 got_int = FALSE; /* don't abandon the command line */
1451 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1452#ifdef FEAT_WILDMENU
1453 xpc.xp_context = EXPAND_NOTHING;
1454#endif
1455 goto cmdline_changed;
1456 }
1457
1458 /* when more than one match, and 'wildmode' first contains
1459 * "list", or no change and 'wildmode' contains "longest,list",
1460 * list all matches */
1461 if (res == OK && xpc.xp_numfiles > 1)
1462 {
1463 /* a "longest" that didn't do anything is skipped (but not
1464 * "list:longest") */
1465 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1466 wim_index = 1;
1467 if ((wim_flags[wim_index] & WIM_LIST)
1468#ifdef FEAT_WILDMENU
1469 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1470#endif
1471 )
1472 {
1473 if (!(wim_flags[0] & WIM_LONGEST))
1474 {
1475#ifdef FEAT_WILDMENU
1476 int p_wmnu_save = p_wmnu;
1477 p_wmnu = 0;
1478#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001479 /* remove match */
1480 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481#ifdef FEAT_WILDMENU
1482 p_wmnu = p_wmnu_save;
1483#endif
1484 }
1485#ifdef FEAT_WILDMENU
1486 (void)showmatches(&xpc, p_wmnu
1487 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1488#else
1489 (void)showmatches(&xpc, FALSE);
1490#endif
1491 redrawcmd();
1492 did_wild_list = TRUE;
1493 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaar52410572019-10-27 05:12:45 +01001494 nextwild(&xpc, WILD_LONGEST, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001495 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01001497 nextwild(&xpc, WILD_NEXT, options,
Bram Moolenaarb3479632012-11-28 16:49:58 +01001498 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001501 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503#ifdef FEAT_WILDMENU
1504 else if (xpc.xp_numfiles == -1)
1505 xpc.xp_context = EXPAND_NOTHING;
1506#endif
1507 }
1508 if (wim_index < 3)
1509 ++wim_index;
1510 if (c == ESC)
1511 gotesc = TRUE;
1512 if (res == OK)
1513 goto cmdline_changed;
1514 }
1515
1516 gotesc = FALSE;
1517
1518 /* <S-Tab> goes to last match, in a clumsy way */
1519 if (c == K_S_TAB && KeyTyped)
1520 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001521 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1522 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1523 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 goto cmdline_changed;
1525 }
1526
1527 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1528 c = NL;
1529
1530 do_abbr = TRUE; /* default: check for abbreviation */
1531
1532 /*
1533 * Big switch for a typed command line character.
1534 */
1535 switch (c)
1536 {
1537 case K_BS:
1538 case Ctrl_H:
1539 case K_DEL:
1540 case K_KDEL:
1541 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (c == K_KDEL)
1543 c = K_DEL;
1544
1545 /*
1546 * delete current character is the same as backspace on next
1547 * character, except at end of line
1548 */
1549 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1550 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (has_mbyte && c == K_DEL)
1552 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1553 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (ccline.cmdpos > 0)
1555 {
1556 char_u *p;
1557
1558 j = ccline.cmdpos;
1559 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (has_mbyte)
1561 {
1562 p = mb_prevptr(ccline.cmdbuff, p);
1563 if (c == Ctrl_W)
1564 {
1565 while (p > ccline.cmdbuff && vim_isspace(*p))
1566 p = mb_prevptr(ccline.cmdbuff, p);
1567 i = mb_get_class(p);
1568 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1569 p = mb_prevptr(ccline.cmdbuff, p);
1570 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001571 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 }
1573 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001574 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
1576 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1577 --p;
1578 i = vim_iswordc(p[-1]);
1579 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1580 && vim_iswordc(p[-1]) == i)
1581 --p;
1582 }
1583 else
1584 --p;
1585 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1586 ccline.cmdlen -= j - ccline.cmdpos;
1587 i = ccline.cmdpos;
1588 while (i < ccline.cmdlen)
1589 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1590
1591 /* Truncate at the end, required for multi-byte chars. */
1592 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001593#ifdef FEAT_SEARCH_EXTRA
1594 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001595 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001596 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001597 /* save view settings, so that the screen
1598 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001599 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001600 }
1601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 redrawcmd();
1603 }
1604 else if (ccline.cmdlen == 0 && c != Ctrl_W
1605 && ccline.cmdprompt == NULL && indent == 0)
1606 {
1607 /* In ex and debug mode it doesn't make sense to return. */
1608 if (exmode_active
1609#ifdef FEAT_EVAL
1610 || ccline.cmdfirstc == '>'
1611#endif
1612 )
1613 goto cmdline_not_changed;
1614
Bram Moolenaard23a8232018-02-10 18:45:26 +01001615 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (!cmd_silent)
1617 {
1618#ifdef FEAT_RIGHTLEFT
1619 if (cmdmsg_rl)
1620 msg_col = Columns;
1621 else
1622#endif
1623 msg_col = 0;
1624 msg_putchar(' '); /* delete ':' */
1625 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001626#ifdef FEAT_SEARCH_EXTRA
1627 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001628 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 redraw_cmdline = TRUE;
1631 goto returncmd; /* back to cmd mode */
1632 }
1633 goto cmdline_changed;
1634
1635 case K_INS:
1636 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 ccline.overstrike = !ccline.overstrike;
1638#ifdef CURSOR_SHAPE
1639 ui_cursor_shape(); /* may show different cursor shape */
1640#endif
1641 goto cmdline_not_changed;
1642
1643 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001644 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 /* ":lmap" mappings exists, toggle use of mappings. */
1647 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001648#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 im_set_active(FALSE); /* Disable input method */
1650#endif
1651 if (b_im_ptr != NULL)
1652 {
1653 if (State & LANGMAP)
1654 *b_im_ptr = B_IMODE_LMAP;
1655 else
1656 *b_im_ptr = B_IMODE_NONE;
1657 }
1658 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001659#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 else
1661 {
1662 /* There are no ":lmap" mappings, toggle IM. When
1663 * 'imdisable' is set don't try getting the status, it's
1664 * always off. */
1665 if ((p_imdisable && b_im_ptr != NULL)
1666 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1667 {
1668 im_set_active(FALSE); /* Disable input method */
1669 if (b_im_ptr != NULL)
1670 *b_im_ptr = B_IMODE_NONE;
1671 }
1672 else
1673 {
1674 im_set_active(TRUE); /* Enable input method */
1675 if (b_im_ptr != NULL)
1676 *b_im_ptr = B_IMODE_IM;
1677 }
1678 }
1679#endif
1680 if (b_im_ptr != NULL)
1681 {
1682 if (b_im_ptr == &curbuf->b_p_iminsert)
1683 set_iminsert_global();
1684 else
1685 set_imsearch_global();
1686 }
1687#ifdef CURSOR_SHAPE
1688 ui_cursor_shape(); /* may show different cursor shape */
1689#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001690#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 /* Show/unshow value of 'keymap' in status lines later. */
1692 status_redraw_curbuf();
1693#endif
1694 goto cmdline_not_changed;
1695
1696/* case '@': only in very old vi */
1697 case Ctrl_U:
1698 /* delete all characters left of the cursor */
1699 j = ccline.cmdpos;
1700 ccline.cmdlen -= j;
1701 i = ccline.cmdpos = 0;
1702 while (i < ccline.cmdlen)
1703 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1704 /* Truncate at the end, required for multi-byte chars. */
1705 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001706#ifdef FEAT_SEARCH_EXTRA
1707 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001708 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 redrawcmd();
1711 goto cmdline_changed;
1712
1713#ifdef FEAT_CLIPBOARD
1714 case Ctrl_Y:
1715 /* Copy the modeless selection, if there is one. */
1716 if (clip_star.state != SELECT_CLEARED)
1717 {
1718 if (clip_star.state == SELECT_DONE)
1719 clip_copy_modeless_selection(TRUE);
1720 goto cmdline_not_changed;
1721 }
1722 break;
1723#endif
1724
1725 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1726 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001727 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001728 * ":normal" runs out of characters. */
1729 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001730 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 goto cmdline_not_changed;
1732
1733 gotesc = TRUE; /* will free ccline.cmdbuff after
1734 putting it in history */
1735 goto returncmd; /* back to cmd mode */
1736
1737 case Ctrl_R: /* insert register */
1738#ifdef USE_ON_FLY_SCROLL
1739 dont_scroll = TRUE; /* disallow scrolling here */
1740#endif
1741 putcmdline('"', TRUE);
1742 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001743 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (i == Ctrl_O)
1745 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1746 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001747 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001748 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 --no_mapping;
1750#ifdef FEAT_EVAL
1751 /*
1752 * Insert the result of an expression.
1753 * Need to save the current command line, to be able to enter
1754 * a new one...
1755 */
1756 new_cmdpos = -1;
1757 if (c == '=')
1758 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001759 if (ccline.cmdfirstc == '=' // can't do this recursively
1760 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
1762 beep_flush();
1763 c = ESC;
1764 }
1765 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768#endif
1769 if (c != ESC) /* use ESC to cancel inserting register */
1770 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001771 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001772
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001773#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001774 /* When there was a serious error abort getting the
1775 * command line. */
1776 if (aborting())
1777 {
1778 gotesc = TRUE; /* will free ccline.cmdbuff after
1779 putting it in history */
1780 goto returncmd; /* back to cmd mode */
1781 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 KeyTyped = FALSE; /* Don't do p_wc completion. */
1784#ifdef FEAT_EVAL
1785 if (new_cmdpos >= 0)
1786 {
1787 /* set_cmdline_pos() was used */
1788 if (new_cmdpos > ccline.cmdlen)
1789 ccline.cmdpos = ccline.cmdlen;
1790 else
1791 ccline.cmdpos = new_cmdpos;
1792 }
1793#endif
1794 }
1795 redrawcmd();
1796 goto cmdline_changed;
1797
1798 case Ctrl_D:
1799 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1800 break; /* Use ^D as normal char instead */
1801
1802 redrawcmd();
1803 continue; /* don't do incremental search now */
1804
1805 case K_RIGHT:
1806 case K_S_RIGHT:
1807 case K_C_RIGHT:
1808 do
1809 {
1810 if (ccline.cmdpos >= ccline.cmdlen)
1811 break;
1812 i = cmdline_charsize(ccline.cmdpos);
1813 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1814 break;
1815 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001817 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 + ccline.cmdpos);
1819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 ++ccline.cmdpos;
1821 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001822 while ((c == K_S_RIGHT || c == K_C_RIGHT
1823 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (has_mbyte)
1826 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 goto cmdline_not_changed;
1828
1829 case K_LEFT:
1830 case K_S_LEFT:
1831 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001832 if (ccline.cmdpos == 0)
1833 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 do
1835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (has_mbyte) /* move to first byte of char */
1838 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1839 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1841 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001842 while (ccline.cmdpos > 0
1843 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001844 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (has_mbyte)
1847 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 goto cmdline_not_changed;
1849
1850 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001851 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001852 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar4f974752019-02-17 17:44:42 +01001854#ifdef FEAT_GUI_MSWIN
1855 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1856 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001857 case K_F4:
1858 if (mod_mask == MOD_MASK_ALT)
1859 {
1860 redrawcmd(); /* somehow the cmdline is cleared */
1861 goto cmdline_not_changed;
1862 }
1863 break;
1864#endif
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 case K_MIDDLEDRAG:
1867 case K_MIDDLERELEASE:
1868 goto cmdline_not_changed; /* Ignore mouse */
1869
1870 case K_MIDDLEMOUSE:
1871# ifdef FEAT_GUI
1872 /* When GUI is active, also paste when 'mouse' is empty */
1873 if (!gui.in_use)
1874# endif
1875 if (!mouse_has(MOUSE_COMMAND))
1876 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001877# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001879 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001881# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001882 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 redrawcmd();
1884 goto cmdline_changed;
1885
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001886# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001888 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 redrawcmd();
1890 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 case K_LEFTDRAG:
1894 case K_LEFTRELEASE:
1895 case K_RIGHTDRAG:
1896 case K_RIGHTRELEASE:
1897 /* Ignore drag and release events when the button-down wasn't
1898 * seen before. */
1899 if (ignore_drag_release)
1900 goto cmdline_not_changed;
1901 /* FALLTHROUGH */
1902 case K_LEFTMOUSE:
1903 case K_RIGHTMOUSE:
1904 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1905 ignore_drag_release = TRUE;
1906 else
1907 ignore_drag_release = FALSE;
1908# ifdef FEAT_GUI
1909 /* When GUI is active, also move when 'mouse' is empty */
1910 if (!gui.in_use)
1911# endif
1912 if (!mouse_has(MOUSE_COMMAND))
1913 goto cmdline_not_changed; /* Ignore mouse */
1914# ifdef FEAT_CLIPBOARD
1915 if (mouse_row < cmdline_row && clip_star.available)
1916 {
1917 int button, is_click, is_drag;
1918
1919 /*
1920 * Handle modeless selection.
1921 */
1922 button = get_mouse_button(KEY2TERMCAP1(c),
1923 &is_click, &is_drag);
1924 if (mouse_model_popup() && button == MOUSE_LEFT
1925 && (mod_mask & MOD_MASK_SHIFT))
1926 {
1927 /* Translate shift-left to right button. */
1928 button = MOUSE_RIGHT;
1929 mod_mask &= ~MOD_MASK_SHIFT;
1930 }
1931 clip_modeless(button, is_click, is_drag);
1932 goto cmdline_not_changed;
1933 }
1934# endif
1935
1936 set_cmdspos();
1937 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1938 ++ccline.cmdpos)
1939 {
1940 i = cmdline_charsize(ccline.cmdpos);
1941 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1942 && mouse_col < ccline.cmdspos % Columns + i)
1943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (has_mbyte)
1945 {
1946 /* Count ">" for double-wide char that doesn't fit. */
1947 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001948 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 + ccline.cmdpos) - 1;
1950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 ccline.cmdspos += i;
1952 }
1953 goto cmdline_not_changed;
1954
1955 /* Mouse scroll wheel: ignored here */
1956 case K_MOUSEDOWN:
1957 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001958 case K_MOUSELEFT:
1959 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 /* Alternate buttons ignored here */
1961 case K_X1MOUSE:
1962 case K_X1DRAG:
1963 case K_X1RELEASE:
1964 case K_X2MOUSE:
1965 case K_X2DRAG:
1966 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001967 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 goto cmdline_not_changed;
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970#ifdef FEAT_GUI
1971 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1972 case K_LEFTRELEASE_NM:
1973 goto cmdline_not_changed;
1974
1975 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001976 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 gui_do_scroll();
1979 redrawcmd();
1980 }
1981 goto cmdline_not_changed;
1982
1983 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001984 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001986 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 redrawcmd();
1988 }
1989 goto cmdline_not_changed;
1990#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001991#ifdef FEAT_GUI_TABLINE
1992 case K_TABLINE:
1993 case K_TABMENU:
1994 /* Don't want to change any tabs here. Make sure the same tab
1995 * is still selected. */
1996 if (gui_use_tabline())
1997 gui_mch_set_curtab(tabpage_index(curtab));
1998 goto cmdline_not_changed;
1999#endif
2000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 case K_SELECT: /* end of Select mode mapping - ignore */
2002 goto cmdline_not_changed;
2003
2004 case Ctrl_B: /* begin of command line */
2005 case K_HOME:
2006 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 case K_S_HOME:
2008 case K_C_HOME:
2009 ccline.cmdpos = 0;
2010 set_cmdspos();
2011 goto cmdline_not_changed;
2012
2013 case Ctrl_E: /* end of command line */
2014 case K_END:
2015 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case K_S_END:
2017 case K_C_END:
2018 ccline.cmdpos = ccline.cmdlen;
2019 set_cmdspos_cursor();
2020 goto cmdline_not_changed;
2021
2022 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002023 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 break;
2025 goto cmdline_changed;
2026
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002027 case Ctrl_L:
2028#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002029 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002030 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002031#endif
2032
2033 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002034 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
2036 goto cmdline_changed;
2037
2038 case Ctrl_N: /* next match */
2039 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002040 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002042 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2043 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002045 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002047 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 case K_UP:
2049 case K_DOWN:
2050 case K_S_UP:
2051 case K_S_DOWN:
2052 case K_PAGEUP:
2053 case K_KPAGEUP:
2054 case K_PAGEDOWN:
2055 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002056 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 goto cmdline_not_changed;
2058
2059 i = hiscnt;
2060
2061 /* save current command string so it can be restored later */
2062 if (lookfor == NULL)
2063 {
2064 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2065 goto cmdline_not_changed;
2066 lookfor[ccline.cmdpos] = NUL;
2067 }
2068
2069 j = (int)STRLEN(lookfor);
2070 for (;;)
2071 {
2072 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002073 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002074 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002076 if (hiscnt == get_hislen()) /* first time */
2077 hiscnt = *get_hisidx(histype);
2078 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2079 hiscnt = get_hislen() - 1;
2080 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 --hiscnt;
2082 else /* at top of list */
2083 {
2084 hiscnt = i;
2085 break;
2086 }
2087 }
2088 else /* one step forwards */
2089 {
2090 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002091 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002093 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 break;
2095 }
2096
2097 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002098 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002100 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 hiscnt = 0;
2102 else
2103 ++hiscnt;
2104 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002105 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 hiscnt = i;
2108 break;
2109 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002110 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002111 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002112 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 lookfor, (size_t)j) == 0)
2114 break;
2115 }
2116
2117 if (hiscnt != i) /* jumped to other entry */
2118 {
2119 char_u *p;
2120 int len;
2121 int old_firstc;
2122
Bram Moolenaar438d1762018-09-30 17:11:48 +02002123 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002124 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002125 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 p = lookfor; /* back to the old one */
2127 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002128 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130 if (histype == HIST_SEARCH
2131 && p != lookfor
2132 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2133 {
2134 /* Correct for the separator character used when
2135 * adding the history entry vs the one used now.
2136 * First loop: count length.
2137 * Second loop: copy the characters. */
2138 for (i = 0; i <= 1; ++i)
2139 {
2140 len = 0;
2141 for (j = 0; p[j] != NUL; ++j)
2142 {
2143 /* Replace old sep with new sep, unless it is
2144 * escaped. */
2145 if (p[j] == old_firstc
2146 && (j == 0 || p[j - 1] != '\\'))
2147 {
2148 if (i > 0)
2149 ccline.cmdbuff[len] = firstc;
2150 }
2151 else
2152 {
2153 /* Escape new sep, unless it is already
2154 * escaped. */
2155 if (p[j] == firstc
2156 && (j == 0 || p[j - 1] != '\\'))
2157 {
2158 if (i > 0)
2159 ccline.cmdbuff[len] = '\\';
2160 ++len;
2161 }
2162 if (i > 0)
2163 ccline.cmdbuff[len] = p[j];
2164 }
2165 ++len;
2166 }
2167 if (i == 0)
2168 {
2169 alloc_cmdbuff(len);
2170 if (ccline.cmdbuff == NULL)
2171 goto returncmd;
2172 }
2173 }
2174 ccline.cmdbuff[len] = NUL;
2175 }
2176 else
2177 {
2178 alloc_cmdbuff((int)STRLEN(p));
2179 if (ccline.cmdbuff == NULL)
2180 goto returncmd;
2181 STRCPY(ccline.cmdbuff, p);
2182 }
2183
2184 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2185 redrawcmd();
2186 goto cmdline_changed;
2187 }
2188 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002189 goto cmdline_not_changed;
2190
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002191#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002192 case Ctrl_G: /* next match */
2193 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002194 if (may_adjust_incsearch_highlighting(
2195 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002196 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199
2200 case Ctrl_V:
2201 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 putcmdline('^', TRUE);
2204 c = get_literal(); /* get next (two) character(s) */
2205 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002206 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 /* may need to remove ^ when composing char was typed */
2208 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2209 {
2210 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2211 msg_putchar(' ');
2212 cursorcmd();
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 break;
2215
2216#ifdef FEAT_DIGRAPHS
2217 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002220# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002224 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (c != NUL)
2226 break;
2227
2228 redrawcmd();
2229 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002230#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232#ifdef FEAT_RIGHTLEFT
2233 case Ctrl__: /* CTRL-_: switch language mode */
2234 if (!p_ari)
2235 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002236 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 goto cmdline_not_changed;
2238#endif
2239
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002240 case K_PS:
2241 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2242 goto cmdline_changed;
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 default:
2245#ifdef UNIX
2246 if (c == intr_char)
2247 {
2248 gotesc = TRUE; /* will free ccline.cmdbuff after
2249 putting it in history */
2250 goto returncmd; /* back to Normal mode */
2251 }
2252#endif
2253 /*
2254 * Normal character with no special meaning. Just set mod_mask
2255 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2256 * the string <S-Space>. This should only happen after ^V.
2257 */
2258 if (!IS_SPECIAL(c))
2259 mod_mask = 0x0;
2260 break;
2261 }
2262 /*
2263 * End of switch on command line character.
2264 * We come here if we have a normal character.
2265 */
2266
Bram Moolenaar13505972019-01-24 15:04:48 +01002267 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2268 && (ccheck_abbr(
2269 // Add ABBR_OFF for characters above 0x100, this is
2270 // what check_abbr() expects.
2271 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2272 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 goto cmdline_changed;
2274
2275 /*
2276 * put the character in the command line
2277 */
2278 if (IS_SPECIAL(c) || mod_mask != 0)
2279 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2280 else
2281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (has_mbyte)
2283 {
2284 j = (*mb_char2bytes)(c, IObuff);
2285 IObuff[j] = NUL; /* exclude composing chars */
2286 put_on_cmdline(IObuff, j, TRUE);
2287 }
2288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 IObuff[0] = c;
2291 put_on_cmdline(IObuff, 1, TRUE);
2292 }
2293 }
2294 goto cmdline_changed;
2295
2296/*
2297 * This part implements incremental searches for "/" and "?"
2298 * Jump to cmdline_not_changed when a character has been read but the command
2299 * line did not change. Then we only search and redraw if something changed in
2300 * the past.
2301 * Jump to cmdline_changed when the command line did change.
2302 * (Sorry for the goto's, I know it is ugly).
2303 */
2304cmdline_not_changed:
2305#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002306 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 continue;
2308#endif
2309
2310cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002311 /* Trigger CmdlineChanged autocommands. */
2312 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002315 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#endif
2317
2318#ifdef FEAT_RIGHTLEFT
2319 if (cmdmsg_rl
2320# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002321 || (p_arshape && !p_tbidi
2322 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323# endif
2324 )
2325 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002326 * right-left typing. Not efficient, but it works.
2327 * Do it only when there are no characters left to read
2328 * to avoid useless intermediate redraws. */
2329 if (vpeekc() == NUL)
2330 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332 }
2333
2334returncmd:
2335
2336#ifdef FEAT_RIGHTLEFT
2337 cmdmsg_rl = FALSE;
2338#endif
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002341 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002344 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345#endif
2346
2347 if (ccline.cmdbuff != NULL)
2348 {
2349 /*
2350 * Put line in history buffer (":" and "=" only when it was typed).
2351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (ccline.cmdlen && firstc != NUL
2353 && (some_key_typed || histype == HIST_SEARCH))
2354 {
2355 add_to_history(histype, ccline.cmdbuff, TRUE,
2356 histype == HIST_SEARCH ? firstc : NUL);
2357 if (firstc == ':')
2358 {
2359 vim_free(new_last_cmdline);
2360 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2361 }
2362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002364 if (gotesc)
2365 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367
2368 /*
2369 * If the screen was shifted up, redraw the whole screen (later).
2370 * If the line is too long, clear it, so ruler and shown command do
2371 * not get printed in the middle of it.
2372 */
2373 msg_check();
2374 msg_scroll = save_msg_scroll;
2375 redir_off = FALSE;
2376
2377 /* When the command line was typed, no need for a wait-return prompt. */
2378 if (some_key_typed)
2379 need_wait_return = FALSE;
2380
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002381 /* Trigger CmdlineLeave autocommands. */
2382 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002385#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2387 im_save_status(b_im_ptr);
2388 im_set_active(FALSE);
2389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391#ifdef CURSOR_SHAPE
2392 ui_cursor_shape(); /* may show different cursor shape */
2393#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002394 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
Bram Moolenaar438d1762018-09-30 17:11:48 +02002396theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002397 {
2398 char_u *p = ccline.cmdbuff;
2399
Bram Moolenaar438d1762018-09-30 17:11:48 +02002400 if (did_save_ccline)
2401 restore_cmdline(&save_ccline);
2402 else
2403 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002404 return p;
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406}
2407
2408#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2409/*
2410 * Get a command line with a prompt.
2411 * This is prepared to be called recursively from getcmdline() (e.g. by
2412 * f_input() when evaluating an expression from CTRL-R =).
2413 * Returns the command line in allocated memory, or NULL.
2414 */
2415 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002416getcmdline_prompt(
2417 int firstc,
2418 char_u *prompt, /* command line prompt */
2419 int attr, /* attributes for prompt */
2420 int xp_context, /* type of expansion */
2421 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002424 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002425 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002427 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar438d1762018-09-30 17:11:48 +02002429 if (ccline.cmdbuff != NULL)
2430 {
2431 // Save the values of the current cmdline and restore them below.
2432 save_cmdline(&save_ccline);
2433 did_save_ccline = TRUE;
2434 }
2435
Bram Moolenaar66b51422019-08-18 21:44:12 +02002436 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 ccline.cmdprompt = prompt;
2438 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002439# ifdef FEAT_EVAL
2440 ccline.xp_context = xp_context;
2441 ccline.xp_arg = xp_arg;
2442 ccline.input_fn = (firstc == '@');
2443# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002444 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002445 s = getcmdline_int(firstc, 1L, 0, FALSE);
2446
2447 if (did_save_ccline)
2448 restore_cmdline(&save_ccline);
2449
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002450 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002451 /* Restore msg_col, the prompt from input() may have changed it.
2452 * But only if called recursively and the commandline is therefore being
2453 * restored to an old one; if not, the input() prompt stays on the screen,
2454 * so we need its modified msg_col left intact. */
2455 if (ccline.cmdbuff != NULL)
2456 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 return s;
2459}
2460#endif
2461
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002462/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002463 * Return TRUE when the text must not be changed and we can't switch to
2464 * another window or buffer. Used when editing the command line, evaluating
2465 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002466 */
2467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002468text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002469{
2470#ifdef FEAT_CMDWIN
2471 if (cmdwin_type != 0)
2472 return TRUE;
2473#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002474 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002475}
2476
2477/*
2478 * Give an error message for a command that isn't allowed while the cmdline
2479 * window is open or editing the cmdline in another way.
2480 */
2481 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002482text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002483{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002484 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002485}
2486
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002487 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002488get_text_locked_msg(void)
2489{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002490#ifdef FEAT_CMDWIN
2491 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002492 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002493#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002494 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002495}
2496
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002497/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002498 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2499 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500 */
2501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002502curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002503{
2504 if (curbuf_lock > 0)
2505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002506 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002507 return TRUE;
2508 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002509 return allbuf_locked();
2510}
2511
2512/*
2513 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2514 * message.
2515 */
2516 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002517allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002518{
2519 if (allbuf_lock > 0)
2520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002522 return TRUE;
2523 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002524 return FALSE;
2525}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002528cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2531 if (cmdline_star > 0) /* showing '*', always 1 position */
2532 return 1;
2533#endif
2534 return ptr2cells(ccline.cmdbuff + idx);
2535}
2536
2537/*
2538 * Compute the offset of the cursor on the command line for the prompt and
2539 * indent.
2540 */
2541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002542set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002544 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 ccline.cmdspos = 1 + ccline.cmdindent;
2546 else
2547 ccline.cmdspos = 0 + ccline.cmdindent;
2548}
2549
2550/*
2551 * Compute the screen position for the cursor on the command line.
2552 */
2553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002554set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 int i, m, c;
2557
2558 set_cmdspos();
2559 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002562 if (m < 0) /* overflow, Columns or Rows at weird value */
2563 m = MAXCOL;
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 else
2566 m = MAXCOL;
2567 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2568 {
2569 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2571 if (has_mbyte)
2572 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002573 /* If the cmdline doesn't fit, show cursor on last visible char.
2574 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if ((ccline.cmdspos += c) >= m)
2576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 ccline.cmdspos -= c;
2578 break;
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002581 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583}
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585/*
2586 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2587 * character that doesn't fit, so that a ">" must be displayed.
2588 */
2589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002590correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002592 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2594 && ccline.cmdspos % Columns + cells > Columns)
2595 ccline.cmdspos++;
2596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598/*
2599 * Get an Ex command line for the ":" command.
2600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602getexline(
2603 int c, /* normally ':', NUL for ":append" */
2604 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002605 int indent, /* indent for inside conditionals */
2606 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 /* When executing a register, remove ':' that's in front of each line. */
2609 if (exec_from_reg && vpeekc() == ':')
2610 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002611 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613
2614/*
2615 * Get an Ex command line for Ex mode.
2616 * In Ex mode we only use the OS supplied line editing features and no
2617 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002618 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002621getexmodeline(
2622 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002623 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002624 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002625 int indent, /* indent for inside conditionals */
2626 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 garray_T line_ga;
2629 char_u *pend;
2630 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002631 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002632 int escaped = FALSE; /* CTRL-V typed */
2633 int vcol = 0;
2634 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002635 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002636 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 /* Switch cursor on now. This avoids that it happens after the "\n", which
2639 * confuses the system function that computes tabstops. */
2640 cursor_on();
2641
2642 /* always start in column 0; write a newline if necessary */
2643 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002648 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002649 if (p_prompt)
2650 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 while (indent-- > 0)
2652 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
2655
2656 ga_init2(&line_ga, 1, 30);
2657
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002658 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002659 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002660 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002661 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002662 while (indent >= 8)
2663 {
2664 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002665 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002666 indent -= 8;
2667 }
2668 while (indent-- > 0)
2669 {
2670 ga_append(&line_ga, ' ');
2671 msg_putchar(' ');
2672 }
2673 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002674 ++no_mapping;
2675 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 /*
2678 * Get the line, one character at a time.
2679 */
2680 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002683 long sw;
2684 char_u *s;
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (ga_grow(&line_ga, 40) == FAIL)
2687 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
Bram Moolenaarba748c82017-02-26 14:00:07 +01002689 /*
2690 * Get one character at a time.
2691 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002692 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002693
2694 /* Check for a ":normal" command and no more characters left. */
2695 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2696 c1 = '\n';
2697 else
2698 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002701 * Handle line editing.
2702 * Previously this was left to the system, putting the terminal in
2703 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 msg_putchar('\n');
2708 break;
2709 }
2710
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002711 if (c1 == K_PS)
2712 {
2713 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2714 goto redraw;
2715 }
2716
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 if (!escaped)
2718 {
2719 /* CR typed means "enter", which is NL */
2720 if (c1 == '\r')
2721 c1 = '\n';
2722
2723 if (c1 == BS || c1 == K_BS
2724 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 if (line_ga.ga_len > 0)
2727 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002728 if (has_mbyte)
2729 {
2730 p = (char_u *)line_ga.ga_data;
2731 p[line_ga.ga_len] = NUL;
2732 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2733 line_ga.ga_len -= len;
2734 }
2735 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002736 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 goto redraw;
2738 }
2739 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 msg_col = startcol;
2745 msg_clr_eos();
2746 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002747 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748 }
2749
2750 if (c1 == Ctrl_T)
2751 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002752 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 p = (char_u *)line_ga.ga_data;
2754 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002755 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002756 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002758 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002760 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 p = (char_u *)line_ga.ga_data;
2762 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2764 *s = ' ';
2765 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767redraw:
2768 /* redraw the line */
2769 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002771 p = (char_u *)line_ga.ga_data;
2772 p[line_ga.ga_len] = NUL;
2773 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002779 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002780 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002784 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 msg_outtrans_len(p, len);
2786 vcol += ptr2cells(p);
2787 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002791 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 continue;
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 if (c1 == Ctrl_D)
2796 {
2797 /* Delete one shiftwidth. */
2798 p = (char_u *)line_ga.ga_data;
2799 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 if (prev_char == '^')
2802 ex_keep_indent = TRUE;
2803 indent = 0;
2804 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
2806 else
2807 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002809 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 if (indent > 0)
2811 {
2812 --indent;
2813 indent -= indent % get_sw_value(curbuf);
2814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002816 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002818 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2820 --line_ga.ga_len;
2821 }
2822 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824
2825 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2826 {
2827 escaped = TRUE;
2828 continue;
2829 }
2830
2831 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2832 if (IS_SPECIAL(c1))
2833 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835
2836 if (IS_SPECIAL(c1))
2837 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002838 if (has_mbyte)
2839 len = (*mb_char2bytes)(c1,
2840 (char_u *)line_ga.ga_data + line_ga.ga_len);
2841 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002842 {
2843 len = 1;
2844 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2845 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 if (c1 == '\n')
2847 msg_putchar('\n');
2848 else if (c1 == TAB)
2849 {
2850 /* Don't use chartabsize(), 'ts' can be different */
2851 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002853 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002858 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002861 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 escaped = FALSE;
2863
2864 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002865 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002867 /* We are done when a NL is entered, but not when it comes after an
2868 * odd number of backslashes, that results in a NUL. */
2869 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002871 int bcount = 0;
2872
2873 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2874 ++bcount;
2875
2876 if (bcount > 0)
2877 {
2878 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2879 * "\NL", etc. */
2880 line_ga.ga_len -= (bcount + 1) / 2;
2881 pend -= (bcount + 1) / 2;
2882 pend[-1] = '\n';
2883 }
2884
2885 if ((bcount & 1) == 0)
2886 {
2887 --line_ga.ga_len;
2888 --pend;
2889 *pend = NUL;
2890 break;
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 }
2894
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 --no_mapping;
2896 --allow_keys;
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /* make following messages go to the next line */
2899 msg_didout = FALSE;
2900 msg_col = 0;
2901 if (msg_row < Rows - 1)
2902 ++msg_row;
2903 emsg_on_display = FALSE; /* don't want ui_delay() */
2904
2905 if (got_int)
2906 ga_clear(&line_ga);
2907
2908 return (char_u *)line_ga.ga_data;
2909}
2910
Bram Moolenaare344bea2005-09-01 20:46:49 +00002911# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2912 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913/*
2914 * Return TRUE if ccline.overstrike is on.
2915 */
2916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002917cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 return ccline.overstrike;
2920}
2921
2922/*
2923 * Return TRUE if the cursor is at the end of the cmdline.
2924 */
2925 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002926cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 return (ccline.cmdpos >= ccline.cmdlen);
2929}
2930#endif
2931
Bram Moolenaar9372a112005-12-06 19:59:18 +00002932#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933/*
2934 * Return the virtual column number at the current cursor position.
2935 * This is used by the IM code to obtain the start of the preedit string.
2936 */
2937 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002938cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2941 return MAXCOL;
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (has_mbyte)
2944 {
2945 colnr_T col;
2946 int i = 0;
2947
2948 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 return col;
2952 }
2953 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 return ccline.cmdpos;
2955}
2956#endif
2957
2958#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2959/*
2960 * If part of the command line is an IM preedit string, redraw it with
2961 * IM feedback attributes. The cursor position is restored after drawing.
2962 */
2963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002964redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 if ((State & CMDLINE)
2967 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002968 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 && !p_imdisable
2970 && im_is_preediting())
2971 {
2972 int cmdpos = 0;
2973 int cmdspos;
2974 int old_row;
2975 int old_col;
2976 colnr_T col;
2977
2978 old_row = msg_row;
2979 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002980 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (has_mbyte)
2983 {
2984 for (col = 0; col < preedit_start_col
2985 && cmdpos < ccline.cmdlen; ++col)
2986 {
2987 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002988 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
2990 }
2991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 cmdspos += preedit_start_col;
2994 cmdpos += preedit_start_col;
2995 }
2996
2997 msg_row = cmdline_row + (cmdspos / (int)Columns);
2998 msg_col = cmdspos % (int)Columns;
2999 if (msg_row >= Rows)
3000 msg_row = Rows - 1;
3001
3002 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3003 {
3004 int char_len;
3005 int char_attr;
3006
3007 char_attr = im_get_feedback_attr(col);
3008 if (char_attr < 0)
3009 break; /* end of preedit string */
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003012 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 char_len = 1;
3015
3016 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3017 cmdpos += char_len;
3018 }
3019
3020 msg_row = old_row;
3021 msg_col = old_col;
3022 }
3023}
3024#endif /* FEAT_XIM && FEAT_GUI_GTK */
3025
3026/*
3027 * Allocate a new command line buffer.
3028 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 */
3030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003031alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 /*
3034 * give some extra space to avoid having to allocate all the time
3035 */
3036 if (len < 80)
3037 len = 100;
3038 else
3039 len += 20;
3040
3041 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3042 ccline.cmdbufflen = len;
3043}
3044
3045/*
3046 * Re-allocate the command line to length len + something extra.
3047 * return FAIL for failure, OK otherwise
3048 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 char_u *p;
3053
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003054 if (len < ccline.cmdbufflen)
3055 return OK; /* no need to resize */
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 p = ccline.cmdbuff;
3058 alloc_cmdbuff(len); /* will get some more */
3059 if (ccline.cmdbuff == NULL) /* out of memory */
3060 {
3061 ccline.cmdbuff = p; /* keep the old one */
3062 return FAIL;
3063 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003064 /* There isn't always a NUL after the command, but it may need to be
3065 * there, thus copy up to the NUL and add a NUL. */
3066 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3067 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003069
3070 if (ccline.xpc != NULL
3071 && ccline.xpc->xp_pattern != NULL
3072 && ccline.xpc->xp_context != EXPAND_NOTHING
3073 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3074 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003075 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003076
3077 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3078 * to point into the newly allocated memory. */
3079 if (i >= 0 && i <= ccline.cmdlen)
3080 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3081 }
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 return OK;
3084}
3085
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003086#if defined(FEAT_ARABIC) || defined(PROTO)
3087static char_u *arshape_buf = NULL;
3088
3089# if defined(EXITFREE) || defined(PROTO)
3090 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003091free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003092{
3093 vim_free(arshape_buf);
3094}
3095# endif
3096#endif
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098/*
3099 * Draw part of the cmdline at the current cursor position. But draw stars
3100 * when cmdline_star is TRUE.
3101 */
3102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3106 int i;
3107
3108 if (cmdline_star > 0)
3109 for (i = 0; i < len; ++i)
3110 {
3111 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003113 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 else
3116#endif
3117#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003118 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 static int buflen = 0;
3121 char_u *p;
3122 int j;
3123 int newlen = 0;
3124 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003125 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 int prev_c = 0;
3127 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003128 int u8c;
3129 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 /*
3133 * Do arabic shaping into a temporary buffer. This is very
3134 * inefficient!
3135 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003136 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 /* Re-allocate the buffer. We keep it around to avoid a lot of
3139 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003140 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003141 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142 arshape_buf = alloc(buflen);
3143 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return; /* out of memory */
3145 }
3146
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003147 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3148 {
3149 /* Prepend a space to draw the leading composing char on. */
3150 arshape_buf[0] = ' ';
3151 newlen = 1;
3152 }
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 for (j = start; j < start + len; j += mb_l)
3155 {
3156 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003157 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003158 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (ARABIC_CHAR(u8c))
3160 {
3161 /* Do Arabic shaping. */
3162 if (cmdmsg_rl)
3163 {
3164 /* displaying from right to left */
3165 pc = prev_c;
3166 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003167 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (j + mb_l >= start + len)
3169 nc = NUL;
3170 else
3171 nc = utf_ptr2char(p + mb_l);
3172 }
3173 else
3174 {
3175 /* displaying from left to right */
3176 if (j + mb_l >= start + len)
3177 pc = NUL;
3178 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003179 {
3180 int pcc[MAX_MCO];
3181
3182 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003184 pc1 = pcc[0];
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 nc = prev_c;
3187 }
3188 prev_c = u8c;
3189
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003190 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003192 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003193 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003195 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3196 if (u8cc[1] != 0)
3197 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 }
3201 else
3202 {
3203 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 newlen += mb_l;
3206 }
3207 }
3208
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003209 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 else
3212#endif
3213 msg_outtrans_len(ccline.cmdbuff + start, len);
3214}
3215
3216/*
3217 * Put a character on the command line. Shifts the following text to the
3218 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3219 * "c" must be printable (fit in one display cell)!
3220 */
3221 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003222putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 if (cmd_silent)
3225 return;
3226 msg_no_more = TRUE;
3227 msg_putchar(c);
3228 if (shift)
3229 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3230 msg_no_more = FALSE;
3231 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003232 extra_char = c;
3233 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234}
3235
3236/*
3237 * Undo a putcmdline(c, FALSE).
3238 */
3239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003240unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 if (cmd_silent)
3243 return;
3244 msg_no_more = TRUE;
3245 if (ccline.cmdlen == ccline.cmdpos)
3246 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003247 else if (has_mbyte)
3248 draw_cmdline(ccline.cmdpos,
3249 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
3251 draw_cmdline(ccline.cmdpos, 1);
3252 msg_no_more = FALSE;
3253 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003254 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255}
3256
3257/*
3258 * Put the given string, of the given length, onto the command line.
3259 * If len is -1, then STRLEN() is used to calculate the length.
3260 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3261 * part will be redrawn, otherwise it will not. If this function is called
3262 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3263 * called afterwards.
3264 */
3265 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003266put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 int retval;
3269 int i;
3270 int m;
3271 int c;
3272
3273 if (len < 0)
3274 len = (int)STRLEN(str);
3275
3276 /* Check if ccline.cmdbuff needs to be longer */
3277 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003278 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else
3280 retval = OK;
3281 if (retval == OK)
3282 {
3283 if (!ccline.overstrike)
3284 {
3285 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3286 ccline.cmdbuff + ccline.cmdpos,
3287 (size_t)(ccline.cmdlen - ccline.cmdpos));
3288 ccline.cmdlen += len;
3289 }
3290 else
3291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (has_mbyte)
3293 {
3294 /* Count nr of characters in the new string. */
3295 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003296 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 ++m;
3298 /* Count nr of bytes in cmdline that are overwritten by these
3299 * characters. */
3300 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003301 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 --m;
3303 if (i < ccline.cmdlen)
3304 {
3305 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3306 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3307 ccline.cmdlen += ccline.cmdpos + len - i;
3308 }
3309 else
3310 ccline.cmdlen = ccline.cmdpos + len;
3311 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003312 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 ccline.cmdlen = ccline.cmdpos + len;
3314 }
3315 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3316 ccline.cmdbuff[ccline.cmdlen] = NUL;
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (enc_utf8)
3319 {
3320 /* When the inserted text starts with a composing character,
3321 * backup to the character before it. There could be two of them.
3322 */
3323 i = 0;
3324 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3325 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3326 {
3327 i = (*mb_head_off)(ccline.cmdbuff,
3328 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3329 ccline.cmdpos -= i;
3330 len += i;
3331 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3332 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003333#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3335 {
3336 /* Check the previous character for Arabic combining pair. */
3337 i = (*mb_head_off)(ccline.cmdbuff,
3338 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3339 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3340 + ccline.cmdpos - i), c))
3341 {
3342 ccline.cmdpos -= i;
3343 len += i;
3344 }
3345 else
3346 i = 0;
3347 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (i != 0)
3350 {
3351 /* Also backup the cursor position. */
3352 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3353 ccline.cmdspos -= i;
3354 msg_col -= i;
3355 if (msg_col < 0)
3356 {
3357 msg_col += Columns;
3358 --msg_row;
3359 }
3360 }
3361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 if (redraw && !cmd_silent)
3364 {
3365 msg_no_more = TRUE;
3366 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003367 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3369 /* Avoid clearing the rest of the line too often. */
3370 if (cmdline_row != i || ccline.overstrike)
3371 msg_clr_eos();
3372 msg_no_more = FALSE;
3373 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003374 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003376 m = Columns * Rows;
3377 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003379 }
3380 else
3381 m = MAXCOL;
3382 for (i = 0; i < len; ++i)
3383 {
3384 c = cmdline_charsize(ccline.cmdpos);
3385 /* count ">" for a double-wide char that doesn't fit. */
3386 if (has_mbyte)
3387 correct_cmdspos(ccline.cmdpos, c);
3388 /* Stop cursor at the end of the screen, but do increment the
3389 * insert position, so that entering a very long command
3390 * works, even though you can't see it. */
3391 if (ccline.cmdspos + c < m)
3392 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003393
Bram Moolenaar14184a32019-02-16 15:10:30 +01003394 if (has_mbyte)
3395 {
3396 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3397 if (c > len - i - 1)
3398 c = len - i - 1;
3399 ccline.cmdpos += c;
3400 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003402 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 }
3405 if (redraw)
3406 msg_check();
3407 return retval;
3408}
3409
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410static cmdline_info_T prev_ccline;
3411static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003412
3413/*
3414 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3415 * and overwrite it. But get_cmdline_str() may need it, thus make it
3416 * available globally in prev_ccline.
3417 */
3418 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003419save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003420{
3421 if (!prev_ccline_used)
3422 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003424 prev_ccline_used = TRUE;
3425 }
3426 *ccp = prev_ccline;
3427 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003428 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003429}
3430
3431/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003432 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003433 */
3434 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003435restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003436{
3437 ccline = prev_ccline;
3438 prev_ccline = *ccp;
3439}
3440
Bram Moolenaar8299df92004-07-10 09:47:34 +00003441/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003442 * Paste a yank register into the command line.
3443 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003444 * insert_reg() can't be used here, because special characters from the
3445 * register contents will be interpreted as commands.
3446 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003447 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003448 */
3449 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450cmdline_paste(
3451 int regname,
3452 int literally, /* Insert text literally instead of "as typed" */
3453 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003454{
3455 long i;
3456 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003457 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003458 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003459
3460 /* check for valid regname; also accept special characters for CTRL-R in
3461 * the command line */
3462 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003463 && regname != Ctrl_A && regname != Ctrl_L
3464 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003465 return FAIL;
3466
3467 /* A register containing CTRL-R can cause an endless loop. Allow using
3468 * CTRL-C to break the loop. */
3469 line_breakcheck();
3470 if (got_int)
3471 return FAIL;
3472
3473#ifdef FEAT_CLIPBOARD
3474 regname = may_get_selection(regname);
3475#endif
3476
Bram Moolenaar438d1762018-09-30 17:11:48 +02003477 // Need to set "textlock" to avoid nasty things like going to another
3478 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003479 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003480 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003481 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003482
3483 if (i)
3484 {
3485 /* Got the value of a special register in "arg". */
3486 if (arg == NULL)
3487 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003488
3489 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3490 * part of the word. */
3491 p = arg;
3492 if (p_is && regname == Ctrl_W)
3493 {
3494 char_u *w;
3495 int len;
3496
3497 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003498 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003499 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003500 if (has_mbyte)
3501 {
3502 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3503 if (!vim_iswordc(mb_ptr2char(w - len)))
3504 break;
3505 w -= len;
3506 }
3507 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003508 {
3509 if (!vim_iswordc(w[-1]))
3510 break;
3511 --w;
3512 }
3513 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003514 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003515 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3516 p += len;
3517 }
3518
3519 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003520 if (allocated)
3521 vim_free(arg);
3522 return OK;
3523 }
3524
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003525 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003526}
3527
3528/*
3529 * Put a string on the command line.
3530 * When "literally" is TRUE, insert literally.
3531 * When "literally" is FALSE, insert as typed, but don't leave the command
3532 * line.
3533 */
3534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003535cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003536{
3537 int c, cv;
3538
3539 if (literally)
3540 put_on_cmdline(s, -1, TRUE);
3541 else
3542 while (*s != NUL)
3543 {
3544 cv = *s;
3545 if (cv == Ctrl_V && s[1])
3546 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003548 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003549 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003551 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3552 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003553#ifdef UNIX
3554 || c == intr_char
3555#endif
3556 || (c == Ctrl_BSL && *s == Ctrl_N))
3557 stuffcharReadbuff(Ctrl_V);
3558 stuffcharReadbuff(c);
3559 }
3560}
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#ifdef FEAT_WILDMENU
3563/*
3564 * Delete characters on the command line, from "from" to the current
3565 * position.
3566 */
3567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003568cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3571 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3572 ccline.cmdlen -= ccline.cmdpos - from;
3573 ccline.cmdpos = from;
3574}
3575#endif
3576
3577/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003578 * This function is called when the screen size changes and with incremental
3579 * search and in other situations where the command line may have been
3580 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
3582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003583redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003585 redrawcmdline_ex(TRUE);
3586}
3587
3588 void
3589redrawcmdline_ex(int do_compute_cmdrow)
3590{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (cmd_silent)
3592 return;
3593 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003594 if (do_compute_cmdrow)
3595 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 redrawcmd();
3597 cursorcmd();
3598}
3599
3600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003601redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 int i;
3604
3605 if (cmd_silent)
3606 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003607 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 msg_putchar(ccline.cmdfirstc);
3609 if (ccline.cmdprompt != NULL)
3610 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003611 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3613 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003614 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 --ccline.cmdindent;
3616 }
3617 else
3618 for (i = ccline.cmdindent; i > 0; --i)
3619 msg_putchar(' ');
3620}
3621
3622/*
3623 * Redraw what is currently on the command line.
3624 */
3625 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003626redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 if (cmd_silent)
3629 return;
3630
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003631 /* when 'incsearch' is set there may be no command line while redrawing */
3632 if (ccline.cmdbuff == NULL)
3633 {
3634 windgoto(cmdline_row, 0);
3635 msg_clr_eos();
3636 return;
3637 }
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 msg_start();
3640 redrawcmdprompt();
3641
3642 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3643 msg_no_more = TRUE;
3644 draw_cmdline(0, ccline.cmdlen);
3645 msg_clr_eos();
3646 msg_no_more = FALSE;
3647
3648 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003649 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003650 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /*
3653 * An emsg() before may have set msg_scroll. This is used in normal mode,
3654 * in cmdline mode we can reset them now.
3655 */
3656 msg_scroll = FALSE; /* next message overwrites cmdline */
3657
3658 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3659 * in cmdline mode */
3660 skip_redraw = FALSE;
3661}
3662
3663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003664compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003666 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 cmdline_row = Rows - 1;
3668 else
3669 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003670 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671}
3672
Bram Moolenaar66b51422019-08-18 21:44:12 +02003673 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003674cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (cmd_silent)
3677 return;
3678
3679#ifdef FEAT_RIGHTLEFT
3680 if (cmdmsg_rl)
3681 {
3682 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3683 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3684 if (msg_row <= 0)
3685 msg_row = Rows - 1;
3686 }
3687 else
3688#endif
3689 {
3690 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3691 msg_col = ccline.cmdspos % (int)Columns;
3692 if (msg_row >= Rows)
3693 msg_row = Rows - 1;
3694 }
3695
3696 windgoto(msg_row, msg_col);
3697#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003698 if (p_imst == IM_ON_THE_SPOT)
3699 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700#endif
3701#ifdef MCH_CURSOR_SHAPE
3702 mch_update_cursor();
3703#endif
3704}
3705
3706 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003707gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 msg_start();
3710#ifdef FEAT_RIGHTLEFT
3711 if (cmdmsg_rl)
3712 msg_col = Columns - 1;
3713 else
3714#endif
3715 msg_col = 0; /* always start in column 0 */
3716 if (clr) /* clear the bottom line(s) */
3717 msg_clr_eos(); /* will reset clear_cmdline */
3718 windgoto(cmdline_row, 0);
3719}
3720
3721/*
3722 * Check the word in front of the cursor for an abbreviation.
3723 * Called when the non-id character "c" has been entered.
3724 * When an abbreviation is recognized it is removed from the text with
3725 * backspaces and the replacement string is inserted, followed by "c".
3726 */
3727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003728ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003730 int spos = 0;
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3733 return FALSE;
3734
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003735 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3736 * Actually accepts any mark. */
3737 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3738 spos++;
3739 if (ccline.cmdlen - spos > 5
3740 && ccline.cmdbuff[spos] == '\''
3741 && ccline.cmdbuff[spos + 2] == ','
3742 && ccline.cmdbuff[spos + 3] == '\'')
3743 spos += 5;
3744 else
3745 /* check abbreviation from the beginning of the commandline */
3746 spos = 0;
3747
3748 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749}
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003752 * Escape special characters in "fname" for when used as a file name argument
3753 * after a Vim command, or, when "shell" is non-zero, a shell command.
3754 * Returns the result in allocated memory.
3755 */
3756 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003757vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003758{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003759 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003760#ifdef BACKSLASH_IN_FILENAME
3761 char_u buf[20];
3762 int j = 0;
3763
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003764 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003765 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003766 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003767 buf[j++] = *p;
3768 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003769 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003770#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003771 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3772 if (shell && csh_like_shell() && p != NULL)
3773 {
3774 char_u *s;
3775
3776 /* For csh and similar shells need to put two backslashes before '!'.
3777 * One is taken by Vim, one by the shell. */
3778 s = vim_strsave_escaped(p, (char_u *)"!");
3779 vim_free(p);
3780 p = s;
3781 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003782#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003783
3784 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3785 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003786 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003787 escape_fname(&p);
3788
3789 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003790}
3791
3792/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003793 * Put a backslash before the file name in "pp", which is in allocated memory.
3794 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003795 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003796escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003797{
3798 char_u *p;
3799
Bram Moolenaar964b3742019-05-24 18:54:09 +02003800 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003801 if (p != NULL)
3802 {
3803 p[0] = '\\';
3804 STRCPY(p + 1, *pp);
3805 vim_free(*pp);
3806 *pp = p;
3807 }
3808}
3809
3810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * For each file name in files[num_files]:
3812 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3813 */
3814 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003815tilde_replace(
3816 char_u *orig_pat,
3817 int num_files,
3818 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 int i;
3821 char_u *p;
3822
3823 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3824 {
3825 for (i = 0; i < num_files; ++i)
3826 {
3827 p = home_replace_save(NULL, files[i]);
3828 if (p != NULL)
3829 {
3830 vim_free(files[i]);
3831 files[i] = p;
3832 }
3833 }
3834 }
3835}
3836
3837/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003838 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003840 cmdline_info_T *
3841get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003843 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844}
3845
Bram Moolenaar064154c2016-03-19 22:50:43 +01003846#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003847/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003848 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003849 * ccline and put the previous value in prev_ccline.
3850 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003851 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003852get_ccline_ptr(void)
3853{
3854 if ((State & CMDLINE) == 0)
3855 return NULL;
3856 if (ccline.cmdbuff != NULL)
3857 return &ccline;
3858 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3859 return &prev_ccline;
3860 return NULL;
3861}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003862#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003863
Bram Moolenaar064154c2016-03-19 22:50:43 +01003864#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003865/*
3866 * Get the current command line in allocated memory.
3867 * Only works when the command line is being edited.
3868 * Returns NULL when something is wrong.
3869 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003870 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003871get_cmdline_str(void)
3872{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003873 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003874
Bram Moolenaaree91c332018-09-25 22:27:35 +02003875 if (cmdline_star > 0)
3876 return NULL;
3877 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003878 if (p == NULL)
3879 return NULL;
3880 return vim_strnsave(p->cmdbuff, p->cmdlen);
3881}
3882
3883/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003884 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003885 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003886 void
3887f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3888{
3889 rettv->v_type = VAR_STRING;
3890 rettv->vval.v_string = get_cmdline_str();
3891}
3892
3893/*
3894 * "getcmdpos()" function
3895 */
3896 void
3897f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003898{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003899 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003900
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003901 rettv->vval.v_number = 0;
3902 if (p != NULL)
3903 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003904}
3905
3906/*
3907 * Set the command line byte position to "pos". Zero is the first position.
3908 * Only works when the command line is being edited.
3909 * Returns 1 when failed, 0 when OK.
3910 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003911 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003912set_cmdline_pos(
3913 int pos)
3914{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003915 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003916
3917 if (p == NULL)
3918 return 1;
3919
3920 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3921 * changed the command line. */
3922 if (pos < 0)
3923 new_cmdpos = 0;
3924 else
3925 new_cmdpos = pos;
3926 return 0;
3927}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003928
3929/*
3930 * "setcmdpos()" function
3931 */
3932 void
3933f_setcmdpos(typval_T *argvars, typval_T *rettv)
3934{
3935 int pos = (int)tv_get_number(&argvars[0]) - 1;
3936
3937 if (pos >= 0)
3938 rettv->vval.v_number = set_cmdline_pos(pos);
3939}
3940
3941/*
3942 * "getcmdtype()" function
3943 */
3944 void
3945f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
3946{
3947 rettv->v_type = VAR_STRING;
3948 rettv->vval.v_string = alloc(2);
3949 if (rettv->vval.v_string != NULL)
3950 {
3951 rettv->vval.v_string[0] = get_cmdline_type();
3952 rettv->vval.v_string[1] = NUL;
3953 }
3954}
3955
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003956#endif
3957
3958#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
3959/*
3960 * Get the current command-line type.
3961 * Returns ':' or '/' or '?' or '@' or '>' or '-'
3962 * Only works when the command line is being edited.
3963 * Returns NUL when something is wrong.
3964 */
3965 int
3966get_cmdline_type(void)
3967{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003968 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003969
3970 if (p == NULL)
3971 return NUL;
3972 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01003973 return
3974# ifdef FEAT_EVAL
3975 (p->input_fn) ? '@' :
3976# endif
3977 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003978 return p->cmdfirstc;
3979}
3980#endif
3981
Bram Moolenaard7663c22019-08-06 21:59:57 +02003982/*
3983 * Return the first character of the current command line.
3984 */
3985 int
3986get_cmdline_firstc(void)
3987{
3988 return ccline.cmdfirstc;
3989}
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991/*
3992 * Get indices "num1,num2" that specify a range within a list (not a range of
3993 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
3994 * Returns OK if parsed successfully, otherwise FAIL.
3995 */
3996 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003997get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 int len;
4000 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004001 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 *str = skipwhite(*str);
4004 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4005 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004006 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *str += len;
4008 *num1 = (int)num;
4009 first = TRUE;
4010 }
4011 *str = skipwhite(*str);
4012 if (**str == ',') /* parse "to" part of range */
4013 {
4014 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02004015 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (len > 0)
4017 {
4018 *num2 = (int)num;
4019 *str = skipwhite(*str + len);
4020 }
4021 else if (!first) /* no number given at all */
4022 return FAIL;
4023 }
4024 else if (first) /* only one number given */
4025 *num2 = *num1;
4026 return OK;
4027}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#if defined(FEAT_CMDWIN) || defined(PROTO)
4030/*
4031 * Open a window on the current command line and history. Allow editing in
4032 * the window. Returns when the window is closed.
4033 * Returns:
4034 * CR if the command is to be executed
4035 * Ctrl_C if it is to be abandoned
4036 * K_IGNORE if editing continues
4037 */
4038 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004039open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004041 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004043 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 win_T *wp;
4045 int i;
4046 linenr_T lnum;
4047 int histtype;
4048 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 int save_restart_edit = restart_edit;
4050 int save_State = State;
4051 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004052#ifdef FEAT_RIGHTLEFT
4053 int save_cmdmsg_rl = cmdmsg_rl;
4054#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004055#ifdef FEAT_FOLDING
4056 int save_KeyTyped;
4057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 /* Can't do this recursively. Can't do it when typing a password. */
4060 if (cmdwin_type != 0
4061# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4062 || cmdline_star > 0
4063# endif
4064 )
4065 {
4066 beep_flush();
4067 return K_IGNORE;
4068 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004069 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004071 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 win_size_save(&winsizes);
4073
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004074 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4075 // command line window, but we don't want the popup menu then.
4076 pum_undisplay();
4077
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004078 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004079 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004080 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004081
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004082 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4084 {
4085 beep_flush();
4086 return K_IGNORE;
4087 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004088 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004090 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004091 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004092 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004095#ifdef FEAT_FOLDING
4096 curwin->w_p_fen = FALSE;
4097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004099 curwin->w_p_rl = cmdmsg_rl;
4100 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004102 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004104 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004105 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004107 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004108 need_wait_return = FALSE;
4109
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004110 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4112 {
4113 if (p_wc == TAB)
4114 {
4115 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4116 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4117 }
4118 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4119 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004120 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004122 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4123 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004124 curbuf->b_p_tw = 0;
4125
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004126 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004128 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004130 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (i >= 0)
4132 {
4133 lnum = 0;
4134 do
4135 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004136 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004138 if (get_histentry(histtype)[i].hisstr != NULL)
4139 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 (colnr_T)0, FALSE);
4141 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004142 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145
4146 /* Replace the empty last line with the current command-line and put the
4147 * cursor there. */
4148 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4149 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4150 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004151 changed_line_abv_curs();
4152 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004153 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar23324a02019-10-01 17:39:04 +02004155 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 exmode_active = 0;
4157
4158 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaar23324a02019-10-01 17:39:04 +02004161 // Reset here so it can be set by a CmdWinEnter autocommand.
4162 cmdwin_result = 0;
4163
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004164 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004165 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004166 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004167 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169 i = RedrawingDisabled;
4170 RedrawingDisabled = 0;
4171
4172 /*
4173 * Call the main loop until <CR> or CTRL-C is typed.
4174 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004175 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 RedrawingDisabled = i;
4178
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004179# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004180 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004181# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004182
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004183 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004184 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004185
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004186# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004187 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004188 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189# endif
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 exmode_active = save_exmode;
4193
Bram Moolenaarf9821062008-06-20 16:31:07 +00004194 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004196 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else
4202 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004203# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 /* autocmds may abort script processing */
4205 if (aborting() && cmdwin_result != K_IGNORE)
4206 cmdwin_result = Ctrl_C;
4207# endif
4208 /* Set the new command line from the cmdline buffer. */
4209 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004210 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004212 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4213
4214 if (histtype == HIST_CMD)
4215 {
4216 /* Execute the command directly. */
4217 ccline.cmdbuff = vim_strsave((char_u *)p);
4218 cmdwin_result = CAR;
4219 }
4220 else
4221 {
4222 /* First need to cancel what we were doing. */
4223 ccline.cmdbuff = NULL;
4224 stuffcharReadbuff(':');
4225 stuffReadbuff((char_u *)p);
4226 stuffcharReadbuff(CAR);
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 else if (cmdwin_result == K_XF2) /* :qa typed */
4230 {
4231 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4232 cmdwin_result = CAR;
4233 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004234 else if (cmdwin_result == Ctrl_C)
4235 {
4236 /* :q or :close, don't execute any command
4237 * and don't modify the cmd window. */
4238 ccline.cmdbuff = NULL;
4239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 else
4241 ccline.cmdbuff = vim_strsave(ml_get_curline());
4242 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004243 {
4244 ccline.cmdbuff = vim_strsave((char_u *)"");
4245 ccline.cmdlen = 0;
4246 ccline.cmdbufflen = 1;
4247 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
4251 {
4252 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4253 ccline.cmdbufflen = ccline.cmdlen + 1;
4254 ccline.cmdpos = curwin->w_cursor.col;
4255 if (ccline.cmdpos > ccline.cmdlen)
4256 ccline.cmdpos = ccline.cmdlen;
4257 if (cmdwin_result == K_IGNORE)
4258 {
4259 set_cmdspos_cursor();
4260 redrawcmd();
4261 }
4262 }
4263
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004264# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004265 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004266 curwin->w_p_cole = 0;
4267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004269 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 win_goto(old_curwin);
4271 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004272
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004273 // win_close() may have already wiped the buffer when 'bh' is
4274 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004275 if (bufref_valid(&bufref))
4276 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004278 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281
4282 ga_clear(&winsizes);
4283 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004284# ifdef FEAT_RIGHTLEFT
4285 cmdmsg_rl = save_cmdmsg_rl;
4286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 return cmdwin_result;
4292}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004293#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295/*
4296 * Used for commands that either take a simple command string argument, or:
4297 * cmd << endmarker
4298 * {script}
4299 * endmarker
4300 * Returns a pointer to allocated memory with {script} or NULL.
4301 */
4302 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004303script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
4305 char_u *theline;
4306 char *end_pattern = NULL;
4307 char dot[] = ".";
4308 garray_T ga;
4309
4310 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4311 return NULL;
4312
4313 ga_init2(&ga, 1, 0x400);
4314
4315 if (cmd[2] != NUL)
4316 end_pattern = (char *)skipwhite(cmd + 2);
4317 else
4318 end_pattern = dot;
4319
4320 for (;;)
4321 {
4322 theline = eap->getline(
4323#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004324 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004326 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004329 {
4330 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 ga_concat(&ga, theline);
4335 ga_append(&ga, '\n');
4336 vim_free(theline);
4337 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004338 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 return (char_u *)ga.ga_data;
4341}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004342
4343#if defined(FEAT_EVAL) || defined(PROTO)
4344/*
4345 * This function is used by f_input() and f_inputdialog() functions. The third
4346 * argument to f_input() specifies the type of completion to use at the
4347 * prompt. The third argument to f_inputdialog() specifies the value to return
4348 * when the user cancels the prompt.
4349 */
4350 void
4351get_user_input(
4352 typval_T *argvars,
4353 typval_T *rettv,
4354 int inputdialog,
4355 int secret)
4356{
4357 char_u *prompt = tv_get_string_chk(&argvars[0]);
4358 char_u *p = NULL;
4359 int c;
4360 char_u buf[NUMBUFLEN];
4361 int cmd_silent_save = cmd_silent;
4362 char_u *defstr = (char_u *)"";
4363 int xp_type = EXPAND_NOTHING;
4364 char_u *xp_arg = NULL;
4365
4366 rettv->v_type = VAR_STRING;
4367 rettv->vval.v_string = NULL;
4368
4369#ifdef NO_CONSOLE_INPUT
4370 // While starting up, there is no place to enter text. When running tests
4371 // with --not-a-term we assume feedkeys() will be used.
4372 if (no_console_input() && !is_not_a_term())
4373 return;
4374#endif
4375
4376 cmd_silent = FALSE; // Want to see the prompt.
4377 if (prompt != NULL)
4378 {
4379 // Only the part of the message after the last NL is considered as
4380 // prompt for the command line
4381 p = vim_strrchr(prompt, '\n');
4382 if (p == NULL)
4383 p = prompt;
4384 else
4385 {
4386 ++p;
4387 c = *p;
4388 *p = NUL;
4389 msg_start();
4390 msg_clr_eos();
4391 msg_puts_attr((char *)prompt, get_echo_attr());
4392 msg_didout = FALSE;
4393 msg_starthere();
4394 *p = c;
4395 }
4396 cmdline_row = msg_row;
4397
4398 if (argvars[1].v_type != VAR_UNKNOWN)
4399 {
4400 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4401 if (defstr != NULL)
4402 stuffReadbuffSpec(defstr);
4403
4404 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4405 {
4406 char_u *xp_name;
4407 int xp_namelen;
4408 long argt;
4409
4410 // input() with a third argument: completion
4411 rettv->vval.v_string = NULL;
4412
4413 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4414 if (xp_name == NULL)
4415 return;
4416
4417 xp_namelen = (int)STRLEN(xp_name);
4418
4419 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4420 &xp_arg) == FAIL)
4421 return;
4422 }
4423 }
4424
4425 if (defstr != NULL)
4426 {
4427 int save_ex_normal_busy = ex_normal_busy;
4428
4429 ex_normal_busy = 0;
4430 rettv->vval.v_string =
4431 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4432 xp_type, xp_arg);
4433 ex_normal_busy = save_ex_normal_busy;
4434 }
4435 if (inputdialog && rettv->vval.v_string == NULL
4436 && argvars[1].v_type != VAR_UNKNOWN
4437 && argvars[2].v_type != VAR_UNKNOWN)
4438 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4439 &argvars[2], buf));
4440
4441 vim_free(xp_arg);
4442
4443 // since the user typed this, no need to wait for return
4444 need_wait_return = FALSE;
4445 msg_didout = FALSE;
4446 }
4447 cmd_silent = cmd_silent_save;
4448}
4449#endif