blob: 24cce5b738676db174a82f404ab191a8086780fc [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;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200897# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000898 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000899# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000900 }
901#endif
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 /*
904 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
905 * doing ":@0" when register 0 doesn't contain a CR.
906 */
907 msg_scroll = FALSE;
908
909 State = CMDLINE;
910
911 if (firstc == '/' || firstc == '?' || firstc == '@')
912 {
913 /* Use ":lmap" mappings for search pattern and input(). */
914 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
915 b_im_ptr = &curbuf->b_p_iminsert;
916 else
917 b_im_ptr = &curbuf->b_p_imsearch;
918 if (*b_im_ptr == B_IMODE_LMAP)
919 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100920#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 im_set_active(*b_im_ptr == B_IMODE_IM);
922#endif
923 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100924#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else if (p_imcmdline)
926 im_set_active(TRUE);
927#endif
928
929#ifdef FEAT_MOUSE
930 setmouse();
931#endif
932#ifdef CURSOR_SHAPE
933 ui_cursor_shape(); /* may show different cursor shape */
934#endif
935
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000936 /* When inside an autocommand for writing "exiting" may be set and
937 * terminal mode set to cooked. Need to set raw mode here then. */
938 settmode(TMODE_RAW);
939
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200940 /* Trigger CmdlineEnter autocommands. */
941 cmdline_type = firstc == NUL ? '-' : firstc;
942 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +0200945 hiscnt = get_hislen(); /* set hiscnt to impossible history value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 histype = hist_char2type(firstc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947
948#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000949 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200952 /* If something above caused an error, reset the flags, we do want to type
953 * and execute commands. Display may be messed up a bit. */
954 if (did_emsg)
955 redrawcmd();
956 did_emsg = FALSE;
957 got_int = FALSE;
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /*
960 * Collect the command string, handling editing keys.
961 */
962 for (;;)
963 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000964 redir_off = TRUE; /* Don't redirect the typed command.
965 Repeated, because a ":redir" inside
966 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef USE_ON_FLY_SCROLL
968 dont_scroll = FALSE; /* allow scrolling here */
969#endif
970 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
971
Bram Moolenaar72532d32018-04-07 19:09:09 +0200972 did_emsg = FALSE; /* There can't really be a reason why an error
973 that occurs while typing a command should
974 cause the command not to be executed. */
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000977
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100978 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
979 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000980 do
Bram Moolenaar30405d32008-01-02 20:55:27 +0000981 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100982 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (KeyTyped)
985 {
986 some_key_typed = TRUE;
987#ifdef FEAT_RIGHTLEFT
988 if (cmd_hkmap)
989 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (cmdmsg_rl && !KeyStuffed)
991 {
992 /* Invert horizontal movements and operations. Only when
993 * typed by the user directly, not when the result of a
994 * mapping. */
995 switch (c)
996 {
997 case K_RIGHT: c = K_LEFT; break;
998 case K_S_RIGHT: c = K_S_LEFT; break;
999 case K_C_RIGHT: c = K_C_LEFT; break;
1000 case K_LEFT: c = K_RIGHT; break;
1001 case K_S_LEFT: c = K_S_RIGHT; break;
1002 case K_C_LEFT: c = K_C_RIGHT; break;
1003 }
1004 }
1005#endif
1006 }
1007
1008 /*
1009 * Ignore got_int when CTRL-C was typed here.
1010 * Don't ignore it in :global, we really need to break then, e.g., for
1011 * ":g/pat/normal /pat" (without the <CR>).
1012 * Don't ignore it for the input() function.
1013 */
1014 if ((c == Ctrl_C
1015#ifdef UNIX
1016 || c == intr_char
1017#endif
1018 )
1019#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1020 && firstc != '@'
1021#endif
1022#ifdef FEAT_EVAL
1023 && !break_ctrl_c
1024#endif
1025 && !global_busy)
1026 got_int = FALSE;
1027
Bram Moolenaard7663c22019-08-06 21:59:57 +02001028 // free old command line when finished moving around in the history
1029 // list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001031 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001032 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 && c != K_PAGEDOWN && c != K_PAGEUP
1034 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001035 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001037 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
1039 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001040 * When there are matching completions to select <S-Tab> works like
1041 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001043 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 c = Ctrl_P;
1045
1046#ifdef FEAT_WILDMENU
1047 /* Special translations for 'wildmenu' */
1048 if (did_wild_list && p_wmnu)
1049 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001050 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001052 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 c = Ctrl_N;
1054 }
1055 /* Hitting CR after "emenu Name.": complete submenu */
1056 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1057 && ccline.cmdpos > 1
1058 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1059 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1060 && (c == '\n' || c == '\r' || c == K_KENTER))
1061 c = K_DOWN;
1062#endif
1063
1064 /* free expanded names when finished walking through matches */
1065 if (xpc.xp_numfiles != -1
1066 && !(c == p_wc && KeyTyped) && c != p_wcm
1067 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1068 && c != Ctrl_L)
1069 {
1070 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1071 did_wild_list = FALSE;
1072#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001073 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#endif
1075 xpc.xp_context = EXPAND_NOTHING;
1076 wim_index = 0;
1077#ifdef FEAT_WILDMENU
1078 if (p_wmnu && wild_menu_showing != 0)
1079 {
1080 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001081 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001082
1083 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001084 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 if (wild_menu_showing == WM_SCROLLED)
1087 {
1088 /* Entered command line, move it up */
1089 cmdline_row--;
1090 redrawcmd();
1091 }
1092 else if (save_p_ls != -1)
1093 {
1094 /* restore 'laststatus' and 'winminheight' */
1095 p_ls = save_p_ls;
1096 p_wmh = save_p_wmh;
1097 last_status(FALSE);
1098 update_screen(VALID); /* redraw the screen NOW */
1099 redrawcmd();
1100 save_p_ls = -1;
1101 }
1102 else
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 redraw_statuslines();
1106 }
1107 KeyTyped = skt;
1108 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001109 if (ccline.input_fn)
1110 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112#endif
1113 }
1114
1115#ifdef FEAT_WILDMENU
1116 /* Special translations for 'wildmenu' */
1117 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1118 {
1119 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001120 if (c == K_DOWN && ccline.cmdpos > 0
1121 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001123 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
1125 /* Hitting <Up>: Remove one submenu name in front of the
1126 * cursor */
1127 int found = FALSE;
1128
1129 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1130 i = 0;
1131 while (--j > 0)
1132 {
1133 /* check for start of menu name */
1134 if (ccline.cmdbuff[j] == ' '
1135 && ccline.cmdbuff[j - 1] != '\\')
1136 {
1137 i = j + 1;
1138 break;
1139 }
1140 /* check for start of submenu name */
1141 if (ccline.cmdbuff[j] == '.'
1142 && ccline.cmdbuff[j - 1] != '\\')
1143 {
1144 if (found)
1145 {
1146 i = j + 1;
1147 break;
1148 }
1149 else
1150 found = TRUE;
1151 }
1152 }
1153 if (i > 0)
1154 cmdline_del(i);
1155 c = p_wc;
1156 xpc.xp_context = EXPAND_NOTHING;
1157 }
1158 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001159 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001160 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001161 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
1163 char_u upseg[5];
1164
1165 upseg[0] = PATHSEP;
1166 upseg[1] = '.';
1167 upseg[2] = '.';
1168 upseg[3] = PATHSEP;
1169 upseg[4] = NUL;
1170
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001171 if (c == K_DOWN
1172 && ccline.cmdpos > 0
1173 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1174 && (ccline.cmdpos < 3
1175 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1177 {
1178 /* go down a directory */
1179 c = p_wc;
1180 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001181 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
1183 /* If in a direct ancestor, strip off one ../ to go down */
1184 int found = FALSE;
1185
1186 j = ccline.cmdpos;
1187 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1188 while (--j > i)
1189 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001190 if (has_mbyte)
1191 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (vim_ispathsep(ccline.cmdbuff[j]))
1193 {
1194 found = TRUE;
1195 break;
1196 }
1197 }
1198 if (found
1199 && ccline.cmdbuff[j - 1] == '.'
1200 && ccline.cmdbuff[j - 2] == '.'
1201 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1202 {
1203 cmdline_del(j - 2);
1204 c = p_wc;
1205 }
1206 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001207 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 {
1209 /* go up a directory */
1210 int found = FALSE;
1211
1212 j = ccline.cmdpos - 1;
1213 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1214 while (--j > i)
1215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (has_mbyte)
1217 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (vim_ispathsep(ccline.cmdbuff[j])
1219#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001220 && vim_strchr((char_u *)" *?[{`$%#",
1221 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#endif
1223 )
1224 {
1225 if (found)
1226 {
1227 i = j + 1;
1228 break;
1229 }
1230 else
1231 found = TRUE;
1232 }
1233 }
1234
1235 if (!found)
1236 j = i;
1237 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1238 j += 4;
1239 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1240 && j == i)
1241 j += 3;
1242 else
1243 j = 0;
1244 if (j > 0)
1245 {
1246 /* TODO this is only for DOS/UNIX systems - need to put in
1247 * machine-specific stuff here and in upseg init */
1248 cmdline_del(j);
1249 put_on_cmdline(upseg + 1, 3, FALSE);
1250 }
1251 else if (ccline.cmdpos > i)
1252 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001253
1254 /* Now complete in the new directory. Set KeyTyped in case the
1255 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001257 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261#endif /* FEAT_WILDMENU */
1262
1263 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1264 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1265 if (c == Ctrl_BSL)
1266 {
1267 ++no_mapping;
1268 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001269 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 --no_mapping;
1271 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001272 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1273 * is in a mapping. */
1274 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001275 || (ccline.cmdfirstc == '=' && KeyTyped)
1276#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001277 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001278#endif
1279 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
1281 vungetc(c);
1282 c = Ctrl_BSL;
1283 }
1284#ifdef FEAT_EVAL
1285 else if (c == 'e')
1286 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001287 char_u *p = NULL;
1288 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 /*
1291 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001292 * Need to save and restore the current command line, to be
1293 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 */
1295 if (ccline.cmdpos == ccline.cmdlen)
1296 new_cmdpos = 99999; /* keep it at the end */
1297 else
1298 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (c == '=')
1302 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001303 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001304 * to avoid nasty things like going to another buffer when
1305 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001306 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001308 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001309
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001310 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001312 len = (int)STRLEN(p);
1313 if (realloc_cmdbuff(len + 1) == OK)
1314 {
1315 ccline.cmdlen = len;
1316 STRCPY(ccline.cmdbuff, p);
1317 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001319 /* Restore the cursor or use the position set with
1320 * set_cmdline_pos(). */
1321 if (new_cmdpos > ccline.cmdlen)
1322 ccline.cmdpos = ccline.cmdlen;
1323 else
1324 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001326 KeyTyped = FALSE; /* Don't do p_wc completion. */
1327 redrawcmd();
1328 goto cmdline_changed;
1329 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001330 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 }
1333 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001334 got_int = FALSE; /* don't abandon the command line */
1335 did_emsg = FALSE;
1336 emsg_on_display = FALSE;
1337 redrawcmd();
1338 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340#endif
1341 else
1342 {
1343 if (c == Ctrl_G && p_im && restart_edit == 0)
1344 restart_edit = 'a';
1345 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1346 in history */
1347 goto returncmd; /* back to Normal mode */
1348 }
1349 }
1350
1351#ifdef FEAT_CMDWIN
1352 if (c == cedit_key || c == K_CMDWIN)
1353 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001354 if (ex_normal_busy == 0 && got_int == FALSE)
1355 {
1356 /*
1357 * Open a window to edit the command line (and history).
1358 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001359 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001360 some_key_typed = TRUE;
1361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363# ifdef FEAT_DIGRAPHS
1364 else
1365# endif
1366#endif
1367#ifdef FEAT_DIGRAPHS
1368 c = do_digraph(c);
1369#endif
1370
1371 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1372 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1373 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001374 /* In Ex mode a backslash escapes a newline. */
1375 if (exmode_active
1376 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001377 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001378 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001379 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001381 if (c == K_KENTER)
1382 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 else
1385 {
1386 gotesc = FALSE; /* Might have typed ESC previously, don't
1387 truncate the cmdline now. */
1388 if (ccheck_abbr(c + ABBR_OFF))
1389 goto cmdline_changed;
1390 if (!cmd_silent)
1391 {
1392 windgoto(msg_row, 0);
1393 out_flush();
1394 }
1395 break;
1396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398
1399 /*
1400 * Completion for 'wildchar' or 'wildcharm' key.
1401 * - hitting <ESC> twice means: abandon command line.
1402 * - wildcard expansion is only done when the 'wildchar' key is really
1403 * typed, not when it comes from a macro
1404 */
1405 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1406 {
1407 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1408 {
1409 /* if 'wildmode' contains "list" may still need to list */
1410 if (xpc.xp_numfiles > 1
1411 && !did_wild_list
1412 && (wim_flags[wim_index] & WIM_LIST))
1413 {
1414 (void)showmatches(&xpc, FALSE);
1415 redrawcmd();
1416 did_wild_list = TRUE;
1417 }
1418 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001419 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1420 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001422 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1423 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 else
1425 res = OK; /* don't insert 'wildchar' now */
1426 }
1427 else /* typed p_wc first time */
1428 {
1429 wim_index = 0;
1430 j = ccline.cmdpos;
1431 /* if 'wildmode' first contains "longest", get longest
1432 * common part */
1433 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001434 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1435 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001437 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1438 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440 /* if interrupted while completing, behave like it failed */
1441 if (got_int)
1442 {
1443 (void)vpeekc(); /* remove <C-C> from input stream */
1444 got_int = FALSE; /* don't abandon the command line */
1445 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1446#ifdef FEAT_WILDMENU
1447 xpc.xp_context = EXPAND_NOTHING;
1448#endif
1449 goto cmdline_changed;
1450 }
1451
1452 /* when more than one match, and 'wildmode' first contains
1453 * "list", or no change and 'wildmode' contains "longest,list",
1454 * list all matches */
1455 if (res == OK && xpc.xp_numfiles > 1)
1456 {
1457 /* a "longest" that didn't do anything is skipped (but not
1458 * "list:longest") */
1459 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1460 wim_index = 1;
1461 if ((wim_flags[wim_index] & WIM_LIST)
1462#ifdef FEAT_WILDMENU
1463 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1464#endif
1465 )
1466 {
1467 if (!(wim_flags[0] & WIM_LONGEST))
1468 {
1469#ifdef FEAT_WILDMENU
1470 int p_wmnu_save = p_wmnu;
1471 p_wmnu = 0;
1472#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 /* remove match */
1474 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475#ifdef FEAT_WILDMENU
1476 p_wmnu = p_wmnu_save;
1477#endif
1478 }
1479#ifdef FEAT_WILDMENU
1480 (void)showmatches(&xpc, p_wmnu
1481 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1482#else
1483 (void)showmatches(&xpc, FALSE);
1484#endif
1485 redrawcmd();
1486 did_wild_list = TRUE;
1487 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001488 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1489 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001491 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1492 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001495 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 }
1497#ifdef FEAT_WILDMENU
1498 else if (xpc.xp_numfiles == -1)
1499 xpc.xp_context = EXPAND_NOTHING;
1500#endif
1501 }
1502 if (wim_index < 3)
1503 ++wim_index;
1504 if (c == ESC)
1505 gotesc = TRUE;
1506 if (res == OK)
1507 goto cmdline_changed;
1508 }
1509
1510 gotesc = FALSE;
1511
1512 /* <S-Tab> goes to last match, in a clumsy way */
1513 if (c == K_S_TAB && KeyTyped)
1514 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001515 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1516 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1517 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 goto cmdline_changed;
1519 }
1520
1521 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1522 c = NL;
1523
1524 do_abbr = TRUE; /* default: check for abbreviation */
1525
1526 /*
1527 * Big switch for a typed command line character.
1528 */
1529 switch (c)
1530 {
1531 case K_BS:
1532 case Ctrl_H:
1533 case K_DEL:
1534 case K_KDEL:
1535 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (c == K_KDEL)
1537 c = K_DEL;
1538
1539 /*
1540 * delete current character is the same as backspace on next
1541 * character, except at end of line
1542 */
1543 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1544 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (has_mbyte && c == K_DEL)
1546 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1547 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (ccline.cmdpos > 0)
1549 {
1550 char_u *p;
1551
1552 j = ccline.cmdpos;
1553 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (has_mbyte)
1555 {
1556 p = mb_prevptr(ccline.cmdbuff, p);
1557 if (c == Ctrl_W)
1558 {
1559 while (p > ccline.cmdbuff && vim_isspace(*p))
1560 p = mb_prevptr(ccline.cmdbuff, p);
1561 i = mb_get_class(p);
1562 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1563 p = mb_prevptr(ccline.cmdbuff, p);
1564 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001565 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001568 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1571 --p;
1572 i = vim_iswordc(p[-1]);
1573 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1574 && vim_iswordc(p[-1]) == i)
1575 --p;
1576 }
1577 else
1578 --p;
1579 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1580 ccline.cmdlen -= j - ccline.cmdpos;
1581 i = ccline.cmdpos;
1582 while (i < ccline.cmdlen)
1583 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1584
1585 /* Truncate at the end, required for multi-byte chars. */
1586 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001587#ifdef FEAT_SEARCH_EXTRA
1588 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001589 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001590 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001591 /* save view settings, so that the screen
1592 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001593 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001594 }
1595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 redrawcmd();
1597 }
1598 else if (ccline.cmdlen == 0 && c != Ctrl_W
1599 && ccline.cmdprompt == NULL && indent == 0)
1600 {
1601 /* In ex and debug mode it doesn't make sense to return. */
1602 if (exmode_active
1603#ifdef FEAT_EVAL
1604 || ccline.cmdfirstc == '>'
1605#endif
1606 )
1607 goto cmdline_not_changed;
1608
Bram Moolenaard23a8232018-02-10 18:45:26 +01001609 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (!cmd_silent)
1611 {
1612#ifdef FEAT_RIGHTLEFT
1613 if (cmdmsg_rl)
1614 msg_col = Columns;
1615 else
1616#endif
1617 msg_col = 0;
1618 msg_putchar(' '); /* delete ':' */
1619 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001620#ifdef FEAT_SEARCH_EXTRA
1621 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001622 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 redraw_cmdline = TRUE;
1625 goto returncmd; /* back to cmd mode */
1626 }
1627 goto cmdline_changed;
1628
1629 case K_INS:
1630 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 ccline.overstrike = !ccline.overstrike;
1632#ifdef CURSOR_SHAPE
1633 ui_cursor_shape(); /* may show different cursor shape */
1634#endif
1635 goto cmdline_not_changed;
1636
1637 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001638 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
1640 /* ":lmap" mappings exists, toggle use of mappings. */
1641 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 im_set_active(FALSE); /* Disable input method */
1644#endif
1645 if (b_im_ptr != NULL)
1646 {
1647 if (State & LANGMAP)
1648 *b_im_ptr = B_IMODE_LMAP;
1649 else
1650 *b_im_ptr = B_IMODE_NONE;
1651 }
1652 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 else
1655 {
1656 /* There are no ":lmap" mappings, toggle IM. When
1657 * 'imdisable' is set don't try getting the status, it's
1658 * always off. */
1659 if ((p_imdisable && b_im_ptr != NULL)
1660 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1661 {
1662 im_set_active(FALSE); /* Disable input method */
1663 if (b_im_ptr != NULL)
1664 *b_im_ptr = B_IMODE_NONE;
1665 }
1666 else
1667 {
1668 im_set_active(TRUE); /* Enable input method */
1669 if (b_im_ptr != NULL)
1670 *b_im_ptr = B_IMODE_IM;
1671 }
1672 }
1673#endif
1674 if (b_im_ptr != NULL)
1675 {
1676 if (b_im_ptr == &curbuf->b_p_iminsert)
1677 set_iminsert_global();
1678 else
1679 set_imsearch_global();
1680 }
1681#ifdef CURSOR_SHAPE
1682 ui_cursor_shape(); /* may show different cursor shape */
1683#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001684#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 /* Show/unshow value of 'keymap' in status lines later. */
1686 status_redraw_curbuf();
1687#endif
1688 goto cmdline_not_changed;
1689
1690/* case '@': only in very old vi */
1691 case Ctrl_U:
1692 /* delete all characters left of the cursor */
1693 j = ccline.cmdpos;
1694 ccline.cmdlen -= j;
1695 i = ccline.cmdpos = 0;
1696 while (i < ccline.cmdlen)
1697 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1698 /* Truncate at the end, required for multi-byte chars. */
1699 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001700#ifdef FEAT_SEARCH_EXTRA
1701 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001702 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 redrawcmd();
1705 goto cmdline_changed;
1706
1707#ifdef FEAT_CLIPBOARD
1708 case Ctrl_Y:
1709 /* Copy the modeless selection, if there is one. */
1710 if (clip_star.state != SELECT_CLEARED)
1711 {
1712 if (clip_star.state == SELECT_DONE)
1713 clip_copy_modeless_selection(TRUE);
1714 goto cmdline_not_changed;
1715 }
1716 break;
1717#endif
1718
1719 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1720 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001721 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001722 * ":normal" runs out of characters. */
1723 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001724 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 goto cmdline_not_changed;
1726
1727 gotesc = TRUE; /* will free ccline.cmdbuff after
1728 putting it in history */
1729 goto returncmd; /* back to cmd mode */
1730
1731 case Ctrl_R: /* insert register */
1732#ifdef USE_ON_FLY_SCROLL
1733 dont_scroll = TRUE; /* disallow scrolling here */
1734#endif
1735 putcmdline('"', TRUE);
1736 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001737 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (i == Ctrl_O)
1739 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1740 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001741 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001742 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 --no_mapping;
1744#ifdef FEAT_EVAL
1745 /*
1746 * Insert the result of an expression.
1747 * Need to save the current command line, to be able to enter
1748 * a new one...
1749 */
1750 new_cmdpos = -1;
1751 if (c == '=')
1752 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001753 if (ccline.cmdfirstc == '=' // can't do this recursively
1754 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 {
1756 beep_flush();
1757 c = ESC;
1758 }
1759 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762#endif
1763 if (c != ESC) /* use ESC to cancel inserting register */
1764 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001765 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001766
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001767#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001768 /* When there was a serious error abort getting the
1769 * command line. */
1770 if (aborting())
1771 {
1772 gotesc = TRUE; /* will free ccline.cmdbuff after
1773 putting it in history */
1774 goto returncmd; /* back to cmd mode */
1775 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 KeyTyped = FALSE; /* Don't do p_wc completion. */
1778#ifdef FEAT_EVAL
1779 if (new_cmdpos >= 0)
1780 {
1781 /* set_cmdline_pos() was used */
1782 if (new_cmdpos > ccline.cmdlen)
1783 ccline.cmdpos = ccline.cmdlen;
1784 else
1785 ccline.cmdpos = new_cmdpos;
1786 }
1787#endif
1788 }
1789 redrawcmd();
1790 goto cmdline_changed;
1791
1792 case Ctrl_D:
1793 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1794 break; /* Use ^D as normal char instead */
1795
1796 redrawcmd();
1797 continue; /* don't do incremental search now */
1798
1799 case K_RIGHT:
1800 case K_S_RIGHT:
1801 case K_C_RIGHT:
1802 do
1803 {
1804 if (ccline.cmdpos >= ccline.cmdlen)
1805 break;
1806 i = cmdline_charsize(ccline.cmdpos);
1807 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1808 break;
1809 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001811 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 + ccline.cmdpos);
1813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 ++ccline.cmdpos;
1815 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001816 while ((c == K_S_RIGHT || c == K_C_RIGHT
1817 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (has_mbyte)
1820 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 goto cmdline_not_changed;
1822
1823 case K_LEFT:
1824 case K_S_LEFT:
1825 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001826 if (ccline.cmdpos == 0)
1827 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 do
1829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (has_mbyte) /* move to first byte of char */
1832 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1833 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1835 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001836 while (ccline.cmdpos > 0
1837 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001838 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (has_mbyte)
1841 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 goto cmdline_not_changed;
1843
1844 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001845 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001846 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar4f974752019-02-17 17:44:42 +01001848#ifdef FEAT_GUI_MSWIN
1849 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1850 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001851 case K_F4:
1852 if (mod_mask == MOD_MASK_ALT)
1853 {
1854 redrawcmd(); /* somehow the cmdline is cleared */
1855 goto cmdline_not_changed;
1856 }
1857 break;
1858#endif
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#ifdef FEAT_MOUSE
1861 case K_MIDDLEDRAG:
1862 case K_MIDDLERELEASE:
1863 goto cmdline_not_changed; /* Ignore mouse */
1864
1865 case K_MIDDLEMOUSE:
1866# ifdef FEAT_GUI
1867 /* When GUI is active, also paste when 'mouse' is empty */
1868 if (!gui.in_use)
1869# endif
1870 if (!mouse_has(MOUSE_COMMAND))
1871 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001872# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001874 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001876# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001877 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 redrawcmd();
1879 goto cmdline_changed;
1880
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001881# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001883 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 redrawcmd();
1885 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 case K_LEFTDRAG:
1889 case K_LEFTRELEASE:
1890 case K_RIGHTDRAG:
1891 case K_RIGHTRELEASE:
1892 /* Ignore drag and release events when the button-down wasn't
1893 * seen before. */
1894 if (ignore_drag_release)
1895 goto cmdline_not_changed;
1896 /* FALLTHROUGH */
1897 case K_LEFTMOUSE:
1898 case K_RIGHTMOUSE:
1899 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1900 ignore_drag_release = TRUE;
1901 else
1902 ignore_drag_release = FALSE;
1903# ifdef FEAT_GUI
1904 /* When GUI is active, also move when 'mouse' is empty */
1905 if (!gui.in_use)
1906# endif
1907 if (!mouse_has(MOUSE_COMMAND))
1908 goto cmdline_not_changed; /* Ignore mouse */
1909# ifdef FEAT_CLIPBOARD
1910 if (mouse_row < cmdline_row && clip_star.available)
1911 {
1912 int button, is_click, is_drag;
1913
1914 /*
1915 * Handle modeless selection.
1916 */
1917 button = get_mouse_button(KEY2TERMCAP1(c),
1918 &is_click, &is_drag);
1919 if (mouse_model_popup() && button == MOUSE_LEFT
1920 && (mod_mask & MOD_MASK_SHIFT))
1921 {
1922 /* Translate shift-left to right button. */
1923 button = MOUSE_RIGHT;
1924 mod_mask &= ~MOD_MASK_SHIFT;
1925 }
1926 clip_modeless(button, is_click, is_drag);
1927 goto cmdline_not_changed;
1928 }
1929# endif
1930
1931 set_cmdspos();
1932 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1933 ++ccline.cmdpos)
1934 {
1935 i = cmdline_charsize(ccline.cmdpos);
1936 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1937 && mouse_col < ccline.cmdspos % Columns + i)
1938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (has_mbyte)
1940 {
1941 /* Count ">" for double-wide char that doesn't fit. */
1942 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001943 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 + ccline.cmdpos) - 1;
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 ccline.cmdspos += i;
1947 }
1948 goto cmdline_not_changed;
1949
1950 /* Mouse scroll wheel: ignored here */
1951 case K_MOUSEDOWN:
1952 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001953 case K_MOUSELEFT:
1954 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 /* Alternate buttons ignored here */
1956 case K_X1MOUSE:
1957 case K_X1DRAG:
1958 case K_X1RELEASE:
1959 case K_X2MOUSE:
1960 case K_X2DRAG:
1961 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001962 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 goto cmdline_not_changed;
1964
1965#endif /* FEAT_MOUSE */
1966
1967#ifdef FEAT_GUI
1968 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1969 case K_LEFTRELEASE_NM:
1970 goto cmdline_not_changed;
1971
1972 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001973 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 gui_do_scroll();
1976 redrawcmd();
1977 }
1978 goto cmdline_not_changed;
1979
1980 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001981 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001983 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 redrawcmd();
1985 }
1986 goto cmdline_not_changed;
1987#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001988#ifdef FEAT_GUI_TABLINE
1989 case K_TABLINE:
1990 case K_TABMENU:
1991 /* Don't want to change any tabs here. Make sure the same tab
1992 * is still selected. */
1993 if (gui_use_tabline())
1994 gui_mch_set_curtab(tabpage_index(curtab));
1995 goto cmdline_not_changed;
1996#endif
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 case K_SELECT: /* end of Select mode mapping - ignore */
1999 goto cmdline_not_changed;
2000
2001 case Ctrl_B: /* begin of command line */
2002 case K_HOME:
2003 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 case K_S_HOME:
2005 case K_C_HOME:
2006 ccline.cmdpos = 0;
2007 set_cmdspos();
2008 goto cmdline_not_changed;
2009
2010 case Ctrl_E: /* end of command line */
2011 case K_END:
2012 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 case K_S_END:
2014 case K_C_END:
2015 ccline.cmdpos = ccline.cmdlen;
2016 set_cmdspos_cursor();
2017 goto cmdline_not_changed;
2018
2019 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002020 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 break;
2022 goto cmdline_changed;
2023
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002024 case Ctrl_L:
2025#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002026 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002027 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002028#endif
2029
2030 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002031 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 break;
2033 goto cmdline_changed;
2034
2035 case Ctrl_N: /* next match */
2036 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002037 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002039 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2040 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002042 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002044 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 case K_UP:
2046 case K_DOWN:
2047 case K_S_UP:
2048 case K_S_DOWN:
2049 case K_PAGEUP:
2050 case K_KPAGEUP:
2051 case K_PAGEDOWN:
2052 case K_KPAGEDOWN:
Bram Moolenaard7663c22019-08-06 21:59:57 +02002053 if (get_hislen() == 0 || firstc == NUL) /* no history */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 goto cmdline_not_changed;
2055
2056 i = hiscnt;
2057
2058 /* save current command string so it can be restored later */
2059 if (lookfor == NULL)
2060 {
2061 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2062 goto cmdline_not_changed;
2063 lookfor[ccline.cmdpos] = NUL;
2064 }
2065
2066 j = (int)STRLEN(lookfor);
2067 for (;;)
2068 {
2069 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002070 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002071 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002073 if (hiscnt == get_hislen()) /* first time */
2074 hiscnt = *get_hisidx(histype);
2075 else if (hiscnt == 0 && *get_hisidx(histype) != get_hislen() - 1)
2076 hiscnt = get_hislen() - 1;
2077 else if (hiscnt != *get_hisidx(histype) + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 --hiscnt;
2079 else /* at top of list */
2080 {
2081 hiscnt = i;
2082 break;
2083 }
2084 }
2085 else /* one step forwards */
2086 {
2087 /* on last entry, clear the line */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002088 if (hiscnt == *get_hisidx(histype))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02002090 hiscnt = get_hislen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 break;
2092 }
2093
2094 /* not on a history line, nothing to do */
Bram Moolenaard7663c22019-08-06 21:59:57 +02002095 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002097 if (hiscnt == get_hislen() - 1) /* wrap around */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 hiscnt = 0;
2099 else
2100 ++hiscnt;
2101 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02002102 if (hiscnt < 0 || get_histentry(histype)[hiscnt].hisstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 hiscnt = i;
2105 break;
2106 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002107 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002108 || hiscnt == i
Bram Moolenaard7663c22019-08-06 21:59:57 +02002109 || STRNCMP(get_histentry(histype)[hiscnt].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 lookfor, (size_t)j) == 0)
2111 break;
2112 }
2113
2114 if (hiscnt != i) /* jumped to other entry */
2115 {
2116 char_u *p;
2117 int len;
2118 int old_firstc;
2119
Bram Moolenaar438d1762018-09-30 17:11:48 +02002120 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002121 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaard7663c22019-08-06 21:59:57 +02002122 if (hiscnt == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 p = lookfor; /* back to the old one */
2124 else
Bram Moolenaard7663c22019-08-06 21:59:57 +02002125 p = get_histentry(histype)[hiscnt].hisstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127 if (histype == HIST_SEARCH
2128 && p != lookfor
2129 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2130 {
2131 /* Correct for the separator character used when
2132 * adding the history entry vs the one used now.
2133 * First loop: count length.
2134 * Second loop: copy the characters. */
2135 for (i = 0; i <= 1; ++i)
2136 {
2137 len = 0;
2138 for (j = 0; p[j] != NUL; ++j)
2139 {
2140 /* Replace old sep with new sep, unless it is
2141 * escaped. */
2142 if (p[j] == old_firstc
2143 && (j == 0 || p[j - 1] != '\\'))
2144 {
2145 if (i > 0)
2146 ccline.cmdbuff[len] = firstc;
2147 }
2148 else
2149 {
2150 /* Escape new sep, unless it is already
2151 * escaped. */
2152 if (p[j] == firstc
2153 && (j == 0 || p[j - 1] != '\\'))
2154 {
2155 if (i > 0)
2156 ccline.cmdbuff[len] = '\\';
2157 ++len;
2158 }
2159 if (i > 0)
2160 ccline.cmdbuff[len] = p[j];
2161 }
2162 ++len;
2163 }
2164 if (i == 0)
2165 {
2166 alloc_cmdbuff(len);
2167 if (ccline.cmdbuff == NULL)
2168 goto returncmd;
2169 }
2170 }
2171 ccline.cmdbuff[len] = NUL;
2172 }
2173 else
2174 {
2175 alloc_cmdbuff((int)STRLEN(p));
2176 if (ccline.cmdbuff == NULL)
2177 goto returncmd;
2178 STRCPY(ccline.cmdbuff, p);
2179 }
2180
2181 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2182 redrawcmd();
2183 goto cmdline_changed;
2184 }
2185 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002186 goto cmdline_not_changed;
2187
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002188#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002189 case Ctrl_G: /* next match */
2190 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002191 if (may_adjust_incsearch_highlighting(
2192 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002193 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196
2197 case Ctrl_V:
2198 case Ctrl_Q:
2199#ifdef FEAT_MOUSE
2200 ignore_drag_release = TRUE;
2201#endif
2202 putcmdline('^', TRUE);
2203 c = get_literal(); /* get next (two) character(s) */
2204 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002205 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /* may need to remove ^ when composing char was typed */
2207 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2208 {
2209 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2210 msg_putchar(' ');
2211 cursorcmd();
2212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 break;
2214
2215#ifdef FEAT_DIGRAPHS
2216 case Ctrl_K:
2217#ifdef FEAT_MOUSE
2218 ignore_drag_release = TRUE;
2219#endif
2220 putcmdline('?', TRUE);
2221#ifdef USE_ON_FLY_SCROLL
2222 dont_scroll = TRUE; /* disallow scrolling here */
2223#endif
2224 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002225 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (c != NUL)
2227 break;
2228
2229 redrawcmd();
2230 goto cmdline_not_changed;
2231#endif /* FEAT_DIGRAPHS */
2232
2233#ifdef FEAT_RIGHTLEFT
2234 case Ctrl__: /* CTRL-_: switch language mode */
2235 if (!p_ari)
2236 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002237 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 goto cmdline_not_changed;
2239#endif
2240
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002241 case K_PS:
2242 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2243 goto cmdline_changed;
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 default:
2246#ifdef UNIX
2247 if (c == intr_char)
2248 {
2249 gotesc = TRUE; /* will free ccline.cmdbuff after
2250 putting it in history */
2251 goto returncmd; /* back to Normal mode */
2252 }
2253#endif
2254 /*
2255 * Normal character with no special meaning. Just set mod_mask
2256 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2257 * the string <S-Space>. This should only happen after ^V.
2258 */
2259 if (!IS_SPECIAL(c))
2260 mod_mask = 0x0;
2261 break;
2262 }
2263 /*
2264 * End of switch on command line character.
2265 * We come here if we have a normal character.
2266 */
2267
Bram Moolenaar13505972019-01-24 15:04:48 +01002268 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2269 && (ccheck_abbr(
2270 // Add ABBR_OFF for characters above 0x100, this is
2271 // what check_abbr() expects.
2272 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2273 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 goto cmdline_changed;
2275
2276 /*
2277 * put the character in the command line
2278 */
2279 if (IS_SPECIAL(c) || mod_mask != 0)
2280 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2281 else
2282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (has_mbyte)
2284 {
2285 j = (*mb_char2bytes)(c, IObuff);
2286 IObuff[j] = NUL; /* exclude composing chars */
2287 put_on_cmdline(IObuff, j, TRUE);
2288 }
2289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 IObuff[0] = c;
2292 put_on_cmdline(IObuff, 1, TRUE);
2293 }
2294 }
2295 goto cmdline_changed;
2296
2297/*
2298 * This part implements incremental searches for "/" and "?"
2299 * Jump to cmdline_not_changed when a character has been read but the command
2300 * line did not change. Then we only search and redraw if something changed in
2301 * the past.
2302 * Jump to cmdline_changed when the command line did change.
2303 * (Sorry for the goto's, I know it is ugly).
2304 */
2305cmdline_not_changed:
2306#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002307 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 continue;
2309#endif
2310
2311cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002312 /* Trigger CmdlineChanged autocommands. */
2313 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002316 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
2318
2319#ifdef FEAT_RIGHTLEFT
2320 if (cmdmsg_rl
2321# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002322 || (p_arshape && !p_tbidi
2323 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324# endif
2325 )
2326 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002327 * right-left typing. Not efficient, but it works.
2328 * Do it only when there are no characters left to read
2329 * to avoid useless intermediate redraws. */
2330 if (vpeekc() == NUL)
2331 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333 }
2334
2335returncmd:
2336
2337#ifdef FEAT_RIGHTLEFT
2338 cmdmsg_rl = FALSE;
2339#endif
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002342 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002345 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#endif
2347
2348 if (ccline.cmdbuff != NULL)
2349 {
2350 /*
2351 * Put line in history buffer (":" and "=" only when it was typed).
2352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 if (ccline.cmdlen && firstc != NUL
2354 && (some_key_typed || histype == HIST_SEARCH))
2355 {
2356 add_to_history(histype, ccline.cmdbuff, TRUE,
2357 histype == HIST_SEARCH ? firstc : NUL);
2358 if (firstc == ':')
2359 {
2360 vim_free(new_last_cmdline);
2361 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2362 }
2363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002365 if (gotesc)
2366 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368
2369 /*
2370 * If the screen was shifted up, redraw the whole screen (later).
2371 * If the line is too long, clear it, so ruler and shown command do
2372 * not get printed in the middle of it.
2373 */
2374 msg_check();
2375 msg_scroll = save_msg_scroll;
2376 redir_off = FALSE;
2377
2378 /* When the command line was typed, no need for a wait-return prompt. */
2379 if (some_key_typed)
2380 need_wait_return = FALSE;
2381
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002382 /* Trigger CmdlineLeave autocommands. */
2383 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002386#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2388 im_save_status(b_im_ptr);
2389 im_set_active(FALSE);
2390#endif
2391#ifdef FEAT_MOUSE
2392 setmouse();
2393#endif
2394#ifdef CURSOR_SHAPE
2395 ui_cursor_shape(); /* may show different cursor shape */
2396#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002397 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaar438d1762018-09-30 17:11:48 +02002399theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002400 {
2401 char_u *p = ccline.cmdbuff;
2402
Bram Moolenaar438d1762018-09-30 17:11:48 +02002403 if (did_save_ccline)
2404 restore_cmdline(&save_ccline);
2405 else
2406 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002407 return p;
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409}
2410
2411#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2412/*
2413 * Get a command line with a prompt.
2414 * This is prepared to be called recursively from getcmdline() (e.g. by
2415 * f_input() when evaluating an expression from CTRL-R =).
2416 * Returns the command line in allocated memory, or NULL.
2417 */
2418 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002419getcmdline_prompt(
2420 int firstc,
2421 char_u *prompt, /* command line prompt */
2422 int attr, /* attributes for prompt */
2423 int xp_context, /* type of expansion */
2424 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426 char_u *s;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002427 cmdline_info_T save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002428 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002430 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaar438d1762018-09-30 17:11:48 +02002432 if (ccline.cmdbuff != NULL)
2433 {
2434 // Save the values of the current cmdline and restore them below.
2435 save_cmdline(&save_ccline);
2436 did_save_ccline = TRUE;
2437 }
2438
Bram Moolenaar66b51422019-08-18 21:44:12 +02002439 vim_memset(&ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 ccline.cmdprompt = prompt;
2441 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002442# ifdef FEAT_EVAL
2443 ccline.xp_context = xp_context;
2444 ccline.xp_arg = xp_arg;
2445 ccline.input_fn = (firstc == '@');
2446# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002447 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002448 s = getcmdline_int(firstc, 1L, 0, FALSE);
2449
2450 if (did_save_ccline)
2451 restore_cmdline(&save_ccline);
2452
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002453 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002454 /* Restore msg_col, the prompt from input() may have changed it.
2455 * But only if called recursively and the commandline is therefore being
2456 * restored to an old one; if not, the input() prompt stays on the screen,
2457 * so we need its modified msg_col left intact. */
2458 if (ccline.cmdbuff != NULL)
2459 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 return s;
2462}
2463#endif
2464
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002465/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002466 * Return TRUE when the text must not be changed and we can't switch to
2467 * another window or buffer. Used when editing the command line, evaluating
2468 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002469 */
2470 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002471text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002472{
2473#ifdef FEAT_CMDWIN
2474 if (cmdwin_type != 0)
2475 return TRUE;
2476#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002477 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002478}
2479
2480/*
2481 * Give an error message for a command that isn't allowed while the cmdline
2482 * window is open or editing the cmdline in another way.
2483 */
2484 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002485text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002486{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002487 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002488}
2489
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002491get_text_locked_msg(void)
2492{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002493#ifdef FEAT_CMDWIN
2494 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002495 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002496#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002497 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002498}
2499
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002501 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2502 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002503 */
2504 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002505curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002506{
2507 if (curbuf_lock > 0)
2508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002509 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002510 return TRUE;
2511 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002512 return allbuf_locked();
2513}
2514
2515/*
2516 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2517 * message.
2518 */
2519 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002520allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002521{
2522 if (allbuf_lock > 0)
2523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002524 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002525 return TRUE;
2526 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002527 return FALSE;
2528}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002531cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2534 if (cmdline_star > 0) /* showing '*', always 1 position */
2535 return 1;
2536#endif
2537 return ptr2cells(ccline.cmdbuff + idx);
2538}
2539
2540/*
2541 * Compute the offset of the cursor on the command line for the prompt and
2542 * indent.
2543 */
2544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002547 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 ccline.cmdspos = 1 + ccline.cmdindent;
2549 else
2550 ccline.cmdspos = 0 + ccline.cmdindent;
2551}
2552
2553/*
2554 * Compute the screen position for the cursor on the command line.
2555 */
2556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002557set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 int i, m, c;
2560
2561 set_cmdspos();
2562 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002565 if (m < 0) /* overflow, Columns or Rows at weird value */
2566 m = MAXCOL;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 else
2569 m = MAXCOL;
2570 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2571 {
2572 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2574 if (has_mbyte)
2575 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002576 /* If the cmdline doesn't fit, show cursor on last visible char.
2577 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if ((ccline.cmdspos += c) >= m)
2579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 ccline.cmdspos -= c;
2581 break;
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002584 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586}
2587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2590 * character that doesn't fit, so that a ">" must be displayed.
2591 */
2592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002595 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2597 && ccline.cmdspos % Columns + cells > Columns)
2598 ccline.cmdspos++;
2599}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601/*
2602 * Get an Ex command line for the ":" command.
2603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002605getexline(
2606 int c, /* normally ':', NUL for ":append" */
2607 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002608 int indent, /* indent for inside conditionals */
2609 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
2611 /* When executing a register, remove ':' that's in front of each line. */
2612 if (exec_from_reg && vpeekc() == ':')
2613 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002614 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615}
2616
2617/*
2618 * Get an Ex command line for Ex mode.
2619 * In Ex mode we only use the OS supplied line editing features and no
2620 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002624getexmodeline(
2625 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002628 int indent, /* indent for inside conditionals */
2629 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 garray_T line_ga;
2632 char_u *pend;
2633 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002634 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 int escaped = FALSE; /* CTRL-V typed */
2636 int vcol = 0;
2637 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002638 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002639 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 /* Switch cursor on now. This avoids that it happens after the "\n", which
2642 * confuses the system function that computes tabstops. */
2643 cursor_on();
2644
2645 /* always start in column 0; write a newline if necessary */
2646 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002649 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002651 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 if (p_prompt)
2653 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 while (indent-- > 0)
2655 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658
2659 ga_init2(&line_ga, 1, 30);
2660
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002661 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002662 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002663 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002664 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002665 while (indent >= 8)
2666 {
2667 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002668 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002669 indent -= 8;
2670 }
2671 while (indent-- > 0)
2672 {
2673 ga_append(&line_ga, ' ');
2674 msg_putchar(' ');
2675 }
2676 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 ++no_mapping;
2678 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /*
2681 * Get the line, one character at a time.
2682 */
2683 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002684 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002686 long sw;
2687 char_u *s;
2688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (ga_grow(&line_ga, 40) == FAIL)
2690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
Bram Moolenaarba748c82017-02-26 14:00:07 +01002692 /*
2693 * Get one character at a time.
2694 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002695 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002696
2697 /* Check for a ":normal" command and no more characters left. */
2698 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2699 c1 = '\n';
2700 else
2701 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 * Handle line editing.
2705 * Previously this was left to the system, putting the terminal in
2706 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 msg_putchar('\n');
2711 break;
2712 }
2713
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002714 if (c1 == K_PS)
2715 {
2716 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2717 goto redraw;
2718 }
2719
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 if (!escaped)
2721 {
2722 /* CR typed means "enter", which is NL */
2723 if (c1 == '\r')
2724 c1 = '\n';
2725
2726 if (c1 == BS || c1 == K_BS
2727 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if (line_ga.ga_len > 0)
2730 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002731 if (has_mbyte)
2732 {
2733 p = (char_u *)line_ga.ga_data;
2734 p[line_ga.ga_len] = NUL;
2735 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2736 line_ga.ga_len -= len;
2737 }
2738 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002739 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002740 goto redraw;
2741 }
2742 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 msg_col = startcol;
2748 msg_clr_eos();
2749 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002750 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751 }
2752
2753 if (c1 == Ctrl_T)
2754 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002755 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 p = (char_u *)line_ga.ga_data;
2757 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002758 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002759 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002761 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002763 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002764 p = (char_u *)line_ga.ga_data;
2765 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2767 *s = ' ';
2768 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770redraw:
2771 /* redraw the line */
2772 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002774 p = (char_u *)line_ga.ga_data;
2775 p[line_ga.ga_len] = NUL;
2776 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002780 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002782 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002783 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002787 len = MB_PTR2LEN(p);
2788 msg_outtrans_len(p, len);
2789 vcol += ptr2cells(p);
2790 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002794 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 continue;
2796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 if (c1 == Ctrl_D)
2799 {
2800 /* Delete one shiftwidth. */
2801 p = (char_u *)line_ga.ga_data;
2802 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 if (prev_char == '^')
2805 ex_keep_indent = TRUE;
2806 indent = 0;
2807 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 else
2810 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002812 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002813 if (indent > 0)
2814 {
2815 --indent;
2816 indent -= indent % get_sw_value(curbuf);
2817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002819 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002821 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2823 --line_ga.ga_len;
2824 }
2825 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827
2828 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2829 {
2830 escaped = TRUE;
2831 continue;
2832 }
2833
2834 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2835 if (IS_SPECIAL(c1))
2836 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002838
2839 if (IS_SPECIAL(c1))
2840 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002841 if (has_mbyte)
2842 len = (*mb_char2bytes)(c1,
2843 (char_u *)line_ga.ga_data + line_ga.ga_len);
2844 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002845 {
2846 len = 1;
2847 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2848 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 if (c1 == '\n')
2850 msg_putchar('\n');
2851 else if (c1 == TAB)
2852 {
2853 /* Don't use chartabsize(), 'ts' can be different */
2854 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002856 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002861 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002864 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 escaped = FALSE;
2866
2867 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002868 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002870 /* We are done when a NL is entered, but not when it comes after an
2871 * odd number of backslashes, that results in a NUL. */
2872 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002874 int bcount = 0;
2875
2876 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2877 ++bcount;
2878
2879 if (bcount > 0)
2880 {
2881 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2882 * "\NL", etc. */
2883 line_ga.ga_len -= (bcount + 1) / 2;
2884 pend -= (bcount + 1) / 2;
2885 pend[-1] = '\n';
2886 }
2887
2888 if ((bcount & 1) == 0)
2889 {
2890 --line_ga.ga_len;
2891 --pend;
2892 *pend = NUL;
2893 break;
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 }
2897
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 --no_mapping;
2899 --allow_keys;
2900
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /* make following messages go to the next line */
2902 msg_didout = FALSE;
2903 msg_col = 0;
2904 if (msg_row < Rows - 1)
2905 ++msg_row;
2906 emsg_on_display = FALSE; /* don't want ui_delay() */
2907
2908 if (got_int)
2909 ga_clear(&line_ga);
2910
2911 return (char_u *)line_ga.ga_data;
2912}
2913
Bram Moolenaare344bea2005-09-01 20:46:49 +00002914# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2915 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916/*
2917 * Return TRUE if ccline.overstrike is on.
2918 */
2919 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002920cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 return ccline.overstrike;
2923}
2924
2925/*
2926 * Return TRUE if the cursor is at the end of the cmdline.
2927 */
2928 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002929cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
2931 return (ccline.cmdpos >= ccline.cmdlen);
2932}
2933#endif
2934
Bram Moolenaar9372a112005-12-06 19:59:18 +00002935#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936/*
2937 * Return the virtual column number at the current cursor position.
2938 * This is used by the IM code to obtain the start of the preedit string.
2939 */
2940 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002941cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2944 return MAXCOL;
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (has_mbyte)
2947 {
2948 colnr_T col;
2949 int i = 0;
2950
2951 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002952 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 return col;
2955 }
2956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 return ccline.cmdpos;
2958}
2959#endif
2960
2961#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2962/*
2963 * If part of the command line is an IM preedit string, redraw it with
2964 * IM feedback attributes. The cursor position is restored after drawing.
2965 */
2966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002967redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 if ((State & CMDLINE)
2970 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002971 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 && !p_imdisable
2973 && im_is_preediting())
2974 {
2975 int cmdpos = 0;
2976 int cmdspos;
2977 int old_row;
2978 int old_col;
2979 colnr_T col;
2980
2981 old_row = msg_row;
2982 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002983 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 if (has_mbyte)
2986 {
2987 for (col = 0; col < preedit_start_col
2988 && cmdpos < ccline.cmdlen; ++col)
2989 {
2990 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002991 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 }
2994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 cmdspos += preedit_start_col;
2997 cmdpos += preedit_start_col;
2998 }
2999
3000 msg_row = cmdline_row + (cmdspos / (int)Columns);
3001 msg_col = cmdspos % (int)Columns;
3002 if (msg_row >= Rows)
3003 msg_row = Rows - 1;
3004
3005 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3006 {
3007 int char_len;
3008 int char_attr;
3009
3010 char_attr = im_get_feedback_attr(col);
3011 if (char_attr < 0)
3012 break; /* end of preedit string */
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003015 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 char_len = 1;
3018
3019 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3020 cmdpos += char_len;
3021 }
3022
3023 msg_row = old_row;
3024 msg_col = old_col;
3025 }
3026}
3027#endif /* FEAT_XIM && FEAT_GUI_GTK */
3028
3029/*
3030 * Allocate a new command line buffer.
3031 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 */
3033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003034alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 /*
3037 * give some extra space to avoid having to allocate all the time
3038 */
3039 if (len < 80)
3040 len = 100;
3041 else
3042 len += 20;
3043
3044 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3045 ccline.cmdbufflen = len;
3046}
3047
3048/*
3049 * Re-allocate the command line to length len + something extra.
3050 * return FAIL for failure, OK otherwise
3051 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 char_u *p;
3056
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003057 if (len < ccline.cmdbufflen)
3058 return OK; /* no need to resize */
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 p = ccline.cmdbuff;
3061 alloc_cmdbuff(len); /* will get some more */
3062 if (ccline.cmdbuff == NULL) /* out of memory */
3063 {
3064 ccline.cmdbuff = p; /* keep the old one */
3065 return FAIL;
3066 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003067 /* There isn't always a NUL after the command, but it may need to be
3068 * there, thus copy up to the NUL and add a NUL. */
3069 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3070 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003072
3073 if (ccline.xpc != NULL
3074 && ccline.xpc->xp_pattern != NULL
3075 && ccline.xpc->xp_context != EXPAND_NOTHING
3076 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3077 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003078 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003079
3080 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3081 * to point into the newly allocated memory. */
3082 if (i >= 0 && i <= ccline.cmdlen)
3083 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3084 }
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 return OK;
3087}
3088
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003089#if defined(FEAT_ARABIC) || defined(PROTO)
3090static char_u *arshape_buf = NULL;
3091
3092# if defined(EXITFREE) || defined(PROTO)
3093 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003094free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003095{
3096 vim_free(arshape_buf);
3097}
3098# endif
3099#endif
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101/*
3102 * Draw part of the cmdline at the current cursor position. But draw stars
3103 * when cmdline_star is TRUE.
3104 */
3105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003106draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3109 int i;
3110
3111 if (cmdline_star > 0)
3112 for (i = 0; i < len; ++i)
3113 {
3114 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003116 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118 else
3119#endif
3120#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003121 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 static int buflen = 0;
3124 char_u *p;
3125 int j;
3126 int newlen = 0;
3127 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003128 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 int prev_c = 0;
3130 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003131 int u8c;
3132 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /*
3136 * Do arabic shaping into a temporary buffer. This is very
3137 * inefficient!
3138 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003139 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141 /* Re-allocate the buffer. We keep it around to avoid a lot of
3142 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003144 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003145 arshape_buf = alloc(buflen);
3146 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 return; /* out of memory */
3148 }
3149
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003150 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3151 {
3152 /* Prepend a space to draw the leading composing char on. */
3153 arshape_buf[0] = ' ';
3154 newlen = 1;
3155 }
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 for (j = start; j < start + len; j += mb_l)
3158 {
3159 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003160 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003161 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (ARABIC_CHAR(u8c))
3163 {
3164 /* Do Arabic shaping. */
3165 if (cmdmsg_rl)
3166 {
3167 /* displaying from right to left */
3168 pc = prev_c;
3169 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003170 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (j + mb_l >= start + len)
3172 nc = NUL;
3173 else
3174 nc = utf_ptr2char(p + mb_l);
3175 }
3176 else
3177 {
3178 /* displaying from left to right */
3179 if (j + mb_l >= start + len)
3180 pc = NUL;
3181 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003182 {
3183 int pcc[MAX_MCO];
3184
3185 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003187 pc1 = pcc[0];
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 nc = prev_c;
3190 }
3191 prev_c = u8c;
3192
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003193 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003196 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003198 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3199 if (u8cc[1] != 0)
3200 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203 }
3204 else
3205 {
3206 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 newlen += mb_l;
3209 }
3210 }
3211
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
3214 else
3215#endif
3216 msg_outtrans_len(ccline.cmdbuff + start, len);
3217}
3218
3219/*
3220 * Put a character on the command line. Shifts the following text to the
3221 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3222 * "c" must be printable (fit in one display cell)!
3223 */
3224 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003225putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 if (cmd_silent)
3228 return;
3229 msg_no_more = TRUE;
3230 msg_putchar(c);
3231 if (shift)
3232 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3233 msg_no_more = FALSE;
3234 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003235 extra_char = c;
3236 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237}
3238
3239/*
3240 * Undo a putcmdline(c, FALSE).
3241 */
3242 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003243unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 if (cmd_silent)
3246 return;
3247 msg_no_more = TRUE;
3248 if (ccline.cmdlen == ccline.cmdpos)
3249 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003250 else if (has_mbyte)
3251 draw_cmdline(ccline.cmdpos,
3252 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 else
3254 draw_cmdline(ccline.cmdpos, 1);
3255 msg_no_more = FALSE;
3256 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003257 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258}
3259
3260/*
3261 * Put the given string, of the given length, onto the command line.
3262 * If len is -1, then STRLEN() is used to calculate the length.
3263 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3264 * part will be redrawn, otherwise it will not. If this function is called
3265 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3266 * called afterwards.
3267 */
3268 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003269put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 int retval;
3272 int i;
3273 int m;
3274 int c;
3275
3276 if (len < 0)
3277 len = (int)STRLEN(str);
3278
3279 /* Check if ccline.cmdbuff needs to be longer */
3280 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003281 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
3283 retval = OK;
3284 if (retval == OK)
3285 {
3286 if (!ccline.overstrike)
3287 {
3288 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3289 ccline.cmdbuff + ccline.cmdpos,
3290 (size_t)(ccline.cmdlen - ccline.cmdpos));
3291 ccline.cmdlen += len;
3292 }
3293 else
3294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (has_mbyte)
3296 {
3297 /* Count nr of characters in the new string. */
3298 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003299 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 ++m;
3301 /* Count nr of bytes in cmdline that are overwritten by these
3302 * characters. */
3303 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003304 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 --m;
3306 if (i < ccline.cmdlen)
3307 {
3308 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3309 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3310 ccline.cmdlen += ccline.cmdpos + len - i;
3311 }
3312 else
3313 ccline.cmdlen = ccline.cmdpos + len;
3314 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003315 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 ccline.cmdlen = ccline.cmdpos + len;
3317 }
3318 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3319 ccline.cmdbuff[ccline.cmdlen] = NUL;
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (enc_utf8)
3322 {
3323 /* When the inserted text starts with a composing character,
3324 * backup to the character before it. There could be two of them.
3325 */
3326 i = 0;
3327 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3328 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3329 {
3330 i = (*mb_head_off)(ccline.cmdbuff,
3331 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3332 ccline.cmdpos -= i;
3333 len += i;
3334 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3335 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003336#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3338 {
3339 /* Check the previous character for Arabic combining pair. */
3340 i = (*mb_head_off)(ccline.cmdbuff,
3341 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3342 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3343 + ccline.cmdpos - i), c))
3344 {
3345 ccline.cmdpos -= i;
3346 len += i;
3347 }
3348 else
3349 i = 0;
3350 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (i != 0)
3353 {
3354 /* Also backup the cursor position. */
3355 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3356 ccline.cmdspos -= i;
3357 msg_col -= i;
3358 if (msg_col < 0)
3359 {
3360 msg_col += Columns;
3361 --msg_row;
3362 }
3363 }
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 if (redraw && !cmd_silent)
3367 {
3368 msg_no_more = TRUE;
3369 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003370 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3372 /* Avoid clearing the rest of the line too often. */
3373 if (cmdline_row != i || ccline.overstrike)
3374 msg_clr_eos();
3375 msg_no_more = FALSE;
3376 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003377 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003379 m = Columns * Rows;
3380 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003382 }
3383 else
3384 m = MAXCOL;
3385 for (i = 0; i < len; ++i)
3386 {
3387 c = cmdline_charsize(ccline.cmdpos);
3388 /* count ">" for a double-wide char that doesn't fit. */
3389 if (has_mbyte)
3390 correct_cmdspos(ccline.cmdpos, c);
3391 /* Stop cursor at the end of the screen, but do increment the
3392 * insert position, so that entering a very long command
3393 * works, even though you can't see it. */
3394 if (ccline.cmdspos + c < m)
3395 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003396
Bram Moolenaar14184a32019-02-16 15:10:30 +01003397 if (has_mbyte)
3398 {
3399 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3400 if (c > len - i - 1)
3401 c = len - i - 1;
3402 ccline.cmdpos += c;
3403 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003405 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407 }
3408 if (redraw)
3409 msg_check();
3410 return retval;
3411}
3412
Bram Moolenaar66b51422019-08-18 21:44:12 +02003413static cmdline_info_T prev_ccline;
3414static int prev_ccline_used = FALSE;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003415
3416/*
3417 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3418 * and overwrite it. But get_cmdline_str() may need it, thus make it
3419 * available globally in prev_ccline.
3420 */
3421 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422save_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003423{
3424 if (!prev_ccline_used)
3425 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02003426 vim_memset(&prev_ccline, 0, sizeof(cmdline_info_T));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003427 prev_ccline_used = TRUE;
3428 }
3429 *ccp = prev_ccline;
3430 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003431 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003432}
3433
3434/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003435 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003436 */
3437 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +02003438restore_cmdline(cmdline_info_T *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003439{
3440 ccline = prev_ccline;
3441 prev_ccline = *ccp;
3442}
3443
Bram Moolenaar8299df92004-07-10 09:47:34 +00003444/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003445 * Paste a yank register into the command line.
3446 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003447 * insert_reg() can't be used here, because special characters from the
3448 * register contents will be interpreted as commands.
3449 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003450 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003451 */
3452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453cmdline_paste(
3454 int regname,
3455 int literally, /* Insert text literally instead of "as typed" */
3456 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003457{
3458 long i;
3459 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003460 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003461 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003462
3463 /* check for valid regname; also accept special characters for CTRL-R in
3464 * the command line */
3465 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003466 && regname != Ctrl_A && regname != Ctrl_L
3467 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003468 return FAIL;
3469
3470 /* A register containing CTRL-R can cause an endless loop. Allow using
3471 * CTRL-C to break the loop. */
3472 line_breakcheck();
3473 if (got_int)
3474 return FAIL;
3475
3476#ifdef FEAT_CLIPBOARD
3477 regname = may_get_selection(regname);
3478#endif
3479
Bram Moolenaar438d1762018-09-30 17:11:48 +02003480 // Need to set "textlock" to avoid nasty things like going to another
3481 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003482 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003483 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003484 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003485
3486 if (i)
3487 {
3488 /* Got the value of a special register in "arg". */
3489 if (arg == NULL)
3490 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003491
3492 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3493 * part of the word. */
3494 p = arg;
3495 if (p_is && regname == Ctrl_W)
3496 {
3497 char_u *w;
3498 int len;
3499
3500 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003501 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003502 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003503 if (has_mbyte)
3504 {
3505 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3506 if (!vim_iswordc(mb_ptr2char(w - len)))
3507 break;
3508 w -= len;
3509 }
3510 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003511 {
3512 if (!vim_iswordc(w[-1]))
3513 break;
3514 --w;
3515 }
3516 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003517 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003518 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3519 p += len;
3520 }
3521
3522 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003523 if (allocated)
3524 vim_free(arg);
3525 return OK;
3526 }
3527
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003528 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003529}
3530
3531/*
3532 * Put a string on the command line.
3533 * When "literally" is TRUE, insert literally.
3534 * When "literally" is FALSE, insert as typed, but don't leave the command
3535 * line.
3536 */
3537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003538cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539{
3540 int c, cv;
3541
3542 if (literally)
3543 put_on_cmdline(s, -1, TRUE);
3544 else
3545 while (*s != NUL)
3546 {
3547 cv = *s;
3548 if (cv == Ctrl_V && s[1])
3549 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003551 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003552 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003553 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003554 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3555 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003556#ifdef UNIX
3557 || c == intr_char
3558#endif
3559 || (c == Ctrl_BSL && *s == Ctrl_N))
3560 stuffcharReadbuff(Ctrl_V);
3561 stuffcharReadbuff(c);
3562 }
3563}
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565#ifdef FEAT_WILDMENU
3566/*
3567 * Delete characters on the command line, from "from" to the current
3568 * position.
3569 */
3570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3574 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3575 ccline.cmdlen -= ccline.cmdpos - from;
3576 ccline.cmdpos = from;
3577}
3578#endif
3579
3580/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003581 * This function is called when the screen size changes and with incremental
3582 * search and in other situations where the command line may have been
3583 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 */
3585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003586redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003588 redrawcmdline_ex(TRUE);
3589}
3590
3591 void
3592redrawcmdline_ex(int do_compute_cmdrow)
3593{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (cmd_silent)
3595 return;
3596 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003597 if (do_compute_cmdrow)
3598 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 redrawcmd();
3600 cursorcmd();
3601}
3602
3603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003604redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
3606 int i;
3607
3608 if (cmd_silent)
3609 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003610 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 msg_putchar(ccline.cmdfirstc);
3612 if (ccline.cmdprompt != NULL)
3613 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003614 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3616 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003617 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 --ccline.cmdindent;
3619 }
3620 else
3621 for (i = ccline.cmdindent; i > 0; --i)
3622 msg_putchar(' ');
3623}
3624
3625/*
3626 * Redraw what is currently on the command line.
3627 */
3628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003629redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 if (cmd_silent)
3632 return;
3633
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003634 /* when 'incsearch' is set there may be no command line while redrawing */
3635 if (ccline.cmdbuff == NULL)
3636 {
3637 windgoto(cmdline_row, 0);
3638 msg_clr_eos();
3639 return;
3640 }
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 msg_start();
3643 redrawcmdprompt();
3644
3645 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3646 msg_no_more = TRUE;
3647 draw_cmdline(0, ccline.cmdlen);
3648 msg_clr_eos();
3649 msg_no_more = FALSE;
3650
3651 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003652 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003653 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /*
3656 * An emsg() before may have set msg_scroll. This is used in normal mode,
3657 * in cmdline mode we can reset them now.
3658 */
3659 msg_scroll = FALSE; /* next message overwrites cmdline */
3660
3661 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3662 * in cmdline mode */
3663 skip_redraw = FALSE;
3664}
3665
3666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003667compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003669 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 cmdline_row = Rows - 1;
3671 else
3672 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003673 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674}
3675
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003677cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 if (cmd_silent)
3680 return;
3681
3682#ifdef FEAT_RIGHTLEFT
3683 if (cmdmsg_rl)
3684 {
3685 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3686 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3687 if (msg_row <= 0)
3688 msg_row = Rows - 1;
3689 }
3690 else
3691#endif
3692 {
3693 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3694 msg_col = ccline.cmdspos % (int)Columns;
3695 if (msg_row >= Rows)
3696 msg_row = Rows - 1;
3697 }
3698
3699 windgoto(msg_row, msg_col);
3700#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003701 if (p_imst == IM_ON_THE_SPOT)
3702 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#endif
3704#ifdef MCH_CURSOR_SHAPE
3705 mch_update_cursor();
3706#endif
3707}
3708
3709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003710gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 msg_start();
3713#ifdef FEAT_RIGHTLEFT
3714 if (cmdmsg_rl)
3715 msg_col = Columns - 1;
3716 else
3717#endif
3718 msg_col = 0; /* always start in column 0 */
3719 if (clr) /* clear the bottom line(s) */
3720 msg_clr_eos(); /* will reset clear_cmdline */
3721 windgoto(cmdline_row, 0);
3722}
3723
3724/*
3725 * Check the word in front of the cursor for an abbreviation.
3726 * Called when the non-id character "c" has been entered.
3727 * When an abbreviation is recognized it is removed from the text with
3728 * backspaces and the replacement string is inserted, followed by "c".
3729 */
3730 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003731ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003733 int spos = 0;
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3736 return FALSE;
3737
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003738 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3739 * Actually accepts any mark. */
3740 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3741 spos++;
3742 if (ccline.cmdlen - spos > 5
3743 && ccline.cmdbuff[spos] == '\''
3744 && ccline.cmdbuff[spos + 2] == ','
3745 && ccline.cmdbuff[spos + 3] == '\'')
3746 spos += 5;
3747 else
3748 /* check abbreviation from the beginning of the commandline */
3749 spos = 0;
3750
3751 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752}
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003755 * Escape special characters in "fname" for when used as a file name argument
3756 * after a Vim command, or, when "shell" is non-zero, a shell command.
3757 * Returns the result in allocated memory.
3758 */
3759 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003760vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003761{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003762 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003763#ifdef BACKSLASH_IN_FILENAME
3764 char_u buf[20];
3765 int j = 0;
3766
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003767 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003768 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003769 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003770 buf[j++] = *p;
3771 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003772 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003773#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003774 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3775 if (shell && csh_like_shell() && p != NULL)
3776 {
3777 char_u *s;
3778
3779 /* For csh and similar shells need to put two backslashes before '!'.
3780 * One is taken by Vim, one by the shell. */
3781 s = vim_strsave_escaped(p, (char_u *)"!");
3782 vim_free(p);
3783 p = s;
3784 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003785#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003786
3787 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3788 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003789 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003790 escape_fname(&p);
3791
3792 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003793}
3794
3795/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003796 * Put a backslash before the file name in "pp", which is in allocated memory.
3797 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003799escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003800{
3801 char_u *p;
3802
Bram Moolenaar964b3742019-05-24 18:54:09 +02003803 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00003804 if (p != NULL)
3805 {
3806 p[0] = '\\';
3807 STRCPY(p + 1, *pp);
3808 vim_free(*pp);
3809 *pp = p;
3810 }
3811}
3812
3813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 * For each file name in files[num_files]:
3815 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3816 */
3817 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003818tilde_replace(
3819 char_u *orig_pat,
3820 int num_files,
3821 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 int i;
3824 char_u *p;
3825
3826 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3827 {
3828 for (i = 0; i < num_files; ++i)
3829 {
3830 p = home_replace_save(NULL, files[i]);
3831 if (p != NULL)
3832 {
3833 vim_free(files[i]);
3834 files[i] = p;
3835 }
3836 }
3837 }
3838}
3839
3840/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003841 * Get a pointer to the current command line info.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003843 cmdline_info_T *
3844get_cmdline_info(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003846 return &ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847}
3848
Bram Moolenaar064154c2016-03-19 22:50:43 +01003849#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003850/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02003851 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003852 * ccline and put the previous value in prev_ccline.
3853 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02003854 static cmdline_info_T *
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003855get_ccline_ptr(void)
3856{
3857 if ((State & CMDLINE) == 0)
3858 return NULL;
3859 if (ccline.cmdbuff != NULL)
3860 return &ccline;
3861 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
3862 return &prev_ccline;
3863 return NULL;
3864}
Bram Moolenaar064154c2016-03-19 22:50:43 +01003865#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003866
Bram Moolenaar064154c2016-03-19 22:50:43 +01003867#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003868/*
3869 * Get the current command line in allocated memory.
3870 * Only works when the command line is being edited.
3871 * Returns NULL when something is wrong.
3872 */
3873 char_u *
3874get_cmdline_str(void)
3875{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003876 cmdline_info_T *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003877
Bram Moolenaaree91c332018-09-25 22:27:35 +02003878 if (cmdline_star > 0)
3879 return NULL;
3880 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003881 if (p == NULL)
3882 return NULL;
3883 return vim_strnsave(p->cmdbuff, p->cmdlen);
3884}
3885
3886/*
3887 * Get the current command line position, counted in bytes.
3888 * Zero is the first position.
3889 * Only works when the command line is being edited.
3890 * Returns -1 when something is wrong.
3891 */
3892 int
3893get_cmdline_pos(void)
3894{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003895 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003896
3897 if (p == NULL)
3898 return -1;
3899 return p->cmdpos;
3900}
3901
3902/*
3903 * Set the command line byte position to "pos". Zero is the first position.
3904 * Only works when the command line is being edited.
3905 * Returns 1 when failed, 0 when OK.
3906 */
3907 int
3908set_cmdline_pos(
3909 int pos)
3910{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003911 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003912
3913 if (p == NULL)
3914 return 1;
3915
3916 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
3917 * changed the command line. */
3918 if (pos < 0)
3919 new_cmdpos = 0;
3920 else
3921 new_cmdpos = pos;
3922 return 0;
3923}
3924#endif
3925
3926#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
3927/*
3928 * Get the current command-line type.
3929 * Returns ':' or '/' or '?' or '@' or '>' or '-'
3930 * Only works when the command line is being edited.
3931 * Returns NUL when something is wrong.
3932 */
3933 int
3934get_cmdline_type(void)
3935{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003936 cmdline_info_T *p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003937
3938 if (p == NULL)
3939 return NUL;
3940 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01003941 return
3942# ifdef FEAT_EVAL
3943 (p->input_fn) ? '@' :
3944# endif
3945 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01003946 return p->cmdfirstc;
3947}
3948#endif
3949
Bram Moolenaard7663c22019-08-06 21:59:57 +02003950/*
3951 * Return the first character of the current command line.
3952 */
3953 int
3954get_cmdline_firstc(void)
3955{
3956 return ccline.cmdfirstc;
3957}
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959/*
3960 * Get indices "num1,num2" that specify a range within a list (not a range of
3961 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
3962 * Returns OK if parsed successfully, otherwise FAIL.
3963 */
3964 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003965get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 int len;
3968 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003969 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971 *str = skipwhite(*str);
3972 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
3973 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003974 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 *str += len;
3976 *num1 = (int)num;
3977 first = TRUE;
3978 }
3979 *str = skipwhite(*str);
3980 if (**str == ',') /* parse "to" part of range */
3981 {
3982 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003983 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (len > 0)
3985 {
3986 *num2 = (int)num;
3987 *str = skipwhite(*str + len);
3988 }
3989 else if (!first) /* no number given at all */
3990 return FAIL;
3991 }
3992 else if (first) /* only one number given */
3993 *num2 = *num1;
3994 return OK;
3995}
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#if defined(FEAT_CMDWIN) || defined(PROTO)
3998/*
3999 * Open a window on the current command line and history. Allow editing in
4000 * the window. Returns when the window is closed.
4001 * Returns:
4002 * CR if the command is to be executed
4003 * Ctrl_C if it is to be abandoned
4004 * K_IGNORE if editing continues
4005 */
4006 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004007open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004009 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004011 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 win_T *wp;
4013 int i;
4014 linenr_T lnum;
4015 int histtype;
4016 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 int save_restart_edit = restart_edit;
4018 int save_State = State;
4019 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00004020#ifdef FEAT_RIGHTLEFT
4021 int save_cmdmsg_rl = cmdmsg_rl;
4022#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004023#ifdef FEAT_FOLDING
4024 int save_KeyTyped;
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /* Can't do this recursively. Can't do it when typing a password. */
4028 if (cmdwin_type != 0
4029# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
4030 || cmdline_star > 0
4031# endif
4032 )
4033 {
4034 beep_flush();
4035 return K_IGNORE;
4036 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004037 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 /* Save current window sizes. */
4040 win_size_save(&winsizes);
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004043 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004044
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01004045#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004046 // When using completion in Insert mode with <C-R>=<C-F> one can open the
4047 // command line window, but we don't want the popup menu then.
4048 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01004049#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01004050
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004051 /* don't use a new tab page */
4052 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02004053 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 /* Create a window for the command-line buffer. */
4056 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
4057 {
4058 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004059 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 return K_IGNORE;
4061 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004062 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004065 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00004066 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00004069#ifdef FEAT_FOLDING
4070 curwin->w_p_fen = FALSE;
4071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00004073 curwin->w_p_rl = cmdmsg_rl;
4074 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02004076 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004079 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02004080 /* But don't allow switching to another buffer. */
4081 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
Bram Moolenaar46152342005-09-07 21:18:43 +00004083 /* Showing the prompt may have set need_wait_return, reset it. */
4084 need_wait_return = FALSE;
4085
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00004086 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
4088 {
4089 if (p_wc == TAB)
4090 {
4091 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
4092 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
4093 }
4094 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
4095 }
Bram Moolenaar18141832017-06-25 21:17:25 +02004096 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004098 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
4099 * sets 'textwidth' to 78). */
4100 curbuf->b_p_tw = 0;
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /* Fill the buffer with the history. */
4103 init_history();
Bram Moolenaard7663c22019-08-06 21:59:57 +02004104 if (get_hislen() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004106 i = *get_hisidx(histtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (i >= 0)
4108 {
4109 lnum = 0;
4110 do
4111 {
Bram Moolenaard7663c22019-08-06 21:59:57 +02004112 if (++i == get_hislen())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 i = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +02004114 if (get_histentry(histtype)[i].hisstr != NULL)
4115 ml_append(lnum++, get_histentry(histtype)[i].hisstr,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 (colnr_T)0, FALSE);
4117 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02004118 while (i != *get_hisidx(histtype));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
4122 /* Replace the empty last line with the current command-line and put the
4123 * cursor there. */
4124 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
4125 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4126 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00004127 changed_line_abv_curs();
4128 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004129 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /* No Ex mode here! */
4132 exmode_active = 0;
4133
4134 State = NORMAL;
4135# ifdef FEAT_MOUSE
4136 setmouse();
4137# endif
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004140 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00004141 if (restart_edit != 0) /* autocmd with ":startinsert" */
4142 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144 i = RedrawingDisabled;
4145 RedrawingDisabled = 0;
4146
4147 /*
4148 * Call the main loop until <CR> or CTRL-C is typed.
4149 */
4150 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004151 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 RedrawingDisabled = i;
4154
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004155# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004156 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004157# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02004160 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004161
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004162# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02004163 /* Restore KeyTyped in case it is modified by autocommands */
4164 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165# endif
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 exmode_active = save_exmode;
4169
Bram Moolenaarf9821062008-06-20 16:31:07 +00004170 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004172 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
4174 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 else
4178 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004179# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 /* autocmds may abort script processing */
4181 if (aborting() && cmdwin_result != K_IGNORE)
4182 cmdwin_result = Ctrl_C;
4183# endif
4184 /* Set the new command line from the cmdline buffer. */
4185 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00004186 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar46152342005-09-07 21:18:43 +00004188 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
4189
4190 if (histtype == HIST_CMD)
4191 {
4192 /* Execute the command directly. */
4193 ccline.cmdbuff = vim_strsave((char_u *)p);
4194 cmdwin_result = CAR;
4195 }
4196 else
4197 {
4198 /* First need to cancel what we were doing. */
4199 ccline.cmdbuff = NULL;
4200 stuffcharReadbuff(':');
4201 stuffReadbuff((char_u *)p);
4202 stuffcharReadbuff(CAR);
4203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 else if (cmdwin_result == K_XF2) /* :qa typed */
4206 {
4207 ccline.cmdbuff = vim_strsave((char_u *)"qa");
4208 cmdwin_result = CAR;
4209 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004210 else if (cmdwin_result == Ctrl_C)
4211 {
4212 /* :q or :close, don't execute any command
4213 * and don't modify the cmd window. */
4214 ccline.cmdbuff = NULL;
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 else
4217 ccline.cmdbuff = vim_strsave(ml_get_curline());
4218 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004219 {
4220 ccline.cmdbuff = vim_strsave((char_u *)"");
4221 ccline.cmdlen = 0;
4222 ccline.cmdbufflen = 1;
4223 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02004225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 else
4227 {
4228 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
4229 ccline.cmdbufflen = ccline.cmdlen + 1;
4230 ccline.cmdpos = curwin->w_cursor.col;
4231 if (ccline.cmdpos > ccline.cmdlen)
4232 ccline.cmdpos = ccline.cmdlen;
4233 if (cmdwin_result == K_IGNORE)
4234 {
4235 set_cmdspos_cursor();
4236 redrawcmd();
4237 }
4238 }
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004241 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02004242# ifdef FEAT_CONCEAL
4243 /* Avoid command-line window first character being concealed. */
4244 curwin->w_p_cole = 0;
4245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004247 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 win_goto(old_curwin);
4249 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01004250
4251 /* win_close() may have already wiped the buffer when 'bh' is
4252 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004253 if (bufref_valid(&bufref))
4254 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 /* Restore window sizes. */
4257 win_size_restore(&winsizes);
4258
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004259 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261
4262 ga_clear(&winsizes);
4263 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00004264# ifdef FEAT_RIGHTLEFT
4265 cmdmsg_rl = save_cmdmsg_rl;
4266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 State = save_State;
4269# ifdef FEAT_MOUSE
4270 setmouse();
4271# endif
4272
4273 return cmdwin_result;
4274}
4275#endif /* FEAT_CMDWIN */
4276
4277/*
4278 * Used for commands that either take a simple command string argument, or:
4279 * cmd << endmarker
4280 * {script}
4281 * endmarker
4282 * Returns a pointer to allocated memory with {script} or NULL.
4283 */
4284 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004285script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
4287 char_u *theline;
4288 char *end_pattern = NULL;
4289 char dot[] = ".";
4290 garray_T ga;
4291
4292 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
4293 return NULL;
4294
4295 ga_init2(&ga, 1, 0x400);
4296
4297 if (cmd[2] != NUL)
4298 end_pattern = (char *)skipwhite(cmd + 2);
4299 else
4300 end_pattern = dot;
4301
4302 for (;;)
4303 {
4304 theline = eap->getline(
4305#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00004306 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02004308 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004311 {
4312 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00004314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 ga_concat(&ga, theline);
4317 ga_append(&ga, '\n');
4318 vim_free(theline);
4319 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00004320 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 return (char_u *)ga.ga_data;
4323}