blob: 5816f3f9e7e1fcd22d4b5bf5e45fe2b3acb25602 [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;
376#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200377 int next_char;
378 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200379 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200380
Bram Moolenaar198cb662018-09-06 21:44:17 +0200381 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100382 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200383 save_last_search_pattern();
384
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200385 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200386 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200387 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200388 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200389 if (did_do_incsearch && vpeekc() == NUL)
390 // may have skipped a redraw, do it now
391 redrawcmd();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200392 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200393 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200394
395 // If there is a character waiting, search and redraw later.
396 if (char_avail())
397 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200398 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200399 is_state->incsearch_postponed = TRUE;
400 return;
401 }
402 is_state->incsearch_postponed = FALSE;
403
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 if (search_first_line == 0)
405 // start at the original cursor position
406 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100407 else if (search_first_line > curbuf->b_ml.ml_line_count)
408 {
409 // start after the last line
410 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
411 curwin->w_cursor.col = MAXCOL;
412 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200413 else
414 {
415 // start at the first line in the range
416 curwin->w_cursor.lnum = search_first_line;
417 curwin->w_cursor.col = 0;
418 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200419
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200420 // Use the previous pattern for ":s//".
421 next_char = ccline.cmdbuff[skiplen + patlen];
422 use_last_pat = patlen == 0 && skiplen > 0
423 && ccline.cmdbuff[skiplen - 1] == next_char;
424
425 // If there is no pattern, don't do anything.
426 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200427 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200428 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200429 set_no_hlsearch(TRUE); // turn off previous highlight
430 redraw_all_later(SOME_VALID);
431 }
432 else
433 {
434 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
435
436 cursor_off(); // so the user knows we're busy
437 out_flush();
438 ++emsg_off; // so it doesn't beep if bad expr
439#ifdef FEAT_RELTIME
440 // Set the time limit to half a second.
441 profile_setlimit(500L, &tm);
442#endif
443 if (!p_hls)
444 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200445 if (search_first_line != 0)
446 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200447 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200448 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200449 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200450#ifdef FEAT_RELTIME
451 &tm, NULL
452#else
453 NULL, NULL
454#endif
455 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200456 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457 --emsg_off;
458
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200459 if (curwin->w_cursor.lnum < search_first_line
460 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200461 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200462 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200463 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200464 curwin->w_cursor = is_state->search_start;
465 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200467 // if interrupted while searching, behave like it failed
468 if (got_int)
469 {
470 (void)vpeekc(); // remove <C-C> from input stream
471 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200472 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 }
474 else if (char_avail())
475 // cancelled searching because a char was typed
476 is_state->incsearch_postponed = TRUE;
477 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200478 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 highlight_match = TRUE; // highlight position
480 else
481 highlight_match = FALSE; // remove highlight
482
483 // First restore the old curwin values, so the screen is positioned in the
484 // same way as the actual search command.
485 restore_viewstate(&is_state->old_viewstate);
486 changed_cline_bef_curs();
487 update_topline();
488
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200489 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 {
491 pos_T save_pos = curwin->w_cursor;
492
493 is_state->match_start = curwin->w_cursor;
494 set_search_match(&curwin->w_cursor);
495 validate_cursor();
496 end_pos = curwin->w_cursor;
497 is_state->match_end = end_pos;
498 curwin->w_cursor = save_pos;
499 }
500 else
501 end_pos = curwin->w_cursor; // shutup gcc 4
502
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200503 // Disable 'hlsearch' highlighting if the pattern matches everything.
504 // Avoids a flash when typing "foo\|".
505 if (!use_last_pat)
506 {
507 next_char = ccline.cmdbuff[skiplen + patlen];
508 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200509 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
510 {
511 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200512 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200513 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200514 ccline.cmdbuff[skiplen + patlen] = next_char;
515 }
516
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200517 validate_cursor();
518 // May redraw the status line to show the cursor position.
519 if (p_ru && curwin->w_status_height > 0)
520 curwin->w_redr_status = TRUE;
521
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200522 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200523 restore_last_search_pattern();
524
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200525 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
526 // end of the pattern, e.g. for ":s/pat/".
527 if (ccline.cmdbuff[skiplen + patlen] != NUL)
528 curwin->w_cursor = is_state->search_start;
529 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200530 curwin->w_cursor = end_pos;
531
532 msg_starthere();
533 redrawcmdline();
534 is_state->did_incsearch = TRUE;
535}
536
537/*
538 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
539 * or previous match.
540 * Returns FAIL when jumping to cmdline_not_changed;
541 */
542 static int
543may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544 int firstc,
545 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200546 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200547 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200548{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200549 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200550 pos_T t;
551 char_u *pat;
552 int search_flags = SEARCH_NOOF;
553 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200554 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200555
Bram Moolenaar198cb662018-09-06 21:44:17 +0200556 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100557 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200558 save_last_search_pattern();
559
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200561 {
562 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200563 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200564 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200566 {
567 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200569 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200570
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200571 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200572 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200573 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200574 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200575 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200576 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200577 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200578 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200579
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200580 cursor_off();
581 out_flush();
582 if (c == Ctrl_G)
583 {
584 t = is_state->match_end;
585 if (LT_POS(is_state->match_start, is_state->match_end))
586 // Start searching at the end of the match not at the beginning of
587 // the next column.
588 (void)decl(&t);
589 search_flags += SEARCH_COL;
590 }
591 else
592 t = is_state->match_start;
593 if (!p_hls)
594 search_flags += SEARCH_KEEP;
595 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 save = pat[patlen];
597 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100598 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 c == Ctrl_G ? FORWARD : BACKWARD,
600 pat, count, search_flags,
601 RE_SEARCH, 0, NULL, NULL);
602 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200603 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 if (i)
605 {
606 is_state->search_start = is_state->match_start;
607 is_state->match_end = t;
608 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 {
611 // Move just before the current match, so that when nv_search
612 // finishes the cursor will be put back on the match.
613 is_state->search_start = t;
614 (void)decl(&is_state->search_start);
615 }
616 else if (c == Ctrl_G && firstc == '?')
617 {
618 // Move just after the current match, so that when nv_search
619 // finishes the cursor will be put back on the match.
620 is_state->search_start = t;
621 (void)incl(&is_state->search_start);
622 }
623 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
624 {
625 // wrap around
626 is_state->search_start = t;
627 if (firstc == '?')
628 (void)incl(&is_state->search_start);
629 else
630 (void)decl(&is_state->search_start);
631 }
632
633 set_search_match(&is_state->match_end);
634 curwin->w_cursor = is_state->match_start;
635 changed_cline_bef_curs();
636 update_topline();
637 validate_cursor();
638 highlight_match = TRUE;
639 save_viewstate(&is_state->old_viewstate);
640 update_screen(NOT_VALID);
641 redrawcmdline();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200642 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643 }
644 else
645 vim_beep(BO_ERROR);
646 restore_last_search_pattern();
647 return FAIL;
648}
649
650/*
651 * When CTRL-L typed: add character from the match to the pattern.
652 * May set "*c" to the added character.
653 * Return OK when jumping to cmdline_not_changed.
654 */
655 static int
656may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
657{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200658 int skiplen, patlen;
659
Bram Moolenaar198cb662018-09-06 21:44:17 +0200660 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100661 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200662 save_last_search_pattern();
663
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200664 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200665 {
666 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200667 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200668 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100669 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200670
671 // Add a character from under the cursor for 'incsearch'.
672 if (is_state->did_incsearch)
673 {
674 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200675 *c = gchar_cursor();
676 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200677 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 // If 'ignorecase' and 'smartcase' are set and the
679 // command line has no uppercase characters, convert
680 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200681 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200682 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200683 if (*c == firstc || vim_strchr((char_u *)(
684 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200685 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200686 // put a backslash before special characters
687 stuffcharReadbuff(*c);
688 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200689 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200690 // add any composing characters
691 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
692 {
693 int save_c = *c;
694
695 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
696 {
697 curwin->w_cursor.col += mb_char2len(*c);
698 *c = gchar_cursor();
699 stuffcharReadbuff(*c);
700 }
701 *c = save_c;
702 }
703 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200704 }
705 }
706 return OK;
707}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200708#endif
709
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200710#ifdef FEAT_ARABIC
711/*
712 * Return TRUE if the command line has an Arabic character at or after "start"
713 * for "len" bytes.
714 */
715 static int
716cmdline_has_arabic(int start, int len)
717{
718 int j;
719 int mb_l;
720 int u8c;
721 char_u *p;
722 int u8cc[MAX_MCO];
723
724 if (!enc_utf8)
725 return FALSE;
726
727 for (j = start; j < start + len; j += mb_l)
728 {
729 p = ccline.cmdbuff + j;
730 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
731 mb_l = utfc_ptr2len_len(p, start + len - j);
732 if (ARABIC_CHAR(u8c))
733 return TRUE;
734 }
735 return FALSE;
736}
737#endif
738
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200739 void
740cmdline_init(void)
741{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200742 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200743}
744
Bram Moolenaar66216052017-12-16 16:33:44 +0100745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * getcmdline() - accept a command line starting with firstc.
747 *
748 * firstc == ':' get ":" command line.
749 * firstc == '/' or '?' get search pattern
750 * firstc == '=' get expression
751 * firstc == '@' get text for input() function
752 * firstc == '>' get text for debug mode
753 * firstc == NUL get text for :insert command
754 * firstc == -1 like NUL, and break on CTRL-C
755 *
756 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
757 * command line.
758 *
759 * Careful: getcmdline() can be called recursively!
760 *
761 * Return pointer to allocated string if there is a commandline, NULL
762 * otherwise.
763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100765getcmdline(
766 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200767 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200768 int indent, // indent for inside conditionals
769 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200770{
771 return getcmdline_int(firstc, count, indent, TRUE);
772}
773
774 static char_u *
775getcmdline_int(
776 int firstc,
777 long count UNUSED, // only used for incremental search
778 int indent, // indent for inside conditionals
779 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 int c;
782 int i;
783 int j;
784 int gotesc = FALSE; /* TRUE when <ESC> just typed */
785 int do_abbr; /* when TRUE check for abbr. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 char_u *lookfor = NULL; /* string to match */
787 int hiscnt; /* current history line in use */
788 int histype; /* history type to be used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200790 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792 int did_wild_list = FALSE; /* did wild_list() recently */
793 int wim_index = 0; /* index in wim_flags[] */
794 int res;
795 int save_msg_scroll = msg_scroll;
796 int save_State = State; /* remember State when called */
797 int some_key_typed = FALSE; /* one of the keys was typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /* mouse drag and release events are ignored, unless they are
799 * preceded with a mouse down event */
800 int ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#ifdef FEAT_EVAL
802 int break_ctrl_c = FALSE;
803#endif
804 expand_T xpc;
805 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200806 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200807 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200808 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar438d1762018-09-30 17:11:48 +0200810 if (ccline.cmdbuff != NULL)
811 {
812 // Being called recursively. Since ccline is global, we need to save
813 // the current buffer and restore it when returning.
814 save_cmdline(&save_ccline);
815 did_save_ccline = TRUE;
816 }
817 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200818 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifdef FEAT_EVAL
821 if (firstc == -1)
822 {
823 firstc = NUL;
824 break_ctrl_c = TRUE;
825 }
826#endif
827#ifdef FEAT_RIGHTLEFT
828 /* start without Hebrew mapping for a command line */
829 if (firstc == ':' || firstc == '=' || firstc == '>')
830 cmd_hkmap = 0;
831#endif
832
833 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200836 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#endif
838
839 /*
840 * set some variables for redrawcmd()
841 */
842 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000843 ccline.cmdindent = (firstc > 0 ? indent : 0);
844
845 /* alloc initial ccline.cmdbuff */
846 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200848 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 ccline.cmdlen = ccline.cmdpos = 0;
850 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100851 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000853 /* autoindent for :insert and :append */
854 if (firstc <= 0)
855 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200856 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000857 ccline.cmdbuff[indent] = NUL;
858 ccline.cmdpos = indent;
859 ccline.cmdspos = indent;
860 ccline.cmdlen = indent;
861 }
862
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000864 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866#ifdef FEAT_RIGHTLEFT
867 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
868 && (firstc == '/' || firstc == '?'))
869 cmdmsg_rl = TRUE;
870 else
871 cmdmsg_rl = FALSE;
872#endif
873
874 redir_off = TRUE; /* don't redirect the typed command */
875 if (!cmd_silent)
876 {
877 i = msg_scrolled;
878 msg_scrolled = 0; /* avoid wait_return message */
879 gotocmdline(TRUE);
880 msg_scrolled += i;
881 redrawcmdprompt(); /* draw prompt or indent */
882 set_cmdspos();
883 }
884 xpc.xp_context = EXPAND_NOTHING;
885 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000886#ifndef BACKSLASH_IN_FILENAME
887 xpc.xp_shell = FALSE;
888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000890#if defined(FEAT_EVAL)
891 if (ccline.input_fn)
892 {
893 xpc.xp_context = ccline.xp_context;
894 xpc.xp_pattern = ccline.cmdbuff;
895 xpc.xp_arg = ccline.xp_arg;
896 }
897#endif
898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 /*
900 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
901 * doing ":@0" when register 0 doesn't contain a CR.
902 */
903 msg_scroll = FALSE;
904
905 State = CMDLINE;
906
907 if (firstc == '/' || firstc == '?' || firstc == '@')
908 {
909 /* Use ":lmap" mappings for search pattern and input(). */
910 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
911 b_im_ptr = &curbuf->b_p_iminsert;
912 else
913 b_im_ptr = &curbuf->b_p_imsearch;
914 if (*b_im_ptr == B_IMODE_LMAP)
915 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100916#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 im_set_active(*b_im_ptr == B_IMODE_IM);
918#endif
919 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100920#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 else if (p_imcmdline)
922 im_set_active(TRUE);
923#endif
924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#ifdef CURSOR_SHAPE
927 ui_cursor_shape(); /* may show different cursor shape */
928#endif
929
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000930 /* When inside an autocommand for writing "exiting" may be set and
931 * terminal mode set to cooked. Need to set raw mode here then. */
932 settmode(TMODE_RAW);
933
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200934 /* Trigger CmdlineEnter autocommands. */
935 cmdline_type = firstc == NUL ? '-' : firstc;
936 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200939 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000943 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#endif
945
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200946 /* If something above caused an error, reset the flags, we do want to type
947 * and execute commands. Display may be messed up a bit. */
948 if (did_emsg)
949 redrawcmd();
950 did_emsg = FALSE;
951 got_int = FALSE;
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /*
954 * Collect the command string, handling editing keys.
955 */
956 for (;;)
957 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000958 redir_off = TRUE; /* Don't redirect the typed command.
959 Repeated, because a ":redir" inside
960 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef USE_ON_FLY_SCROLL
962 dont_scroll = FALSE; /* allow scrolling here */
963#endif
964 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
965
Bram Moolenaar72532d32018-04-07 19:09:09 +0200966 did_emsg = FALSE; /* There can't really be a reason why an error
967 that occurs while typing a command should
968 cause the command not to be executed. */
969
Bram Moolenaar8aeec402019-09-15 23:02:04 +0200970 // Trigger SafeState if nothing is pending.
971 may_trigger_safestate(xpc.xp_numfiles <= 0);
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000974
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100975 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
976 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000977 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000978 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100979 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000980
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (KeyTyped)
982 {
983 some_key_typed = TRUE;
984#ifdef FEAT_RIGHTLEFT
985 if (cmd_hkmap)
986 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 if (cmdmsg_rl && !KeyStuffed)
988 {
989 /* Invert horizontal movements and operations. Only when
990 * typed by the user directly, not when the result of a
991 * mapping. */
992 switch (c)
993 {
994 case K_RIGHT: c = K_LEFT; break;
995 case K_S_RIGHT: c = K_S_LEFT; break;
996 case K_C_RIGHT: c = K_C_LEFT; break;
997 case K_LEFT: c = K_RIGHT; break;
998 case K_S_LEFT: c = K_S_RIGHT; break;
999 case K_C_LEFT: c = K_C_RIGHT; break;
1000 }
1001 }
1002#endif
1003 }
1004
1005 /*
1006 * Ignore got_int when CTRL-C was typed here.
1007 * Don't ignore it in :global, we really need to break then, e.g., for
1008 * ":g/pat/normal /pat" (without the <CR>).
1009 * Don't ignore it for the input() function.
1010 */
1011 if ((c == Ctrl_C
1012#ifdef UNIX
1013 || c == intr_char
1014#endif
1015 )
1016#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1017 && firstc != '@'
1018#endif
1019#ifdef FEAT_EVAL
1020 && !break_ctrl_c
1021#endif
1022 && !global_busy)
1023 got_int = FALSE;
1024
Bram Moolenaard7663c22019-08-06 21:59:57 +02001025 // free old command line when finished moving around in the history
1026 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001028 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001029 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && c != K_PAGEDOWN && c != K_PAGEUP
1031 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001032 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001034 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001037 * When there are matching completions to select <S-Tab> works like
1038 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001040 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 c = Ctrl_P;
1042
1043#ifdef FEAT_WILDMENU
1044 /* Special translations for 'wildmenu' */
1045 if (did_wild_list && p_wmnu)
1046 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001047 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 c = Ctrl_N;
1051 }
1052 /* Hitting CR after "emenu Name.": complete submenu */
1053 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1054 && ccline.cmdpos > 1
1055 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1056 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1057 && (c == '\n' || c == '\r' || c == K_KENTER))
1058 c = K_DOWN;
1059#endif
1060
1061 /* free expanded names when finished walking through matches */
1062 if (xpc.xp_numfiles != -1
1063 && !(c == p_wc && KeyTyped) && c != p_wcm
1064 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1065 && c != Ctrl_L)
1066 {
1067 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1068 did_wild_list = FALSE;
1069#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001070 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071#endif
1072 xpc.xp_context = EXPAND_NOTHING;
1073 wim_index = 0;
1074#ifdef FEAT_WILDMENU
1075 if (p_wmnu && wild_menu_showing != 0)
1076 {
1077 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001078 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001079
1080 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001081 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 if (wild_menu_showing == WM_SCROLLED)
1084 {
1085 /* Entered command line, move it up */
1086 cmdline_row--;
1087 redrawcmd();
1088 }
1089 else if (save_p_ls != -1)
1090 {
1091 /* restore 'laststatus' and 'winminheight' */
1092 p_ls = save_p_ls;
1093 p_wmh = save_p_wmh;
1094 last_status(FALSE);
1095 update_screen(VALID); /* redraw the screen NOW */
1096 redrawcmd();
1097 save_p_ls = -1;
1098 }
1099 else
1100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 redraw_statuslines();
1103 }
1104 KeyTyped = skt;
1105 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001106 if (ccline.input_fn)
1107 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109#endif
1110 }
1111
1112#ifdef FEAT_WILDMENU
1113 /* Special translations for 'wildmenu' */
1114 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1115 {
1116 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001117 if (c == K_DOWN && ccline.cmdpos > 0
1118 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001120 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 /* Hitting <Up>: Remove one submenu name in front of the
1123 * cursor */
1124 int found = FALSE;
1125
1126 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1127 i = 0;
1128 while (--j > 0)
1129 {
1130 /* check for start of menu name */
1131 if (ccline.cmdbuff[j] == ' '
1132 && ccline.cmdbuff[j - 1] != '\\')
1133 {
1134 i = j + 1;
1135 break;
1136 }
1137 /* check for start of submenu name */
1138 if (ccline.cmdbuff[j] == '.'
1139 && ccline.cmdbuff[j - 1] != '\\')
1140 {
1141 if (found)
1142 {
1143 i = j + 1;
1144 break;
1145 }
1146 else
1147 found = TRUE;
1148 }
1149 }
1150 if (i > 0)
1151 cmdline_del(i);
1152 c = p_wc;
1153 xpc.xp_context = EXPAND_NOTHING;
1154 }
1155 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001156 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001157 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001158 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 char_u upseg[5];
1161
1162 upseg[0] = PATHSEP;
1163 upseg[1] = '.';
1164 upseg[2] = '.';
1165 upseg[3] = PATHSEP;
1166 upseg[4] = NUL;
1167
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001168 if (c == K_DOWN
1169 && ccline.cmdpos > 0
1170 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1171 && (ccline.cmdpos < 3
1172 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1174 {
1175 /* go down a directory */
1176 c = p_wc;
1177 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001178 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 /* If in a direct ancestor, strip off one ../ to go down */
1181 int found = FALSE;
1182
1183 j = ccline.cmdpos;
1184 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1185 while (--j > i)
1186 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001187 if (has_mbyte)
1188 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (vim_ispathsep(ccline.cmdbuff[j]))
1190 {
1191 found = TRUE;
1192 break;
1193 }
1194 }
1195 if (found
1196 && ccline.cmdbuff[j - 1] == '.'
1197 && ccline.cmdbuff[j - 2] == '.'
1198 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1199 {
1200 cmdline_del(j - 2);
1201 c = p_wc;
1202 }
1203 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001204 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
1206 /* go up a directory */
1207 int found = FALSE;
1208
1209 j = ccline.cmdpos - 1;
1210 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1211 while (--j > i)
1212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (has_mbyte)
1214 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (vim_ispathsep(ccline.cmdbuff[j])
1216#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001217 && vim_strchr((char_u *)" *?[{`$%#",
1218 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
1220 )
1221 {
1222 if (found)
1223 {
1224 i = j + 1;
1225 break;
1226 }
1227 else
1228 found = TRUE;
1229 }
1230 }
1231
1232 if (!found)
1233 j = i;
1234 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1235 j += 4;
1236 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1237 && j == i)
1238 j += 3;
1239 else
1240 j = 0;
1241 if (j > 0)
1242 {
1243 /* TODO this is only for DOS/UNIX systems - need to put in
1244 * machine-specific stuff here and in upseg init */
1245 cmdline_del(j);
1246 put_on_cmdline(upseg + 1, 3, FALSE);
1247 }
1248 else if (ccline.cmdpos > i)
1249 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001250
1251 /* Now complete in the new directory. Set KeyTyped in case the
1252 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001254 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258#endif /* FEAT_WILDMENU */
1259
1260 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1261 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1262 if (c == Ctrl_BSL)
1263 {
1264 ++no_mapping;
1265 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001266 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 --no_mapping;
1268 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001269 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1270 * is in a mapping. */
1271 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001272 || (ccline.cmdfirstc == '=' && KeyTyped)
1273#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001274 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001275#endif
1276 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
1278 vungetc(c);
1279 c = Ctrl_BSL;
1280 }
1281#ifdef FEAT_EVAL
1282 else if (c == 'e')
1283 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001284 char_u *p = NULL;
1285 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 /*
1288 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001289 * Need to save and restore the current command line, to be
1290 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 */
1292 if (ccline.cmdpos == ccline.cmdlen)
1293 new_cmdpos = 99999; /* keep it at the end */
1294 else
1295 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (c == '=')
1299 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001300 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001301 * to avoid nasty things like going to another buffer when
1302 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001303 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001305 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001306
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001307 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001309 len = (int)STRLEN(p);
1310 if (realloc_cmdbuff(len + 1) == OK)
1311 {
1312 ccline.cmdlen = len;
1313 STRCPY(ccline.cmdbuff, p);
1314 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001316 /* Restore the cursor or use the position set with
1317 * set_cmdline_pos(). */
1318 if (new_cmdpos > ccline.cmdlen)
1319 ccline.cmdpos = ccline.cmdlen;
1320 else
1321 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001323 KeyTyped = FALSE; /* Don't do p_wc completion. */
1324 redrawcmd();
1325 goto cmdline_changed;
1326 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001327 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001331 got_int = FALSE; /* don't abandon the command line */
1332 did_emsg = FALSE;
1333 emsg_on_display = FALSE;
1334 redrawcmd();
1335 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337#endif
1338 else
1339 {
1340 if (c == Ctrl_G && p_im && restart_edit == 0)
1341 restart_edit = 'a';
1342 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1343 in history */
1344 goto returncmd; /* back to Normal mode */
1345 }
1346 }
1347
1348#ifdef FEAT_CMDWIN
1349 if (c == cedit_key || c == K_CMDWIN)
1350 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001351 if (ex_normal_busy == 0 && got_int == FALSE)
1352 {
1353 /*
1354 * Open a window to edit the command line (and history).
1355 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001356 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001357 some_key_typed = TRUE;
1358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360# ifdef FEAT_DIGRAPHS
1361 else
1362# endif
1363#endif
1364#ifdef FEAT_DIGRAPHS
1365 c = do_digraph(c);
1366#endif
1367
1368 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1369 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1370 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001371 /* In Ex mode a backslash escapes a newline. */
1372 if (exmode_active
1373 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001374 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001375 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001376 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001378 if (c == K_KENTER)
1379 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001381 else
1382 {
1383 gotesc = FALSE; /* Might have typed ESC previously, don't
1384 truncate the cmdline now. */
1385 if (ccheck_abbr(c + ABBR_OFF))
1386 goto cmdline_changed;
1387 if (!cmd_silent)
1388 {
1389 windgoto(msg_row, 0);
1390 out_flush();
1391 }
1392 break;
1393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 /*
1397 * Completion for 'wildchar' or 'wildcharm' key.
1398 * - hitting <ESC> twice means: abandon command line.
1399 * - wildcard expansion is only done when the 'wildchar' key is really
1400 * typed, not when it comes from a macro
1401 */
1402 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1403 {
1404 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1405 {
1406 /* if 'wildmode' contains "list" may still need to list */
1407 if (xpc.xp_numfiles > 1
1408 && !did_wild_list
1409 && (wim_flags[wim_index] & WIM_LIST))
1410 {
1411 (void)showmatches(&xpc, FALSE);
1412 redrawcmd();
1413 did_wild_list = TRUE;
1414 }
1415 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001416 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1417 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001419 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1420 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else
1422 res = OK; /* don't insert 'wildchar' now */
1423 }
1424 else /* typed p_wc first time */
1425 {
1426 wim_index = 0;
1427 j = ccline.cmdpos;
1428 /* if 'wildmode' first contains "longest", get longest
1429 * common part */
1430 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001431 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1432 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001434 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1435 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* if interrupted while completing, behave like it failed */
1438 if (got_int)
1439 {
1440 (void)vpeekc(); /* remove <C-C> from input stream */
1441 got_int = FALSE; /* don't abandon the command line */
1442 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1443#ifdef FEAT_WILDMENU
1444 xpc.xp_context = EXPAND_NOTHING;
1445#endif
1446 goto cmdline_changed;
1447 }
1448
1449 /* when more than one match, and 'wildmode' first contains
1450 * "list", or no change and 'wildmode' contains "longest,list",
1451 * list all matches */
1452 if (res == OK && xpc.xp_numfiles > 1)
1453 {
1454 /* a "longest" that didn't do anything is skipped (but not
1455 * "list:longest") */
1456 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1457 wim_index = 1;
1458 if ((wim_flags[wim_index] & WIM_LIST)
1459#ifdef FEAT_WILDMENU
1460 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1461#endif
1462 )
1463 {
1464 if (!(wim_flags[0] & WIM_LONGEST))
1465 {
1466#ifdef FEAT_WILDMENU
1467 int p_wmnu_save = p_wmnu;
1468 p_wmnu = 0;
1469#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 /* remove match */
1471 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472#ifdef FEAT_WILDMENU
1473 p_wmnu = p_wmnu_save;
1474#endif
1475 }
1476#ifdef FEAT_WILDMENU
1477 (void)showmatches(&xpc, p_wmnu
1478 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1479#else
1480 (void)showmatches(&xpc, FALSE);
1481#endif
1482 redrawcmd();
1483 did_wild_list = TRUE;
1484 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001485 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1486 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001488 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1489 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001492 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494#ifdef FEAT_WILDMENU
1495 else if (xpc.xp_numfiles == -1)
1496 xpc.xp_context = EXPAND_NOTHING;
1497#endif
1498 }
1499 if (wim_index < 3)
1500 ++wim_index;
1501 if (c == ESC)
1502 gotesc = TRUE;
1503 if (res == OK)
1504 goto cmdline_changed;
1505 }
1506
1507 gotesc = FALSE;
1508
1509 /* <S-Tab> goes to last match, in a clumsy way */
1510 if (c == K_S_TAB && KeyTyped)
1511 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001512 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1513 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1514 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 goto cmdline_changed;
1516 }
1517
1518 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1519 c = NL;
1520
1521 do_abbr = TRUE; /* default: check for abbreviation */
1522
1523 /*
1524 * Big switch for a typed command line character.
1525 */
1526 switch (c)
1527 {
1528 case K_BS:
1529 case Ctrl_H:
1530 case K_DEL:
1531 case K_KDEL:
1532 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (c == K_KDEL)
1534 c = K_DEL;
1535
1536 /*
1537 * delete current character is the same as backspace on next
1538 * character, except at end of line
1539 */
1540 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1541 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if (has_mbyte && c == K_DEL)
1543 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1544 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (ccline.cmdpos > 0)
1546 {
1547 char_u *p;
1548
1549 j = ccline.cmdpos;
1550 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (has_mbyte)
1552 {
1553 p = mb_prevptr(ccline.cmdbuff, p);
1554 if (c == Ctrl_W)
1555 {
1556 while (p > ccline.cmdbuff && vim_isspace(*p))
1557 p = mb_prevptr(ccline.cmdbuff, p);
1558 i = mb_get_class(p);
1559 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1560 p = mb_prevptr(ccline.cmdbuff, p);
1561 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001562 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001565 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1568 --p;
1569 i = vim_iswordc(p[-1]);
1570 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1571 && vim_iswordc(p[-1]) == i)
1572 --p;
1573 }
1574 else
1575 --p;
1576 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1577 ccline.cmdlen -= j - ccline.cmdpos;
1578 i = ccline.cmdpos;
1579 while (i < ccline.cmdlen)
1580 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1581
1582 /* Truncate at the end, required for multi-byte chars. */
1583 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001584#ifdef FEAT_SEARCH_EXTRA
1585 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001586 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001587 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001588 /* save view settings, so that the screen
1589 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001590 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001591 }
1592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 redrawcmd();
1594 }
1595 else if (ccline.cmdlen == 0 && c != Ctrl_W
1596 && ccline.cmdprompt == NULL && indent == 0)
1597 {
1598 /* In ex and debug mode it doesn't make sense to return. */
1599 if (exmode_active
1600#ifdef FEAT_EVAL
1601 || ccline.cmdfirstc == '>'
1602#endif
1603 )
1604 goto cmdline_not_changed;
1605
Bram Moolenaard23a8232018-02-10 18:45:26 +01001606 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (!cmd_silent)
1608 {
1609#ifdef FEAT_RIGHTLEFT
1610 if (cmdmsg_rl)
1611 msg_col = Columns;
1612 else
1613#endif
1614 msg_col = 0;
1615 msg_putchar(' '); /* delete ':' */
1616 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001617#ifdef FEAT_SEARCH_EXTRA
1618 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001619 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 redraw_cmdline = TRUE;
1622 goto returncmd; /* back to cmd mode */
1623 }
1624 goto cmdline_changed;
1625
1626 case K_INS:
1627 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 ccline.overstrike = !ccline.overstrike;
1629#ifdef CURSOR_SHAPE
1630 ui_cursor_shape(); /* may show different cursor shape */
1631#endif
1632 goto cmdline_not_changed;
1633
1634 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001635 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
1637 /* ":lmap" mappings exists, toggle use of mappings. */
1638 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001639#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 im_set_active(FALSE); /* Disable input method */
1641#endif
1642 if (b_im_ptr != NULL)
1643 {
1644 if (State & LANGMAP)
1645 *b_im_ptr = B_IMODE_LMAP;
1646 else
1647 *b_im_ptr = B_IMODE_NONE;
1648 }
1649 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001650#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 else
1652 {
1653 /* There are no ":lmap" mappings, toggle IM. When
1654 * 'imdisable' is set don't try getting the status, it's
1655 * always off. */
1656 if ((p_imdisable && b_im_ptr != NULL)
1657 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1658 {
1659 im_set_active(FALSE); /* Disable input method */
1660 if (b_im_ptr != NULL)
1661 *b_im_ptr = B_IMODE_NONE;
1662 }
1663 else
1664 {
1665 im_set_active(TRUE); /* Enable input method */
1666 if (b_im_ptr != NULL)
1667 *b_im_ptr = B_IMODE_IM;
1668 }
1669 }
1670#endif
1671 if (b_im_ptr != NULL)
1672 {
1673 if (b_im_ptr == &curbuf->b_p_iminsert)
1674 set_iminsert_global();
1675 else
1676 set_imsearch_global();
1677 }
1678#ifdef CURSOR_SHAPE
1679 ui_cursor_shape(); /* may show different cursor shape */
1680#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001681#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 /* Show/unshow value of 'keymap' in status lines later. */
1683 status_redraw_curbuf();
1684#endif
1685 goto cmdline_not_changed;
1686
1687/* case '@': only in very old vi */
1688 case Ctrl_U:
1689 /* delete all characters left of the cursor */
1690 j = ccline.cmdpos;
1691 ccline.cmdlen -= j;
1692 i = ccline.cmdpos = 0;
1693 while (i < ccline.cmdlen)
1694 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1695 /* Truncate at the end, required for multi-byte chars. */
1696 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001697#ifdef FEAT_SEARCH_EXTRA
1698 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001699 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 redrawcmd();
1702 goto cmdline_changed;
1703
1704#ifdef FEAT_CLIPBOARD
1705 case Ctrl_Y:
1706 /* Copy the modeless selection, if there is one. */
1707 if (clip_star.state != SELECT_CLEARED)
1708 {
1709 if (clip_star.state == SELECT_DONE)
1710 clip_copy_modeless_selection(TRUE);
1711 goto cmdline_not_changed;
1712 }
1713 break;
1714#endif
1715
1716 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1717 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001718 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001719 * ":normal" runs out of characters. */
1720 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001721 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 goto cmdline_not_changed;
1723
1724 gotesc = TRUE; /* will free ccline.cmdbuff after
1725 putting it in history */
1726 goto returncmd; /* back to cmd mode */
1727
1728 case Ctrl_R: /* insert register */
1729#ifdef USE_ON_FLY_SCROLL
1730 dont_scroll = TRUE; /* disallow scrolling here */
1731#endif
1732 putcmdline('"', TRUE);
1733 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001734 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (i == Ctrl_O)
1736 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1737 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001738 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001739 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 --no_mapping;
1741#ifdef FEAT_EVAL
1742 /*
1743 * Insert the result of an expression.
1744 * Need to save the current command line, to be able to enter
1745 * a new one...
1746 */
1747 new_cmdpos = -1;
1748 if (c == '=')
1749 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001750 if (ccline.cmdfirstc == '=' // can't do this recursively
1751 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 beep_flush();
1754 c = ESC;
1755 }
1756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759#endif
1760 if (c != ESC) /* use ESC to cancel inserting register */
1761 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001762 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001763
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001764#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001765 /* When there was a serious error abort getting the
1766 * command line. */
1767 if (aborting())
1768 {
1769 gotesc = TRUE; /* will free ccline.cmdbuff after
1770 putting it in history */
1771 goto returncmd; /* back to cmd mode */
1772 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 KeyTyped = FALSE; /* Don't do p_wc completion. */
1775#ifdef FEAT_EVAL
1776 if (new_cmdpos >= 0)
1777 {
1778 /* set_cmdline_pos() was used */
1779 if (new_cmdpos > ccline.cmdlen)
1780 ccline.cmdpos = ccline.cmdlen;
1781 else
1782 ccline.cmdpos = new_cmdpos;
1783 }
1784#endif
1785 }
1786 redrawcmd();
1787 goto cmdline_changed;
1788
1789 case Ctrl_D:
1790 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1791 break; /* Use ^D as normal char instead */
1792
1793 redrawcmd();
1794 continue; /* don't do incremental search now */
1795
1796 case K_RIGHT:
1797 case K_S_RIGHT:
1798 case K_C_RIGHT:
1799 do
1800 {
1801 if (ccline.cmdpos >= ccline.cmdlen)
1802 break;
1803 i = cmdline_charsize(ccline.cmdpos);
1804 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1805 break;
1806 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001808 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 + ccline.cmdpos);
1810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 ++ccline.cmdpos;
1812 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001813 while ((c == K_S_RIGHT || c == K_C_RIGHT
1814 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (has_mbyte)
1817 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 goto cmdline_not_changed;
1819
1820 case K_LEFT:
1821 case K_S_LEFT:
1822 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001823 if (ccline.cmdpos == 0)
1824 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 do
1826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (has_mbyte) /* move to first byte of char */
1829 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1830 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1832 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001833 while (ccline.cmdpos > 0
1834 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001835 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (has_mbyte)
1838 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 goto cmdline_not_changed;
1840
1841 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001842 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001843 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaar4f974752019-02-17 17:44:42 +01001845#ifdef FEAT_GUI_MSWIN
1846 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1847 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001848 case K_F4:
1849 if (mod_mask == MOD_MASK_ALT)
1850 {
1851 redrawcmd(); /* somehow the cmdline is cleared */
1852 goto cmdline_not_changed;
1853 }
1854 break;
1855#endif
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 case K_MIDDLEDRAG:
1858 case K_MIDDLERELEASE:
1859 goto cmdline_not_changed; /* Ignore mouse */
1860
1861 case K_MIDDLEMOUSE:
1862# ifdef FEAT_GUI
1863 /* When GUI is active, also paste when 'mouse' is empty */
1864 if (!gui.in_use)
1865# endif
1866 if (!mouse_has(MOUSE_COMMAND))
1867 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001868# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001870 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001872# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001873 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 redrawcmd();
1875 goto cmdline_changed;
1876
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001877# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001879 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 redrawcmd();
1881 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 case K_LEFTDRAG:
1885 case K_LEFTRELEASE:
1886 case K_RIGHTDRAG:
1887 case K_RIGHTRELEASE:
1888 /* Ignore drag and release events when the button-down wasn't
1889 * seen before. */
1890 if (ignore_drag_release)
1891 goto cmdline_not_changed;
1892 /* FALLTHROUGH */
1893 case K_LEFTMOUSE:
1894 case K_RIGHTMOUSE:
1895 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1896 ignore_drag_release = TRUE;
1897 else
1898 ignore_drag_release = FALSE;
1899# ifdef FEAT_GUI
1900 /* When GUI is active, also move when 'mouse' is empty */
1901 if (!gui.in_use)
1902# endif
1903 if (!mouse_has(MOUSE_COMMAND))
1904 goto cmdline_not_changed; /* Ignore mouse */
1905# ifdef FEAT_CLIPBOARD
1906 if (mouse_row < cmdline_row && clip_star.available)
1907 {
1908 int button, is_click, is_drag;
1909
1910 /*
1911 * Handle modeless selection.
1912 */
1913 button = get_mouse_button(KEY2TERMCAP1(c),
1914 &is_click, &is_drag);
1915 if (mouse_model_popup() && button == MOUSE_LEFT
1916 && (mod_mask & MOD_MASK_SHIFT))
1917 {
1918 /* Translate shift-left to right button. */
1919 button = MOUSE_RIGHT;
1920 mod_mask &= ~MOD_MASK_SHIFT;
1921 }
1922 clip_modeless(button, is_click, is_drag);
1923 goto cmdline_not_changed;
1924 }
1925# endif
1926
1927 set_cmdspos();
1928 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1929 ++ccline.cmdpos)
1930 {
1931 i = cmdline_charsize(ccline.cmdpos);
1932 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1933 && mouse_col < ccline.cmdspos % Columns + i)
1934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (has_mbyte)
1936 {
1937 /* Count ">" for double-wide char that doesn't fit. */
1938 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001939 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 + ccline.cmdpos) - 1;
1941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 ccline.cmdspos += i;
1943 }
1944 goto cmdline_not_changed;
1945
1946 /* Mouse scroll wheel: ignored here */
1947 case K_MOUSEDOWN:
1948 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001949 case K_MOUSELEFT:
1950 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 /* Alternate buttons ignored here */
1952 case K_X1MOUSE:
1953 case K_X1DRAG:
1954 case K_X1RELEASE:
1955 case K_X2MOUSE:
1956 case K_X2DRAG:
1957 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001958 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 goto cmdline_not_changed;
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#ifdef FEAT_GUI
1962 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1963 case K_LEFTRELEASE_NM:
1964 goto cmdline_not_changed;
1965
1966 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001967 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 gui_do_scroll();
1970 redrawcmd();
1971 }
1972 goto cmdline_not_changed;
1973
1974 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001975 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001977 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 redrawcmd();
1979 }
1980 goto cmdline_not_changed;
1981#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001982#ifdef FEAT_GUI_TABLINE
1983 case K_TABLINE:
1984 case K_TABMENU:
1985 /* Don't want to change any tabs here. Make sure the same tab
1986 * is still selected. */
1987 if (gui_use_tabline())
1988 gui_mch_set_curtab(tabpage_index(curtab));
1989 goto cmdline_not_changed;
1990#endif
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 case K_SELECT: /* end of Select mode mapping - ignore */
1993 goto cmdline_not_changed;
1994
1995 case Ctrl_B: /* begin of command line */
1996 case K_HOME:
1997 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 case K_S_HOME:
1999 case K_C_HOME:
2000 ccline.cmdpos = 0;
2001 set_cmdspos();
2002 goto cmdline_not_changed;
2003
2004 case Ctrl_E: /* end of command line */
2005 case K_END:
2006 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 case K_S_END:
2008 case K_C_END:
2009 ccline.cmdpos = ccline.cmdlen;
2010 set_cmdspos_cursor();
2011 goto cmdline_not_changed;
2012
2013 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002014 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 break;
2016 goto cmdline_changed;
2017
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002018 case Ctrl_L:
2019#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002020 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002021 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002022#endif
2023
2024 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002025 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 break;
2027 goto cmdline_changed;
2028
2029 case Ctrl_N: /* next match */
2030 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002031 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002033 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2034 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002036 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002038 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 case K_UP:
2040 case K_DOWN:
2041 case K_S_UP:
2042 case K_S_DOWN:
2043 case K_PAGEUP:
2044 case K_KPAGEUP:
2045 case K_PAGEDOWN:
2046 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002047 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 goto cmdline_not_changed;
2049
2050 i = hiscnt;
2051
2052 /* save current command string so it can be restored later */
2053 if (lookfor == NULL)
2054 {
2055 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2056 goto cmdline_not_changed;
2057 lookfor[ccline.cmdpos] = NUL;
2058 }
2059
2060 j = (int)STRLEN(lookfor);
2061 for (;;)
2062 {
2063 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002064 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002065 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002067 if (hiscnt == get_hislen()) /* first time */
2068 hiscnt = *get_hisidx(histype);
2069 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2070 hiscnt = get_hislen() - 1;
2071 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 --hiscnt;
2073 else /* at top of list */
2074 {
2075 hiscnt = i;
2076 break;
2077 }
2078 }
2079 else /* one step forwards */
2080 {
2081 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002082 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002084 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 break;
2086 }
2087
2088 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002089 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002091 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 hiscnt = 0;
2093 else
2094 ++hiscnt;
2095 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002096 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
2098 hiscnt = i;
2099 break;
2100 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002101 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002102 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002103 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 lookfor, (size_t)j) == 0)
2105 break;
2106 }
2107
2108 if (hiscnt != i) /* jumped to other entry */
2109 {
2110 char_u *p;
2111 int len;
2112 int old_firstc;
2113
Bram Moolenaar438d1762018-09-30 17:11:48 +02002114 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002115 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002116 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 p = lookfor; /* back to the old one */
2118 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002119 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 if (histype == HIST_SEARCH
2122 && p != lookfor
2123 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2124 {
2125 /* Correct for the separator character used when
2126 * adding the history entry vs the one used now.
2127 * First loop: count length.
2128 * Second loop: copy the characters. */
2129 for (i = 0; i <= 1; ++i)
2130 {
2131 len = 0;
2132 for (j = 0; p[j] != NUL; ++j)
2133 {
2134 /* Replace old sep with new sep, unless it is
2135 * escaped. */
2136 if (p[j] == old_firstc
2137 && (j == 0 || p[j - 1] != '\\'))
2138 {
2139 if (i > 0)
2140 ccline.cmdbuff[len] = firstc;
2141 }
2142 else
2143 {
2144 /* Escape new sep, unless it is already
2145 * escaped. */
2146 if (p[j] == firstc
2147 && (j == 0 || p[j - 1] != '\\'))
2148 {
2149 if (i > 0)
2150 ccline.cmdbuff[len] = '\\';
2151 ++len;
2152 }
2153 if (i > 0)
2154 ccline.cmdbuff[len] = p[j];
2155 }
2156 ++len;
2157 }
2158 if (i == 0)
2159 {
2160 alloc_cmdbuff(len);
2161 if (ccline.cmdbuff == NULL)
2162 goto returncmd;
2163 }
2164 }
2165 ccline.cmdbuff[len] = NUL;
2166 }
2167 else
2168 {
2169 alloc_cmdbuff((int)STRLEN(p));
2170 if (ccline.cmdbuff == NULL)
2171 goto returncmd;
2172 STRCPY(ccline.cmdbuff, p);
2173 }
2174
2175 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2176 redrawcmd();
2177 goto cmdline_changed;
2178 }
2179 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002180 goto cmdline_not_changed;
2181
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002182#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002183 case Ctrl_G: /* next match */
2184 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002185 if (may_adjust_incsearch_highlighting(
2186 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002187 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#endif
2190
2191 case Ctrl_V:
2192 case Ctrl_Q:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 putcmdline('^', TRUE);
2195 c = get_literal(); /* get next (two) character(s) */
2196 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002197 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 /* may need to remove ^ when composing char was typed */
2199 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2200 {
2201 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2202 msg_putchar(' ');
2203 cursorcmd();
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 break;
2206
2207#ifdef FEAT_DIGRAPHS
2208 case Ctrl_K:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 ignore_drag_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 putcmdline('?', TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002211# ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 dont_scroll = TRUE; /* disallow scrolling here */
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002215 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (c != NUL)
2217 break;
2218
2219 redrawcmd();
2220 goto cmdline_not_changed;
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002221#endif // FEAT_DIGRAPHS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223#ifdef FEAT_RIGHTLEFT
2224 case Ctrl__: /* CTRL-_: switch language mode */
2225 if (!p_ari)
2226 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002227 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 goto cmdline_not_changed;
2229#endif
2230
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002231 case K_PS:
2232 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2233 goto cmdline_changed;
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 default:
2236#ifdef UNIX
2237 if (c == intr_char)
2238 {
2239 gotesc = TRUE; /* will free ccline.cmdbuff after
2240 putting it in history */
2241 goto returncmd; /* back to Normal mode */
2242 }
2243#endif
2244 /*
2245 * Normal character with no special meaning. Just set mod_mask
2246 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2247 * the string <S-Space>. This should only happen after ^V.
2248 */
2249 if (!IS_SPECIAL(c))
2250 mod_mask = 0x0;
2251 break;
2252 }
2253 /*
2254 * End of switch on command line character.
2255 * We come here if we have a normal character.
2256 */
2257
Bram Moolenaar13505972019-01-24 15:04:48 +01002258 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2259 && (ccheck_abbr(
2260 // Add ABBR_OFF for characters above 0x100, this is
2261 // what check_abbr() expects.
2262 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2263 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 goto cmdline_changed;
2265
2266 /*
2267 * put the character in the command line
2268 */
2269 if (IS_SPECIAL(c) || mod_mask != 0)
2270 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2271 else
2272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (has_mbyte)
2274 {
2275 j = (*mb_char2bytes)(c, IObuff);
2276 IObuff[j] = NUL; /* exclude composing chars */
2277 put_on_cmdline(IObuff, j, TRUE);
2278 }
2279 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
2281 IObuff[0] = c;
2282 put_on_cmdline(IObuff, 1, TRUE);
2283 }
2284 }
2285 goto cmdline_changed;
2286
2287/*
2288 * This part implements incremental searches for "/" and "?"
2289 * Jump to cmdline_not_changed when a character has been read but the command
2290 * line did not change. Then we only search and redraw if something changed in
2291 * the past.
2292 * Jump to cmdline_changed when the command line did change.
2293 * (Sorry for the goto's, I know it is ugly).
2294 */
2295cmdline_not_changed:
2296#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002297 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 continue;
2299#endif
2300
2301cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002302 /* Trigger CmdlineChanged autocommands. */
2303 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002306 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
2308
2309#ifdef FEAT_RIGHTLEFT
2310 if (cmdmsg_rl
2311# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002312 || (p_arshape && !p_tbidi
2313 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314# endif
2315 )
2316 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002317 * right-left typing. Not efficient, but it works.
2318 * Do it only when there are no characters left to read
2319 * to avoid useless intermediate redraws. */
2320 if (vpeekc() == NUL)
2321 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#endif
2323 }
2324
2325returncmd:
2326
2327#ifdef FEAT_RIGHTLEFT
2328 cmdmsg_rl = FALSE;
2329#endif
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002332 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002335 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#endif
2337
2338 if (ccline.cmdbuff != NULL)
2339 {
2340 /*
2341 * Put line in history buffer (":" and "=" only when it was typed).
2342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (ccline.cmdlen && firstc != NUL
2344 && (some_key_typed || histype == HIST_SEARCH))
2345 {
2346 add_to_history(histype, ccline.cmdbuff, TRUE,
2347 histype == HIST_SEARCH ? firstc : NUL);
2348 if (firstc == ':')
2349 {
2350 vim_free(new_last_cmdline);
2351 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2352 }
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002355 if (gotesc)
2356 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
2359 /*
2360 * If the screen was shifted up, redraw the whole screen (later).
2361 * If the line is too long, clear it, so ruler and shown command do
2362 * not get printed in the middle of it.
2363 */
2364 msg_check();
2365 msg_scroll = save_msg_scroll;
2366 redir_off = FALSE;
2367
2368 /* When the command line was typed, no need for a wait-return prompt. */
2369 if (some_key_typed)
2370 need_wait_return = FALSE;
2371
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002372 /* Trigger CmdlineLeave autocommands. */
2373 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002376#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2378 im_save_status(b_im_ptr);
2379 im_set_active(FALSE);
2380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef CURSOR_SHAPE
2383 ui_cursor_shape(); /* may show different cursor shape */
2384#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002385 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaar438d1762018-09-30 17:11:48 +02002387theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002388 {
2389 char_u *p = ccline.cmdbuff;
2390
Bram Moolenaar438d1762018-09-30 17:11:48 +02002391 if (did_save_ccline)
2392 restore_cmdline(&save_ccline);
2393 else
2394 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002395 return p;
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397}
2398
2399#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2400/*
2401 * Get a command line with a prompt.
2402 * This is prepared to be called recursively from getcmdline() (e.g. by
2403 * f_input() when evaluating an expression from CTRL-R =).
2404 * Returns the command line in allocated memory, or NULL.
2405 */
2406 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002407getcmdline_prompt(
2408 int firstc,
2409 char_u *prompt, /* command line prompt */
2410 int attr, /* attributes for prompt */
2411 int xp_context, /* type of expansion */
2412 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002415 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002416 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002418 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar438d1762018-09-30 17:11:48 +02002420 if (ccline.cmdbuff != NULL)
2421 {
2422 // Save the values of the current cmdline and restore them below.
2423 save_cmdline(&save_ccline);
2424 did_save_ccline = TRUE;
2425 }
2426
Bram Moolenaar66b51422019-08-18 21:44:12 +02002427 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 ccline.cmdprompt = prompt;
2429 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002430# ifdef FEAT_EVAL
2431 ccline.xp_context = xp_context;
2432 ccline.xp_arg = xp_arg;
2433 ccline.input_fn = (firstc == '@');
2434# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002435 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002436 s = getcmdline_int(firstc, 1L, 0, FALSE);
2437
2438 if (did_save_ccline)
2439 restore_cmdline(&save_ccline);
2440
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002441 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002442 /* Restore msg_col, the prompt from input() may have changed it.
2443 * But only if called recursively and the commandline is therefore being
2444 * restored to an old one; if not, the input() prompt stays on the screen,
2445 * so we need its modified msg_col left intact. */
2446 if (ccline.cmdbuff != NULL)
2447 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 return s;
2450}
2451#endif
2452
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002453/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002454 * Return TRUE when the text must not be changed and we can't switch to
2455 * another window or buffer. Used when editing the command line, evaluating
2456 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002457 */
2458 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002459text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002460{
2461#ifdef FEAT_CMDWIN
2462 if (cmdwin_type != 0)
2463 return TRUE;
2464#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002465 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002466}
2467
2468/*
2469 * Give an error message for a command that isn't allowed while the cmdline
2470 * window is open or editing the cmdline in another way.
2471 */
2472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002473text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002474{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002475 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002476}
2477
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002479get_text_locked_msg(void)
2480{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002481#ifdef FEAT_CMDWIN
2482 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002483 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002484#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002485 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002486}
2487
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002488/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002489 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2490 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002491 */
2492 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002493curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494{
2495 if (curbuf_lock > 0)
2496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002497 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002498 return TRUE;
2499 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002500 return allbuf_locked();
2501}
2502
2503/*
2504 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2505 * message.
2506 */
2507 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002508allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002509{
2510 if (allbuf_lock > 0)
2511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002512 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002513 return TRUE;
2514 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002515 return FALSE;
2516}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002519cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2522 if (cmdline_star > 0) /* showing '*', always 1 position */
2523 return 1;
2524#endif
2525 return ptr2cells(ccline.cmdbuff + idx);
2526}
2527
2528/*
2529 * Compute the offset of the cursor on the command line for the prompt and
2530 * indent.
2531 */
2532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002533set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002535 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 ccline.cmdspos = 1 + ccline.cmdindent;
2537 else
2538 ccline.cmdspos = 0 + ccline.cmdindent;
2539}
2540
2541/*
2542 * Compute the screen position for the cursor on the command line.
2543 */
2544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int i, m, c;
2548
2549 set_cmdspos();
2550 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002553 if (m < 0) /* overflow, Columns or Rows at weird value */
2554 m = MAXCOL;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 else
2557 m = MAXCOL;
2558 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2559 {
2560 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2562 if (has_mbyte)
2563 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002564 /* If the cmdline doesn't fit, show cursor on last visible char.
2565 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if ((ccline.cmdspos += c) >= m)
2567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 ccline.cmdspos -= c;
2569 break;
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002572 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574}
2575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576/*
2577 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2578 * character that doesn't fit, so that a ">" must be displayed.
2579 */
2580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002581correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002583 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2585 && ccline.cmdspos % Columns + cells > Columns)
2586 ccline.cmdspos++;
2587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589/*
2590 * Get an Ex command line for the ":" command.
2591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593getexline(
2594 int c, /* normally ':', NUL for ":append" */
2595 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002596 int indent, /* indent for inside conditionals */
2597 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
2599 /* When executing a register, remove ':' that's in front of each line. */
2600 if (exec_from_reg && vpeekc() == ':')
2601 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002602 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603}
2604
2605/*
2606 * Get an Ex command line for Ex mode.
2607 * In Ex mode we only use the OS supplied line editing features and no
2608 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002609 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002612getexmodeline(
2613 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002614 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002615 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002616 int indent, /* indent for inside conditionals */
2617 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002619 garray_T line_ga;
2620 char_u *pend;
2621 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002622 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002623 int escaped = FALSE; /* CTRL-V typed */
2624 int vcol = 0;
2625 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002626 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002627 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /* Switch cursor on now. This avoids that it happens after the "\n", which
2630 * confuses the system function that computes tabstops. */
2631 cursor_on();
2632
2633 /* always start in column 0; write a newline if necessary */
2634 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002637 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002639 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 if (p_prompt)
2641 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 while (indent-- > 0)
2643 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646
2647 ga_init2(&line_ga, 1, 30);
2648
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002649 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002651 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002652 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002653 while (indent >= 8)
2654 {
2655 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002656 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002657 indent -= 8;
2658 }
2659 while (indent-- > 0)
2660 {
2661 ga_append(&line_ga, ' ');
2662 msg_putchar(' ');
2663 }
2664 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002665 ++no_mapping;
2666 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 /*
2669 * Get the line, one character at a time.
2670 */
2671 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002674 long sw;
2675 char_u *s;
2676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (ga_grow(&line_ga, 40) == FAIL)
2678 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaarba748c82017-02-26 14:00:07 +01002680 /*
2681 * Get one character at a time.
2682 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002683 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002684
2685 /* Check for a ":normal" command and no more characters left. */
2686 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2687 c1 = '\n';
2688 else
2689 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002692 * Handle line editing.
2693 * Previously this was left to the system, putting the terminal in
2694 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 msg_putchar('\n');
2699 break;
2700 }
2701
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002702 if (c1 == K_PS)
2703 {
2704 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2705 goto redraw;
2706 }
2707
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 if (!escaped)
2709 {
2710 /* CR typed means "enter", which is NL */
2711 if (c1 == '\r')
2712 c1 = '\n';
2713
2714 if (c1 == BS || c1 == K_BS
2715 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 if (line_ga.ga_len > 0)
2718 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002719 if (has_mbyte)
2720 {
2721 p = (char_u *)line_ga.ga_data;
2722 p[line_ga.ga_len] = NUL;
2723 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2724 line_ga.ga_len -= len;
2725 }
2726 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002727 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 goto redraw;
2729 }
2730 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 msg_col = startcol;
2736 msg_clr_eos();
2737 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002738 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 }
2740
2741 if (c1 == Ctrl_T)
2742 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002743 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 p = (char_u *)line_ga.ga_data;
2745 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002746 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002747 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002748add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002749 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002751 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002752 p = (char_u *)line_ga.ga_data;
2753 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2755 *s = ' ';
2756 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758redraw:
2759 /* redraw the line */
2760 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002762 p = (char_u *)line_ga.ga_data;
2763 p[line_ga.ga_len] = NUL;
2764 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002769 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002770 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002771 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002775 len = mb_ptr2len(p);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002776 msg_outtrans_len(p, len);
2777 vcol += ptr2cells(p);
2778 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002782 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 continue;
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 if (c1 == Ctrl_D)
2787 {
2788 /* Delete one shiftwidth. */
2789 p = (char_u *)line_ga.ga_data;
2790 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 if (prev_char == '^')
2793 ex_keep_indent = TRUE;
2794 indent = 0;
2795 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 else
2798 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002800 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002801 if (indent > 0)
2802 {
2803 --indent;
2804 indent -= indent % get_sw_value(curbuf);
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002807 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002809 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2811 --line_ga.ga_len;
2812 }
2813 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815
2816 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2817 {
2818 escaped = TRUE;
2819 continue;
2820 }
2821
2822 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2823 if (IS_SPECIAL(c1))
2824 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826
2827 if (IS_SPECIAL(c1))
2828 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002829 if (has_mbyte)
2830 len = (*mb_char2bytes)(c1,
2831 (char_u *)line_ga.ga_data + line_ga.ga_len);
2832 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002833 {
2834 len = 1;
2835 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2836 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837 if (c1 == '\n')
2838 msg_putchar('\n');
2839 else if (c1 == TAB)
2840 {
2841 /* Don't use chartabsize(), 'ts' can be different */
2842 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002844 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002852 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 escaped = FALSE;
2854
2855 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002856 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002858 /* We are done when a NL is entered, but not when it comes after an
2859 * odd number of backslashes, that results in a NUL. */
2860 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002862 int bcount = 0;
2863
2864 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2865 ++bcount;
2866
2867 if (bcount > 0)
2868 {
2869 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2870 * "\NL", etc. */
2871 line_ga.ga_len -= (bcount + 1) / 2;
2872 pend -= (bcount + 1) / 2;
2873 pend[-1] = '\n';
2874 }
2875
2876 if ((bcount & 1) == 0)
2877 {
2878 --line_ga.ga_len;
2879 --pend;
2880 *pend = NUL;
2881 break;
2882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
2885
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 --no_mapping;
2887 --allow_keys;
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 /* make following messages go to the next line */
2890 msg_didout = FALSE;
2891 msg_col = 0;
2892 if (msg_row < Rows - 1)
2893 ++msg_row;
2894 emsg_on_display = FALSE; /* don't want ui_delay() */
2895
2896 if (got_int)
2897 ga_clear(&line_ga);
2898
2899 return (char_u *)line_ga.ga_data;
2900}
2901
Bram Moolenaare344bea2005-09-01 20:46:49 +00002902# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2903 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904/*
2905 * Return TRUE if ccline.overstrike is on.
2906 */
2907 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002908cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
2910 return ccline.overstrike;
2911}
2912
2913/*
2914 * Return TRUE if the cursor is at the end of the cmdline.
2915 */
2916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002917cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 return (ccline.cmdpos >= ccline.cmdlen);
2920}
2921#endif
2922
Bram Moolenaar9372a112005-12-06 19:59:18 +00002923#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924/*
2925 * Return the virtual column number at the current cursor position.
2926 * This is used by the IM code to obtain the start of the preedit string.
2927 */
2928 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002929cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2932 return MAXCOL;
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (has_mbyte)
2935 {
2936 colnr_T col;
2937 int i = 0;
2938
2939 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002940 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 return col;
2943 }
2944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return ccline.cmdpos;
2946}
2947#endif
2948
2949#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2950/*
2951 * If part of the command line is an IM preedit string, redraw it with
2952 * IM feedback attributes. The cursor position is restored after drawing.
2953 */
2954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002955redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 if ((State & CMDLINE)
2958 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002959 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 && !p_imdisable
2961 && im_is_preediting())
2962 {
2963 int cmdpos = 0;
2964 int cmdspos;
2965 int old_row;
2966 int old_col;
2967 colnr_T col;
2968
2969 old_row = msg_row;
2970 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002971 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (has_mbyte)
2974 {
2975 for (col = 0; col < preedit_start_col
2976 && cmdpos < ccline.cmdlen; ++col)
2977 {
2978 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002979 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 }
2982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
2984 cmdspos += preedit_start_col;
2985 cmdpos += preedit_start_col;
2986 }
2987
2988 msg_row = cmdline_row + (cmdspos / (int)Columns);
2989 msg_col = cmdspos % (int)Columns;
2990 if (msg_row >= Rows)
2991 msg_row = Rows - 1;
2992
2993 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2994 {
2995 int char_len;
2996 int char_attr;
2997
2998 char_attr = im_get_feedback_attr(col);
2999 if (char_attr < 0)
3000 break; /* end of preedit string */
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003003 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 char_len = 1;
3006
3007 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3008 cmdpos += char_len;
3009 }
3010
3011 msg_row = old_row;
3012 msg_col = old_col;
3013 }
3014}
3015#endif /* FEAT_XIM && FEAT_GUI_GTK */
3016
3017/*
3018 * Allocate a new command line buffer.
3019 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
3021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003022alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 /*
3025 * give some extra space to avoid having to allocate all the time
3026 */
3027 if (len < 80)
3028 len = 100;
3029 else
3030 len += 20;
3031
3032 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3033 ccline.cmdbufflen = len;
3034}
3035
3036/*
3037 * Re-allocate the command line to length len + something extra.
3038 * return FAIL for failure, OK otherwise
3039 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 char_u *p;
3044
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003045 if (len < ccline.cmdbufflen)
3046 return OK; /* no need to resize */
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 p = ccline.cmdbuff;
3049 alloc_cmdbuff(len); /* will get some more */
3050 if (ccline.cmdbuff == NULL) /* out of memory */
3051 {
3052 ccline.cmdbuff = p; /* keep the old one */
3053 return FAIL;
3054 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003055 /* There isn't always a NUL after the command, but it may need to be
3056 * there, thus copy up to the NUL and add a NUL. */
3057 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3058 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003060
3061 if (ccline.xpc != NULL
3062 && ccline.xpc->xp_pattern != NULL
3063 && ccline.xpc->xp_context != EXPAND_NOTHING
3064 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3065 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003066 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003067
3068 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3069 * to point into the newly allocated memory. */
3070 if (i >= 0 && i <= ccline.cmdlen)
3071 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3072 }
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 return OK;
3075}
3076
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003077#if defined(FEAT_ARABIC) || defined(PROTO)
3078static char_u *arshape_buf = NULL;
3079
3080# if defined(EXITFREE) || defined(PROTO)
3081 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003082free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003083{
3084 vim_free(arshape_buf);
3085}
3086# endif
3087#endif
3088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089/*
3090 * Draw part of the cmdline at the current cursor position. But draw stars
3091 * when cmdline_star is TRUE.
3092 */
3093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3097 int i;
3098
3099 if (cmdline_star > 0)
3100 for (i = 0; i < len; ++i)
3101 {
3102 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003104 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106 else
3107#endif
3108#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003109 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 static int buflen = 0;
3112 char_u *p;
3113 int j;
3114 int newlen = 0;
3115 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003116 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int prev_c = 0;
3118 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003119 int u8c;
3120 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123 /*
3124 * Do arabic shaping into a temporary buffer. This is very
3125 * inefficient!
3126 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003127 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 /* Re-allocate the buffer. We keep it around to avoid a lot of
3130 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003131 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003132 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003133 arshape_buf = alloc(buflen);
3134 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 return; /* out of memory */
3136 }
3137
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003138 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3139 {
3140 /* Prepend a space to draw the leading composing char on. */
3141 arshape_buf[0] = ' ';
3142 newlen = 1;
3143 }
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 for (j = start; j < start + len; j += mb_l)
3146 {
3147 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003148 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003149 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (ARABIC_CHAR(u8c))
3151 {
3152 /* Do Arabic shaping. */
3153 if (cmdmsg_rl)
3154 {
3155 /* displaying from right to left */
3156 pc = prev_c;
3157 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003158 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (j + mb_l >= start + len)
3160 nc = NUL;
3161 else
3162 nc = utf_ptr2char(p + mb_l);
3163 }
3164 else
3165 {
3166 /* displaying from left to right */
3167 if (j + mb_l >= start + len)
3168 pc = NUL;
3169 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003170 {
3171 int pcc[MAX_MCO];
3172
3173 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003175 pc1 = pcc[0];
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 nc = prev_c;
3178 }
3179 prev_c = u8c;
3180
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003181 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003183 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003184 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003186 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3187 if (u8cc[1] != 0)
3188 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191 }
3192 else
3193 {
3194 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 newlen += mb_l;
3197 }
3198 }
3199
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 else
3203#endif
3204 msg_outtrans_len(ccline.cmdbuff + start, len);
3205}
3206
3207/*
3208 * Put a character on the command line. Shifts the following text to the
3209 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3210 * "c" must be printable (fit in one display cell)!
3211 */
3212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003213putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 if (cmd_silent)
3216 return;
3217 msg_no_more = TRUE;
3218 msg_putchar(c);
3219 if (shift)
3220 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3221 msg_no_more = FALSE;
3222 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003223 extra_char = c;
3224 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225}
3226
3227/*
3228 * Undo a putcmdline(c, FALSE).
3229 */
3230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003231unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 if (cmd_silent)
3234 return;
3235 msg_no_more = TRUE;
3236 if (ccline.cmdlen == ccline.cmdpos)
3237 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003238 else if (has_mbyte)
3239 draw_cmdline(ccline.cmdpos,
3240 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else
3242 draw_cmdline(ccline.cmdpos, 1);
3243 msg_no_more = FALSE;
3244 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003245 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248/*
3249 * Put the given string, of the given length, onto the command line.
3250 * If len is -1, then STRLEN() is used to calculate the length.
3251 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3252 * part will be redrawn, otherwise it will not. If this function is called
3253 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3254 * called afterwards.
3255 */
3256 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003257put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258{
3259 int retval;
3260 int i;
3261 int m;
3262 int c;
3263
3264 if (len < 0)
3265 len = (int)STRLEN(str);
3266
3267 /* Check if ccline.cmdbuff needs to be longer */
3268 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003269 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else
3271 retval = OK;
3272 if (retval == OK)
3273 {
3274 if (!ccline.overstrike)
3275 {
3276 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3277 ccline.cmdbuff + ccline.cmdpos,
3278 (size_t)(ccline.cmdlen - ccline.cmdpos));
3279 ccline.cmdlen += len;
3280 }
3281 else
3282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (has_mbyte)
3284 {
3285 /* Count nr of characters in the new string. */
3286 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003287 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 ++m;
3289 /* Count nr of bytes in cmdline that are overwritten by these
3290 * characters. */
3291 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003292 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 --m;
3294 if (i < ccline.cmdlen)
3295 {
3296 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3297 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3298 ccline.cmdlen += ccline.cmdpos + len - i;
3299 }
3300 else
3301 ccline.cmdlen = ccline.cmdpos + len;
3302 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003303 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 ccline.cmdlen = ccline.cmdpos + len;
3305 }
3306 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3307 ccline.cmdbuff[ccline.cmdlen] = NUL;
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (enc_utf8)
3310 {
3311 /* When the inserted text starts with a composing character,
3312 * backup to the character before it. There could be two of them.
3313 */
3314 i = 0;
3315 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3316 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3317 {
3318 i = (*mb_head_off)(ccline.cmdbuff,
3319 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3320 ccline.cmdpos -= i;
3321 len += i;
3322 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3323 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003324#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3326 {
3327 /* Check the previous character for Arabic combining pair. */
3328 i = (*mb_head_off)(ccline.cmdbuff,
3329 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3330 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3331 + ccline.cmdpos - i), c))
3332 {
3333 ccline.cmdpos -= i;
3334 len += i;
3335 }
3336 else
3337 i = 0;
3338 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (i != 0)
3341 {
3342 /* Also backup the cursor position. */
3343 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3344 ccline.cmdspos -= i;
3345 msg_col -= i;
3346 if (msg_col < 0)
3347 {
3348 msg_col += Columns;
3349 --msg_row;
3350 }
3351 }
3352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354 if (redraw && !cmd_silent)
3355 {
3356 msg_no_more = TRUE;
3357 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003358 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3360 /* Avoid clearing the rest of the line too often. */
3361 if (cmdline_row != i || ccline.overstrike)
3362 msg_clr_eos();
3363 msg_no_more = FALSE;
3364 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003365 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003367 m = Columns * Rows;
3368 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003370 }
3371 else
3372 m = MAXCOL;
3373 for (i = 0; i < len; ++i)
3374 {
3375 c = cmdline_charsize(ccline.cmdpos);
3376 /* count ">" for a double-wide char that doesn't fit. */
3377 if (has_mbyte)
3378 correct_cmdspos(ccline.cmdpos, c);
3379 /* Stop cursor at the end of the screen, but do increment the
3380 * insert position, so that entering a very long command
3381 * works, even though you can't see it. */
3382 if (ccline.cmdspos + c < m)
3383 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003384
Bram Moolenaar14184a32019-02-16 15:10:30 +01003385 if (has_mbyte)
3386 {
3387 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3388 if (c > len - i - 1)
3389 c = len - i - 1;
3390 ccline.cmdpos += c;
3391 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003393 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 }
3396 if (redraw)
3397 msg_check();
3398 return retval;
3399}
3400
Bram Moolenaar66b51422019-08-18 21:44:12 +02003401static cmdline_info_T prev_ccline;
3402static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003403
3404/*
3405 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3406 * and overwrite it. But get_cmdline_str() may need it, thus make it
3407 * available globally in prev_ccline.
3408 */
3409 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003411{
3412 if (!prev_ccline_used)
3413 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003415 prev_ccline_used = TRUE;
3416 }
3417 *ccp = prev_ccline;
3418 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003419 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003420}
3421
3422/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003423 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003424 */
3425 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003426restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003427{
3428 ccline = prev_ccline;
3429 prev_ccline = *ccp;
3430}
3431
Bram Moolenaar8299df92004-07-10 09:47:34 +00003432/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003433 * Paste a yank register into the command line.
3434 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003435 * insert_reg() can't be used here, because special characters from the
3436 * register contents will be interpreted as commands.
3437 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003438 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003439 */
3440 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003441cmdline_paste(
3442 int regname,
3443 int literally, /* Insert text literally instead of "as typed" */
3444 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003445{
3446 long i;
3447 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003448 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003449 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003450
3451 /* check for valid regname; also accept special characters for CTRL-R in
3452 * the command line */
3453 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003454 && regname != Ctrl_A && regname != Ctrl_L
3455 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003456 return FAIL;
3457
3458 /* A register containing CTRL-R can cause an endless loop. Allow using
3459 * CTRL-C to break the loop. */
3460 line_breakcheck();
3461 if (got_int)
3462 return FAIL;
3463
3464#ifdef FEAT_CLIPBOARD
3465 regname = may_get_selection(regname);
3466#endif
3467
Bram Moolenaar438d1762018-09-30 17:11:48 +02003468 // Need to set "textlock" to avoid nasty things like going to another
3469 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003470 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003471 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003472 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003473
3474 if (i)
3475 {
3476 /* Got the value of a special register in "arg". */
3477 if (arg == NULL)
3478 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003479
3480 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3481 * part of the word. */
3482 p = arg;
3483 if (p_is && regname == Ctrl_W)
3484 {
3485 char_u *w;
3486 int len;
3487
3488 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003489 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003490 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003491 if (has_mbyte)
3492 {
3493 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3494 if (!vim_iswordc(mb_ptr2char(w - len)))
3495 break;
3496 w -= len;
3497 }
3498 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003499 {
3500 if (!vim_iswordc(w[-1]))
3501 break;
3502 --w;
3503 }
3504 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003505 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003506 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3507 p += len;
3508 }
3509
3510 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003511 if (allocated)
3512 vim_free(arg);
3513 return OK;
3514 }
3515
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003516 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003517}
3518
3519/*
3520 * Put a string on the command line.
3521 * When "literally" is TRUE, insert literally.
3522 * When "literally" is FALSE, insert as typed, but don't leave the command
3523 * line.
3524 */
3525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003526cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003527{
3528 int c, cv;
3529
3530 if (literally)
3531 put_on_cmdline(s, -1, TRUE);
3532 else
3533 while (*s != NUL)
3534 {
3535 cv = *s;
3536 if (cv == Ctrl_V && s[1])
3537 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003538 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003539 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003540 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003542 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3543 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003544#ifdef UNIX
3545 || c == intr_char
3546#endif
3547 || (c == Ctrl_BSL && *s == Ctrl_N))
3548 stuffcharReadbuff(Ctrl_V);
3549 stuffcharReadbuff(c);
3550 }
3551}
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#ifdef FEAT_WILDMENU
3554/*
3555 * Delete characters on the command line, from "from" to the current
3556 * position.
3557 */
3558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003559cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3562 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3563 ccline.cmdlen -= ccline.cmdpos - from;
3564 ccline.cmdpos = from;
3565}
3566#endif
3567
3568/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003569 * This function is called when the screen size changes and with incremental
3570 * search and in other situations where the command line may have been
3571 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 */
3573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003574redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003576 redrawcmdline_ex(TRUE);
3577}
3578
3579 void
3580redrawcmdline_ex(int do_compute_cmdrow)
3581{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (cmd_silent)
3583 return;
3584 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003585 if (do_compute_cmdrow)
3586 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 redrawcmd();
3588 cursorcmd();
3589}
3590
3591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003592redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
3594 int i;
3595
3596 if (cmd_silent)
3597 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003598 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 msg_putchar(ccline.cmdfirstc);
3600 if (ccline.cmdprompt != NULL)
3601 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003602 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3604 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003605 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 --ccline.cmdindent;
3607 }
3608 else
3609 for (i = ccline.cmdindent; i > 0; --i)
3610 msg_putchar(' ');
3611}
3612
3613/*
3614 * Redraw what is currently on the command line.
3615 */
3616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003617redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
3619 if (cmd_silent)
3620 return;
3621
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003622 /* when 'incsearch' is set there may be no command line while redrawing */
3623 if (ccline.cmdbuff == NULL)
3624 {
3625 windgoto(cmdline_row, 0);
3626 msg_clr_eos();
3627 return;
3628 }
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 msg_start();
3631 redrawcmdprompt();
3632
3633 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3634 msg_no_more = TRUE;
3635 draw_cmdline(0, ccline.cmdlen);
3636 msg_clr_eos();
3637 msg_no_more = FALSE;
3638
3639 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003640 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003641 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 /*
3644 * An emsg() before may have set msg_scroll. This is used in normal mode,
3645 * in cmdline mode we can reset them now.
3646 */
3647 msg_scroll = FALSE; /* next message overwrites cmdline */
3648
3649 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3650 * in cmdline mode */
3651 skip_redraw = FALSE;
3652}
3653
3654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003655compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003657 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 cmdline_row = Rows - 1;
3659 else
3660 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003661 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662}
3663
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003665cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
3667 if (cmd_silent)
3668 return;
3669
3670#ifdef FEAT_RIGHTLEFT
3671 if (cmdmsg_rl)
3672 {
3673 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3674 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3675 if (msg_row <= 0)
3676 msg_row = Rows - 1;
3677 }
3678 else
3679#endif
3680 {
3681 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3682 msg_col = ccline.cmdspos % (int)Columns;
3683 if (msg_row >= Rows)
3684 msg_row = Rows - 1;
3685 }
3686
3687 windgoto(msg_row, msg_col);
3688#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003689 if (p_imst == IM_ON_THE_SPOT)
3690 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691#endif
3692#ifdef MCH_CURSOR_SHAPE
3693 mch_update_cursor();
3694#endif
3695}
3696
3697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003698gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 msg_start();
3701#ifdef FEAT_RIGHTLEFT
3702 if (cmdmsg_rl)
3703 msg_col = Columns - 1;
3704 else
3705#endif
3706 msg_col = 0; /* always start in column 0 */
3707 if (clr) /* clear the bottom line(s) */
3708 msg_clr_eos(); /* will reset clear_cmdline */
3709 windgoto(cmdline_row, 0);
3710}
3711
3712/*
3713 * Check the word in front of the cursor for an abbreviation.
3714 * Called when the non-id character "c" has been entered.
3715 * When an abbreviation is recognized it is removed from the text with
3716 * backspaces and the replacement string is inserted, followed by "c".
3717 */
3718 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003719ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003721 int spos = 0;
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3724 return FALSE;
3725
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003726 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3727 * Actually accepts any mark. */
3728 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3729 spos++;
3730 if (ccline.cmdlen - spos > 5
3731 && ccline.cmdbuff[spos] == '\''
3732 && ccline.cmdbuff[spos + 2] == ','
3733 && ccline.cmdbuff[spos + 3] == '\'')
3734 spos += 5;
3735 else
3736 /* check abbreviation from the beginning of the commandline */
3737 spos = 0;
3738
3739 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740}
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003743 * Escape special characters in "fname" for when used as a file name argument
3744 * after a Vim command, or, when "shell" is non-zero, a shell command.
3745 * Returns the result in allocated memory.
3746 */
3747 char_u *
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02003748vim_strsave_fnameescape(char_u *fname, int shell UNUSED)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003749{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003750 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003751#ifdef BACKSLASH_IN_FILENAME
3752 char_u buf[20];
3753 int j = 0;
3754
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003755 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003756 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003757 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003758 buf[j++] = *p;
3759 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003760 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003761#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003762 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3763 if (shell && csh_like_shell() && p != NULL)
3764 {
3765 char_u *s;
3766
3767 /* For csh and similar shells need to put two backslashes before '!'.
3768 * One is taken by Vim, one by the shell. */
3769 s = vim_strsave_escaped(p, (char_u *)"!");
3770 vim_free(p);
3771 p = s;
3772 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003773#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003774
3775 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3776 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003777 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003778 escape_fname(&p);
3779
3780 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003781}
3782
3783/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003784 * Put a backslash before the file name in "pp", which is in allocated memory.
3785 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003787escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003788{
3789 char_u *p;
3790
Bram Moolenaar964b3742019-05-24 18:54:09 +02003791 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003792 if (p != NULL)
3793 {
3794 p[0] = '\\';
3795 STRCPY(p + 1, *pp);
3796 vim_free(*pp);
3797 *pp = p;
3798 }
3799}
3800
3801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 * For each file name in files[num_files]:
3803 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3804 */
3805 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003806tilde_replace(
3807 char_u *orig_pat,
3808 int num_files,
3809 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 int i;
3812 char_u *p;
3813
3814 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3815 {
3816 for (i = 0; i < num_files; ++i)
3817 {
3818 p = home_replace_save(NULL, files[i]);
3819 if (p != NULL)
3820 {
3821 vim_free(files[i]);
3822 files[i] = p;
3823 }
3824 }
3825 }
3826}
3827
3828/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003829 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003831 cmdline_info_T *
3832get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003834 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835}
3836
Bram Moolenaar064154c2016-03-19 22:50:43 +01003837#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003838/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003839 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003840 * ccline and put the previous value in prev_ccline.
3841 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003842 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003843get_ccline_ptr(void)
3844{
3845 if ((State & CMDLINE) == 0)
3846 return NULL;
3847 if (ccline.cmdbuff != NULL)
3848 return &ccline;
3849 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3850 return &prev_ccline;
3851 return NULL;
3852}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003853#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003854
Bram Moolenaar064154c2016-03-19 22:50:43 +01003855#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003856/*
3857 * Get the current command line in allocated memory.
3858 * Only works when the command line is being edited.
3859 * Returns NULL when something is wrong.
3860 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003861 static char_u *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003862get_cmdline_str(void)
3863{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003864 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003865
Bram Moolenaaree91c332018-09-25 22:27:35 +02003866 if (cmdline_star > 0)
3867 return NULL;
3868 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003869 if (p == NULL)
3870 return NULL;
3871 return vim_strnsave(p->cmdbuff, p->cmdlen);
3872}
3873
3874/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003875 * "getcmdline()" function
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003876 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003877 void
3878f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
3879{
3880 rettv->v_type = VAR_STRING;
3881 rettv->vval.v_string = get_cmdline_str();
3882}
3883
3884/*
3885 * "getcmdpos()" function
3886 */
3887 void
3888f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003889{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003890 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003891
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003892 rettv->vval.v_number = 0;
3893 if (p != NULL)
3894 rettv->vval.v_number = p->cmdpos + 1;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003895}
3896
3897/*
3898 * Set the command line byte position to "pos". Zero is the first position.
3899 * Only works when the command line is being edited.
3900 * Returns 1 when failed, 0 when OK.
3901 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003902 static int
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003903set_cmdline_pos(
3904 int pos)
3905{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003906 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003907
3908 if (p == NULL)
3909 return 1;
3910
3911 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3912 * changed the command line. */
3913 if (pos < 0)
3914 new_cmdpos = 0;
3915 else
3916 new_cmdpos = pos;
3917 return 0;
3918}
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003919
3920/*
3921 * "setcmdpos()" function
3922 */
3923 void
3924f_setcmdpos(typval_T *argvars, typval_T *rettv)
3925{
3926 int pos = (int)tv_get_number(&argvars[0]) - 1;
3927
3928 if (pos >= 0)
3929 rettv->vval.v_number = set_cmdline_pos(pos);
3930}
3931
3932/*
3933 * "getcmdtype()" function
3934 */
3935 void
3936f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
3937{
3938 rettv->v_type = VAR_STRING;
3939 rettv->vval.v_string = alloc(2);
3940 if (rettv->vval.v_string != NULL)
3941 {
3942 rettv->vval.v_string[0] = get_cmdline_type();
3943 rettv->vval.v_string[1] = NUL;
3944 }
3945}
3946
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003947#endif
3948
3949#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
3950/*
3951 * Get the current command-line type.
3952 * Returns ':' or '/' or '?' or '@' or '>' or '-'
3953 * Only works when the command line is being edited.
3954 * Returns NUL when something is wrong.
3955 */
3956 int
3957get_cmdline_type(void)
3958{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003959 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003960
3961 if (p == NULL)
3962 return NUL;
3963 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01003964 return
3965# ifdef FEAT_EVAL
3966 (p->input_fn) ? '@' :
3967# endif
3968 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003969 return p->cmdfirstc;
3970}
3971#endif
3972
Bram Moolenaard7663c22019-08-06 21:59:57 +02003973/*
3974 * Return the first character of the current command line.
3975 */
3976 int
3977get_cmdline_firstc(void)
3978{
3979 return ccline.cmdfirstc;
3980}
3981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982/*
3983 * Get indices "num1,num2" that specify a range within a list (not a range of
3984 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
3985 * Returns OK if parsed successfully, otherwise FAIL.
3986 */
3987 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003988get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 int len;
3991 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003992 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 *str = skipwhite(*str);
3995 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
3996 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003997 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *str += len;
3999 *num1 = (int)num;
4000 first = TRUE;
4001 }
4002 *str = skipwhite(*str);
4003 if (**str == ',') /* parse "to" part of range */
4004 {
4005 *str = skipwhite(*str + 1);
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 if (len > 0)
4008 {
4009 *num2 = (int)num;
4010 *str = skipwhite(*str + len);
4011 }
4012 else if (!first) /* no number given at all */
4013 return FAIL;
4014 }
4015 else if (first) /* only one number given */
4016 *num2 = *num1;
4017 return OK;
4018}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02004019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#if defined(FEAT_CMDWIN) || defined(PROTO)
4021/*
4022 * Open a window on the current command line and history. Allow editing in
4023 * the window. Returns when the window is closed.
4024 * Returns:
4025 * CR if the command is to be executed
4026 * Ctrl_C if it is to be abandoned
4027 * K_IGNORE if editing continues
4028 */
4029 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004030open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004032 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004034 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 win_T *wp;
4036 int i;
4037 linenr_T lnum;
4038 int histtype;
4039 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 int save_restart_edit = restart_edit;
4041 int save_State = State;
4042 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004043#ifdef FEAT_RIGHTLEFT
4044 int save_cmdmsg_rl = cmdmsg_rl;
4045#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004046#ifdef FEAT_FOLDING
4047 int save_KeyTyped;
4048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 /* Can't do this recursively. Can't do it when typing a password. */
4051 if (cmdwin_type != 0
4052# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4053 || cmdline_star > 0
4054# endif
4055 )
4056 {
4057 beep_flush();
4058 return K_IGNORE;
4059 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004060 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004062 // Save current window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 win_size_save(&winsizes);
4064
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004065 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4066 // command line window, but we don't want the popup menu then.
4067 pum_undisplay();
4068
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004069 // don't use a new tab page
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004070 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004071 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004072
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004073 // Create a window for the command-line buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4075 {
4076 beep_flush();
4077 return K_IGNORE;
4078 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004079 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004081 // Create the command-line buffer empty.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004082 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004083 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004086#ifdef FEAT_FOLDING
4087 curwin->w_p_fen = FALSE;
4088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004090 curwin->w_p_rl = cmdmsg_rl;
4091 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004093 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004095 // Don't allow switching to another buffer.
Bram Moolenaar18141832017-06-25 21:17:25 +02004096 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004098 // Showing the prompt may have set need_wait_return, reset it.
Bram Moolenaar46152342005-09-07 21:18:43 +00004099 need_wait_return = FALSE;
4100
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004101 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4103 {
4104 if (p_wc == TAB)
4105 {
4106 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4107 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4108 }
4109 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4110 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004111 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004113 // Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4114 // sets 'textwidth' to 78).
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004115 curbuf->b_p_tw = 0;
4116
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004117 // Fill the buffer with the history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004119 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004121 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if (i >= 0)
4123 {
4124 lnum = 0;
4125 do
4126 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004127 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004129 if (get_histentry(histtype)[i].hisstr != NULL)
4130 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 (colnr_T)0, FALSE);
4132 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004133 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 }
4136
4137 /* Replace the empty last line with the current command-line and put the
4138 * cursor there. */
4139 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4140 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4141 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004142 changed_line_abv_curs();
4143 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004144 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar23324a02019-10-01 17:39:04 +02004146 // No Ex mode here!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 exmode_active = 0;
4148
4149 State = NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Bram Moolenaar23324a02019-10-01 17:39:04 +02004152 // Reset here so it can be set by a CmdWinEnter autocommand.
4153 cmdwin_result = 0;
4154
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004155 // Trigger CmdwinEnter autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004156 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004157 if (restart_edit != 0) // autocmd with ":startinsert"
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004158 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 i = RedrawingDisabled;
4161 RedrawingDisabled = 0;
4162
4163 /*
4164 * Call the main loop until <CR> or CTRL-C is typed.
4165 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004166 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 RedrawingDisabled = i;
4169
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004170# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004171 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004172# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004173
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004174 // Trigger CmdwinLeave autocommands.
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004175 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004176
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004177# ifdef FEAT_FOLDING
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004178 // Restore KeyTyped in case it is modified by autocommands
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004179 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180# endif
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 exmode_active = save_exmode;
4184
Bram Moolenaarf9821062008-06-20 16:31:07 +00004185 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004187 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 else
4193 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004194# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 /* autocmds may abort script processing */
4196 if (aborting() && cmdwin_result != K_IGNORE)
4197 cmdwin_result = Ctrl_C;
4198# endif
4199 /* Set the new command line from the cmdline buffer. */
4200 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004201 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004203 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4204
4205 if (histtype == HIST_CMD)
4206 {
4207 /* Execute the command directly. */
4208 ccline.cmdbuff = vim_strsave((char_u *)p);
4209 cmdwin_result = CAR;
4210 }
4211 else
4212 {
4213 /* First need to cancel what we were doing. */
4214 ccline.cmdbuff = NULL;
4215 stuffcharReadbuff(':');
4216 stuffReadbuff((char_u *)p);
4217 stuffcharReadbuff(CAR);
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 else if (cmdwin_result == K_XF2) /* :qa typed */
4221 {
4222 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4223 cmdwin_result = CAR;
4224 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004225 else if (cmdwin_result == Ctrl_C)
4226 {
4227 /* :q or :close, don't execute any command
4228 * and don't modify the cmd window. */
4229 ccline.cmdbuff = NULL;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 else
4232 ccline.cmdbuff = vim_strsave(ml_get_curline());
4233 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004234 {
4235 ccline.cmdbuff = vim_strsave((char_u *)"");
4236 ccline.cmdlen = 0;
4237 ccline.cmdbufflen = 1;
4238 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else
4242 {
4243 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4244 ccline.cmdbufflen = ccline.cmdlen + 1;
4245 ccline.cmdpos = curwin->w_cursor.col;
4246 if (ccline.cmdpos > ccline.cmdlen)
4247 ccline.cmdpos = ccline.cmdlen;
4248 if (cmdwin_result == K_IGNORE)
4249 {
4250 set_cmdspos_cursor();
4251 redrawcmd();
4252 }
4253 }
4254
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004255# ifdef FEAT_CONCEAL
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004256 // Avoid command-line window first character being concealed.
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004257 curwin->w_p_cole = 0;
4258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004260 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 win_goto(old_curwin);
4262 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004263
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004264 // win_close() may have already wiped the buffer when 'bh' is
4265 // set to 'wipe'
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004266 if (bufref_valid(&bufref))
4267 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004269 // Restore window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 win_size_restore(&winsizes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272
4273 ga_clear(&winsizes);
4274 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004275# ifdef FEAT_RIGHTLEFT
4276 cmdmsg_rl = save_cmdmsg_rl;
4277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 State = save_State;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 return cmdwin_result;
4283}
Bram Moolenaar96e38a82019-09-09 18:35:33 +02004284#endif // FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
4286/*
4287 * Used for commands that either take a simple command string argument, or:
4288 * cmd << endmarker
4289 * {script}
4290 * endmarker
4291 * Returns a pointer to allocated memory with {script} or NULL.
4292 */
4293 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004294script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 char_u *theline;
4297 char *end_pattern = NULL;
4298 char dot[] = ".";
4299 garray_T ga;
4300
4301 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4302 return NULL;
4303
4304 ga_init2(&ga, 1, 0x400);
4305
4306 if (cmd[2] != NUL)
4307 end_pattern = (char *)skipwhite(cmd + 2);
4308 else
4309 end_pattern = dot;
4310
4311 for (;;)
4312 {
4313 theline = eap->getline(
4314#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004315 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004317 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004320 {
4321 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 ga_concat(&ga, theline);
4326 ga_append(&ga, '\n');
4327 vim_free(theline);
4328 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004329 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 return (char_u *)ga.ga_data;
4332}
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004333
4334#if defined(FEAT_EVAL) || defined(PROTO)
4335/*
4336 * This function is used by f_input() and f_inputdialog() functions. The third
4337 * argument to f_input() specifies the type of completion to use at the
4338 * prompt. The third argument to f_inputdialog() specifies the value to return
4339 * when the user cancels the prompt.
4340 */
4341 void
4342get_user_input(
4343 typval_T *argvars,
4344 typval_T *rettv,
4345 int inputdialog,
4346 int secret)
4347{
4348 char_u *prompt = tv_get_string_chk(&argvars[0]);
4349 char_u *p = NULL;
4350 int c;
4351 char_u buf[NUMBUFLEN];
4352 int cmd_silent_save = cmd_silent;
4353 char_u *defstr = (char_u *)"";
4354 int xp_type = EXPAND_NOTHING;
4355 char_u *xp_arg = NULL;
4356
4357 rettv->v_type = VAR_STRING;
4358 rettv->vval.v_string = NULL;
4359
4360#ifdef NO_CONSOLE_INPUT
4361 // While starting up, there is no place to enter text. When running tests
4362 // with --not-a-term we assume feedkeys() will be used.
4363 if (no_console_input() && !is_not_a_term())
4364 return;
4365#endif
4366
4367 cmd_silent = FALSE; // Want to see the prompt.
4368 if (prompt != NULL)
4369 {
4370 // Only the part of the message after the last NL is considered as
4371 // prompt for the command line
4372 p = vim_strrchr(prompt, '\n');
4373 if (p == NULL)
4374 p = prompt;
4375 else
4376 {
4377 ++p;
4378 c = *p;
4379 *p = NUL;
4380 msg_start();
4381 msg_clr_eos();
4382 msg_puts_attr((char *)prompt, get_echo_attr());
4383 msg_didout = FALSE;
4384 msg_starthere();
4385 *p = c;
4386 }
4387 cmdline_row = msg_row;
4388
4389 if (argvars[1].v_type != VAR_UNKNOWN)
4390 {
4391 defstr = tv_get_string_buf_chk(&argvars[1], buf);
4392 if (defstr != NULL)
4393 stuffReadbuffSpec(defstr);
4394
4395 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4396 {
4397 char_u *xp_name;
4398 int xp_namelen;
4399 long argt;
4400
4401 // input() with a third argument: completion
4402 rettv->vval.v_string = NULL;
4403
4404 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4405 if (xp_name == NULL)
4406 return;
4407
4408 xp_namelen = (int)STRLEN(xp_name);
4409
4410 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4411 &xp_arg) == FAIL)
4412 return;
4413 }
4414 }
4415
4416 if (defstr != NULL)
4417 {
4418 int save_ex_normal_busy = ex_normal_busy;
4419
4420 ex_normal_busy = 0;
4421 rettv->vval.v_string =
4422 getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4423 xp_type, xp_arg);
4424 ex_normal_busy = save_ex_normal_busy;
4425 }
4426 if (inputdialog && rettv->vval.v_string == NULL
4427 && argvars[1].v_type != VAR_UNKNOWN
4428 && argvars[2].v_type != VAR_UNKNOWN)
4429 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4430 &argvars[2], buf));
4431
4432 vim_free(xp_arg);
4433
4434 // since the user typed this, no need to wait for return
4435 need_wait_return = FALSE;
4436 msg_didout = FALSE;
4437 }
4438 cmd_silent = cmd_silent_save;
4439}
4440#endif