blob: 5d1c8efb7f1a9d4dbaf8f68e6e30575e95fa48a9 [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
139 pos_T save_cursor;
140 viewstate_T init_viewstate;
141 viewstate_T old_viewstate;
142 pos_T match_start;
143 pos_T match_end;
144 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 */
798#ifdef FEAT_MOUSE
799 /* mouse drag and release events are ignored, unless they are
800 * preceded with a mouse down event */
801 int ignore_drag_release = TRUE;
802#endif
803#ifdef FEAT_EVAL
804 int break_ctrl_c = FALSE;
805#endif
806 expand_T xpc;
807 long *b_im_ptr = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200808 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200809 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200810 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar438d1762018-09-30 17:11:48 +0200812 if (ccline.cmdbuff != NULL)
813 {
814 // Being called recursively. Since ccline is global, we need to save
815 // the current buffer and restore it when returning.
816 save_cmdline(&save_ccline);
817 did_save_ccline = TRUE;
818 }
819 if (init_ccline)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200820 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar438d1762018-09-30 17:11:48 +0200821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822#ifdef FEAT_EVAL
823 if (firstc == -1)
824 {
825 firstc = NUL;
826 break_ctrl_c = TRUE;
827 }
828#endif
829#ifdef FEAT_RIGHTLEFT
830 /* start without Hebrew mapping for a command line */
831 if (firstc == ':' || firstc == '=' || firstc == '>')
832 cmd_hkmap = 0;
833#endif
834
835 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200838 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#endif
840
841 /*
842 * set some variables for redrawcmd()
843 */
844 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000845 ccline.cmdindent = (firstc > 0 ? indent : 0);
846
847 /* alloc initial ccline.cmdbuff */
848 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200850 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 ccline.cmdlen = ccline.cmdpos = 0;
852 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100853 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000855 /* autoindent for :insert and :append */
856 if (firstc <= 0)
857 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200858 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000859 ccline.cmdbuff[indent] = NUL;
860 ccline.cmdpos = indent;
861 ccline.cmdspos = indent;
862 ccline.cmdlen = indent;
863 }
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000866 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868#ifdef FEAT_RIGHTLEFT
869 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
870 && (firstc == '/' || firstc == '?'))
871 cmdmsg_rl = TRUE;
872 else
873 cmdmsg_rl = FALSE;
874#endif
875
876 redir_off = TRUE; /* don't redirect the typed command */
877 if (!cmd_silent)
878 {
879 i = msg_scrolled;
880 msg_scrolled = 0; /* avoid wait_return message */
881 gotocmdline(TRUE);
882 msg_scrolled += i;
883 redrawcmdprompt(); /* draw prompt or indent */
884 set_cmdspos();
885 }
886 xpc.xp_context = EXPAND_NOTHING;
887 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000888#ifndef BACKSLASH_IN_FILENAME
889 xpc.xp_shell = FALSE;
890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000892#if defined(FEAT_EVAL)
893 if (ccline.input_fn)
894 {
895 xpc.xp_context = ccline.xp_context;
896 xpc.xp_pattern = ccline.cmdbuff;
897 xpc.xp_arg = ccline.xp_arg;
898 }
899#endif
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /*
902 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
903 * doing ":@0" when register 0 doesn't contain a CR.
904 */
905 msg_scroll = FALSE;
906
907 State = CMDLINE;
908
909 if (firstc == '/' || firstc == '?' || firstc == '@')
910 {
911 /* Use ":lmap" mappings for search pattern and input(). */
912 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
913 b_im_ptr = &curbuf->b_p_iminsert;
914 else
915 b_im_ptr = &curbuf->b_p_imsearch;
916 if (*b_im_ptr == B_IMODE_LMAP)
917 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100918#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 im_set_active(*b_im_ptr == B_IMODE_IM);
920#endif
921 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100922#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else if (p_imcmdline)
924 im_set_active(TRUE);
925#endif
926
927#ifdef FEAT_MOUSE
928 setmouse();
929#endif
930#ifdef CURSOR_SHAPE
931 ui_cursor_shape(); /* may show different cursor shape */
932#endif
933
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000934 /* When inside an autocommand for writing "exiting" may be set and
935 * terminal mode set to cooked. Need to set raw mode here then. */
936 settmode(TMODE_RAW);
937
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200938 /* Trigger CmdlineEnter autocommands. */
939 cmdline_type = firstc == NUL ? '-' : firstc;
940 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200941
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200943 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000947 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200950 /* If something above caused an error, reset the flags, we do want to type
951 * and execute commands. Display may be messed up a bit. */
952 if (did_emsg)
953 redrawcmd();
954 did_emsg = FALSE;
955 got_int = FALSE;
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 /*
958 * Collect the command string, handling editing keys.
959 */
960 for (;;)
961 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000962 redir_off = TRUE; /* Don't redirect the typed command.
963 Repeated, because a ":redir" inside
964 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#ifdef USE_ON_FLY_SCROLL
966 dont_scroll = FALSE; /* allow scrolling here */
967#endif
968 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
969
Bram Moolenaar72532d32018-04-07 19:09:09 +0200970 did_emsg = FALSE; /* There can't really be a reason why an error
971 that occurs while typing a command should
972 cause the command not to be executed. */
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000975
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100976 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
977 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000978 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000979 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100980 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (KeyTyped)
983 {
984 some_key_typed = TRUE;
985#ifdef FEAT_RIGHTLEFT
986 if (cmd_hkmap)
987 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (cmdmsg_rl && !KeyStuffed)
989 {
990 /* Invert horizontal movements and operations. Only when
991 * typed by the user directly, not when the result of a
992 * mapping. */
993 switch (c)
994 {
995 case K_RIGHT: c = K_LEFT; break;
996 case K_S_RIGHT: c = K_S_LEFT; break;
997 case K_C_RIGHT: c = K_C_LEFT; break;
998 case K_LEFT: c = K_RIGHT; break;
999 case K_S_LEFT: c = K_S_RIGHT; break;
1000 case K_C_LEFT: c = K_C_RIGHT; break;
1001 }
1002 }
1003#endif
1004 }
1005
1006 /*
1007 * Ignore got_int when CTRL-C was typed here.
1008 * Don't ignore it in :global, we really need to break then, e.g., for
1009 * ":g/pat/normal /pat" (without the <CR>).
1010 * Don't ignore it for the input() function.
1011 */
1012 if ((c == Ctrl_C
1013#ifdef UNIX
1014 || c == intr_char
1015#endif
1016 )
1017#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1018 && firstc != '@'
1019#endif
1020#ifdef FEAT_EVAL
1021 && !break_ctrl_c
1022#endif
1023 && !global_busy)
1024 got_int = FALSE;
1025
Bram Moolenaard7663c22019-08-06 21:59:57 +02001026 // free old command line when finished moving around in the history
1027 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001029 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001030 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && c != K_PAGEDOWN && c != K_PAGEUP
1032 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001033 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001035 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001038 * When there are matching completions to select <S-Tab> works like
1039 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001041 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 c = Ctrl_P;
1043
1044#ifdef FEAT_WILDMENU
1045 /* Special translations for 'wildmenu' */
1046 if (did_wild_list && p_wmnu)
1047 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001048 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001050 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 c = Ctrl_N;
1052 }
1053 /* Hitting CR after "emenu Name.": complete submenu */
1054 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1055 && ccline.cmdpos > 1
1056 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1057 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1058 && (c == '\n' || c == '\r' || c == K_KENTER))
1059 c = K_DOWN;
1060#endif
1061
1062 /* free expanded names when finished walking through matches */
1063 if (xpc.xp_numfiles != -1
1064 && !(c == p_wc && KeyTyped) && c != p_wcm
1065 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1066 && c != Ctrl_L)
1067 {
1068 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1069 did_wild_list = FALSE;
1070#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001071 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
1073 xpc.xp_context = EXPAND_NOTHING;
1074 wim_index = 0;
1075#ifdef FEAT_WILDMENU
1076 if (p_wmnu && wild_menu_showing != 0)
1077 {
1078 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001079 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001080
1081 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001082 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 if (wild_menu_showing == WM_SCROLLED)
1085 {
1086 /* Entered command line, move it up */
1087 cmdline_row--;
1088 redrawcmd();
1089 }
1090 else if (save_p_ls != -1)
1091 {
1092 /* restore 'laststatus' and 'winminheight' */
1093 p_ls = save_p_ls;
1094 p_wmh = save_p_wmh;
1095 last_status(FALSE);
1096 update_screen(VALID); /* redraw the screen NOW */
1097 redrawcmd();
1098 save_p_ls = -1;
1099 }
1100 else
1101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 redraw_statuslines();
1104 }
1105 KeyTyped = skt;
1106 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001107 if (ccline.input_fn)
1108 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110#endif
1111 }
1112
1113#ifdef FEAT_WILDMENU
1114 /* Special translations for 'wildmenu' */
1115 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1116 {
1117 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001118 if (c == K_DOWN && ccline.cmdpos > 0
1119 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001121 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 /* Hitting <Up>: Remove one submenu name in front of the
1124 * cursor */
1125 int found = FALSE;
1126
1127 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1128 i = 0;
1129 while (--j > 0)
1130 {
1131 /* check for start of menu name */
1132 if (ccline.cmdbuff[j] == ' '
1133 && ccline.cmdbuff[j - 1] != '\\')
1134 {
1135 i = j + 1;
1136 break;
1137 }
1138 /* check for start of submenu name */
1139 if (ccline.cmdbuff[j] == '.'
1140 && ccline.cmdbuff[j - 1] != '\\')
1141 {
1142 if (found)
1143 {
1144 i = j + 1;
1145 break;
1146 }
1147 else
1148 found = TRUE;
1149 }
1150 }
1151 if (i > 0)
1152 cmdline_del(i);
1153 c = p_wc;
1154 xpc.xp_context = EXPAND_NOTHING;
1155 }
1156 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001157 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001158 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001159 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 char_u upseg[5];
1162
1163 upseg[0] = PATHSEP;
1164 upseg[1] = '.';
1165 upseg[2] = '.';
1166 upseg[3] = PATHSEP;
1167 upseg[4] = NUL;
1168
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001169 if (c == K_DOWN
1170 && ccline.cmdpos > 0
1171 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1172 && (ccline.cmdpos < 3
1173 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1175 {
1176 /* go down a directory */
1177 c = p_wc;
1178 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001179 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 {
1181 /* If in a direct ancestor, strip off one ../ to go down */
1182 int found = FALSE;
1183
1184 j = ccline.cmdpos;
1185 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1186 while (--j > i)
1187 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001188 if (has_mbyte)
1189 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (vim_ispathsep(ccline.cmdbuff[j]))
1191 {
1192 found = TRUE;
1193 break;
1194 }
1195 }
1196 if (found
1197 && ccline.cmdbuff[j - 1] == '.'
1198 && ccline.cmdbuff[j - 2] == '.'
1199 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1200 {
1201 cmdline_del(j - 2);
1202 c = p_wc;
1203 }
1204 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001205 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 /* go up a directory */
1208 int found = FALSE;
1209
1210 j = ccline.cmdpos - 1;
1211 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1212 while (--j > i)
1213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (has_mbyte)
1215 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (vim_ispathsep(ccline.cmdbuff[j])
1217#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001218 && vim_strchr((char_u *)" *?[{`$%#",
1219 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 )
1222 {
1223 if (found)
1224 {
1225 i = j + 1;
1226 break;
1227 }
1228 else
1229 found = TRUE;
1230 }
1231 }
1232
1233 if (!found)
1234 j = i;
1235 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1236 j += 4;
1237 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1238 && j == i)
1239 j += 3;
1240 else
1241 j = 0;
1242 if (j > 0)
1243 {
1244 /* TODO this is only for DOS/UNIX systems - need to put in
1245 * machine-specific stuff here and in upseg init */
1246 cmdline_del(j);
1247 put_on_cmdline(upseg + 1, 3, FALSE);
1248 }
1249 else if (ccline.cmdpos > i)
1250 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001251
1252 /* Now complete in the new directory. Set KeyTyped in case the
1253 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001255 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259#endif /* FEAT_WILDMENU */
1260
1261 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1262 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1263 if (c == Ctrl_BSL)
1264 {
1265 ++no_mapping;
1266 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001267 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 --no_mapping;
1269 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001270 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1271 * is in a mapping. */
1272 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001273 || (ccline.cmdfirstc == '=' && KeyTyped)
1274#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001275 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001276#endif
1277 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 vungetc(c);
1280 c = Ctrl_BSL;
1281 }
1282#ifdef FEAT_EVAL
1283 else if (c == 'e')
1284 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001285 char_u *p = NULL;
1286 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 /*
1289 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001290 * Need to save and restore the current command line, to be
1291 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 */
1293 if (ccline.cmdpos == ccline.cmdlen)
1294 new_cmdpos = 99999; /* keep it at the end */
1295 else
1296 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (c == '=')
1300 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001301 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001302 * to avoid nasty things like going to another buffer when
1303 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001304 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001306 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001307
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001308 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001310 len = (int)STRLEN(p);
1311 if (realloc_cmdbuff(len + 1) == OK)
1312 {
1313 ccline.cmdlen = len;
1314 STRCPY(ccline.cmdbuff, p);
1315 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001317 /* Restore the cursor or use the position set with
1318 * set_cmdline_pos(). */
1319 if (new_cmdpos > ccline.cmdlen)
1320 ccline.cmdpos = ccline.cmdlen;
1321 else
1322 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001324 KeyTyped = FALSE; /* Don't do p_wc completion. */
1325 redrawcmd();
1326 goto cmdline_changed;
1327 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001328 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 }
1331 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001332 got_int = FALSE; /* don't abandon the command line */
1333 did_emsg = FALSE;
1334 emsg_on_display = FALSE;
1335 redrawcmd();
1336 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338#endif
1339 else
1340 {
1341 if (c == Ctrl_G && p_im && restart_edit == 0)
1342 restart_edit = 'a';
1343 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1344 in history */
1345 goto returncmd; /* back to Normal mode */
1346 }
1347 }
1348
1349#ifdef FEAT_CMDWIN
1350 if (c == cedit_key || c == K_CMDWIN)
1351 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001352 if (ex_normal_busy == 0 && got_int == FALSE)
1353 {
1354 /*
1355 * Open a window to edit the command line (and history).
1356 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001357 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001358 some_key_typed = TRUE;
1359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361# ifdef FEAT_DIGRAPHS
1362 else
1363# endif
1364#endif
1365#ifdef FEAT_DIGRAPHS
1366 c = do_digraph(c);
1367#endif
1368
1369 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1370 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1371 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001372 /* In Ex mode a backslash escapes a newline. */
1373 if (exmode_active
1374 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001375 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001376 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001377 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001379 if (c == K_KENTER)
1380 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001382 else
1383 {
1384 gotesc = FALSE; /* Might have typed ESC previously, don't
1385 truncate the cmdline now. */
1386 if (ccheck_abbr(c + ABBR_OFF))
1387 goto cmdline_changed;
1388 if (!cmd_silent)
1389 {
1390 windgoto(msg_row, 0);
1391 out_flush();
1392 }
1393 break;
1394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396
1397 /*
1398 * Completion for 'wildchar' or 'wildcharm' key.
1399 * - hitting <ESC> twice means: abandon command line.
1400 * - wildcard expansion is only done when the 'wildchar' key is really
1401 * typed, not when it comes from a macro
1402 */
1403 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1404 {
1405 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1406 {
1407 /* if 'wildmode' contains "list" may still need to list */
1408 if (xpc.xp_numfiles > 1
1409 && !did_wild_list
1410 && (wim_flags[wim_index] & WIM_LIST))
1411 {
1412 (void)showmatches(&xpc, FALSE);
1413 redrawcmd();
1414 did_wild_list = TRUE;
1415 }
1416 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001417 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1418 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001420 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1421 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 else
1423 res = OK; /* don't insert 'wildchar' now */
1424 }
1425 else /* typed p_wc first time */
1426 {
1427 wim_index = 0;
1428 j = ccline.cmdpos;
1429 /* if 'wildmode' first contains "longest", get longest
1430 * common part */
1431 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001432 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1433 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001435 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1436 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 /* if interrupted while completing, behave like it failed */
1439 if (got_int)
1440 {
1441 (void)vpeekc(); /* remove <C-C> from input stream */
1442 got_int = FALSE; /* don't abandon the command line */
1443 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1444#ifdef FEAT_WILDMENU
1445 xpc.xp_context = EXPAND_NOTHING;
1446#endif
1447 goto cmdline_changed;
1448 }
1449
1450 /* when more than one match, and 'wildmode' first contains
1451 * "list", or no change and 'wildmode' contains "longest,list",
1452 * list all matches */
1453 if (res == OK && xpc.xp_numfiles > 1)
1454 {
1455 /* a "longest" that didn't do anything is skipped (but not
1456 * "list:longest") */
1457 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1458 wim_index = 1;
1459 if ((wim_flags[wim_index] & WIM_LIST)
1460#ifdef FEAT_WILDMENU
1461 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1462#endif
1463 )
1464 {
1465 if (!(wim_flags[0] & WIM_LONGEST))
1466 {
1467#ifdef FEAT_WILDMENU
1468 int p_wmnu_save = p_wmnu;
1469 p_wmnu = 0;
1470#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001471 /* remove match */
1472 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473#ifdef FEAT_WILDMENU
1474 p_wmnu = p_wmnu_save;
1475#endif
1476 }
1477#ifdef FEAT_WILDMENU
1478 (void)showmatches(&xpc, p_wmnu
1479 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1480#else
1481 (void)showmatches(&xpc, FALSE);
1482#endif
1483 redrawcmd();
1484 did_wild_list = TRUE;
1485 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1487 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001489 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1490 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001493 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495#ifdef FEAT_WILDMENU
1496 else if (xpc.xp_numfiles == -1)
1497 xpc.xp_context = EXPAND_NOTHING;
1498#endif
1499 }
1500 if (wim_index < 3)
1501 ++wim_index;
1502 if (c == ESC)
1503 gotesc = TRUE;
1504 if (res == OK)
1505 goto cmdline_changed;
1506 }
1507
1508 gotesc = FALSE;
1509
1510 /* <S-Tab> goes to last match, in a clumsy way */
1511 if (c == K_S_TAB && KeyTyped)
1512 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001513 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1514 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1515 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 goto cmdline_changed;
1517 }
1518
1519 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1520 c = NL;
1521
1522 do_abbr = TRUE; /* default: check for abbreviation */
1523
1524 /*
1525 * Big switch for a typed command line character.
1526 */
1527 switch (c)
1528 {
1529 case K_BS:
1530 case Ctrl_H:
1531 case K_DEL:
1532 case K_KDEL:
1533 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (c == K_KDEL)
1535 c = K_DEL;
1536
1537 /*
1538 * delete current character is the same as backspace on next
1539 * character, except at end of line
1540 */
1541 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1542 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (has_mbyte && c == K_DEL)
1544 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1545 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (ccline.cmdpos > 0)
1547 {
1548 char_u *p;
1549
1550 j = ccline.cmdpos;
1551 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (has_mbyte)
1553 {
1554 p = mb_prevptr(ccline.cmdbuff, p);
1555 if (c == Ctrl_W)
1556 {
1557 while (p > ccline.cmdbuff && vim_isspace(*p))
1558 p = mb_prevptr(ccline.cmdbuff, p);
1559 i = mb_get_class(p);
1560 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1561 p = mb_prevptr(ccline.cmdbuff, p);
1562 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001563 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001566 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1569 --p;
1570 i = vim_iswordc(p[-1]);
1571 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1572 && vim_iswordc(p[-1]) == i)
1573 --p;
1574 }
1575 else
1576 --p;
1577 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1578 ccline.cmdlen -= j - ccline.cmdpos;
1579 i = ccline.cmdpos;
1580 while (i < ccline.cmdlen)
1581 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1582
1583 /* Truncate at the end, required for multi-byte chars. */
1584 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001585#ifdef FEAT_SEARCH_EXTRA
1586 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001587 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001588 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001589 /* save view settings, so that the screen
1590 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001591 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001592 }
1593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 redrawcmd();
1595 }
1596 else if (ccline.cmdlen == 0 && c != Ctrl_W
1597 && ccline.cmdprompt == NULL && indent == 0)
1598 {
1599 /* In ex and debug mode it doesn't make sense to return. */
1600 if (exmode_active
1601#ifdef FEAT_EVAL
1602 || ccline.cmdfirstc == '>'
1603#endif
1604 )
1605 goto cmdline_not_changed;
1606
Bram Moolenaard23a8232018-02-10 18:45:26 +01001607 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!cmd_silent)
1609 {
1610#ifdef FEAT_RIGHTLEFT
1611 if (cmdmsg_rl)
1612 msg_col = Columns;
1613 else
1614#endif
1615 msg_col = 0;
1616 msg_putchar(' '); /* delete ':' */
1617 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001618#ifdef FEAT_SEARCH_EXTRA
1619 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001620 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 redraw_cmdline = TRUE;
1623 goto returncmd; /* back to cmd mode */
1624 }
1625 goto cmdline_changed;
1626
1627 case K_INS:
1628 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ccline.overstrike = !ccline.overstrike;
1630#ifdef CURSOR_SHAPE
1631 ui_cursor_shape(); /* may show different cursor shape */
1632#endif
1633 goto cmdline_not_changed;
1634
1635 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001636 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 {
1638 /* ":lmap" mappings exists, toggle use of mappings. */
1639 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001640#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 im_set_active(FALSE); /* Disable input method */
1642#endif
1643 if (b_im_ptr != NULL)
1644 {
1645 if (State & LANGMAP)
1646 *b_im_ptr = B_IMODE_LMAP;
1647 else
1648 *b_im_ptr = B_IMODE_NONE;
1649 }
1650 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001651#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 else
1653 {
1654 /* There are no ":lmap" mappings, toggle IM. When
1655 * 'imdisable' is set don't try getting the status, it's
1656 * always off. */
1657 if ((p_imdisable && b_im_ptr != NULL)
1658 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1659 {
1660 im_set_active(FALSE); /* Disable input method */
1661 if (b_im_ptr != NULL)
1662 *b_im_ptr = B_IMODE_NONE;
1663 }
1664 else
1665 {
1666 im_set_active(TRUE); /* Enable input method */
1667 if (b_im_ptr != NULL)
1668 *b_im_ptr = B_IMODE_IM;
1669 }
1670 }
1671#endif
1672 if (b_im_ptr != NULL)
1673 {
1674 if (b_im_ptr == &curbuf->b_p_iminsert)
1675 set_iminsert_global();
1676 else
1677 set_imsearch_global();
1678 }
1679#ifdef CURSOR_SHAPE
1680 ui_cursor_shape(); /* may show different cursor shape */
1681#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001682#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 /* Show/unshow value of 'keymap' in status lines later. */
1684 status_redraw_curbuf();
1685#endif
1686 goto cmdline_not_changed;
1687
1688/* case '@': only in very old vi */
1689 case Ctrl_U:
1690 /* delete all characters left of the cursor */
1691 j = ccline.cmdpos;
1692 ccline.cmdlen -= j;
1693 i = ccline.cmdpos = 0;
1694 while (i < ccline.cmdlen)
1695 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1696 /* Truncate at the end, required for multi-byte chars. */
1697 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001698#ifdef FEAT_SEARCH_EXTRA
1699 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001700 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 redrawcmd();
1703 goto cmdline_changed;
1704
1705#ifdef FEAT_CLIPBOARD
1706 case Ctrl_Y:
1707 /* Copy the modeless selection, if there is one. */
1708 if (clip_star.state != SELECT_CLEARED)
1709 {
1710 if (clip_star.state == SELECT_DONE)
1711 clip_copy_modeless_selection(TRUE);
1712 goto cmdline_not_changed;
1713 }
1714 break;
1715#endif
1716
1717 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1718 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001719 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001720 * ":normal" runs out of characters. */
1721 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001722 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 goto cmdline_not_changed;
1724
1725 gotesc = TRUE; /* will free ccline.cmdbuff after
1726 putting it in history */
1727 goto returncmd; /* back to cmd mode */
1728
1729 case Ctrl_R: /* insert register */
1730#ifdef USE_ON_FLY_SCROLL
1731 dont_scroll = TRUE; /* disallow scrolling here */
1732#endif
1733 putcmdline('"', TRUE);
1734 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001735 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (i == Ctrl_O)
1737 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1738 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001739 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001740 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 --no_mapping;
1742#ifdef FEAT_EVAL
1743 /*
1744 * Insert the result of an expression.
1745 * Need to save the current command line, to be able to enter
1746 * a new one...
1747 */
1748 new_cmdpos = -1;
1749 if (c == '=')
1750 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001751 if (ccline.cmdfirstc == '=' // can't do this recursively
1752 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
1754 beep_flush();
1755 c = ESC;
1756 }
1757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760#endif
1761 if (c != ESC) /* use ESC to cancel inserting register */
1762 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001763 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001764
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001765#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001766 /* When there was a serious error abort getting the
1767 * command line. */
1768 if (aborting())
1769 {
1770 gotesc = TRUE; /* will free ccline.cmdbuff after
1771 putting it in history */
1772 goto returncmd; /* back to cmd mode */
1773 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 KeyTyped = FALSE; /* Don't do p_wc completion. */
1776#ifdef FEAT_EVAL
1777 if (new_cmdpos >= 0)
1778 {
1779 /* set_cmdline_pos() was used */
1780 if (new_cmdpos > ccline.cmdlen)
1781 ccline.cmdpos = ccline.cmdlen;
1782 else
1783 ccline.cmdpos = new_cmdpos;
1784 }
1785#endif
1786 }
1787 redrawcmd();
1788 goto cmdline_changed;
1789
1790 case Ctrl_D:
1791 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1792 break; /* Use ^D as normal char instead */
1793
1794 redrawcmd();
1795 continue; /* don't do incremental search now */
1796
1797 case K_RIGHT:
1798 case K_S_RIGHT:
1799 case K_C_RIGHT:
1800 do
1801 {
1802 if (ccline.cmdpos >= ccline.cmdlen)
1803 break;
1804 i = cmdline_charsize(ccline.cmdpos);
1805 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1806 break;
1807 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001809 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 + ccline.cmdpos);
1811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 ++ccline.cmdpos;
1813 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001814 while ((c == K_S_RIGHT || c == K_C_RIGHT
1815 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (has_mbyte)
1818 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 goto cmdline_not_changed;
1820
1821 case K_LEFT:
1822 case K_S_LEFT:
1823 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001824 if (ccline.cmdpos == 0)
1825 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 do
1827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (has_mbyte) /* move to first byte of char */
1830 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1831 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1833 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001834 while (ccline.cmdpos > 0
1835 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001836 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (has_mbyte)
1839 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 goto cmdline_not_changed;
1841
1842 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001843 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001844 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar4f974752019-02-17 17:44:42 +01001846#ifdef FEAT_GUI_MSWIN
1847 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1848 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001849 case K_F4:
1850 if (mod_mask == MOD_MASK_ALT)
1851 {
1852 redrawcmd(); /* somehow the cmdline is cleared */
1853 goto cmdline_not_changed;
1854 }
1855 break;
1856#endif
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#ifdef FEAT_MOUSE
1859 case K_MIDDLEDRAG:
1860 case K_MIDDLERELEASE:
1861 goto cmdline_not_changed; /* Ignore mouse */
1862
1863 case K_MIDDLEMOUSE:
1864# ifdef FEAT_GUI
1865 /* When GUI is active, also paste when 'mouse' is empty */
1866 if (!gui.in_use)
1867# endif
1868 if (!mouse_has(MOUSE_COMMAND))
1869 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001870# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001872 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001874# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001875 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 redrawcmd();
1877 goto cmdline_changed;
1878
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001879# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001881 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 redrawcmd();
1883 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 case K_LEFTDRAG:
1887 case K_LEFTRELEASE:
1888 case K_RIGHTDRAG:
1889 case K_RIGHTRELEASE:
1890 /* Ignore drag and release events when the button-down wasn't
1891 * seen before. */
1892 if (ignore_drag_release)
1893 goto cmdline_not_changed;
1894 /* FALLTHROUGH */
1895 case K_LEFTMOUSE:
1896 case K_RIGHTMOUSE:
1897 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1898 ignore_drag_release = TRUE;
1899 else
1900 ignore_drag_release = FALSE;
1901# ifdef FEAT_GUI
1902 /* When GUI is active, also move when 'mouse' is empty */
1903 if (!gui.in_use)
1904# endif
1905 if (!mouse_has(MOUSE_COMMAND))
1906 goto cmdline_not_changed; /* Ignore mouse */
1907# ifdef FEAT_CLIPBOARD
1908 if (mouse_row < cmdline_row && clip_star.available)
1909 {
1910 int button, is_click, is_drag;
1911
1912 /*
1913 * Handle modeless selection.
1914 */
1915 button = get_mouse_button(KEY2TERMCAP1(c),
1916 &is_click, &is_drag);
1917 if (mouse_model_popup() && button == MOUSE_LEFT
1918 && (mod_mask & MOD_MASK_SHIFT))
1919 {
1920 /* Translate shift-left to right button. */
1921 button = MOUSE_RIGHT;
1922 mod_mask &= ~MOD_MASK_SHIFT;
1923 }
1924 clip_modeless(button, is_click, is_drag);
1925 goto cmdline_not_changed;
1926 }
1927# endif
1928
1929 set_cmdspos();
1930 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1931 ++ccline.cmdpos)
1932 {
1933 i = cmdline_charsize(ccline.cmdpos);
1934 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1935 && mouse_col < ccline.cmdspos % Columns + i)
1936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (has_mbyte)
1938 {
1939 /* Count ">" for double-wide char that doesn't fit. */
1940 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001941 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 + ccline.cmdpos) - 1;
1943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 ccline.cmdspos += i;
1945 }
1946 goto cmdline_not_changed;
1947
1948 /* Mouse scroll wheel: ignored here */
1949 case K_MOUSEDOWN:
1950 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001951 case K_MOUSELEFT:
1952 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 /* Alternate buttons ignored here */
1954 case K_X1MOUSE:
1955 case K_X1DRAG:
1956 case K_X1RELEASE:
1957 case K_X2MOUSE:
1958 case K_X2DRAG:
1959 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001960 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 goto cmdline_not_changed;
1962
1963#endif /* FEAT_MOUSE */
1964
1965#ifdef FEAT_GUI
1966 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1967 case K_LEFTRELEASE_NM:
1968 goto cmdline_not_changed;
1969
1970 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001971 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 gui_do_scroll();
1974 redrawcmd();
1975 }
1976 goto cmdline_not_changed;
1977
1978 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001979 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001981 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 redrawcmd();
1983 }
1984 goto cmdline_not_changed;
1985#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001986#ifdef FEAT_GUI_TABLINE
1987 case K_TABLINE:
1988 case K_TABMENU:
1989 /* Don't want to change any tabs here. Make sure the same tab
1990 * is still selected. */
1991 if (gui_use_tabline())
1992 gui_mch_set_curtab(tabpage_index(curtab));
1993 goto cmdline_not_changed;
1994#endif
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 case K_SELECT: /* end of Select mode mapping - ignore */
1997 goto cmdline_not_changed;
1998
1999 case Ctrl_B: /* begin of command line */
2000 case K_HOME:
2001 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 case K_S_HOME:
2003 case K_C_HOME:
2004 ccline.cmdpos = 0;
2005 set_cmdspos();
2006 goto cmdline_not_changed;
2007
2008 case Ctrl_E: /* end of command line */
2009 case K_END:
2010 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 case K_S_END:
2012 case K_C_END:
2013 ccline.cmdpos = ccline.cmdlen;
2014 set_cmdspos_cursor();
2015 goto cmdline_not_changed;
2016
2017 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002018 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 break;
2020 goto cmdline_changed;
2021
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002022 case Ctrl_L:
2023#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002024 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002025 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002026#endif
2027
2028 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002029 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031 goto cmdline_changed;
2032
2033 case Ctrl_N: /* next match */
2034 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002035 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002037 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2038 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002040 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002042 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 case K_UP:
2044 case K_DOWN:
2045 case K_S_UP:
2046 case K_S_DOWN:
2047 case K_PAGEUP:
2048 case K_KPAGEUP:
2049 case K_PAGEDOWN:
2050 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002051 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 goto cmdline_not_changed;
2053
2054 i = hiscnt;
2055
2056 /* save current command string so it can be restored later */
2057 if (lookfor == NULL)
2058 {
2059 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2060 goto cmdline_not_changed;
2061 lookfor[ccline.cmdpos] = NUL;
2062 }
2063
2064 j = (int)STRLEN(lookfor);
2065 for (;;)
2066 {
2067 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002068 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002069 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002071 if (hiscnt == get_hislen()) /* first time */
2072 hiscnt = *get_hisidx(histype);
2073 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2074 hiscnt = get_hislen() - 1;
2075 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 --hiscnt;
2077 else /* at top of list */
2078 {
2079 hiscnt = i;
2080 break;
2081 }
2082 }
2083 else /* one step forwards */
2084 {
2085 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002086 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002088 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 break;
2090 }
2091
2092 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002093 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002095 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 hiscnt = 0;
2097 else
2098 ++hiscnt;
2099 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002100 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
2102 hiscnt = i;
2103 break;
2104 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002105 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002106 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002107 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 lookfor, (size_t)j) == 0)
2109 break;
2110 }
2111
2112 if (hiscnt != i) /* jumped to other entry */
2113 {
2114 char_u *p;
2115 int len;
2116 int old_firstc;
2117
Bram Moolenaar438d1762018-09-30 17:11:48 +02002118 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002119 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002120 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 p = lookfor; /* back to the old one */
2122 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002123 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 if (histype == HIST_SEARCH
2126 && p != lookfor
2127 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2128 {
2129 /* Correct for the separator character used when
2130 * adding the history entry vs the one used now.
2131 * First loop: count length.
2132 * Second loop: copy the characters. */
2133 for (i = 0; i <= 1; ++i)
2134 {
2135 len = 0;
2136 for (j = 0; p[j] != NUL; ++j)
2137 {
2138 /* Replace old sep with new sep, unless it is
2139 * escaped. */
2140 if (p[j] == old_firstc
2141 && (j == 0 || p[j - 1] != '\\'))
2142 {
2143 if (i > 0)
2144 ccline.cmdbuff[len] = firstc;
2145 }
2146 else
2147 {
2148 /* Escape new sep, unless it is already
2149 * escaped. */
2150 if (p[j] == firstc
2151 && (j == 0 || p[j - 1] != '\\'))
2152 {
2153 if (i > 0)
2154 ccline.cmdbuff[len] = '\\';
2155 ++len;
2156 }
2157 if (i > 0)
2158 ccline.cmdbuff[len] = p[j];
2159 }
2160 ++len;
2161 }
2162 if (i == 0)
2163 {
2164 alloc_cmdbuff(len);
2165 if (ccline.cmdbuff == NULL)
2166 goto returncmd;
2167 }
2168 }
2169 ccline.cmdbuff[len] = NUL;
2170 }
2171 else
2172 {
2173 alloc_cmdbuff((int)STRLEN(p));
2174 if (ccline.cmdbuff == NULL)
2175 goto returncmd;
2176 STRCPY(ccline.cmdbuff, p);
2177 }
2178
2179 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2180 redrawcmd();
2181 goto cmdline_changed;
2182 }
2183 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002184 goto cmdline_not_changed;
2185
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002186#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002187 case Ctrl_G: /* next match */
2188 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002189 if (may_adjust_incsearch_highlighting(
2190 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002191 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#endif
2194
2195 case Ctrl_V:
2196 case Ctrl_Q:
2197#ifdef FEAT_MOUSE
2198 ignore_drag_release = TRUE;
2199#endif
2200 putcmdline('^', TRUE);
2201 c = get_literal(); /* get next (two) character(s) */
2202 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002203 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /* may need to remove ^ when composing char was typed */
2205 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2206 {
2207 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2208 msg_putchar(' ');
2209 cursorcmd();
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 break;
2212
2213#ifdef FEAT_DIGRAPHS
2214 case Ctrl_K:
2215#ifdef FEAT_MOUSE
2216 ignore_drag_release = TRUE;
2217#endif
2218 putcmdline('?', TRUE);
2219#ifdef USE_ON_FLY_SCROLL
2220 dont_scroll = TRUE; /* disallow scrolling here */
2221#endif
2222 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002223 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 if (c != NUL)
2225 break;
2226
2227 redrawcmd();
2228 goto cmdline_not_changed;
2229#endif /* FEAT_DIGRAPHS */
2230
2231#ifdef FEAT_RIGHTLEFT
2232 case Ctrl__: /* CTRL-_: switch language mode */
2233 if (!p_ari)
2234 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002235 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 goto cmdline_not_changed;
2237#endif
2238
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002239 case K_PS:
2240 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2241 goto cmdline_changed;
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 default:
2244#ifdef UNIX
2245 if (c == intr_char)
2246 {
2247 gotesc = TRUE; /* will free ccline.cmdbuff after
2248 putting it in history */
2249 goto returncmd; /* back to Normal mode */
2250 }
2251#endif
2252 /*
2253 * Normal character with no special meaning. Just set mod_mask
2254 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2255 * the string <S-Space>. This should only happen after ^V.
2256 */
2257 if (!IS_SPECIAL(c))
2258 mod_mask = 0x0;
2259 break;
2260 }
2261 /*
2262 * End of switch on command line character.
2263 * We come here if we have a normal character.
2264 */
2265
Bram Moolenaar13505972019-01-24 15:04:48 +01002266 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2267 && (ccheck_abbr(
2268 // Add ABBR_OFF for characters above 0x100, this is
2269 // what check_abbr() expects.
2270 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2271 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 goto cmdline_changed;
2273
2274 /*
2275 * put the character in the command line
2276 */
2277 if (IS_SPECIAL(c) || mod_mask != 0)
2278 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2279 else
2280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (has_mbyte)
2282 {
2283 j = (*mb_char2bytes)(c, IObuff);
2284 IObuff[j] = NUL; /* exclude composing chars */
2285 put_on_cmdline(IObuff, j, TRUE);
2286 }
2287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 IObuff[0] = c;
2290 put_on_cmdline(IObuff, 1, TRUE);
2291 }
2292 }
2293 goto cmdline_changed;
2294
2295/*
2296 * This part implements incremental searches for "/" and "?"
2297 * Jump to cmdline_not_changed when a character has been read but the command
2298 * line did not change. Then we only search and redraw if something changed in
2299 * the past.
2300 * Jump to cmdline_changed when the command line did change.
2301 * (Sorry for the goto's, I know it is ugly).
2302 */
2303cmdline_not_changed:
2304#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002305 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 continue;
2307#endif
2308
2309cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002310 /* Trigger CmdlineChanged autocommands. */
2311 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002314 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#endif
2316
2317#ifdef FEAT_RIGHTLEFT
2318 if (cmdmsg_rl
2319# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002320 || (p_arshape && !p_tbidi
2321 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322# endif
2323 )
2324 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002325 * right-left typing. Not efficient, but it works.
2326 * Do it only when there are no characters left to read
2327 * to avoid useless intermediate redraws. */
2328 if (vpeekc() == NUL)
2329 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331 }
2332
2333returncmd:
2334
2335#ifdef FEAT_RIGHTLEFT
2336 cmdmsg_rl = FALSE;
2337#endif
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002340 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002343 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#endif
2345
2346 if (ccline.cmdbuff != NULL)
2347 {
2348 /*
2349 * Put line in history buffer (":" and "=" only when it was typed).
2350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (ccline.cmdlen && firstc != NUL
2352 && (some_key_typed || histype == HIST_SEARCH))
2353 {
2354 add_to_history(histype, ccline.cmdbuff, TRUE,
2355 histype == HIST_SEARCH ? firstc : NUL);
2356 if (firstc == ':')
2357 {
2358 vim_free(new_last_cmdline);
2359 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2360 }
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002363 if (gotesc)
2364 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 /*
2368 * If the screen was shifted up, redraw the whole screen (later).
2369 * If the line is too long, clear it, so ruler and shown command do
2370 * not get printed in the middle of it.
2371 */
2372 msg_check();
2373 msg_scroll = save_msg_scroll;
2374 redir_off = FALSE;
2375
2376 /* When the command line was typed, no need for a wait-return prompt. */
2377 if (some_key_typed)
2378 need_wait_return = FALSE;
2379
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002380 /* Trigger CmdlineLeave autocommands. */
2381 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002384#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2386 im_save_status(b_im_ptr);
2387 im_set_active(FALSE);
2388#endif
2389#ifdef FEAT_MOUSE
2390 setmouse();
2391#endif
2392#ifdef CURSOR_SHAPE
2393 ui_cursor_shape(); /* may show different cursor shape */
2394#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002395 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
Bram Moolenaar438d1762018-09-30 17:11:48 +02002397theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002398 {
2399 char_u *p = ccline.cmdbuff;
2400
Bram Moolenaar438d1762018-09-30 17:11:48 +02002401 if (did_save_ccline)
2402 restore_cmdline(&save_ccline);
2403 else
2404 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002405 return p;
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407}
2408
2409#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2410/*
2411 * Get a command line with a prompt.
2412 * This is prepared to be called recursively from getcmdline() (e.g. by
2413 * f_input() when evaluating an expression from CTRL-R =).
2414 * Returns the command line in allocated memory, or NULL.
2415 */
2416 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002417getcmdline_prompt(
2418 int firstc,
2419 char_u *prompt, /* command line prompt */
2420 int attr, /* attributes for prompt */
2421 int xp_context, /* type of expansion */
2422 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002425 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002426 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002428 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
Bram Moolenaar438d1762018-09-30 17:11:48 +02002430 if (ccline.cmdbuff != NULL)
2431 {
2432 // Save the values of the current cmdline and restore them below.
2433 save_cmdline(&save_ccline);
2434 did_save_ccline = TRUE;
2435 }
2436
Bram Moolenaar66b51422019-08-18 21:44:12 +02002437 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 ccline.cmdprompt = prompt;
2439 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002440# ifdef FEAT_EVAL
2441 ccline.xp_context = xp_context;
2442 ccline.xp_arg = xp_arg;
2443 ccline.input_fn = (firstc == '@');
2444# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002445 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002446 s = getcmdline_int(firstc, 1L, 0, FALSE);
2447
2448 if (did_save_ccline)
2449 restore_cmdline(&save_ccline);
2450
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002451 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002452 /* Restore msg_col, the prompt from input() may have changed it.
2453 * But only if called recursively and the commandline is therefore being
2454 * restored to an old one; if not, the input() prompt stays on the screen,
2455 * so we need its modified msg_col left intact. */
2456 if (ccline.cmdbuff != NULL)
2457 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 return s;
2460}
2461#endif
2462
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002463/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002464 * Return TRUE when the text must not be changed and we can't switch to
2465 * another window or buffer. Used when editing the command line, evaluating
2466 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002467 */
2468 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002469text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002470{
2471#ifdef FEAT_CMDWIN
2472 if (cmdwin_type != 0)
2473 return TRUE;
2474#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002475 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002476}
2477
2478/*
2479 * Give an error message for a command that isn't allowed while the cmdline
2480 * window is open or editing the cmdline in another way.
2481 */
2482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002483text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002484{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002485 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002486}
2487
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002488 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002489get_text_locked_msg(void)
2490{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002491#ifdef FEAT_CMDWIN
2492 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002493 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002494#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002495 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002496}
2497
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002498/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002499 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2500 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002501 */
2502 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002503curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002504{
2505 if (curbuf_lock > 0)
2506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002507 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002508 return TRUE;
2509 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002510 return allbuf_locked();
2511}
2512
2513/*
2514 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2515 * message.
2516 */
2517 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002518allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002519{
2520 if (allbuf_lock > 0)
2521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002523 return TRUE;
2524 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002525 return FALSE;
2526}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002529cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2532 if (cmdline_star > 0) /* showing '*', always 1 position */
2533 return 1;
2534#endif
2535 return ptr2cells(ccline.cmdbuff + idx);
2536}
2537
2538/*
2539 * Compute the offset of the cursor on the command line for the prompt and
2540 * indent.
2541 */
2542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002543set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002545 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 ccline.cmdspos = 1 + ccline.cmdindent;
2547 else
2548 ccline.cmdspos = 0 + ccline.cmdindent;
2549}
2550
2551/*
2552 * Compute the screen position for the cursor on the command line.
2553 */
2554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002555set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
2557 int i, m, c;
2558
2559 set_cmdspos();
2560 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002563 if (m < 0) /* overflow, Columns or Rows at weird value */
2564 m = MAXCOL;
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 else
2567 m = MAXCOL;
2568 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2569 {
2570 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2572 if (has_mbyte)
2573 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002574 /* If the cmdline doesn't fit, show cursor on last visible char.
2575 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if ((ccline.cmdspos += c) >= m)
2577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 ccline.cmdspos -= c;
2579 break;
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002582 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584}
2585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586/*
2587 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2588 * character that doesn't fit, so that a ">" must be displayed.
2589 */
2590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002591correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002593 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2595 && ccline.cmdspos % Columns + cells > Columns)
2596 ccline.cmdspos++;
2597}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599/*
2600 * Get an Ex command line for the ":" command.
2601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603getexline(
2604 int c, /* normally ':', NUL for ":append" */
2605 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002606 int indent, /* indent for inside conditionals */
2607 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 /* When executing a register, remove ':' that's in front of each line. */
2610 if (exec_from_reg && vpeekc() == ':')
2611 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002612 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613}
2614
2615/*
2616 * Get an Ex command line for Ex mode.
2617 * In Ex mode we only use the OS supplied line editing features and no
2618 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002619 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002622getexmodeline(
2623 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002624 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002625 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002626 int indent, /* indent for inside conditionals */
2627 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 garray_T line_ga;
2630 char_u *pend;
2631 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002632 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002633 int escaped = FALSE; /* CTRL-V typed */
2634 int vcol = 0;
2635 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002636 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002637 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 /* Switch cursor on now. This avoids that it happens after the "\n", which
2640 * confuses the system function that computes tabstops. */
2641 cursor_on();
2642
2643 /* always start in column 0; write a newline if necessary */
2644 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002649 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650 if (p_prompt)
2651 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 while (indent-- > 0)
2653 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656
2657 ga_init2(&line_ga, 1, 30);
2658
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002659 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002660 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002661 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002662 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002663 while (indent >= 8)
2664 {
2665 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002666 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002667 indent -= 8;
2668 }
2669 while (indent-- > 0)
2670 {
2671 ga_append(&line_ga, ' ');
2672 msg_putchar(' ');
2673 }
2674 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002675 ++no_mapping;
2676 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 /*
2679 * Get the line, one character at a time.
2680 */
2681 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002684 long sw;
2685 char_u *s;
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (ga_grow(&line_ga, 40) == FAIL)
2688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaarba748c82017-02-26 14:00:07 +01002690 /*
2691 * Get one character at a time.
2692 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002693 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002694
2695 /* Check for a ":normal" command and no more characters left. */
2696 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2697 c1 = '\n';
2698 else
2699 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 * Handle line editing.
2703 * Previously this was left to the system, putting the terminal in
2704 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 msg_putchar('\n');
2709 break;
2710 }
2711
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002712 if (c1 == K_PS)
2713 {
2714 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2715 goto redraw;
2716 }
2717
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 if (!escaped)
2719 {
2720 /* CR typed means "enter", which is NL */
2721 if (c1 == '\r')
2722 c1 = '\n';
2723
2724 if (c1 == BS || c1 == K_BS
2725 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 if (line_ga.ga_len > 0)
2728 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002729 if (has_mbyte)
2730 {
2731 p = (char_u *)line_ga.ga_data;
2732 p[line_ga.ga_len] = NUL;
2733 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2734 line_ga.ga_len -= len;
2735 }
2736 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002737 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 goto redraw;
2739 }
2740 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002743 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 msg_col = startcol;
2746 msg_clr_eos();
2747 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002748 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 }
2750
2751 if (c1 == Ctrl_T)
2752 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002753 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 p = (char_u *)line_ga.ga_data;
2755 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002756 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002757 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002759 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002761 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002762 p = (char_u *)line_ga.ga_data;
2763 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2765 *s = ' ';
2766 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768redraw:
2769 /* redraw the line */
2770 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002772 p = (char_u *)line_ga.ga_data;
2773 p[line_ga.ga_len] = NUL;
2774 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002780 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002781 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 len = MB_PTR2LEN(p);
2786 msg_outtrans_len(p, len);
2787 vcol += ptr2cells(p);
2788 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002792 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 continue;
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002796 if (c1 == Ctrl_D)
2797 {
2798 /* Delete one shiftwidth. */
2799 p = (char_u *)line_ga.ga_data;
2800 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (prev_char == '^')
2803 ex_keep_indent = TRUE;
2804 indent = 0;
2805 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 else
2808 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002810 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002811 if (indent > 0)
2812 {
2813 --indent;
2814 indent -= indent % get_sw_value(curbuf);
2815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002817 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002819 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2821 --line_ga.ga_len;
2822 }
2823 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825
2826 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2827 {
2828 escaped = TRUE;
2829 continue;
2830 }
2831
2832 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2833 if (IS_SPECIAL(c1))
2834 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836
2837 if (IS_SPECIAL(c1))
2838 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002839 if (has_mbyte)
2840 len = (*mb_char2bytes)(c1,
2841 (char_u *)line_ga.ga_data + line_ga.ga_len);
2842 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002843 {
2844 len = 1;
2845 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2846 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 if (c1 == '\n')
2848 msg_putchar('\n');
2849 else if (c1 == TAB)
2850 {
2851 /* Don't use chartabsize(), 'ts' can be different */
2852 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002854 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002859 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002862 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 escaped = FALSE;
2864
2865 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002866 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002868 /* We are done when a NL is entered, but not when it comes after an
2869 * odd number of backslashes, that results in a NUL. */
2870 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002872 int bcount = 0;
2873
2874 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2875 ++bcount;
2876
2877 if (bcount > 0)
2878 {
2879 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2880 * "\NL", etc. */
2881 line_ga.ga_len -= (bcount + 1) / 2;
2882 pend -= (bcount + 1) / 2;
2883 pend[-1] = '\n';
2884 }
2885
2886 if ((bcount & 1) == 0)
2887 {
2888 --line_ga.ga_len;
2889 --pend;
2890 *pend = NUL;
2891 break;
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 }
2895
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896 --no_mapping;
2897 --allow_keys;
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* make following messages go to the next line */
2900 msg_didout = FALSE;
2901 msg_col = 0;
2902 if (msg_row < Rows - 1)
2903 ++msg_row;
2904 emsg_on_display = FALSE; /* don't want ui_delay() */
2905
2906 if (got_int)
2907 ga_clear(&line_ga);
2908
2909 return (char_u *)line_ga.ga_data;
2910}
2911
Bram Moolenaare344bea2005-09-01 20:46:49 +00002912# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2913 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914/*
2915 * Return TRUE if ccline.overstrike is on.
2916 */
2917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002918cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 return ccline.overstrike;
2921}
2922
2923/*
2924 * Return TRUE if the cursor is at the end of the cmdline.
2925 */
2926 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002927cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
2929 return (ccline.cmdpos >= ccline.cmdlen);
2930}
2931#endif
2932
Bram Moolenaar9372a112005-12-06 19:59:18 +00002933#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934/*
2935 * Return the virtual column number at the current cursor position.
2936 * This is used by the IM code to obtain the start of the preedit string.
2937 */
2938 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002939cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
2941 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2942 return MAXCOL;
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (has_mbyte)
2945 {
2946 colnr_T col;
2947 int i = 0;
2948
2949 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002950 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 return col;
2953 }
2954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 return ccline.cmdpos;
2956}
2957#endif
2958
2959#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2960/*
2961 * If part of the command line is an IM preedit string, redraw it with
2962 * IM feedback attributes. The cursor position is restored after drawing.
2963 */
2964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002965redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 if ((State & CMDLINE)
2968 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002969 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 && !p_imdisable
2971 && im_is_preediting())
2972 {
2973 int cmdpos = 0;
2974 int cmdspos;
2975 int old_row;
2976 int old_col;
2977 colnr_T col;
2978
2979 old_row = msg_row;
2980 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002981 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (has_mbyte)
2984 {
2985 for (col = 0; col < preedit_start_col
2986 && cmdpos < ccline.cmdlen; ++col)
2987 {
2988 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002989 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991 }
2992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
2994 cmdspos += preedit_start_col;
2995 cmdpos += preedit_start_col;
2996 }
2997
2998 msg_row = cmdline_row + (cmdspos / (int)Columns);
2999 msg_col = cmdspos % (int)Columns;
3000 if (msg_row >= Rows)
3001 msg_row = Rows - 1;
3002
3003 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3004 {
3005 int char_len;
3006 int char_attr;
3007
3008 char_attr = im_get_feedback_attr(col);
3009 if (char_attr < 0)
3010 break; /* end of preedit string */
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003013 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 char_len = 1;
3016
3017 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3018 cmdpos += char_len;
3019 }
3020
3021 msg_row = old_row;
3022 msg_col = old_col;
3023 }
3024}
3025#endif /* FEAT_XIM && FEAT_GUI_GTK */
3026
3027/*
3028 * Allocate a new command line buffer.
3029 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 */
3031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003032alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 /*
3035 * give some extra space to avoid having to allocate all the time
3036 */
3037 if (len < 80)
3038 len = 100;
3039 else
3040 len += 20;
3041
3042 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3043 ccline.cmdbufflen = len;
3044}
3045
3046/*
3047 * Re-allocate the command line to length len + something extra.
3048 * return FAIL for failure, OK otherwise
3049 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003051realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 char_u *p;
3054
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003055 if (len < ccline.cmdbufflen)
3056 return OK; /* no need to resize */
3057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 p = ccline.cmdbuff;
3059 alloc_cmdbuff(len); /* will get some more */
3060 if (ccline.cmdbuff == NULL) /* out of memory */
3061 {
3062 ccline.cmdbuff = p; /* keep the old one */
3063 return FAIL;
3064 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003065 /* There isn't always a NUL after the command, but it may need to be
3066 * there, thus copy up to the NUL and add a NUL. */
3067 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3068 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003070
3071 if (ccline.xpc != NULL
3072 && ccline.xpc->xp_pattern != NULL
3073 && ccline.xpc->xp_context != EXPAND_NOTHING
3074 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3075 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003076 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003077
3078 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3079 * to point into the newly allocated memory. */
3080 if (i >= 0 && i <= ccline.cmdlen)
3081 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3082 }
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 return OK;
3085}
3086
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003087#if defined(FEAT_ARABIC) || defined(PROTO)
3088static char_u *arshape_buf = NULL;
3089
3090# if defined(EXITFREE) || defined(PROTO)
3091 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003092free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003093{
3094 vim_free(arshape_buf);
3095}
3096# endif
3097#endif
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099/*
3100 * Draw part of the cmdline at the current cursor position. But draw stars
3101 * when cmdline_star is TRUE.
3102 */
3103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003104draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3107 int i;
3108
3109 if (cmdline_star > 0)
3110 for (i = 0; i < len; ++i)
3111 {
3112 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003114 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
3116 else
3117#endif
3118#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003119 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 static int buflen = 0;
3122 char_u *p;
3123 int j;
3124 int newlen = 0;
3125 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003126 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 int prev_c = 0;
3128 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003129 int u8c;
3130 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133 /*
3134 * Do arabic shaping into a temporary buffer. This is very
3135 * inefficient!
3136 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003137 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 /* Re-allocate the buffer. We keep it around to avoid a lot of
3140 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003141 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003142 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143 arshape_buf = alloc(buflen);
3144 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return; /* out of memory */
3146 }
3147
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003148 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3149 {
3150 /* Prepend a space to draw the leading composing char on. */
3151 arshape_buf[0] = ' ';
3152 newlen = 1;
3153 }
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 for (j = start; j < start + len; j += mb_l)
3156 {
3157 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003158 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003159 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (ARABIC_CHAR(u8c))
3161 {
3162 /* Do Arabic shaping. */
3163 if (cmdmsg_rl)
3164 {
3165 /* displaying from right to left */
3166 pc = prev_c;
3167 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003168 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (j + mb_l >= start + len)
3170 nc = NUL;
3171 else
3172 nc = utf_ptr2char(p + mb_l);
3173 }
3174 else
3175 {
3176 /* displaying from left to right */
3177 if (j + mb_l >= start + len)
3178 pc = NUL;
3179 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 {
3181 int pcc[MAX_MCO];
3182
3183 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003185 pc1 = pcc[0];
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 nc = prev_c;
3188 }
3189 prev_c = u8c;
3190
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003191 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003193 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003196 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3197 if (u8cc[1] != 0)
3198 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 }
3202 else
3203 {
3204 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 newlen += mb_l;
3207 }
3208 }
3209
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003210 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212 else
3213#endif
3214 msg_outtrans_len(ccline.cmdbuff + start, len);
3215}
3216
3217/*
3218 * Put a character on the command line. Shifts the following text to the
3219 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3220 * "c" must be printable (fit in one display cell)!
3221 */
3222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003223putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 if (cmd_silent)
3226 return;
3227 msg_no_more = TRUE;
3228 msg_putchar(c);
3229 if (shift)
3230 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3231 msg_no_more = FALSE;
3232 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003233 extra_char = c;
3234 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235}
3236
3237/*
3238 * Undo a putcmdline(c, FALSE).
3239 */
3240 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003241unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
3243 if (cmd_silent)
3244 return;
3245 msg_no_more = TRUE;
3246 if (ccline.cmdlen == ccline.cmdpos)
3247 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003248 else if (has_mbyte)
3249 draw_cmdline(ccline.cmdpos,
3250 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else
3252 draw_cmdline(ccline.cmdpos, 1);
3253 msg_no_more = FALSE;
3254 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003255 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256}
3257
3258/*
3259 * Put the given string, of the given length, onto the command line.
3260 * If len is -1, then STRLEN() is used to calculate the length.
3261 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3262 * part will be redrawn, otherwise it will not. If this function is called
3263 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3264 * called afterwards.
3265 */
3266 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003267put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 int retval;
3270 int i;
3271 int m;
3272 int c;
3273
3274 if (len < 0)
3275 len = (int)STRLEN(str);
3276
3277 /* Check if ccline.cmdbuff needs to be longer */
3278 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003279 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else
3281 retval = OK;
3282 if (retval == OK)
3283 {
3284 if (!ccline.overstrike)
3285 {
3286 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3287 ccline.cmdbuff + ccline.cmdpos,
3288 (size_t)(ccline.cmdlen - ccline.cmdpos));
3289 ccline.cmdlen += len;
3290 }
3291 else
3292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 if (has_mbyte)
3294 {
3295 /* Count nr of characters in the new string. */
3296 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003297 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 ++m;
3299 /* Count nr of bytes in cmdline that are overwritten by these
3300 * characters. */
3301 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003302 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 --m;
3304 if (i < ccline.cmdlen)
3305 {
3306 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3307 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3308 ccline.cmdlen += ccline.cmdpos + len - i;
3309 }
3310 else
3311 ccline.cmdlen = ccline.cmdpos + len;
3312 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003313 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 ccline.cmdlen = ccline.cmdpos + len;
3315 }
3316 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3317 ccline.cmdbuff[ccline.cmdlen] = NUL;
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (enc_utf8)
3320 {
3321 /* When the inserted text starts with a composing character,
3322 * backup to the character before it. There could be two of them.
3323 */
3324 i = 0;
3325 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3326 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3327 {
3328 i = (*mb_head_off)(ccline.cmdbuff,
3329 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3330 ccline.cmdpos -= i;
3331 len += i;
3332 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3333 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003334#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3336 {
3337 /* Check the previous character for Arabic combining pair. */
3338 i = (*mb_head_off)(ccline.cmdbuff,
3339 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3340 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3341 + ccline.cmdpos - i), c))
3342 {
3343 ccline.cmdpos -= i;
3344 len += i;
3345 }
3346 else
3347 i = 0;
3348 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (i != 0)
3351 {
3352 /* Also backup the cursor position. */
3353 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3354 ccline.cmdspos -= i;
3355 msg_col -= i;
3356 if (msg_col < 0)
3357 {
3358 msg_col += Columns;
3359 --msg_row;
3360 }
3361 }
3362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 if (redraw && !cmd_silent)
3365 {
3366 msg_no_more = TRUE;
3367 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003368 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3370 /* Avoid clearing the rest of the line too often. */
3371 if (cmdline_row != i || ccline.overstrike)
3372 msg_clr_eos();
3373 msg_no_more = FALSE;
3374 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003375 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003377 m = Columns * Rows;
3378 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003380 }
3381 else
3382 m = MAXCOL;
3383 for (i = 0; i < len; ++i)
3384 {
3385 c = cmdline_charsize(ccline.cmdpos);
3386 /* count ">" for a double-wide char that doesn't fit. */
3387 if (has_mbyte)
3388 correct_cmdspos(ccline.cmdpos, c);
3389 /* Stop cursor at the end of the screen, but do increment the
3390 * insert position, so that entering a very long command
3391 * works, even though you can't see it. */
3392 if (ccline.cmdspos + c < m)
3393 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003394
Bram Moolenaar14184a32019-02-16 15:10:30 +01003395 if (has_mbyte)
3396 {
3397 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3398 if (c > len - i - 1)
3399 c = len - i - 1;
3400 ccline.cmdpos += c;
3401 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003403 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405 }
3406 if (redraw)
3407 msg_check();
3408 return retval;
3409}
3410
Bram Moolenaar66b51422019-08-18 21:44:12 +02003411static cmdline_info_T prev_ccline;
3412static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003413
3414/*
3415 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3416 * and overwrite it. But get_cmdline_str() may need it, thus make it
3417 * available globally in prev_ccline.
3418 */
3419 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003420save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003421{
3422 if (!prev_ccline_used)
3423 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003424 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003425 prev_ccline_used = TRUE;
3426 }
3427 *ccp = prev_ccline;
3428 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003429 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003430}
3431
3432/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003433 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003434 */
3435 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003436restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003437{
3438 ccline = prev_ccline;
3439 prev_ccline = *ccp;
3440}
3441
Bram Moolenaar8299df92004-07-10 09:47:34 +00003442/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003443 * Paste a yank register into the command line.
3444 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003445 * insert_reg() can't be used here, because special characters from the
3446 * register contents will be interpreted as commands.
3447 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003448 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003449 */
3450 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003451cmdline_paste(
3452 int regname,
3453 int literally, /* Insert text literally instead of "as typed" */
3454 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003455{
3456 long i;
3457 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003458 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003459 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003460
3461 /* check for valid regname; also accept special characters for CTRL-R in
3462 * the command line */
3463 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003464 && regname != Ctrl_A && regname != Ctrl_L
3465 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003466 return FAIL;
3467
3468 /* A register containing CTRL-R can cause an endless loop. Allow using
3469 * CTRL-C to break the loop. */
3470 line_breakcheck();
3471 if (got_int)
3472 return FAIL;
3473
3474#ifdef FEAT_CLIPBOARD
3475 regname = may_get_selection(regname);
3476#endif
3477
Bram Moolenaar438d1762018-09-30 17:11:48 +02003478 // Need to set "textlock" to avoid nasty things like going to another
3479 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003480 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003481 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003482 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003483
3484 if (i)
3485 {
3486 /* Got the value of a special register in "arg". */
3487 if (arg == NULL)
3488 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003489
3490 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3491 * part of the word. */
3492 p = arg;
3493 if (p_is && regname == Ctrl_W)
3494 {
3495 char_u *w;
3496 int len;
3497
3498 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003499 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003500 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003501 if (has_mbyte)
3502 {
3503 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3504 if (!vim_iswordc(mb_ptr2char(w - len)))
3505 break;
3506 w -= len;
3507 }
3508 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003509 {
3510 if (!vim_iswordc(w[-1]))
3511 break;
3512 --w;
3513 }
3514 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003515 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003516 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3517 p += len;
3518 }
3519
3520 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 if (allocated)
3522 vim_free(arg);
3523 return OK;
3524 }
3525
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003526 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003527}
3528
3529/*
3530 * Put a string on the command line.
3531 * When "literally" is TRUE, insert literally.
3532 * When "literally" is FALSE, insert as typed, but don't leave the command
3533 * line.
3534 */
3535 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003536cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537{
3538 int c, cv;
3539
3540 if (literally)
3541 put_on_cmdline(s, -1, TRUE);
3542 else
3543 while (*s != NUL)
3544 {
3545 cv = *s;
3546 if (cv == Ctrl_V && s[1])
3547 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003548 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003549 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003551 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003552 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3553 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003554#ifdef UNIX
3555 || c == intr_char
3556#endif
3557 || (c == Ctrl_BSL && *s == Ctrl_N))
3558 stuffcharReadbuff(Ctrl_V);
3559 stuffcharReadbuff(c);
3560 }
3561}
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#ifdef FEAT_WILDMENU
3564/*
3565 * Delete characters on the command line, from "from" to the current
3566 * position.
3567 */
3568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003569cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3572 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3573 ccline.cmdlen -= ccline.cmdpos - from;
3574 ccline.cmdpos = from;
3575}
3576#endif
3577
3578/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003579 * This function is called when the screen size changes and with incremental
3580 * search and in other situations where the command line may have been
3581 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
3583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003584redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003586 redrawcmdline_ex(TRUE);
3587}
3588
3589 void
3590redrawcmdline_ex(int do_compute_cmdrow)
3591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (cmd_silent)
3593 return;
3594 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003595 if (do_compute_cmdrow)
3596 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 redrawcmd();
3598 cursorcmd();
3599}
3600
3601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 int i;
3605
3606 if (cmd_silent)
3607 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003608 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 msg_putchar(ccline.cmdfirstc);
3610 if (ccline.cmdprompt != NULL)
3611 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003612 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3614 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003615 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 --ccline.cmdindent;
3617 }
3618 else
3619 for (i = ccline.cmdindent; i > 0; --i)
3620 msg_putchar(' ');
3621}
3622
3623/*
3624 * Redraw what is currently on the command line.
3625 */
3626 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003627redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 if (cmd_silent)
3630 return;
3631
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003632 /* when 'incsearch' is set there may be no command line while redrawing */
3633 if (ccline.cmdbuff == NULL)
3634 {
3635 windgoto(cmdline_row, 0);
3636 msg_clr_eos();
3637 return;
3638 }
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 msg_start();
3641 redrawcmdprompt();
3642
3643 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3644 msg_no_more = TRUE;
3645 draw_cmdline(0, ccline.cmdlen);
3646 msg_clr_eos();
3647 msg_no_more = FALSE;
3648
3649 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003650 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003651 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653 /*
3654 * An emsg() before may have set msg_scroll. This is used in normal mode,
3655 * in cmdline mode we can reset them now.
3656 */
3657 msg_scroll = FALSE; /* next message overwrites cmdline */
3658
3659 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3660 * in cmdline mode */
3661 skip_redraw = FALSE;
3662}
3663
3664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003665compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003667 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 cmdline_row = Rows - 1;
3669 else
3670 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003671 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672}
3673
Bram Moolenaar66b51422019-08-18 21:44:12 +02003674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003675cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 if (cmd_silent)
3678 return;
3679
3680#ifdef FEAT_RIGHTLEFT
3681 if (cmdmsg_rl)
3682 {
3683 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3684 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3685 if (msg_row <= 0)
3686 msg_row = Rows - 1;
3687 }
3688 else
3689#endif
3690 {
3691 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3692 msg_col = ccline.cmdspos % (int)Columns;
3693 if (msg_row >= Rows)
3694 msg_row = Rows - 1;
3695 }
3696
3697 windgoto(msg_row, msg_col);
3698#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003699 if (p_imst == IM_ON_THE_SPOT)
3700 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701#endif
3702#ifdef MCH_CURSOR_SHAPE
3703 mch_update_cursor();
3704#endif
3705}
3706
3707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003708gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 msg_start();
3711#ifdef FEAT_RIGHTLEFT
3712 if (cmdmsg_rl)
3713 msg_col = Columns - 1;
3714 else
3715#endif
3716 msg_col = 0; /* always start in column 0 */
3717 if (clr) /* clear the bottom line(s) */
3718 msg_clr_eos(); /* will reset clear_cmdline */
3719 windgoto(cmdline_row, 0);
3720}
3721
3722/*
3723 * Check the word in front of the cursor for an abbreviation.
3724 * Called when the non-id character "c" has been entered.
3725 * When an abbreviation is recognized it is removed from the text with
3726 * backspaces and the replacement string is inserted, followed by "c".
3727 */
3728 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003729ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003731 int spos = 0;
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3734 return FALSE;
3735
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003736 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3737 * Actually accepts any mark. */
3738 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3739 spos++;
3740 if (ccline.cmdlen - spos > 5
3741 && ccline.cmdbuff[spos] == '\''
3742 && ccline.cmdbuff[spos + 2] == ','
3743 && ccline.cmdbuff[spos + 3] == '\'')
3744 spos += 5;
3745 else
3746 /* check abbreviation from the beginning of the commandline */
3747 spos = 0;
3748
3749 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750}
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003753 * Escape special characters in "fname" for when used as a file name argument
3754 * after a Vim command, or, when "shell" is non-zero, a shell command.
3755 * Returns the result in allocated memory.
3756 */
3757 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003758vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003759{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003760 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003761#ifdef BACKSLASH_IN_FILENAME
3762 char_u buf[20];
3763 int j = 0;
3764
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003765 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003766 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003767 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003768 buf[j++] = *p;
3769 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003770 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003771#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003772 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3773 if (shell && csh_like_shell() && p != NULL)
3774 {
3775 char_u *s;
3776
3777 /* For csh and similar shells need to put two backslashes before '!'.
3778 * One is taken by Vim, one by the shell. */
3779 s = vim_strsave_escaped(p, (char_u *)"!");
3780 vim_free(p);
3781 p = s;
3782 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003783#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003784
3785 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3786 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003787 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003788 escape_fname(&p);
3789
3790 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003791}
3792
3793/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003794 * Put a backslash before the file name in "pp", which is in allocated memory.
3795 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003797escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003798{
3799 char_u *p;
3800
Bram Moolenaar964b3742019-05-24 18:54:09 +02003801 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003802 if (p != NULL)
3803 {
3804 p[0] = '\\';
3805 STRCPY(p + 1, *pp);
3806 vim_free(*pp);
3807 *pp = p;
3808 }
3809}
3810
3811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 * For each file name in files[num_files]:
3813 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3814 */
3815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003816tilde_replace(
3817 char_u *orig_pat,
3818 int num_files,
3819 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 int i;
3822 char_u *p;
3823
3824 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3825 {
3826 for (i = 0; i < num_files; ++i)
3827 {
3828 p = home_replace_save(NULL, files[i]);
3829 if (p != NULL)
3830 {
3831 vim_free(files[i]);
3832 files[i] = p;
3833 }
3834 }
3835 }
3836}
3837
3838/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003839 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003841 cmdline_info_T *
3842get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003844 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845}
3846
Bram Moolenaar064154c2016-03-19 22:50:43 +01003847#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003848/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003849 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003850 * ccline and put the previous value in prev_ccline.
3851 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003852 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003853get_ccline_ptr(void)
3854{
3855 if ((State & CMDLINE) == 0)
3856 return NULL;
3857 if (ccline.cmdbuff != NULL)
3858 return &ccline;
3859 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3860 return &prev_ccline;
3861 return NULL;
3862}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003863#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003864
Bram Moolenaar064154c2016-03-19 22:50:43 +01003865#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003866/*
3867 * Get the current command line in allocated memory.
3868 * Only works when the command line is being edited.
3869 * Returns NULL when something is wrong.
3870 */
3871 char_u *
3872get_cmdline_str(void)
3873{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003874 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003875
Bram Moolenaaree91c332018-09-25 22:27:35 +02003876 if (cmdline_star > 0)
3877 return NULL;
3878 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003879 if (p == NULL)
3880 return NULL;
3881 return vim_strnsave(p->cmdbuff, p->cmdlen);
3882}
3883
3884/*
3885 * Get the current command line position, counted in bytes.
3886 * Zero is the first position.
3887 * Only works when the command line is being edited.
3888 * Returns -1 when something is wrong.
3889 */
3890 int
3891get_cmdline_pos(void)
3892{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003893 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003894
3895 if (p == NULL)
3896 return -1;
3897 return p->cmdpos;
3898}
3899
3900/*
3901 * Set the command line byte position to "pos". Zero is the first position.
3902 * Only works when the command line is being edited.
3903 * Returns 1 when failed, 0 when OK.
3904 */
3905 int
3906set_cmdline_pos(
3907 int pos)
3908{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003909 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003910
3911 if (p == NULL)
3912 return 1;
3913
3914 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3915 * changed the command line. */
3916 if (pos < 0)
3917 new_cmdpos = 0;
3918 else
3919 new_cmdpos = pos;
3920 return 0;
3921}
3922#endif
3923
3924#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
3925/*
3926 * Get the current command-line type.
3927 * Returns ':' or '/' or '?' or '@' or '>' or '-'
3928 * Only works when the command line is being edited.
3929 * Returns NUL when something is wrong.
3930 */
3931 int
3932get_cmdline_type(void)
3933{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003934 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003935
3936 if (p == NULL)
3937 return NUL;
3938 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01003939 return
3940# ifdef FEAT_EVAL
3941 (p->input_fn) ? '@' :
3942# endif
3943 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003944 return p->cmdfirstc;
3945}
3946#endif
3947
Bram Moolenaard7663c22019-08-06 21:59:57 +02003948/*
3949 * Return the first character of the current command line.
3950 */
3951 int
3952get_cmdline_firstc(void)
3953{
3954 return ccline.cmdfirstc;
3955}
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957/*
3958 * Get indices "num1,num2" that specify a range within a list (not a range of
3959 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
3960 * Returns OK if parsed successfully, otherwise FAIL.
3961 */
3962 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003963get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 int len;
3966 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003967 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969 *str = skipwhite(*str);
3970 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
3971 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003972 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *str += len;
3974 *num1 = (int)num;
3975 first = TRUE;
3976 }
3977 *str = skipwhite(*str);
3978 if (**str == ',') /* parse "to" part of range */
3979 {
3980 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003981 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (len > 0)
3983 {
3984 *num2 = (int)num;
3985 *str = skipwhite(*str + len);
3986 }
3987 else if (!first) /* no number given at all */
3988 return FAIL;
3989 }
3990 else if (first) /* only one number given */
3991 *num2 = *num1;
3992 return OK;
3993}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995#if defined(FEAT_CMDWIN) || defined(PROTO)
3996/*
3997 * Open a window on the current command line and history. Allow editing in
3998 * the window. Returns when the window is closed.
3999 * Returns:
4000 * CR if the command is to be executed
4001 * Ctrl_C if it is to be abandoned
4002 * K_IGNORE if editing continues
4003 */
4004 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004005open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004007 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004009 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 win_T *wp;
4011 int i;
4012 linenr_T lnum;
4013 int histtype;
4014 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 int save_restart_edit = restart_edit;
4016 int save_State = State;
4017 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004018#ifdef FEAT_RIGHTLEFT
4019 int save_cmdmsg_rl = cmdmsg_rl;
4020#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004021#ifdef FEAT_FOLDING
4022 int save_KeyTyped;
4023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /* Can't do this recursively. Can't do it when typing a password. */
4026 if (cmdwin_type != 0
4027# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4028 || cmdline_star > 0
4029# endif
4030 )
4031 {
4032 beep_flush();
4033 return K_IGNORE;
4034 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004035 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037 /* Save current window sizes. */
4038 win_size_save(&winsizes);
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004041 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004042
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004043 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4044 // command line window, but we don't want the popup menu then.
4045 pum_undisplay();
4046
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004047 /* don't use a new tab page */
4048 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004049 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* Create a window for the command-line buffer. */
4052 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4053 {
4054 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004055 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 return K_IGNORE;
4057 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004058 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004061 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004062 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004065#ifdef FEAT_FOLDING
4066 curwin->w_p_fen = FALSE;
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004069 curwin->w_p_rl = cmdmsg_rl;
4070 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004072 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004075 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02004076 /* But don't allow switching to another buffer. */
4077 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
Bram Moolenaar46152342005-09-07 21:18:43 +00004079 /* Showing the prompt may have set need_wait_return, reset it. */
4080 need_wait_return = FALSE;
4081
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004082 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4084 {
4085 if (p_wc == TAB)
4086 {
4087 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4088 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4089 }
4090 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4091 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004092 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004094 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4095 * sets 'textwidth' to 78). */
4096 curbuf->b_p_tw = 0;
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 /* Fill the buffer with the history. */
4099 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004100 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004102 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (i >= 0)
4104 {
4105 lnum = 0;
4106 do
4107 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004108 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004110 if (get_histentry(histtype)[i].hisstr != NULL)
4111 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 (colnr_T)0, FALSE);
4113 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004114 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 }
4117
4118 /* Replace the empty last line with the current command-line and put the
4119 * cursor there. */
4120 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4121 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4122 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004123 changed_line_abv_curs();
4124 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004125 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 /* No Ex mode here! */
4128 exmode_active = 0;
4129
4130 State = NORMAL;
4131# ifdef FEAT_MOUSE
4132 setmouse();
4133# endif
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004136 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004137 if (restart_edit != 0) /* autocmd with ":startinsert" */
4138 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 i = RedrawingDisabled;
4141 RedrawingDisabled = 0;
4142
4143 /*
4144 * Call the main loop until <CR> or CTRL-C is typed.
4145 */
4146 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004147 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 RedrawingDisabled = i;
4150
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004151# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004152 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004153# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004156 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004157
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004158# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004159 /* Restore KeyTyped in case it is modified by autocommands */
4160 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161# endif
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 exmode_active = save_exmode;
4165
Bram Moolenaarf9821062008-06-20 16:31:07 +00004166 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004168 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004171 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 else
4174 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004175# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /* autocmds may abort script processing */
4177 if (aborting() && cmdwin_result != K_IGNORE)
4178 cmdwin_result = Ctrl_C;
4179# endif
4180 /* Set the new command line from the cmdline buffer. */
4181 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004182 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004184 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4185
4186 if (histtype == HIST_CMD)
4187 {
4188 /* Execute the command directly. */
4189 ccline.cmdbuff = vim_strsave((char_u *)p);
4190 cmdwin_result = CAR;
4191 }
4192 else
4193 {
4194 /* First need to cancel what we were doing. */
4195 ccline.cmdbuff = NULL;
4196 stuffcharReadbuff(':');
4197 stuffReadbuff((char_u *)p);
4198 stuffcharReadbuff(CAR);
4199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 else if (cmdwin_result == K_XF2) /* :qa typed */
4202 {
4203 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4204 cmdwin_result = CAR;
4205 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004206 else if (cmdwin_result == Ctrl_C)
4207 {
4208 /* :q or :close, don't execute any command
4209 * and don't modify the cmd window. */
4210 ccline.cmdbuff = NULL;
4211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 else
4213 ccline.cmdbuff = vim_strsave(ml_get_curline());
4214 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004215 {
4216 ccline.cmdbuff = vim_strsave((char_u *)"");
4217 ccline.cmdlen = 0;
4218 ccline.cmdbufflen = 1;
4219 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 else
4223 {
4224 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4225 ccline.cmdbufflen = ccline.cmdlen + 1;
4226 ccline.cmdpos = curwin->w_cursor.col;
4227 if (ccline.cmdpos > ccline.cmdlen)
4228 ccline.cmdpos = ccline.cmdlen;
4229 if (cmdwin_result == K_IGNORE)
4230 {
4231 set_cmdspos_cursor();
4232 redrawcmd();
4233 }
4234 }
4235
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004237 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004238# ifdef FEAT_CONCEAL
4239 /* Avoid command-line window first character being concealed. */
4240 curwin->w_p_cole = 0;
4241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004243 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 win_goto(old_curwin);
4245 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004246
4247 /* win_close() may have already wiped the buffer when 'bh' is
4248 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004249 if (bufref_valid(&bufref))
4250 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /* Restore window sizes. */
4253 win_size_restore(&winsizes);
4254
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004255 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257
4258 ga_clear(&winsizes);
4259 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004260# ifdef FEAT_RIGHTLEFT
4261 cmdmsg_rl = save_cmdmsg_rl;
4262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 State = save_State;
4265# ifdef FEAT_MOUSE
4266 setmouse();
4267# endif
4268
4269 return cmdwin_result;
4270}
4271#endif /* FEAT_CMDWIN */
4272
4273/*
4274 * Used for commands that either take a simple command string argument, or:
4275 * cmd << endmarker
4276 * {script}
4277 * endmarker
4278 * Returns a pointer to allocated memory with {script} or NULL.
4279 */
4280 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004281script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
4283 char_u *theline;
4284 char *end_pattern = NULL;
4285 char dot[] = ".";
4286 garray_T ga;
4287
4288 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4289 return NULL;
4290
4291 ga_init2(&ga, 1, 0x400);
4292
4293 if (cmd[2] != NUL)
4294 end_pattern = (char *)skipwhite(cmd + 2);
4295 else
4296 end_pattern = dot;
4297
4298 for (;;)
4299 {
4300 theline = eap->getline(
4301#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004302 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004304 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004307 {
4308 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 ga_concat(&ga, theline);
4313 ga_append(&ga, '\n');
4314 vim_free(theline);
4315 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004316 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 return (char_u *)ga.ga_data;
4319}